PageRenderTime 64ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/arch/arm/nwfpe/softfloat.c

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