/* IEEE754 floating point arithmetic1* double precision: common utilities2*/3/*4* MIPS floating point support5* Copyright (C) 1994-2000 Algorithmics Ltd.6*7* ########################################################################8*9* This program is free software; you can distribute it and/or modify it10* under the terms of the GNU General Public License (Version 2) as11* published by the Free Software Foundation.12*13* This program is distributed in the hope it will be useful, but WITHOUT14* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or15* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License16* for more details.17*18* You should have received a copy of the GNU General Public License along19* with this program; if not, write to the Free Software Foundation, Inc.,20* 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.21*22* ########################################################################23*/242526#include "ieee754dp.h"2728s64 ieee754dp_tlong(ieee754dp x)29{30COMPXDP;3132CLEARCX;3334EXPLODEXDP;35FLUSHXDP;3637switch (xc) {38case IEEE754_CLASS_SNAN:39case IEEE754_CLASS_QNAN:40case IEEE754_CLASS_INF:41SETCX(IEEE754_INVALID_OPERATION);42return ieee754di_xcpt(ieee754di_indef(), "dp_tlong", x);43case IEEE754_CLASS_ZERO:44return 0;45case IEEE754_CLASS_DNORM:46case IEEE754_CLASS_NORM:47break;48}49if (xe >= 63) {50/* look for valid corner case */51if (xe == 63 && xs && xm == DP_HIDDEN_BIT)52return -0x8000000000000000LL;53/* Set invalid. We will only use overflow for floating54point overflow */55SETCX(IEEE754_INVALID_OPERATION);56return ieee754di_xcpt(ieee754di_indef(), "dp_tlong", x);57}58/* oh gawd */59if (xe > DP_MBITS) {60xm <<= xe - DP_MBITS;61} else if (xe < DP_MBITS) {62u64 residue;63int round;64int sticky;65int odd;6667if (xe < -1) {68residue = xm;69round = 0;70sticky = residue != 0;71xm = 0;72} else {73/* Shifting a u64 64 times does not work,74* so we do it in two steps. Be aware that xe75* may be -1 */76residue = xm << (xe + 1);77residue <<= 63 - DP_MBITS;78round = (residue >> 63) != 0;79sticky = (residue << 1) != 0;80xm >>= DP_MBITS - xe;81}82odd = (xm & 0x1) != 0x0;83switch (ieee754_csr.rm) {84case IEEE754_RN:85if (round && (sticky || odd))86xm++;87break;88case IEEE754_RZ:89break;90case IEEE754_RU: /* toward +Infinity */91if ((round || sticky) && !xs)92xm++;93break;94case IEEE754_RD: /* toward -Infinity */95if ((round || sticky) && xs)96xm++;97break;98}99if ((xm >> 63) != 0) {100/* This can happen after rounding */101SETCX(IEEE754_INVALID_OPERATION);102return ieee754di_xcpt(ieee754di_indef(), "dp_tlong", x);103}104if (round || sticky)105SETCX(IEEE754_INEXACT);106}107if (xs)108return -xm;109else110return xm;111}112113114u64 ieee754dp_tulong(ieee754dp x)115{116ieee754dp hb = ieee754dp_1e63();117118/* what if x < 0 ?? */119if (ieee754dp_lt(x, hb))120return (u64) ieee754dp_tlong(x);121122return (u64) ieee754dp_tlong(ieee754dp_sub(x, hb)) |123(1ULL << 63);124}125126127