Path: blob/master/arch/parisc/math-emu/cnv_float.h
10817 views
/*1* Linux/PA-RISC Project (http://www.parisc-linux.org/)2*3* Floating-point emulation code4* Copyright (C) 2001 Hewlett-Packard (Paul Bame) <[email protected]>5*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License as published by8* the Free Software Foundation; either version 2, or (at your option)9* any later version.10*11* This program is distributed in the hope that it will be useful,12* but WITHOUT ANY WARRANTY; without even the implied warranty of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14* GNU General Public License for more details.15*16* You should have received a copy of the GNU General Public License17* along with this program; if not, write to the Free Software18* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA19*/2021#ifdef __NO_PA_HDRS22PA header file -- do not include this header file for non-PA builds.23#endif2425/*26* Some more constants27*/28#define SGL_FX_MAX_EXP 3029#define DBL_FX_MAX_EXP 6230#define QUAD_FX_MAX_EXP 1263132#define Dintp1(object) (object)33#define Dintp2(object) (object)3435#define Duintp1(object) (object)36#define Duintp2(object) (object)3738#define Qintp0(object) (object)39#define Qintp1(object) (object)40#define Qintp2(object) (object)41#define Qintp3(object) (object)424344/*45* These macros will be used specifically by the convert instructions.46*47*48* Single format macros49*/5051#define Sgl_to_dbl_exponent(src_exponent,dest) \52Deposit_dexponent(dest,src_exponent+(DBL_BIAS-SGL_BIAS))5354#define Sgl_to_dbl_mantissa(src_mantissa,destA,destB) \55Deposit_dmantissap1(destA,src_mantissa>>3); \56Dmantissap2(destB) = src_mantissa << 295758#define Sgl_isinexact_to_fix(sgl_value,exponent) \59((exponent < (SGL_P - 1)) ? \60(Sall(sgl_value) << (SGL_EXP_LENGTH + 1 + exponent)) : FALSE)6162#define Int_isinexact_to_sgl(int_value) (int_value << 33 - SGL_EXP_LENGTH)6364#define Sgl_roundnearest_from_int(int_value,sgl_value) \65if (int_value & 1<<(SGL_EXP_LENGTH - 2)) /* round bit */ \66if ((int_value << 34 - SGL_EXP_LENGTH) || Slow(sgl_value)) \67Sall(sgl_value)++6869#define Dint_isinexact_to_sgl(dint_valueA,dint_valueB) \70((Dintp1(dint_valueA) << 33 - SGL_EXP_LENGTH) || Dintp2(dint_valueB))7172#define Sgl_roundnearest_from_dint(dint_valueA,dint_valueB,sgl_value) \73if (Dintp1(dint_valueA) & 1<<(SGL_EXP_LENGTH - 2)) \74if ((Dintp1(dint_valueA) << 34 - SGL_EXP_LENGTH) || \75Dintp2(dint_valueB) || Slow(sgl_value)) Sall(sgl_value)++7677#define Dint_isinexact_to_dbl(dint_value) \78(Dintp2(dint_value) << 33 - DBL_EXP_LENGTH)7980#define Dbl_roundnearest_from_dint(dint_opndB,dbl_opndA,dbl_opndB) \81if (Dintp2(dint_opndB) & 1<<(DBL_EXP_LENGTH - 2)) \82if ((Dintp2(dint_opndB) << 34 - DBL_EXP_LENGTH) || Dlowp2(dbl_opndB)) \83if ((++Dallp2(dbl_opndB))==0) Dallp1(dbl_opndA)++8485#define Sgl_isone_roundbit(sgl_value,exponent) \86((Sall(sgl_value) << (SGL_EXP_LENGTH + 1 + exponent)) >> 31)8788#define Sgl_isone_stickybit(sgl_value,exponent) \89(exponent < (SGL_P - 2) ? \90Sall(sgl_value) << (SGL_EXP_LENGTH + 2 + exponent) : FALSE)919293/*94* Double format macros95*/9697#define Dbl_to_sgl_exponent(src_exponent,dest) \98dest = src_exponent + (SGL_BIAS - DBL_BIAS)99100#define Dbl_to_sgl_mantissa(srcA,srcB,dest,inexact,guard,sticky,odd) \101Shiftdouble(Dmantissap1(srcA),Dmantissap2(srcB),29,dest); \102guard = Dbit3p2(srcB); \103sticky = Dallp2(srcB)<<4; \104inexact = guard | sticky; \105odd = Dbit2p2(srcB)106107#define Dbl_to_sgl_denormalized(srcA,srcB,exp,dest,inexact,guard,sticky,odd,tiny) \108Deposit_dexponent(srcA,1); \109tiny = TRUE; \110if (exp >= -2) { \111if (exp == 0) { \112inexact = Dallp2(srcB) << 3; \113guard = inexact >> 31; \114sticky = inexact << 1; \115Shiftdouble(Dmantissap1(srcA),Dmantissap2(srcB),29,dest); \116odd = dest << 31; \117if (inexact) { \118switch(Rounding_mode()) { \119case ROUNDPLUS: \120if (Dbl_iszero_sign(srcA)) { \121dest++; \122if (Sgl_isone_hidden(dest)) \123tiny = FALSE; \124dest--; \125} \126break; \127case ROUNDMINUS: \128if (Dbl_isone_sign(srcA)) { \129dest++; \130if (Sgl_isone_hidden(dest)) \131tiny = FALSE; \132dest--; \133} \134break; \135case ROUNDNEAREST: \136if (guard && (sticky || odd)) { \137dest++; \138if (Sgl_isone_hidden(dest)) \139tiny = FALSE; \140dest--; \141} \142break; \143} \144} \145/* shift right by one to get correct result */ \146guard = odd; \147sticky = inexact; \148inexact |= guard; \149dest >>= 1; \150Deposit_dsign(srcA,0); \151Shiftdouble(Dallp1(srcA),Dallp2(srcB),30,dest); \152odd = dest << 31; \153} \154else { \155inexact = Dallp2(srcB) << (2 + exp); \156guard = inexact >> 31; \157sticky = inexact << 1; \158Deposit_dsign(srcA,0); \159if (exp == -2) dest = Dallp1(srcA); \160else Variable_shift_double(Dallp1(srcA),Dallp2(srcB),30-exp,dest); \161odd = dest << 31; \162} \163} \164else { \165Deposit_dsign(srcA,0); \166if (exp > (1 - SGL_P)) { \167dest = Dallp1(srcA) >> (- 2 - exp); \168inexact = Dallp1(srcA) << (34 + exp); \169guard = inexact >> 31; \170sticky = (inexact << 1) | Dallp2(srcB); \171inexact |= Dallp2(srcB); \172odd = dest << 31; \173} \174else { \175dest = 0; \176inexact = Dallp1(srcA) | Dallp2(srcB); \177if (exp == (1 - SGL_P)) { \178guard = Dhidden(srcA); \179sticky = Dmantissap1(srcA) | Dallp2(srcB); \180} \181else { \182guard = 0; \183sticky = inexact; \184} \185odd = 0; \186} \187} \188exp = 0189190#define Dbl_isinexact_to_fix(dbl_valueA,dbl_valueB,exponent) \191(exponent < (DBL_P-33) ? \192Dallp2(dbl_valueB) || Dallp1(dbl_valueA) << (DBL_EXP_LENGTH+1+exponent) : \193(exponent < (DBL_P-1) ? Dallp2(dbl_valueB) << (exponent + (33-DBL_P)) : \194FALSE))195196#define Dbl_isoverflow_to_int(exponent,dbl_valueA,dbl_valueB) \197((exponent > SGL_FX_MAX_EXP + 1) || Dsign(dbl_valueA)==0 || \198Dmantissap1(dbl_valueA)!=0 || (Dallp2(dbl_valueB)>>21)!=0 )199200#define Dbl_isone_roundbit(dbl_valueA,dbl_valueB,exponent) \201((exponent < (DBL_P - 33) ? \202Dallp1(dbl_valueA) >> ((30 - DBL_EXP_LENGTH) - exponent) : \203Dallp2(dbl_valueB) >> ((DBL_P - 2) - exponent)) & 1)204205#define Dbl_isone_stickybit(dbl_valueA,dbl_valueB,exponent) \206(exponent < (DBL_P-34) ? \207(Dallp2(dbl_valueB) || Dallp1(dbl_valueA)<<(DBL_EXP_LENGTH+2+exponent)) : \208(exponent<(DBL_P-2) ? (Dallp2(dbl_valueB) << (exponent + (34-DBL_P))) : \209FALSE))210211212/* Int macros */213214#define Int_from_sgl_mantissa(sgl_value,exponent) \215Sall(sgl_value) = \216(unsigned)(Sall(sgl_value) << SGL_EXP_LENGTH)>>(31 - exponent)217218#define Int_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent) \219Shiftdouble(Dallp1(dbl_valueA),Dallp2(dbl_valueB),22,Dallp1(dbl_valueA)); \220if (exponent < 31) Dallp1(dbl_valueA) >>= 30 - exponent; \221else Dallp1(dbl_valueA) <<= 1222223#define Int_negate(int_value) int_value = -int_value224225226/* Dint macros */227228#define Dint_from_sgl_mantissa(sgl_value,exponent,dresultA,dresultB) \229{Sall(sgl_value) <<= SGL_EXP_LENGTH; /* left-justify */ \230if (exponent <= 31) { \231Dintp1(dresultA) = 0; \232Dintp2(dresultB) = (unsigned)Sall(sgl_value) >> (31 - exponent); \233} \234else { \235Dintp1(dresultA) = Sall(sgl_value) >> (63 - exponent); \236Dintp2(dresultB) = Sall(sgl_value) << (exponent - 31); \237}}238239240#define Dint_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent,destA,destB) \241{if (exponent < 32) { \242Dintp1(destA) = 0; \243if (exponent <= 20) \244Dintp2(destB) = Dallp1(dbl_valueA) >> 20-exponent; \245else Variable_shift_double(Dallp1(dbl_valueA),Dallp2(dbl_valueB), \24652-exponent,Dintp2(destB)); \247} \248else { \249if (exponent <= 52) { \250Dintp1(destA) = Dallp1(dbl_valueA) >> 52-exponent; \251if (exponent == 52) Dintp2(destB) = Dallp2(dbl_valueB); \252else Variable_shift_double(Dallp1(dbl_valueA),Dallp2(dbl_valueB), \25352-exponent,Dintp2(destB)); \254} \255else { \256Variable_shift_double(Dallp1(dbl_valueA),Dallp2(dbl_valueB), \25784-exponent,Dintp1(destA)); \258Dintp2(destB) = Dallp2(dbl_valueB) << exponent-52; \259} \260}}261262#define Dint_setzero(dresultA,dresultB) \263Dintp1(dresultA) = 0; \264Dintp2(dresultB) = 0265266#define Dint_setone_sign(dresultA,dresultB) \267Dintp1(dresultA) = ~Dintp1(dresultA); \268if ((Dintp2(dresultB) = -Dintp2(dresultB)) == 0) Dintp1(dresultA)++269270#define Dint_set_minint(dresultA,dresultB) \271Dintp1(dresultA) = (unsigned int)1<<31; \272Dintp2(dresultB) = 0273274#define Dint_isone_lowp2(dresultB) (Dintp2(dresultB) & 01)275276#define Dint_increment(dresultA,dresultB) \277if ((++Dintp2(dresultB))==0) Dintp1(dresultA)++278279#define Dint_decrement(dresultA,dresultB) \280if ((Dintp2(dresultB)--)==0) Dintp1(dresultA)--281282#define Dint_negate(dresultA,dresultB) \283Dintp1(dresultA) = ~Dintp1(dresultA); \284if ((Dintp2(dresultB) = -Dintp2(dresultB))==0) Dintp1(dresultA)++285286#define Dint_copyfromptr(src,destA,destB) \287Dintp1(destA) = src->wd0; \288Dintp2(destB) = src->wd1289#define Dint_copytoptr(srcA,srcB,dest) \290dest->wd0 = Dintp1(srcA); \291dest->wd1 = Dintp2(srcB)292293294/* other macros */295296#define Find_ms_one_bit(value, position) \297{ \298int var; \299for (var=8; var >=1; var >>= 1) { \300if (value >> 32 - position) \301position -= var; \302else position += var; \303} \304if ((value >> 32 - position) == 0) \305position--; \306else position -= 2; \307}308309310/*311* Unsigned int macros312*/313#define Duint_copyfromptr(src,destA,destB) \314Dint_copyfromptr(src,destA,destB)315#define Duint_copytoptr(srcA,srcB,dest) \316Dint_copytoptr(srcA,srcB,dest)317318#define Suint_isinexact_to_sgl(int_value) \319(int_value << 32 - SGL_EXP_LENGTH)320321#define Sgl_roundnearest_from_suint(suint_value,sgl_value) \322if (suint_value & 1<<(SGL_EXP_LENGTH - 1)) /* round bit */ \323if ((suint_value << 33 - SGL_EXP_LENGTH) || Slow(sgl_value)) \324Sall(sgl_value)++325326#define Duint_isinexact_to_sgl(duint_valueA,duint_valueB) \327((Duintp1(duint_valueA) << 32 - SGL_EXP_LENGTH) || Duintp2(duint_valueB))328329#define Sgl_roundnearest_from_duint(duint_valueA,duint_valueB,sgl_value) \330if (Duintp1(duint_valueA) & 1<<(SGL_EXP_LENGTH - 1)) \331if ((Duintp1(duint_valueA) << 33 - SGL_EXP_LENGTH) || \332Duintp2(duint_valueB) || Slow(sgl_value)) Sall(sgl_value)++333334#define Duint_isinexact_to_dbl(duint_value) \335(Duintp2(duint_value) << 32 - DBL_EXP_LENGTH)336337#define Dbl_roundnearest_from_duint(duint_opndB,dbl_opndA,dbl_opndB) \338if (Duintp2(duint_opndB) & 1<<(DBL_EXP_LENGTH - 1)) \339if ((Duintp2(duint_opndB) << 33 - DBL_EXP_LENGTH) || Dlowp2(dbl_opndB)) \340if ((++Dallp2(dbl_opndB))==0) Dallp1(dbl_opndA)++341342#define Suint_from_sgl_mantissa(src,exponent,result) \343Sall(result) = (unsigned)(Sall(src) << SGL_EXP_LENGTH)>>(31 - exponent)344345#define Sgl_isinexact_to_unsigned(sgl_value,exponent) \346Sgl_isinexact_to_fix(sgl_value,exponent)347348#define Duint_from_sgl_mantissa(sgl_value,exponent,dresultA,dresultB) \349{Sall(sgl_value) <<= SGL_EXP_LENGTH; /* left-justify */ \350if (exponent <= 31) { \351Dintp1(dresultA) = 0; \352Dintp2(dresultB) = (unsigned)Sall(sgl_value) >> (31 - exponent); \353} \354else { \355Dintp1(dresultA) = Sall(sgl_value) >> (63 - exponent); \356Dintp2(dresultB) = Sall(sgl_value) << (exponent - 31); \357} \358Sall(sgl_value) >>= SGL_EXP_LENGTH; /* return to original */ \359}360361#define Duint_setzero(dresultA,dresultB) \362Dint_setzero(dresultA,dresultB)363364#define Duint_increment(dresultA,dresultB) Dint_increment(dresultA,dresultB)365366#define Duint_isone_lowp2(dresultB) Dint_isone_lowp2(dresultB)367368#define Suint_from_dbl_mantissa(srcA,srcB,exponent,dest) \369Shiftdouble(Dallp1(srcA),Dallp2(srcB),21,dest); \370dest = (unsigned)dest >> 31 - exponent371372#define Dbl_isinexact_to_unsigned(dbl_valueA,dbl_valueB,exponent) \373Dbl_isinexact_to_fix(dbl_valueA,dbl_valueB,exponent)374375#define Duint_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent,destA,destB) \376Dint_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent,destA,destB)377378379