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