Path: blob/master/arch/sh/kernel/cpu/sh4/softfloat.c
37217 views
/*1* Floating point emulation support for subnormalised numbers on SH42* architecture This file is derived from the SoftFloat IEC/IEEE3* Floating-point Arithmetic Package, Release 2 the original license of4* which is reproduced below.5*6* ========================================================================7*8* This C source file is part of the SoftFloat IEC/IEEE Floating-point9* Arithmetic Package, Release 2.10*11* Written by John R. Hauser. This work was made possible in part by the12* International Computer Science Institute, located at Suite 600, 1947 Center13* Street, Berkeley, California 94704. Funding was partially provided by the14* National Science Foundation under grant MIP-9311980. The original version15* of this code was written as part of a project to build a fixed-point vector16* processor in collaboration with the University of California at Berkeley,17* overseen by Profs. Nelson Morgan and John Wawrzynek. More information18* is available through the web page `http://HTTP.CS.Berkeley.EDU/~jhauser/19* arithmetic/softfloat.html'.20*21* THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort22* has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT23* TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO24* PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY25* AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.26*27* Derivative works are acceptable, even for commercial purposes, so long as28* (1) they include prominent notice that the work is derivative, and (2) they29* include prominent notice akin to these three paragraphs for those parts of30* this code that are retained.31*32* ========================================================================33*34* SH4 modifications by Ismail Dhaoui <[email protected]>35* and Kamel Khelifi <[email protected]>36*/37#include <linux/kernel.h>38#include <cpu/fpu.h>39#include <asm/div64.h>4041#define LIT64( a ) a##LL4243typedef char flag;44typedef unsigned char uint8;45typedef signed char int8;46typedef int uint16;47typedef int int16;48typedef unsigned int uint32;49typedef signed int int32;5051typedef unsigned long long int bits64;52typedef signed long long int sbits64;5354typedef unsigned char bits8;55typedef signed char sbits8;56typedef unsigned short int bits16;57typedef signed short int sbits16;58typedef unsigned int bits32;59typedef signed int sbits32;6061typedef unsigned long long int uint64;62typedef signed long long int int64;6364typedef unsigned long int float32;65typedef unsigned long long float64;6667extern void float_raise(unsigned int flags); /* in fpu.c */68extern int float_rounding_mode(void); /* in fpu.c */6970bits64 extractFloat64Frac(float64 a);71flag extractFloat64Sign(float64 a);72int16 extractFloat64Exp(float64 a);73int16 extractFloat32Exp(float32 a);74flag extractFloat32Sign(float32 a);75bits32 extractFloat32Frac(float32 a);76float64 packFloat64(flag zSign, int16 zExp, bits64 zSig);77void shift64RightJamming(bits64 a, int16 count, bits64 * zPtr);78float32 packFloat32(flag zSign, int16 zExp, bits32 zSig);79void shift32RightJamming(bits32 a, int16 count, bits32 * zPtr);80float64 float64_sub(float64 a, float64 b);81float32 float32_sub(float32 a, float32 b);82float32 float32_add(float32 a, float32 b);83float64 float64_add(float64 a, float64 b);84float64 float64_div(float64 a, float64 b);85float32 float32_div(float32 a, float32 b);86float32 float32_mul(float32 a, float32 b);87float64 float64_mul(float64 a, float64 b);88float32 float64_to_float32(float64 a);89void add128(bits64 a0, bits64 a1, bits64 b0, bits64 b1, bits64 * z0Ptr,90bits64 * z1Ptr);91void sub128(bits64 a0, bits64 a1, bits64 b0, bits64 b1, bits64 * z0Ptr,92bits64 * z1Ptr);93void mul64To128(bits64 a, bits64 b, bits64 * z0Ptr, bits64 * z1Ptr);9495static int8 countLeadingZeros32(bits32 a);96static int8 countLeadingZeros64(bits64 a);97static float64 normalizeRoundAndPackFloat64(flag zSign, int16 zExp,98bits64 zSig);99static float64 subFloat64Sigs(float64 a, float64 b, flag zSign);100static float64 addFloat64Sigs(float64 a, float64 b, flag zSign);101static float32 roundAndPackFloat32(flag zSign, int16 zExp, bits32 zSig);102static float32 normalizeRoundAndPackFloat32(flag zSign, int16 zExp,103bits32 zSig);104static float64 roundAndPackFloat64(flag zSign, int16 zExp, bits64 zSig);105static float32 subFloat32Sigs(float32 a, float32 b, flag zSign);106static float32 addFloat32Sigs(float32 a, float32 b, flag zSign);107static void normalizeFloat64Subnormal(bits64 aSig, int16 * zExpPtr,108bits64 * zSigPtr);109static bits64 estimateDiv128To64(bits64 a0, bits64 a1, bits64 b);110static void normalizeFloat32Subnormal(bits32 aSig, int16 * zExpPtr,111bits32 * zSigPtr);112113bits64 extractFloat64Frac(float64 a)114{115return a & LIT64(0x000FFFFFFFFFFFFF);116}117118flag extractFloat64Sign(float64 a)119{120return a >> 63;121}122123int16 extractFloat64Exp(float64 a)124{125return (a >> 52) & 0x7FF;126}127128int16 extractFloat32Exp(float32 a)129{130return (a >> 23) & 0xFF;131}132133flag extractFloat32Sign(float32 a)134{135return a >> 31;136}137138bits32 extractFloat32Frac(float32 a)139{140return a & 0x007FFFFF;141}142143float64 packFloat64(flag zSign, int16 zExp, bits64 zSig)144{145return (((bits64) zSign) << 63) + (((bits64) zExp) << 52) + zSig;146}147148void shift64RightJamming(bits64 a, int16 count, bits64 * zPtr)149{150bits64 z;151152if (count == 0) {153z = a;154} else if (count < 64) {155z = (a >> count) | ((a << ((-count) & 63)) != 0);156} else {157z = (a != 0);158}159*zPtr = z;160}161162static int8 countLeadingZeros32(bits32 a)163{164static const int8 countLeadingZerosHigh[] = {1658, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,1663, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,1672, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,1682, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,1691, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1701, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1711, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1721, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1730, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,1740, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,1750, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,1760, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,1770, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,1780, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,1790, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,1800, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0181};182int8 shiftCount;183184shiftCount = 0;185if (a < 0x10000) {186shiftCount += 16;187a <<= 16;188}189if (a < 0x1000000) {190shiftCount += 8;191a <<= 8;192}193shiftCount += countLeadingZerosHigh[a >> 24];194return shiftCount;195196}197198static int8 countLeadingZeros64(bits64 a)199{200int8 shiftCount;201202shiftCount = 0;203if (a < ((bits64) 1) << 32) {204shiftCount += 32;205} else {206a >>= 32;207}208shiftCount += countLeadingZeros32(a);209return shiftCount;210211}212213static float64 normalizeRoundAndPackFloat64(flag zSign, int16 zExp, bits64 zSig)214{215int8 shiftCount;216217shiftCount = countLeadingZeros64(zSig) - 1;218return roundAndPackFloat64(zSign, zExp - shiftCount,219zSig << shiftCount);220221}222223static float64 subFloat64Sigs(float64 a, float64 b, flag zSign)224{225int16 aExp, bExp, zExp;226bits64 aSig, bSig, zSig;227int16 expDiff;228229aSig = extractFloat64Frac(a);230aExp = extractFloat64Exp(a);231bSig = extractFloat64Frac(b);232bExp = extractFloat64Exp(b);233expDiff = aExp - bExp;234aSig <<= 10;235bSig <<= 10;236if (0 < expDiff)237goto aExpBigger;238if (expDiff < 0)239goto bExpBigger;240if (aExp == 0) {241aExp = 1;242bExp = 1;243}244if (bSig < aSig)245goto aBigger;246if (aSig < bSig)247goto bBigger;248return packFloat64(float_rounding_mode() == FPSCR_RM_ZERO, 0, 0);249bExpBigger:250if (bExp == 0x7FF) {251return packFloat64(zSign ^ 1, 0x7FF, 0);252}253if (aExp == 0) {254++expDiff;255} else {256aSig |= LIT64(0x4000000000000000);257}258shift64RightJamming(aSig, -expDiff, &aSig);259bSig |= LIT64(0x4000000000000000);260bBigger:261zSig = bSig - aSig;262zExp = bExp;263zSign ^= 1;264goto normalizeRoundAndPack;265aExpBigger:266if (aExp == 0x7FF) {267return a;268}269if (bExp == 0) {270--expDiff;271} else {272bSig |= LIT64(0x4000000000000000);273}274shift64RightJamming(bSig, expDiff, &bSig);275aSig |= LIT64(0x4000000000000000);276aBigger:277zSig = aSig - bSig;278zExp = aExp;279normalizeRoundAndPack:280--zExp;281return normalizeRoundAndPackFloat64(zSign, zExp, zSig);282283}284static float64 addFloat64Sigs(float64 a, float64 b, flag zSign)285{286int16 aExp, bExp, zExp;287bits64 aSig, bSig, zSig;288int16 expDiff;289290aSig = extractFloat64Frac(a);291aExp = extractFloat64Exp(a);292bSig = extractFloat64Frac(b);293bExp = extractFloat64Exp(b);294expDiff = aExp - bExp;295aSig <<= 9;296bSig <<= 9;297if (0 < expDiff) {298if (aExp == 0x7FF) {299return a;300}301if (bExp == 0) {302--expDiff;303} else {304bSig |= LIT64(0x2000000000000000);305}306shift64RightJamming(bSig, expDiff, &bSig);307zExp = aExp;308} else if (expDiff < 0) {309if (bExp == 0x7FF) {310return packFloat64(zSign, 0x7FF, 0);311}312if (aExp == 0) {313++expDiff;314} else {315aSig |= LIT64(0x2000000000000000);316}317shift64RightJamming(aSig, -expDiff, &aSig);318zExp = bExp;319} else {320if (aExp == 0x7FF) {321return a;322}323if (aExp == 0)324return packFloat64(zSign, 0, (aSig + bSig) >> 9);325zSig = LIT64(0x4000000000000000) + aSig + bSig;326zExp = aExp;327goto roundAndPack;328}329aSig |= LIT64(0x2000000000000000);330zSig = (aSig + bSig) << 1;331--zExp;332if ((sbits64) zSig < 0) {333zSig = aSig + bSig;334++zExp;335}336roundAndPack:337return roundAndPackFloat64(zSign, zExp, zSig);338339}340341float32 packFloat32(flag zSign, int16 zExp, bits32 zSig)342{343return (((bits32) zSign) << 31) + (((bits32) zExp) << 23) + zSig;344}345346void shift32RightJamming(bits32 a, int16 count, bits32 * zPtr)347{348bits32 z;349if (count == 0) {350z = a;351} else if (count < 32) {352z = (a >> count) | ((a << ((-count) & 31)) != 0);353} else {354z = (a != 0);355}356*zPtr = z;357}358359static float32 roundAndPackFloat32(flag zSign, int16 zExp, bits32 zSig)360{361flag roundNearestEven;362int8 roundIncrement, roundBits;363flag isTiny;364365/* SH4 has only 2 rounding modes - round to nearest and round to zero */366roundNearestEven = (float_rounding_mode() == FPSCR_RM_NEAREST);367roundIncrement = 0x40;368if (!roundNearestEven) {369roundIncrement = 0;370}371roundBits = zSig & 0x7F;372if (0xFD <= (bits16) zExp) {373if ((0xFD < zExp)374|| ((zExp == 0xFD)375&& ((sbits32) (zSig + roundIncrement) < 0))376) {377float_raise(FPSCR_CAUSE_OVERFLOW | FPSCR_CAUSE_INEXACT);378return packFloat32(zSign, 0xFF,3790) - (roundIncrement == 0);380}381if (zExp < 0) {382isTiny = (zExp < -1)383|| (zSig + roundIncrement < 0x80000000);384shift32RightJamming(zSig, -zExp, &zSig);385zExp = 0;386roundBits = zSig & 0x7F;387if (isTiny && roundBits)388float_raise(FPSCR_CAUSE_UNDERFLOW);389}390}391if (roundBits)392float_raise(FPSCR_CAUSE_INEXACT);393zSig = (zSig + roundIncrement) >> 7;394zSig &= ~(((roundBits ^ 0x40) == 0) & roundNearestEven);395if (zSig == 0)396zExp = 0;397return packFloat32(zSign, zExp, zSig);398399}400401static float32 normalizeRoundAndPackFloat32(flag zSign, int16 zExp, bits32 zSig)402{403int8 shiftCount;404405shiftCount = countLeadingZeros32(zSig) - 1;406return roundAndPackFloat32(zSign, zExp - shiftCount,407zSig << shiftCount);408}409410static float64 roundAndPackFloat64(flag zSign, int16 zExp, bits64 zSig)411{412flag roundNearestEven;413int16 roundIncrement, roundBits;414flag isTiny;415416/* SH4 has only 2 rounding modes - round to nearest and round to zero */417roundNearestEven = (float_rounding_mode() == FPSCR_RM_NEAREST);418roundIncrement = 0x200;419if (!roundNearestEven) {420roundIncrement = 0;421}422roundBits = zSig & 0x3FF;423if (0x7FD <= (bits16) zExp) {424if ((0x7FD < zExp)425|| ((zExp == 0x7FD)426&& ((sbits64) (zSig + roundIncrement) < 0))427) {428float_raise(FPSCR_CAUSE_OVERFLOW | FPSCR_CAUSE_INEXACT);429return packFloat64(zSign, 0x7FF,4300) - (roundIncrement == 0);431}432if (zExp < 0) {433isTiny = (zExp < -1)434|| (zSig + roundIncrement <435LIT64(0x8000000000000000));436shift64RightJamming(zSig, -zExp, &zSig);437zExp = 0;438roundBits = zSig & 0x3FF;439if (isTiny && roundBits)440float_raise(FPSCR_CAUSE_UNDERFLOW);441}442}443if (roundBits)444float_raise(FPSCR_CAUSE_INEXACT);445zSig = (zSig + roundIncrement) >> 10;446zSig &= ~(((roundBits ^ 0x200) == 0) & roundNearestEven);447if (zSig == 0)448zExp = 0;449return packFloat64(zSign, zExp, zSig);450451}452453static float32 subFloat32Sigs(float32 a, float32 b, flag zSign)454{455int16 aExp, bExp, zExp;456bits32 aSig, bSig, zSig;457int16 expDiff;458459aSig = extractFloat32Frac(a);460aExp = extractFloat32Exp(a);461bSig = extractFloat32Frac(b);462bExp = extractFloat32Exp(b);463expDiff = aExp - bExp;464aSig <<= 7;465bSig <<= 7;466if (0 < expDiff)467goto aExpBigger;468if (expDiff < 0)469goto bExpBigger;470if (aExp == 0) {471aExp = 1;472bExp = 1;473}474if (bSig < aSig)475goto aBigger;476if (aSig < bSig)477goto bBigger;478return packFloat32(float_rounding_mode() == FPSCR_RM_ZERO, 0, 0);479bExpBigger:480if (bExp == 0xFF) {481return packFloat32(zSign ^ 1, 0xFF, 0);482}483if (aExp == 0) {484++expDiff;485} else {486aSig |= 0x40000000;487}488shift32RightJamming(aSig, -expDiff, &aSig);489bSig |= 0x40000000;490bBigger:491zSig = bSig - aSig;492zExp = bExp;493zSign ^= 1;494goto normalizeRoundAndPack;495aExpBigger:496if (aExp == 0xFF) {497return a;498}499if (bExp == 0) {500--expDiff;501} else {502bSig |= 0x40000000;503}504shift32RightJamming(bSig, expDiff, &bSig);505aSig |= 0x40000000;506aBigger:507zSig = aSig - bSig;508zExp = aExp;509normalizeRoundAndPack:510--zExp;511return normalizeRoundAndPackFloat32(zSign, zExp, zSig);512513}514515static float32 addFloat32Sigs(float32 a, float32 b, flag zSign)516{517int16 aExp, bExp, zExp;518bits32 aSig, bSig, zSig;519int16 expDiff;520521aSig = extractFloat32Frac(a);522aExp = extractFloat32Exp(a);523bSig = extractFloat32Frac(b);524bExp = extractFloat32Exp(b);525expDiff = aExp - bExp;526aSig <<= 6;527bSig <<= 6;528if (0 < expDiff) {529if (aExp == 0xFF) {530return a;531}532if (bExp == 0) {533--expDiff;534} else {535bSig |= 0x20000000;536}537shift32RightJamming(bSig, expDiff, &bSig);538zExp = aExp;539} else if (expDiff < 0) {540if (bExp == 0xFF) {541return packFloat32(zSign, 0xFF, 0);542}543if (aExp == 0) {544++expDiff;545} else {546aSig |= 0x20000000;547}548shift32RightJamming(aSig, -expDiff, &aSig);549zExp = bExp;550} else {551if (aExp == 0xFF) {552return a;553}554if (aExp == 0)555return packFloat32(zSign, 0, (aSig + bSig) >> 6);556zSig = 0x40000000 + aSig + bSig;557zExp = aExp;558goto roundAndPack;559}560aSig |= 0x20000000;561zSig = (aSig + bSig) << 1;562--zExp;563if ((sbits32) zSig < 0) {564zSig = aSig + bSig;565++zExp;566}567roundAndPack:568return roundAndPackFloat32(zSign, zExp, zSig);569570}571572float64 float64_sub(float64 a, float64 b)573{574flag aSign, bSign;575576aSign = extractFloat64Sign(a);577bSign = extractFloat64Sign(b);578if (aSign == bSign) {579return subFloat64Sigs(a, b, aSign);580} else {581return addFloat64Sigs(a, b, aSign);582}583584}585586float32 float32_sub(float32 a, float32 b)587{588flag aSign, bSign;589590aSign = extractFloat32Sign(a);591bSign = extractFloat32Sign(b);592if (aSign == bSign) {593return subFloat32Sigs(a, b, aSign);594} else {595return addFloat32Sigs(a, b, aSign);596}597598}599600float32 float32_add(float32 a, float32 b)601{602flag aSign, bSign;603604aSign = extractFloat32Sign(a);605bSign = extractFloat32Sign(b);606if (aSign == bSign) {607return addFloat32Sigs(a, b, aSign);608} else {609return subFloat32Sigs(a, b, aSign);610}611612}613614float64 float64_add(float64 a, float64 b)615{616flag aSign, bSign;617618aSign = extractFloat64Sign(a);619bSign = extractFloat64Sign(b);620if (aSign == bSign) {621return addFloat64Sigs(a, b, aSign);622} else {623return subFloat64Sigs(a, b, aSign);624}625}626627static void628normalizeFloat64Subnormal(bits64 aSig, int16 * zExpPtr, bits64 * zSigPtr)629{630int8 shiftCount;631632shiftCount = countLeadingZeros64(aSig) - 11;633*zSigPtr = aSig << shiftCount;634*zExpPtr = 1 - shiftCount;635}636637void add128(bits64 a0, bits64 a1, bits64 b0, bits64 b1, bits64 * z0Ptr,638bits64 * z1Ptr)639{640bits64 z1;641642z1 = a1 + b1;643*z1Ptr = z1;644*z0Ptr = a0 + b0 + (z1 < a1);645}646647void648sub128(bits64 a0, bits64 a1, bits64 b0, bits64 b1, bits64 * z0Ptr,649bits64 * z1Ptr)650{651*z1Ptr = a1 - b1;652*z0Ptr = a0 - b0 - (a1 < b1);653}654655static bits64 estimateDiv128To64(bits64 a0, bits64 a1, bits64 b)656{657bits64 b0, b1;658bits64 rem0, rem1, term0, term1;659bits64 z, tmp;660if (b <= a0)661return LIT64(0xFFFFFFFFFFFFFFFF);662b0 = b >> 32;663tmp = a0;664do_div(tmp, b0);665666z = (b0 << 32 <= a0) ? LIT64(0xFFFFFFFF00000000) : tmp << 32;667mul64To128(b, z, &term0, &term1);668sub128(a0, a1, term0, term1, &rem0, &rem1);669while (((sbits64) rem0) < 0) {670z -= LIT64(0x100000000);671b1 = b << 32;672add128(rem0, rem1, b0, b1, &rem0, &rem1);673}674rem0 = (rem0 << 32) | (rem1 >> 32);675tmp = rem0;676do_div(tmp, b0);677z |= (b0 << 32 <= rem0) ? 0xFFFFFFFF : tmp;678return z;679}680681void mul64To128(bits64 a, bits64 b, bits64 * z0Ptr, bits64 * z1Ptr)682{683bits32 aHigh, aLow, bHigh, bLow;684bits64 z0, zMiddleA, zMiddleB, z1;685686aLow = a;687aHigh = a >> 32;688bLow = b;689bHigh = b >> 32;690z1 = ((bits64) aLow) * bLow;691zMiddleA = ((bits64) aLow) * bHigh;692zMiddleB = ((bits64) aHigh) * bLow;693z0 = ((bits64) aHigh) * bHigh;694zMiddleA += zMiddleB;695z0 += (((bits64) (zMiddleA < zMiddleB)) << 32) + (zMiddleA >> 32);696zMiddleA <<= 32;697z1 += zMiddleA;698z0 += (z1 < zMiddleA);699*z1Ptr = z1;700*z0Ptr = z0;701702}703704static void normalizeFloat32Subnormal(bits32 aSig, int16 * zExpPtr,705bits32 * zSigPtr)706{707int8 shiftCount;708709shiftCount = countLeadingZeros32(aSig) - 8;710*zSigPtr = aSig << shiftCount;711*zExpPtr = 1 - shiftCount;712713}714715float64 float64_div(float64 a, float64 b)716{717flag aSign, bSign, zSign;718int16 aExp, bExp, zExp;719bits64 aSig, bSig, zSig;720bits64 rem0, rem1;721bits64 term0, term1;722723aSig = extractFloat64Frac(a);724aExp = extractFloat64Exp(a);725aSign = extractFloat64Sign(a);726bSig = extractFloat64Frac(b);727bExp = extractFloat64Exp(b);728bSign = extractFloat64Sign(b);729zSign = aSign ^ bSign;730if (aExp == 0x7FF) {731if (bExp == 0x7FF) {732}733return packFloat64(zSign, 0x7FF, 0);734}735if (bExp == 0x7FF) {736return packFloat64(zSign, 0, 0);737}738if (bExp == 0) {739if (bSig == 0) {740if ((aExp | aSig) == 0) {741float_raise(FPSCR_CAUSE_INVALID);742}743return packFloat64(zSign, 0x7FF, 0);744}745normalizeFloat64Subnormal(bSig, &bExp, &bSig);746}747if (aExp == 0) {748if (aSig == 0)749return packFloat64(zSign, 0, 0);750normalizeFloat64Subnormal(aSig, &aExp, &aSig);751}752zExp = aExp - bExp + 0x3FD;753aSig = (aSig | LIT64(0x0010000000000000)) << 10;754bSig = (bSig | LIT64(0x0010000000000000)) << 11;755if (bSig <= (aSig + aSig)) {756aSig >>= 1;757++zExp;758}759zSig = estimateDiv128To64(aSig, 0, bSig);760if ((zSig & 0x1FF) <= 2) {761mul64To128(bSig, zSig, &term0, &term1);762sub128(aSig, 0, term0, term1, &rem0, &rem1);763while ((sbits64) rem0 < 0) {764--zSig;765add128(rem0, rem1, 0, bSig, &rem0, &rem1);766}767zSig |= (rem1 != 0);768}769return roundAndPackFloat64(zSign, zExp, zSig);770771}772773float32 float32_div(float32 a, float32 b)774{775flag aSign, bSign, zSign;776int16 aExp, bExp, zExp;777bits32 aSig, bSig;778uint64_t zSig;779780aSig = extractFloat32Frac(a);781aExp = extractFloat32Exp(a);782aSign = extractFloat32Sign(a);783bSig = extractFloat32Frac(b);784bExp = extractFloat32Exp(b);785bSign = extractFloat32Sign(b);786zSign = aSign ^ bSign;787if (aExp == 0xFF) {788if (bExp == 0xFF) {789}790return packFloat32(zSign, 0xFF, 0);791}792if (bExp == 0xFF) {793return packFloat32(zSign, 0, 0);794}795if (bExp == 0) {796if (bSig == 0) {797return packFloat32(zSign, 0xFF, 0);798}799normalizeFloat32Subnormal(bSig, &bExp, &bSig);800}801if (aExp == 0) {802if (aSig == 0)803return packFloat32(zSign, 0, 0);804normalizeFloat32Subnormal(aSig, &aExp, &aSig);805}806zExp = aExp - bExp + 0x7D;807aSig = (aSig | 0x00800000) << 7;808bSig = (bSig | 0x00800000) << 8;809if (bSig <= (aSig + aSig)) {810aSig >>= 1;811++zExp;812}813zSig = (((bits64) aSig) << 32);814do_div(zSig, bSig);815816if ((zSig & 0x3F) == 0) {817zSig |= (((bits64) bSig) * zSig != ((bits64) aSig) << 32);818}819return roundAndPackFloat32(zSign, zExp, (bits32)zSig);820821}822823float32 float32_mul(float32 a, float32 b)824{825char aSign, bSign, zSign;826int aExp, bExp, zExp;827unsigned int aSig, bSig;828unsigned long long zSig64;829unsigned int zSig;830831aSig = extractFloat32Frac(a);832aExp = extractFloat32Exp(a);833aSign = extractFloat32Sign(a);834bSig = extractFloat32Frac(b);835bExp = extractFloat32Exp(b);836bSign = extractFloat32Sign(b);837zSign = aSign ^ bSign;838if (aExp == 0) {839if (aSig == 0)840return packFloat32(zSign, 0, 0);841normalizeFloat32Subnormal(aSig, &aExp, &aSig);842}843if (bExp == 0) {844if (bSig == 0)845return packFloat32(zSign, 0, 0);846normalizeFloat32Subnormal(bSig, &bExp, &bSig);847}848if ((bExp == 0xff && bSig == 0) || (aExp == 0xff && aSig == 0))849return roundAndPackFloat32(zSign, 0xff, 0);850851zExp = aExp + bExp - 0x7F;852aSig = (aSig | 0x00800000) << 7;853bSig = (bSig | 0x00800000) << 8;854shift64RightJamming(((unsigned long long)aSig) * bSig, 32, &zSig64);855zSig = zSig64;856if (0 <= (signed int)(zSig << 1)) {857zSig <<= 1;858--zExp;859}860return roundAndPackFloat32(zSign, zExp, zSig);861862}863864float64 float64_mul(float64 a, float64 b)865{866char aSign, bSign, zSign;867int aExp, bExp, zExp;868unsigned long long int aSig, bSig, zSig0, zSig1;869870aSig = extractFloat64Frac(a);871aExp = extractFloat64Exp(a);872aSign = extractFloat64Sign(a);873bSig = extractFloat64Frac(b);874bExp = extractFloat64Exp(b);875bSign = extractFloat64Sign(b);876zSign = aSign ^ bSign;877878if (aExp == 0) {879if (aSig == 0)880return packFloat64(zSign, 0, 0);881normalizeFloat64Subnormal(aSig, &aExp, &aSig);882}883if (bExp == 0) {884if (bSig == 0)885return packFloat64(zSign, 0, 0);886normalizeFloat64Subnormal(bSig, &bExp, &bSig);887}888if ((aExp == 0x7ff && aSig == 0) || (bExp == 0x7ff && bSig == 0))889return roundAndPackFloat64(zSign, 0x7ff, 0);890891zExp = aExp + bExp - 0x3FF;892aSig = (aSig | 0x0010000000000000LL) << 10;893bSig = (bSig | 0x0010000000000000LL) << 11;894mul64To128(aSig, bSig, &zSig0, &zSig1);895zSig0 |= (zSig1 != 0);896if (0 <= (signed long long int)(zSig0 << 1)) {897zSig0 <<= 1;898--zExp;899}900return roundAndPackFloat64(zSign, zExp, zSig0);901}902903/*904* -------------------------------------------------------------------------------905* Returns the result of converting the double-precision floating-point value906* `a' to the single-precision floating-point format. The conversion is907* performed according to the IEC/IEEE Standard for Binary Floating-point908* Arithmetic.909* -------------------------------------------------------------------------------910* */911float32 float64_to_float32(float64 a)912{913flag aSign;914int16 aExp;915bits64 aSig;916bits32 zSig;917918aSig = extractFloat64Frac( a );919aExp = extractFloat64Exp( a );920aSign = extractFloat64Sign( a );921922shift64RightJamming( aSig, 22, &aSig );923zSig = aSig;924if ( aExp || zSig ) {925zSig |= 0x40000000;926aExp -= 0x381;927}928return roundAndPackFloat32(aSign, aExp, zSig);929}930931932