softfloat.c 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439
  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 `http://HTTP.CS.Berkeley.EDU/~jhauser/
  13. arithmetic/softfloat.html'.
  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 "fpa11.h"
  26. #include "milieu.h"
  27. #include "softfloat.h"
  28. /*
  29. -------------------------------------------------------------------------------
  30. Floating-point rounding mode, extended double-precision rounding precision,
  31. and exception flags.
  32. -------------------------------------------------------------------------------
  33. */
  34. int8 float_rounding_mode = float_round_nearest_even;
  35. int8 floatx80_rounding_precision = 80;
  36. int8 float_exception_flags;
  37. /*
  38. -------------------------------------------------------------------------------
  39. Primitive arithmetic functions, including multi-word arithmetic, and
  40. division and square root approximations. (Can be specialized to target if
  41. desired.)
  42. -------------------------------------------------------------------------------
  43. */
  44. #include "softfloat-macros"
  45. /*
  46. -------------------------------------------------------------------------------
  47. Functions and definitions to determine: (1) whether tininess for underflow
  48. is detected before or after rounding by default, (2) what (if anything)
  49. happens when exceptions are raised, (3) how signaling NaNs are distinguished
  50. from quiet NaNs, (4) the default generated quiet NaNs, and (5) how NaNs
  51. are propagated from function inputs to output. These details are target-
  52. specific.
  53. -------------------------------------------------------------------------------
  54. */
  55. #include "softfloat-specialize"
  56. /*
  57. -------------------------------------------------------------------------------
  58. Takes a 64-bit fixed-point value `absZ' with binary point between bits 6
  59. and 7, and returns the properly rounded 32-bit integer corresponding to the
  60. input. If `zSign' is nonzero, the input is negated before being converted
  61. to an integer. Bit 63 of `absZ' must be zero. Ordinarily, the fixed-point
  62. input is simply rounded to an integer, with the inexact exception raised if
  63. the input cannot be represented exactly as an integer. If the fixed-point
  64. input is too large, however, the invalid exception is raised and the largest
  65. positive or negative integer is returned.
  66. -------------------------------------------------------------------------------
  67. */
  68. static int32 roundAndPackInt32( flag zSign, bits64 absZ )
  69. {
  70. int8 roundingMode;
  71. flag roundNearestEven;
  72. int8 roundIncrement, roundBits;
  73. int32 z;
  74. roundingMode = float_rounding_mode;
  75. roundNearestEven = ( roundingMode == float_round_nearest_even );
  76. roundIncrement = 0x40;
  77. if ( ! roundNearestEven ) {
  78. if ( roundingMode == float_round_to_zero ) {
  79. roundIncrement = 0;
  80. }
  81. else {
  82. roundIncrement = 0x7F;
  83. if ( zSign ) {
  84. if ( roundingMode == float_round_up ) roundIncrement = 0;
  85. }
  86. else {
  87. if ( roundingMode == float_round_down ) roundIncrement = 0;
  88. }
  89. }
  90. }
  91. roundBits = absZ & 0x7F;
  92. absZ = ( absZ + roundIncrement )>>7;
  93. absZ &= ~ ( ( ( roundBits ^ 0x40 ) == 0 ) & roundNearestEven );
  94. z = absZ;
  95. if ( zSign ) z = - z;
  96. if ( ( absZ>>32 ) || ( z && ( ( z < 0 ) ^ zSign ) ) ) {
  97. float_exception_flags |= float_flag_invalid;
  98. return zSign ? 0x80000000 : 0x7FFFFFFF;
  99. }
  100. if ( roundBits ) float_exception_flags |= float_flag_inexact;
  101. return z;
  102. }
  103. /*
  104. -------------------------------------------------------------------------------
  105. Returns the fraction bits of the single-precision floating-point value `a'.
  106. -------------------------------------------------------------------------------
  107. */
  108. INLINE bits32 extractFloat32Frac( float32 a )
  109. {
  110. return a & 0x007FFFFF;
  111. }
  112. /*
  113. -------------------------------------------------------------------------------
  114. Returns the exponent bits of the single-precision floating-point value `a'.
  115. -------------------------------------------------------------------------------
  116. */
  117. INLINE int16 extractFloat32Exp( float32 a )
  118. {
  119. return ( a>>23 ) & 0xFF;
  120. }
  121. /*
  122. -------------------------------------------------------------------------------
  123. Returns the sign bit of the single-precision floating-point value `a'.
  124. -------------------------------------------------------------------------------
  125. */
  126. INLINE flag extractFloat32Sign( float32 a )
  127. {
  128. return a>>31;
  129. }
  130. /*
  131. -------------------------------------------------------------------------------
  132. Normalizes the subnormal single-precision floating-point value represented
  133. by the denormalized significand `aSig'. The normalized exponent and
  134. significand are stored at the locations pointed to by `zExpPtr' and
  135. `zSigPtr', respectively.
  136. -------------------------------------------------------------------------------
  137. */
  138. static void
  139. normalizeFloat32Subnormal( bits32 aSig, int16 *zExpPtr, bits32 *zSigPtr )
  140. {
  141. int8 shiftCount;
  142. shiftCount = countLeadingZeros32( aSig ) - 8;
  143. *zSigPtr = aSig<<shiftCount;
  144. *zExpPtr = 1 - shiftCount;
  145. }
  146. /*
  147. -------------------------------------------------------------------------------
  148. Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
  149. single-precision floating-point value, returning the result. After being
  150. shifted into the proper positions, the three fields are simply added
  151. together to form the result. This means that any integer portion of `zSig'
  152. will be added into the exponent. Since a properly normalized significand
  153. will have an integer portion equal to 1, the `zExp' input should be 1 less
  154. than the desired result exponent whenever `zSig' is a complete, normalized
  155. significand.
  156. -------------------------------------------------------------------------------
  157. */
  158. INLINE float32 packFloat32( flag zSign, int16 zExp, bits32 zSig )
  159. {
  160. #if 0
  161. float32 f;
  162. __asm__("@ packFloat32; \n\
  163. mov %0, %1, asl #31; \n\
  164. orr %0, %2, asl #23; \n\
  165. orr %0, %3"
  166. : /* no outputs */
  167. : "g" (f), "g" (zSign), "g" (zExp), "g" (zSig)
  168. : "cc");
  169. return f;
  170. #else
  171. return ( ( (bits32) zSign )<<31 ) + ( ( (bits32) zExp )<<23 ) + zSig;
  172. #endif
  173. }
  174. /*
  175. -------------------------------------------------------------------------------
  176. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  177. and significand `zSig', and returns the proper single-precision floating-
  178. point value corresponding to the abstract input. Ordinarily, the abstract
  179. value is simply rounded and packed into the single-precision format, with
  180. the inexact exception raised if the abstract input cannot be represented
  181. exactly. If the abstract value is too large, however, the overflow and
  182. inexact exceptions are raised and an infinity or maximal finite value is
  183. returned. If the abstract value is too small, the input value is rounded to
  184. a subnormal number, and the underflow and inexact exceptions are raised if
  185. the abstract input cannot be represented exactly as a subnormal single-
  186. precision floating-point number.
  187. The input significand `zSig' has its binary point between bits 30
  188. and 29, which is 7 bits to the left of the usual location. This shifted
  189. significand must be normalized or smaller. If `zSig' is not normalized,
  190. `zExp' must be 0; in that case, the result returned is a subnormal number,
  191. and it must not require rounding. In the usual case that `zSig' is
  192. normalized, `zExp' must be 1 less than the ``true'' floating-point exponent.
  193. The handling of underflow and overflow follows the IEC/IEEE Standard for
  194. Binary Floating-point Arithmetic.
  195. -------------------------------------------------------------------------------
  196. */
  197. static float32 roundAndPackFloat32( flag zSign, int16 zExp, bits32 zSig )
  198. {
  199. int8 roundingMode;
  200. flag roundNearestEven;
  201. int8 roundIncrement, roundBits;
  202. flag isTiny;
  203. roundingMode = float_rounding_mode;
  204. roundNearestEven = ( roundingMode == float_round_nearest_even );
  205. roundIncrement = 0x40;
  206. if ( ! roundNearestEven ) {
  207. if ( roundingMode == float_round_to_zero ) {
  208. roundIncrement = 0;
  209. }
  210. else {
  211. roundIncrement = 0x7F;
  212. if ( zSign ) {
  213. if ( roundingMode == float_round_up ) roundIncrement = 0;
  214. }
  215. else {
  216. if ( roundingMode == float_round_down ) roundIncrement = 0;
  217. }
  218. }
  219. }
  220. roundBits = zSig & 0x7F;
  221. if ( 0xFD <= (bits16) zExp ) {
  222. if ( ( 0xFD < zExp )
  223. || ( ( zExp == 0xFD )
  224. && ( (sbits32) ( zSig + roundIncrement ) < 0 ) )
  225. ) {
  226. float_raise( float_flag_overflow | float_flag_inexact );
  227. return packFloat32( zSign, 0xFF, 0 ) - ( roundIncrement == 0 );
  228. }
  229. if ( zExp < 0 ) {
  230. isTiny =
  231. ( float_detect_tininess == float_tininess_before_rounding )
  232. || ( zExp < -1 )
  233. || ( zSig + roundIncrement < 0x80000000 );
  234. shift32RightJamming( zSig, - zExp, &zSig );
  235. zExp = 0;
  236. roundBits = zSig & 0x7F;
  237. if ( isTiny && roundBits ) float_raise( float_flag_underflow );
  238. }
  239. }
  240. if ( roundBits ) float_exception_flags |= float_flag_inexact;
  241. zSig = ( zSig + roundIncrement )>>7;
  242. zSig &= ~ ( ( ( roundBits ^ 0x40 ) == 0 ) & roundNearestEven );
  243. if ( zSig == 0 ) zExp = 0;
  244. return packFloat32( zSign, zExp, zSig );
  245. }
  246. /*
  247. -------------------------------------------------------------------------------
  248. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  249. and significand `zSig', and returns the proper single-precision floating-
  250. point value corresponding to the abstract input. This routine is just like
  251. `roundAndPackFloat32' except that `zSig' does not have to be normalized in
  252. any way. In all cases, `zExp' must be 1 less than the ``true'' floating-
  253. point exponent.
  254. -------------------------------------------------------------------------------
  255. */
  256. static float32
  257. normalizeRoundAndPackFloat32( flag zSign, int16 zExp, bits32 zSig )
  258. {
  259. int8 shiftCount;
  260. shiftCount = countLeadingZeros32( zSig ) - 1;
  261. return roundAndPackFloat32( zSign, zExp - shiftCount, zSig<<shiftCount );
  262. }
  263. /*
  264. -------------------------------------------------------------------------------
  265. Returns the fraction bits of the double-precision floating-point value `a'.
  266. -------------------------------------------------------------------------------
  267. */
  268. INLINE bits64 extractFloat64Frac( float64 a )
  269. {
  270. return a & LIT64( 0x000FFFFFFFFFFFFF );
  271. }
  272. /*
  273. -------------------------------------------------------------------------------
  274. Returns the exponent bits of the double-precision floating-point value `a'.
  275. -------------------------------------------------------------------------------
  276. */
  277. INLINE int16 extractFloat64Exp( float64 a )
  278. {
  279. return ( a>>52 ) & 0x7FF;
  280. }
  281. /*
  282. -------------------------------------------------------------------------------
  283. Returns the sign bit of the double-precision floating-point value `a'.
  284. -------------------------------------------------------------------------------
  285. */
  286. INLINE flag extractFloat64Sign( float64 a )
  287. {
  288. return a>>63;
  289. }
  290. /*
  291. -------------------------------------------------------------------------------
  292. Normalizes the subnormal double-precision floating-point value represented
  293. by the denormalized significand `aSig'. The normalized exponent and
  294. significand are stored at the locations pointed to by `zExpPtr' and
  295. `zSigPtr', respectively.
  296. -------------------------------------------------------------------------------
  297. */
  298. static void
  299. normalizeFloat64Subnormal( bits64 aSig, int16 *zExpPtr, bits64 *zSigPtr )
  300. {
  301. int8 shiftCount;
  302. shiftCount = countLeadingZeros64( aSig ) - 11;
  303. *zSigPtr = aSig<<shiftCount;
  304. *zExpPtr = 1 - shiftCount;
  305. }
  306. /*
  307. -------------------------------------------------------------------------------
  308. Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
  309. double-precision floating-point value, returning the result. After being
  310. shifted into the proper positions, the three fields are simply added
  311. together to form the result. This means that any integer portion of `zSig'
  312. will be added into the exponent. Since a properly normalized significand
  313. will have an integer portion equal to 1, the `zExp' input should be 1 less
  314. than the desired result exponent whenever `zSig' is a complete, normalized
  315. significand.
  316. -------------------------------------------------------------------------------
  317. */
  318. INLINE float64 packFloat64( flag zSign, int16 zExp, bits64 zSig )
  319. {
  320. return ( ( (bits64) zSign )<<63 ) + ( ( (bits64) zExp )<<52 ) + zSig;
  321. }
  322. /*
  323. -------------------------------------------------------------------------------
  324. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  325. and significand `zSig', and returns the proper double-precision floating-
  326. point value corresponding to the abstract input. Ordinarily, the abstract
  327. value is simply rounded and packed into the double-precision format, with
  328. the inexact exception raised if the abstract input cannot be represented
  329. exactly. If the abstract value is too large, however, the overflow and
  330. inexact exceptions are raised and an infinity or maximal finite value is
  331. returned. If the abstract value is too small, the input value is rounded to
  332. a subnormal number, and the underflow and inexact exceptions are raised if
  333. the abstract input cannot be represented exactly as a subnormal double-
  334. precision floating-point number.
  335. The input significand `zSig' has its binary point between bits 62
  336. and 61, which is 10 bits to the left of the usual location. This shifted
  337. significand must be normalized or smaller. If `zSig' is not normalized,
  338. `zExp' must be 0; in that case, the result returned is a subnormal number,
  339. and it must not require rounding. In the usual case that `zSig' is
  340. normalized, `zExp' must be 1 less than the ``true'' floating-point exponent.
  341. The handling of underflow and overflow follows the IEC/IEEE Standard for
  342. Binary Floating-point Arithmetic.
  343. -------------------------------------------------------------------------------
  344. */
  345. static float64 roundAndPackFloat64( flag zSign, int16 zExp, bits64 zSig )
  346. {
  347. int8 roundingMode;
  348. flag roundNearestEven;
  349. int16 roundIncrement, roundBits;
  350. flag isTiny;
  351. roundingMode = float_rounding_mode;
  352. roundNearestEven = ( roundingMode == float_round_nearest_even );
  353. roundIncrement = 0x200;
  354. if ( ! roundNearestEven ) {
  355. if ( roundingMode == float_round_to_zero ) {
  356. roundIncrement = 0;
  357. }
  358. else {
  359. roundIncrement = 0x3FF;
  360. if ( zSign ) {
  361. if ( roundingMode == float_round_up ) roundIncrement = 0;
  362. }
  363. else {
  364. if ( roundingMode == float_round_down ) roundIncrement = 0;
  365. }
  366. }
  367. }
  368. roundBits = zSig & 0x3FF;
  369. if ( 0x7FD <= (bits16) zExp ) {
  370. if ( ( 0x7FD < zExp )
  371. || ( ( zExp == 0x7FD )
  372. && ( (sbits64) ( zSig + roundIncrement ) < 0 ) )
  373. ) {
  374. //register int lr = __builtin_return_address(0);
  375. //printk("roundAndPackFloat64 called from 0x%08x\n",lr);
  376. float_raise( float_flag_overflow | float_flag_inexact );
  377. return packFloat64( zSign, 0x7FF, 0 ) - ( roundIncrement == 0 );
  378. }
  379. if ( zExp < 0 ) {
  380. isTiny =
  381. ( float_detect_tininess == float_tininess_before_rounding )
  382. || ( zExp < -1 )
  383. || ( zSig + roundIncrement < LIT64( 0x8000000000000000 ) );
  384. shift64RightJamming( zSig, - zExp, &zSig );
  385. zExp = 0;
  386. roundBits = zSig & 0x3FF;
  387. if ( isTiny && roundBits ) float_raise( float_flag_underflow );
  388. }
  389. }
  390. if ( roundBits ) float_exception_flags |= float_flag_inexact;
  391. zSig = ( zSig + roundIncrement )>>10;
  392. zSig &= ~ ( ( ( roundBits ^ 0x200 ) == 0 ) & roundNearestEven );
  393. if ( zSig == 0 ) zExp = 0;
  394. return packFloat64( zSign, zExp, zSig );
  395. }
  396. /*
  397. -------------------------------------------------------------------------------
  398. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  399. and significand `zSig', and returns the proper double-precision floating-
  400. point value corresponding to the abstract input. This routine is just like
  401. `roundAndPackFloat64' except that `zSig' does not have to be normalized in
  402. any way. In all cases, `zExp' must be 1 less than the ``true'' floating-
  403. point exponent.
  404. -------------------------------------------------------------------------------
  405. */
  406. static float64
  407. normalizeRoundAndPackFloat64( flag zSign, int16 zExp, bits64 zSig )
  408. {
  409. int8 shiftCount;
  410. shiftCount = countLeadingZeros64( zSig ) - 1;
  411. return roundAndPackFloat64( zSign, zExp - shiftCount, zSig<<shiftCount );
  412. }
  413. #ifdef FLOATX80
  414. /*
  415. -------------------------------------------------------------------------------
  416. Returns the fraction bits of the extended double-precision floating-point
  417. value `a'.
  418. -------------------------------------------------------------------------------
  419. */
  420. INLINE bits64 extractFloatx80Frac( floatx80 a )
  421. {
  422. return a.low;
  423. }
  424. /*
  425. -------------------------------------------------------------------------------
  426. Returns the exponent bits of the extended double-precision floating-point
  427. value `a'.
  428. -------------------------------------------------------------------------------
  429. */
  430. INLINE int32 extractFloatx80Exp( floatx80 a )
  431. {
  432. return a.high & 0x7FFF;
  433. }
  434. /*
  435. -------------------------------------------------------------------------------
  436. Returns the sign bit of the extended double-precision floating-point value
  437. `a'.
  438. -------------------------------------------------------------------------------
  439. */
  440. INLINE flag extractFloatx80Sign( floatx80 a )
  441. {
  442. return a.high>>15;
  443. }
  444. /*
  445. -------------------------------------------------------------------------------
  446. Normalizes the subnormal extended double-precision floating-point value
  447. represented by the denormalized significand `aSig'. The normalized exponent
  448. and significand are stored at the locations pointed to by `zExpPtr' and
  449. `zSigPtr', respectively.
  450. -------------------------------------------------------------------------------
  451. */
  452. static void
  453. normalizeFloatx80Subnormal( bits64 aSig, int32 *zExpPtr, bits64 *zSigPtr )
  454. {
  455. int8 shiftCount;
  456. shiftCount = countLeadingZeros64( aSig );
  457. *zSigPtr = aSig<<shiftCount;
  458. *zExpPtr = 1 - shiftCount;
  459. }
  460. /*
  461. -------------------------------------------------------------------------------
  462. Packs the sign `zSign', exponent `zExp', and significand `zSig' into an
  463. extended double-precision floating-point value, returning the result.
  464. -------------------------------------------------------------------------------
  465. */
  466. INLINE floatx80 packFloatx80( flag zSign, int32 zExp, bits64 zSig )
  467. {
  468. floatx80 z;
  469. z.low = zSig;
  470. z.high = ( ( (bits16) zSign )<<15 ) + zExp;
  471. return z;
  472. }
  473. /*
  474. -------------------------------------------------------------------------------
  475. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  476. and extended significand formed by the concatenation of `zSig0' and `zSig1',
  477. and returns the proper extended double-precision floating-point value
  478. corresponding to the abstract input. Ordinarily, the abstract value is
  479. rounded and packed into the extended double-precision format, with the
  480. inexact exception raised if the abstract input cannot be represented
  481. exactly. If the abstract value is too large, however, the overflow and
  482. inexact exceptions are raised and an infinity or maximal finite value is
  483. returned. If the abstract value is too small, the input value is rounded to
  484. a subnormal number, and the underflow and inexact exceptions are raised if
  485. the abstract input cannot be represented exactly as a subnormal extended
  486. double-precision floating-point number.
  487. If `roundingPrecision' is 32 or 64, the result is rounded to the same
  488. number of bits as single or double precision, respectively. Otherwise, the
  489. result is rounded to the full precision of the extended double-precision
  490. format.
  491. The input significand must be normalized or smaller. If the input
  492. significand is not normalized, `zExp' must be 0; in that case, the result
  493. returned is a subnormal number, and it must not require rounding. The
  494. handling of underflow and overflow follows the IEC/IEEE Standard for Binary
  495. Floating-point Arithmetic.
  496. -------------------------------------------------------------------------------
  497. */
  498. static floatx80
  499. roundAndPackFloatx80(
  500. int8 roundingPrecision, flag zSign, int32 zExp, bits64 zSig0, bits64 zSig1
  501. )
  502. {
  503. int8 roundingMode;
  504. flag roundNearestEven, increment, isTiny;
  505. int64 roundIncrement, roundMask, roundBits;
  506. roundingMode = float_rounding_mode;
  507. roundNearestEven = ( roundingMode == float_round_nearest_even );
  508. if ( roundingPrecision == 80 ) goto precision80;
  509. if ( roundingPrecision == 64 ) {
  510. roundIncrement = LIT64( 0x0000000000000400 );
  511. roundMask = LIT64( 0x00000000000007FF );
  512. }
  513. else if ( roundingPrecision == 32 ) {
  514. roundIncrement = LIT64( 0x0000008000000000 );
  515. roundMask = LIT64( 0x000000FFFFFFFFFF );
  516. }
  517. else {
  518. goto precision80;
  519. }
  520. zSig0 |= ( zSig1 != 0 );
  521. if ( ! roundNearestEven ) {
  522. if ( roundingMode == float_round_to_zero ) {
  523. roundIncrement = 0;
  524. }
  525. else {
  526. roundIncrement = roundMask;
  527. if ( zSign ) {
  528. if ( roundingMode == float_round_up ) roundIncrement = 0;
  529. }
  530. else {
  531. if ( roundingMode == float_round_down ) roundIncrement = 0;
  532. }
  533. }
  534. }
  535. roundBits = zSig0 & roundMask;
  536. if ( 0x7FFD <= (bits32) ( zExp - 1 ) ) {
  537. if ( ( 0x7FFE < zExp )
  538. || ( ( zExp == 0x7FFE ) && ( zSig0 + roundIncrement < zSig0 ) )
  539. ) {
  540. goto overflow;
  541. }
  542. if ( zExp <= 0 ) {
  543. isTiny =
  544. ( float_detect_tininess == float_tininess_before_rounding )
  545. || ( zExp < 0 )
  546. || ( zSig0 <= zSig0 + roundIncrement );
  547. shift64RightJamming( zSig0, 1 - zExp, &zSig0 );
  548. zExp = 0;
  549. roundBits = zSig0 & roundMask;
  550. if ( isTiny && roundBits ) float_raise( float_flag_underflow );
  551. if ( roundBits ) float_exception_flags |= float_flag_inexact;
  552. zSig0 += roundIncrement;
  553. if ( (sbits64) zSig0 < 0 ) zExp = 1;
  554. roundIncrement = roundMask + 1;
  555. if ( roundNearestEven && ( roundBits<<1 == roundIncrement ) ) {
  556. roundMask |= roundIncrement;
  557. }
  558. zSig0 &= ~ roundMask;
  559. return packFloatx80( zSign, zExp, zSig0 );
  560. }
  561. }
  562. if ( roundBits ) float_exception_flags |= float_flag_inexact;
  563. zSig0 += roundIncrement;
  564. if ( zSig0 < roundIncrement ) {
  565. ++zExp;
  566. zSig0 = LIT64( 0x8000000000000000 );
  567. }
  568. roundIncrement = roundMask + 1;
  569. if ( roundNearestEven && ( roundBits<<1 == roundIncrement ) ) {
  570. roundMask |= roundIncrement;
  571. }
  572. zSig0 &= ~ roundMask;
  573. if ( zSig0 == 0 ) zExp = 0;
  574. return packFloatx80( zSign, zExp, zSig0 );
  575. precision80:
  576. increment = ( (sbits64) zSig1 < 0 );
  577. if ( ! roundNearestEven ) {
  578. if ( roundingMode == float_round_to_zero ) {
  579. increment = 0;
  580. }
  581. else {
  582. if ( zSign ) {
  583. increment = ( roundingMode == float_round_down ) && zSig1;
  584. }
  585. else {
  586. increment = ( roundingMode == float_round_up ) && zSig1;
  587. }
  588. }
  589. }
  590. if ( 0x7FFD <= (bits32) ( zExp - 1 ) ) {
  591. if ( ( 0x7FFE < zExp )
  592. || ( ( zExp == 0x7FFE )
  593. && ( zSig0 == LIT64( 0xFFFFFFFFFFFFFFFF ) )
  594. && increment
  595. )
  596. ) {
  597. roundMask = 0;
  598. overflow:
  599. float_raise( float_flag_overflow | float_flag_inexact );
  600. if ( ( roundingMode == float_round_to_zero )
  601. || ( zSign && ( roundingMode == float_round_up ) )
  602. || ( ! zSign && ( roundingMode == float_round_down ) )
  603. ) {
  604. return packFloatx80( zSign, 0x7FFE, ~ roundMask );
  605. }
  606. return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
  607. }
  608. if ( zExp <= 0 ) {
  609. isTiny =
  610. ( float_detect_tininess == float_tininess_before_rounding )
  611. || ( zExp < 0 )
  612. || ! increment
  613. || ( zSig0 < LIT64( 0xFFFFFFFFFFFFFFFF ) );
  614. shift64ExtraRightJamming( zSig0, zSig1, 1 - zExp, &zSig0, &zSig1 );
  615. zExp = 0;
  616. if ( isTiny && zSig1 ) float_raise( float_flag_underflow );
  617. if ( zSig1 ) float_exception_flags |= float_flag_inexact;
  618. if ( roundNearestEven ) {
  619. increment = ( (sbits64) zSig1 < 0 );
  620. }
  621. else {
  622. if ( zSign ) {
  623. increment = ( roundingMode == float_round_down ) && zSig1;
  624. }
  625. else {
  626. increment = ( roundingMode == float_round_up ) && zSig1;
  627. }
  628. }
  629. if ( increment ) {
  630. ++zSig0;
  631. zSig0 &= ~ ( ( zSig1 + zSig1 == 0 ) & roundNearestEven );
  632. if ( (sbits64) zSig0 < 0 ) zExp = 1;
  633. }
  634. return packFloatx80( zSign, zExp, zSig0 );
  635. }
  636. }
  637. if ( zSig1 ) float_exception_flags |= float_flag_inexact;
  638. if ( increment ) {
  639. ++zSig0;
  640. if ( zSig0 == 0 ) {
  641. ++zExp;
  642. zSig0 = LIT64( 0x8000000000000000 );
  643. }
  644. else {
  645. zSig0 &= ~ ( ( zSig1 + zSig1 == 0 ) & roundNearestEven );
  646. }
  647. }
  648. else {
  649. if ( zSig0 == 0 ) zExp = 0;
  650. }
  651. return packFloatx80( zSign, zExp, zSig0 );
  652. }
  653. /*
  654. -------------------------------------------------------------------------------
  655. Takes an abstract floating-point value having sign `zSign', exponent
  656. `zExp', and significand formed by the concatenation of `zSig0' and `zSig1',
  657. and returns the proper extended double-precision floating-point value
  658. corresponding to the abstract input. This routine is just like
  659. `roundAndPackFloatx80' except that the input significand does not have to be
  660. normalized.
  661. -------------------------------------------------------------------------------
  662. */
  663. static floatx80
  664. normalizeRoundAndPackFloatx80(
  665. int8 roundingPrecision, flag zSign, int32 zExp, bits64 zSig0, bits64 zSig1
  666. )
  667. {
  668. int8 shiftCount;
  669. if ( zSig0 == 0 ) {
  670. zSig0 = zSig1;
  671. zSig1 = 0;
  672. zExp -= 64;
  673. }
  674. shiftCount = countLeadingZeros64( zSig0 );
  675. shortShift128Left( zSig0, zSig1, shiftCount, &zSig0, &zSig1 );
  676. zExp -= shiftCount;
  677. return
  678. roundAndPackFloatx80( roundingPrecision, zSign, zExp, zSig0, zSig1 );
  679. }
  680. #endif
  681. /*
  682. -------------------------------------------------------------------------------
  683. Returns the result of converting the 32-bit two's complement integer `a' to
  684. the single-precision floating-point format. The conversion is performed
  685. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  686. -------------------------------------------------------------------------------
  687. */
  688. float32 int32_to_float32( int32 a )
  689. {
  690. flag zSign;
  691. if ( a == 0 ) return 0;
  692. if ( a == 0x80000000 ) return packFloat32( 1, 0x9E, 0 );
  693. zSign = ( a < 0 );
  694. return normalizeRoundAndPackFloat32( zSign, 0x9C, zSign ? - a : a );
  695. }
  696. /*
  697. -------------------------------------------------------------------------------
  698. Returns the result of converting the 32-bit two's complement integer `a' to
  699. the double-precision floating-point format. The conversion is performed
  700. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  701. -------------------------------------------------------------------------------
  702. */
  703. float64 int32_to_float64( int32 a )
  704. {
  705. flag aSign;
  706. uint32 absA;
  707. int8 shiftCount;
  708. bits64 zSig;
  709. if ( a == 0 ) return 0;
  710. aSign = ( a < 0 );
  711. absA = aSign ? - a : a;
  712. shiftCount = countLeadingZeros32( absA ) + 21;
  713. zSig = absA;
  714. return packFloat64( aSign, 0x432 - shiftCount, zSig<<shiftCount );
  715. }
  716. #ifdef FLOATX80
  717. /*
  718. -------------------------------------------------------------------------------
  719. Returns the result of converting the 32-bit two's complement integer `a'
  720. to the extended double-precision floating-point format. The conversion
  721. is performed according to the IEC/IEEE Standard for Binary Floating-point
  722. Arithmetic.
  723. -------------------------------------------------------------------------------
  724. */
  725. floatx80 int32_to_floatx80( int32 a )
  726. {
  727. flag zSign;
  728. uint32 absA;
  729. int8 shiftCount;
  730. bits64 zSig;
  731. if ( a == 0 ) return packFloatx80( 0, 0, 0 );
  732. zSign = ( a < 0 );
  733. absA = zSign ? - a : a;
  734. shiftCount = countLeadingZeros32( absA ) + 32;
  735. zSig = absA;
  736. return packFloatx80( zSign, 0x403E - shiftCount, zSig<<shiftCount );
  737. }
  738. #endif
  739. /*
  740. -------------------------------------------------------------------------------
  741. Returns the result of converting the single-precision floating-point value
  742. `a' to the 32-bit two's complement integer format. The conversion is
  743. performed according to the IEC/IEEE Standard for Binary Floating-point
  744. Arithmetic---which means in particular that the conversion is rounded
  745. according to the current rounding mode. If `a' is a NaN, the largest
  746. positive integer is returned. Otherwise, if the conversion overflows, the
  747. largest integer with the same sign as `a' is returned.
  748. -------------------------------------------------------------------------------
  749. */
  750. int32 float32_to_int32( float32 a )
  751. {
  752. flag aSign;
  753. int16 aExp, shiftCount;
  754. bits32 aSig;
  755. bits64 zSig;
  756. aSig = extractFloat32Frac( a );
  757. aExp = extractFloat32Exp( a );
  758. aSign = extractFloat32Sign( a );
  759. if ( ( aExp == 0x7FF ) && aSig ) aSign = 0;
  760. if ( aExp ) aSig |= 0x00800000;
  761. shiftCount = 0xAF - aExp;
  762. zSig = aSig;
  763. zSig <<= 32;
  764. if ( 0 < shiftCount ) shift64RightJamming( zSig, shiftCount, &zSig );
  765. return roundAndPackInt32( aSign, zSig );
  766. }
  767. /*
  768. -------------------------------------------------------------------------------
  769. Returns the result of converting the single-precision floating-point value
  770. `a' to the 32-bit two's complement integer format. The conversion is
  771. performed according to the IEC/IEEE Standard for Binary Floating-point
  772. Arithmetic, except that the conversion is always rounded toward zero. If
  773. `a' is a NaN, the largest positive integer is returned. Otherwise, if the
  774. conversion overflows, the largest integer with the same sign as `a' is
  775. returned.
  776. -------------------------------------------------------------------------------
  777. */
  778. int32 float32_to_int32_round_to_zero( float32 a )
  779. {
  780. flag aSign;
  781. int16 aExp, shiftCount;
  782. bits32 aSig;
  783. int32 z;
  784. aSig = extractFloat32Frac( a );
  785. aExp = extractFloat32Exp( a );
  786. aSign = extractFloat32Sign( a );
  787. shiftCount = aExp - 0x9E;
  788. if ( 0 <= shiftCount ) {
  789. if ( a == 0xCF000000 ) return 0x80000000;
  790. float_raise( float_flag_invalid );
  791. if ( ! aSign || ( ( aExp == 0xFF ) && aSig ) ) return 0x7FFFFFFF;
  792. return 0x80000000;
  793. }
  794. else if ( aExp <= 0x7E ) {
  795. if ( aExp | aSig ) float_exception_flags |= float_flag_inexact;
  796. return 0;
  797. }
  798. aSig = ( aSig | 0x00800000 )<<8;
  799. z = aSig>>( - shiftCount );
  800. if ( (bits32) ( aSig<<( shiftCount & 31 ) ) ) {
  801. float_exception_flags |= float_flag_inexact;
  802. }
  803. return aSign ? - z : z;
  804. }
  805. /*
  806. -------------------------------------------------------------------------------
  807. Returns the result of converting the single-precision floating-point value
  808. `a' to the double-precision floating-point format. The conversion is
  809. performed according to the IEC/IEEE Standard for Binary Floating-point
  810. Arithmetic.
  811. -------------------------------------------------------------------------------
  812. */
  813. float64 float32_to_float64( float32 a )
  814. {
  815. flag aSign;
  816. int16 aExp;
  817. bits32 aSig;
  818. aSig = extractFloat32Frac( a );
  819. aExp = extractFloat32Exp( a );
  820. aSign = extractFloat32Sign( a );
  821. if ( aExp == 0xFF ) {
  822. if ( aSig ) return commonNaNToFloat64( float32ToCommonNaN( a ) );
  823. return packFloat64( aSign, 0x7FF, 0 );
  824. }
  825. if ( aExp == 0 ) {
  826. if ( aSig == 0 ) return packFloat64( aSign, 0, 0 );
  827. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  828. --aExp;
  829. }
  830. return packFloat64( aSign, aExp + 0x380, ( (bits64) aSig )<<29 );
  831. }
  832. #ifdef FLOATX80
  833. /*
  834. -------------------------------------------------------------------------------
  835. Returns the result of converting the single-precision floating-point value
  836. `a' to the extended double-precision floating-point format. The conversion
  837. is performed according to the IEC/IEEE Standard for Binary Floating-point
  838. Arithmetic.
  839. -------------------------------------------------------------------------------
  840. */
  841. floatx80 float32_to_floatx80( float32 a )
  842. {
  843. flag aSign;
  844. int16 aExp;
  845. bits32 aSig;
  846. aSig = extractFloat32Frac( a );
  847. aExp = extractFloat32Exp( a );
  848. aSign = extractFloat32Sign( a );
  849. if ( aExp == 0xFF ) {
  850. if ( aSig ) return commonNaNToFloatx80( float32ToCommonNaN( a ) );
  851. return packFloatx80( aSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
  852. }
  853. if ( aExp == 0 ) {
  854. if ( aSig == 0 ) return packFloatx80( aSign, 0, 0 );
  855. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  856. }
  857. aSig |= 0x00800000;
  858. return packFloatx80( aSign, aExp + 0x3F80, ( (bits64) aSig )<<40 );
  859. }
  860. #endif
  861. /*
  862. -------------------------------------------------------------------------------
  863. Rounds the single-precision floating-point value `a' to an integer, and
  864. returns the result as a single-precision floating-point value. The
  865. operation is performed according to the IEC/IEEE Standard for Binary
  866. Floating-point Arithmetic.
  867. -------------------------------------------------------------------------------
  868. */
  869. float32 float32_round_to_int( float32 a )
  870. {
  871. flag aSign;
  872. int16 aExp;
  873. bits32 lastBitMask, roundBitsMask;
  874. int8 roundingMode;
  875. float32 z;
  876. aExp = extractFloat32Exp( a );
  877. if ( 0x96 <= aExp ) {
  878. if ( ( aExp == 0xFF ) && extractFloat32Frac( a ) ) {
  879. return propagateFloat32NaN( a, a );
  880. }
  881. return a;
  882. }
  883. if ( aExp <= 0x7E ) {
  884. if ( (bits32) ( a<<1 ) == 0 ) return a;
  885. float_exception_flags |= float_flag_inexact;
  886. aSign = extractFloat32Sign( a );
  887. switch ( float_rounding_mode ) {
  888. case float_round_nearest_even:
  889. if ( ( aExp == 0x7E ) && extractFloat32Frac( a ) ) {
  890. return packFloat32( aSign, 0x7F, 0 );
  891. }
  892. break;
  893. case float_round_down:
  894. return aSign ? 0xBF800000 : 0;
  895. case float_round_up:
  896. return aSign ? 0x80000000 : 0x3F800000;
  897. }
  898. return packFloat32( aSign, 0, 0 );
  899. }
  900. lastBitMask = 1;
  901. lastBitMask <<= 0x96 - aExp;
  902. roundBitsMask = lastBitMask - 1;
  903. z = a;
  904. roundingMode = float_rounding_mode;
  905. if ( roundingMode == float_round_nearest_even ) {
  906. z += lastBitMask>>1;
  907. if ( ( z & roundBitsMask ) == 0 ) z &= ~ lastBitMask;
  908. }
  909. else if ( roundingMode != float_round_to_zero ) {
  910. if ( extractFloat32Sign( z ) ^ ( roundingMode == float_round_up ) ) {
  911. z += roundBitsMask;
  912. }
  913. }
  914. z &= ~ roundBitsMask;
  915. if ( z != a ) float_exception_flags |= float_flag_inexact;
  916. return z;
  917. }
  918. /*
  919. -------------------------------------------------------------------------------
  920. Returns the result of adding the absolute values of the single-precision
  921. floating-point values `a' and `b'. If `zSign' is true, the sum is negated
  922. before being returned. `zSign' is ignored if the result is a NaN. The
  923. addition is performed according to the IEC/IEEE Standard for Binary
  924. Floating-point Arithmetic.
  925. -------------------------------------------------------------------------------
  926. */
  927. static float32 addFloat32Sigs( float32 a, float32 b, flag zSign )
  928. {
  929. int16 aExp, bExp, zExp;
  930. bits32 aSig, bSig, zSig;
  931. int16 expDiff;
  932. aSig = extractFloat32Frac( a );
  933. aExp = extractFloat32Exp( a );
  934. bSig = extractFloat32Frac( b );
  935. bExp = extractFloat32Exp( b );
  936. expDiff = aExp - bExp;
  937. aSig <<= 6;
  938. bSig <<= 6;
  939. if ( 0 < expDiff ) {
  940. if ( aExp == 0xFF ) {
  941. if ( aSig ) return propagateFloat32NaN( a, b );
  942. return a;
  943. }
  944. if ( bExp == 0 ) {
  945. --expDiff;
  946. }
  947. else {
  948. bSig |= 0x20000000;
  949. }
  950. shift32RightJamming( bSig, expDiff, &bSig );
  951. zExp = aExp;
  952. }
  953. else if ( expDiff < 0 ) {
  954. if ( bExp == 0xFF ) {
  955. if ( bSig ) return propagateFloat32NaN( a, b );
  956. return packFloat32( zSign, 0xFF, 0 );
  957. }
  958. if ( aExp == 0 ) {
  959. ++expDiff;
  960. }
  961. else {
  962. aSig |= 0x20000000;
  963. }
  964. shift32RightJamming( aSig, - expDiff, &aSig );
  965. zExp = bExp;
  966. }
  967. else {
  968. if ( aExp == 0xFF ) {
  969. if ( aSig | bSig ) return propagateFloat32NaN( a, b );
  970. return a;
  971. }
  972. if ( aExp == 0 ) return packFloat32( zSign, 0, ( aSig + bSig )>>6 );
  973. zSig = 0x40000000 + aSig + bSig;
  974. zExp = aExp;
  975. goto roundAndPack;
  976. }
  977. aSig |= 0x20000000;
  978. zSig = ( aSig + bSig )<<1;
  979. --zExp;
  980. if ( (sbits32) zSig < 0 ) {
  981. zSig = aSig + bSig;
  982. ++zExp;
  983. }
  984. roundAndPack:
  985. return roundAndPackFloat32( zSign, zExp, zSig );
  986. }
  987. /*
  988. -------------------------------------------------------------------------------
  989. Returns the result of subtracting the absolute values of the single-
  990. precision floating-point values `a' and `b'. If `zSign' is true, the
  991. difference is negated before being returned. `zSign' is ignored if the
  992. result is a NaN. The subtraction is performed according to the IEC/IEEE
  993. Standard for Binary Floating-point Arithmetic.
  994. -------------------------------------------------------------------------------
  995. */
  996. static float32 subFloat32Sigs( float32 a, float32 b, flag zSign )
  997. {
  998. int16 aExp, bExp, zExp;
  999. bits32 aSig, bSig, zSig;
  1000. int16 expDiff;
  1001. aSig = extractFloat32Frac( a );
  1002. aExp = extractFloat32Exp( a );
  1003. bSig = extractFloat32Frac( b );
  1004. bExp = extractFloat32Exp( b );
  1005. expDiff = aExp - bExp;
  1006. aSig <<= 7;
  1007. bSig <<= 7;
  1008. if ( 0 < expDiff ) goto aExpBigger;
  1009. if ( expDiff < 0 ) goto bExpBigger;
  1010. if ( aExp == 0xFF ) {
  1011. if ( aSig | bSig ) return propagateFloat32NaN( a, b );
  1012. float_raise( float_flag_invalid );
  1013. return float32_default_nan;
  1014. }
  1015. if ( aExp == 0 ) {
  1016. aExp = 1;
  1017. bExp = 1;
  1018. }
  1019. if ( bSig < aSig ) goto aBigger;
  1020. if ( aSig < bSig ) goto bBigger;
  1021. return packFloat32( float_rounding_mode == float_round_down, 0, 0 );
  1022. bExpBigger:
  1023. if ( bExp == 0xFF ) {
  1024. if ( bSig ) return propagateFloat32NaN( a, b );
  1025. return packFloat32( zSign ^ 1, 0xFF, 0 );
  1026. }
  1027. if ( aExp == 0 ) {
  1028. ++expDiff;
  1029. }
  1030. else {
  1031. aSig |= 0x40000000;
  1032. }
  1033. shift32RightJamming( aSig, - expDiff, &aSig );
  1034. bSig |= 0x40000000;
  1035. bBigger:
  1036. zSig = bSig - aSig;
  1037. zExp = bExp;
  1038. zSign ^= 1;
  1039. goto normalizeRoundAndPack;
  1040. aExpBigger:
  1041. if ( aExp == 0xFF ) {
  1042. if ( aSig ) return propagateFloat32NaN( a, b );
  1043. return a;
  1044. }
  1045. if ( bExp == 0 ) {
  1046. --expDiff;
  1047. }
  1048. else {
  1049. bSig |= 0x40000000;
  1050. }
  1051. shift32RightJamming( bSig, expDiff, &bSig );
  1052. aSig |= 0x40000000;
  1053. aBigger:
  1054. zSig = aSig - bSig;
  1055. zExp = aExp;
  1056. normalizeRoundAndPack:
  1057. --zExp;
  1058. return normalizeRoundAndPackFloat32( zSign, zExp, zSig );
  1059. }
  1060. /*
  1061. -------------------------------------------------------------------------------
  1062. Returns the result of adding the single-precision floating-point values `a'
  1063. and `b'. The operation is performed according to the IEC/IEEE Standard for
  1064. Binary Floating-point Arithmetic.
  1065. -------------------------------------------------------------------------------
  1066. */
  1067. float32 float32_add( float32 a, float32 b )
  1068. {
  1069. flag aSign, bSign;
  1070. aSign = extractFloat32Sign( a );
  1071. bSign = extractFloat32Sign( b );
  1072. if ( aSign == bSign ) {
  1073. return addFloat32Sigs( a, b, aSign );
  1074. }
  1075. else {
  1076. return subFloat32Sigs( a, b, aSign );
  1077. }
  1078. }
  1079. /*
  1080. -------------------------------------------------------------------------------
  1081. Returns the result of subtracting the single-precision floating-point values
  1082. `a' and `b'. The operation is performed according to the IEC/IEEE Standard
  1083. for Binary Floating-point Arithmetic.
  1084. -------------------------------------------------------------------------------
  1085. */
  1086. float32 float32_sub( float32 a, float32 b )
  1087. {
  1088. flag aSign, bSign;
  1089. aSign = extractFloat32Sign( a );
  1090. bSign = extractFloat32Sign( b );
  1091. if ( aSign == bSign ) {
  1092. return subFloat32Sigs( a, b, aSign );
  1093. }
  1094. else {
  1095. return addFloat32Sigs( a, b, aSign );
  1096. }
  1097. }
  1098. /*
  1099. -------------------------------------------------------------------------------
  1100. Returns the result of multiplying the single-precision floating-point values
  1101. `a' and `b'. The operation is performed according to the IEC/IEEE Standard
  1102. for Binary Floating-point Arithmetic.
  1103. -------------------------------------------------------------------------------
  1104. */
  1105. float32 float32_mul( float32 a, float32 b )
  1106. {
  1107. flag aSign, bSign, zSign;
  1108. int16 aExp, bExp, zExp;
  1109. bits32 aSig, bSig;
  1110. bits64 zSig64;
  1111. bits32 zSig;
  1112. aSig = extractFloat32Frac( a );
  1113. aExp = extractFloat32Exp( a );
  1114. aSign = extractFloat32Sign( a );
  1115. bSig = extractFloat32Frac( b );
  1116. bExp = extractFloat32Exp( b );
  1117. bSign = extractFloat32Sign( b );
  1118. zSign = aSign ^ bSign;
  1119. if ( aExp == 0xFF ) {
  1120. if ( aSig || ( ( bExp == 0xFF ) && bSig ) ) {
  1121. return propagateFloat32NaN( a, b );
  1122. }
  1123. if ( ( bExp | bSig ) == 0 ) {
  1124. float_raise( float_flag_invalid );
  1125. return float32_default_nan;
  1126. }
  1127. return packFloat32( zSign, 0xFF, 0 );
  1128. }
  1129. if ( bExp == 0xFF ) {
  1130. if ( bSig ) return propagateFloat32NaN( a, b );
  1131. if ( ( aExp | aSig ) == 0 ) {
  1132. float_raise( float_flag_invalid );
  1133. return float32_default_nan;
  1134. }
  1135. return packFloat32( zSign, 0xFF, 0 );
  1136. }
  1137. if ( aExp == 0 ) {
  1138. if ( aSig == 0 ) return packFloat32( zSign, 0, 0 );
  1139. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  1140. }
  1141. if ( bExp == 0 ) {
  1142. if ( bSig == 0 ) return packFloat32( zSign, 0, 0 );
  1143. normalizeFloat32Subnormal( bSig, &bExp, &bSig );
  1144. }
  1145. zExp = aExp + bExp - 0x7F;
  1146. aSig = ( aSig | 0x00800000 )<<7;
  1147. bSig = ( bSig | 0x00800000 )<<8;
  1148. shift64RightJamming( ( (bits64) aSig ) * bSig, 32, &zSig64 );
  1149. zSig = zSig64;
  1150. if ( 0 <= (sbits32) ( zSig<<1 ) ) {
  1151. zSig <<= 1;
  1152. --zExp;
  1153. }
  1154. return roundAndPackFloat32( zSign, zExp, zSig );
  1155. }
  1156. /*
  1157. -------------------------------------------------------------------------------
  1158. Returns the result of dividing the single-precision floating-point value `a'
  1159. by the corresponding value `b'. The operation is performed according to the
  1160. IEC/IEEE Standard for Binary Floating-point Arithmetic.
  1161. -------------------------------------------------------------------------------
  1162. */
  1163. float32 float32_div( float32 a, float32 b )
  1164. {
  1165. flag aSign, bSign, zSign;
  1166. int16 aExp, bExp, zExp;
  1167. bits32 aSig, bSig, zSig;
  1168. aSig = extractFloat32Frac( a );
  1169. aExp = extractFloat32Exp( a );
  1170. aSign = extractFloat32Sign( a );
  1171. bSig = extractFloat32Frac( b );
  1172. bExp = extractFloat32Exp( b );
  1173. bSign = extractFloat32Sign( b );
  1174. zSign = aSign ^ bSign;
  1175. if ( aExp == 0xFF ) {
  1176. if ( aSig ) return propagateFloat32NaN( a, b );
  1177. if ( bExp == 0xFF ) {
  1178. if ( bSig ) return propagateFloat32NaN( a, b );
  1179. float_raise( float_flag_invalid );
  1180. return float32_default_nan;
  1181. }
  1182. return packFloat32( zSign, 0xFF, 0 );
  1183. }
  1184. if ( bExp == 0xFF ) {
  1185. if ( bSig ) return propagateFloat32NaN( a, b );
  1186. return packFloat32( zSign, 0, 0 );
  1187. }
  1188. if ( bExp == 0 ) {
  1189. if ( bSig == 0 ) {
  1190. if ( ( aExp | aSig ) == 0 ) {
  1191. float_raise( float_flag_invalid );
  1192. return float32_default_nan;
  1193. }
  1194. float_raise( float_flag_divbyzero );
  1195. return packFloat32( zSign, 0xFF, 0 );
  1196. }
  1197. normalizeFloat32Subnormal( bSig, &bExp, &bSig );
  1198. }
  1199. if ( aExp == 0 ) {
  1200. if ( aSig == 0 ) return packFloat32( zSign, 0, 0 );
  1201. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  1202. }
  1203. zExp = aExp - bExp + 0x7D;
  1204. aSig = ( aSig | 0x00800000 )<<7;
  1205. bSig = ( bSig | 0x00800000 )<<8;
  1206. if ( bSig <= ( aSig + aSig ) ) {
  1207. aSig >>= 1;
  1208. ++zExp;
  1209. }
  1210. zSig = ( ( (bits64) aSig )<<32 ) / bSig;
  1211. if ( ( zSig & 0x3F ) == 0 ) {
  1212. zSig |= ( ( (bits64) bSig ) * zSig != ( (bits64) aSig )<<32 );
  1213. }
  1214. return roundAndPackFloat32( zSign, zExp, zSig );
  1215. }
  1216. /*
  1217. -------------------------------------------------------------------------------
  1218. Returns the remainder of the single-precision floating-point value `a'
  1219. with respect to the corresponding value `b'. The operation is performed
  1220. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  1221. -------------------------------------------------------------------------------
  1222. */
  1223. float32 float32_rem( float32 a, float32 b )
  1224. {
  1225. flag aSign, bSign, zSign;
  1226. int16 aExp, bExp, expDiff;
  1227. bits32 aSig, bSig;
  1228. bits32 q;
  1229. bits64 aSig64, bSig64, q64;
  1230. bits32 alternateASig;
  1231. sbits32 sigMean;
  1232. aSig = extractFloat32Frac( a );
  1233. aExp = extractFloat32Exp( a );
  1234. aSign = extractFloat32Sign( a );
  1235. bSig = extractFloat32Frac( b );
  1236. bExp = extractFloat32Exp( b );
  1237. bSign = extractFloat32Sign( b );
  1238. if ( aExp == 0xFF ) {
  1239. if ( aSig || ( ( bExp == 0xFF ) && bSig ) ) {
  1240. return propagateFloat32NaN( a, b );
  1241. }
  1242. float_raise( float_flag_invalid );
  1243. return float32_default_nan;
  1244. }
  1245. if ( bExp == 0xFF ) {
  1246. if ( bSig ) return propagateFloat32NaN( a, b );
  1247. return a;
  1248. }
  1249. if ( bExp == 0 ) {
  1250. if ( bSig == 0 ) {
  1251. float_raise( float_flag_invalid );
  1252. return float32_default_nan;
  1253. }
  1254. normalizeFloat32Subnormal( bSig, &bExp, &bSig );
  1255. }
  1256. if ( aExp == 0 ) {
  1257. if ( aSig == 0 ) return a;
  1258. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  1259. }
  1260. expDiff = aExp - bExp;
  1261. aSig |= 0x00800000;
  1262. bSig |= 0x00800000;
  1263. if ( expDiff < 32 ) {
  1264. aSig <<= 8;
  1265. bSig <<= 8;
  1266. if ( expDiff < 0 ) {
  1267. if ( expDiff < -1 ) return a;
  1268. aSig >>= 1;
  1269. }
  1270. q = ( bSig <= aSig );
  1271. if ( q ) aSig -= bSig;
  1272. if ( 0 < expDiff ) {
  1273. q = ( ( (bits64) aSig )<<32 ) / bSig;
  1274. q >>= 32 - expDiff;
  1275. bSig >>= 2;
  1276. aSig = ( ( aSig>>1 )<<( expDiff - 1 ) ) - bSig * q;
  1277. }
  1278. else {
  1279. aSig >>= 2;
  1280. bSig >>= 2;
  1281. }
  1282. }
  1283. else {
  1284. if ( bSig <= aSig ) aSig -= bSig;
  1285. aSig64 = ( (bits64) aSig )<<40;
  1286. bSig64 = ( (bits64) bSig )<<40;
  1287. expDiff -= 64;
  1288. while ( 0 < expDiff ) {
  1289. q64 = estimateDiv128To64( aSig64, 0, bSig64 );
  1290. q64 = ( 2 < q64 ) ? q64 - 2 : 0;
  1291. aSig64 = - ( ( bSig * q64 )<<38 );
  1292. expDiff -= 62;
  1293. }
  1294. expDiff += 64;
  1295. q64 = estimateDiv128To64( aSig64, 0, bSig64 );
  1296. q64 = ( 2 < q64 ) ? q64 - 2 : 0;
  1297. q = q64>>( 64 - expDiff );
  1298. bSig <<= 6;
  1299. aSig = ( ( aSig64>>33 )<<( expDiff - 1 ) ) - bSig * q;
  1300. }
  1301. do {
  1302. alternateASig = aSig;
  1303. ++q;
  1304. aSig -= bSig;
  1305. } while ( 0 <= (sbits32) aSig );
  1306. sigMean = aSig + alternateASig;
  1307. if ( ( sigMean < 0 ) || ( ( sigMean == 0 ) && ( q & 1 ) ) ) {
  1308. aSig = alternateASig;
  1309. }
  1310. zSign = ( (sbits32) aSig < 0 );
  1311. if ( zSign ) aSig = - aSig;
  1312. return normalizeRoundAndPackFloat32( aSign ^ zSign, bExp, aSig );
  1313. }
  1314. /*
  1315. -------------------------------------------------------------------------------
  1316. Returns the square root of the single-precision floating-point value `a'.
  1317. The operation is performed according to the IEC/IEEE Standard for Binary
  1318. Floating-point Arithmetic.
  1319. -------------------------------------------------------------------------------
  1320. */
  1321. float32 float32_sqrt( float32 a )
  1322. {
  1323. flag aSign;
  1324. int16 aExp, zExp;
  1325. bits32 aSig, zSig;
  1326. bits64 rem, term;
  1327. aSig = extractFloat32Frac( a );
  1328. aExp = extractFloat32Exp( a );
  1329. aSign = extractFloat32Sign( a );
  1330. if ( aExp == 0xFF ) {
  1331. if ( aSig ) return propagateFloat32NaN( a, 0 );
  1332. if ( ! aSign ) return a;
  1333. float_raise( float_flag_invalid );
  1334. return float32_default_nan;
  1335. }
  1336. if ( aSign ) {
  1337. if ( ( aExp | aSig ) == 0 ) return a;
  1338. float_raise( float_flag_invalid );
  1339. return float32_default_nan;
  1340. }
  1341. if ( aExp == 0 ) {
  1342. if ( aSig == 0 ) return 0;
  1343. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  1344. }
  1345. zExp = ( ( aExp - 0x7F )>>1 ) + 0x7E;
  1346. aSig = ( aSig | 0x00800000 )<<8;
  1347. zSig = estimateSqrt32( aExp, aSig ) + 2;
  1348. if ( ( zSig & 0x7F ) <= 5 ) {
  1349. if ( zSig < 2 ) {
  1350. zSig = 0xFFFFFFFF;
  1351. }
  1352. else {
  1353. aSig >>= aExp & 1;
  1354. term = ( (bits64) zSig ) * zSig;
  1355. rem = ( ( (bits64) aSig )<<32 ) - term;
  1356. while ( (sbits64) rem < 0 ) {
  1357. --zSig;
  1358. rem += ( ( (bits64) zSig )<<1 ) | 1;
  1359. }
  1360. zSig |= ( rem != 0 );
  1361. }
  1362. }
  1363. shift32RightJamming( zSig, 1, &zSig );
  1364. return roundAndPackFloat32( 0, zExp, zSig );
  1365. }
  1366. /*
  1367. -------------------------------------------------------------------------------
  1368. Returns 1 if the single-precision floating-point value `a' is equal to the
  1369. corresponding value `b', and 0 otherwise. The comparison is performed
  1370. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  1371. -------------------------------------------------------------------------------
  1372. */
  1373. flag float32_eq( float32 a, float32 b )
  1374. {
  1375. if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
  1376. || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
  1377. ) {
  1378. if ( float32_is_signaling_nan( a ) || float32_is_signaling_nan( b ) ) {
  1379. float_raise( float_flag_invalid );
  1380. }
  1381. return 0;
  1382. }
  1383. return ( a == b ) || ( (bits32) ( ( a | b )<<1 ) == 0 );
  1384. }
  1385. /*
  1386. -------------------------------------------------------------------------------
  1387. Returns 1 if the single-precision floating-point value `a' is less than or
  1388. equal to the corresponding value `b', and 0 otherwise. The comparison is
  1389. performed according to the IEC/IEEE Standard for Binary Floating-point
  1390. Arithmetic.
  1391. -------------------------------------------------------------------------------
  1392. */
  1393. flag float32_le( float32 a, float32 b )
  1394. {
  1395. flag aSign, bSign;
  1396. if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
  1397. || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
  1398. ) {
  1399. float_raise( float_flag_invalid );
  1400. return 0;
  1401. }
  1402. aSign = extractFloat32Sign( a );
  1403. bSign = extractFloat32Sign( b );
  1404. if ( aSign != bSign ) return aSign || ( (bits32) ( ( a | b )<<1 ) == 0 );
  1405. return ( a == b ) || ( aSign ^ ( a < b ) );
  1406. }
  1407. /*
  1408. -------------------------------------------------------------------------------
  1409. Returns 1 if the single-precision floating-point value `a' is less than
  1410. the corresponding value `b', and 0 otherwise. The comparison is performed
  1411. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  1412. -------------------------------------------------------------------------------
  1413. */
  1414. flag float32_lt( float32 a, float32 b )
  1415. {
  1416. flag aSign, bSign;
  1417. if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
  1418. || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
  1419. ) {
  1420. float_raise( float_flag_invalid );
  1421. return 0;
  1422. }
  1423. aSign = extractFloat32Sign( a );
  1424. bSign = extractFloat32Sign( b );
  1425. if ( aSign != bSign ) return aSign && ( (bits32) ( ( a | b )<<1 ) != 0 );
  1426. return ( a != b ) && ( aSign ^ ( a < b ) );
  1427. }
  1428. /*
  1429. -------------------------------------------------------------------------------
  1430. Returns 1 if the single-precision floating-point value `a' is equal to the
  1431. corresponding value `b', and 0 otherwise. The invalid exception is raised
  1432. if either operand is a NaN. Otherwise, the comparison is performed
  1433. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  1434. -------------------------------------------------------------------------------
  1435. */
  1436. flag float32_eq_signaling( float32 a, float32 b )
  1437. {
  1438. if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
  1439. || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
  1440. ) {
  1441. float_raise( float_flag_invalid );
  1442. return 0;
  1443. }
  1444. return ( a == b ) || ( (bits32) ( ( a | b )<<1 ) == 0 );
  1445. }
  1446. /*
  1447. -------------------------------------------------------------------------------
  1448. Returns 1 if the single-precision floating-point value `a' is less than or
  1449. equal to the corresponding value `b', and 0 otherwise. Quiet NaNs do not
  1450. cause an exception. Otherwise, the comparison is performed according to the
  1451. IEC/IEEE Standard for Binary Floating-point Arithmetic.
  1452. -------------------------------------------------------------------------------
  1453. */
  1454. flag float32_le_quiet( float32 a, float32 b )
  1455. {
  1456. flag aSign, bSign;
  1457. //int16 aExp, bExp;
  1458. if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
  1459. || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
  1460. ) {
  1461. if ( float32_is_signaling_nan( a ) || float32_is_signaling_nan( b ) ) {
  1462. float_raise( float_flag_invalid );
  1463. }
  1464. return 0;
  1465. }
  1466. aSign = extractFloat32Sign( a );
  1467. bSign = extractFloat32Sign( b );
  1468. if ( aSign != bSign ) return aSign || ( (bits32) ( ( a | b )<<1 ) == 0 );
  1469. return ( a == b ) || ( aSign ^ ( a < b ) );
  1470. }
  1471. /*
  1472. -------------------------------------------------------------------------------
  1473. Returns 1 if the single-precision floating-point value `a' is less than
  1474. the corresponding value `b', and 0 otherwise. Quiet NaNs do not cause an
  1475. exception. Otherwise, the comparison is performed according to the IEC/IEEE
  1476. Standard for Binary Floating-point Arithmetic.
  1477. -------------------------------------------------------------------------------
  1478. */
  1479. flag float32_lt_quiet( float32 a, float32 b )
  1480. {
  1481. flag aSign, bSign;
  1482. if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
  1483. || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
  1484. ) {
  1485. if ( float32_is_signaling_nan( a ) || float32_is_signaling_nan( b ) ) {
  1486. float_raise( float_flag_invalid );
  1487. }
  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( 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( 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_exception_flags |= 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_exception_flags |= float_flag_invalid;
  1557. return aSign ? 0x80000000 : 0x7FFFFFFF;
  1558. }
  1559. if ( ( aSig<<shiftCount ) != savedASig ) {
  1560. float_exception_flags |= 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( 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( 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_exception_flags |= 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_exception_flags |= float_flag_invalid;
  1625. return aSign ? 0x80000000 : 0x7FFFFFFF;
  1626. }
  1627. if ( ( aSig<<shiftCount ) != savedASig ) {
  1628. float_exception_flags |= 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( 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( 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( 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. float_exception_flags |= float_flag_inexact;
  1716. aSign = extractFloat64Sign( a );
  1717. switch ( float_rounding_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 = float_rounding_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 ) float_exception_flags |= 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( 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( 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( 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. float_raise( 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( float_rounding_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( 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( float64 a, float64 b )
  1899. {
  1900. flag aSign, bSign;
  1901. aSign = extractFloat64Sign( a );
  1902. bSign = extractFloat64Sign( b );
  1903. if ( aSign == bSign ) {
  1904. return addFloat64Sigs( a, b, aSign );
  1905. }
  1906. else {
  1907. return subFloat64Sigs( 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( float64 a, float64 b )
  1918. {
  1919. flag aSign, bSign;
  1920. aSign = extractFloat64Sign( a );
  1921. bSign = extractFloat64Sign( b );
  1922. if ( aSign == bSign ) {
  1923. return subFloat64Sigs( a, b, aSign );
  1924. }
  1925. else {
  1926. return addFloat64Sigs( 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( 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. float_raise( 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. float_raise( 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( 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( 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. float_raise( 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. float_raise( float_flag_invalid );
  2023. return float64_default_nan;
  2024. }
  2025. float_raise( 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( 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( 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. float_raise( 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. float_raise( 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( 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( 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. float_raise( float_flag_invalid );
  2157. return float64_default_nan;
  2158. }
  2159. if ( aSign ) {
  2160. if ( ( aExp | aSig ) == 0 ) return a;
  2161. float_raise( 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( 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. if ( float64_is_signaling_nan( a ) || float64_is_signaling_nan( b ) ) {
  2290. float_raise( float_flag_invalid );
  2291. }
  2292. return 0;
  2293. }
  2294. aSign = extractFloat64Sign( a );
  2295. bSign = extractFloat64Sign( b );
  2296. if ( aSign != bSign ) return aSign || ( (bits64) ( ( a | b )<<1 ) == 0 );
  2297. return ( a == b ) || ( aSign ^ ( a < b ) );
  2298. }
  2299. /*
  2300. -------------------------------------------------------------------------------
  2301. Returns 1 if the double-precision floating-point value `a' is less than
  2302. the corresponding value `b', and 0 otherwise. Quiet NaNs do not cause an
  2303. exception. Otherwise, the comparison is performed according to the IEC/IEEE
  2304. Standard for Binary Floating-point Arithmetic.
  2305. -------------------------------------------------------------------------------
  2306. */
  2307. flag float64_lt_quiet( float64 a, float64 b )
  2308. {
  2309. flag aSign, bSign;
  2310. if ( ( ( extractFloat64Exp( a ) == 0x7FF ) && extractFloat64Frac( a ) )
  2311. || ( ( extractFloat64Exp( b ) == 0x7FF ) && extractFloat64Frac( b ) )
  2312. ) {
  2313. if ( float64_is_signaling_nan( a ) || float64_is_signaling_nan( b ) ) {
  2314. float_raise( float_flag_invalid );
  2315. }
  2316. return 0;
  2317. }
  2318. aSign = extractFloat64Sign( a );
  2319. bSign = extractFloat64Sign( b );
  2320. if ( aSign != bSign ) return aSign && ( (bits64) ( ( a | b )<<1 ) != 0 );
  2321. return ( a != b ) && ( aSign ^ ( a < b ) );
  2322. }
  2323. #ifdef FLOATX80
  2324. /*
  2325. -------------------------------------------------------------------------------
  2326. Returns the result of converting the extended double-precision floating-
  2327. point value `a' to the 32-bit two's complement integer format. The
  2328. conversion is performed according to the IEC/IEEE Standard for Binary
  2329. Floating-point Arithmetic---which means in particular that the conversion
  2330. is rounded according to the current rounding mode. If `a' is a NaN, the
  2331. largest positive integer is returned. Otherwise, if the conversion
  2332. overflows, the largest integer with the same sign as `a' is returned.
  2333. -------------------------------------------------------------------------------
  2334. */
  2335. int32 floatx80_to_int32( floatx80 a )
  2336. {
  2337. flag aSign;
  2338. int32 aExp, shiftCount;
  2339. bits64 aSig;
  2340. aSig = extractFloatx80Frac( a );
  2341. aExp = extractFloatx80Exp( a );
  2342. aSign = extractFloatx80Sign( a );
  2343. if ( ( aExp == 0x7FFF ) && (bits64) ( aSig<<1 ) ) aSign = 0;
  2344. shiftCount = 0x4037 - aExp;
  2345. if ( shiftCount <= 0 ) shiftCount = 1;
  2346. shift64RightJamming( aSig, shiftCount, &aSig );
  2347. return roundAndPackInt32( aSign, aSig );
  2348. }
  2349. /*
  2350. -------------------------------------------------------------------------------
  2351. Returns the result of converting the extended double-precision floating-
  2352. point value `a' to the 32-bit two's complement integer format. The
  2353. conversion is performed according to the IEC/IEEE Standard for Binary
  2354. Floating-point Arithmetic, except that the conversion is always rounded
  2355. toward zero. If `a' is a NaN, the largest positive integer is returned.
  2356. Otherwise, if the conversion overflows, the largest integer with the same
  2357. sign as `a' is returned.
  2358. -------------------------------------------------------------------------------
  2359. */
  2360. int32 floatx80_to_int32_round_to_zero( floatx80 a )
  2361. {
  2362. flag aSign;
  2363. int32 aExp, shiftCount;
  2364. bits64 aSig, savedASig;
  2365. int32 z;
  2366. aSig = extractFloatx80Frac( a );
  2367. aExp = extractFloatx80Exp( a );
  2368. aSign = extractFloatx80Sign( a );
  2369. shiftCount = 0x403E - aExp;
  2370. if ( shiftCount < 32 ) {
  2371. if ( ( aExp == 0x7FFF ) && (bits64) ( aSig<<1 ) ) aSign = 0;
  2372. goto invalid;
  2373. }
  2374. else if ( 63 < shiftCount ) {
  2375. if ( aExp || aSig ) float_exception_flags |= float_flag_inexact;
  2376. return 0;
  2377. }
  2378. savedASig = aSig;
  2379. aSig >>= shiftCount;
  2380. z = aSig;
  2381. if ( aSign ) z = - z;
  2382. if ( ( z < 0 ) ^ aSign ) {
  2383. invalid:
  2384. float_exception_flags |= float_flag_invalid;
  2385. return aSign ? 0x80000000 : 0x7FFFFFFF;
  2386. }
  2387. if ( ( aSig<<shiftCount ) != savedASig ) {
  2388. float_exception_flags |= float_flag_inexact;
  2389. }
  2390. return z;
  2391. }
  2392. /*
  2393. -------------------------------------------------------------------------------
  2394. Returns the result of converting the extended double-precision floating-
  2395. point value `a' to the single-precision floating-point format. The
  2396. conversion is performed according to the IEC/IEEE Standard for Binary
  2397. Floating-point Arithmetic.
  2398. -------------------------------------------------------------------------------
  2399. */
  2400. float32 floatx80_to_float32( floatx80 a )
  2401. {
  2402. flag aSign;
  2403. int32 aExp;
  2404. bits64 aSig;
  2405. aSig = extractFloatx80Frac( a );
  2406. aExp = extractFloatx80Exp( a );
  2407. aSign = extractFloatx80Sign( a );
  2408. if ( aExp == 0x7FFF ) {
  2409. if ( (bits64) ( aSig<<1 ) ) {
  2410. return commonNaNToFloat32( floatx80ToCommonNaN( a ) );
  2411. }
  2412. return packFloat32( aSign, 0xFF, 0 );
  2413. }
  2414. shift64RightJamming( aSig, 33, &aSig );
  2415. if ( aExp || aSig ) aExp -= 0x3F81;
  2416. return roundAndPackFloat32( aSign, aExp, aSig );
  2417. }
  2418. /*
  2419. -------------------------------------------------------------------------------
  2420. Returns the result of converting the extended double-precision floating-
  2421. point value `a' to the double-precision floating-point format. The
  2422. conversion is performed according to the IEC/IEEE Standard for Binary
  2423. Floating-point Arithmetic.
  2424. -------------------------------------------------------------------------------
  2425. */
  2426. float64 floatx80_to_float64( floatx80 a )
  2427. {
  2428. flag aSign;
  2429. int32 aExp;
  2430. bits64 aSig, zSig;
  2431. aSig = extractFloatx80Frac( a );
  2432. aExp = extractFloatx80Exp( a );
  2433. aSign = extractFloatx80Sign( a );
  2434. if ( aExp == 0x7FFF ) {
  2435. if ( (bits64) ( aSig<<1 ) ) {
  2436. return commonNaNToFloat64( floatx80ToCommonNaN( a ) );
  2437. }
  2438. return packFloat64( aSign, 0x7FF, 0 );
  2439. }
  2440. shift64RightJamming( aSig, 1, &zSig );
  2441. if ( aExp || aSig ) aExp -= 0x3C01;
  2442. return roundAndPackFloat64( aSign, aExp, zSig );
  2443. }
  2444. /*
  2445. -------------------------------------------------------------------------------
  2446. Rounds the extended double-precision floating-point value `a' to an integer,
  2447. and returns the result as an extended quadruple-precision floating-point
  2448. value. The operation is performed according to the IEC/IEEE Standard for
  2449. Binary Floating-point Arithmetic.
  2450. -------------------------------------------------------------------------------
  2451. */
  2452. floatx80 floatx80_round_to_int( floatx80 a )
  2453. {
  2454. flag aSign;
  2455. int32 aExp;
  2456. bits64 lastBitMask, roundBitsMask;
  2457. int8 roundingMode;
  2458. floatx80 z;
  2459. aExp = extractFloatx80Exp( a );
  2460. if ( 0x403E <= aExp ) {
  2461. if ( ( aExp == 0x7FFF ) && (bits64) ( extractFloatx80Frac( a )<<1 ) ) {
  2462. return propagateFloatx80NaN( a, a );
  2463. }
  2464. return a;
  2465. }
  2466. if ( aExp <= 0x3FFE ) {
  2467. if ( ( aExp == 0 )
  2468. && ( (bits64) ( extractFloatx80Frac( a )<<1 ) == 0 ) ) {
  2469. return a;
  2470. }
  2471. float_exception_flags |= float_flag_inexact;
  2472. aSign = extractFloatx80Sign( a );
  2473. switch ( float_rounding_mode ) {
  2474. case float_round_nearest_even:
  2475. if ( ( aExp == 0x3FFE ) && (bits64) ( extractFloatx80Frac( a )<<1 )
  2476. ) {
  2477. return
  2478. packFloatx80( aSign, 0x3FFF, LIT64( 0x8000000000000000 ) );
  2479. }
  2480. break;
  2481. case float_round_down:
  2482. return
  2483. aSign ?
  2484. packFloatx80( 1, 0x3FFF, LIT64( 0x8000000000000000 ) )
  2485. : packFloatx80( 0, 0, 0 );
  2486. case float_round_up:
  2487. return
  2488. aSign ? packFloatx80( 1, 0, 0 )
  2489. : packFloatx80( 0, 0x3FFF, LIT64( 0x8000000000000000 ) );
  2490. }
  2491. return packFloatx80( aSign, 0, 0 );
  2492. }
  2493. lastBitMask = 1;
  2494. lastBitMask <<= 0x403E - aExp;
  2495. roundBitsMask = lastBitMask - 1;
  2496. z = a;
  2497. roundingMode = float_rounding_mode;
  2498. if ( roundingMode == float_round_nearest_even ) {
  2499. z.low += lastBitMask>>1;
  2500. if ( ( z.low & roundBitsMask ) == 0 ) z.low &= ~ lastBitMask;
  2501. }
  2502. else if ( roundingMode != float_round_to_zero ) {
  2503. if ( extractFloatx80Sign( z ) ^ ( roundingMode == float_round_up ) ) {
  2504. z.low += roundBitsMask;
  2505. }
  2506. }
  2507. z.low &= ~ roundBitsMask;
  2508. if ( z.low == 0 ) {
  2509. ++z.high;
  2510. z.low = LIT64( 0x8000000000000000 );
  2511. }
  2512. if ( z.low != a.low ) float_exception_flags |= float_flag_inexact;
  2513. return z;
  2514. }
  2515. /*
  2516. -------------------------------------------------------------------------------
  2517. Returns the result of adding the absolute values of the extended double-
  2518. precision floating-point values `a' and `b'. If `zSign' is true, the sum is
  2519. negated before being returned. `zSign' is ignored if the result is a NaN.
  2520. The addition is performed according to the IEC/IEEE Standard for Binary
  2521. Floating-point Arithmetic.
  2522. -------------------------------------------------------------------------------
  2523. */
  2524. static floatx80 addFloatx80Sigs( floatx80 a, floatx80 b, flag zSign )
  2525. {
  2526. int32 aExp, bExp, zExp;
  2527. bits64 aSig, bSig, zSig0, zSig1;
  2528. int32 expDiff;
  2529. aSig = extractFloatx80Frac( a );
  2530. aExp = extractFloatx80Exp( a );
  2531. bSig = extractFloatx80Frac( b );
  2532. bExp = extractFloatx80Exp( b );
  2533. expDiff = aExp - bExp;
  2534. if ( 0 < expDiff ) {
  2535. if ( aExp == 0x7FFF ) {
  2536. if ( (bits64) ( aSig<<1 ) ) return propagateFloatx80NaN( a, b );
  2537. return a;
  2538. }
  2539. if ( bExp == 0 ) --expDiff;
  2540. shift64ExtraRightJamming( bSig, 0, expDiff, &bSig, &zSig1 );
  2541. zExp = aExp;
  2542. }
  2543. else if ( expDiff < 0 ) {
  2544. if ( bExp == 0x7FFF ) {
  2545. if ( (bits64) ( bSig<<1 ) ) return propagateFloatx80NaN( a, b );
  2546. return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
  2547. }
  2548. if ( aExp == 0 ) ++expDiff;
  2549. shift64ExtraRightJamming( aSig, 0, - expDiff, &aSig, &zSig1 );
  2550. zExp = bExp;
  2551. }
  2552. else {
  2553. if ( aExp == 0x7FFF ) {
  2554. if ( (bits64) ( ( aSig | bSig )<<1 ) ) {
  2555. return propagateFloatx80NaN( a, b );
  2556. }
  2557. return a;
  2558. }
  2559. zSig1 = 0;
  2560. zSig0 = aSig + bSig;
  2561. if ( aExp == 0 ) {
  2562. normalizeFloatx80Subnormal( zSig0, &zExp, &zSig0 );
  2563. goto roundAndPack;
  2564. }
  2565. zExp = aExp;
  2566. goto shiftRight1;
  2567. }
  2568. zSig0 = aSig + bSig;
  2569. if ( (sbits64) zSig0 < 0 ) goto roundAndPack;
  2570. shiftRight1:
  2571. shift64ExtraRightJamming( zSig0, zSig1, 1, &zSig0, &zSig1 );
  2572. zSig0 |= LIT64( 0x8000000000000000 );
  2573. ++zExp;
  2574. roundAndPack:
  2575. return
  2576. roundAndPackFloatx80(
  2577. floatx80_rounding_precision, zSign, zExp, zSig0, zSig1 );
  2578. }
  2579. /*
  2580. -------------------------------------------------------------------------------
  2581. Returns the result of subtracting the absolute values of the extended
  2582. double-precision floating-point values `a' and `b'. If `zSign' is true,
  2583. the difference is negated before being returned. `zSign' is ignored if the
  2584. result is a NaN. The subtraction is performed according to the IEC/IEEE
  2585. Standard for Binary Floating-point Arithmetic.
  2586. -------------------------------------------------------------------------------
  2587. */
  2588. static floatx80 subFloatx80Sigs( floatx80 a, floatx80 b, flag zSign )
  2589. {
  2590. int32 aExp, bExp, zExp;
  2591. bits64 aSig, bSig, zSig0, zSig1;
  2592. int32 expDiff;
  2593. floatx80 z;
  2594. aSig = extractFloatx80Frac( a );
  2595. aExp = extractFloatx80Exp( a );
  2596. bSig = extractFloatx80Frac( b );
  2597. bExp = extractFloatx80Exp( b );
  2598. expDiff = aExp - bExp;
  2599. if ( 0 < expDiff ) goto aExpBigger;
  2600. if ( expDiff < 0 ) goto bExpBigger;
  2601. if ( aExp == 0x7FFF ) {
  2602. if ( (bits64) ( ( aSig | bSig )<<1 ) ) {
  2603. return propagateFloatx80NaN( a, b );
  2604. }
  2605. float_raise( float_flag_invalid );
  2606. z.low = floatx80_default_nan_low;
  2607. z.high = floatx80_default_nan_high;
  2608. return z;
  2609. }
  2610. if ( aExp == 0 ) {
  2611. aExp = 1;
  2612. bExp = 1;
  2613. }
  2614. zSig1 = 0;
  2615. if ( bSig < aSig ) goto aBigger;
  2616. if ( aSig < bSig ) goto bBigger;
  2617. return packFloatx80( float_rounding_mode == float_round_down, 0, 0 );
  2618. bExpBigger:
  2619. if ( bExp == 0x7FFF ) {
  2620. if ( (bits64) ( bSig<<1 ) ) return propagateFloatx80NaN( a, b );
  2621. return packFloatx80( zSign ^ 1, 0x7FFF, LIT64( 0x8000000000000000 ) );
  2622. }
  2623. if ( aExp == 0 ) ++expDiff;
  2624. shift128RightJamming( aSig, 0, - expDiff, &aSig, &zSig1 );
  2625. bBigger:
  2626. sub128( bSig, 0, aSig, zSig1, &zSig0, &zSig1 );
  2627. zExp = bExp;
  2628. zSign ^= 1;
  2629. goto normalizeRoundAndPack;
  2630. aExpBigger:
  2631. if ( aExp == 0x7FFF ) {
  2632. if ( (bits64) ( aSig<<1 ) ) return propagateFloatx80NaN( a, b );
  2633. return a;
  2634. }
  2635. if ( bExp == 0 ) --expDiff;
  2636. shift128RightJamming( bSig, 0, expDiff, &bSig, &zSig1 );
  2637. aBigger:
  2638. sub128( aSig, 0, bSig, zSig1, &zSig0, &zSig1 );
  2639. zExp = aExp;
  2640. normalizeRoundAndPack:
  2641. return
  2642. normalizeRoundAndPackFloatx80(
  2643. floatx80_rounding_precision, zSign, zExp, zSig0, zSig1 );
  2644. }
  2645. /*
  2646. -------------------------------------------------------------------------------
  2647. Returns the result of adding the extended double-precision floating-point
  2648. values `a' and `b'. The operation is performed according to the IEC/IEEE
  2649. Standard for Binary Floating-point Arithmetic.
  2650. -------------------------------------------------------------------------------
  2651. */
  2652. floatx80 floatx80_add( floatx80 a, floatx80 b )
  2653. {
  2654. flag aSign, bSign;
  2655. aSign = extractFloatx80Sign( a );
  2656. bSign = extractFloatx80Sign( b );
  2657. if ( aSign == bSign ) {
  2658. return addFloatx80Sigs( a, b, aSign );
  2659. }
  2660. else {
  2661. return subFloatx80Sigs( a, b, aSign );
  2662. }
  2663. }
  2664. /*
  2665. -------------------------------------------------------------------------------
  2666. Returns the result of subtracting the extended double-precision floating-
  2667. point values `a' and `b'. The operation is performed according to the
  2668. IEC/IEEE Standard for Binary Floating-point Arithmetic.
  2669. -------------------------------------------------------------------------------
  2670. */
  2671. floatx80 floatx80_sub( floatx80 a, floatx80 b )
  2672. {
  2673. flag aSign, bSign;
  2674. aSign = extractFloatx80Sign( a );
  2675. bSign = extractFloatx80Sign( b );
  2676. if ( aSign == bSign ) {
  2677. return subFloatx80Sigs( a, b, aSign );
  2678. }
  2679. else {
  2680. return addFloatx80Sigs( a, b, aSign );
  2681. }
  2682. }
  2683. /*
  2684. -------------------------------------------------------------------------------
  2685. Returns the result of multiplying the extended double-precision floating-
  2686. point values `a' and `b'. The operation is performed according to the
  2687. IEC/IEEE Standard for Binary Floating-point Arithmetic.
  2688. -------------------------------------------------------------------------------
  2689. */
  2690. floatx80 floatx80_mul( floatx80 a, floatx80 b )
  2691. {
  2692. flag aSign, bSign, zSign;
  2693. int32 aExp, bExp, zExp;
  2694. bits64 aSig, bSig, zSig0, zSig1;
  2695. floatx80 z;
  2696. aSig = extractFloatx80Frac( a );
  2697. aExp = extractFloatx80Exp( a );
  2698. aSign = extractFloatx80Sign( a );
  2699. bSig = extractFloatx80Frac( b );
  2700. bExp = extractFloatx80Exp( b );
  2701. bSign = extractFloatx80Sign( b );
  2702. zSign = aSign ^ bSign;
  2703. if ( aExp == 0x7FFF ) {
  2704. if ( (bits64) ( aSig<<1 )
  2705. || ( ( bExp == 0x7FFF ) && (bits64) ( bSig<<1 ) ) ) {
  2706. return propagateFloatx80NaN( a, b );
  2707. }
  2708. if ( ( bExp | bSig ) == 0 ) goto invalid;
  2709. return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
  2710. }
  2711. if ( bExp == 0x7FFF ) {
  2712. if ( (bits64) ( bSig<<1 ) ) return propagateFloatx80NaN( a, b );
  2713. if ( ( aExp | aSig ) == 0 ) {
  2714. invalid:
  2715. float_raise( float_flag_invalid );
  2716. z.low = floatx80_default_nan_low;
  2717. z.high = floatx80_default_nan_high;
  2718. return z;
  2719. }
  2720. return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
  2721. }
  2722. if ( aExp == 0 ) {
  2723. if ( aSig == 0 ) return packFloatx80( zSign, 0, 0 );
  2724. normalizeFloatx80Subnormal( aSig, &aExp, &aSig );
  2725. }
  2726. if ( bExp == 0 ) {
  2727. if ( bSig == 0 ) return packFloatx80( zSign, 0, 0 );
  2728. normalizeFloatx80Subnormal( bSig, &bExp, &bSig );
  2729. }
  2730. zExp = aExp + bExp - 0x3FFE;
  2731. mul64To128( aSig, bSig, &zSig0, &zSig1 );
  2732. if ( 0 < (sbits64) zSig0 ) {
  2733. shortShift128Left( zSig0, zSig1, 1, &zSig0, &zSig1 );
  2734. --zExp;
  2735. }
  2736. return
  2737. roundAndPackFloatx80(
  2738. floatx80_rounding_precision, zSign, zExp, zSig0, zSig1 );
  2739. }
  2740. /*
  2741. -------------------------------------------------------------------------------
  2742. Returns the result of dividing the extended double-precision floating-point
  2743. value `a' by the corresponding value `b'. The operation is performed
  2744. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  2745. -------------------------------------------------------------------------------
  2746. */
  2747. floatx80 floatx80_div( floatx80 a, floatx80 b )
  2748. {
  2749. flag aSign, bSign, zSign;
  2750. int32 aExp, bExp, zExp;
  2751. bits64 aSig, bSig, zSig0, zSig1;
  2752. bits64 rem0, rem1, rem2, term0, term1, term2;
  2753. floatx80 z;
  2754. aSig = extractFloatx80Frac( a );
  2755. aExp = extractFloatx80Exp( a );
  2756. aSign = extractFloatx80Sign( a );
  2757. bSig = extractFloatx80Frac( b );
  2758. bExp = extractFloatx80Exp( b );
  2759. bSign = extractFloatx80Sign( b );
  2760. zSign = aSign ^ bSign;
  2761. if ( aExp == 0x7FFF ) {
  2762. if ( (bits64) ( aSig<<1 ) ) return propagateFloatx80NaN( a, b );
  2763. if ( bExp == 0x7FFF ) {
  2764. if ( (bits64) ( bSig<<1 ) ) return propagateFloatx80NaN( a, b );
  2765. goto invalid;
  2766. }
  2767. return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
  2768. }
  2769. if ( bExp == 0x7FFF ) {
  2770. if ( (bits64) ( bSig<<1 ) ) return propagateFloatx80NaN( a, b );
  2771. return packFloatx80( zSign, 0, 0 );
  2772. }
  2773. if ( bExp == 0 ) {
  2774. if ( bSig == 0 ) {
  2775. if ( ( aExp | aSig ) == 0 ) {
  2776. invalid:
  2777. float_raise( float_flag_invalid );
  2778. z.low = floatx80_default_nan_low;
  2779. z.high = floatx80_default_nan_high;
  2780. return z;
  2781. }
  2782. float_raise( float_flag_divbyzero );
  2783. return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
  2784. }
  2785. normalizeFloatx80Subnormal( bSig, &bExp, &bSig );
  2786. }
  2787. if ( aExp == 0 ) {
  2788. if ( aSig == 0 ) return packFloatx80( zSign, 0, 0 );
  2789. normalizeFloatx80Subnormal( aSig, &aExp, &aSig );
  2790. }
  2791. zExp = aExp - bExp + 0x3FFE;
  2792. rem1 = 0;
  2793. if ( bSig <= aSig ) {
  2794. shift128Right( aSig, 0, 1, &aSig, &rem1 );
  2795. ++zExp;
  2796. }
  2797. zSig0 = estimateDiv128To64( aSig, rem1, bSig );
  2798. mul64To128( bSig, zSig0, &term0, &term1 );
  2799. sub128( aSig, rem1, term0, term1, &rem0, &rem1 );
  2800. while ( (sbits64) rem0 < 0 ) {
  2801. --zSig0;
  2802. add128( rem0, rem1, 0, bSig, &rem0, &rem1 );
  2803. }
  2804. zSig1 = estimateDiv128To64( rem1, 0, bSig );
  2805. if ( (bits64) ( zSig1<<1 ) <= 8 ) {
  2806. mul64To128( bSig, zSig1, &term1, &term2 );
  2807. sub128( rem1, 0, term1, term2, &rem1, &rem2 );
  2808. while ( (sbits64) rem1 < 0 ) {
  2809. --zSig1;
  2810. add128( rem1, rem2, 0, bSig, &rem1, &rem2 );
  2811. }
  2812. zSig1 |= ( ( rem1 | rem2 ) != 0 );
  2813. }
  2814. return
  2815. roundAndPackFloatx80(
  2816. floatx80_rounding_precision, zSign, zExp, zSig0, zSig1 );
  2817. }
  2818. /*
  2819. -------------------------------------------------------------------------------
  2820. Returns the remainder of the extended double-precision floating-point value
  2821. `a' with respect to the corresponding value `b'. The operation is performed
  2822. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  2823. -------------------------------------------------------------------------------
  2824. */
  2825. floatx80 floatx80_rem( floatx80 a, floatx80 b )
  2826. {
  2827. flag aSign, bSign, zSign;
  2828. int32 aExp, bExp, expDiff;
  2829. bits64 aSig0, aSig1, bSig;
  2830. bits64 q, term0, term1, alternateASig0, alternateASig1;
  2831. floatx80 z;
  2832. aSig0 = extractFloatx80Frac( a );
  2833. aExp = extractFloatx80Exp( a );
  2834. aSign = extractFloatx80Sign( a );
  2835. bSig = extractFloatx80Frac( b );
  2836. bExp = extractFloatx80Exp( b );
  2837. bSign = extractFloatx80Sign( b );
  2838. if ( aExp == 0x7FFF ) {
  2839. if ( (bits64) ( aSig0<<1 )
  2840. || ( ( bExp == 0x7FFF ) && (bits64) ( bSig<<1 ) ) ) {
  2841. return propagateFloatx80NaN( a, b );
  2842. }
  2843. goto invalid;
  2844. }
  2845. if ( bExp == 0x7FFF ) {
  2846. if ( (bits64) ( bSig<<1 ) ) return propagateFloatx80NaN( a, b );
  2847. return a;
  2848. }
  2849. if ( bExp == 0 ) {
  2850. if ( bSig == 0 ) {
  2851. invalid:
  2852. float_raise( float_flag_invalid );
  2853. z.low = floatx80_default_nan_low;
  2854. z.high = floatx80_default_nan_high;
  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. 80, 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( 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. float_raise( float_flag_invalid );
  2940. z.low = floatx80_default_nan_low;
  2941. z.high = floatx80_default_nan_high;
  2942. return z;
  2943. }
  2944. if ( aExp == 0 ) {
  2945. if ( aSig0 == 0 ) return packFloatx80( 0, 0, 0 );
  2946. normalizeFloatx80Subnormal( aSig0, &aExp, &aSig0 );
  2947. }
  2948. zExp = ( ( aExp - 0x3FFF )>>1 ) + 0x3FFF;
  2949. zSig0 = estimateSqrt32( aExp, aSig0>>32 );
  2950. zSig0 <<= 31;
  2951. aSig1 = 0;
  2952. shift128Right( aSig0, 0, ( aExp & 1 ) + 2, &aSig0, &aSig1 );
  2953. zSig0 = estimateDiv128To64( aSig0, aSig1, zSig0 ) + zSig0 + 4;
  2954. if ( 0 <= (sbits64) zSig0 ) zSig0 = LIT64( 0xFFFFFFFFFFFFFFFF );
  2955. shortShift128Left( aSig0, aSig1, 2, &aSig0, &aSig1 );
  2956. mul64To128( zSig0, zSig0, &term0, &term1 );
  2957. sub128( aSig0, aSig1, term0, term1, &rem0, &rem1 );
  2958. while ( (sbits64) rem0 < 0 ) {
  2959. --zSig0;
  2960. shortShift128Left( 0, zSig0, 1, &term0, &term1 );
  2961. term1 |= 1;
  2962. add128( rem0, rem1, term0, term1, &rem0, &rem1 );
  2963. }
  2964. shortShift128Left( rem0, rem1, 63, &shiftedRem0, &shiftedRem1 );
  2965. zSig1 = estimateDiv128To64( shiftedRem0, shiftedRem1, zSig0 );
  2966. if ( (bits64) ( zSig1<<1 ) <= 10 ) {
  2967. if ( zSig1 == 0 ) zSig1 = 1;
  2968. mul64To128( zSig0, zSig1, &term1, &term2 );
  2969. shortShift128Left( term1, term2, 1, &term1, &term2 );
  2970. sub128( rem1, 0, term1, term2, &rem1, &rem2 );
  2971. mul64To128( zSig1, zSig1, &term2, &term3 );
  2972. sub192( rem1, rem2, 0, 0, term2, term3, &rem1, &rem2, &rem3 );
  2973. while ( (sbits64) rem1 < 0 ) {
  2974. --zSig1;
  2975. shortShift192Left( 0, zSig0, zSig1, 1, &term1, &term2, &term3 );
  2976. term3 |= 1;
  2977. add192(
  2978. rem1, rem2, rem3, term1, term2, term3, &rem1, &rem2, &rem3 );
  2979. }
  2980. zSig1 |= ( ( rem1 | rem2 | rem3 ) != 0 );
  2981. }
  2982. return
  2983. roundAndPackFloatx80(
  2984. floatx80_rounding_precision, 0, zExp, zSig0, zSig1 );
  2985. }
  2986. /*
  2987. -------------------------------------------------------------------------------
  2988. Returns 1 if the extended double-precision floating-point value `a' is
  2989. equal to the corresponding value `b', and 0 otherwise. The comparison is
  2990. performed according to the IEC/IEEE Standard for Binary Floating-point
  2991. Arithmetic.
  2992. -------------------------------------------------------------------------------
  2993. */
  2994. flag floatx80_eq( floatx80 a, floatx80 b )
  2995. {
  2996. if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
  2997. && (bits64) ( extractFloatx80Frac( a )<<1 ) )
  2998. || ( ( extractFloatx80Exp( b ) == 0x7FFF )
  2999. && (bits64) ( extractFloatx80Frac( b )<<1 ) )
  3000. ) {
  3001. if ( floatx80_is_signaling_nan( a )
  3002. || floatx80_is_signaling_nan( b ) ) {
  3003. float_raise( float_flag_invalid );
  3004. }
  3005. return 0;
  3006. }
  3007. return
  3008. ( a.low == b.low )
  3009. && ( ( a.high == b.high )
  3010. || ( ( a.low == 0 )
  3011. && ( (bits16) ( ( a.high | b.high )<<1 ) == 0 ) )
  3012. );
  3013. }
  3014. /*
  3015. -------------------------------------------------------------------------------
  3016. Returns 1 if the extended double-precision floating-point value `a' is
  3017. less than or equal to the corresponding value `b', and 0 otherwise. The
  3018. comparison is performed according to the IEC/IEEE Standard for Binary
  3019. Floating-point Arithmetic.
  3020. -------------------------------------------------------------------------------
  3021. */
  3022. flag floatx80_le( floatx80 a, floatx80 b )
  3023. {
  3024. flag aSign, bSign;
  3025. if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
  3026. && (bits64) ( extractFloatx80Frac( a )<<1 ) )
  3027. || ( ( extractFloatx80Exp( b ) == 0x7FFF )
  3028. && (bits64) ( extractFloatx80Frac( b )<<1 ) )
  3029. ) {
  3030. float_raise( float_flag_invalid );
  3031. return 0;
  3032. }
  3033. aSign = extractFloatx80Sign( a );
  3034. bSign = extractFloatx80Sign( b );
  3035. if ( aSign != bSign ) {
  3036. return
  3037. aSign
  3038. || ( ( ( (bits16) ( ( a.high | b.high )<<1 ) ) | a.low | b.low )
  3039. == 0 );
  3040. }
  3041. return
  3042. aSign ? le128( b.high, b.low, a.high, a.low )
  3043. : le128( a.high, a.low, b.high, b.low );
  3044. }
  3045. /*
  3046. -------------------------------------------------------------------------------
  3047. Returns 1 if the extended double-precision floating-point value `a' is
  3048. less than the corresponding value `b', and 0 otherwise. The comparison
  3049. is performed according to the IEC/IEEE Standard for Binary Floating-point
  3050. Arithmetic.
  3051. -------------------------------------------------------------------------------
  3052. */
  3053. flag floatx80_lt( floatx80 a, floatx80 b )
  3054. {
  3055. flag aSign, bSign;
  3056. if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
  3057. && (bits64) ( extractFloatx80Frac( a )<<1 ) )
  3058. || ( ( extractFloatx80Exp( b ) == 0x7FFF )
  3059. && (bits64) ( extractFloatx80Frac( b )<<1 ) )
  3060. ) {
  3061. float_raise( float_flag_invalid );
  3062. return 0;
  3063. }
  3064. aSign = extractFloatx80Sign( a );
  3065. bSign = extractFloatx80Sign( b );
  3066. if ( aSign != bSign ) {
  3067. return
  3068. aSign
  3069. && ( ( ( (bits16) ( ( a.high | b.high )<<1 ) ) | a.low | b.low )
  3070. != 0 );
  3071. }
  3072. return
  3073. aSign ? lt128( b.high, b.low, a.high, a.low )
  3074. : lt128( a.high, a.low, b.high, b.low );
  3075. }
  3076. /*
  3077. -------------------------------------------------------------------------------
  3078. Returns 1 if the extended double-precision floating-point value `a' is equal
  3079. to the corresponding value `b', and 0 otherwise. The invalid exception is
  3080. raised if either operand is a NaN. Otherwise, the comparison is performed
  3081. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  3082. -------------------------------------------------------------------------------
  3083. */
  3084. flag floatx80_eq_signaling( floatx80 a, floatx80 b )
  3085. {
  3086. if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
  3087. && (bits64) ( extractFloatx80Frac( a )<<1 ) )
  3088. || ( ( extractFloatx80Exp( b ) == 0x7FFF )
  3089. && (bits64) ( extractFloatx80Frac( b )<<1 ) )
  3090. ) {
  3091. float_raise( float_flag_invalid );
  3092. return 0;
  3093. }
  3094. return
  3095. ( a.low == b.low )
  3096. && ( ( a.high == b.high )
  3097. || ( ( a.low == 0 )
  3098. && ( (bits16) ( ( a.high | b.high )<<1 ) == 0 ) )
  3099. );
  3100. }
  3101. /*
  3102. -------------------------------------------------------------------------------
  3103. Returns 1 if the extended double-precision floating-point value `a' is less
  3104. than or equal to the corresponding value `b', and 0 otherwise. Quiet NaNs
  3105. do not cause an exception. Otherwise, the comparison is performed according
  3106. to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  3107. -------------------------------------------------------------------------------
  3108. */
  3109. flag floatx80_le_quiet( floatx80 a, floatx80 b )
  3110. {
  3111. flag aSign, bSign;
  3112. if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
  3113. && (bits64) ( extractFloatx80Frac( a )<<1 ) )
  3114. || ( ( extractFloatx80Exp( b ) == 0x7FFF )
  3115. && (bits64) ( extractFloatx80Frac( b )<<1 ) )
  3116. ) {
  3117. if ( floatx80_is_signaling_nan( a )
  3118. || floatx80_is_signaling_nan( b ) ) {
  3119. float_raise( float_flag_invalid );
  3120. }
  3121. return 0;
  3122. }
  3123. aSign = extractFloatx80Sign( a );
  3124. bSign = extractFloatx80Sign( b );
  3125. if ( aSign != bSign ) {
  3126. return
  3127. aSign
  3128. || ( ( ( (bits16) ( ( a.high | b.high )<<1 ) ) | a.low | b.low )
  3129. == 0 );
  3130. }
  3131. return
  3132. aSign ? le128( b.high, b.low, a.high, a.low )
  3133. : le128( a.high, a.low, b.high, b.low );
  3134. }
  3135. /*
  3136. -------------------------------------------------------------------------------
  3137. Returns 1 if the extended double-precision floating-point value `a' is less
  3138. than the corresponding value `b', and 0 otherwise. Quiet NaNs do not cause
  3139. an exception. Otherwise, the comparison is performed according to the
  3140. IEC/IEEE Standard for Binary Floating-point Arithmetic.
  3141. -------------------------------------------------------------------------------
  3142. */
  3143. flag floatx80_lt_quiet( floatx80 a, floatx80 b )
  3144. {
  3145. flag aSign, bSign;
  3146. if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
  3147. && (bits64) ( extractFloatx80Frac( a )<<1 ) )
  3148. || ( ( extractFloatx80Exp( b ) == 0x7FFF )
  3149. && (bits64) ( extractFloatx80Frac( b )<<1 ) )
  3150. ) {
  3151. if ( floatx80_is_signaling_nan( a )
  3152. || floatx80_is_signaling_nan( b ) ) {
  3153. float_raise( float_flag_invalid );
  3154. }
  3155. return 0;
  3156. }
  3157. aSign = extractFloatx80Sign( a );
  3158. bSign = extractFloatx80Sign( b );
  3159. if ( aSign != bSign ) {
  3160. return
  3161. aSign
  3162. && ( ( ( (bits16) ( ( a.high | b.high )<<1 ) ) | a.low | b.low )
  3163. != 0 );
  3164. }
  3165. return
  3166. aSign ? lt128( b.high, b.low, a.high, a.low )
  3167. : lt128( a.high, a.low, b.high, b.low );
  3168. }
  3169. #endif