softfloat.c 116 KB

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