| 1 | |
| 2 | /*============================================================================ |
| 3 | |
| 4 | This C source fragment is part of the SoftFloat IEC/IEEE Floating-point |
| 5 | Arithmetic Package, Release 2b. |
| 6 | |
| 7 | Written by John R. Hauser. This work was made possible in part by the |
| 8 | International Computer Science Institute, located at Suite 600, 1947 Center |
| 9 | Street, Berkeley, California 94704. Funding was partially provided by the |
| 10 | National Science Foundation under grant MIP-9311980. The original version |
| 11 | of this code was written as part of a project to build a fixed-point vector |
| 12 | processor in collaboration with the University of California at Berkeley, |
| 13 | overseen by Profs. Nelson Morgan and John Wawrzynek. More information |
| 14 | is available through the Web page `http://www.cs.berkeley.edu/~jhauser/ |
| 15 | arithmetic/SoftFloat.html'. |
| 16 | |
| 17 | THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort has |
| 18 | been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT TIMES |
| 19 | RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO PERSONS |
| 20 | AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ALL LOSSES, |
| 21 | COSTS, OR OTHER PROBLEMS THEY INCUR DUE TO THE SOFTWARE, AND WHO FURTHERMORE |
| 22 | EFFECTIVELY INDEMNIFY JOHN HAUSER AND THE INTERNATIONAL COMPUTER SCIENCE |
| 23 | INSTITUTE (possibly via similar legal warning) AGAINST ALL LOSSES, COSTS, OR |
| 24 | OTHER PROBLEMS INCURRED BY THEIR CUSTOMERS AND CLIENTS DUE TO THE SOFTWARE. |
| 25 | |
| 26 | Derivative works are acceptable, even for commercial purposes, so long as |
| 27 | (1) the source code for the derivative work includes prominent notice that |
| 28 | the work is derivative, and (2) the source code includes prominent notice with |
| 29 | these four paragraphs for those parts of this code that are retained. |
| 30 | |
| 31 | =============================================================================*/ |
| 32 | |
| 33 | /*---------------------------------------------------------------------------- |
| 34 | | Shifts `a' right by the number of bits given in `count'. If any nonzero |
| 35 | | bits are shifted off, they are ``jammed'' into the least significant bit of |
| 36 | | the result by setting the least significant bit to 1. The value of `count' |
| 37 | | can be arbitrarily large; in particular, if `count' is greater than 32, the |
| 38 | | result will be either 0 or 1, depending on whether `a' is zero or nonzero. |
| 39 | | The result is stored in the location pointed to by `zPtr'. |
| 40 | *----------------------------------------------------------------------------*/ |
| 41 | |
| 42 | INLINE void shift32RightJamming( bits32 a, int16 count, bits32 *zPtr ) |
| 43 | { |
| 44 | bits32 z; |
| 45 | |
| 46 | if ( count == 0 ) { |
| 47 | z = a; |
| 48 | } |
| 49 | else if ( count < 32 ) { |
| 50 | z = ( a>>count ) | ( ( a<<( ( - count ) & 31 ) ) != 0 ); |
| 51 | } |
| 52 | else { |
| 53 | z = ( a != 0 ); |
| 54 | } |
| 55 | *zPtr = z; |
| 56 | |
| 57 | } |
| 58 | |
| 59 | /*---------------------------------------------------------------------------- |
| 60 | | Shifts the 64-bit value formed by concatenating `a0' and `a1' right by the |
| 61 | | number of bits given in `count'. Any bits shifted off are lost. The value |
| 62 | | of `count' can be arbitrarily large; in particular, if `count' is greater |
| 63 | | than 64, the result will be 0. The result is broken into two 32-bit pieces |
| 64 | | which are stored at the locations pointed to by `z0Ptr' and `z1Ptr'. |
| 65 | *----------------------------------------------------------------------------*/ |
| 66 | |
| 67 | INLINE void |
| 68 | shift64Right( |
| 69 | bits32 a0, bits32 a1, int16 count, bits32 *z0Ptr, bits32 *z1Ptr ) |
| 70 | { |
| 71 | bits32 z0, z1; |
| 72 | int8 negCount = ( - count ) & 31; |
| 73 | |
| 74 | if ( count == 0 ) { |
| 75 | z1 = a1; |
| 76 | z0 = a0; |
| 77 | } |
| 78 | else if ( count < 32 ) { |
| 79 | z1 = ( a0<<negCount ) | ( a1>>count ); |
| 80 | z0 = a0>>count; |
| 81 | } |
| 82 | else { |
| 83 | z1 = ( count < 64 ) ? ( a0>>( count & 31 ) ) : 0; |
| 84 | z0 = 0; |
| 85 | } |
| 86 | *z1Ptr = z1; |
| 87 | *z0Ptr = z0; |
| 88 | |
| 89 | } |
| 90 | |
| 91 | /*---------------------------------------------------------------------------- |
| 92 | | Shifts the 64-bit value formed by concatenating `a0' and `a1' right by the |
| 93 | | number of bits given in `count'. If any nonzero bits are shifted off, they |
| 94 | | are ``jammed'' into the least significant bit of the result by setting the |
| 95 | | least significant bit to 1. The value of `count' can be arbitrarily large; |
| 96 | | in particular, if `count' is greater than 64, the result will be either 0 |
| 97 | | or 1, depending on whether the concatenation of `a0' and `a1' is zero or |
| 98 | | nonzero. The result is broken into two 32-bit pieces which are stored at |
| 99 | | the locations pointed to by `z0Ptr' and `z1Ptr'. |
| 100 | *----------------------------------------------------------------------------*/ |
| 101 | |
| 102 | INLINE void |
| 103 | shift64RightJamming( |
| 104 | bits32 a0, bits32 a1, int16 count, bits32 *z0Ptr, bits32 *z1Ptr ) |
| 105 | { |
| 106 | bits32 z0, z1; |
| 107 | int8 negCount = ( - count ) & 31; |
| 108 | |
| 109 | if ( count == 0 ) { |
| 110 | z1 = a1; |
| 111 | z0 = a0; |
| 112 | } |
| 113 | else if ( count < 32 ) { |
| 114 | z1 = ( a0<<negCount ) | ( a1>>count ) | ( ( a1<<negCount ) != 0 ); |
| 115 | z0 = a0>>count; |
| 116 | } |
| 117 | else { |
| 118 | if ( count == 32 ) { |
| 119 | z1 = a0 | ( a1 != 0 ); |
| 120 | } |
| 121 | else if ( count < 64 ) { |
| 122 | z1 = ( a0>>( count & 31 ) ) | ( ( ( a0<<negCount ) | a1 ) != 0 ); |
| 123 | } |
| 124 | else { |
| 125 | z1 = ( ( a0 | a1 ) != 0 ); |
| 126 | } |
| 127 | z0 = 0; |
| 128 | } |
| 129 | *z1Ptr = z1; |
| 130 | *z0Ptr = z0; |
| 131 | |
| 132 | } |
| 133 | |
| 134 | /*---------------------------------------------------------------------------- |
| 135 | | Shifts the 96-bit value formed by concatenating `a0', `a1', and `a2' right |
| 136 | | by 32 _plus_ the number of bits given in `count'. The shifted result is |
| 137 | | at most 64 nonzero bits; these are broken into two 32-bit pieces which are |
| 138 | | stored at the locations pointed to by `z0Ptr' and `z1Ptr'. The bits shifted |
| 139 | | off form a third 32-bit result as follows: The _last_ bit shifted off is |
| 140 | | the most-significant bit of the extra result, and the other 31 bits of the |
| 141 | | extra result are all zero if and only if _all_but_the_last_ bits shifted off |
| 142 | | were all zero. This extra result is stored in the location pointed to by |
| 143 | | `z2Ptr'. The value of `count' can be arbitrarily large. |
| 144 | | (This routine makes more sense if `a0', `a1', and `a2' are considered |
| 145 | | to form a fixed-point value with binary point between `a1' and `a2'. This |
| 146 | | fixed-point value is shifted right by the number of bits given in `count', |
| 147 | | and the integer part of the result is returned at the locations pointed to |
| 148 | | by `z0Ptr' and `z1Ptr'. The fractional part of the result may be slightly |
| 149 | | corrupted as described above, and is returned at the location pointed to by |
| 150 | | `z2Ptr'.) |
| 151 | *----------------------------------------------------------------------------*/ |
| 152 | |
| 153 | INLINE void |
| 154 | shift64ExtraRightJamming( |
| 155 | bits32 a0, |
| 156 | bits32 a1, |
| 157 | bits32 a2, |
| 158 | int16 count, |
| 159 | bits32 *z0Ptr, |
| 160 | bits32 *z1Ptr, |
| 161 | bits32 *z2Ptr |
| 162 | ) |
| 163 | { |
| 164 | bits32 z0, z1, z2; |
| 165 | int8 negCount = ( - count ) & 31; |
| 166 | |
| 167 | if ( count == 0 ) { |
| 168 | z2 = a2; |
| 169 | z1 = a1; |
| 170 | z0 = a0; |
| 171 | } |
| 172 | else { |
| 173 | if ( count < 32 ) { |
| 174 | z2 = a1<<negCount; |
| 175 | z1 = ( a0<<negCount ) | ( a1>>count ); |
| 176 | z0 = a0>>count; |
| 177 | } |
| 178 | else { |
| 179 | if ( count == 32 ) { |
| 180 | z2 = a1; |
| 181 | z1 = a0; |
| 182 | } |
| 183 | else { |
| 184 | a2 |= a1; |
| 185 | if ( count < 64 ) { |
| 186 | z2 = a0<<negCount; |
| 187 | z1 = a0>>( count & 31 ); |
| 188 | } |
| 189 | else { |
| 190 | z2 = ( count == 64 ) ? a0 : ( a0 != 0 ); |
| 191 | z1 = 0; |
| 192 | } |
| 193 | } |
| 194 | z0 = 0; |
| 195 | } |
| 196 | z2 |= ( a2 != 0 ); |
| 197 | } |
| 198 | *z2Ptr = z2; |
| 199 | *z1Ptr = z1; |
| 200 | *z0Ptr = z0; |
| 201 | |
| 202 | } |
| 203 | |
| 204 | /*---------------------------------------------------------------------------- |
| 205 | | Shifts the 64-bit value formed by concatenating `a0' and `a1' left by the |
| 206 | | number of bits given in `count'. Any bits shifted off are lost. The value |
| 207 | | of `count' must be less than 32. The result is broken into two 32-bit |
| 208 | | pieces which are stored at the locations pointed to by `z0Ptr' and `z1Ptr'. |
| 209 | *----------------------------------------------------------------------------*/ |
| 210 | |
| 211 | INLINE void |
| 212 | shortShift64Left( |
| 213 | bits32 a0, bits32 a1, int16 count, bits32 *z0Ptr, bits32 *z1Ptr ) |
| 214 | { |
| 215 | |
| 216 | *z1Ptr = a1<<count; |
| 217 | *z0Ptr = |
| 218 | ( count == 0 ) ? a0 : ( a0<<count ) | ( a1>>( ( - count ) & 31 ) ); |
| 219 | |
| 220 | } |
| 221 | |
| 222 | /*---------------------------------------------------------------------------- |
| 223 | | Shifts the 96-bit value formed by concatenating `a0', `a1', and `a2' left |
| 224 | | by the number of bits given in `count'. Any bits shifted off are lost. |
| 225 | | The value of `count' must be less than 32. The result is broken into three |
| 226 | | 32-bit pieces which are stored at the locations pointed to by `z0Ptr', |
| 227 | | `z1Ptr', and `z2Ptr'. |
| 228 | *----------------------------------------------------------------------------*/ |
| 229 | |
| 230 | INLINE void |
| 231 | shortShift96Left( |
| 232 | bits32 a0, |
| 233 | bits32 a1, |
| 234 | bits32 a2, |
| 235 | int16 count, |
| 236 | bits32 *z0Ptr, |
| 237 | bits32 *z1Ptr, |
| 238 | bits32 *z2Ptr |
| 239 | ) |
| 240 | { |
| 241 | bits32 z0, z1, z2; |
| 242 | int8 negCount; |
| 243 | |
| 244 | z2 = a2<<count; |
| 245 | z1 = a1<<count; |
| 246 | z0 = a0<<count; |
| 247 | if ( 0 < count ) { |
| 248 | negCount = ( ( - count ) & 31 ); |
| 249 | z1 |= a2>>negCount; |
| 250 | z0 |= a1>>negCount; |
| 251 | } |
| 252 | *z2Ptr = z2; |
| 253 | *z1Ptr = z1; |
| 254 | *z0Ptr = z0; |
| 255 | |
| 256 | } |
| 257 | |
| 258 | /*---------------------------------------------------------------------------- |
| 259 | | Adds the 64-bit value formed by concatenating `a0' and `a1' to the 64-bit |
| 260 | | value formed by concatenating `b0' and `b1'. Addition is modulo 2^64, so |
| 261 | | any carry out is lost. The result is broken into two 32-bit pieces which |
| 262 | | are stored at the locations pointed to by `z0Ptr' and `z1Ptr'. |
| 263 | *----------------------------------------------------------------------------*/ |
| 264 | |
| 265 | INLINE void |
| 266 | add64( |
| 267 | bits32 a0, bits32 a1, bits32 b0, bits32 b1, bits32 *z0Ptr, bits32 *z1Ptr ) |
| 268 | { |
| 269 | bits32 z1; |
| 270 | |
| 271 | z1 = a1 + b1; |
| 272 | *z1Ptr = z1; |
| 273 | *z0Ptr = a0 + b0 + ( z1 < a1 ); |
| 274 | |
| 275 | } |
| 276 | |
| 277 | /*---------------------------------------------------------------------------- |
| 278 | | Adds the 96-bit value formed by concatenating `a0', `a1', and `a2' to the |
| 279 | | 96-bit value formed by concatenating `b0', `b1', and `b2'. Addition is |
| 280 | | modulo 2^96, so any carry out is lost. The result is broken into three |
| 281 | | 32-bit pieces which are stored at the locations pointed to by `z0Ptr', |
| 282 | | `z1Ptr', and `z2Ptr'. |
| 283 | *----------------------------------------------------------------------------*/ |
| 284 | |
| 285 | INLINE void |
| 286 | add96( |
| 287 | bits32 a0, |
| 288 | bits32 a1, |
| 289 | bits32 a2, |
| 290 | bits32 b0, |
| 291 | bits32 b1, |
| 292 | bits32 b2, |
| 293 | bits32 *z0Ptr, |
| 294 | bits32 *z1Ptr, |
| 295 | bits32 *z2Ptr |
| 296 | ) |
| 297 | { |
| 298 | bits32 z0, z1, z2; |
| 299 | int8 carry0, carry1; |
| 300 | |
| 301 | z2 = a2 + b2; |
| 302 | carry1 = ( z2 < a2 ); |
| 303 | z1 = a1 + b1; |
| 304 | carry0 = ( z1 < a1 ); |
| 305 | z0 = a0 + b0; |
| 306 | z1 += carry1; |
| 307 | z0 += ( z1 < carry1 ); |
| 308 | z0 += carry0; |
| 309 | *z2Ptr = z2; |
| 310 | *z1Ptr = z1; |
| 311 | *z0Ptr = z0; |
| 312 | |
| 313 | } |
| 314 | |
| 315 | /*---------------------------------------------------------------------------- |
| 316 | | Subtracts the 64-bit value formed by concatenating `b0' and `b1' from the |
| 317 | | 64-bit value formed by concatenating `a0' and `a1'. Subtraction is modulo |
| 318 | | 2^64, so any borrow out (carry out) is lost. The result is broken into two |
| 319 | | 32-bit pieces which are stored at the locations pointed to by `z0Ptr' and |
| 320 | | `z1Ptr'. |
| 321 | *----------------------------------------------------------------------------*/ |
| 322 | |
| 323 | INLINE void |
| 324 | sub64( |
| 325 | bits32 a0, bits32 a1, bits32 b0, bits32 b1, bits32 *z0Ptr, bits32 *z1Ptr ) |
| 326 | { |
| 327 | |
| 328 | *z1Ptr = a1 - b1; |
| 329 | *z0Ptr = a0 - b0 - ( a1 < b1 ); |
| 330 | |
| 331 | } |
| 332 | |
| 333 | /*---------------------------------------------------------------------------- |
| 334 | | Subtracts the 96-bit value formed by concatenating `b0', `b1', and `b2' from |
| 335 | | the 96-bit value formed by concatenating `a0', `a1', and `a2'. Subtraction |
| 336 | | is modulo 2^96, so any borrow out (carry out) is lost. The result is broken |
| 337 | | into three 32-bit pieces which are stored at the locations pointed to by |
| 338 | | `z0Ptr', `z1Ptr', and `z2Ptr'. |
| 339 | *----------------------------------------------------------------------------*/ |
| 340 | |
| 341 | INLINE void |
| 342 | sub96( |
| 343 | bits32 a0, |
| 344 | bits32 a1, |
| 345 | bits32 a2, |
| 346 | bits32 b0, |
| 347 | bits32 b1, |
| 348 | bits32 b2, |
| 349 | bits32 *z0Ptr, |
| 350 | bits32 *z1Ptr, |
| 351 | bits32 *z2Ptr |
| 352 | ) |
| 353 | { |
| 354 | bits32 z0, z1, z2; |
| 355 | int8 borrow0, borrow1; |
| 356 | |
| 357 | z2 = a2 - b2; |
| 358 | borrow1 = ( a2 < b2 ); |
| 359 | z1 = a1 - b1; |
| 360 | borrow0 = ( a1 < b1 ); |
| 361 | z0 = a0 - b0; |
| 362 | z0 -= ( z1 < borrow1 ); |
| 363 | z1 -= borrow1; |
| 364 | z0 -= borrow0; |
| 365 | *z2Ptr = z2; |
| 366 | *z1Ptr = z1; |
| 367 | *z0Ptr = z0; |
| 368 | |
| 369 | } |
| 370 | |
| 371 | /*---------------------------------------------------------------------------- |
| 372 | | Multiplies `a' by `b' to obtain a 64-bit product. The product is broken |
| 373 | | into two 32-bit pieces which are stored at the locations pointed to by |
| 374 | | `z0Ptr' and `z1Ptr'. |
| 375 | *----------------------------------------------------------------------------*/ |
| 376 | |
| 377 | INLINE void mul32To64( bits32 a, bits32 b, bits32 *z0Ptr, bits32 *z1Ptr ) |
| 378 | { |
| 379 | bits16 aHigh, aLow, bHigh, bLow; |
| 380 | bits32 z0, zMiddleA, zMiddleB, z1; |
| 381 | |
| 382 | aLow = a; |
| 383 | aHigh = a>>16; |
| 384 | bLow = b; |
| 385 | bHigh = b>>16; |
| 386 | z1 = ( (bits32) aLow ) * bLow; |
| 387 | zMiddleA = ( (bits32) aLow ) * bHigh; |
| 388 | zMiddleB = ( (bits32) aHigh ) * bLow; |
| 389 | z0 = ( (bits32) aHigh ) * bHigh; |
| 390 | zMiddleA += zMiddleB; |
| 391 | z0 += ( ( (bits32) ( zMiddleA < zMiddleB ) )<<16 ) + ( zMiddleA>>16 ); |
| 392 | zMiddleA <<= 16; |
| 393 | z1 += zMiddleA; |
| 394 | z0 += ( z1 < zMiddleA ); |
| 395 | *z1Ptr = z1; |
| 396 | *z0Ptr = z0; |
| 397 | |
| 398 | } |
| 399 | |
| 400 | /*---------------------------------------------------------------------------- |
| 401 | | Multiplies the 64-bit value formed by concatenating `a0' and `a1' by `b' |
| 402 | | to obtain a 96-bit product. The product is broken into three 32-bit pieces |
| 403 | | which are stored at the locations pointed to by `z0Ptr', `z1Ptr', and |
| 404 | | `z2Ptr'. |
| 405 | *----------------------------------------------------------------------------*/ |
| 406 | |
| 407 | INLINE void |
| 408 | mul64By32To96( |
| 409 | bits32 a0, |
| 410 | bits32 a1, |
| 411 | bits32 b, |
| 412 | bits32 *z0Ptr, |
| 413 | bits32 *z1Ptr, |
| 414 | bits32 *z2Ptr |
| 415 | ) |
| 416 | { |
| 417 | bits32 z0, z1, z2, more1; |
| 418 | |
| 419 | mul32To64( a1, b, &z1, &z2 ); |
| 420 | mul32To64( a0, b, &z0, &more1 ); |
| 421 | add64( z0, more1, 0, z1, &z0, &z1 ); |
| 422 | *z2Ptr = z2; |
| 423 | *z1Ptr = z1; |
| 424 | *z0Ptr = z0; |
| 425 | |
| 426 | } |
| 427 | |
| 428 | /*---------------------------------------------------------------------------- |
| 429 | | Multiplies the 64-bit value formed by concatenating `a0' and `a1' to the |
| 430 | | 64-bit value formed by concatenating `b0' and `b1' to obtain a 128-bit |
| 431 | | product. The product is broken into four 32-bit pieces which are stored at |
| 432 | | the locations pointed to by `z0Ptr', `z1Ptr', `z2Ptr', and `z3Ptr'. |
| 433 | *----------------------------------------------------------------------------*/ |
| 434 | |
| 435 | INLINE void |
| 436 | mul64To128( |
| 437 | bits32 a0, |
| 438 | bits32 a1, |
| 439 | bits32 b0, |
| 440 | bits32 b1, |
| 441 | bits32 *z0Ptr, |
| 442 | bits32 *z1Ptr, |
| 443 | bits32 *z2Ptr, |
| 444 | bits32 *z3Ptr |
| 445 | ) |
| 446 | { |
| 447 | bits32 z0, z1, z2, z3; |
| 448 | bits32 more1, more2; |
| 449 | |
| 450 | mul32To64( a1, b1, &z2, &z3 ); |
| 451 | mul32To64( a1, b0, &z1, &more2 ); |
| 452 | add64( z1, more2, 0, z2, &z1, &z2 ); |
| 453 | mul32To64( a0, b0, &z0, &more1 ); |
| 454 | add64( z0, more1, 0, z1, &z0, &z1 ); |
| 455 | mul32To64( a0, b1, &more1, &more2 ); |
| 456 | add64( more1, more2, 0, z2, &more1, &z2 ); |
| 457 | add64( z0, z1, 0, more1, &z0, &z1 ); |
| 458 | *z3Ptr = z3; |
| 459 | *z2Ptr = z2; |
| 460 | *z1Ptr = z1; |
| 461 | *z0Ptr = z0; |
| 462 | |
| 463 | } |
| 464 | |
| 465 | /*---------------------------------------------------------------------------- |
| 466 | | Returns an approximation to the 32-bit integer quotient obtained by dividing |
| 467 | | `b' into the 64-bit value formed by concatenating `a0' and `a1'. The |
| 468 | | divisor `b' must be at least 2^31. If q is the exact quotient truncated |
| 469 | | toward zero, the approximation returned lies between q and q + 2 inclusive. |
| 470 | | If the exact quotient q is larger than 32 bits, the maximum positive 32-bit |
| 471 | | unsigned integer is returned. |
| 472 | *----------------------------------------------------------------------------*/ |
| 473 | |
| 474 | static bits32 estimateDiv64To32( bits32 a0, bits32 a1, bits32 b ) |
| 475 | { |
| 476 | bits32 b0, b1; |
| 477 | bits32 rem0, rem1, term0, term1; |
| 478 | bits32 z; |
| 479 | |
| 480 | if ( b <= a0 ) return 0xFFFFFFFF; |
| 481 | b0 = b>>16; |
| 482 | z = ( b0<<16 <= a0 ) ? 0xFFFF0000 : ( a0 / b0 )<<16; |
| 483 | mul32To64( b, z, &term0, &term1 ); |
| 484 | sub64( a0, a1, term0, term1, &rem0, &rem1 ); |
| 485 | while ( ( (sbits32) rem0 ) < 0 ) { |
| 486 | z -= 0x10000; |
| 487 | b1 = b<<16; |
| 488 | add64( rem0, rem1, b0, b1, &rem0, &rem1 ); |
| 489 | } |
| 490 | rem0 = ( rem0<<16 ) | ( rem1>>16 ); |
| 491 | z |= ( b0<<16 <= rem0 ) ? 0xFFFF : rem0 / b0; |
| 492 | return z; |
| 493 | |
| 494 | } |
| 495 | |
| 496 | /*---------------------------------------------------------------------------- |
| 497 | | Returns an approximation to the square root of the 32-bit significand given |
| 498 | | by `a'. Considered as an integer, `a' must be at least 2^31. If bit 0 of |
| 499 | | `aExp' (the least significant bit) is 1, the integer returned approximates |
| 500 | | 2^31*sqrt(`a'/2^31), where `a' is considered an integer. If bit 0 of `aExp' |
| 501 | | is 0, the integer returned approximates 2^31*sqrt(`a'/2^30). In either |
| 502 | | case, the approximation returned lies strictly within +/-2 of the exact |
| 503 | | value. |
| 504 | *----------------------------------------------------------------------------*/ |
| 505 | |
| 506 | static bits32 estimateSqrt32( int16 aExp, bits32 a ) |
| 507 | { |
| 508 | static const bits16 sqrtOddAdjustments[] = { |
| 509 | 0x0004, 0x0022, 0x005D, 0x00B1, 0x011D, 0x019F, 0x0236, 0x02E0, |
| 510 | 0x039C, 0x0468, 0x0545, 0x0631, 0x072B, 0x0832, 0x0946, 0x0A67 |
| 511 | }; |
| 512 | static const bits16 sqrtEvenAdjustments[] = { |
| 513 | 0x0A2D, 0x08AF, 0x075A, 0x0629, 0x051A, 0x0429, 0x0356, 0x029E, |
| 514 | 0x0200, 0x0179, 0x0109, 0x00AF, 0x0068, 0x0034, 0x0012, 0x0002 |
| 515 | }; |
| 516 | int8 index; |
| 517 | bits32 z; |
| 518 | |
| 519 | index = ( a>>27 ) & 15; |
| 520 | if ( aExp & 1 ) { |
| 521 | z = 0x4000 + ( a>>17 ) - sqrtOddAdjustments[ index ]; |
| 522 | z = ( ( a / z )<<14 ) + ( z<<15 ); |
| 523 | a >>= 1; |
| 524 | } |
| 525 | else { |
| 526 | z = 0x8000 + ( a>>17 ) - sqrtEvenAdjustments[ index ]; |
| 527 | z = a / z + z; |
| 528 | z = ( 0x20000 <= z ) ? 0xFFFF8000 : ( z<<15 ); |
| 529 | if ( z <= a ) return (bits32) ( ( (sbits32) a )>>1 ); |
| 530 | } |
| 531 | return ( ( estimateDiv64To32( a, 0, z ) )>>1 ) + ( z>>1 ); |
| 532 | |
| 533 | } |
| 534 | |
| 535 | /*---------------------------------------------------------------------------- |
| 536 | | Returns the number of leading 0 bits before the most-significant 1 bit of |
| 537 | | `a'. If `a' is zero, 32 is returned. |
| 538 | *----------------------------------------------------------------------------*/ |
| 539 | |
| 540 | static int8 countLeadingZeros32( bits32 a ) |
| 541 | { |
| 542 | static const int8 countLeadingZerosHigh[] = { |
| 543 | 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, |
| 544 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 545 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 546 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 547 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 548 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 549 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 550 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 551 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 552 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 553 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 554 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 555 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 556 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 557 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 558 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 559 | }; |
| 560 | int8 shiftCount; |
| 561 | |
| 562 | shiftCount = 0; |
| 563 | if ( a < 0x10000 ) { |
| 564 | shiftCount += 16; |
| 565 | a <<= 16; |
| 566 | } |
| 567 | if ( a < 0x1000000 ) { |
| 568 | shiftCount += 8; |
| 569 | a <<= 8; |
| 570 | } |
| 571 | shiftCount += countLeadingZerosHigh[ a>>24 ]; |
| 572 | return shiftCount; |
| 573 | |
| 574 | } |
| 575 | |
| 576 | /*---------------------------------------------------------------------------- |
| 577 | | Returns 1 if the 64-bit value formed by concatenating `a0' and `a1' is |
| 578 | | equal to the 64-bit value formed by concatenating `b0' and `b1'. Otherwise, |
| 579 | | returns 0. |
| 580 | *----------------------------------------------------------------------------*/ |
| 581 | |
| 582 | INLINE flag eq64( bits32 a0, bits32 a1, bits32 b0, bits32 b1 ) |
| 583 | { |
| 584 | |
| 585 | return ( a0 == b0 ) && ( a1 == b1 ); |
| 586 | |
| 587 | } |
| 588 | |
| 589 | /*---------------------------------------------------------------------------- |
| 590 | | Returns 1 if the 64-bit value formed by concatenating `a0' and `a1' is less |
| 591 | | than or equal to the 64-bit value formed by concatenating `b0' and `b1'. |
| 592 | | Otherwise, returns 0. |
| 593 | *----------------------------------------------------------------------------*/ |
| 594 | |
| 595 | INLINE flag le64( bits32 a0, bits32 a1, bits32 b0, bits32 b1 ) |
| 596 | { |
| 597 | |
| 598 | return ( a0 < b0 ) || ( ( a0 == b0 ) && ( a1 <= b1 ) ); |
| 599 | |
| 600 | } |
| 601 | |
| 602 | /*---------------------------------------------------------------------------- |
| 603 | | Returns 1 if the 64-bit value formed by concatenating `a0' and `a1' is less |
| 604 | | than the 64-bit value formed by concatenating `b0' and `b1'. Otherwise, |
| 605 | | returns 0. |
| 606 | *----------------------------------------------------------------------------*/ |
| 607 | |
| 608 | INLINE flag lt64( bits32 a0, bits32 a1, bits32 b0, bits32 b1 ) |
| 609 | { |
| 610 | |
| 611 | return ( a0 < b0 ) || ( ( a0 == b0 ) && ( a1 < b1 ) ); |
| 612 | |
| 613 | } |
| 614 | |
| 615 | /*---------------------------------------------------------------------------- |
| 616 | | Returns 1 if the 64-bit value formed by concatenating `a0' and `a1' is not |
| 617 | | equal to the 64-bit value formed by concatenating `b0' and `b1'. Otherwise, |
| 618 | | returns 0. |
| 619 | *----------------------------------------------------------------------------*/ |
| 620 | |
| 621 | INLINE flag ne64( bits32 a0, bits32 a1, bits32 b0, bits32 b1 ) |
| 622 | { |
| 623 | |
| 624 | return ( a0 != b0 ) || ( a1 != b1 ); |
| 625 | |
| 626 | } |
| 627 |