PageRenderTime 1361ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/arm/nwfpe/softfloat.c

https://github.com/ab3416/linux-2.6
C | 1426 lines | 922 code | 118 blank | 386 comment | 350 complexity | 7565592ccc1ed64672923ca7b357ff5f MD5 | raw file
  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
  13. http://www.jhauser.us/arithmetic/SoftFloat-2b/SoftFloat-source.txt
  14. THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
  15. has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
  16. TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
  17. PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
  18. AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
  19. Derivative works are acceptable, even for commercial purposes, so long as
  20. (1) they include prominent notice that the work is derivative, and (2) they
  21. include prominent notice akin to these three paragraphs for those parts of
  22. this code that are retained.
  23. ===============================================================================
  24. */
  25. #include <asm/div64.h>
  26. #include "fpa11.h"
  27. //#include "milieu.h"
  28. //#include "softfloat.h"
  29. /*
  30. -------------------------------------------------------------------------------
  31. Primitive arithmetic functions, including multi-word arithmetic, and
  32. division and square root approximations. (Can be specialized to target if
  33. desired.)
  34. -------------------------------------------------------------------------------
  35. */
  36. #include "softfloat-macros"
  37. /*
  38. -------------------------------------------------------------------------------
  39. Functions and definitions to determine: (1) whether tininess for underflow
  40. is detected before or after rounding by default, (2) what (if anything)
  41. happens when exceptions are raised, (3) how signaling NaNs are distinguished
  42. from quiet NaNs, (4) the default generated quiet NaNs, and (5) how NaNs
  43. are propagated from function inputs to output. These details are target-
  44. specific.
  45. -------------------------------------------------------------------------------
  46. */
  47. #include "softfloat-specialize"
  48. /*
  49. -------------------------------------------------------------------------------
  50. Takes a 64-bit fixed-point value `absZ' with binary point between bits 6
  51. and 7, and returns the properly rounded 32-bit integer corresponding to the
  52. input. If `zSign' is nonzero, the input is negated before being converted
  53. to an integer. Bit 63 of `absZ' must be zero. Ordinarily, the fixed-point
  54. input is simply rounded to an integer, with the inexact exception raised if
  55. the input cannot be represented exactly as an integer. If the fixed-point
  56. input is too large, however, the invalid exception is raised and the largest
  57. positive or negative integer is returned.
  58. -------------------------------------------------------------------------------
  59. */
  60. static int32 roundAndPackInt32( struct roundingData *roundData, flag zSign, bits64 absZ )
  61. {
  62. int8 roundingMode;
  63. flag roundNearestEven;
  64. int8 roundIncrement, roundBits;
  65. int32 z;
  66. roundingMode = roundData->mode;
  67. roundNearestEven = ( roundingMode == float_round_nearest_even );
  68. roundIncrement = 0x40;
  69. if ( ! roundNearestEven ) {
  70. if ( roundingMode == float_round_to_zero ) {
  71. roundIncrement = 0;
  72. }
  73. else {
  74. roundIncrement = 0x7F;
  75. if ( zSign ) {
  76. if ( roundingMode == float_round_up ) roundIncrement = 0;
  77. }
  78. else {
  79. if ( roundingMode == float_round_down ) roundIncrement = 0;
  80. }
  81. }
  82. }
  83. roundBits = absZ & 0x7F;
  84. absZ = ( absZ + roundIncrement )>>7;
  85. absZ &= ~ ( ( ( roundBits ^ 0x40 ) == 0 ) & roundNearestEven );
  86. z = absZ;
  87. if ( zSign ) z = - z;
  88. if ( ( absZ>>32 ) || ( z && ( ( z < 0 ) ^ zSign ) ) ) {
  89. roundData->exception |= float_flag_invalid;
  90. return zSign ? 0x80000000 : 0x7FFFFFFF;
  91. }
  92. if ( roundBits ) roundData->exception |= float_flag_inexact;
  93. return z;
  94. }
  95. /*
  96. -------------------------------------------------------------------------------
  97. Returns the fraction bits of the single-precision floating-point value `a'.
  98. -------------------------------------------------------------------------------
  99. */
  100. INLINE bits32 extractFloat32Frac( float32 a )
  101. {
  102. return a & 0x007FFFFF;
  103. }
  104. /*
  105. -------------------------------------------------------------------------------
  106. Returns the exponent bits of the single-precision floating-point value `a'.
  107. -------------------------------------------------------------------------------
  108. */
  109. INLINE int16 extractFloat32Exp( float32 a )
  110. {
  111. return ( a>>23 ) & 0xFF;
  112. }
  113. /*
  114. -------------------------------------------------------------------------------
  115. Returns the sign bit of the single-precision floating-point value `a'.
  116. -------------------------------------------------------------------------------
  117. */
  118. #if 0 /* in softfloat.h */
  119. INLINE flag extractFloat32Sign( float32 a )
  120. {
  121. return a>>31;
  122. }
  123. #endif
  124. /*
  125. -------------------------------------------------------------------------------
  126. Normalizes the subnormal single-precision floating-point value represented
  127. by the denormalized significand `aSig'. The normalized exponent and
  128. significand are stored at the locations pointed to by `zExpPtr' and
  129. `zSigPtr', respectively.
  130. -------------------------------------------------------------------------------
  131. */
  132. static void
  133. normalizeFloat32Subnormal( bits32 aSig, int16 *zExpPtr, bits32 *zSigPtr )
  134. {
  135. int8 shiftCount;
  136. shiftCount = countLeadingZeros32( aSig ) - 8;
  137. *zSigPtr = aSig<<shiftCount;
  138. *zExpPtr = 1 - shiftCount;
  139. }
  140. /*
  141. -------------------------------------------------------------------------------
  142. Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
  143. single-precision floating-point value, returning the result. After being
  144. shifted into the proper positions, the three fields are simply added
  145. together to form the result. This means that any integer portion of `zSig'
  146. will be added into the exponent. Since a properly normalized significand
  147. will have an integer portion equal to 1, the `zExp' input should be 1 less
  148. than the desired result exponent whenever `zSig' is a complete, normalized
  149. significand.
  150. -------------------------------------------------------------------------------
  151. */
  152. INLINE float32 packFloat32( flag zSign, int16 zExp, bits32 zSig )
  153. {
  154. #if 0
  155. float32 f;
  156. __asm__("@ packFloat32 \n\
  157. mov %0, %1, asl #31 \n\
  158. orr %0, %2, asl #23 \n\
  159. orr %0, %3"
  160. : /* no outputs */
  161. : "g" (f), "g" (zSign), "g" (zExp), "g" (zSig)
  162. : "cc");
  163. return f;
  164. #else
  165. return ( ( (bits32) zSign )<<31 ) + ( ( (bits32) zExp )<<23 ) + zSig;
  166. #endif
  167. }
  168. /*
  169. -------------------------------------------------------------------------------
  170. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  171. and significand `zSig', and returns the proper single-precision floating-
  172. point value corresponding to the abstract input. Ordinarily, the abstract
  173. value is simply rounded and packed into the single-precision format, with
  174. the inexact exception raised if the abstract input cannot be represented
  175. exactly. If the abstract value is too large, however, the overflow and
  176. inexact exceptions are raised and an infinity or maximal finite value is
  177. returned. If the abstract value is too small, the input value is rounded to
  178. a subnormal number, and the underflow and inexact exceptions are raised if
  179. the abstract input cannot be represented exactly as a subnormal single-
  180. precision floating-point number.
  181. The input significand `zSig' has its binary point between bits 30
  182. and 29, which is 7 bits to the left of the usual location. This shifted
  183. significand must be normalized or smaller. If `zSig' is not normalized,
  184. `zExp' must be 0; in that case, the result returned is a subnormal number,
  185. and it must not require rounding. In the usual case that `zSig' is
  186. normalized, `zExp' must be 1 less than the ``true'' floating-point exponent.
  187. The handling of underflow and overflow follows the IEC/IEEE Standard for
  188. Binary Floating-point Arithmetic.
  189. -------------------------------------------------------------------------------
  190. */
  191. static float32 roundAndPackFloat32( struct roundingData *roundData, flag zSign, int16 zExp, bits32 zSig )
  192. {
  193. int8 roundingMode;
  194. flag roundNearestEven;
  195. int8 roundIncrement, roundBits;
  196. flag isTiny;
  197. roundingMode = roundData->mode;
  198. roundNearestEven = ( roundingMode == float_round_nearest_even );
  199. roundIncrement = 0x40;
  200. if ( ! roundNearestEven ) {
  201. if ( roundingMode == float_round_to_zero ) {
  202. roundIncrement = 0;
  203. }
  204. else {
  205. roundIncrement = 0x7F;
  206. if ( zSign ) {
  207. if ( roundingMode == float_round_up ) roundIncrement = 0;
  208. }
  209. else {
  210. if ( roundingMode == float_round_down ) roundIncrement = 0;
  211. }
  212. }
  213. }
  214. roundBits = zSig & 0x7F;
  215. if ( 0xFD <= (bits16) zExp ) {
  216. if ( ( 0xFD < zExp )
  217. || ( ( zExp == 0xFD )
  218. && ( (sbits32) ( zSig + roundIncrement ) < 0 ) )
  219. ) {
  220. roundData->exception |= float_flag_overflow | float_flag_inexact;
  221. return packFloat32( zSign, 0xFF, 0 ) - ( roundIncrement == 0 );
  222. }
  223. if ( zExp < 0 ) {
  224. isTiny =
  225. ( float_detect_tininess == float_tininess_before_rounding )
  226. || ( zExp < -1 )
  227. || ( zSig + roundIncrement < 0x80000000 );
  228. shift32RightJamming( zSig, - zExp, &zSig );
  229. zExp = 0;
  230. roundBits = zSig & 0x7F;
  231. if ( isTiny && roundBits ) roundData->exception |= float_flag_underflow;
  232. }
  233. }
  234. if ( roundBits ) roundData->exception |= float_flag_inexact;
  235. zSig = ( zSig + roundIncrement )>>7;
  236. zSig &= ~ ( ( ( roundBits ^ 0x40 ) == 0 ) & roundNearestEven );
  237. if ( zSig == 0 ) zExp = 0;
  238. return packFloat32( zSign, zExp, zSig );
  239. }
  240. /*
  241. -------------------------------------------------------------------------------
  242. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  243. and significand `zSig', and returns the proper single-precision floating-
  244. point value corresponding to the abstract input. This routine is just like
  245. `roundAndPackFloat32' except that `zSig' does not have to be normalized in
  246. any way. In all cases, `zExp' must be 1 less than the ``true'' floating-
  247. point exponent.
  248. -------------------------------------------------------------------------------
  249. */
  250. static float32
  251. normalizeRoundAndPackFloat32( struct roundingData *roundData, flag zSign, int16 zExp, bits32 zSig )
  252. {
  253. int8 shiftCount;
  254. shiftCount = countLeadingZeros32( zSig ) - 1;
  255. return roundAndPackFloat32( roundData, zSign, zExp - shiftCount, zSig<<shiftCount );
  256. }
  257. /*
  258. -------------------------------------------------------------------------------
  259. Returns the fraction bits of the double-precision floating-point value `a'.
  260. -------------------------------------------------------------------------------
  261. */
  262. INLINE bits64 extractFloat64Frac( float64 a )
  263. {
  264. return a & LIT64( 0x000FFFFFFFFFFFFF );
  265. }
  266. /*
  267. -------------------------------------------------------------------------------
  268. Returns the exponent bits of the double-precision floating-point value `a'.
  269. -------------------------------------------------------------------------------
  270. */
  271. INLINE int16 extractFloat64Exp( float64 a )
  272. {
  273. return ( a>>52 ) & 0x7FF;
  274. }
  275. /*
  276. -------------------------------------------------------------------------------
  277. Returns the sign bit of the double-precision floating-point value `a'.
  278. -------------------------------------------------------------------------------
  279. */
  280. #if 0 /* in softfloat.h */
  281. INLINE flag extractFloat64Sign( float64 a )
  282. {
  283. return a>>63;
  284. }
  285. #endif
  286. /*
  287. -------------------------------------------------------------------------------
  288. Normalizes the subnormal double-precision floating-point value represented
  289. by the denormalized significand `aSig'. The normalized exponent and
  290. significand are stored at the locations pointed to by `zExpPtr' and
  291. `zSigPtr', respectively.
  292. -------------------------------------------------------------------------------
  293. */
  294. static void
  295. normalizeFloat64Subnormal( bits64 aSig, int16 *zExpPtr, bits64 *zSigPtr )
  296. {
  297. int8 shiftCount;
  298. shiftCount = countLeadingZeros64( aSig ) - 11;
  299. *zSigPtr = aSig<<shiftCount;
  300. *zExpPtr = 1 - shiftCount;
  301. }
  302. /*
  303. -------------------------------------------------------------------------------
  304. Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
  305. double-precision floating-point value, returning the result. After being
  306. shifted into the proper positions, the three fields are simply added
  307. together to form the result. This means that any integer portion of `zSig'
  308. will be added into the exponent. Since a properly normalized significand
  309. will have an integer portion equal to 1, the `zExp' input should be 1 less
  310. than the desired result exponent whenever `zSig' is a complete, normalized
  311. significand.
  312. -------------------------------------------------------------------------------
  313. */
  314. INLINE float64 packFloat64( flag zSign, int16 zExp, bits64 zSig )
  315. {
  316. return ( ( (bits64) zSign )<<63 ) + ( ( (bits64) zExp )<<52 ) + zSig;
  317. }
  318. /*
  319. -------------------------------------------------------------------------------
  320. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  321. and significand `zSig', and returns the proper double-precision floating-
  322. point value corresponding to the abstract input. Ordinarily, the abstract
  323. value is simply rounded and packed into the double-precision format, with
  324. the inexact exception raised if the abstract input cannot be represented
  325. exactly. If the abstract value is too large, however, the overflow and
  326. inexact exceptions are raised and an infinity or maximal finite value is
  327. returned. If the abstract value is too small, the input value is rounded to
  328. a subnormal number, and the underflow and inexact exceptions are raised if
  329. the abstract input cannot be represented exactly as a subnormal double-
  330. precision floating-point number.
  331. The input significand `zSig' has its binary point between bits 62
  332. and 61, which is 10 bits to the left of the usual location. This shifted
  333. significand must be normalized or smaller. If `zSig' is not normalized,
  334. `zExp' must be 0; in that case, the result returned is a subnormal number,
  335. and it must not require rounding. In the usual case that `zSig' is
  336. normalized, `zExp' must be 1 less than the ``true'' floating-point exponent.
  337. The handling of underflow and overflow follows the IEC/IEEE Standard for
  338. Binary Floating-point Arithmetic.
  339. -------------------------------------------------------------------------------
  340. */
  341. static float64 roundAndPackFloat64( struct roundingData *roundData, flag zSign, int16 zExp, bits64 zSig )
  342. {
  343. int8 roundingMode;
  344. flag roundNearestEven;
  345. int16 roundIncrement, roundBits;
  346. flag isTiny;
  347. roundingMode = roundData->mode;
  348. roundNearestEven = ( roundingMode == float_round_nearest_even );
  349. roundIncrement = 0x200;
  350. if ( ! roundNearestEven ) {
  351. if ( roundingMode == float_round_to_zero ) {
  352. roundIncrement = 0;
  353. }
  354. else {
  355. roundIncrement = 0x3FF;
  356. if ( zSign ) {
  357. if ( roundingMode == float_round_up ) roundIncrement = 0;
  358. }
  359. else {
  360. if ( roundingMode == float_round_down ) roundIncrement = 0;
  361. }
  362. }
  363. }
  364. roundBits = zSig & 0x3FF;
  365. if ( 0x7FD <= (bits16) zExp ) {
  366. if ( ( 0x7FD < zExp )
  367. || ( ( zExp == 0x7FD )
  368. && ( (sbits64) ( zSig + roundIncrement ) < 0 ) )
  369. ) {
  370. //register int lr = __builtin_return_address(0);
  371. //printk("roundAndPackFloat64 called from 0x%08x\n",lr);
  372. roundData->exception |= float_flag_overflow | float_flag_inexact;
  373. return packFloat64( zSign, 0x7FF, 0 ) - ( roundIncrement == 0 );
  374. }
  375. if ( zExp < 0 ) {
  376. isTiny =
  377. ( float_detect_tininess == float_tininess_before_rounding )
  378. || ( zExp < -1 )
  379. || ( zSig + roundIncrement < LIT64( 0x8000000000000000 ) );
  380. shift64RightJamming( zSig, - zExp, &zSig );
  381. zExp = 0;
  382. roundBits = zSig & 0x3FF;
  383. if ( isTiny && roundBits ) roundData->exception |= float_flag_underflow;
  384. }
  385. }
  386. if ( roundBits ) roundData->exception |= float_flag_inexact;
  387. zSig = ( zSig + roundIncrement )>>10;
  388. zSig &= ~ ( ( ( roundBits ^ 0x200 ) == 0 ) & roundNearestEven );
  389. if ( zSig == 0 ) zExp = 0;
  390. return packFloat64( zSign, zExp, zSig );
  391. }
  392. /*
  393. -------------------------------------------------------------------------------
  394. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  395. and significand `zSig', and returns the proper double-precision floating-
  396. point value corresponding to the abstract input. This routine is just like
  397. `roundAndPackFloat64' except that `zSig' does not have to be normalized in
  398. any way. In all cases, `zExp' must be 1 less than the ``true'' floating-
  399. point exponent.
  400. -------------------------------------------------------------------------------
  401. */
  402. static float64
  403. normalizeRoundAndPackFloat64( struct roundingData *roundData, flag zSign, int16 zExp, bits64 zSig )
  404. {
  405. int8 shiftCount;
  406. shiftCount = countLeadingZeros64( zSig ) - 1;
  407. return roundAndPackFloat64( roundData, zSign, zExp - shiftCount, zSig<<shiftCount );
  408. }
  409. #ifdef FLOATX80
  410. /*
  411. -------------------------------------------------------------------------------
  412. Returns the fraction bits of the extended double-precision floating-point
  413. value `a'.
  414. -------------------------------------------------------------------------------
  415. */
  416. INLINE bits64 extractFloatx80Frac( floatx80 a )
  417. {
  418. return a.low;
  419. }
  420. /*
  421. -------------------------------------------------------------------------------
  422. Returns the exponent bits of the extended double-precision floating-point
  423. value `a'.
  424. -------------------------------------------------------------------------------
  425. */
  426. INLINE int32 extractFloatx80Exp( floatx80 a )
  427. {
  428. return a.high & 0x7FFF;
  429. }
  430. /*
  431. -------------------------------------------------------------------------------
  432. Returns the sign bit of the extended double-precision floating-point value
  433. `a'.
  434. -------------------------------------------------------------------------------
  435. */
  436. INLINE flag extractFloatx80Sign( floatx80 a )
  437. {
  438. return a.high>>15;
  439. }
  440. /*
  441. -------------------------------------------------------------------------------
  442. Normalizes the subnormal extended double-precision floating-point value
  443. represented by the denormalized significand `aSig'. The normalized exponent
  444. and significand are stored at the locations pointed to by `zExpPtr' and
  445. `zSigPtr', respectively.
  446. -------------------------------------------------------------------------------
  447. */
  448. static void
  449. normalizeFloatx80Subnormal( bits64 aSig, int32 *zExpPtr, bits64 *zSigPtr )
  450. {
  451. int8 shiftCount;
  452. shiftCount = countLeadingZeros64( aSig );
  453. *zSigPtr = aSig<<shiftCount;
  454. *zExpPtr = 1 - shiftCount;
  455. }
  456. /*
  457. -------------------------------------------------------------------------------
  458. Packs the sign `zSign', exponent `zExp', and significand `zSig' into an
  459. extended double-precision floating-point value, returning the result.
  460. -------------------------------------------------------------------------------
  461. */
  462. INLINE floatx80 packFloatx80( flag zSign, int32 zExp, bits64 zSig )
  463. {
  464. floatx80 z;
  465. z.low = zSig;
  466. z.high = ( ( (bits16) zSign )<<15 ) + zExp;
  467. z.__padding = 0;
  468. return z;
  469. }
  470. /*
  471. -------------------------------------------------------------------------------
  472. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  473. and extended significand formed by the concatenation of `zSig0' and `zSig1',
  474. and returns the proper extended double-precision floating-point value
  475. corresponding to the abstract input. Ordinarily, the abstract value is
  476. rounded and packed into the extended double-precision format, with the
  477. inexact exception raised if the abstract input cannot be represented
  478. exactly. If the abstract value is too large, however, the overflow and
  479. inexact exceptions are raised and an infinity or maximal finite value is
  480. returned. If the abstract value is too small, the input value is rounded to
  481. a subnormal number, and the underflow and inexact exceptions are raised if
  482. the abstract input cannot be represented exactly as a subnormal extended
  483. double-precision floating-point number.
  484. If `roundingPrecision' is 32 or 64, the result is rounded to the same
  485. number of bits as single or double precision, respectively. Otherwise, the
  486. result is rounded to the full precision of the extended double-precision
  487. format.
  488. The input significand must be normalized or smaller. If the input
  489. significand is not normalized, `zExp' must be 0; in that case, the result
  490. returned is a subnormal number, and it must not require rounding. The
  491. handling of underflow and overflow follows the IEC/IEEE Standard for Binary
  492. Floating-point Arithmetic.
  493. -------------------------------------------------------------------------------
  494. */
  495. static floatx80
  496. roundAndPackFloatx80(
  497. struct roundingData *roundData, flag zSign, int32 zExp, bits64 zSig0, bits64 zSig1
  498. )
  499. {
  500. int8 roundingMode, roundingPrecision;
  501. flag roundNearestEven, increment, isTiny;
  502. int64 roundIncrement, roundMask, roundBits;
  503. roundingMode = roundData->mode;
  504. roundingPrecision = roundData->precision;
  505. roundNearestEven = ( roundingMode == float_round_nearest_even );
  506. if ( roundingPrecision == 80 ) goto precision80;
  507. if ( roundingPrecision == 64 ) {
  508. roundIncrement = LIT64( 0x0000000000000400 );
  509. roundMask = LIT64( 0x00000000000007FF );
  510. }
  511. else if ( roundingPrecision == 32 ) {
  512. roundIncrement = LIT64( 0x0000008000000000 );
  513. roundMask = LIT64( 0x000000FFFFFFFFFF );
  514. }
  515. else {
  516. goto precision80;
  517. }
  518. zSig0 |= ( zSig1 != 0 );
  519. if ( ! roundNearestEven ) {
  520. if ( roundingMode == float_round_to_zero ) {
  521. roundIncrement = 0;
  522. }
  523. else {
  524. roundIncrement = roundMask;
  525. if ( zSign ) {
  526. if ( roundingMode == float_round_up ) roundIncrement = 0;
  527. }
  528. else {
  529. if ( roundingMode == float_round_down ) roundIncrement = 0;
  530. }
  531. }
  532. }
  533. roundBits = zSig0 & roundMask;
  534. if ( 0x7FFD <= (bits32) ( zExp - 1 ) ) {
  535. if ( ( 0x7FFE < zExp )
  536. || ( ( zExp == 0x7FFE ) && ( zSig0 + roundIncrement < zSig0 ) )
  537. ) {
  538. goto overflow;
  539. }
  540. if ( zExp <= 0 ) {
  541. isTiny =
  542. ( float_detect_tininess == float_tininess_before_rounding )
  543. || ( zExp < 0 )
  544. || ( zSig0 <= zSig0 + roundIncrement );
  545. shift64RightJamming( zSig0, 1 - zExp, &zSig0 );
  546. zExp = 0;
  547. roundBits = zSig0 & roundMask;
  548. if ( isTiny && roundBits ) roundData->exception |= float_flag_underflow;
  549. if ( roundBits ) roundData->exception |= float_flag_inexact;
  550. zSig0 += roundIncrement;
  551. if ( (sbits64) zSig0 < 0 ) zExp = 1;
  552. roundIncrement = roundMask + 1;
  553. if ( roundNearestEven && ( roundBits<<1 == roundIncrement ) ) {
  554. roundMask |= roundIncrement;
  555. }
  556. zSig0 &= ~ roundMask;
  557. return packFloatx80( zSign, zExp, zSig0 );
  558. }
  559. }
  560. if ( roundBits ) roundData->exception |= float_flag_inexact;
  561. zSig0 += roundIncrement;
  562. if ( zSig0 < roundIncrement ) {
  563. ++zExp;
  564. zSig0 = LIT64( 0x8000000000000000 );
  565. }
  566. roundIncrement = roundMask + 1;
  567. if ( roundNearestEven && ( roundBits<<1 == roundIncrement ) ) {
  568. roundMask |= roundIncrement;
  569. }
  570. zSig0 &= ~ roundMask;
  571. if ( zSig0 == 0 ) zExp = 0;
  572. return packFloatx80( zSign, zExp, zSig0 );
  573. precision80:
  574. increment = ( (sbits64) zSig1 < 0 );
  575. if ( ! roundNearestEven ) {
  576. if ( roundingMode == float_round_to_zero ) {
  577. increment = 0;
  578. }
  579. else {
  580. if ( zSign ) {
  581. increment = ( roundingMode == float_round_down ) && zSig1;
  582. }
  583. else {
  584. increment = ( roundingMode == float_round_up ) && zSig1;
  585. }
  586. }
  587. }
  588. if ( 0x7FFD <= (bits32) ( zExp - 1 ) ) {
  589. if ( ( 0x7FFE < zExp )
  590. || ( ( zExp == 0x7FFE )
  591. && ( zSig0 == LIT64( 0xFFFFFFFFFFFFFFFF ) )
  592. && increment
  593. )
  594. ) {
  595. roundMask = 0;
  596. overflow:
  597. roundData->exception |= float_flag_overflow | float_flag_inexact;
  598. if ( ( roundingMode == float_round_to_zero )
  599. || ( zSign && ( roundingMode == float_round_up ) )
  600. || ( ! zSign && ( roundingMode == float_round_down ) )
  601. ) {
  602. return packFloatx80( zSign, 0x7FFE, ~ roundMask );
  603. }
  604. return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
  605. }
  606. if ( zExp <= 0 ) {
  607. isTiny =
  608. ( float_detect_tininess == float_tininess_before_rounding )
  609. || ( zExp < 0 )
  610. || ! increment
  611. || ( zSig0 < LIT64( 0xFFFFFFFFFFFFFFFF ) );
  612. shift64ExtraRightJamming( zSig0, zSig1, 1 - zExp, &zSig0, &zSig1 );
  613. zExp = 0;
  614. if ( isTiny && zSig1 ) roundData->exception |= float_flag_underflow;
  615. if ( zSig1 ) roundData->exception |= float_flag_inexact;
  616. if ( roundNearestEven ) {
  617. increment = ( (sbits64) zSig1 < 0 );
  618. }
  619. else {
  620. if ( zSign ) {
  621. increment = ( roundingMode == float_round_down ) && zSig1;
  622. }
  623. else {
  624. increment = ( roundingMode == float_round_up ) && zSig1;
  625. }
  626. }
  627. if ( increment ) {
  628. ++zSig0;
  629. zSig0 &= ~ ( ( zSig1 + zSig1 == 0 ) & roundNearestEven );
  630. if ( (sbits64) zSig0 < 0 ) zExp = 1;
  631. }
  632. return packFloatx80( zSign, zExp, zSig0 );
  633. }
  634. }
  635. if ( zSig1 ) roundData->exception |= float_flag_inexact;
  636. if ( increment ) {
  637. ++zSig0;
  638. if ( zSig0 == 0 ) {
  639. ++zExp;
  640. zSig0 = LIT64( 0x8000000000000000 );
  641. }
  642. else {
  643. zSig0 &= ~ ( ( zSig1 + zSig1 == 0 ) & roundNearestEven );
  644. }
  645. }
  646. else {
  647. if ( zSig0 == 0 ) zExp = 0;
  648. }
  649. return packFloatx80( zSign, zExp, zSig0 );
  650. }
  651. /*
  652. -------------------------------------------------------------------------------
  653. Takes an abstract floating-point value having sign `zSign', exponent
  654. `zExp', and significand formed by the concatenation of `zSig0' and `zSig1',
  655. and returns the proper extended double-precision floating-point value
  656. corresponding to the abstract input. This routine is just like
  657. `roundAndPackFloatx80' except that the input significand does not have to be
  658. normalized.
  659. -------------------------------------------------------------------------------
  660. */
  661. static floatx80
  662. normalizeRoundAndPackFloatx80(
  663. struct roundingData *roundData, flag zSign, int32 zExp, bits64 zSig0, bits64 zSig1
  664. )
  665. {
  666. int8 shiftCount;
  667. if ( zSig0 == 0 ) {
  668. zSig0 = zSig1;
  669. zSig1 = 0;
  670. zExp -= 64;
  671. }
  672. shiftCount = countLeadingZeros64( zSig0 );
  673. shortShift128Left( zSig0, zSig1, shiftCount, &zSig0, &zSig1 );
  674. zExp -= shiftCount;
  675. return
  676. roundAndPackFloatx80( roundData, zSign, zExp, zSig0, zSig1 );
  677. }
  678. #endif
  679. /*
  680. -------------------------------------------------------------------------------
  681. Returns the result of converting the 32-bit two's complement integer `a' to
  682. the single-precision floating-point format. The conversion is performed
  683. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  684. -------------------------------------------------------------------------------
  685. */
  686. float32 int32_to_float32(struct roundingData *roundData, int32 a)
  687. {
  688. flag zSign;
  689. if ( a == 0 ) return 0;
  690. if ( a == 0x80000000 ) return packFloat32( 1, 0x9E, 0 );
  691. zSign = ( a < 0 );
  692. return normalizeRoundAndPackFloat32( roundData, zSign, 0x9C, zSign ? - a : a );
  693. }
  694. /*
  695. -------------------------------------------------------------------------------
  696. Returns the result of converting the 32-bit two's complement integer `a' to
  697. the double-precision floating-point format. The conversion is performed
  698. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  699. -------------------------------------------------------------------------------
  700. */
  701. float64 int32_to_float64( int32 a )
  702. {
  703. flag aSign;
  704. uint32 absA;
  705. int8 shiftCount;
  706. bits64 zSig;
  707. if ( a == 0 ) return 0;
  708. aSign = ( a < 0 );
  709. absA = aSign ? - a : a;
  710. shiftCount = countLeadingZeros32( absA ) + 21;
  711. zSig = absA;
  712. return packFloat64( aSign, 0x432 - shiftCount, zSig<<shiftCount );
  713. }
  714. #ifdef FLOATX80
  715. /*
  716. -------------------------------------------------------------------------------
  717. Returns the result of converting the 32-bit two's complement integer `a'
  718. to the extended double-precision floating-point format. The conversion
  719. is performed according to the IEC/IEEE Standard for Binary Floating-point
  720. Arithmetic.
  721. -------------------------------------------------------------------------------
  722. */
  723. floatx80 int32_to_floatx80( int32 a )
  724. {
  725. flag zSign;
  726. uint32 absA;
  727. int8 shiftCount;
  728. bits64 zSig;
  729. if ( a == 0 ) return packFloatx80( 0, 0, 0 );
  730. zSign = ( a < 0 );
  731. absA = zSign ? - a : a;
  732. shiftCount = countLeadingZeros32( absA ) + 32;
  733. zSig = absA;
  734. return packFloatx80( zSign, 0x403E - shiftCount, zSig<<shiftCount );
  735. }
  736. #endif
  737. /*
  738. -------------------------------------------------------------------------------
  739. Returns the result of converting the single-precision floating-point value
  740. `a' to the 32-bit two's complement integer format. The conversion is
  741. performed according to the IEC/IEEE Standard for Binary Floating-point
  742. Arithmetic---which means in particular that the conversion is rounded
  743. according to the current rounding mode. If `a' is a NaN, the largest
  744. positive integer is returned. Otherwise, if the conversion overflows, the
  745. largest integer with the same sign as `a' is returned.
  746. -------------------------------------------------------------------------------
  747. */
  748. int32 float32_to_int32( struct roundingData *roundData, float32 a )
  749. {
  750. flag aSign;
  751. int16 aExp, shiftCount;
  752. bits32 aSig;
  753. bits64 zSig;
  754. aSig = extractFloat32Frac( a );
  755. aExp = extractFloat32Exp( a );
  756. aSign = extractFloat32Sign( a );
  757. if ( ( aExp == 0x7FF ) && aSig ) aSign = 0;
  758. if ( aExp ) aSig |= 0x00800000;
  759. shiftCount = 0xAF - aExp;
  760. zSig = aSig;
  761. zSig <<= 32;
  762. if ( 0 < shiftCount ) shift64RightJamming( zSig, shiftCount, &zSig );
  763. return roundAndPackInt32( roundData, aSign, zSig );
  764. }
  765. /*
  766. -------------------------------------------------------------------------------
  767. Returns the result of converting the single-precision floating-point value
  768. `a' to the 32-bit two's complement integer format. The conversion is
  769. performed according to the IEC/IEEE Standard for Binary Floating-point
  770. Arithmetic, except that the conversion is always rounded toward zero. If
  771. `a' is a NaN, the largest positive integer is returned. Otherwise, if the
  772. conversion overflows, the largest integer with the same sign as `a' is
  773. returned.
  774. -------------------------------------------------------------------------------
  775. */
  776. int32 float32_to_int32_round_to_zero( float32 a )
  777. {
  778. flag aSign;
  779. int16 aExp, shiftCount;
  780. bits32 aSig;
  781. int32 z;
  782. aSig = extractFloat32Frac( a );
  783. aExp = extractFloat32Exp( a );
  784. aSign = extractFloat32Sign( a );
  785. shiftCount = aExp - 0x9E;
  786. if ( 0 <= shiftCount ) {
  787. if ( a == 0xCF000000 ) return 0x80000000;
  788. float_raise( float_flag_invalid );
  789. if ( ! aSign || ( ( aExp == 0xFF ) && aSig ) ) return 0x7FFFFFFF;
  790. return 0x80000000;
  791. }
  792. else if ( aExp <= 0x7E ) {
  793. if ( aExp | aSig ) float_raise( float_flag_inexact );
  794. return 0;
  795. }
  796. aSig = ( aSig | 0x00800000 )<<8;
  797. z = aSig>>( - shiftCount );
  798. if ( (bits32) ( aSig<<( shiftCount & 31 ) ) ) {
  799. float_raise( float_flag_inexact );
  800. }
  801. return aSign ? - z : z;
  802. }
  803. /*
  804. -------------------------------------------------------------------------------
  805. Returns the result of converting the single-precision floating-point value
  806. `a' to the double-precision floating-point format. The conversion is
  807. performed according to the IEC/IEEE Standard for Binary Floating-point
  808. Arithmetic.
  809. -------------------------------------------------------------------------------
  810. */
  811. float64 float32_to_float64( float32 a )
  812. {
  813. flag aSign;
  814. int16 aExp;
  815. bits32 aSig;
  816. aSig = extractFloat32Frac( a );
  817. aExp = extractFloat32Exp( a );
  818. aSign = extractFloat32Sign( a );
  819. if ( aExp == 0xFF ) {
  820. if ( aSig ) return commonNaNToFloat64( float32ToCommonNaN( a ) );
  821. return packFloat64( aSign, 0x7FF, 0 );
  822. }
  823. if ( aExp == 0 ) {
  824. if ( aSig == 0 ) return packFloat64( aSign, 0, 0 );
  825. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  826. --aExp;
  827. }
  828. return packFloat64( aSign, aExp + 0x380, ( (bits64) aSig )<<29 );
  829. }
  830. #ifdef FLOATX80
  831. /*
  832. -------------------------------------------------------------------------------
  833. Returns the result of converting the single-precision floating-point value
  834. `a' to the extended double-precision floating-point format. The conversion
  835. is performed according to the IEC/IEEE Standard for Binary Floating-point
  836. Arithmetic.
  837. -------------------------------------------------------------------------------
  838. */
  839. floatx80 float32_to_floatx80( float32 a )
  840. {
  841. flag aSign;
  842. int16 aExp;
  843. bits32 aSig;
  844. aSig = extractFloat32Frac( a );
  845. aExp = extractFloat32Exp( a );
  846. aSign = extractFloat32Sign( a );
  847. if ( aExp == 0xFF ) {
  848. if ( aSig ) return commonNaNToFloatx80( float32ToCommonNaN( a ) );
  849. return packFloatx80( aSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
  850. }
  851. if ( aExp == 0 ) {
  852. if ( aSig == 0 ) return packFloatx80( aSign, 0, 0 );
  853. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  854. }
  855. aSig |= 0x00800000;
  856. return packFloatx80( aSign, aExp + 0x3F80, ( (bits64) aSig )<<40 );
  857. }
  858. #endif
  859. /*
  860. -------------------------------------------------------------------------------
  861. Rounds the single-precision floating-point value `a' to an integer, and
  862. returns the result as a single-precision floating-point value. The
  863. operation is performed according to the IEC/IEEE Standard for Binary
  864. Floating-point Arithmetic.
  865. -------------------------------------------------------------------------------
  866. */
  867. float32 float32_round_to_int( struct roundingData *roundData, float32 a )
  868. {
  869. flag aSign;
  870. int16 aExp;
  871. bits32 lastBitMask, roundBitsMask;
  872. int8 roundingMode;
  873. float32 z;
  874. aExp = extractFloat32Exp( a );
  875. if ( 0x96 <= aExp ) {
  876. if ( ( aExp == 0xFF ) && extractFloat32Frac( a ) ) {
  877. return propagateFloat32NaN( a, a );
  878. }
  879. return a;
  880. }
  881. roundingMode = roundData->mode;
  882. if ( aExp <= 0x7E ) {
  883. if ( (bits32) ( a<<1 ) == 0 ) return a;
  884. roundData->exception |= float_flag_inexact;
  885. aSign = extractFloat32Sign( a );
  886. switch ( roundingMode ) {
  887. case float_round_nearest_even:
  888. if ( ( aExp == 0x7E ) && extractFloat32Frac( a ) ) {
  889. return packFloat32( aSign, 0x7F, 0 );
  890. }
  891. break;
  892. case float_round_down:
  893. return aSign ? 0xBF800000 : 0;
  894. case float_round_up:
  895. return aSign ? 0x80000000 : 0x3F800000;
  896. }
  897. return packFloat32( aSign, 0, 0 );
  898. }
  899. lastBitMask = 1;
  900. lastBitMask <<= 0x96 - aExp;
  901. roundBitsMask = lastBitMask - 1;
  902. z = a;
  903. if ( roundingMode == float_round_nearest_even ) {
  904. z += lastBitMask>>1;
  905. if ( ( z & roundBitsMask ) == 0 ) z &= ~ lastBitMask;
  906. }
  907. else if ( roundingMode != float_round_to_zero ) {
  908. if ( extractFloat32Sign( z ) ^ ( roundingMode == float_round_up ) ) {
  909. z += roundBitsMask;
  910. }
  911. }
  912. z &= ~ roundBitsMask;
  913. if ( z != a ) roundData->exception |= float_flag_inexact;
  914. return z;
  915. }
  916. /*
  917. -------------------------------------------------------------------------------
  918. Returns the result of adding the absolute values of the single-precision
  919. floating-point values `a' and `b'. If `zSign' is true, the sum is negated
  920. before being returned. `zSign' is ignored if the result is a NaN. The
  921. addition is performed according to the IEC/IEEE Standard for Binary
  922. Floating-point Arithmetic.
  923. -------------------------------------------------------------------------------
  924. */
  925. static float32 addFloat32Sigs( struct roundingData *roundData, float32 a, float32 b, flag zSign )
  926. {
  927. int16 aExp, bExp, zExp;
  928. bits32 aSig, bSig, zSig;
  929. int16 expDiff;
  930. aSig = extractFloat32Frac( a );
  931. aExp = extractFloat32Exp( a );
  932. bSig = extractFloat32Frac( b );
  933. bExp = extractFloat32Exp( b );
  934. expDiff = aExp - bExp;
  935. aSig <<= 6;
  936. bSig <<= 6;
  937. if ( 0 < expDiff ) {
  938. if ( aExp == 0xFF ) {
  939. if ( aSig ) return propagateFloat32NaN( a, b );
  940. return a;
  941. }
  942. if ( bExp == 0 ) {
  943. --expDiff;
  944. }
  945. else {
  946. bSig |= 0x20000000;
  947. }
  948. shift32RightJamming( bSig, expDiff, &bSig );
  949. zExp = aExp;
  950. }
  951. else if ( expDiff < 0 ) {
  952. if ( bExp == 0xFF ) {
  953. if ( bSig ) return propagateFloat32NaN( a, b );
  954. return packFloat32( zSign, 0xFF, 0 );
  955. }
  956. if ( aExp == 0 ) {
  957. ++expDiff;
  958. }
  959. else {
  960. aSig |= 0x20000000;
  961. }
  962. shift32RightJamming( aSig, - expDiff, &aSig );
  963. zExp = bExp;
  964. }
  965. else {
  966. if ( aExp == 0xFF ) {
  967. if ( aSig | bSig ) return propagateFloat32NaN( a, b );
  968. return a;
  969. }
  970. if ( aExp == 0 ) return packFloat32( zSign, 0, ( aSig + bSig )>>6 );
  971. zSig = 0x40000000 + aSig + bSig;
  972. zExp = aExp;
  973. goto roundAndPack;
  974. }
  975. aSig |= 0x20000000;
  976. zSig = ( aSig + bSig )<<1;
  977. --zExp;
  978. if ( (sbits32) zSig < 0 ) {
  979. zSig = aSig + bSig;
  980. ++zExp;
  981. }
  982. roundAndPack:
  983. return roundAndPackFloat32( roundData, zSign, zExp, zSig );
  984. }
  985. /*
  986. -------------------------------------------------------------------------------
  987. Returns the result of subtracting the absolute values of the single-
  988. precision floating-point values `a' and `b'. If `zSign' is true, the
  989. difference is negated before being returned. `zSign' is ignored if the
  990. result is a NaN. The subtraction is performed according to the IEC/IEEE
  991. Standard for Binary Floating-point Arithmetic.
  992. -------------------------------------------------------------------------------
  993. */
  994. static float32 subFloat32Sigs( struct roundingData *roundData, float32 a, float32 b, flag zSign )
  995. {
  996. int16 aExp, bExp, zExp;
  997. bits32 aSig, bSig, zSig;
  998. int16 expDiff;
  999. aSig = extractFloat32Frac( a );
  1000. aExp = extractFloat32Exp( a );
  1001. bSig = extractFloat32Frac( b );
  1002. bExp = extractFloat32Exp( b );
  1003. expDiff = aExp - bExp;
  1004. aSig <<= 7;
  1005. bSig <<= 7;
  1006. if ( 0 < expDiff ) goto aExpBigger;
  1007. if ( expDiff < 0 ) goto bExpBigger;
  1008. if ( aExp == 0xFF ) {
  1009. if ( aSig | bSig ) return propagateFloat32NaN( a, b );
  1010. roundData->exception |= float_flag_invalid;
  1011. return float32_default_nan;
  1012. }
  1013. if ( aExp == 0 ) {
  1014. aExp = 1;
  1015. bExp = 1;
  1016. }
  1017. if ( bSig < aSig ) goto aBigger;
  1018. if ( aSig < bSig ) goto bBigger;
  1019. return packFloat32( roundData->mode == float_round_down, 0, 0 );
  1020. bExpBigger:
  1021. if ( bExp == 0xFF ) {
  1022. if ( bSig ) return propagateFloat32NaN( a, b );
  1023. return packFloat32( zSign ^ 1, 0xFF, 0 );
  1024. }
  1025. if ( aExp == 0 ) {
  1026. ++expDiff;
  1027. }
  1028. else {
  1029. aSig |= 0x40000000;
  1030. }
  1031. shift32RightJamming( aSig, - expDiff, &aSig );
  1032. bSig |= 0x40000000;
  1033. bBigger:
  1034. zSig = bSig - aSig;
  1035. zExp = bExp;
  1036. zSign ^= 1;
  1037. goto normalizeRoundAndPack;
  1038. aExpBigger:
  1039. if ( aExp == 0xFF ) {
  1040. if ( aSig ) return propagateFloat32NaN( a, b );
  1041. return a;
  1042. }
  1043. if ( bExp == 0 ) {
  1044. --expDiff;
  1045. }
  1046. else {
  1047. bSig |= 0x40000000;
  1048. }
  1049. shift32RightJamming( bSig, expDiff, &bSig );
  1050. aSig |= 0x40000000;
  1051. aBigger:
  1052. zSig = aSig - bSig;
  1053. zExp = aExp;
  1054. normalizeRoundAndPack:
  1055. --zExp;
  1056. return normalizeRoundAndPackFloat32( roundData, zSign, zExp, zSig );
  1057. }
  1058. /*
  1059. -------------------------------------------------------------------------------
  1060. Returns the result of adding the single-precision floating-point values `a'
  1061. and `b'. The operation is performed according to the IEC/IEEE Standard for
  1062. Binary Floating-point Arithmetic.
  1063. -------------------------------------------------------------------------------
  1064. */
  1065. float32 float32_add( struct roundingData *roundData, float32 a, float32 b )
  1066. {
  1067. flag aSign, bSign;
  1068. aSign = extractFloat32Sign( a );
  1069. bSign = extractFloat32Sign( b );
  1070. if ( aSign == bSign ) {
  1071. return addFloat32Sigs( roundData, a, b, aSign );
  1072. }
  1073. else {
  1074. return subFloat32Sigs( roundData, a, b, aSign );
  1075. }
  1076. }
  1077. /*
  1078. -------------------------------------------------------------------------------
  1079. Returns the result of subtracting the single-precision floating-point values
  1080. `a' and `b'. The operation is performed according to the IEC/IEEE Standard
  1081. for Binary Floating-point Arithmetic.
  1082. -------------------------------------------------------------------------------
  1083. */
  1084. float32 float32_sub( struct roundingData *roundData, float32 a, float32 b )
  1085. {
  1086. flag aSign, bSign;
  1087. aSign = extractFloat32Sign( a );
  1088. bSign = extractFloat32Sign( b );
  1089. if ( aSign == bSign ) {
  1090. return subFloat32Sigs( roundData, a, b, aSign );
  1091. }
  1092. else {
  1093. return addFloat32Sigs( roundData, a, b, aSign );
  1094. }
  1095. }
  1096. /*
  1097. -------------------------------------------------------------------------------
  1098. Returns the result of multiplying the single-precision floating-point values
  1099. `a' and `b'. The operation is performed according to the IEC/IEEE Standard
  1100. for Binary Floating-point Arithmetic.
  1101. -------------------------------------------------------------------------------
  1102. */
  1103. float32 float32_mul( struct roundingData *roundData, float32 a, float32 b )
  1104. {
  1105. flag aSign, bSign, zSign;
  1106. int16 aExp, bExp, zExp;
  1107. bits32 aSig, bSig;
  1108. bits64 zSig64;
  1109. bits32 zSig;
  1110. aSig = extractFloat32Frac( a );
  1111. aExp = extractFloat32Exp( a );
  1112. aSign = extractFloat32Sign( a );
  1113. bSig = extractFloat32Frac( b );
  1114. bExp = extractFloat32Exp( b );
  1115. bSign = extractFloat32Sign( b );
  1116. zSign = aSign ^ bSign;
  1117. if ( aExp == 0xFF ) {
  1118. if ( aSig || ( ( bExp == 0xFF ) && bSig ) ) {
  1119. return propagateFloat32NaN( a, b );
  1120. }
  1121. if ( ( bExp | bSig ) == 0 ) {
  1122. roundData->exception |= float_flag_invalid;
  1123. return float32_default_nan;
  1124. }
  1125. return packFloat32( zSign, 0xFF, 0 );
  1126. }
  1127. if ( bExp == 0xFF ) {
  1128. if ( bSig ) return propagateFloat32NaN( a, b );
  1129. if ( ( aExp | aSig ) == 0 ) {
  1130. roundData->exception |= float_flag_invalid;
  1131. return float32_default_nan;
  1132. }
  1133. return packFloat32( zSign, 0xFF, 0 );
  1134. }
  1135. if ( aExp == 0 ) {
  1136. if ( aSig == 0 ) return packFloat32( zSign, 0, 0 );
  1137. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  1138. }
  1139. if ( bExp == 0 ) {
  1140. if ( bSig == 0 ) return packFloat32( zSign, 0, 0 );
  1141. normalizeFloat32Subnormal( bSig, &bExp, &bSig );
  1142. }
  1143. zExp = aExp + bExp - 0x7F;
  1144. aSig = ( aSig | 0x00800000 )<<7;
  1145. bSig = ( bSig | 0x00800000 )<<8;
  1146. shift64RightJamming( ( (bits64) aSig ) * bSig, 32, &zSig64 );
  1147. zSig = zSig64;
  1148. if ( 0 <= (sbits32) ( zSig<<1 ) ) {
  1149. zSig <<= 1;
  1150. --zExp;
  1151. }
  1152. return roundAndPackFloat32( roundData, zSign, zExp, zSig );
  1153. }
  1154. /*
  1155. -------------------------------------------------------------------------------
  1156. Returns the result of dividing the single-precision floating-point value `a'
  1157. by the corresponding value `b'. The operation is performed according to the
  1158. IEC/IEEE Standard for Binary Floating-point Arithmetic.
  1159. -------------------------------------------------------------------------------
  1160. */
  1161. float32 float32_div( struct roundingData *roundData, float32 a, float32 b )
  1162. {
  1163. flag aSign, bSign, zSign;
  1164. int16 aExp, bExp, zExp;
  1165. bits32 aSig, bSig, zSig;
  1166. aSig = extractFloat32Frac( a );
  1167. aExp = extractFloat32Exp( a );
  1168. aSign = extractFloat32Sign( a );
  1169. bSig = extractFloat32Frac( b );
  1170. bExp = extractFloat32Exp( b );
  1171. bSign = extractFloat32Sign( b );
  1172. zSign = aSign ^ bSign;
  1173. if ( aExp == 0xFF ) {
  1174. if ( aSig ) return propagateFloat32NaN( a, b );
  1175. if ( bExp == 0xFF ) {
  1176. if ( bSig ) return propagateFloat32NaN( a, b );
  1177. roundData->exception |= float_flag_invalid;
  1178. return float32_default_nan;
  1179. }
  1180. return packFloat32( zSign, 0xFF, 0 );
  1181. }
  1182. if ( bExp == 0xFF ) {
  1183. if ( bSig ) return propagateFloat32NaN( a, b );
  1184. return packFloat32( zSign, 0, 0 );
  1185. }
  1186. if ( bExp == 0 ) {
  1187. if ( bSig == 0 ) {
  1188. if ( ( aExp | aSig ) == 0 ) {
  1189. roundData->exception |= float_flag_invalid;
  1190. return float32_default_nan;
  1191. }
  1192. roundData->exception |= float_flag_divbyzero;
  1193. return packFloat32( zSign, 0xFF, 0 );
  1194. }
  1195. normalizeFloat32Subnormal( bSig, &bExp, &bSig );
  1196. }
  1197. if ( aExp == 0 ) {
  1198. if ( aSig == 0 ) return packFloat32( zSign, 0, 0 );
  1199. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  1200. }
  1201. zExp = aExp - bExp + 0x7D;
  1202. aSig = ( aSig | 0x00800000 )<<7;
  1203. bSig = ( bSig | 0x00800000 )<<8;
  1204. if ( bSig <= ( aSig + aSig ) ) {
  1205. aSig >>= 1;
  1206. ++zExp;
  1207. }
  1208. {
  1209. bits64 tmp = ( (bits64) aSig )<<32;
  1210. do_div( tmp, bSig );
  1211. zSig = tmp;
  1212. }
  1213. if ( ( zSig & 0x3F ) == 0 ) {
  1214. zSig |= ( ( (bits64) bSig ) * zSig != ( (bits64) aSig )<<32 );
  1215. }
  1216. return roundAndPackFloat32( roundData, zSign, zExp, zSig );
  1217. }
  1218. /*
  1219. -------------------------------------------------------------------------------
  1220. Returns the remainder of the single-precision floating-point value `a'
  1221. with respect to the corresponding value `b'. The operation is performed
  1222. according to the IEC/IEEE Standard for Binary Floating-point Arithmetic.
  1223. -------------------------------------------------------------------------------
  1224. */
  1225. float32 float32_rem( struct roundingData *roundData, float32 a, float32 b )
  1226. {
  1227. flag aSign, bSign, zSign;
  1228. int16 aExp, bExp, expDiff;
  1229. bits32 aSig, bSig;
  1230. bits32 q;
  1231. bits64 aSig64, bSig64, q64;
  1232. bits32 alternateASig;
  1233. sbits32 sigMean;
  1234. aSig = extractFloat32Frac( a );
  1235. aExp = extractFloat32Exp( a );
  1236. aSign = extractFloat32Sign( a );
  1237. bSig = extractFloat32Frac( b );
  1238. bExp = extractFloat32Exp( b );
  1239. bSign = extractFloat32Sign( b );
  1240. if ( aExp == 0xFF ) {
  1241. if ( aSig || ( ( bExp == 0xFF ) && bSig ) ) {
  1242. return propagateFloat32NaN( a, b );
  1243. }
  1244. roundData->exception |= float_flag_invalid;
  1245. return float32_default_nan;
  1246. }
  1247. if ( bExp == 0xFF ) {
  1248. if ( bSig ) return propagateFloat32NaN( a, b );
  1249. return a;
  1250. }
  1251. if ( bExp == 0 ) {
  1252. if ( bSig == 0 ) {
  1253. roundData->exception |= float_flag_invalid;
  1254. return float32_default_nan;
  1255. }
  1256. normalizeFloat32Subnormal( bSig, &bExp, &bSig );
  1257. }
  1258. if ( aExp == 0 ) {
  1259. if ( aSig == 0 ) return a;
  1260. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  1261. }
  1262. expDiff = aExp - bExp;
  1263. aSig |= 0x00800000;
  1264. bSig |= 0x00800000;
  1265. if ( expDiff < 32 ) {
  1266. aSig <<= 8;
  1267. bSig <<= 8;
  1268. if ( expDiff < 0 ) {
  1269. if ( expDiff < -1 ) return a;
  1270. aSig >>= 1;
  1271. }
  1272. q = ( bSig <= aSig );
  1273. if ( q ) aSig -= bSig;
  1274. if ( 0 < expDiff ) {
  1275. bits64 tmp = ( (bits64) aSig )<<32;
  1276. do_div( tmp, bSig );
  1277. q = tmp;
  1278. q >>= 32 - expDiff;
  1279. bSig >>= 2;
  1280. aSig = ( ( aSig>>1 )<<( expDiff - 1 ) ) - bSig * q;
  1281. }
  1282. else {
  1283. aSig >>= 2;
  1284. bSig >>= 2;
  1285. }
  1286. }
  1287. else {
  1288. if ( bSig <= aSig ) aSig -= bSig;
  1289. aSig64 = ( (bits64) aSig )<<40;
  1290. bSig64 = ( (bits64) bSig )<<40;
  1291. expDiff -= 64;
  1292. while ( 0 < expDiff ) {
  1293. q64 = estimateDiv128To64( aSig64, 0, bSig64 );
  1294. q64 = ( 2 < q64 ) ? q64 - 2 : 0;
  1295. aSig64 = - ( ( bSig * q64 )<<38 );
  1296. expDiff -= 62;
  1297. }
  1298. expDiff += 64;
  1299. q64 = estimateDiv128To64( aSig64, 0, bSig64 );
  1300. q64 = ( 2 < q64 ) ? q64 - 2 : 0;
  1301. q = q64>>( 64 - expDiff );
  1302. bSig <<= 6;
  1303. aSig = ( ( aSig64>>33 )<<( expDiff - 1 ) )