softfloat-macros 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. ===============================================================================
  3. This C source fragment 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. /*
  26. -------------------------------------------------------------------------------
  27. Shifts `a' right by the number of bits given in `count'. If any nonzero
  28. bits are shifted off, they are ``jammed'' into the least significant bit of
  29. the result by setting the least significant bit to 1. The value of `count'
  30. can be arbitrarily large; in particular, if `count' is greater than 32, the
  31. result will be either 0 or 1, depending on whether `a' is zero or nonzero.
  32. The result is stored in the location pointed to by `zPtr'.
  33. -------------------------------------------------------------------------------
  34. */
  35. INLINE void shift32RightJamming( bits32 a, int16 count, bits32 *zPtr )
  36. {
  37. bits32 z;
  38. if ( count == 0 ) {
  39. z = a;
  40. }
  41. else if ( count < 32 ) {
  42. z = ( a>>count ) | ( ( a<<( ( - count ) & 31 ) ) != 0 );
  43. }
  44. else {
  45. z = ( a != 0 );
  46. }
  47. *zPtr = z;
  48. }
  49. /*
  50. -------------------------------------------------------------------------------
  51. Shifts `a' right by the number of bits given in `count'. If any nonzero
  52. bits are shifted off, they are ``jammed'' into the least significant bit of
  53. the result by setting the least significant bit to 1. The value of `count'
  54. can be arbitrarily large; in particular, if `count' is greater than 64, the
  55. result will be either 0 or 1, depending on whether `a' is zero or nonzero.
  56. The result is stored in the location pointed to by `zPtr'.
  57. -------------------------------------------------------------------------------
  58. */
  59. INLINE void shift64RightJamming( bits64 a, int16 count, bits64 *zPtr )
  60. {
  61. bits64 z;
  62. __asm__("@shift64RightJamming -- start");
  63. if ( count == 0 ) {
  64. z = a;
  65. }
  66. else if ( count < 64 ) {
  67. z = ( a>>count ) | ( ( a<<( ( - count ) & 63 ) ) != 0 );
  68. }
  69. else {
  70. z = ( a != 0 );
  71. }
  72. __asm__("@shift64RightJamming -- end");
  73. *zPtr = z;
  74. }
  75. /*
  76. -------------------------------------------------------------------------------
  77. Shifts the 128-bit value formed by concatenating `a0' and `a1' right by 64
  78. _plus_ the number of bits given in `count'. The shifted result is at most
  79. 64 nonzero bits; this is stored at the location pointed to by `z0Ptr'. The
  80. bits shifted off form a second 64-bit result as follows: The _last_ bit
  81. shifted off is the most-significant bit of the extra result, and the other
  82. 63 bits of the extra result are all zero if and only if _all_but_the_last_
  83. bits shifted off were all zero. This extra result is stored in the location
  84. pointed to by `z1Ptr'. The value of `count' can be arbitrarily large.
  85. (This routine makes more sense if `a0' and `a1' are considered to form a
  86. fixed-point value with binary point between `a0' and `a1'. This fixed-point
  87. value is shifted right by the number of bits given in `count', and the
  88. integer part of the result is returned at the location pointed to by
  89. `z0Ptr'. The fractional part of the result may be slightly corrupted as
  90. described above, and is returned at the location pointed to by `z1Ptr'.)
  91. -------------------------------------------------------------------------------
  92. */
  93. INLINE void
  94. shift64ExtraRightJamming(
  95. bits64 a0, bits64 a1, int16 count, bits64 *z0Ptr, bits64 *z1Ptr )
  96. {
  97. bits64 z0, z1;
  98. int8 negCount = ( - count ) & 63;
  99. if ( count == 0 ) {
  100. z1 = a1;
  101. z0 = a0;
  102. }
  103. else if ( count < 64 ) {
  104. z1 = ( a0<<negCount ) | ( a1 != 0 );
  105. z0 = a0>>count;
  106. }
  107. else {
  108. if ( count == 64 ) {
  109. z1 = a0 | ( a1 != 0 );
  110. }
  111. else {
  112. z1 = ( ( a0 | a1 ) != 0 );
  113. }
  114. z0 = 0;
  115. }
  116. *z1Ptr = z1;
  117. *z0Ptr = z0;
  118. }
  119. /*
  120. -------------------------------------------------------------------------------
  121. Shifts the 128-bit value formed by concatenating `a0' and `a1' right by the
  122. number of bits given in `count'. Any bits shifted off are lost. The value
  123. of `count' can be arbitrarily large; in particular, if `count' is greater
  124. than 128, the result will be 0. The result is broken into two 64-bit pieces
  125. which are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
  126. -------------------------------------------------------------------------------
  127. */
  128. INLINE void
  129. shift128Right(
  130. bits64 a0, bits64 a1, int16 count, bits64 *z0Ptr, bits64 *z1Ptr )
  131. {
  132. bits64 z0, z1;
  133. int8 negCount = ( - count ) & 63;
  134. if ( count == 0 ) {
  135. z1 = a1;
  136. z0 = a0;
  137. }
  138. else if ( count < 64 ) {
  139. z1 = ( a0<<negCount ) | ( a1>>count );
  140. z0 = a0>>count;
  141. }
  142. else {
  143. z1 = ( count < 64 ) ? ( a0>>( count & 63 ) ) : 0;
  144. z0 = 0;
  145. }
  146. *z1Ptr = z1;
  147. *z0Ptr = z0;
  148. }
  149. /*
  150. -------------------------------------------------------------------------------
  151. Shifts the 128-bit value formed by concatenating `a0' and `a1' right by the
  152. number of bits given in `count'. If any nonzero bits are shifted off, they
  153. are ``jammed'' into the least significant bit of the result by setting the
  154. least significant bit to 1. The value of `count' can be arbitrarily large;
  155. in particular, if `count' is greater than 128, the result will be either 0
  156. or 1, depending on whether the concatenation of `a0' and `a1' is zero or
  157. nonzero. The result is broken into two 64-bit pieces which are stored at
  158. the locations pointed to by `z0Ptr' and `z1Ptr'.
  159. -------------------------------------------------------------------------------
  160. */
  161. INLINE void
  162. shift128RightJamming(
  163. bits64 a0, bits64 a1, int16 count, bits64 *z0Ptr, bits64 *z1Ptr )
  164. {
  165. bits64 z0, z1;
  166. int8 negCount = ( - count ) & 63;
  167. if ( count == 0 ) {
  168. z1 = a1;
  169. z0 = a0;
  170. }
  171. else if ( count < 64 ) {
  172. z1 = ( a0<<negCount ) | ( a1>>count ) | ( ( a1<<negCount ) != 0 );
  173. z0 = a0>>count;
  174. }
  175. else {
  176. if ( count == 64 ) {
  177. z1 = a0 | ( a1 != 0 );
  178. }
  179. else if ( count < 128 ) {
  180. z1 = ( a0>>( count & 63 ) ) | ( ( ( a0<<negCount ) | a1 ) != 0 );
  181. }
  182. else {
  183. z1 = ( ( a0 | a1 ) != 0 );
  184. }
  185. z0 = 0;
  186. }
  187. *z1Ptr = z1;
  188. *z0Ptr = z0;
  189. }
  190. /*
  191. -------------------------------------------------------------------------------
  192. Shifts the 192-bit value formed by concatenating `a0', `a1', and `a2' right
  193. by 64 _plus_ the number of bits given in `count'. The shifted result is
  194. at most 128 nonzero bits; these are broken into two 64-bit pieces which are
  195. stored at the locations pointed to by `z0Ptr' and `z1Ptr'. The bits shifted
  196. off form a third 64-bit result as follows: The _last_ bit shifted off is
  197. the most-significant bit of the extra result, and the other 63 bits of the
  198. extra result are all zero if and only if _all_but_the_last_ bits shifted off
  199. were all zero. This extra result is stored in the location pointed to by
  200. `z2Ptr'. The value of `count' can be arbitrarily large.
  201. (This routine makes more sense if `a0', `a1', and `a2' are considered
  202. to form a fixed-point value with binary point between `a1' and `a2'. This
  203. fixed-point value is shifted right by the number of bits given in `count',
  204. and the integer part of the result is returned at the locations pointed to
  205. by `z0Ptr' and `z1Ptr'. The fractional part of the result may be slightly
  206. corrupted as described above, and is returned at the location pointed to by
  207. `z2Ptr'.)
  208. -------------------------------------------------------------------------------
  209. */
  210. INLINE void
  211. shift128ExtraRightJamming(
  212. bits64 a0,
  213. bits64 a1,
  214. bits64 a2,
  215. int16 count,
  216. bits64 *z0Ptr,
  217. bits64 *z1Ptr,
  218. bits64 *z2Ptr
  219. )
  220. {
  221. bits64 z0, z1, z2;
  222. int8 negCount = ( - count ) & 63;
  223. if ( count == 0 ) {
  224. z2 = a2;
  225. z1 = a1;
  226. z0 = a0;
  227. }
  228. else {
  229. if ( count < 64 ) {
  230. z2 = a1<<negCount;
  231. z1 = ( a0<<negCount ) | ( a1>>count );
  232. z0 = a0>>count;
  233. }
  234. else {
  235. if ( count == 64 ) {
  236. z2 = a1;
  237. z1 = a0;
  238. }
  239. else {
  240. a2 |= a1;
  241. if ( count < 128 ) {
  242. z2 = a0<<negCount;
  243. z1 = a0>>( count & 63 );
  244. }
  245. else {
  246. z2 = ( count == 128 ) ? a0 : ( a0 != 0 );
  247. z1 = 0;
  248. }
  249. }
  250. z0 = 0;
  251. }
  252. z2 |= ( a2 != 0 );
  253. }
  254. *z2Ptr = z2;
  255. *z1Ptr = z1;
  256. *z0Ptr = z0;
  257. }
  258. /*
  259. -------------------------------------------------------------------------------
  260. Shifts the 128-bit value formed by concatenating `a0' and `a1' left by the
  261. number of bits given in `count'. Any bits shifted off are lost. The value
  262. of `count' must be less than 64. The result is broken into two 64-bit
  263. pieces which are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
  264. -------------------------------------------------------------------------------
  265. */
  266. INLINE void
  267. shortShift128Left(
  268. bits64 a0, bits64 a1, int16 count, bits64 *z0Ptr, bits64 *z1Ptr )
  269. {
  270. *z1Ptr = a1<<count;
  271. *z0Ptr =
  272. ( count == 0 ) ? a0 : ( a0<<count ) | ( a1>>( ( - count ) & 63 ) );
  273. }
  274. /*
  275. -------------------------------------------------------------------------------
  276. Shifts the 192-bit value formed by concatenating `a0', `a1', and `a2' left
  277. by the number of bits given in `count'. Any bits shifted off are lost.
  278. The value of `count' must be less than 64. The result is broken into three
  279. 64-bit pieces which are stored at the locations pointed to by `z0Ptr',
  280. `z1Ptr', and `z2Ptr'.
  281. -------------------------------------------------------------------------------
  282. */
  283. INLINE void
  284. shortShift192Left(
  285. bits64 a0,
  286. bits64 a1,
  287. bits64 a2,
  288. int16 count,
  289. bits64 *z0Ptr,
  290. bits64 *z1Ptr,
  291. bits64 *z2Ptr
  292. )
  293. {
  294. bits64 z0, z1, z2;
  295. int8 negCount;
  296. z2 = a2<<count;
  297. z1 = a1<<count;
  298. z0 = a0<<count;
  299. if ( 0 < count ) {
  300. negCount = ( ( - count ) & 63 );
  301. z1 |= a2>>negCount;
  302. z0 |= a1>>negCount;
  303. }
  304. *z2Ptr = z2;
  305. *z1Ptr = z1;
  306. *z0Ptr = z0;
  307. }
  308. /*
  309. -------------------------------------------------------------------------------
  310. Adds the 128-bit value formed by concatenating `a0' and `a1' to the 128-bit
  311. value formed by concatenating `b0' and `b1'. Addition is modulo 2^128, so
  312. any carry out is lost. The result is broken into two 64-bit pieces which
  313. are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
  314. -------------------------------------------------------------------------------
  315. */
  316. INLINE void
  317. add128(
  318. bits64 a0, bits64 a1, bits64 b0, bits64 b1, bits64 *z0Ptr, bits64 *z1Ptr )
  319. {
  320. bits64 z1;
  321. z1 = a1 + b1;
  322. *z1Ptr = z1;
  323. *z0Ptr = a0 + b0 + ( z1 < a1 );
  324. }
  325. /*
  326. -------------------------------------------------------------------------------
  327. Adds the 192-bit value formed by concatenating `a0', `a1', and `a2' to the
  328. 192-bit value formed by concatenating `b0', `b1', and `b2'. Addition is
  329. modulo 2^192, so any carry out is lost. The result is broken into three
  330. 64-bit pieces which are stored at the locations pointed to by `z0Ptr',
  331. `z1Ptr', and `z2Ptr'.
  332. -------------------------------------------------------------------------------
  333. */
  334. INLINE void
  335. add192(
  336. bits64 a0,
  337. bits64 a1,
  338. bits64 a2,
  339. bits64 b0,
  340. bits64 b1,
  341. bits64 b2,
  342. bits64 *z0Ptr,
  343. bits64 *z1Ptr,
  344. bits64 *z2Ptr
  345. )
  346. {
  347. bits64 z0, z1, z2;
  348. int8 carry0, carry1;
  349. z2 = a2 + b2;
  350. carry1 = ( z2 < a2 );
  351. z1 = a1 + b1;
  352. carry0 = ( z1 < a1 );
  353. z0 = a0 + b0;
  354. z1 += carry1;
  355. z0 += ( z1 < carry1 );
  356. z0 += carry0;
  357. *z2Ptr = z2;
  358. *z1Ptr = z1;
  359. *z0Ptr = z0;
  360. }
  361. /*
  362. -------------------------------------------------------------------------------
  363. Subtracts the 128-bit value formed by concatenating `b0' and `b1' from the
  364. 128-bit value formed by concatenating `a0' and `a1'. Subtraction is modulo
  365. 2^128, so any borrow out (carry out) is lost. The result is broken into two
  366. 64-bit pieces which are stored at the locations pointed to by `z0Ptr' and
  367. `z1Ptr'.
  368. -------------------------------------------------------------------------------
  369. */
  370. INLINE void
  371. sub128(
  372. bits64 a0, bits64 a1, bits64 b0, bits64 b1, bits64 *z0Ptr, bits64 *z1Ptr )
  373. {
  374. *z1Ptr = a1 - b1;
  375. *z0Ptr = a0 - b0 - ( a1 < b1 );
  376. }
  377. /*
  378. -------------------------------------------------------------------------------
  379. Subtracts the 192-bit value formed by concatenating `b0', `b1', and `b2'
  380. from the 192-bit value formed by concatenating `a0', `a1', and `a2'.
  381. Subtraction is modulo 2^192, so any borrow out (carry out) is lost. The
  382. result is broken into three 64-bit pieces which are stored at the locations
  383. pointed to by `z0Ptr', `z1Ptr', and `z2Ptr'.
  384. -------------------------------------------------------------------------------
  385. */
  386. INLINE void
  387. sub192(
  388. bits64 a0,
  389. bits64 a1,
  390. bits64 a2,
  391. bits64 b0,
  392. bits64 b1,
  393. bits64 b2,
  394. bits64 *z0Ptr,
  395. bits64 *z1Ptr,
  396. bits64 *z2Ptr
  397. )
  398. {
  399. bits64 z0, z1, z2;
  400. int8 borrow0, borrow1;
  401. z2 = a2 - b2;
  402. borrow1 = ( a2 < b2 );
  403. z1 = a1 - b1;
  404. borrow0 = ( a1 < b1 );
  405. z0 = a0 - b0;
  406. z0 -= ( z1 < borrow1 );
  407. z1 -= borrow1;
  408. z0 -= borrow0;
  409. *z2Ptr = z2;
  410. *z1Ptr = z1;
  411. *z0Ptr = z0;
  412. }
  413. /*
  414. -------------------------------------------------------------------------------
  415. Multiplies `a' by `b' to obtain a 128-bit product. The product is broken
  416. into two 64-bit pieces which are stored at the locations pointed to by
  417. `z0Ptr' and `z1Ptr'.
  418. -------------------------------------------------------------------------------
  419. */
  420. INLINE void mul64To128( bits64 a, bits64 b, bits64 *z0Ptr, bits64 *z1Ptr )
  421. {
  422. bits32 aHigh, aLow, bHigh, bLow;
  423. bits64 z0, zMiddleA, zMiddleB, z1;
  424. aLow = a;
  425. aHigh = a>>32;
  426. bLow = b;
  427. bHigh = b>>32;
  428. z1 = ( (bits64) aLow ) * bLow;
  429. zMiddleA = ( (bits64) aLow ) * bHigh;
  430. zMiddleB = ( (bits64) aHigh ) * bLow;
  431. z0 = ( (bits64) aHigh ) * bHigh;
  432. zMiddleA += zMiddleB;
  433. z0 += ( ( (bits64) ( zMiddleA < zMiddleB ) )<<32 ) + ( zMiddleA>>32 );
  434. zMiddleA <<= 32;
  435. z1 += zMiddleA;
  436. z0 += ( z1 < zMiddleA );
  437. *z1Ptr = z1;
  438. *z0Ptr = z0;
  439. }
  440. /*
  441. -------------------------------------------------------------------------------
  442. Multiplies the 128-bit value formed by concatenating `a0' and `a1' by `b' to
  443. obtain a 192-bit product. The product is broken into three 64-bit pieces
  444. which are stored at the locations pointed to by `z0Ptr', `z1Ptr', and
  445. `z2Ptr'.
  446. -------------------------------------------------------------------------------
  447. */
  448. INLINE void
  449. mul128By64To192(
  450. bits64 a0,
  451. bits64 a1,
  452. bits64 b,
  453. bits64 *z0Ptr,
  454. bits64 *z1Ptr,
  455. bits64 *z2Ptr
  456. )
  457. {
  458. bits64 z0, z1, z2, more1;
  459. mul64To128( a1, b, &z1, &z2 );
  460. mul64To128( a0, b, &z0, &more1 );
  461. add128( z0, more1, 0, z1, &z0, &z1 );
  462. *z2Ptr = z2;
  463. *z1Ptr = z1;
  464. *z0Ptr = z0;
  465. }
  466. /*
  467. -------------------------------------------------------------------------------
  468. Multiplies the 128-bit value formed by concatenating `a0' and `a1' to the
  469. 128-bit value formed by concatenating `b0' and `b1' to obtain a 256-bit
  470. product. The product is broken into four 64-bit pieces which are stored at
  471. the locations pointed to by `z0Ptr', `z1Ptr', `z2Ptr', and `z3Ptr'.
  472. -------------------------------------------------------------------------------
  473. */
  474. INLINE void
  475. mul128To256(
  476. bits64 a0,
  477. bits64 a1,
  478. bits64 b0,
  479. bits64 b1,
  480. bits64 *z0Ptr,
  481. bits64 *z1Ptr,
  482. bits64 *z2Ptr,
  483. bits64 *z3Ptr
  484. )
  485. {
  486. bits64 z0, z1, z2, z3;
  487. bits64 more1, more2;
  488. mul64To128( a1, b1, &z2, &z3 );
  489. mul64To128( a1, b0, &z1, &more2 );
  490. add128( z1, more2, 0, z2, &z1, &z2 );
  491. mul64To128( a0, b0, &z0, &more1 );
  492. add128( z0, more1, 0, z1, &z0, &z1 );
  493. mul64To128( a0, b1, &more1, &more2 );
  494. add128( more1, more2, 0, z2, &more1, &z2 );
  495. add128( z0, z1, 0, more1, &z0, &z1 );
  496. *z3Ptr = z3;
  497. *z2Ptr = z2;
  498. *z1Ptr = z1;
  499. *z0Ptr = z0;
  500. }
  501. /*
  502. -------------------------------------------------------------------------------
  503. Returns an approximation to the 64-bit integer quotient obtained by dividing
  504. `b' into the 128-bit value formed by concatenating `a0' and `a1'. The
  505. divisor `b' must be at least 2^63. If q is the exact quotient truncated
  506. toward zero, the approximation returned lies between q and q + 2 inclusive.
  507. If the exact quotient q is larger than 64 bits, the maximum positive 64-bit
  508. unsigned integer is returned.
  509. -------------------------------------------------------------------------------
  510. */
  511. static bits64 estimateDiv128To64( bits64 a0, bits64 a1, bits64 b )
  512. {
  513. bits64 b0, b1;
  514. bits64 rem0, rem1, term0, term1;
  515. bits64 z;
  516. if ( b <= a0 ) return LIT64( 0xFFFFFFFFFFFFFFFF );
  517. b0 = b>>32; /* hence b0 is 32 bits wide now */
  518. if ( b0<<32 <= a0 ) {
  519. z = LIT64( 0xFFFFFFFF00000000 );
  520. } else {
  521. z = a0;
  522. do_div( z, b0 );
  523. z <<= 32;
  524. }
  525. mul64To128( b, z, &term0, &term1 );
  526. sub128( a0, a1, term0, term1, &rem0, &rem1 );
  527. while ( ( (sbits64) rem0 ) < 0 ) {
  528. z -= LIT64( 0x100000000 );
  529. b1 = b<<32;
  530. add128( rem0, rem1, b0, b1, &rem0, &rem1 );
  531. }
  532. rem0 = ( rem0<<32 ) | ( rem1>>32 );
  533. if ( b0<<32 <= rem0 ) {
  534. z |= 0xFFFFFFFF;
  535. } else {
  536. do_div( rem0, b0 );
  537. z |= rem0;
  538. }
  539. return z;
  540. }
  541. /*
  542. -------------------------------------------------------------------------------
  543. Returns an approximation to the square root of the 32-bit significand given
  544. by `a'. Considered as an integer, `a' must be at least 2^31. If bit 0 of
  545. `aExp' (the least significant bit) is 1, the integer returned approximates
  546. 2^31*sqrt(`a'/2^31), where `a' is considered an integer. If bit 0 of `aExp'
  547. is 0, the integer returned approximates 2^31*sqrt(`a'/2^30). In either
  548. case, the approximation returned lies strictly within +/-2 of the exact
  549. value.
  550. -------------------------------------------------------------------------------
  551. */
  552. static bits32 estimateSqrt32( int16 aExp, bits32 a )
  553. {
  554. static const bits16 sqrtOddAdjustments[] = {
  555. 0x0004, 0x0022, 0x005D, 0x00B1, 0x011D, 0x019F, 0x0236, 0x02E0,
  556. 0x039C, 0x0468, 0x0545, 0x0631, 0x072B, 0x0832, 0x0946, 0x0A67
  557. };
  558. static const bits16 sqrtEvenAdjustments[] = {
  559. 0x0A2D, 0x08AF, 0x075A, 0x0629, 0x051A, 0x0429, 0x0356, 0x029E,
  560. 0x0200, 0x0179, 0x0109, 0x00AF, 0x0068, 0x0034, 0x0012, 0x0002
  561. };
  562. int8 index;
  563. bits32 z;
  564. bits64 A;
  565. index = ( a>>27 ) & 15;
  566. if ( aExp & 1 ) {
  567. z = 0x4000 + ( a>>17 ) - sqrtOddAdjustments[ index ];
  568. z = ( ( a / z )<<14 ) + ( z<<15 );
  569. a >>= 1;
  570. }
  571. else {
  572. z = 0x8000 + ( a>>17 ) - sqrtEvenAdjustments[ index ];
  573. z = a / z + z;
  574. z = ( 0x20000 <= z ) ? 0xFFFF8000 : ( z<<15 );
  575. if ( z <= a ) return (bits32) ( ( (sbits32) a )>>1 );
  576. }
  577. A = ( (bits64) a )<<31;
  578. do_div( A, z );
  579. return ( (bits32) A ) + ( z>>1 );
  580. }
  581. /*
  582. -------------------------------------------------------------------------------
  583. Returns the number of leading 0 bits before the most-significant 1 bit
  584. of `a'. If `a' is zero, 32 is returned.
  585. -------------------------------------------------------------------------------
  586. */
  587. static int8 countLeadingZeros32( bits32 a )
  588. {
  589. static const int8 countLeadingZerosHigh[] = {
  590. 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
  591. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  592. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  593. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  594. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  595. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  596. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  597. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  598. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  599. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  600. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  601. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  602. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  603. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  604. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  605. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  606. };
  607. int8 shiftCount;
  608. shiftCount = 0;
  609. if ( a < 0x10000 ) {
  610. shiftCount += 16;
  611. a <<= 16;
  612. }
  613. if ( a < 0x1000000 ) {
  614. shiftCount += 8;
  615. a <<= 8;
  616. }
  617. shiftCount += countLeadingZerosHigh[ a>>24 ];
  618. return shiftCount;
  619. }
  620. /*
  621. -------------------------------------------------------------------------------
  622. Returns the number of leading 0 bits before the most-significant 1 bit
  623. of `a'. If `a' is zero, 64 is returned.
  624. -------------------------------------------------------------------------------
  625. */
  626. static int8 countLeadingZeros64( bits64 a )
  627. {
  628. int8 shiftCount;
  629. shiftCount = 0;
  630. if ( a < ( (bits64) 1 )<<32 ) {
  631. shiftCount += 32;
  632. }
  633. else {
  634. a >>= 32;
  635. }
  636. shiftCount += countLeadingZeros32( a );
  637. return shiftCount;
  638. }
  639. /*
  640. -------------------------------------------------------------------------------
  641. Returns 1 if the 128-bit value formed by concatenating `a0' and `a1'
  642. is equal to the 128-bit value formed by concatenating `b0' and `b1'.
  643. Otherwise, returns 0.
  644. -------------------------------------------------------------------------------
  645. */
  646. INLINE flag eq128( bits64 a0, bits64 a1, bits64 b0, bits64 b1 )
  647. {
  648. return ( a0 == b0 ) && ( a1 == b1 );
  649. }
  650. /*
  651. -------------------------------------------------------------------------------
  652. Returns 1 if the 128-bit value formed by concatenating `a0' and `a1' is less
  653. than or equal to the 128-bit value formed by concatenating `b0' and `b1'.
  654. Otherwise, returns 0.
  655. -------------------------------------------------------------------------------
  656. */
  657. INLINE flag le128( bits64 a0, bits64 a1, bits64 b0, bits64 b1 )
  658. {
  659. return ( a0 < b0 ) || ( ( a0 == b0 ) && ( a1 <= b1 ) );
  660. }
  661. /*
  662. -------------------------------------------------------------------------------
  663. Returns 1 if the 128-bit value formed by concatenating `a0' and `a1' is less
  664. than the 128-bit value formed by concatenating `b0' and `b1'. Otherwise,
  665. returns 0.
  666. -------------------------------------------------------------------------------
  667. */
  668. INLINE flag lt128( bits64 a0, bits64 a1, bits64 b0, bits64 b1 )
  669. {
  670. return ( a0 < b0 ) || ( ( a0 == b0 ) && ( a1 < b1 ) );
  671. }
  672. /*
  673. -------------------------------------------------------------------------------
  674. Returns 1 if the 128-bit value formed by concatenating `a0' and `a1' is
  675. not equal to the 128-bit value formed by concatenating `b0' and `b1'.
  676. Otherwise, returns 0.
  677. -------------------------------------------------------------------------------
  678. */
  679. INLINE flag ne128( bits64 a0, bits64 a1, bits64 b0, bits64 b1 )
  680. {
  681. return ( a0 != b0 ) || ( a1 != b1 );
  682. }