softfloat.c 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436
  1. /*
  2. ===============================================================================
  3. This C source file is part of the SoftFloat IEC/IEEE Floating-point
  4. Arithmetic Package, Release 2.
  5. Written by John R. Hauser. This work was made possible in part by the
  6. International Computer Science Institute, located at Suite 600, 1947 Center
  7. Street, Berkeley, California 94704. Funding was partially provided by the
  8. National Science Foundation under grant MIP-9311980. The original version
  9. of this code was written as part of a project to build a fixed-point vector
  10. processor in collaboration with the University of California at Berkeley,
  11. overseen by Profs. Nelson Morgan and John Wawrzynek. More information
  12. is available through the web page
  13. http://www.jhauser.us/arithmetic/SoftFloat-2b/SoftFloat-source.txt
  14. THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
  15. has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
  16. TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
  17. PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
  18. AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
  19. Derivative works are acceptable, even for commercial purposes, so long as
  20. (1) they include prominent notice that the work is derivative, and (2) they
  21. include prominent notice akin to these three paragraphs for those parts of
  22. this code that are retained.
  23. ===============================================================================
  24. */
  25. #include <asm/div64.h>
  26. #include "fpa11.h"
  27. //#include "milieu.h"
  28. //#include "softfloat.h"
  29. /*
  30. -------------------------------------------------------------------------------
  31. Primitive arithmetic functions, including multi-word arithmetic, and
  32. division and square root approximations. (Can be specialized to target if
  33. desired.)
  34. -------------------------------------------------------------------------------
  35. */
  36. #include "softfloat-macros"
  37. /*
  38. -------------------------------------------------------------------------------
  39. Functions and definitions to determine: (1) whether tininess for underflow
  40. is detected before or after rounding by default, (2) what (if anything)
  41. happens when exceptions are raised, (3) how signaling NaNs are distinguished
  42. from quiet NaNs, (4) the default generated quiet NaNs, and (5) how NaNs
  43. are propagated from function inputs to output. These details are target-
  44. specific.
  45. -------------------------------------------------------------------------------
  46. */
  47. #include "softfloat-specialize"
  48. /*
  49. -------------------------------------------------------------------------------
  50. Takes a 64-bit fixed-point value `absZ' with binary point between bits 6
  51. and 7, and returns the properly rounded 32-bit integer corresponding to the
  52. input. If `zSign' is nonzero, the input is negated before being converted
  53. to an integer. Bit 63 of `absZ' must be zero. Ordinarily, the fixed-point
  54. input is simply rounded to an integer, with the inexact exception raised if
  55. the input cannot be represented exactly as an integer. If the fixed-point
  56. input is too large, however, the invalid exception is raised and the largest
  57. positive or negative integer is returned.
  58. -------------------------------------------------------------------------------
  59. */
  60. static int32 roundAndPackInt32( struct roundingData *roundData, flag zSign, bits64 absZ )
  61. {
  62. int8 roundingMode;
  63. flag roundNearestEven;
  64. int8 roundIncrement, roundBits;
  65. int32 z;
  66. roundingMode = roundData->mode;
  67. roundNearestEven = ( roundingMode == float_round_nearest_even );
  68. roundIncrement = 0x40;
  69. if ( ! roundNearestEven ) {
  70. if ( roundingMode == float_round_to_zero ) {
  71. roundIncrement = 0;
  72. }
  73. else {
  74. roundIncrement = 0x7F;
  75. if ( zSign ) {
  76. if ( roundingMode == float_round_up ) roundIncrement = 0;
  77. }
  78. else {
  79. if ( roundingMode == float_round_down ) roundIncrement = 0;
  80. }
  81. }
  82. }
  83. roundBits = absZ & 0x7F;
  84. absZ = ( absZ + roundIncrement )>>7;
  85. absZ &= ~ ( ( ( roundBits ^ 0x40 ) == 0 ) & roundNearestEven );
  86. z = absZ;
  87. if ( zSign ) z = - z;
  88. if ( ( absZ>>32 ) || ( z && ( ( z < 0 ) ^ zSign ) ) ) {
  89. roundData->exception |= float_flag_invalid;
  90. return zSign ? 0x80000000 : 0x7FFFFFFF;
  91. }
  92. if ( roundBits ) roundData->exception |= float_flag_inexact;
  93. return z;
  94. }
  95. /*
  96. -------------------------------------------------------------------------------
  97. Returns the fraction bits of the single-precision floating-point value `a'.
  98. -------------------------------------------------------------------------------
  99. */
  100. INLINE bits32 extractFloat32Frac( float32 a )
  101. {
  102. return a & 0x007FFFFF;
  103. }
  104. /*
  105. -------------------------------------------------------------------------------
  106. Returns the exponent bits of the single-precision floating-point value `a'.
  107. -------------------------------------------------------------------------------
  108. */
  109. INLINE int16 extractFloat32Exp( float32 a )
  110. {
  111. return ( a>>23 ) & 0xFF;
  112. }
  113. /*
  114. -------------------------------------------------------------------------------
  115. Returns the sign bit of the single-precision floating-point value `a'.
  116. -------------------------------------------------------------------------------
  117. */
  118. #if 0 /* in softfloat.h */
  119. INLINE flag extractFloat32Sign( float32 a )
  120. {
  121. return a>>31;
  122. }
  123. #endif
  124. /*
  125. -------------------------------------------------------------------------------
  126. Normalizes the subnormal single-precision floating-point value represented
  127. by the denormalized significand `aSig'. The normalized exponent and
  128. significand are stored at the locations pointed to by `zExpPtr' and
  129. `zSigPtr', respectively.
  130. -------------------------------------------------------------------------------
  131. */
  132. static void
  133. normalizeFloat32Subnormal( bits32 aSig, int16 *zExpPtr, bits32 *zSigPtr )
  134. {
  135. int8 shiftCount;
  136. shiftCount = countLeadingZeros32( aSig ) - 8;
  137. *zSigPtr = aSig<<shiftCount;
  138. *zExpPtr = 1 - shiftCount;
  139. }
  140. /*
  141. -------------------------------------------------------------------------------
  142. Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
  143. single-precision floating-point value, returning the result. After being
  144. shifted into the proper positions, the three fields are simply added
  145. together to form the result. This means that any integer portion of `zSig'
  146. will be added into the exponent. Since a properly normalized significand
  147. will have an integer portion equal to 1, the `zExp' input should be 1 less
  148. than the desired result exponent whenever `zSig' is a complete, normalized
  149. significand.
  150. -------------------------------------------------------------------------------
  151. */
  152. INLINE float32 packFloat32( flag zSign, int16 zExp, bits32 zSig )
  153. {
  154. #if 0
  155. float32 f;
  156. __asm__("@ packFloat32 \n\
  157. mov %0, %1, asl #31 \n\
  158. orr %0, %2, asl #23 \n\
  159. orr %0, %3"
  160. : /* no outputs */
  161. : "g" (f), "g" (zSign), "g" (zExp), "g" (zSig)
  162. : "cc");
  163. return f;
  164. #else
  165. return ( ( (bits32) zSign )<<31 ) + ( ( (bits32) zExp )<<23 ) + zSig;
  166. #endif
  167. }
  168. /*
  169. -------------------------------------------------------------------------------
  170. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  171. and significand `zSig', and returns the proper single-precision floating-
  172. point value corresponding to the abstract input. Ordinarily, the abstract
  173. value is simply rounded and packed into the single-precision format, with
  174. the inexact exception raised if the abstract input cannot be represented
  175. exactly. If the abstract value is too large, however, the overflow and
  176. inexact exceptions are raised and an infinity or maximal finite value is
  177. returned. If the abstract value is too small, the input value is rounded to
  178. a subnormal number, and the underflow and inexact exceptions are raised if
  179. the abstract input cannot be represented exactly as a subnormal single-
  180. precision floating-point number.
  181. The input significand `zSig' has its binary point between bits 30
  182. and 29, which is 7 bits to the left of the usual location. This shifted
  183. significand must be normalized or smaller. If `zSig' is not normalized,
  184. `zExp' must be 0; in that case, the result returned is a subnormal number,
  185. and it must not require rounding. In the usual case that `zSig' is
  186. normalized, `zExp' must be 1 less than the ``true'' floating-point exponent.
  187. The handling of underflow and overflow follows the IEC/IEEE Standard for
  188. Binary Floating-point Arithmetic.
  189. -------------------------------------------------------------------------------
  190. */
  191. static float32 roundAndPackFloat32( struct roundingData *roundData, flag zSign, int16 zExp, bits32 zSig )
  192. {
  193. int8 roundingMode;
  194. flag roundNearestEven;
  195. int8 roundIncrement, roundBits;
  196. flag isTiny;
  197. roundingMode = roundData->mode;
  198. roundNearestEven = ( roundingMode == float_round_nearest_even );
  199. roundIncrement = 0x40;
  200. if ( ! roundNearestEven ) {
  201. if ( roundingMode == float_round_to_zero ) {
  202. roundIncrement = 0;
  203. }
  204. else {
  205. roundIncrement = 0x7F;
  206. if ( zSign ) {
  207. if ( roundingMode == float_round_up ) roundIncrement = 0;
  208. }
  209. else {
  210. if ( roundingMode == float_round_down ) roundIncrement = 0;
  211. }
  212. }
  213. }
  214. roundBits = zSig & 0x7F;
  215. if ( 0xFD <= (bits16) zExp ) {
  216. if ( ( 0xFD < zExp )
  217. || ( ( zExp == 0xFD )
  218. && ( (sbits32) ( zSig + roundIncrement ) < 0 ) )
  219. ) {
  220. roundData->exception |= float_flag_overflow | float_flag_inexact;
  221. return packFloat32( zSign, 0xFF, 0 ) - ( roundIncrement == 0 );
  222. }
  223. if ( zExp < 0 ) {
  224. isTiny =
  225. ( float_detect_tininess == float_tininess_before_rounding )
  226. || ( zExp < -1 )
  227. || ( zSig + roundIncrement < 0x80000000 );
  228. shift32RightJamming( zSig, - zExp, &zSig );
  229. zExp = 0;
  230. roundBits = zSig & 0x7F;
  231. if ( isTiny && roundBits ) roundData->exception |= float_flag_underflow;
  232. }
  233. }
  234. if ( roundBits ) roundData->exception |= float_flag_inexact;
  235. zSig = ( zSig + roundIncrement )>>7;
  236. zSig &= ~ ( ( ( roundBits ^ 0x40 ) == 0 ) & roundNearestEven );
  237. if ( zSig == 0 ) zExp = 0;
  238. return packFloat32( zSign, zExp, zSig );
  239. }
  240. /*
  241. -------------------------------------------------------------------------------
  242. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  243. and significand `zSig', and returns the proper single-precision floating-
  244. point value corresponding to the abstract input. This routine is just like
  245. `roundAndPackFloat32' except that `zSig' does not have to be normalized in
  246. any way. In all cases, `zExp' must be 1 less than the ``true'' floating-
  247. point exponent.
  248. -------------------------------------------------------------------------------
  249. */
  250. static float32
  251. normalizeRoundAndPackFloat32( struct roundingData *roundData, flag zSign, int16 zExp, bits32 zSig )
  252. {
  253. int8 shiftCount;
  254. shiftCount = countLeadingZeros32( zSig ) - 1;
  255. return roundAndPackFloat32( roundData, zSign, zExp - shiftCount, zSig<<shiftCount );
  256. }
  257. /*
  258. -------------------------------------------------------------------------------
  259. Returns the fraction bits of the double-precision floating-point value `a'.
  260. -------------------------------------------------------------------------------
  261. */
  262. INLINE bits64 extractFloat64Frac( float64 a )
  263. {
  264. return a & LIT64( 0x000FFFFFFFFFFFFF );
  265. }
  266. /*
  267. -------------------------------------------------------------------------------
  268. Returns the exponent bits of the double-precision floating-point value `a'.
  269. -------------------------------------------------------------------------------
  270. */
  271. INLINE int16 extractFloat64Exp( float64 a )
  272. {
  273. return ( a>>52 ) & 0x7FF;
  274. }
  275. /*
  276. -------------------------------------------------------------------------------
  277. Returns the sign bit of the double-precision floating-point value `a'.
  278. -------------------------------------------------------------------------------
  279. */
  280. #if 0 /* in softfloat.h */
  281. INLINE flag extractFloat64Sign( float64 a )
  282. {
  283. return a>>63;
  284. }
  285. #endif
  286. /*
  287. -------------------------------------------------------------------------------
  288. Normalizes the subnormal double-precision floating-point value represented
  289. by the denormalized significand `aSig'. The normalized exponent and
  290. significand are stored at the locations pointed to by `zExpPtr' and
  291. `zSigPtr', respectively.
  292. -------------------------------------------------------------------------------
  293. */
  294. static void
  295. normalizeFloat64Subnormal( bits64 aSig, int16 *zExpPtr, bits64 *zSigPtr )
  296. {
  297. int8 shiftCount;
  298. shiftCount = countLeadingZeros64( aSig ) - 11;
  299. *zSigPtr = aSig<<shiftCount;
  300. *zExpPtr = 1 - shiftCount;
  301. }
  302. /*
  303. -------------------------------------------------------------------------------
  304. Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
  305. double-precision floating-point value, returning the result. After being
  306. shifted into the proper positions, the three fields are simply added
  307. together to form the result. This means that any integer portion of `zSig'
  308. will be added into the exponent. Since a properly normalized significand
  309. will have an integer portion equal to 1, the `zExp' input should be 1 less
  310. than the desired result exponent whenever `zSig' is a complete, normalized
  311. significand.
  312. -------------------------------------------------------------------------------
  313. */
  314. INLINE float64 packFloat64( flag zSign, int16 zExp, bits64 zSig )
  315. {
  316. return ( ( (bits64) zSign )<<63 ) + ( ( (bits64) zExp )<<52 ) + zSig;
  317. }
  318. /*
  319. -------------------------------------------------------------------------------
  320. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  321. and significand `zSig', and returns the proper double-precision floating-
  322. point value corresponding to the abstract input. Ordinarily, the abstract
  323. value is simply rounded and packed into the double-precision format, with
  324. the inexact exception raised if the abstract input cannot be represented
  325. exactly. If the abstract value is too large, however, the overflow and
  326. inexact exceptions are raised and an infinity or maximal finite value is
  327. returned. If the abstract value is too small, the input value is rounded to
  328. a subnormal number, and the underflow and inexact exceptions are raised if
  329. the abstract input cannot be represented exactly as a subnormal double-
  330. precision floating-point number.
  331. The input significand `zSig' has its binary point between bits 62
  332. and 61, which is 10 bits to the left of the usual location. This shifted
  333. significand must be normalized or smaller. If `zSig' is not normalized,
  334. `zExp' must be 0; in that case, the result returned is a subnormal number,
  335. and it must not require rounding. In the usual case that `zSig' is
  336. normalized, `zExp' must be 1 less than the ``true'' floating-point exponent.
  337. The handling of underflow and overflow follows the IEC/IEEE Standard for
  338. Binary Floating-point Arithmetic.
  339. -------------------------------------------------------------------------------
  340. */
  341. static float64 roundAndPackFloat64( struct roundingData *roundData, flag zSign, int16 zExp, bits64 zSig )
  342. {
  343. int8 roundingMode;
  344. flag roundNearestEven;
  345. int16 roundIncrement, roundBits;
  346. flag isTiny;
  347. roundingMode = roundData->mode;
  348. roundNearestEven = ( roundingMode == float_round_nearest_even );
  349. roundIncrement = 0x200;
  350. if ( ! roundNearestEven ) {
  351. if ( roundingMode == float_round_to_zero ) {
  352. roundIncrement = 0;
  353. }
  354. else {
  355. roundIncrement = 0x3FF;
  356. if ( zSign ) {
  357. if ( roundingMode == float_round_up ) roundIncrement = 0;
  358. }
  359. else {
  360. if ( roundingMode == float_round_down ) roundIncrement = 0;
  361. }
  362. }
  363. }
  364. roundBits = zSig & 0x3FF;
  365. if ( 0x7FD <= (bits16) zExp ) {
  366. if ( ( 0x7FD < zExp )
  367. || ( ( zExp == 0x7FD )
  368. && ( (sbits64) ( zSig + roundIncrement ) < 0 ) )
  369. ) {
  370. //register int lr = __builtin_return_address(0);
  371. //printk("roundAndPackFloat64 called from 0x%08x\n",lr);
  372. roundData->exception |= float_flag_overflow | float_flag_inexact;
  373. return packFloat64( zSign, 0x7FF, 0 ) - ( roundIncrement == 0 );
  374. }
  375. if ( zExp < 0 ) {
  376. isTiny =
  377. ( float_detect_tininess == float_tininess_before_rounding )
  378. || ( zExp < -1 )
  379. || ( zSig + roundIncrement < LIT64( 0x8000000000000000 ) );
  380. shift64RightJamming( zSig, - zExp, &zSig );
  381. zExp = 0;
  382. roundBits = zSig & 0x3FF;
  383. if ( isTiny && roundBits ) roundData->exception |= float_flag_underflow;
  384. }
  385. }
  386. if ( roundBits ) roundData->exception |= float_flag_inexact;
  387. zSig = ( zSig + roundIncrement )>>10;
  388. zSig &= ~ ( ( ( roundBits ^ 0x200 ) == 0 ) & roundNearestEven );
  389. if ( zSig == 0 ) zExp = 0;
  390. return packFloat64( zSign, zExp, zSig );
  391. }
  392. /*
  393. -------------------------------------------------------------------------------
  394. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  395. and significand `zSig', and returns the proper double-precision floating-
  396. point value corresponding to the abstract input. This routine is just like
  397. `roundAndPackFloat64' except that `zSig' does not have to be normalized in
  398. any way. In all cases, `zExp' must be 1 less than the ``true'' floating-
  399. point exponent.
  400. -------------------------------------------------------------------------------
  401. */
  402. static float64
  403. normalizeRoundAndPackFloat64( struct roundingData *roundData, flag zSign, int16 zExp, bits64 zSig )
  404. {
  405. int8 shiftCount;
  406. shiftCount = countLeadingZeros64( zSig ) - 1;
  407. return roundAndPackFloat64( roundData, zSign, zExp - shiftCount, zSig<<shiftCount );
  408. }
  409. #ifdef FLOATX80
  410. /*
  411. -------------------------------------------------------------------------------
  412. Returns the fraction bits of the extended double-precision floating-point
  413. value `a'.
  414. -------------------------------------------------------------------------------
  415. */
  416. INLINE bits64 extractFloatx80Frac( floatx80 a )
  417. {
  418. return a.low;
  419. }
  420. /*
  421. -------------------------------------------------------------------------------
  422. Returns the exponent bits of the extended double-precision floating-point
  423. value `a'.
  424. -------------------------------------------------------------------------------
  425. */
  426. INLINE int32 extractFloatx80Exp( floatx80 a )
  427. {
  428. return a.high & 0x7FFF;
  429. }
  430. /*
  431. -------------------------------------------------------------------------------
  432. Returns the sign bit of the extended double-precision floating-point value
  433. `a'.
  434. -------------------------------------------------------------------------------
  435. */
  436. INLINE flag extractFloatx80Sign( floatx80 a )
  437. {
  438. return a.high>>15;
  439. }
  440. /*
  441. -------------------------------------------------------------------------------
  442. Normalizes the subnormal extended double-precision floating-point value
  443. represented by the denormalized significand `aSig'. The normalized exponent
  444. and significand are stored at the locations pointed to by `zExpPtr' and
  445. `zSigPtr', respectively.
  446. -------------------------------------------------------------------------------
  447. */
  448. static void
  449. normalizeFloatx80Subnormal( bits64 aSig, int32 *zExpPtr, bits64 *zSigPtr )
  450. {
  451. int8 shiftCount;
  452. shiftCount = countLeadingZeros64( aSig );
  453. *zSigPtr = aSig<<shiftCount;
  454. *zExpPtr = 1 - shiftCount;
  455. }
  456. /*
  457. -------------------------------------------------------------------------------
  458. Packs the sign `zSign', exponent `zExp', and significand `zSig' into an
  459. extended double-precision floating-point value, returning the result.
  460. -------------------------------------------------------------------------------
  461. */
  462. INLINE floatx80 packFloatx80( flag zSign, int32 zExp, bits64 zSig )
  463. {
  464. floatx80 z;
  465. z.low = zSig;
  466. z.high = ( ( (bits16) zSign )<<15 ) + zExp;
  467. z.__padding = 0;
  468. return z;
  469. }
  470. /*
  471. -------------------------------------------------------------------------------
  472. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  473. and extended significand formed by the concatenation of `zSig0' and `zSig1',
  474. and returns the proper extended double-precision floating-point value
  475. corresponding to the abstract input. Ordinarily, the abstract value is
  476. rounded and packed into the extended double-precision format, with the
  477. inexact exception raised if the abstract input cannot be represented
  478. exactly. If the abstract value is too large, however, the overflow and
  479. inexact exceptions are raised and an infinity or maximal finite value is
  480. returned. If the abstract value is too small, the input value is rounded to
  481. a subnormal number, and the underflow and inexact exceptions are raised if
  482. the abstract input cannot be represented exactly as a subnormal extended
  483. double-precision floating-point number.
  484. If `roundingPrecision' is 32 or 64, the result is rounded to the same
  485. number of bits as single or double precision, respectively. Otherwise, the
  486. result is rounded to the full precision of the extended double-precision
  487. format.
  488. The input significand must be normalized or smaller. If the input
  489. significand is not normalized, `zExp' must be 0; in that case, the result
  490. returned is a subnormal number, and it must not require rounding. The
  491. handling of underflow and overflow follows the IEC/IEEE Standard for Binary
  492. Floating-point Arithmetic.
  493. -------------------------------------------------------------------------------
  494. */
  495. static floatx80
  496. roundAndPackFloatx80(
  497. struct roundingData *roundData, flag zSign, int32 zExp, bits64 zSig0, bits64 zSig1
  498. )
  499. {
  500. int8 roundingMode, roundingPrecision;
  501. flag roundNearestEven, increment, isTiny;
  502. int64 roundIncrement, roundMask, roundBits;
  503. roundingMode = roundData->mode;
  504. roundingPrecision = roundData->precision;
  505. roundNearestEven = ( roundingMode == float_round_nearest_even );
  506. if ( roundingPrecision == 80 ) goto precision80;
  507. if ( roundingPrecision == 64 ) {
  508. roundIncrement = LIT64( 0x0000000000000400 );
  509. roundMask = LIT64( 0x00000000000007FF );
  510. }
  511. else if ( roundingPrecision == 32 ) {
  512. roundIncrement = LIT64( 0x0000008000000000 );
  513. roundMask = LIT64( 0x000000FFFFFFFFFF );
  514. }
  515. else {
  516. goto precision80;
  517. }
  518. zSig0 |= ( zSig1 != 0 );
  519. if ( ! roundNearestEven ) {
  520. if ( roundingMode == float_round_to_zero ) {
  521. roundIncrement = 0;
  522. }
  523. else {
  524. roundIncrement = roundMask;
  525. if ( zSign ) {
  526. if ( roundingMode == float_round_up ) roundIncrement = 0;
  527. }
  528. else {
  529. if ( roundingMode == float_round_down ) roundIncrement = 0;
  530. }
  531. }
  532. }
  533. roundBits = zSig0 & roundMask;
  534. if ( 0x7FFD <= (bits32) ( zExp - 1 ) ) {
  535. if ( ( 0x7FFE < zExp )
  536. || ( ( zExp == 0x7FFE ) && ( zSig0 + roundIncrement < zSig0 ) )
  537. ) {
  538. goto overflow;
  539. }
  540. if ( zExp <= 0 ) {
  541. isTiny =
  542. ( float_detect_tininess == float_tininess_before_rounding )
  543. || ( zExp < 0 )
  544. || ( zSig0 <= zSig0 + roundIncrement );
  545. shift64RightJamming( zSig0, 1 - zExp, &zSig0 );
  546. zExp = 0;
  547. roundBits = zSig0 & roundMask;
  548. if ( isTiny && roundBits ) roundData->exception |= float_flag_underflow;
  549. if ( roundBits ) roundData->exception |= float_flag_inexact;
  550. zSig0 += roundIncrement;
  551. if ( (sbits64) zSig0 < 0 ) zExp = 1;
  552. roundIncrement = roundMask + 1;
  553. if ( roundNearestEven && ( roundBits<<1 == roundIncrement ) ) {
  554. roundMask |= roundIncrement;
  555. }
  556. zSig0 &= ~ roundMask;
  557. return packFloatx80( zSign, zExp, zSig0 );
  558. }
  559. }
  560. if ( roundBits ) roundData->exception |= float_flag_inexact;
  561. zSig0 += roundIncrement;
  562. if ( zSig0 < roundIncrement ) {
  563. ++zExp;
  564. zSig0 = LIT64( 0x8000000000000000 );
  565. }
  566. roundIncrement = roundMask + 1;
  567. if ( roundNearestEven && ( roundBits<<1 == roundIncrement ) ) {
  568. roundMask |= roundIncrement;
  569. }
  570. zSig0 &= ~ roundMask;
  571. if ( zSig0 == 0 ) zExp = 0;
  572. return packFloatx80( zSign, zExp, zSig0 );
  573. precision80:
  574. increment = ( (sbits64) zSig1 < 0 );
  575. if ( ! roundNearestEven ) {
  576. if ( roundingMode == float_round_to_zero ) {
  577. increment = 0;
  578. }
  579. else {
  580. if ( zSign ) {
  581. increment = ( roundingMode == float_round_down ) && zSig1;
  582. }
  583. else {
  584. increment = ( roundingMode == float_round_up ) && zSig1;
  585. }
  586. }
  587. }
  588. if ( 0x7FFD <= (bits32) ( zExp - 1 ) ) {
  589. if ( ( 0x7FFE < zExp )
  590. || ( ( zExp == 0x7FFE )
  591. && ( zSig0 == LIT64( 0xFFFFFFFFFFFFFFFF ) )
  592. && increment
  593. )
  594. ) {
  595. roundMask = 0;
  596. overflow:
  597. roundData->exception |= float_flag_overflow | float_flag_inexact;
  598. if ( ( roundingMode == float_round_to_zero )
  599. || ( zSign && ( roundingMode == float_round_up ) )
  600. || ( ! zSign && ( roundingMode == float_round_down ) )
  601. ) {
  602. return packFloatx80( zSign, 0x7FFE, ~ roundMask );
  603. }
  604. return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
  605. }
  606. if ( zExp <= 0 ) {
  607. isTiny =
  608. ( float_detect_tininess == float_tininess_before_rounding )
  609. || ( zExp < 0 )
  610. || ! increment
  611. || ( zSig0 < LIT64( 0xFFFFFFFFFFFFFFFF ) );
  612. shift64ExtraRightJamming( zSig0, zSig1, 1 - zExp, &zSig0, &zSig1 );
  613. zExp = 0;
  614. if ( isTiny && zSig1 ) roundData->exception |= float_flag_underflow;
  615. if ( zSig1 ) roundData->exception |= float_flag_inexact;
  616. if ( roundNearestEven ) {
  617. increment = ( (sbits64) zSig1 < 0 );
  618. }
  619. else {
  620. if ( zSign ) {
  621. increment = ( roundingMode == float_round_down ) && zSig1;
  622. }
  623. else {
  624. increment = ( roundingMode == float_round_up ) && zSig1;
  625. }
  626. }
  627. if ( increment ) {
  628. ++zSig0;
  629. zSig0 &= ~ ( ( zSig1 + zSig1 == 0 ) & roundNearestEven );
  630. if ( (sbits64) zSig0 < 0 ) zExp = 1;
  631. }
  632. return packFloatx80( zSign, zExp, zSig0 );
  633. }
  634. }
  635. if ( zSig1 ) roundData->exception |= float_flag_inexact;
  636. if ( increment ) {
  637. ++zSig0;
  638. if ( zSig0 == 0 ) {
  639. ++zExp;
  640. zSig0 = LIT64( 0x8000000000000000 );
  641. }
  642. else {
  643. zSig0 &= ~ ( ( zSig1 + zSig1 == 0 ) & roundNearestEven );
  644. }
  645. }
  646. else {
  647. if ( zSig0 == 0 ) zExp = 0;
  648. }
  649. return packFloatx80( zSign, zExp, zSig0 );
  650. }
  651. /*
  652. -------------------------------------------------------------------------------
  653. Takes an abstract floating-point value having sign `zSign', exponent
  654. `zExp', and significand formed by the concatenation of `zSig0' and `zSig1',
  655. and returns the proper extended double-precision floating-point value
  656. corresponding to the abstract input. This routine is just like
  657. `roundAndPackFloatx80' except that the input significand does not have to be
  658. normalized.
  659. -------------------------------------------------------------------------------
  660. */
  661. static floatx80
  662. normalizeRoundAndPackFloatx80(
  663. struct roundingData *roundData, flag zSign, int32 zExp, bits64 zSig0, bits64 zSig1
  664. )
  665. {
  666. int8 shiftCount;
  667. if ( zSig0 == 0 ) {
  668. zSig0 = zSig1;
  669. zSig1 = 0;
  670. zExp -= 64;
  671. }
  672. shiftCount = countLeadingZeros64( zSig0 );
  673. shortShift128Left( zSig0, zSig1, shiftCount, &zSig0, &zSig1 );
  674. zExp -= shiftCount;
  675. return
  676. roundAndPackFloatx80( roundData, zSign, zExp, zSig0, zSig1 );
  677. }
  678. #endif
  679. /*
  680. -------------------------------------------------------------------------------
  681. Returns the result of converting the 32-bit two's complement integer `a' to
  682. the single-precision floating-point format. The conversion is performed
  683. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  684. -------------------------------------------------------------------------------
  685. */
  686. float32 int32_to_float32(struct roundingData *roundData, int32 a)
  687. {
  688. flag zSign;
  689. if ( a == 0 ) return 0;
  690. if ( a == 0x80000000 ) return packFloat32( 1, 0x9E, 0 );
  691. zSign = ( a < 0 );
  692. return normalizeRoundAndPackFloat32( roundData, zSign, 0x9C, zSign ? - a : a );
  693. }
  694. /*
  695. -------------------------------------------------------------------------------
  696. Returns the result of converting the 32-bit two's complement integer `a' to
  697. the double-precision floating-point format. The conversion is performed
  698. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  699. -------------------------------------------------------------------------------
  700. */
  701. float64 int32_to_float64( int32 a )
  702. {
  703. flag aSign;
  704. uint32 absA;
  705. int8 shiftCount;
  706. bits64 zSig;
  707. if ( a == 0 ) return 0;
  708. aSign = ( a < 0 );
  709. absA = aSign ? - a : a;
  710. shiftCount = countLeadingZeros32( absA ) + 21;
  711. zSig = absA;
  712. return packFloat64( aSign, 0x432 - shiftCount, zSig<<shiftCount );
  713. }
  714. #ifdef FLOATX80
  715. /*
  716. -------------------------------------------------------------------------------
  717. Returns the result of converting the 32-bit two's complement integer `a'
  718. to the extended double-precision floating-point format. The conversion
  719. is performed according to the IEC/IEEE Standard for Binary Floating-point
  720. Arithmetic.
  721. -------------------------------------------------------------------------------
  722. */
  723. floatx80 int32_to_floatx80( int32 a )
  724. {
  725. flag zSign;
  726. uint32 absA;
  727. int8 shiftCount;
  728. bits64 zSig;
  729. if ( a == 0 ) return packFloatx80( 0, 0, 0 );
  730. zSign = ( a < 0 );
  731. absA = zSign ? - a : a;
  732. shiftCount = countLeadingZeros32( absA ) + 32;
  733. zSig = absA;
  734. return packFloatx80( zSign, 0x403E - shiftCount, zSig<<shiftCount );
  735. }
  736. #endif
  737. /*
  738. -------------------------------------------------------------------------------
  739. Returns the result of converting the single-precision floating-point value
  740. `a' to the 32-bit two's complement integer format. The conversion is
  741. performed according to the IEC/IEEE Standard for Binary Floating-point
  742. Arithmetic---which means in particular that the conversion is rounded
  743. according to the current rounding mode. If `a' is a NaN, the largest
  744. positive integer is returned. Otherwise, if the conversion overflows, the
  745. largest integer with the same sign as `a' is returned.
  746. -------------------------------------------------------------------------------
  747. */
  748. int32 float32_to_int32( struct roundingData *roundData, float32 a )
  749. {
  750. flag aSign;
  751. int16 aExp, shiftCount;
  752. bits32 aSig;
  753. bits64 zSig;
  754. aSig = extractFloat32Frac( a );
  755. aExp = extractFloat32Exp( a );
  756. aSign = extractFloat32Sign( a );
  757. if ( ( aExp == 0x7FF ) && aSig ) aSign = 0;
  758. if ( aExp ) aSig |= 0x00800000;
  759. shiftCount = 0xAF - aExp;
  760. zSig = aSig;
  761. zSig <<= 32;
  762. if ( 0 < shiftCount ) shift64RightJamming( zSig, shiftCount, &zSig );
  763. return roundAndPackInt32( roundData, aSign, zSig );
  764. }
  765. /*
  766. -------------------------------------------------------------------------------
  767. Returns the result of converting the single-precision floating-point value
  768. `a' to the 32-bit two's complement integer format. The conversion is
  769. performed according to the IEC/IEEE Standard for Binary Floating-point
  770. Arithmetic, except that the conversion is always rounded toward zero. If
  771. `a' is a NaN, the largest positive integer is returned. Otherwise, if the
  772. conversion overflows, the largest integer with the same sign as `a' is
  773. returned.
  774. -------------------------------------------------------------------------------
  775. */
  776. int32 float32_to_int32_round_to_zero( float32 a )
  777. {
  778. flag aSign;
  779. int16 aExp, shiftCount;
  780. bits32 aSig;
  781. int32 z;
  782. aSig = extractFloat32Frac( a );
  783. aExp = extractFloat32Exp( a );
  784. aSign = extractFloat32Sign( a );
  785. shiftCount = aExp - 0x9E;
  786. if ( 0 <= shiftCount ) {
  787. if ( a == 0xCF000000 ) return 0x80000000;
  788. float_raise( float_flag_invalid );
  789. if ( ! aSign || ( ( aExp == 0xFF ) && aSig ) ) return 0x7FFFFFFF;
  790. return 0x80000000;
  791. }
  792. else if ( aExp <= 0x7E ) {
  793. if ( aExp | aSig ) float_raise( float_flag_inexact );
  794. return 0;
  795. }
  796. aSig = ( aSig | 0x00800000 )<<8;
  797. z = aSig>>( - shiftCount );
  798. if ( (bits32) ( aSig<<( shiftCount & 31 ) ) ) {
  799. float_raise( float_flag_inexact );
  800. }
  801. return aSign ? - z : z;
  802. }
  803. /*
  804. -------------------------------------------------------------------------------
  805. Returns the result of converting the single-precision floating-point value
  806. `a' to the double-precision floating-point format. The conversion is
  807. performed according to the IEC/IEEE Standard for Binary Floating-point
  808. Arithmetic.
  809. -------------------------------------------------------------------------------
  810. */
  811. float64 float32_to_float64( float32 a )
  812. {
  813. flag aSign;
  814. int16 aExp;
  815. bits32 aSig;
  816. aSig = extractFloat32Frac( a );
  817. aExp = extractFloat32Exp( a );
  818. aSign = extractFloat32Sign( a );
  819. if ( aExp == 0xFF ) {
  820. if ( aSig ) return commonNaNToFloat64( float32ToCommonNaN( a ) );
  821. return packFloat64( aSign, 0x7FF, 0 );
  822. }
  823. if ( aExp == 0 ) {
  824. if ( aSig == 0 ) return packFloat64( aSign, 0, 0 );
  825. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  826. --aExp;
  827. }
  828. return packFloat64( aSign, aExp + 0x380, ( (bits64) aSig )<<29 );
  829. }
  830. #ifdef FLOATX80
  831. /*
  832. -------------------------------------------------------------------------------
  833. Returns the result of converting the single-precision floating-point value
  834. `a' to the extended double-precision floating-point format. The conversion
  835. is performed according to the IEC/IEEE Standard for Binary Floating-point
  836. Arithmetic.
  837. -------------------------------------------------------------------------------
  838. */
  839. floatx80 float32_to_floatx80( float32 a )
  840. {
  841. flag aSign;
  842. int16 aExp;
  843. bits32 aSig;
  844. aSig = extractFloat32Frac( a );
  845. aExp = extractFloat32Exp( a );
  846. aSign = extractFloat32Sign( a );
  847. if ( aExp == 0xFF ) {
  848. if ( aSig ) return commonNaNToFloatx80( float32ToCommonNaN( a ) );
  849. return packFloatx80( aSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
  850. }
  851. if ( aExp == 0 ) {
  852. if ( aSig == 0 ) return packFloatx80( aSign, 0, 0 );
  853. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  854. }
  855. aSig |= 0x00800000;
  856. return packFloatx80( aSign, aExp + 0x3F80, ( (bits64) aSig )<<40 );
  857. }
  858. #endif
  859. /*
  860. -------------------------------------------------------------------------------
  861. Rounds the single-precision floating-point value `a' to an integer, and
  862. returns the result as a single-precision floating-point value. The
  863. operation is performed according to the IEC/IEEE Standard for Binary
  864. Floating-point Arithmetic.
  865. -------------------------------------------------------------------------------
  866. */
  867. float32 float32_round_to_int( struct roundingData *roundData, float32 a )
  868. {
  869. flag aSign;
  870. int16 aExp;
  871. bits32 lastBitMask, roundBitsMask;
  872. int8 roundingMode;
  873. float32 z;
  874. aExp = extractFloat32Exp( a );
  875. if ( 0x96 <= aExp ) {
  876. if ( ( aExp == 0xFF ) && extractFloat32Frac( a ) ) {
  877. return propagateFloat32NaN( a, a );
  878. }
  879. return a;
  880. }
  881. roundingMode = roundData->mode;
  882. if ( aExp <= 0x7E ) {
  883. if ( (bits32) ( a<<1 ) == 0 ) return a;
  884. roundData->exception |= float_flag_inexact;
  885. aSign = extractFloat32Sign( a );
  886. switch ( roundingMode ) {
  887. case float_round_nearest_even:
  888. if ( ( aExp == 0x7E ) && extractFloat32Frac( a ) ) {
  889. return packFloat32( aSign, 0x7F, 0 );
  890. }
  891. break;
  892. case float_round_down:
  893. return aSign ? 0xBF800000 : 0;
  894. case float_round_up:
  895. return aSign ? 0x80000000 : 0x3F800000;
  896. }
  897. return packFloat32( aSign, 0, 0 );
  898. }
  899. lastBitMask = 1;
  900. lastBitMask <<= 0x96 - aExp;
  901. roundBitsMask = lastBitMask - 1;
  902. z = a;
  903. if ( roundingMode == float_round_nearest_even ) {
  904. z += lastBitMask>>1;
  905. if ( ( z & roundBitsMask ) == 0 ) z &= ~ lastBitMask;
  906. }
  907. else if ( roundingMode != float_round_to_zero ) {
  908. if ( extractFloat32Sign( z ) ^ ( roundingMode == float_round_up ) ) {
  909. z += roundBitsMask;
  910. }
  911. }
  912. z &= ~ roundBitsMask;
  913. if ( z != a ) roundData->exception |= float_flag_inexact;
  914. return z;
  915. }
  916. /*
  917. -------------------------------------------------------------------------------
  918. Returns the result of adding the absolute values of the single-precision
  919. floating-point values `a' and `b'. If `zSign' is true, the sum is negated
  920. before being returned. `zSign' is ignored if the result is a NaN. The
  921. addition is performed according to the IEC/IEEE Standard for Binary
  922. Floating-point Arithmetic.
  923. -------------------------------------------------------------------------------
  924. */
  925. static float32 addFloat32Sigs( struct roundingData *roundData, float32 a, float32 b, flag zSign )
  926. {
  927. int16 aExp, bExp, zExp;
  928. bits32 aSig, bSig, zSig;
  929. int16 expDiff;
  930. aSig = extractFloat32Frac( a );
  931. aExp = extractFloat32Exp( a );
  932. bSig = extractFloat32Frac( b );
  933. bExp = extractFloat32Exp( b );
  934. expDiff = aExp - bExp;
  935. aSig <<= 6;
  936. bSig <<= 6;
  937. if ( 0 < expDiff ) {
  938. if ( aExp == 0xFF ) {
  939. if ( aSig ) return propagateFloat32NaN( a, b );
  940. return a;
  941. }
  942. if ( bExp == 0 ) {
  943. --expDiff;
  944. }
  945. else {
  946. bSig |= 0x20000000;
  947. }
  948. shift32RightJamming( bSig, expDiff, &bSig );
  949. zExp = aExp;
  950. }
  951. else if ( expDiff < 0 ) {
  952. if ( bExp == 0xFF ) {
  953. if ( bSig ) return propagateFloat32NaN( a, b );
  954. return packFloat32( zSign, 0xFF, 0 );
  955. }
  956. if ( aExp == 0 ) {
  957. ++expDiff;
  958. }
  959. else {
  960. aSig |= 0x20000000;
  961. }
  962. shift32RightJamming( aSig, - expDiff, &aSig );
  963. zExp = bExp;
  964. }
  965. else {
  966. if ( aExp == 0xFF ) {
  967. if ( aSig | bSig ) return propagateFloat32NaN( a, b );
  968. return a;
  969. }
  970. if ( aExp == 0 ) return packFloat32( zSign, 0, ( aSig + bSig )>>6 );
  971. zSig = 0x40000000 + aSig + bSig;
  972. zExp = aExp;
  973. goto roundAndPack;
  974. }
  975. aSig |= 0x20000000;
  976. zSig = ( aSig + bSig )<<1;
  977. --zExp;
  978. if ( (sbits32) zSig < 0 ) {
  979. zSig = aSig + bSig;
  980. ++zExp;
  981. }
  982. roundAndPack:
  983. return roundAndPackFloat32( roundData, zSign, zExp, zSig );
  984. }
  985. /*
  986. -------------------------------------------------------------------------------
  987. Returns the result of subtracting the absolute values of the single-
  988. precision floating-point values `a' and `b'. If `zSign' is true, the
  989. difference is negated before being returned. `zSign' is ignored if the
  990. result is a NaN. The subtraction is performed according to the IEC/IEEE
  991. Standard for Binary Floating-point Arithmetic.
  992. -------------------------------------------------------------------------------
  993. */
  994. static float32 subFloat32Sigs( struct roundingData *roundData, float32 a, float32 b, flag zSign )
  995. {
  996. int16 aExp, bExp, zExp;
  997. bits32 aSig, bSig, zSig;
  998. int16 expDiff;
  999. aSig = extractFloat32Frac( a );
  1000. aExp = extractFloat32Exp( a );
  1001. bSig = extractFloat32Frac( b );
  1002. bExp = extractFloat32Exp( b );
  1003. expDiff = aExp - bExp;
  1004. aSig <<= 7;
  1005. bSig <<= 7;
  1006. if ( 0 < expDiff ) goto aExpBigger;
  1007. if ( expDiff < 0 ) goto bExpBigger;
  1008. if ( aExp == 0xFF ) {
  1009. if ( aSig | bSig ) return propagateFloat32NaN( a, b );
  1010. roundData->exception |= float_flag_invalid;
  1011. return float32_default_nan;
  1012. }
  1013. if ( aExp == 0 ) {
  1014. aExp = 1;
  1015. bExp = 1;
  1016. }
  1017. if ( bSig < aSig ) goto aBigger;
  1018. if ( aSig < bSig ) goto bBigger;
  1019. return packFloat32( roundData->mode == float_round_down, 0, 0 );
  1020. bExpBigger:
  1021. if ( bExp == 0xFF ) {
  1022. if ( bSig ) return propagateFloat32NaN( a, b );
  1023. return packFloat32( zSign ^ 1, 0xFF, 0 );
  1024. }
  1025. if ( aExp == 0 ) {
  1026. ++expDiff;
  1027. }
  1028. else {
  1029. aSig |= 0x40000000;
  1030. }
  1031. shift32RightJamming( aSig, - expDiff, &aSig );
  1032. bSig |= 0x40000000;
  1033. bBigger:
  1034. zSig = bSig - aSig;
  1035. zExp = bExp;
  1036. zSign ^= 1;
  1037. goto normalizeRoundAndPack;
  1038. aExpBigger:
  1039. if ( aExp == 0xFF ) {
  1040. if ( aSig ) return propagateFloat32NaN( a, b );
  1041. return a;
  1042. }
  1043. if ( bExp == 0 ) {
  1044. --expDiff;
  1045. }
  1046. else {
  1047. bSig |= 0x40000000;
  1048. }
  1049. shift32RightJamming( bSig, expDiff, &bSig );
  1050. aSig |= 0x40000000;
  1051. aBigger:
  1052. zSig = aSig - bSig;
  1053. zExp = aExp;
  1054. normalizeRoundAndPack:
  1055. --zExp;
  1056. return normalizeRoundAndPackFloat32( roundData, zSign, zExp, zSig );
  1057. }
  1058. /*
  1059. -------------------------------------------------------------------------------
  1060. Returns the result of adding the single-precision floating-point values `a'
  1061. and `b'. The operation is performed according to the IEC/IEEE Standard for
  1062. Binary Floating-point Arithmetic.
  1063. -------------------------------------------------------------------------------
  1064. */
  1065. float32 float32_add( struct roundingData *roundData, float32 a, float32 b )
  1066. {
  1067. flag aSign, bSign;
  1068. aSign = extractFloat32Sign( a );
  1069. bSign = extractFloat32Sign( b );
  1070. if ( aSign == bSign ) {
  1071. return addFloat32Sigs( roundData, a, b, aSign );
  1072. }
  1073. else {
  1074. return subFloat32Sigs( roundData, a, b, aSign );
  1075. }
  1076. }
  1077. /*
  1078. -------------------------------------------------------------------------------
  1079. Returns the result of subtracting the single-precision floating-point values
  1080. `a' and `b'. The operation is performed according to the IEC/IEEE Standard
  1081. for Binary Floating-point Arithmetic.
  1082. -------------------------------------------------------------------------------
  1083. */
  1084. float32 float32_sub( struct roundingData *roundData, float32 a, float32 b )
  1085. {
  1086. flag aSign, bSign;
  1087. aSign = extractFloat32Sign( a );
  1088. bSign = extractFloat32Sign( b );
  1089. if ( aSign == bSign ) {
  1090. return subFloat32Sigs( roundData, a, b, aSign );
  1091. }
  1092. else {
  1093. return addFloat32Sigs( roundData, a, b, aSign );
  1094. }
  1095. }
  1096. /*
  1097. -------------------------------------------------------------------------------
  1098. Returns the result of multiplying the single-precision floating-point values
  1099. `a' and `b'. The operation is performed according to the IEC/IEEE Standard
  1100. for Binary Floating-point Arithmetic.
  1101. -------------------------------------------------------------------------------
  1102. */
  1103. float32 float32_mul( struct roundingData *roundData, float32 a, float32 b )
  1104. {
  1105. flag aSign, bSign, zSign;
  1106. int16 aExp, bExp, zExp;
  1107. bits32 aSig, bSig;
  1108. bits64 zSig64;
  1109. bits32 zSig;
  1110. aSig = extractFloat32Frac( a );
  1111. aExp = extractFloat32Exp( a );
  1112. aSign = extractFloat32Sign( a );
  1113. bSig = extractFloat32Frac( b );
  1114. bExp = extractFloat32Exp( b );
  1115. bSign = extractFloat32Sign( b );
  1116. zSign = aSign ^ bSign;
  1117. if ( aExp == 0xFF ) {
  1118. if ( aSig || ( ( bExp == 0xFF ) && bSig ) ) {
  1119. return propagateFloat32NaN( a, b );
  1120. }
  1121. if ( ( bExp | bSig ) == 0 ) {
  1122. roundData->exception |= float_flag_invalid;
  1123. return float32_default_nan;
  1124. }
  1125. return packFloat32( zSign, 0xFF, 0 );
  1126. }
  1127. if ( bExp == 0xFF ) {
  1128. if ( bSig ) return propagateFloat32NaN( a, b );
  1129. if ( ( aExp | aSig ) == 0 ) {
  1130. roundData->exception |= float_flag_invalid;
  1131. return float32_default_nan;
  1132. }
  1133. return packFloat32( zSign, 0xFF, 0 );
  1134. }
  1135. if ( aExp == 0 ) {
  1136. if ( aSig == 0 ) return packFloat32( zSign, 0, 0 );
  1137. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  1138. }
  1139. if ( bExp == 0 ) {
  1140. if ( bSig == 0 ) return packFloat32( zSign, 0, 0 );
  1141. normalizeFloat32Subnormal( bSig, &bExp, &bSig );
  1142. }
  1143. zExp = aExp + bExp - 0x7F;
  1144. aSig = ( aSig | 0x00800000 )<<7;
  1145. bSig = ( bSig | 0x00800000 )<<8;
  1146. shift64RightJamming( ( (bits64) aSig ) * bSig, 32, &zSig64 );
  1147. zSig = zSig64;
  1148. if ( 0 <= (sbits32) ( zSig<<1 ) ) {
  1149. zSig <<= 1;
  1150. --zExp;
  1151. }
  1152. return roundAndPackFloat32( roundData, zSign, zExp, zSig );
  1153. }
  1154. /*
  1155. -------------------------------------------------------------------------------
  1156. Returns the result of dividing the single-precision floating-point value `a'
  1157. by the corresponding value `b'. The operation is performed according to the
  1158. IEC/IEEE Standard for Binary Floating-point Arithmetic.
  1159. -------------------------------------------------------------------------------
  1160. */
  1161. float32 float32_div( struct roundingData *roundData, float32 a, float32 b )
  1162. {
  1163. flag aSign, bSign, zSign;
  1164. int16 aExp, bExp, zExp;
  1165. bits32 aSig, bSig, zSig;
  1166. aSig = extractFloat32Frac( a );
  1167. aExp = extractFloat32Exp( a );
  1168. aSign = extractFloat32Sign( a );
  1169. bSig = extractFloat32Frac( b );
  1170. bExp = extractFloat32Exp( b );
  1171. bSign = extractFloat32Sign( b );
  1172. zSign = aSign ^ bSign;
  1173. if ( aExp == 0xFF ) {
  1174. if ( aSig ) return propagateFloat32NaN( a, b );
  1175. if ( bExp == 0xFF ) {
  1176. if ( bSig ) return propagateFloat32NaN( a, b );
  1177. roundData->exception |= float_flag_invalid;
  1178. return float32_default_nan;
  1179. }
  1180. return packFloat32( zSign, 0xFF, 0 );
  1181. }
  1182. if ( bExp == 0xFF ) {
  1183. if ( bSig ) return propagateFloat32NaN( a, b );
  1184. return packFloat32( zSign, 0, 0 );
  1185. }
  1186. if ( bExp == 0 ) {
  1187. if ( bSig == 0 ) {
  1188. if ( ( aExp | aSig ) == 0 ) {
  1189. roundData->exception |= float_flag_invalid;
  1190. return float32_default_nan;
  1191. }
  1192. roundData->exception |= float_flag_divbyzero;
  1193. return packFloat32( zSign, 0xFF, 0 );
  1194. }
  1195. normalizeFloat32Subnormal( bSig, &bExp, &bSig );
  1196. }
  1197. if ( aExp == 0 ) {
  1198. if ( aSig == 0 ) return packFloat32( zSign, 0, 0 );
  1199. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  1200. }
  1201. zExp = aExp - bExp + 0x7D;
  1202. aSig = ( aSig | 0x00800000 )<<7;
  1203. bSig = ( bSig | 0x00800000 )<<8;
  1204. if ( bSig <= ( aSig + aSig ) ) {
  1205. aSig >>= 1;
  1206. ++zExp;
  1207. }
  1208. {
  1209. bits64 tmp = ( (bits64) aSig )<<32;
  1210. do_div( tmp, bSig );
  1211. zSig = tmp;
  1212. }
  1213. if ( ( zSig & 0x3F ) == 0 ) {
  1214. zSig |= ( ( (bits64) bSig ) * zSig != ( (bits64) aSig )<<32 );
  1215. }
  1216. return roundAndPackFloat32( roundData, zSign, zExp, zSig );
  1217. }
  1218. /*
  1219. -------------------------------------------------------------------------------
  1220. Returns the remainder of the single-precision floating-point value `a'
  1221. with respect to the corresponding value `b'. The operation is performed
  1222. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  1223. -------------------------------------------------------------------------------
  1224. */
  1225. float32 float32_rem( struct roundingData *roundData, float32 a, float32 b )
  1226. {
  1227. flag aSign, bSign, zSign;
  1228. int16 aExp, bExp, expDiff;
  1229. bits32 aSig, bSig;
  1230. bits32 q;
  1231. bits64 aSig64, bSig64, q64;
  1232. bits32 alternateASig;
  1233. sbits32 sigMean;
  1234. aSig = extractFloat32Frac( a );
  1235. aExp = extractFloat32Exp( a );
  1236. aSign = extractFloat32Sign( a );
  1237. bSig = extractFloat32Frac( b );
  1238. bExp = extractFloat32Exp( b );
  1239. bSign = extractFloat32Sign( b );
  1240. if ( aExp == 0xFF ) {
  1241. if ( aSig || ( ( bExp == 0xFF ) && bSig ) ) {
  1242. return propagateFloat32NaN( a, b );
  1243. }
  1244. roundData->exception |= float_flag_invalid;
  1245. return float32_default_nan;
  1246. }
  1247. if ( bExp == 0xFF ) {
  1248. if ( bSig ) return propagateFloat32NaN( a, b );
  1249. return a;
  1250. }
  1251. if ( bExp == 0 ) {
  1252. if ( bSig == 0 ) {
  1253. roundData->exception |= float_flag_invalid;
  1254. return float32_default_nan;
  1255. }
  1256. normalizeFloat32Subnormal( bSig, &bExp, &bSig );
  1257. }
  1258. if ( aExp == 0 ) {
  1259. if ( aSig == 0 ) return a;
  1260. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  1261. }
  1262. expDiff = aExp - bExp;
  1263. aSig |= 0x00800000;
  1264. bSig |= 0x00800000;
  1265. if ( expDiff < 32 ) {
  1266. aSig <<= 8;
  1267. bSig <<= 8;
  1268. if ( expDiff < 0 ) {
  1269. if ( expDiff < -1 ) return a;
  1270. aSig >>= 1;
  1271. }
  1272. q = ( bSig <= aSig );
  1273. if ( q ) aSig -= bSig;
  1274. if ( 0 < expDiff ) {
  1275. bits64 tmp = ( (bits64) aSig )<<32;
  1276. do_div( tmp, bSig );
  1277. q = tmp;
  1278. q >>= 32 - expDiff;
  1279. bSig >>= 2;
  1280. aSig = ( ( aSig>>1 )<<( expDiff - 1 ) ) - bSig * q;
  1281. }
  1282. else {
  1283. aSig >>= 2;
  1284. bSig >>= 2;
  1285. }
  1286. }
  1287. else {
  1288. if ( bSig <= aSig ) aSig -= bSig;
  1289. aSig64 = ( (bits64) aSig )<<40;
  1290. bSig64 = ( (bits64) bSig )<<40;
  1291. expDiff -= 64;
  1292. while ( 0 < expDiff ) {
  1293. q64 = estimateDiv128To64( aSig64, 0, bSig64 );
  1294. q64 = ( 2 < q64 ) ? q64 - 2 : 0;
  1295. aSig64 = - ( ( bSig * q64 )<<38 );
  1296. expDiff -= 62;
  1297. }
  1298. expDiff += 64;
  1299. q64 = estimateDiv128To64( aSig64, 0, bSig64 );
  1300. q64 = ( 2 < q64 ) ? q64 - 2 : 0;
  1301. q = q64>>( 64 - expDiff );
  1302. bSig <<= 6;
  1303. aSig = ( ( aSig64>>33 )<<( expDiff - 1 ) ) - bSig * q;
  1304. }
  1305. do {
  1306. alternateASig = aSig;
  1307. ++q;
  1308. aSig -= bSig;
  1309. } while ( 0 <= (sbits32) aSig );
  1310. sigMean = aSig + alternateASig;
  1311. if ( ( sigMean < 0 ) || ( ( sigMean == 0 ) && ( q & 1 ) ) ) {
  1312. aSig = alternateASig;
  1313. }
  1314. zSign = ( (sbits32) aSig < 0 );
  1315. if ( zSign ) aSig = - aSig;
  1316. return normalizeRoundAndPackFloat32( roundData, aSign ^ zSign, bExp, aSig );
  1317. }
  1318. /*
  1319. -------------------------------------------------------------------------------
  1320. Returns the square root of the single-precision floating-point value `a'.
  1321. The operation is performed according to the IEC/IEEE Standard for Binary
  1322. Floating-point Arithmetic.
  1323. -------------------------------------------------------------------------------
  1324. */
  1325. float32 float32_sqrt( struct roundingData *roundData, float32 a )
  1326. {
  1327. flag aSign;
  1328. int16 aExp, zExp;
  1329. bits32 aSig, zSig;
  1330. bits64 rem, term;
  1331. aSig = extractFloat32Frac( a );
  1332. aExp = extractFloat32Exp( a );
  1333. aSign = extractFloat32Sign( a );
  1334. if ( aExp == 0xFF ) {
  1335. if ( aSig ) return propagateFloat32NaN( a, 0 );
  1336. if ( ! aSign ) return a;
  1337. roundData->exception |= float_flag_invalid;
  1338. return float32_default_nan;
  1339. }
  1340. if ( aSign ) {
  1341. if ( ( aExp | aSig ) == 0 ) return a;
  1342. roundData->exception |= float_flag_invalid;
  1343. return float32_default_nan;
  1344. }
  1345. if ( aExp == 0 ) {
  1346. if ( aSig == 0 ) return 0;
  1347. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  1348. }
  1349. zExp = ( ( aExp - 0x7F )>>1 ) + 0x7E;
  1350. aSig = ( aSig | 0x00800000 )<<8;
  1351. zSig = estimateSqrt32( aExp, aSig ) + 2;
  1352. if ( ( zSig & 0x7F ) <= 5 ) {
  1353. if ( zSig < 2 ) {
  1354. zSig = 0xFFFFFFFF;
  1355. }
  1356. else {
  1357. aSig >>= aExp & 1;
  1358. term = ( (bits64) zSig ) * zSig;
  1359. rem = ( ( (bits64) aSig )<<32 ) - term;
  1360. while ( (sbits64) rem < 0 ) {
  1361. --zSig;
  1362. rem += ( ( (bits64) zSig )<<1 ) | 1;
  1363. }
  1364. zSig |= ( rem != 0 );
  1365. }
  1366. }
  1367. shift32RightJamming( zSig, 1, &zSig );
  1368. return roundAndPackFloat32( roundData, 0, zExp, zSig );
  1369. }
  1370. /*
  1371. -------------------------------------------------------------------------------
  1372. Returns 1 if the single-precision floating-point value `a' is equal to the
  1373. corresponding value `b', and 0 otherwise. The comparison is performed
  1374. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  1375. -------------------------------------------------------------------------------
  1376. */
  1377. flag float32_eq( float32 a, float32 b )
  1378. {
  1379. if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
  1380. || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
  1381. ) {
  1382. if ( float32_is_signaling_nan( a ) || float32_is_signaling_nan( b ) ) {
  1383. float_raise( float_flag_invalid );
  1384. }
  1385. return 0;
  1386. }
  1387. return ( a == b ) || ( (bits32) ( ( a | b )<<1 ) == 0 );
  1388. }
  1389. /*
  1390. -------------------------------------------------------------------------------
  1391. Returns 1 if the single-precision floating-point value `a' is less than or
  1392. equal to the corresponding value `b', and 0 otherwise. The comparison is
  1393. performed according to the IEC/IEEE Standard for Binary Floating-point
  1394. Arithmetic.
  1395. -------------------------------------------------------------------------------
  1396. */
  1397. flag float32_le( float32 a, float32 b )
  1398. {
  1399. flag aSign, bSign;
  1400. if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
  1401. || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
  1402. ) {
  1403. float_raise( float_flag_invalid );
  1404. return 0;
  1405. }
  1406. aSign = extractFloat32Sign( a );
  1407. bSign = extractFloat32Sign( b );
  1408. if ( aSign != bSign ) return aSign || ( (bits32) ( ( a | b )<<1 ) == 0 );
  1409. return ( a == b ) || ( aSign ^ ( a < b ) );
  1410. }
  1411. /*
  1412. -------------------------------------------------------------------------------
  1413. Returns 1 if the single-precision floating-point value `a' is less than
  1414. the corresponding value `b', and 0 otherwise. The comparison is performed
  1415. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  1416. -------------------------------------------------------------------------------
  1417. */
  1418. flag float32_lt( float32 a, float32 b )
  1419. {
  1420. flag aSign, bSign;
  1421. if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
  1422. || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
  1423. ) {
  1424. float_raise( float_flag_invalid );
  1425. return 0;
  1426. }
  1427. aSign = extractFloat32Sign( a );
  1428. bSign = extractFloat32Sign( b );
  1429. if ( aSign != bSign ) return aSign && ( (bits32) ( ( a | b )<<1 ) != 0 );
  1430. return ( a != b ) && ( aSign ^ ( a < b ) );
  1431. }
  1432. /*
  1433. -------------------------------------------------------------------------------
  1434. Returns 1 if the single-precision floating-point value `a' is equal to the
  1435. corresponding value `b', and 0 otherwise. The invalid exception is raised
  1436. if either operand is a NaN. Otherwise, the comparison is performed
  1437. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  1438. -------------------------------------------------------------------------------
  1439. */
  1440. flag float32_eq_signaling( float32 a, float32 b )
  1441. {
  1442. if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
  1443. || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
  1444. ) {
  1445. float_raise( float_flag_invalid );
  1446. return 0;
  1447. }
  1448. return ( a == b ) || ( (bits32) ( ( a | b )<<1 ) == 0 );
  1449. }
  1450. /*
  1451. -------------------------------------------------------------------------------
  1452. Returns 1 if the single-precision floating-point value `a' is less than or
  1453. equal to the corresponding value `b', and 0 otherwise. Quiet NaNs do not
  1454. cause an exception. Otherwise, the comparison is performed according to the
  1455. IEC/IEEE Standard for Binary Floating-point Arithmetic.
  1456. -------------------------------------------------------------------------------
  1457. */
  1458. flag float32_le_quiet( float32 a, float32 b )
  1459. {
  1460. flag aSign, bSign;
  1461. //int16 aExp, bExp;
  1462. if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
  1463. || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
  1464. ) {
  1465. /* Do nothing, even if NaN as we're quiet */
  1466. return 0;
  1467. }
  1468. aSign = extractFloat32Sign( a );
  1469. bSign = extractFloat32Sign( b );
  1470. if ( aSign != bSign ) return aSign || ( (bits32) ( ( a | b )<<1 ) == 0 );
  1471. return ( a == b ) || ( aSign ^ ( a < b ) );
  1472. }
  1473. /*
  1474. -------------------------------------------------------------------------------
  1475. Returns 1 if the single-precision floating-point value `a' is less than
  1476. the corresponding value `b', and 0 otherwise. Quiet NaNs do not cause an
  1477. exception. Otherwise, the comparison is performed according to the IEC/IEEE
  1478. Standard for Binary Floating-point Arithmetic.
  1479. -------------------------------------------------------------------------------
  1480. */
  1481. flag float32_lt_quiet( float32 a, float32 b )
  1482. {
  1483. flag aSign, bSign;
  1484. if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
  1485. || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
  1486. ) {
  1487. /* Do nothing, even if NaN as we're quiet */
  1488. return 0;
  1489. }
  1490. aSign = extractFloat32Sign( a );
  1491. bSign = extractFloat32Sign( b );
  1492. if ( aSign != bSign ) return aSign && ( (bits32) ( ( a | b )<<1 ) != 0 );
  1493. return ( a != b ) && ( aSign ^ ( a < b ) );
  1494. }
  1495. /*
  1496. -------------------------------------------------------------------------------
  1497. Returns the result of converting the double-precision floating-point value
  1498. `a' to the 32-bit two's complement integer format. The conversion is
  1499. performed according to the IEC/IEEE Standard for Binary Floating-point
  1500. Arithmetic---which means in particular that the conversion is rounded
  1501. according to the current rounding mode. If `a' is a NaN, the largest
  1502. positive integer is returned. Otherwise, if the conversion overflows, the
  1503. largest integer with the same sign as `a' is returned.
  1504. -------------------------------------------------------------------------------
  1505. */
  1506. int32 float64_to_int32( struct roundingData *roundData, float64 a )
  1507. {
  1508. flag aSign;
  1509. int16 aExp, shiftCount;
  1510. bits64 aSig;
  1511. aSig = extractFloat64Frac( a );
  1512. aExp = extractFloat64Exp( a );
  1513. aSign = extractFloat64Sign( a );
  1514. if ( ( aExp == 0x7FF ) && aSig ) aSign = 0;
  1515. if ( aExp ) aSig |= LIT64( 0x0010000000000000 );
  1516. shiftCount = 0x42C - aExp;
  1517. if ( 0 < shiftCount ) shift64RightJamming( aSig, shiftCount, &aSig );
  1518. return roundAndPackInt32( roundData, aSign, aSig );
  1519. }
  1520. /*
  1521. -------------------------------------------------------------------------------
  1522. Returns the result of converting the double-precision floating-point value
  1523. `a' to the 32-bit two's complement integer format. The conversion is
  1524. performed according to the IEC/IEEE Standard for Binary Floating-point
  1525. Arithmetic, except that the conversion is always rounded toward zero. If
  1526. `a' is a NaN, the largest positive integer is returned. Otherwise, if the
  1527. conversion overflows, the largest integer with the same sign as `a' is
  1528. returned.
  1529. -------------------------------------------------------------------------------
  1530. */
  1531. int32 float64_to_int32_round_to_zero( float64 a )
  1532. {
  1533. flag aSign;
  1534. int16 aExp, shiftCount;
  1535. bits64 aSig, savedASig;
  1536. int32 z;
  1537. aSig = extractFloat64Frac( a );
  1538. aExp = extractFloat64Exp( a );
  1539. aSign = extractFloat64Sign( a );
  1540. shiftCount = 0x433 - aExp;
  1541. if ( shiftCount < 21 ) {
  1542. if ( ( aExp == 0x7FF ) && aSig ) aSign = 0;
  1543. goto invalid;
  1544. }
  1545. else if ( 52 < shiftCount ) {
  1546. if ( aExp || aSig ) float_raise( float_flag_inexact );
  1547. return 0;
  1548. }
  1549. aSig |= LIT64( 0x0010000000000000 );
  1550. savedASig = aSig;
  1551. aSig >>= shiftCount;
  1552. z = aSig;
  1553. if ( aSign ) z = - z;
  1554. if ( ( z < 0 ) ^ aSign ) {
  1555. invalid:
  1556. float_raise( float_flag_invalid );
  1557. return aSign ? 0x80000000 : 0x7FFFFFFF;
  1558. }
  1559. if ( ( aSig<<shiftCount ) != savedASig ) {
  1560. float_raise( float_flag_inexact );
  1561. }
  1562. return z;
  1563. }
  1564. /*
  1565. -------------------------------------------------------------------------------
  1566. Returns the result of converting the double-precision floating-point value
  1567. `a' to the 32-bit two's complement unsigned integer format. The conversion
  1568. is performed according to the IEC/IEEE Standard for Binary Floating-point
  1569. Arithmetic---which means in particular that the conversion is rounded
  1570. according to the current rounding mode. If `a' is a NaN, the largest
  1571. positive integer is returned. Otherwise, if the conversion overflows, the
  1572. largest positive integer is returned.
  1573. -------------------------------------------------------------------------------
  1574. */
  1575. int32 float64_to_uint32( struct roundingData *roundData, float64 a )
  1576. {
  1577. flag aSign;
  1578. int16 aExp, shiftCount;
  1579. bits64 aSig;
  1580. aSig = extractFloat64Frac( a );
  1581. aExp = extractFloat64Exp( a );
  1582. aSign = 0; //extractFloat64Sign( a );
  1583. //if ( ( aExp == 0x7FF ) && aSig ) aSign = 0;
  1584. if ( aExp ) aSig |= LIT64( 0x0010000000000000 );
  1585. shiftCount = 0x42C - aExp;
  1586. if ( 0 < shiftCount ) shift64RightJamming( aSig, shiftCount, &aSig );
  1587. return roundAndPackInt32( roundData, aSign, aSig );
  1588. }
  1589. /*
  1590. -------------------------------------------------------------------------------
  1591. Returns the result of converting the double-precision floating-point value
  1592. `a' to the 32-bit two's complement integer format. The conversion is
  1593. performed according to the IEC/IEEE Standard for Binary Floating-point
  1594. Arithmetic, except that the conversion is always rounded toward zero. If
  1595. `a' is a NaN, the largest positive integer is returned. Otherwise, if the
  1596. conversion overflows, the largest positive integer is returned.
  1597. -------------------------------------------------------------------------------
  1598. */
  1599. int32 float64_to_uint32_round_to_zero( float64 a )
  1600. {
  1601. flag aSign;
  1602. int16 aExp, shiftCount;
  1603. bits64 aSig, savedASig;
  1604. int32 z;
  1605. aSig = extractFloat64Frac( a );
  1606. aExp = extractFloat64Exp( a );
  1607. aSign = extractFloat64Sign( a );
  1608. shiftCount = 0x433 - aExp;
  1609. if ( shiftCount < 21 ) {
  1610. if ( ( aExp == 0x7FF ) && aSig ) aSign = 0;
  1611. goto invalid;
  1612. }
  1613. else if ( 52 < shiftCount ) {
  1614. if ( aExp || aSig ) float_raise( float_flag_inexact );
  1615. return 0;
  1616. }
  1617. aSig |= LIT64( 0x0010000000000000 );
  1618. savedASig = aSig;
  1619. aSig >>= shiftCount;
  1620. z = aSig;
  1621. if ( aSign ) z = - z;
  1622. if ( ( z < 0 ) ^ aSign ) {
  1623. invalid:
  1624. float_raise( float_flag_invalid );
  1625. return aSign ? 0x80000000 : 0x7FFFFFFF;
  1626. }
  1627. if ( ( aSig<<shiftCount ) != savedASig ) {
  1628. float_raise( float_flag_inexact );
  1629. }
  1630. return z;
  1631. }
  1632. /*
  1633. -------------------------------------------------------------------------------
  1634. Returns the result of converting the double-precision floating-point value
  1635. `a' to the single-precision floating-point format. The conversion is
  1636. performed according to the IEC/IEEE Standard for Binary Floating-point
  1637. Arithmetic.
  1638. -------------------------------------------------------------------------------
  1639. */
  1640. float32 float64_to_float32( struct roundingData *roundData, float64 a )
  1641. {
  1642. flag aSign;
  1643. int16 aExp;
  1644. bits64 aSig;
  1645. bits32 zSig;
  1646. aSig = extractFloat64Frac( a );
  1647. aExp = extractFloat64Exp( a );
  1648. aSign = extractFloat64Sign( a );
  1649. if ( aExp == 0x7FF ) {
  1650. if ( aSig ) return commonNaNToFloat32( float64ToCommonNaN( a ) );
  1651. return packFloat32( aSign, 0xFF, 0 );
  1652. }
  1653. shift64RightJamming( aSig, 22, &aSig );
  1654. zSig = aSig;
  1655. if ( aExp || zSig ) {
  1656. zSig |= 0x40000000;
  1657. aExp -= 0x381;
  1658. }
  1659. return roundAndPackFloat32( roundData, aSign, aExp, zSig );
  1660. }
  1661. #ifdef FLOATX80
  1662. /*
  1663. -------------------------------------------------------------------------------
  1664. Returns the result of converting the double-precision floating-point value
  1665. `a' to the extended double-precision floating-point format. The conversion
  1666. is performed according to the IEC/IEEE Standard for Binary Floating-point
  1667. Arithmetic.
  1668. -------------------------------------------------------------------------------
  1669. */
  1670. floatx80 float64_to_floatx80( float64 a )
  1671. {
  1672. flag aSign;
  1673. int16 aExp;
  1674. bits64 aSig;
  1675. aSig = extractFloat64Frac( a );
  1676. aExp = extractFloat64Exp( a );
  1677. aSign = extractFloat64Sign( a );
  1678. if ( aExp == 0x7FF ) {
  1679. if ( aSig ) return commonNaNToFloatx80( float64ToCommonNaN( a ) );
  1680. return packFloatx80( aSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
  1681. }
  1682. if ( aExp == 0 ) {
  1683. if ( aSig == 0 ) return packFloatx80( aSign, 0, 0 );
  1684. normalizeFloat64Subnormal( aSig, &aExp, &aSig );
  1685. }
  1686. return
  1687. packFloatx80(
  1688. aSign, aExp + 0x3C00, ( aSig | LIT64( 0x0010000000000000 ) )<<11 );
  1689. }
  1690. #endif
  1691. /*
  1692. -------------------------------------------------------------------------------
  1693. Rounds the double-precision floating-point value `a' to an integer, and
  1694. returns the result as a double-precision floating-point value. The
  1695. operation is performed according to the IEC/IEEE Standard for Binary
  1696. Floating-point Arithmetic.
  1697. -------------------------------------------------------------------------------
  1698. */
  1699. float64 float64_round_to_int( struct roundingData *roundData, float64 a )
  1700. {
  1701. flag aSign;
  1702. int16 aExp;
  1703. bits64 lastBitMask, roundBitsMask;
  1704. int8 roundingMode;
  1705. float64 z;
  1706. aExp = extractFloat64Exp( a );
  1707. if ( 0x433 <= aExp ) {
  1708. if ( ( aExp == 0x7FF ) && extractFloat64Frac( a ) ) {
  1709. return propagateFloat64NaN( a, a );
  1710. }
  1711. return a;
  1712. }
  1713. if ( aExp <= 0x3FE ) {
  1714. if ( (bits64) ( a<<1 ) == 0 ) return a;
  1715. roundData->exception |= float_flag_inexact;
  1716. aSign = extractFloat64Sign( a );
  1717. switch ( roundData->mode ) {
  1718. case float_round_nearest_even:
  1719. if ( ( aExp == 0x3FE ) && extractFloat64Frac( a ) ) {
  1720. return packFloat64( aSign, 0x3FF, 0 );
  1721. }
  1722. break;
  1723. case float_round_down:
  1724. return aSign ? LIT64( 0xBFF0000000000000 ) : 0;
  1725. case float_round_up:
  1726. return
  1727. aSign ? LIT64( 0x8000000000000000 ) : LIT64( 0x3FF0000000000000 );
  1728. }
  1729. return packFloat64( aSign, 0, 0 );
  1730. }
  1731. lastBitMask = 1;
  1732. lastBitMask <<= 0x433 - aExp;
  1733. roundBitsMask = lastBitMask - 1;
  1734. z = a;
  1735. roundingMode = roundData->mode;
  1736. if ( roundingMode == float_round_nearest_even ) {
  1737. z += lastBitMask>>1;
  1738. if ( ( z & roundBitsMask ) == 0 ) z &= ~ lastBitMask;
  1739. }
  1740. else if ( roundingMode != float_round_to_zero ) {
  1741. if ( extractFloat64Sign( z ) ^ ( roundingMode == float_round_up ) ) {
  1742. z += roundBitsMask;
  1743. }
  1744. }
  1745. z &= ~ roundBitsMask;
  1746. if ( z != a ) roundData->exception |= float_flag_inexact;
  1747. return z;
  1748. }
  1749. /*
  1750. -------------------------------------------------------------------------------
  1751. Returns the result of adding the absolute values of the double-precision
  1752. floating-point values `a' and `b'. If `zSign' is true, the sum is negated
  1753. before being returned. `zSign' is ignored if the result is a NaN. The
  1754. addition is performed according to the IEC/IEEE Standard for Binary
  1755. Floating-point Arithmetic.
  1756. -------------------------------------------------------------------------------
  1757. */
  1758. static float64 addFloat64Sigs( struct roundingData *roundData, float64 a, float64 b, flag zSign )
  1759. {
  1760. int16 aExp, bExp, zExp;
  1761. bits64 aSig, bSig, zSig;
  1762. int16 expDiff;
  1763. aSig = extractFloat64Frac( a );
  1764. aExp = extractFloat64Exp( a );
  1765. bSig = extractFloat64Frac( b );
  1766. bExp = extractFloat64Exp( b );
  1767. expDiff = aExp - bExp;
  1768. aSig <<= 9;
  1769. bSig <<= 9;
  1770. if ( 0 < expDiff ) {
  1771. if ( aExp == 0x7FF ) {
  1772. if ( aSig ) return propagateFloat64NaN( a, b );
  1773. return a;
  1774. }
  1775. if ( bExp == 0 ) {
  1776. --expDiff;
  1777. }
  1778. else {
  1779. bSig |= LIT64( 0x2000000000000000 );
  1780. }
  1781. shift64RightJamming( bSig, expDiff, &bSig );
  1782. zExp = aExp;
  1783. }
  1784. else if ( expDiff < 0 ) {
  1785. if ( bExp == 0x7FF ) {
  1786. if ( bSig ) return propagateFloat64NaN( a, b );
  1787. return packFloat64( zSign, 0x7FF, 0 );
  1788. }
  1789. if ( aExp == 0 ) {
  1790. ++expDiff;
  1791. }
  1792. else {
  1793. aSig |= LIT64( 0x2000000000000000 );
  1794. }
  1795. shift64RightJamming( aSig, - expDiff, &aSig );
  1796. zExp = bExp;
  1797. }
  1798. else {
  1799. if ( aExp == 0x7FF ) {
  1800. if ( aSig | bSig ) return propagateFloat64NaN( a, b );
  1801. return a;
  1802. }
  1803. if ( aExp == 0 ) return packFloat64( zSign, 0, ( aSig + bSig )>>9 );
  1804. zSig = LIT64( 0x4000000000000000 ) + aSig + bSig;
  1805. zExp = aExp;
  1806. goto roundAndPack;
  1807. }
  1808. aSig |= LIT64( 0x2000000000000000 );
  1809. zSig = ( aSig + bSig )<<1;
  1810. --zExp;
  1811. if ( (sbits64) zSig < 0 ) {
  1812. zSig = aSig + bSig;
  1813. ++zExp;
  1814. }
  1815. roundAndPack:
  1816. return roundAndPackFloat64( roundData, zSign, zExp, zSig );
  1817. }
  1818. /*
  1819. -------------------------------------------------------------------------------
  1820. Returns the result of subtracting the absolute values of the double-
  1821. precision floating-point values `a' and `b'. If `zSign' is true, the
  1822. difference is negated before being returned. `zSign' is ignored if the
  1823. result is a NaN. The subtraction is performed according to the IEC/IEEE
  1824. Standard for Binary Floating-point Arithmetic.
  1825. -------------------------------------------------------------------------------
  1826. */
  1827. static float64 subFloat64Sigs( struct roundingData *roundData, float64 a, float64 b, flag zSign )
  1828. {
  1829. int16 aExp, bExp, zExp;
  1830. bits64 aSig, bSig, zSig;
  1831. int16 expDiff;
  1832. aSig = extractFloat64Frac( a );
  1833. aExp = extractFloat64Exp( a );
  1834. bSig = extractFloat64Frac( b );
  1835. bExp = extractFloat64Exp( b );
  1836. expDiff = aExp - bExp;
  1837. aSig <<= 10;
  1838. bSig <<= 10;
  1839. if ( 0 < expDiff ) goto aExpBigger;
  1840. if ( expDiff < 0 ) goto bExpBigger;
  1841. if ( aExp == 0x7FF ) {
  1842. if ( aSig | bSig ) return propagateFloat64NaN( a, b );
  1843. roundData->exception |= float_flag_invalid;
  1844. return float64_default_nan;
  1845. }
  1846. if ( aExp == 0 ) {
  1847. aExp = 1;
  1848. bExp = 1;
  1849. }
  1850. if ( bSig < aSig ) goto aBigger;
  1851. if ( aSig < bSig ) goto bBigger;
  1852. return packFloat64( roundData->mode == float_round_down, 0, 0 );
  1853. bExpBigger:
  1854. if ( bExp == 0x7FF ) {
  1855. if ( bSig ) return propagateFloat64NaN( a, b );
  1856. return packFloat64( zSign ^ 1, 0x7FF, 0 );
  1857. }
  1858. if ( aExp == 0 ) {
  1859. ++expDiff;
  1860. }
  1861. else {
  1862. aSig |= LIT64( 0x4000000000000000 );
  1863. }
  1864. shift64RightJamming( aSig, - expDiff, &aSig );
  1865. bSig |= LIT64( 0x4000000000000000 );
  1866. bBigger:
  1867. zSig = bSig - aSig;
  1868. zExp = bExp;
  1869. zSign ^= 1;
  1870. goto normalizeRoundAndPack;
  1871. aExpBigger:
  1872. if ( aExp == 0x7FF ) {
  1873. if ( aSig ) return propagateFloat64NaN( a, b );
  1874. return a;
  1875. }
  1876. if ( bExp == 0 ) {
  1877. --expDiff;
  1878. }
  1879. else {
  1880. bSig |= LIT64( 0x4000000000000000 );
  1881. }
  1882. shift64RightJamming( bSig, expDiff, &bSig );
  1883. aSig |= LIT64( 0x4000000000000000 );
  1884. aBigger:
  1885. zSig = aSig - bSig;
  1886. zExp = aExp;
  1887. normalizeRoundAndPack:
  1888. --zExp;
  1889. return normalizeRoundAndPackFloat64( roundData, zSign, zExp, zSig );
  1890. }
  1891. /*
  1892. -------------------------------------------------------------------------------
  1893. Returns the result of adding the double-precision floating-point values `a'
  1894. and `b'. The operation is performed according to the IEC/IEEE Standard for
  1895. Binary Floating-point Arithmetic.
  1896. -------------------------------------------------------------------------------
  1897. */
  1898. float64 float64_add( struct roundingData *roundData, float64 a, float64 b )
  1899. {
  1900. flag aSign, bSign;
  1901. aSign = extractFloat64Sign( a );
  1902. bSign = extractFloat64Sign( b );
  1903. if ( aSign == bSign ) {
  1904. return addFloat64Sigs( roundData, a, b, aSign );
  1905. }
  1906. else {
  1907. return subFloat64Sigs( roundData, a, b, aSign );
  1908. }
  1909. }
  1910. /*
  1911. -------------------------------------------------------------------------------
  1912. Returns the result of subtracting the double-precision floating-point values
  1913. `a' and `b'. The operation is performed according to the IEC/IEEE Standard
  1914. for Binary Floating-point Arithmetic.
  1915. -------------------------------------------------------------------------------
  1916. */
  1917. float64 float64_sub( struct roundingData *roundData, float64 a, float64 b )
  1918. {
  1919. flag aSign, bSign;
  1920. aSign = extractFloat64Sign( a );
  1921. bSign = extractFloat64Sign( b );
  1922. if ( aSign == bSign ) {
  1923. return subFloat64Sigs( roundData, a, b, aSign );
  1924. }
  1925. else {
  1926. return addFloat64Sigs( roundData, a, b, aSign );
  1927. }
  1928. }
  1929. /*
  1930. -------------------------------------------------------------------------------
  1931. Returns the result of multiplying the double-precision floating-point values
  1932. `a' and `b'. The operation is performed according to the IEC/IEEE Standard
  1933. for Binary Floating-point Arithmetic.
  1934. -------------------------------------------------------------------------------
  1935. */
  1936. float64 float64_mul( struct roundingData *roundData, float64 a, float64 b )
  1937. {
  1938. flag aSign, bSign, zSign;
  1939. int16 aExp, bExp, zExp;
  1940. bits64 aSig, bSig, zSig0, zSig1;
  1941. aSig = extractFloat64Frac( a );
  1942. aExp = extractFloat64Exp( a );
  1943. aSign = extractFloat64Sign( a );
  1944. bSig = extractFloat64Frac( b );
  1945. bExp = extractFloat64Exp( b );
  1946. bSign = extractFloat64Sign( b );
  1947. zSign = aSign ^ bSign;
  1948. if ( aExp == 0x7FF ) {
  1949. if ( aSig || ( ( bExp == 0x7FF ) && bSig ) ) {
  1950. return propagateFloat64NaN( a, b );
  1951. }
  1952. if ( ( bExp | bSig ) == 0 ) {
  1953. roundData->exception |= float_flag_invalid;
  1954. return float64_default_nan;
  1955. }
  1956. return packFloat64( zSign, 0x7FF, 0 );
  1957. }
  1958. if ( bExp == 0x7FF ) {
  1959. if ( bSig ) return propagateFloat64NaN( a, b );
  1960. if ( ( aExp | aSig ) == 0 ) {
  1961. roundData->exception |= float_flag_invalid;
  1962. return float64_default_nan;
  1963. }
  1964. return packFloat64( zSign, 0x7FF, 0 );
  1965. }
  1966. if ( aExp == 0 ) {
  1967. if ( aSig == 0 ) return packFloat64( zSign, 0, 0 );
  1968. normalizeFloat64Subnormal( aSig, &aExp, &aSig );
  1969. }
  1970. if ( bExp == 0 ) {
  1971. if ( bSig == 0 ) return packFloat64( zSign, 0, 0 );
  1972. normalizeFloat64Subnormal( bSig, &bExp, &bSig );
  1973. }
  1974. zExp = aExp + bExp - 0x3FF;
  1975. aSig = ( aSig | LIT64( 0x0010000000000000 ) )<<10;
  1976. bSig = ( bSig | LIT64( 0x0010000000000000 ) )<<11;
  1977. mul64To128( aSig, bSig, &zSig0, &zSig1 );
  1978. zSig0 |= ( zSig1 != 0 );
  1979. if ( 0 <= (sbits64) ( zSig0<<1 ) ) {
  1980. zSig0 <<= 1;
  1981. --zExp;
  1982. }
  1983. return roundAndPackFloat64( roundData, zSign, zExp, zSig0 );
  1984. }
  1985. /*
  1986. -------------------------------------------------------------------------------
  1987. Returns the result of dividing the double-precision floating-point value `a'
  1988. by the corresponding value `b'. The operation is performed according to
  1989. the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  1990. -------------------------------------------------------------------------------
  1991. */
  1992. float64 float64_div( struct roundingData *roundData, float64 a, float64 b )
  1993. {
  1994. flag aSign, bSign, zSign;
  1995. int16 aExp, bExp, zExp;
  1996. bits64 aSig, bSig, zSig;
  1997. bits64 rem0, rem1;
  1998. bits64 term0, term1;
  1999. aSig = extractFloat64Frac( a );
  2000. aExp = extractFloat64Exp( a );
  2001. aSign = extractFloat64Sign( a );
  2002. bSig = extractFloat64Frac( b );
  2003. bExp = extractFloat64Exp( b );
  2004. bSign = extractFloat64Sign( b );
  2005. zSign = aSign ^ bSign;
  2006. if ( aExp == 0x7FF ) {
  2007. if ( aSig ) return propagateFloat64NaN( a, b );
  2008. if ( bExp == 0x7FF ) {
  2009. if ( bSig ) return propagateFloat64NaN( a, b );
  2010. roundData->exception |= float_flag_invalid;
  2011. return float64_default_nan;
  2012. }
  2013. return packFloat64( zSign, 0x7FF, 0 );
  2014. }
  2015. if ( bExp == 0x7FF ) {
  2016. if ( bSig ) return propagateFloat64NaN( a, b );
  2017. return packFloat64( zSign, 0, 0 );
  2018. }
  2019. if ( bExp == 0 ) {
  2020. if ( bSig == 0 ) {
  2021. if ( ( aExp | aSig ) == 0 ) {
  2022. roundData->exception |= float_flag_invalid;
  2023. return float64_default_nan;
  2024. }
  2025. roundData->exception |= float_flag_divbyzero;
  2026. return packFloat64( zSign, 0x7FF, 0 );
  2027. }
  2028. normalizeFloat64Subnormal( bSig, &bExp, &bSig );
  2029. }
  2030. if ( aExp == 0 ) {
  2031. if ( aSig == 0 ) return packFloat64( zSign, 0, 0 );
  2032. normalizeFloat64Subnormal( aSig, &aExp, &aSig );
  2033. }
  2034. zExp = aExp - bExp + 0x3FD;
  2035. aSig = ( aSig | LIT64( 0x0010000000000000 ) )<<10;
  2036. bSig = ( bSig | LIT64( 0x0010000000000000 ) )<<11;
  2037. if ( bSig <= ( aSig + aSig ) ) {
  2038. aSig >>= 1;
  2039. ++zExp;
  2040. }
  2041. zSig = estimateDiv128To64( aSig, 0, bSig );
  2042. if ( ( zSig & 0x1FF ) <= 2 ) {
  2043. mul64To128( bSig, zSig, &term0, &term1 );
  2044. sub128( aSig, 0, term0, term1, &rem0, &rem1 );
  2045. while ( (sbits64) rem0 < 0 ) {
  2046. --zSig;
  2047. add128( rem0, rem1, 0, bSig, &rem0, &rem1 );
  2048. }
  2049. zSig |= ( rem1 != 0 );
  2050. }
  2051. return roundAndPackFloat64( roundData, zSign, zExp, zSig );
  2052. }
  2053. /*
  2054. -------------------------------------------------------------------------------
  2055. Returns the remainder of the double-precision floating-point value `a'
  2056. with respect to the corresponding value `b'. The operation is performed
  2057. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  2058. -------------------------------------------------------------------------------
  2059. */
  2060. float64 float64_rem( struct roundingData *roundData, float64 a, float64 b )
  2061. {
  2062. flag aSign, bSign, zSign;
  2063. int16 aExp, bExp, expDiff;
  2064. bits64 aSig, bSig;
  2065. bits64 q, alternateASig;
  2066. sbits64 sigMean;
  2067. aSig = extractFloat64Frac( a );
  2068. aExp = extractFloat64Exp( a );
  2069. aSign = extractFloat64Sign( a );
  2070. bSig = extractFloat64Frac( b );
  2071. bExp = extractFloat64Exp( b );
  2072. bSign = extractFloat64Sign( b );
  2073. if ( aExp == 0x7FF ) {
  2074. if ( aSig || ( ( bExp == 0x7FF ) && bSig ) ) {
  2075. return propagateFloat64NaN( a, b );
  2076. }
  2077. roundData->exception |= float_flag_invalid;
  2078. return float64_default_nan;
  2079. }
  2080. if ( bExp == 0x7FF ) {
  2081. if ( bSig ) return propagateFloat64NaN( a, b );
  2082. return a;
  2083. }
  2084. if ( bExp == 0 ) {
  2085. if ( bSig == 0 ) {
  2086. roundData->exception |= float_flag_invalid;
  2087. return float64_default_nan;
  2088. }
  2089. normalizeFloat64Subnormal( bSig, &bExp, &bSig );
  2090. }
  2091. if ( aExp == 0 ) {
  2092. if ( aSig == 0 ) return a;
  2093. normalizeFloat64Subnormal( aSig, &aExp, &aSig );
  2094. }
  2095. expDiff = aExp - bExp;
  2096. aSig = ( aSig | LIT64( 0x0010000000000000 ) )<<11;
  2097. bSig = ( bSig | LIT64( 0x0010000000000000 ) )<<11;
  2098. if ( expDiff < 0 ) {
  2099. if ( expDiff < -1 ) return a;
  2100. aSig >>= 1;
  2101. }
  2102. q = ( bSig <= aSig );
  2103. if ( q ) aSig -= bSig;
  2104. expDiff -= 64;
  2105. while ( 0 < expDiff ) {
  2106. q = estimateDiv128To64( aSig, 0, bSig );
  2107. q = ( 2 < q ) ? q - 2 : 0;
  2108. aSig = - ( ( bSig>>2 ) * q );
  2109. expDiff -= 62;
  2110. }
  2111. expDiff += 64;
  2112. if ( 0 < expDiff ) {
  2113. q = estimateDiv128To64( aSig, 0, bSig );
  2114. q = ( 2 < q ) ? q - 2 : 0;
  2115. q >>= 64 - expDiff;
  2116. bSig >>= 2;
  2117. aSig = ( ( aSig>>1 )<<( expDiff - 1 ) ) - bSig * q;
  2118. }
  2119. else {
  2120. aSig >>= 2;
  2121. bSig >>= 2;
  2122. }
  2123. do {
  2124. alternateASig = aSig;
  2125. ++q;
  2126. aSig -= bSig;
  2127. } while ( 0 <= (sbits64) aSig );
  2128. sigMean = aSig + alternateASig;
  2129. if ( ( sigMean < 0 ) || ( ( sigMean == 0 ) && ( q & 1 ) ) ) {
  2130. aSig = alternateASig;
  2131. }
  2132. zSign = ( (sbits64) aSig < 0 );
  2133. if ( zSign ) aSig = - aSig;
  2134. return normalizeRoundAndPackFloat64( roundData, aSign ^ zSign, bExp, aSig );
  2135. }
  2136. /*
  2137. -------------------------------------------------------------------------------
  2138. Returns the square root of the double-precision floating-point value `a'.
  2139. The operation is performed according to the IEC/IEEE Standard for Binary
  2140. Floating-point Arithmetic.
  2141. -------------------------------------------------------------------------------
  2142. */
  2143. float64 float64_sqrt( struct roundingData *roundData, float64 a )
  2144. {
  2145. flag aSign;
  2146. int16 aExp, zExp;
  2147. bits64 aSig, zSig;
  2148. bits64 rem0, rem1, term0, term1; //, shiftedRem;
  2149. //float64 z;
  2150. aSig = extractFloat64Frac( a );
  2151. aExp = extractFloat64Exp( a );
  2152. aSign = extractFloat64Sign( a );
  2153. if ( aExp == 0x7FF ) {
  2154. if ( aSig ) return propagateFloat64NaN( a, a );
  2155. if ( ! aSign ) return a;
  2156. roundData->exception |= float_flag_invalid;
  2157. return float64_default_nan;
  2158. }
  2159. if ( aSign ) {
  2160. if ( ( aExp | aSig ) == 0 ) return a;
  2161. roundData->exception |= float_flag_invalid;
  2162. return float64_default_nan;
  2163. }
  2164. if ( aExp == 0 ) {
  2165. if ( aSig == 0 ) return 0;
  2166. normalizeFloat64Subnormal( aSig, &aExp, &aSig );
  2167. }
  2168. zExp = ( ( aExp - 0x3FF )>>1 ) + 0x3FE;
  2169. aSig |= LIT64( 0x0010000000000000 );
  2170. zSig = estimateSqrt32( aExp, aSig>>21 );
  2171. zSig <<= 31;
  2172. aSig <<= 9 - ( aExp & 1 );
  2173. zSig = estimateDiv128To64( aSig, 0, zSig ) + zSig + 2;
  2174. if ( ( zSig & 0x3FF ) <= 5 ) {
  2175. if ( zSig < 2 ) {
  2176. zSig = LIT64( 0xFFFFFFFFFFFFFFFF );
  2177. }
  2178. else {
  2179. aSig <<= 2;
  2180. mul64To128( zSig, zSig, &term0, &term1 );
  2181. sub128( aSig, 0, term0, term1, &rem0, &rem1 );
  2182. while ( (sbits64) rem0 < 0 ) {
  2183. --zSig;
  2184. shortShift128Left( 0, zSig, 1, &term0, &term1 );
  2185. term1 |= 1;
  2186. add128( rem0, rem1, term0, term1, &rem0, &rem1 );
  2187. }
  2188. zSig |= ( ( rem0 | rem1 ) != 0 );
  2189. }
  2190. }
  2191. shift64RightJamming( zSig, 1, &zSig );
  2192. return roundAndPackFloat64( roundData, 0, zExp, zSig );
  2193. }
  2194. /*
  2195. -------------------------------------------------------------------------------
  2196. Returns 1 if the double-precision floating-point value `a' is equal to the
  2197. corresponding value `b', and 0 otherwise. The comparison is performed
  2198. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  2199. -------------------------------------------------------------------------------
  2200. */
  2201. flag float64_eq( float64 a, float64 b )
  2202. {
  2203. if ( ( ( extractFloat64Exp( a ) == 0x7FF ) && extractFloat64Frac( a ) )
  2204. || ( ( extractFloat64Exp( b ) == 0x7FF ) && extractFloat64Frac( b ) )
  2205. ) {
  2206. if ( float64_is_signaling_nan( a ) || float64_is_signaling_nan( b ) ) {
  2207. float_raise( float_flag_invalid );
  2208. }
  2209. return 0;
  2210. }
  2211. return ( a == b ) || ( (bits64) ( ( a | b )<<1 ) == 0 );
  2212. }
  2213. /*
  2214. -------------------------------------------------------------------------------
  2215. Returns 1 if the double-precision floating-point value `a' is less than or
  2216. equal to the corresponding value `b', and 0 otherwise. The comparison is
  2217. performed according to the IEC/IEEE Standard for Binary Floating-point
  2218. Arithmetic.
  2219. -------------------------------------------------------------------------------
  2220. */
  2221. flag float64_le( float64 a, float64 b )
  2222. {
  2223. flag aSign, bSign;
  2224. if ( ( ( extractFloat64Exp( a ) == 0x7FF ) && extractFloat64Frac( a ) )
  2225. || ( ( extractFloat64Exp( b ) == 0x7FF ) && extractFloat64Frac( b ) )
  2226. ) {
  2227. float_raise( float_flag_invalid );
  2228. return 0;
  2229. }
  2230. aSign = extractFloat64Sign( a );
  2231. bSign = extractFloat64Sign( b );
  2232. if ( aSign != bSign ) return aSign || ( (bits64) ( ( a | b )<<1 ) == 0 );
  2233. return ( a == b ) || ( aSign ^ ( a < b ) );
  2234. }
  2235. /*
  2236. -------------------------------------------------------------------------------
  2237. Returns 1 if the double-precision floating-point value `a' is less than
  2238. the corresponding value `b', and 0 otherwise. The comparison is performed
  2239. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  2240. -------------------------------------------------------------------------------
  2241. */
  2242. flag float64_lt( float64 a, float64 b )
  2243. {
  2244. flag aSign, bSign;
  2245. if ( ( ( extractFloat64Exp( a ) == 0x7FF ) && extractFloat64Frac( a ) )
  2246. || ( ( extractFloat64Exp( b ) == 0x7FF ) && extractFloat64Frac( b ) )
  2247. ) {
  2248. float_raise( float_flag_invalid );
  2249. return 0;
  2250. }
  2251. aSign = extractFloat64Sign( a );
  2252. bSign = extractFloat64Sign( b );
  2253. if ( aSign != bSign ) return aSign && ( (bits64) ( ( a | b )<<1 ) != 0 );
  2254. return ( a != b ) && ( aSign ^ ( a < b ) );
  2255. }
  2256. /*
  2257. -------------------------------------------------------------------------------
  2258. Returns 1 if the double-precision floating-point value `a' is equal to the
  2259. corresponding value `b', and 0 otherwise. The invalid exception is raised
  2260. if either operand is a NaN. Otherwise, the comparison is performed
  2261. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  2262. -------------------------------------------------------------------------------
  2263. */
  2264. flag float64_eq_signaling( float64 a, float64 b )
  2265. {
  2266. if ( ( ( extractFloat64Exp( a ) == 0x7FF ) && extractFloat64Frac( a ) )
  2267. || ( ( extractFloat64Exp( b ) == 0x7FF ) && extractFloat64Frac( b ) )
  2268. ) {
  2269. float_raise( float_flag_invalid );
  2270. return 0;
  2271. }
  2272. return ( a == b ) || ( (bits64) ( ( a | b )<<1 ) == 0 );
  2273. }
  2274. /*
  2275. -------------------------------------------------------------------------------
  2276. Returns 1 if the double-precision floating-point value `a' is less than or
  2277. equal to the corresponding value `b', and 0 otherwise. Quiet NaNs do not
  2278. cause an exception. Otherwise, the comparison is performed according to the
  2279. IEC/IEEE Standard for Binary Floating-point Arithmetic.
  2280. -------------------------------------------------------------------------------
  2281. */
  2282. flag float64_le_quiet( float64 a, float64 b )
  2283. {
  2284. flag aSign, bSign;
  2285. //int16 aExp, bExp;
  2286. if ( ( ( extractFloat64Exp( a ) == 0x7FF ) && extractFloat64Frac( a ) )
  2287. || ( ( extractFloat64Exp( b ) == 0x7FF ) && extractFloat64Frac( b ) )
  2288. ) {
  2289. /* Do nothing, even if NaN as we're quiet */
  2290. return 0;
  2291. }
  2292. aSign = extractFloat64Sign( a );
  2293. bSign = extractFloat64Sign( b );
  2294. if ( aSign != bSign ) return aSign || ( (bits64) ( ( a | b )<<1 ) == 0 );
  2295. return ( a == b ) || ( aSign ^ ( a < b ) );
  2296. }
  2297. /*
  2298. -------------------------------------------------------------------------------
  2299. Returns 1 if the double-precision floating-point value `a' is less than
  2300. the corresponding value `b', and 0 otherwise. Quiet NaNs do not cause an
  2301. exception. Otherwise, the comparison is performed according to the IEC/IEEE
  2302. Standard for Binary Floating-point Arithmetic.
  2303. -------------------------------------------------------------------------------
  2304. */
  2305. flag float64_lt_quiet( float64 a, float64 b )
  2306. {
  2307. flag aSign, bSign;
  2308. if ( ( ( extractFloat64Exp( a ) == 0x7FF ) && extractFloat64Frac( a ) )
  2309. || ( ( extractFloat64Exp( b ) == 0x7FF ) && extractFloat64Frac( b ) )
  2310. ) {
  2311. /* Do nothing, even if NaN as we're quiet */
  2312. return 0;
  2313. }
  2314. aSign = extractFloat64Sign( a );
  2315. bSign = extractFloat64Sign( b );
  2316. if ( aSign != bSign ) return aSign && ( (bits64) ( ( a | b )<<1 ) != 0 );
  2317. return ( a != b ) && ( aSign ^ ( a < b ) );
  2318. }
  2319. #ifdef FLOATX80
  2320. /*
  2321. -------------------------------------------------------------------------------
  2322. Returns the result of converting the extended double-precision floating-
  2323. point value `a' to the 32-bit two's complement integer format. The
  2324. conversion is performed according to the IEC/IEEE Standard for Binary
  2325. Floating-point Arithmetic---which means in particular that the conversion
  2326. is rounded according to the current rounding mode. If `a' is a NaN, the
  2327. largest positive integer is returned. Otherwise, if the conversion
  2328. overflows, the largest integer with the same sign as `a' is returned.
  2329. -------------------------------------------------------------------------------
  2330. */
  2331. int32 floatx80_to_int32( struct roundingData *roundData, floatx80 a )
  2332. {
  2333. flag aSign;
  2334. int32 aExp, shiftCount;
  2335. bits64 aSig;
  2336. aSig = extractFloatx80Frac( a );
  2337. aExp = extractFloatx80Exp( a );
  2338. aSign = extractFloatx80Sign( a );
  2339. if ( ( aExp == 0x7FFF ) && (bits64) ( aSig<<1 ) ) aSign = 0;
  2340. shiftCount = 0x4037 - aExp;
  2341. if ( shiftCount <= 0 ) shiftCount = 1;
  2342. shift64RightJamming( aSig, shiftCount, &aSig );
  2343. return roundAndPackInt32( roundData, aSign, aSig );
  2344. }
  2345. /*
  2346. -------------------------------------------------------------------------------
  2347. Returns the result of converting the extended double-precision floating-
  2348. point value `a' to the 32-bit two's complement integer format. The
  2349. conversion is performed according to the IEC/IEEE Standard for Binary
  2350. Floating-point Arithmetic, except that the conversion is always rounded
  2351. toward zero. If `a' is a NaN, the largest positive integer is returned.
  2352. Otherwise, if the conversion overflows, the largest integer with the same
  2353. sign as `a' is returned.
  2354. -------------------------------------------------------------------------------
  2355. */
  2356. int32 floatx80_to_int32_round_to_zero( floatx80 a )
  2357. {
  2358. flag aSign;
  2359. int32 aExp, shiftCount;
  2360. bits64 aSig, savedASig;
  2361. int32 z;
  2362. aSig = extractFloatx80Frac( a );
  2363. aExp = extractFloatx80Exp( a );
  2364. aSign = extractFloatx80Sign( a );
  2365. shiftCount = 0x403E - aExp;
  2366. if ( shiftCount < 32 ) {
  2367. if ( ( aExp == 0x7FFF ) && (bits64) ( aSig<<1 ) ) aSign = 0;
  2368. goto invalid;
  2369. }
  2370. else if ( 63 < shiftCount ) {
  2371. if ( aExp || aSig ) float_raise( float_flag_inexact );
  2372. return 0;
  2373. }
  2374. savedASig = aSig;
  2375. aSig >>= shiftCount;
  2376. z = aSig;
  2377. if ( aSign ) z = - z;
  2378. if ( ( z < 0 ) ^ aSign ) {
  2379. invalid:
  2380. float_raise( float_flag_invalid );
  2381. return aSign ? 0x80000000 : 0x7FFFFFFF;
  2382. }
  2383. if ( ( aSig<<shiftCount ) != savedASig ) {
  2384. float_raise( float_flag_inexact );
  2385. }
  2386. return z;
  2387. }
  2388. /*
  2389. -------------------------------------------------------------------------------
  2390. Returns the result of converting the extended double-precision floating-
  2391. point value `a' to the single-precision floating-point format. The
  2392. conversion is performed according to the IEC/IEEE Standard for Binary
  2393. Floating-point Arithmetic.
  2394. -------------------------------------------------------------------------------
  2395. */
  2396. float32 floatx80_to_float32( struct roundingData *roundData, floatx80 a )
  2397. {
  2398. flag aSign;
  2399. int32 aExp;
  2400. bits64 aSig;
  2401. aSig = extractFloatx80Frac( a );
  2402. aExp = extractFloatx80Exp( a );
  2403. aSign = extractFloatx80Sign( a );
  2404. if ( aExp == 0x7FFF ) {
  2405. if ( (bits64) ( aSig<<1 ) ) {
  2406. return commonNaNToFloat32( floatx80ToCommonNaN( a ) );
  2407. }
  2408. return packFloat32( aSign, 0xFF, 0 );
  2409. }
  2410. shift64RightJamming( aSig, 33, &aSig );
  2411. if ( aExp || aSig ) aExp -= 0x3F81;
  2412. return roundAndPackFloat32( roundData, aSign, aExp, aSig );
  2413. }
  2414. /*
  2415. -------------------------------------------------------------------------------
  2416. Returns the result of converting the extended double-precision floating-
  2417. point value `a' to the double-precision floating-point format. The
  2418. conversion is performed according to the IEC/IEEE Standard for Binary
  2419. Floating-point Arithmetic.
  2420. -------------------------------------------------------------------------------
  2421. */
  2422. float64 floatx80_to_float64( struct roundingData *roundData, floatx80 a )
  2423. {
  2424. flag aSign;
  2425. int32 aExp;
  2426. bits64 aSig, zSig;
  2427. aSig = extractFloatx80Frac( a );
  2428. aExp = extractFloatx80Exp( a );
  2429. aSign = extractFloatx80Sign( a );
  2430. if ( aExp == 0x7FFF ) {
  2431. if ( (bits64) ( aSig<<1 ) ) {
  2432. return commonNaNToFloat64( floatx80ToCommonNaN( a ) );
  2433. }
  2434. return packFloat64( aSign, 0x7FF, 0 );
  2435. }
  2436. shift64RightJamming( aSig, 1, &zSig );
  2437. if ( aExp || aSig ) aExp -= 0x3C01;
  2438. return roundAndPackFloat64( roundData, aSign, aExp, zSig );
  2439. }
  2440. /*
  2441. -------------------------------------------------------------------------------
  2442. Rounds the extended double-precision floating-point value `a' to an integer,
  2443. and returns the result as an extended quadruple-precision floating-point
  2444. value. The operation is performed according to the IEC/IEEE Standard for
  2445. Binary Floating-point Arithmetic.
  2446. -------------------------------------------------------------------------------
  2447. */
  2448. floatx80 floatx80_round_to_int( struct roundingData *roundData, floatx80 a )
  2449. {
  2450. flag aSign;
  2451. int32 aExp;
  2452. bits64 lastBitMask, roundBitsMask;
  2453. int8 roundingMode;
  2454. floatx80 z;
  2455. aExp = extractFloatx80Exp( a );
  2456. if ( 0x403E <= aExp ) {
  2457. if ( ( aExp == 0x7FFF ) && (bits64) ( extractFloatx80Frac( a )<<1 ) ) {
  2458. return propagateFloatx80NaN( a, a );
  2459. }
  2460. return a;
  2461. }
  2462. if ( aExp <= 0x3FFE ) {
  2463. if ( ( aExp == 0 )
  2464. && ( (bits64) ( extractFloatx80Frac( a )<<1 ) == 0 ) ) {
  2465. return a;
  2466. }
  2467. roundData->exception |= float_flag_inexact;
  2468. aSign = extractFloatx80Sign( a );
  2469. switch ( roundData->mode ) {
  2470. case float_round_nearest_even:
  2471. if ( ( aExp == 0x3FFE ) && (bits64) ( extractFloatx80Frac( a )<<1 )
  2472. ) {
  2473. return
  2474. packFloatx80( aSign, 0x3FFF, LIT64( 0x8000000000000000 ) );
  2475. }
  2476. break;
  2477. case float_round_down:
  2478. return
  2479. aSign ?
  2480. packFloatx80( 1, 0x3FFF, LIT64( 0x8000000000000000 ) )
  2481. : packFloatx80( 0, 0, 0 );
  2482. case float_round_up:
  2483. return
  2484. aSign ? packFloatx80( 1, 0, 0 )
  2485. : packFloatx80( 0, 0x3FFF, LIT64( 0x8000000000000000 ) );
  2486. }
  2487. return packFloatx80( aSign, 0, 0 );
  2488. }
  2489. lastBitMask = 1;
  2490. lastBitMask <<= 0x403E - aExp;
  2491. roundBitsMask = lastBitMask - 1;
  2492. z = a;
  2493. roundingMode = roundData->mode;
  2494. if ( roundingMode == float_round_nearest_even ) {
  2495. z.low += lastBitMask>>1;
  2496. if ( ( z.low & roundBitsMask ) == 0 ) z.low &= ~ lastBitMask;
  2497. }
  2498. else if ( roundingMode != float_round_to_zero ) {
  2499. if ( extractFloatx80Sign( z ) ^ ( roundingMode == float_round_up ) ) {
  2500. z.low += roundBitsMask;
  2501. }
  2502. }
  2503. z.low &= ~ roundBitsMask;
  2504. if ( z.low == 0 ) {
  2505. ++z.high;
  2506. z.low = LIT64( 0x8000000000000000 );
  2507. }
  2508. if ( z.low != a.low ) roundData->exception |= float_flag_inexact;
  2509. return z;
  2510. }
  2511. /*
  2512. -------------------------------------------------------------------------------
  2513. Returns the result of adding the absolute values of the extended double-
  2514. precision floating-point values `a' and `b'. If `zSign' is true, the sum is
  2515. negated before being returned. `zSign' is ignored if the result is a NaN.
  2516. The addition is performed according to the IEC/IEEE Standard for Binary
  2517. Floating-point Arithmetic.
  2518. -------------------------------------------------------------------------------
  2519. */
  2520. static floatx80 addFloatx80Sigs( struct roundingData *roundData, floatx80 a, floatx80 b, flag zSign )
  2521. {
  2522. int32 aExp, bExp, zExp;
  2523. bits64 aSig, bSig, zSig0, zSig1;
  2524. int32 expDiff;
  2525. aSig = extractFloatx80Frac( a );
  2526. aExp = extractFloatx80Exp( a );
  2527. bSig = extractFloatx80Frac( b );
  2528. bExp = extractFloatx80Exp( b );
  2529. expDiff = aExp - bExp;
  2530. if ( 0 < expDiff ) {
  2531. if ( aExp == 0x7FFF ) {
  2532. if ( (bits64) ( aSig<<1 ) ) return propagateFloatx80NaN( a, b );
  2533. return a;
  2534. }
  2535. if ( bExp == 0 ) --expDiff;
  2536. shift64ExtraRightJamming( bSig, 0, expDiff, &bSig, &zSig1 );
  2537. zExp = aExp;
  2538. }
  2539. else if ( expDiff < 0 ) {
  2540. if ( bExp == 0x7FFF ) {
  2541. if ( (bits64) ( bSig<<1 ) ) return propagateFloatx80NaN( a, b );
  2542. return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
  2543. }
  2544. if ( aExp == 0 ) ++expDiff;
  2545. shift64ExtraRightJamming( aSig, 0, - expDiff, &aSig, &zSig1 );
  2546. zExp = bExp;
  2547. }
  2548. else {
  2549. if ( aExp == 0x7FFF ) {
  2550. if ( (bits64) ( ( aSig | bSig )<<1 ) ) {
  2551. return propagateFloatx80NaN( a, b );
  2552. }
  2553. return a;
  2554. }
  2555. zSig1 = 0;
  2556. zSig0 = aSig + bSig;
  2557. if ( aExp == 0 ) {
  2558. normalizeFloatx80Subnormal( zSig0, &zExp, &zSig0 );
  2559. goto roundAndPack;
  2560. }
  2561. zExp = aExp;
  2562. goto shiftRight1;
  2563. }
  2564. zSig0 = aSig + bSig;
  2565. if ( (sbits64) zSig0 < 0 ) goto roundAndPack;
  2566. shiftRight1:
  2567. shift64ExtraRightJamming( zSig0, zSig1, 1, &zSig0, &zSig1 );
  2568. zSig0 |= LIT64( 0x8000000000000000 );
  2569. ++zExp;
  2570. roundAndPack:
  2571. return
  2572. roundAndPackFloatx80(
  2573. roundData, zSign, zExp, zSig0, zSig1 );
  2574. }
  2575. /*
  2576. -------------------------------------------------------------------------------
  2577. Returns the result of subtracting the absolute values of the extended
  2578. double-precision floating-point values `a' and `b'. If `zSign' is true,
  2579. the difference is negated before being returned. `zSign' is ignored if the
  2580. result is a NaN. The subtraction is performed according to the IEC/IEEE
  2581. Standard for Binary Floating-point Arithmetic.
  2582. -------------------------------------------------------------------------------
  2583. */
  2584. static floatx80 subFloatx80Sigs( struct roundingData *roundData, floatx80 a, floatx80 b, flag zSign )
  2585. {
  2586. int32 aExp, bExp, zExp;
  2587. bits64 aSig, bSig, zSig0, zSig1;
  2588. int32 expDiff;
  2589. floatx80 z;
  2590. aSig = extractFloatx80Frac( a );
  2591. aExp = extractFloatx80Exp( a );
  2592. bSig = extractFloatx80Frac( b );
  2593. bExp = extractFloatx80Exp( b );
  2594. expDiff = aExp - bExp;
  2595. if ( 0 < expDiff ) goto aExpBigger;
  2596. if ( expDiff < 0 ) goto bExpBigger;
  2597. if ( aExp == 0x7FFF ) {
  2598. if ( (bits64) ( ( aSig | bSig )<<1 ) ) {
  2599. return propagateFloatx80NaN( a, b );
  2600. }
  2601. roundData->exception |= float_flag_invalid;
  2602. z.low = floatx80_default_nan_low;
  2603. z.high = floatx80_default_nan_high;
  2604. z.__padding = 0;
  2605. return z;
  2606. }
  2607. if ( aExp == 0 ) {
  2608. aExp = 1;
  2609. bExp = 1;
  2610. }
  2611. zSig1 = 0;
  2612. if ( bSig < aSig ) goto aBigger;
  2613. if ( aSig < bSig ) goto bBigger;
  2614. return packFloatx80( roundData->mode == float_round_down, 0, 0 );
  2615. bExpBigger:
  2616. if ( bExp == 0x7FFF ) {
  2617. if ( (bits64) ( bSig<<1 ) ) return propagateFloatx80NaN( a, b );
  2618. return packFloatx80( zSign ^ 1, 0x7FFF, LIT64( 0x8000000000000000 ) );
  2619. }
  2620. if ( aExp == 0 ) ++expDiff;
  2621. shift128RightJamming( aSig, 0, - expDiff, &aSig, &zSig1 );
  2622. bBigger:
  2623. sub128( bSig, 0, aSig, zSig1, &zSig0, &zSig1 );
  2624. zExp = bExp;
  2625. zSign ^= 1;
  2626. goto normalizeRoundAndPack;
  2627. aExpBigger:
  2628. if ( aExp == 0x7FFF ) {
  2629. if ( (bits64) ( aSig<<1 ) ) return propagateFloatx80NaN( a, b );
  2630. return a;
  2631. }
  2632. if ( bExp == 0 ) --expDiff;
  2633. shift128RightJamming( bSig, 0, expDiff, &bSig, &zSig1 );
  2634. aBigger:
  2635. sub128( aSig, 0, bSig, zSig1, &zSig0, &zSig1 );
  2636. zExp = aExp;
  2637. normalizeRoundAndPack:
  2638. return
  2639. normalizeRoundAndPackFloatx80(
  2640. roundData, zSign, zExp, zSig0, zSig1 );
  2641. }
  2642. /*
  2643. -------------------------------------------------------------------------------
  2644. Returns the result of adding the extended double-precision floating-point
  2645. values `a' and `b'. The operation is performed according to the IEC/IEEE
  2646. Standard for Binary Floating-point Arithmetic.
  2647. -------------------------------------------------------------------------------
  2648. */
  2649. floatx80 floatx80_add( struct roundingData *roundData, floatx80 a, floatx80 b )
  2650. {
  2651. flag aSign, bSign;
  2652. aSign = extractFloatx80Sign( a );
  2653. bSign = extractFloatx80Sign( b );
  2654. if ( aSign == bSign ) {
  2655. return addFloatx80Sigs( roundData, a, b, aSign );
  2656. }
  2657. else {
  2658. return subFloatx80Sigs( roundData, a, b, aSign );
  2659. }
  2660. }
  2661. /*
  2662. -------------------------------------------------------------------------------
  2663. Returns the result of subtracting the extended double-precision floating-
  2664. point values `a' and `b'. The operation is performed according to the
  2665. IEC/IEEE Standard for Binary Floating-point Arithmetic.
  2666. -------------------------------------------------------------------------------
  2667. */
  2668. floatx80 floatx80_sub( struct roundingData *roundData, floatx80 a, floatx80 b )
  2669. {
  2670. flag aSign, bSign;
  2671. aSign = extractFloatx80Sign( a );
  2672. bSign = extractFloatx80Sign( b );
  2673. if ( aSign == bSign ) {
  2674. return subFloatx80Sigs( roundData, a, b, aSign );
  2675. }
  2676. else {
  2677. return addFloatx80Sigs( roundData, a, b, aSign );
  2678. }
  2679. }
  2680. /*
  2681. -------------------------------------------------------------------------------
  2682. Returns the result of multiplying the extended double-precision floating-
  2683. point values `a' and `b'. The operation is performed according to the
  2684. IEC/IEEE Standard for Binary Floating-point Arithmetic.
  2685. -------------------------------------------------------------------------------
  2686. */
  2687. floatx80 floatx80_mul( struct roundingData *roundData, floatx80 a, floatx80 b )
  2688. {
  2689. flag aSign, bSign, zSign;
  2690. int32 aExp, bExp, zExp;
  2691. bits64 aSig, bSig, zSig0, zSig1;
  2692. floatx80 z;
  2693. aSig = extractFloatx80Frac( a );
  2694. aExp = extractFloatx80Exp( a );
  2695. aSign = extractFloatx80Sign( a );
  2696. bSig = extractFloatx80Frac( b );
  2697. bExp = extractFloatx80Exp( b );
  2698. bSign = extractFloatx80Sign( b );
  2699. zSign = aSign ^ bSign;
  2700. if ( aExp == 0x7FFF ) {
  2701. if ( (bits64) ( aSig<<1 )
  2702. || ( ( bExp == 0x7FFF ) && (bits64) ( bSig<<1 ) ) ) {
  2703. return propagateFloatx80NaN( a, b );
  2704. }
  2705. if ( ( bExp | bSig ) == 0 ) goto invalid;
  2706. return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
  2707. }
  2708. if ( bExp == 0x7FFF ) {
  2709. if ( (bits64) ( bSig<<1 ) ) return propagateFloatx80NaN( a, b );
  2710. if ( ( aExp | aSig ) == 0 ) {
  2711. invalid:
  2712. roundData->exception |= float_flag_invalid;
  2713. z.low = floatx80_default_nan_low;
  2714. z.high = floatx80_default_nan_high;
  2715. z.__padding = 0;
  2716. return z;
  2717. }
  2718. return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
  2719. }
  2720. if ( aExp == 0 ) {
  2721. if ( aSig == 0 ) return packFloatx80( zSign, 0, 0 );
  2722. normalizeFloatx80Subnormal( aSig, &aExp, &aSig );
  2723. }
  2724. if ( bExp == 0 ) {
  2725. if ( bSig == 0 ) return packFloatx80( zSign, 0, 0 );
  2726. normalizeFloatx80Subnormal( bSig, &bExp, &bSig );
  2727. }
  2728. zExp = aExp + bExp - 0x3FFE;
  2729. mul64To128( aSig, bSig, &zSig0, &zSig1 );
  2730. if ( 0 < (sbits64) zSig0 ) {
  2731. shortShift128Left( zSig0, zSig1, 1, &zSig0, &zSig1 );
  2732. --zExp;
  2733. }
  2734. return
  2735. roundAndPackFloatx80(
  2736. roundData, zSign, zExp, zSig0, zSig1 );
  2737. }
  2738. /*
  2739. -------------------------------------------------------------------------------
  2740. Returns the result of dividing the extended double-precision floating-point
  2741. value `a' by the corresponding value `b'. The operation is performed
  2742. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  2743. -------------------------------------------------------------------------------
  2744. */
  2745. floatx80 floatx80_div( struct roundingData *roundData, floatx80 a, floatx80 b )
  2746. {
  2747. flag aSign, bSign, zSign;
  2748. int32 aExp, bExp, zExp;
  2749. bits64 aSig, bSig, zSig0, zSig1;
  2750. bits64 rem0, rem1, rem2, term0, term1, term2;
  2751. floatx80 z;
  2752. aSig = extractFloatx80Frac( a );
  2753. aExp = extractFloatx80Exp( a );
  2754. aSign = extractFloatx80Sign( a );
  2755. bSig = extractFloatx80Frac( b );
  2756. bExp = extractFloatx80Exp( b );
  2757. bSign = extractFloatx80Sign( b );
  2758. zSign = aSign ^ bSign;
  2759. if ( aExp == 0x7FFF ) {
  2760. if ( (bits64) ( aSig<<1 ) ) return propagateFloatx80NaN( a, b );
  2761. if ( bExp == 0x7FFF ) {
  2762. if ( (bits64) ( bSig<<1 ) ) return propagateFloatx80NaN( a, b );
  2763. goto invalid;
  2764. }
  2765. return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
  2766. }
  2767. if ( bExp == 0x7FFF ) {
  2768. if ( (bits64) ( bSig<<1 ) ) return propagateFloatx80NaN( a, b );
  2769. return packFloatx80( zSign, 0, 0 );
  2770. }
  2771. if ( bExp == 0 ) {
  2772. if ( bSig == 0 ) {
  2773. if ( ( aExp | aSig ) == 0 ) {
  2774. invalid:
  2775. roundData->exception |= float_flag_invalid;
  2776. z.low = floatx80_default_nan_low;
  2777. z.high = floatx80_default_nan_high;
  2778. z.__padding = 0;
  2779. return z;
  2780. }
  2781. roundData->exception |= float_flag_divbyzero;
  2782. return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
  2783. }
  2784. normalizeFloatx80Subnormal( bSig, &bExp, &bSig );
  2785. }
  2786. if ( aExp == 0 ) {
  2787. if ( aSig == 0 ) return packFloatx80( zSign, 0, 0 );
  2788. normalizeFloatx80Subnormal( aSig, &aExp, &aSig );
  2789. }
  2790. zExp = aExp - bExp + 0x3FFE;
  2791. rem1 = 0;
  2792. if ( bSig <= aSig ) {
  2793. shift128Right( aSig, 0, 1, &aSig, &rem1 );
  2794. ++zExp;
  2795. }
  2796. zSig0 = estimateDiv128To64( aSig, rem1, bSig );
  2797. mul64To128( bSig, zSig0, &term0, &term1 );
  2798. sub128( aSig, rem1, term0, term1, &rem0, &rem1 );
  2799. while ( (sbits64) rem0 < 0 ) {
  2800. --zSig0;
  2801. add128( rem0, rem1, 0, bSig, &rem0, &rem1 );
  2802. }
  2803. zSig1 = estimateDiv128To64( rem1, 0, bSig );
  2804. if ( (bits64) ( zSig1<<1 ) <= 8 ) {
  2805. mul64To128( bSig, zSig1, &term1, &term2 );
  2806. sub128( rem1, 0, term1, term2, &rem1, &rem2 );
  2807. while ( (sbits64) rem1 < 0 ) {
  2808. --zSig1;
  2809. add128( rem1, rem2, 0, bSig, &rem1, &rem2 );
  2810. }
  2811. zSig1 |= ( ( rem1 | rem2 ) != 0 );
  2812. }
  2813. return
  2814. roundAndPackFloatx80(
  2815. roundData, zSign, zExp, zSig0, zSig1 );
  2816. }
  2817. /*
  2818. -------------------------------------------------------------------------------
  2819. Returns the remainder of the extended double-precision floating-point value
  2820. `a' with respect to the corresponding value `b'. The operation is performed
  2821. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  2822. -------------------------------------------------------------------------------
  2823. */
  2824. floatx80 floatx80_rem( struct roundingData *roundData, floatx80 a, floatx80 b )
  2825. {
  2826. flag aSign, bSign, zSign;
  2827. int32 aExp, bExp, expDiff;
  2828. bits64 aSig0, aSig1, bSig;
  2829. bits64 q, term0, term1, alternateASig0, alternateASig1;
  2830. floatx80 z;
  2831. aSig0 = extractFloatx80Frac( a );
  2832. aExp = extractFloatx80Exp( a );
  2833. aSign = extractFloatx80Sign( a );
  2834. bSig = extractFloatx80Frac( b );
  2835. bExp = extractFloatx80Exp( b );
  2836. bSign = extractFloatx80Sign( b );
  2837. if ( aExp == 0x7FFF ) {
  2838. if ( (bits64) ( aSig0<<1 )
  2839. || ( ( bExp == 0x7FFF ) && (bits64) ( bSig<<1 ) ) ) {
  2840. return propagateFloatx80NaN( a, b );
  2841. }
  2842. goto invalid;
  2843. }
  2844. if ( bExp == 0x7FFF ) {
  2845. if ( (bits64) ( bSig<<1 ) ) return propagateFloatx80NaN( a, b );
  2846. return a;
  2847. }
  2848. if ( bExp == 0 ) {
  2849. if ( bSig == 0 ) {
  2850. invalid:
  2851. roundData->exception |= float_flag_invalid;
  2852. z.low = floatx80_default_nan_low;
  2853. z.high = floatx80_default_nan_high;
  2854. z.__padding = 0;
  2855. return z;
  2856. }
  2857. normalizeFloatx80Subnormal( bSig, &bExp, &bSig );
  2858. }
  2859. if ( aExp == 0 ) {
  2860. if ( (bits64) ( aSig0<<1 ) == 0 ) return a;
  2861. normalizeFloatx80Subnormal( aSig0, &aExp, &aSig0 );
  2862. }
  2863. bSig |= LIT64( 0x8000000000000000 );
  2864. zSign = aSign;
  2865. expDiff = aExp - bExp;
  2866. aSig1 = 0;
  2867. if ( expDiff < 0 ) {
  2868. if ( expDiff < -1 ) return a;
  2869. shift128Right( aSig0, 0, 1, &aSig0, &aSig1 );
  2870. expDiff = 0;
  2871. }
  2872. q = ( bSig <= aSig0 );
  2873. if ( q ) aSig0 -= bSig;
  2874. expDiff -= 64;
  2875. while ( 0 < expDiff ) {
  2876. q = estimateDiv128To64( aSig0, aSig1, bSig );
  2877. q = ( 2 < q ) ? q - 2 : 0;
  2878. mul64To128( bSig, q, &term0, &term1 );
  2879. sub128( aSig0, aSig1, term0, term1, &aSig0, &aSig1 );
  2880. shortShift128Left( aSig0, aSig1, 62, &aSig0, &aSig1 );
  2881. expDiff -= 62;
  2882. }
  2883. expDiff += 64;
  2884. if ( 0 < expDiff ) {
  2885. q = estimateDiv128To64( aSig0, aSig1, bSig );
  2886. q = ( 2 < q ) ? q - 2 : 0;
  2887. q >>= 64 - expDiff;
  2888. mul64To128( bSig, q<<( 64 - expDiff ), &term0, &term1 );
  2889. sub128( aSig0, aSig1, term0, term1, &aSig0, &aSig1 );
  2890. shortShift128Left( 0, bSig, 64 - expDiff, &term0, &term1 );
  2891. while ( le128( term0, term1, aSig0, aSig1 ) ) {
  2892. ++q;
  2893. sub128( aSig0, aSig1, term0, term1, &aSig0, &aSig1 );
  2894. }
  2895. }
  2896. else {
  2897. term1 = 0;
  2898. term0 = bSig;
  2899. }
  2900. sub128( term0, term1, aSig0, aSig1, &alternateASig0, &alternateASig1 );
  2901. if ( lt128( alternateASig0, alternateASig1, aSig0, aSig1 )
  2902. || ( eq128( alternateASig0, alternateASig1, aSig0, aSig1 )
  2903. && ( q & 1 ) )
  2904. ) {
  2905. aSig0 = alternateASig0;
  2906. aSig1 = alternateASig1;
  2907. zSign = ! zSign;
  2908. }
  2909. return
  2910. normalizeRoundAndPackFloatx80(
  2911. roundData, zSign, bExp + expDiff, aSig0, aSig1 );
  2912. }
  2913. /*
  2914. -------------------------------------------------------------------------------
  2915. Returns the square root of the extended double-precision floating-point
  2916. value `a'. The operation is performed according to the IEC/IEEE Standard
  2917. for Binary Floating-point Arithmetic.
  2918. -------------------------------------------------------------------------------
  2919. */
  2920. floatx80 floatx80_sqrt( struct roundingData *roundData, floatx80 a )
  2921. {
  2922. flag aSign;
  2923. int32 aExp, zExp;
  2924. bits64 aSig0, aSig1, zSig0, zSig1;
  2925. bits64 rem0, rem1, rem2, rem3, term0, term1, term2, term3;
  2926. bits64 shiftedRem0, shiftedRem1;
  2927. floatx80 z;
  2928. aSig0 = extractFloatx80Frac( a );
  2929. aExp = extractFloatx80Exp( a );
  2930. aSign = extractFloatx80Sign( a );
  2931. if ( aExp == 0x7FFF ) {
  2932. if ( (bits64) ( aSig0<<1 ) ) return propagateFloatx80NaN( a, a );
  2933. if ( ! aSign ) return a;
  2934. goto invalid;
  2935. }
  2936. if ( aSign ) {
  2937. if ( ( aExp | aSig0 ) == 0 ) return a;
  2938. invalid:
  2939. roundData->exception |= float_flag_invalid;
  2940. z.low = floatx80_default_nan_low;
  2941. z.high = floatx80_default_nan_high;
  2942. z.__padding = 0;
  2943. return z;
  2944. }
  2945. if ( aExp == 0 ) {
  2946. if ( aSig0 == 0 ) return packFloatx80( 0, 0, 0 );
  2947. normalizeFloatx80Subnormal( aSig0, &aExp, &aSig0 );
  2948. }
  2949. zExp = ( ( aExp - 0x3FFF )>>1 ) + 0x3FFF;
  2950. zSig0 = estimateSqrt32( aExp, aSig0>>32 );
  2951. zSig0 <<= 31;
  2952. aSig1 = 0;
  2953. shift128Right( aSig0, 0, ( aExp & 1 ) + 2, &aSig0, &aSig1 );
  2954. zSig0 = estimateDiv128To64( aSig0, aSig1, zSig0 ) + zSig0 + 4;
  2955. if ( 0 <= (sbits64) zSig0 ) zSig0 = LIT64( 0xFFFFFFFFFFFFFFFF );
  2956. shortShift128Left( aSig0, aSig1, 2, &aSig0, &aSig1 );
  2957. mul64To128( zSig0, zSig0, &term0, &term1 );
  2958. sub128( aSig0, aSig1, term0, term1, &rem0, &rem1 );
  2959. while ( (sbits64) rem0 < 0 ) {
  2960. --zSig0;
  2961. shortShift128Left( 0, zSig0, 1, &term0, &term1 );
  2962. term1 |= 1;
  2963. add128( rem0, rem1, term0, term1, &rem0, &rem1 );
  2964. }
  2965. shortShift128Left( rem0, rem1, 63, &shiftedRem0, &shiftedRem1 );
  2966. zSig1 = estimateDiv128To64( shiftedRem0, shiftedRem1, zSig0 );
  2967. if ( (bits64) ( zSig1<<1 ) <= 10 ) {
  2968. if ( zSig1 == 0 ) zSig1 = 1;
  2969. mul64To128( zSig0, zSig1, &term1, &term2 );
  2970. shortShift128Left( term1, term2, 1, &term1, &term2 );
  2971. sub128( rem1, 0, term1, term2, &rem1, &rem2 );
  2972. mul64To128( zSig1, zSig1, &term2, &term3 );
  2973. sub192( rem1, rem2, 0, 0, term2, term3, &rem1, &rem2, &rem3 );
  2974. while ( (sbits64) rem1 < 0 ) {
  2975. --zSig1;
  2976. shortShift192Left( 0, zSig0, zSig1, 1, &term1, &term2, &term3 );
  2977. term3 |= 1;
  2978. add192(
  2979. rem1, rem2, rem3, term1, term2, term3, &rem1, &rem2, &rem3 );
  2980. }
  2981. zSig1 |= ( ( rem1 | rem2 | rem3 ) != 0 );
  2982. }
  2983. return
  2984. roundAndPackFloatx80(
  2985. roundData, 0, zExp, zSig0, zSig1 );
  2986. }
  2987. /*
  2988. -------------------------------------------------------------------------------
  2989. Returns 1 if the extended double-precision floating-point value `a' is
  2990. equal to the corresponding value `b', and 0 otherwise. The comparison is
  2991. performed according to the IEC/IEEE Standard for Binary Floating-point
  2992. Arithmetic.
  2993. -------------------------------------------------------------------------------
  2994. */
  2995. flag floatx80_eq( floatx80 a, floatx80 b )
  2996. {
  2997. if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
  2998. && (bits64) ( extractFloatx80Frac( a )<<1 ) )
  2999. || ( ( extractFloatx80Exp( b ) == 0x7FFF )
  3000. && (bits64) ( extractFloatx80Frac( b )<<1 ) )
  3001. ) {
  3002. if ( floatx80_is_signaling_nan( a )
  3003. || floatx80_is_signaling_nan( b ) ) {
  3004. float_raise( float_flag_invalid );
  3005. }
  3006. return 0;
  3007. }
  3008. return
  3009. ( a.low == b.low )
  3010. && ( ( a.high == b.high )
  3011. || ( ( a.low == 0 )
  3012. && ( (bits16) ( ( a.high | b.high )<<1 ) == 0 ) )
  3013. );
  3014. }
  3015. /*
  3016. -------------------------------------------------------------------------------
  3017. Returns 1 if the extended double-precision floating-point value `a' is
  3018. less than or equal to the corresponding value `b', and 0 otherwise. The
  3019. comparison is performed according to the IEC/IEEE Standard for Binary
  3020. Floating-point Arithmetic.
  3021. -------------------------------------------------------------------------------
  3022. */
  3023. flag floatx80_le( floatx80 a, floatx80 b )
  3024. {
  3025. flag aSign, bSign;
  3026. if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
  3027. && (bits64) ( extractFloatx80Frac( a )<<1 ) )
  3028. || ( ( extractFloatx80Exp( b ) == 0x7FFF )
  3029. && (bits64) ( extractFloatx80Frac( b )<<1 ) )
  3030. ) {
  3031. float_raise( float_flag_invalid );
  3032. return 0;
  3033. }
  3034. aSign = extractFloatx80Sign( a );
  3035. bSign = extractFloatx80Sign( b );
  3036. if ( aSign != bSign ) {
  3037. return
  3038. aSign
  3039. || ( ( ( (bits16) ( ( a.high | b.high )<<1 ) ) | a.low | b.low )
  3040. == 0 );
  3041. }
  3042. return
  3043. aSign ? le128( b.high, b.low, a.high, a.low )
  3044. : le128( a.high, a.low, b.high, b.low );
  3045. }
  3046. /*
  3047. -------------------------------------------------------------------------------
  3048. Returns 1 if the extended double-precision floating-point value `a' is
  3049. less than the corresponding value `b', and 0 otherwise. The comparison
  3050. is performed according to the IEC/IEEE Standard for Binary Floating-point
  3051. Arithmetic.
  3052. -------------------------------------------------------------------------------
  3053. */
  3054. flag floatx80_lt( floatx80 a, floatx80 b )
  3055. {
  3056. flag aSign, bSign;
  3057. if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
  3058. && (bits64) ( extractFloatx80Frac( a )<<1 ) )
  3059. || ( ( extractFloatx80Exp( b ) == 0x7FFF )
  3060. && (bits64) ( extractFloatx80Frac( b )<<1 ) )
  3061. ) {
  3062. float_raise( float_flag_invalid );
  3063. return 0;
  3064. }
  3065. aSign = extractFloatx80Sign( a );
  3066. bSign = extractFloatx80Sign( b );
  3067. if ( aSign != bSign ) {
  3068. return
  3069. aSign
  3070. && ( ( ( (bits16) ( ( a.high | b.high )<<1 ) ) | a.low | b.low )
  3071. != 0 );
  3072. }
  3073. return
  3074. aSign ? lt128( b.high, b.low, a.high, a.low )
  3075. : lt128( a.high, a.low, b.high, b.low );
  3076. }
  3077. /*
  3078. -------------------------------------------------------------------------------
  3079. Returns 1 if the extended double-precision floating-point value `a' is equal
  3080. to the corresponding value `b', and 0 otherwise. The invalid exception is
  3081. raised if either operand is a NaN. Otherwise, the comparison is performed
  3082. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  3083. -------------------------------------------------------------------------------
  3084. */
  3085. flag floatx80_eq_signaling( floatx80 a, floatx80 b )
  3086. {
  3087. if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
  3088. && (bits64) ( extractFloatx80Frac( a )<<1 ) )
  3089. || ( ( extractFloatx80Exp( b ) == 0x7FFF )
  3090. && (bits64) ( extractFloatx80Frac( b )<<1 ) )
  3091. ) {
  3092. float_raise( float_flag_invalid );
  3093. return 0;
  3094. }
  3095. return
  3096. ( a.low == b.low )
  3097. && ( ( a.high == b.high )
  3098. || ( ( a.low == 0 )
  3099. && ( (bits16) ( ( a.high | b.high )<<1 ) == 0 ) )
  3100. );
  3101. }
  3102. /*
  3103. -------------------------------------------------------------------------------
  3104. Returns 1 if the extended double-precision floating-point value `a' is less
  3105. than or equal to the corresponding value `b', and 0 otherwise. Quiet NaNs
  3106. do not cause an exception. Otherwise, the comparison is performed according
  3107. to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  3108. -------------------------------------------------------------------------------
  3109. */
  3110. flag floatx80_le_quiet( floatx80 a, floatx80 b )
  3111. {
  3112. flag aSign, bSign;
  3113. if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
  3114. && (bits64) ( extractFloatx80Frac( a )<<1 ) )
  3115. || ( ( extractFloatx80Exp( b ) == 0x7FFF )
  3116. && (bits64) ( extractFloatx80Frac( b )<<1 ) )
  3117. ) {
  3118. /* Do nothing, even if NaN as we're quiet */
  3119. return 0;
  3120. }
  3121. aSign = extractFloatx80Sign( a );
  3122. bSign = extractFloatx80Sign( b );
  3123. if ( aSign != bSign ) {
  3124. return
  3125. aSign
  3126. || ( ( ( (bits16) ( ( a.high | b.high )<<1 ) ) | a.low | b.low )
  3127. == 0 );
  3128. }
  3129. return
  3130. aSign ? le128( b.high, b.low, a.high, a.low )
  3131. : le128( a.high, a.low, b.high, b.low );
  3132. }
  3133. /*
  3134. -------------------------------------------------------------------------------
  3135. Returns 1 if the extended double-precision floating-point value `a' is less
  3136. than the corresponding value `b', and 0 otherwise. Quiet NaNs do not cause
  3137. an exception. Otherwise, the comparison is performed according to the
  3138. IEC/IEEE Standard for Binary Floating-point Arithmetic.
  3139. -------------------------------------------------------------------------------
  3140. */
  3141. flag floatx80_lt_quiet( floatx80 a, floatx80 b )
  3142. {
  3143. flag aSign, bSign;
  3144. if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
  3145. && (bits64) ( extractFloatx80Frac( a )<<1 ) )
  3146. || ( ( extractFloatx80Exp( b ) == 0x7FFF )
  3147. && (bits64) ( extractFloatx80Frac( b )<<1 ) )
  3148. ) {
  3149. /* Do nothing, even if NaN as we're quiet */
  3150. return 0;
  3151. }
  3152. aSign = extractFloatx80Sign( a );
  3153. bSign = extractFloatx80Sign( b );
  3154. if ( aSign != bSign ) {
  3155. return
  3156. aSign
  3157. && ( ( ( (bits16) ( ( a.high | b.high )<<1 ) ) | a.low | b.low )
  3158. != 0 );
  3159. }
  3160. return
  3161. aSign ? lt128( b.high, b.low, a.high, a.low )
  3162. : lt128( a.high, a.low, b.high, b.low );
  3163. }
  3164. #endif