softfloat.c 116 KB

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