softfloat.c 115 KB

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