glsl: Add utility function to round and pack uint64_t value
[mesa.git] / src / compiler / glsl / float64.glsl
1 /*
2 * The implementations contained in this file are heavily based on the
3 * implementations found in the Berkeley SoftFloat library. As such, they are
4 * licensed under the same 3-clause BSD license:
5 *
6 * License for Berkeley SoftFloat Release 3e
7 *
8 * John R. Hauser
9 * 2018 January 20
10 *
11 * The following applies to the whole of SoftFloat Release 3e as well as to
12 * each source file individually.
13 *
14 * Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 The Regents of the
15 * University of California. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are met:
19 *
20 * 1. Redistributions of source code must retain the above copyright notice,
21 * this list of conditions, and the following disclaimer.
22 *
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions, and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * 3. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
32 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
34 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
35 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
42
43 #version 430
44 #extension GL_ARB_gpu_shader_int64 : enable
45 #extension GL_ARB_shader_bit_encoding : enable
46 #extension GL_EXT_shader_integer_mix : enable
47 #extension GL_MESA_shader_integer_functions : enable
48
49 #pragma warning(off)
50
51 /* Software IEEE floating-point rounding mode.
52 * GLSL spec section "4.7.1 Range and Precision":
53 * The rounding mode cannot be set and is undefined.
54 * But here, we are able to define the rounding mode at the compilation time.
55 */
56 #define FLOAT_ROUND_NEAREST_EVEN 0
57 #define FLOAT_ROUND_TO_ZERO 1
58 #define FLOAT_ROUND_DOWN 2
59 #define FLOAT_ROUND_UP 3
60 #define FLOAT_ROUNDING_MODE FLOAT_ROUND_NEAREST_EVEN
61
62 /* Absolute value of a Float64 :
63 * Clear the sign bit
64 */
65 uint64_t
66 __fabs64(uint64_t __a)
67 {
68 uvec2 a = unpackUint2x32(__a);
69 a.y &= 0x7FFFFFFFu;
70 return packUint2x32(a);
71 }
72
73 /* Returns 1 if the double-precision floating-point value `a' is a NaN;
74 * otherwise returns 0.
75 */
76 bool
77 __is_nan(uint64_t __a)
78 {
79 uvec2 a = unpackUint2x32(__a);
80 return (0xFFE00000u <= (a.y<<1)) &&
81 ((a.x != 0u) || ((a.y & 0x000FFFFFu) != 0u));
82 }
83
84 /* Negate value of a Float64 :
85 * Toggle the sign bit
86 */
87 uint64_t
88 __fneg64(uint64_t __a)
89 {
90 uvec2 a = unpackUint2x32(__a);
91 uint t = a.y;
92
93 t ^= (1u << 31);
94 a.y = mix(t, a.y, __is_nan(__a));
95 return packUint2x32(a);
96 }
97
98 uint64_t
99 __fsign64(uint64_t __a)
100 {
101 uvec2 a = unpackUint2x32(__a);
102 uvec2 retval;
103 retval.x = 0u;
104 retval.y = mix((a.y & 0x80000000u) | 0x3FF00000u, 0u, (a.y << 1 | a.x) == 0u);
105 return packUint2x32(retval);
106 }
107
108 /* Returns the fraction bits of the double-precision floating-point value `a'.*/
109 uint
110 __extractFloat64FracLo(uint64_t a)
111 {
112 return unpackUint2x32(a).x;
113 }
114
115 uint
116 __extractFloat64FracHi(uint64_t a)
117 {
118 return unpackUint2x32(a).y & 0x000FFFFFu;
119 }
120
121 /* Returns the exponent bits of the double-precision floating-point value `a'.*/
122 int
123 __extractFloat64Exp(uint64_t __a)
124 {
125 uvec2 a = unpackUint2x32(__a);
126 return int((a.y>>20) & 0x7FFu);
127 }
128
129 bool
130 __feq64_nonnan(uint64_t __a, uint64_t __b)
131 {
132 uvec2 a = unpackUint2x32(__a);
133 uvec2 b = unpackUint2x32(__b);
134 return (a.x == b.x) &&
135 ((a.y == b.y) || ((a.x == 0u) && (((a.y | b.y)<<1) == 0u)));
136 }
137
138 /* Returns true if the double-precision floating-point value `a' is equal to the
139 * corresponding value `b', and false otherwise. The comparison is performed
140 * according to the IEEE Standard for Floating-Point Arithmetic.
141 */
142 bool
143 __feq64(uint64_t a, uint64_t b)
144 {
145 if (__is_nan(a) || __is_nan(b))
146 return false;
147
148 return __feq64_nonnan(a, b);
149 }
150
151 /* Returns true if the double-precision floating-point value `a' is not equal
152 * to the corresponding value `b', and false otherwise. The comparison is
153 * performed according to the IEEE Standard for Floating-Point Arithmetic.
154 */
155 bool
156 __fne64(uint64_t a, uint64_t b)
157 {
158 if (__is_nan(a) || __is_nan(b))
159 return true;
160
161 return !__feq64_nonnan(a, b);
162 }
163
164 /* Returns the sign bit of the double-precision floating-point value `a'.*/
165 uint
166 __extractFloat64Sign(uint64_t a)
167 {
168 return unpackUint2x32(a).y >> 31;
169 }
170
171 /* Returns true if the 64-bit value formed by concatenating `a0' and `a1' is less
172 * than the 64-bit value formed by concatenating `b0' and `b1'. Otherwise,
173 * returns false.
174 */
175 bool
176 lt64(uint a0, uint a1, uint b0, uint b1)
177 {
178 return (a0 < b0) || ((a0 == b0) && (a1 < b1));
179 }
180
181 bool
182 __flt64_nonnan(uint64_t __a, uint64_t __b)
183 {
184 uvec2 a = unpackUint2x32(__a);
185 uvec2 b = unpackUint2x32(__b);
186 uint aSign = __extractFloat64Sign(__a);
187 uint bSign = __extractFloat64Sign(__b);
188 if (aSign != bSign)
189 return (aSign != 0u) && ((((a.y | b.y)<<1) | a.x | b.x) != 0u);
190
191 return mix(lt64(a.y, a.x, b.y, b.x), lt64(b.y, b.x, a.y, a.x), aSign != 0u);
192 }
193
194 /* Returns true if the double-precision floating-point value `a' is less than
195 * the corresponding value `b', and false otherwise. The comparison is performed
196 * according to the IEEE Standard for Floating-Point Arithmetic.
197 */
198 bool
199 __flt64(uint64_t a, uint64_t b)
200 {
201 if (__is_nan(a) || __is_nan(b))
202 return false;
203
204 return __flt64_nonnan(a, b);
205 }
206
207 /* Returns true if the double-precision floating-point value `a' is greater
208 * than or equal to * the corresponding value `b', and false otherwise. The
209 * comparison is performed * according to the IEEE Standard for Floating-Point
210 * Arithmetic.
211 */
212 bool
213 __fge64(uint64_t a, uint64_t b)
214 {
215 if (__is_nan(a) || __is_nan(b))
216 return false;
217
218 return !__flt64_nonnan(a, b);
219 }
220
221 /* Adds the 64-bit value formed by concatenating `a0' and `a1' to the 64-bit
222 * value formed by concatenating `b0' and `b1'. Addition is modulo 2^64, so
223 * any carry out is lost. The result is broken into two 32-bit pieces which
224 * are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
225 */
226 void
227 __add64(uint a0, uint a1, uint b0, uint b1,
228 out uint z0Ptr,
229 out uint z1Ptr)
230 {
231 uint z1 = a1 + b1;
232 z1Ptr = z1;
233 z0Ptr = a0 + b0 + uint(z1 < a1);
234 }
235
236
237 /* Subtracts the 64-bit value formed by concatenating `b0' and `b1' from the
238 * 64-bit value formed by concatenating `a0' and `a1'. Subtraction is modulo
239 * 2^64, so any borrow out (carry out) is lost. The result is broken into two
240 * 32-bit pieces which are stored at the locations pointed to by `z0Ptr' and
241 * `z1Ptr'.
242 */
243 void
244 __sub64(uint a0, uint a1, uint b0, uint b1,
245 out uint z0Ptr,
246 out uint z1Ptr)
247 {
248 z1Ptr = a1 - b1;
249 z0Ptr = a0 - b0 - uint(a1 < b1);
250 }
251
252 /* Shifts the 64-bit value formed by concatenating `a0' and `a1' right by the
253 * number of bits given in `count'. If any nonzero bits are shifted off, they
254 * are "jammed" into the least significant bit of the result by setting the
255 * least significant bit to 1. The value of `count' can be arbitrarily large;
256 * in particular, if `count' is greater than 64, the result will be either 0
257 * or 1, depending on whether the concatenation of `a0' and `a1' is zero or
258 * nonzero. The result is broken into two 32-bit pieces which are stored at
259 * the locations pointed to by `z0Ptr' and `z1Ptr'.
260 */
261 void
262 __shift64RightJamming(uint a0,
263 uint a1,
264 int count,
265 out uint z0Ptr,
266 out uint z1Ptr)
267 {
268 uint z0;
269 uint z1;
270 int negCount = (-count) & 31;
271
272 z0 = mix(0u, a0, count == 0);
273 z0 = mix(z0, (a0 >> count), count < 32);
274
275 z1 = uint((a0 | a1) != 0u); /* count >= 64 */
276 uint z1_lt64 = (a0>>(count & 31)) | uint(((a0<<negCount) | a1) != 0u);
277 z1 = mix(z1, z1_lt64, count < 64);
278 z1 = mix(z1, (a0 | uint(a1 != 0u)), count == 32);
279 uint z1_lt32 = (a0<<negCount) | (a1>>count) | uint ((a1<<negCount) != 0u);
280 z1 = mix(z1, z1_lt32, count < 32);
281 z1 = mix(z1, a1, count == 0);
282 z1Ptr = z1;
283 z0Ptr = z0;
284 }
285
286 /* Shifts the 96-bit value formed by concatenating `a0', `a1', and `a2' right
287 * by 32 _plus_ the number of bits given in `count'. The shifted result is
288 * at most 64 nonzero bits; these are broken into two 32-bit pieces which are
289 * stored at the locations pointed to by `z0Ptr' and `z1Ptr'. The bits shifted
290 * off form a third 32-bit result as follows: The _last_ bit shifted off is
291 * the most-significant bit of the extra result, and the other 31 bits of the
292 * extra result are all zero if and only if _all_but_the_last_ bits shifted off
293 * were all zero. This extra result is stored in the location pointed to by
294 * `z2Ptr'. The value of `count' can be arbitrarily large.
295 * (This routine makes more sense if `a0', `a1', and `a2' are considered
296 * to form a fixed-point value with binary point between `a1' and `a2'. This
297 * fixed-point value is shifted right by the number of bits given in `count',
298 * and the integer part of the result is returned at the locations pointed to
299 * by `z0Ptr' and `z1Ptr'. The fractional part of the result may be slightly
300 * corrupted as described above, and is returned at the location pointed to by
301 * `z2Ptr'.)
302 */
303 void
304 __shift64ExtraRightJamming(uint a0, uint a1, uint a2,
305 int count,
306 out uint z0Ptr,
307 out uint z1Ptr,
308 out uint z2Ptr)
309 {
310 uint z0 = 0u;
311 uint z1;
312 uint z2;
313 int negCount = (-count) & 31;
314
315 z2 = mix(uint(a0 != 0u), a0, count == 64);
316 z2 = mix(z2, a0 << negCount, count < 64);
317 z2 = mix(z2, a1 << negCount, count < 32);
318
319 z1 = mix(0u, (a0 >> (count & 31)), count < 64);
320 z1 = mix(z1, (a0<<negCount) | (a1>>count), count < 32);
321
322 a2 = mix(a2 | a1, a2, count < 32);
323 z0 = mix(z0, a0 >> count, count < 32);
324 z2 |= uint(a2 != 0u);
325
326 z0 = mix(z0, 0u, (count == 32));
327 z1 = mix(z1, a0, (count == 32));
328 z2 = mix(z2, a1, (count == 32));
329 z0 = mix(z0, a0, (count == 0));
330 z1 = mix(z1, a1, (count == 0));
331 z2 = mix(z2, a2, (count == 0));
332 z2Ptr = z2;
333 z1Ptr = z1;
334 z0Ptr = z0;
335 }
336
337 /* Shifts the 64-bit value formed by concatenating `a0' and `a1' left by the
338 * number of bits given in `count'. Any bits shifted off are lost. The value
339 * of `count' must be less than 32. The result is broken into two 32-bit
340 * pieces which are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
341 */
342 void
343 __shortShift64Left(uint a0, uint a1,
344 int count,
345 out uint z0Ptr,
346 out uint z1Ptr)
347 {
348 z1Ptr = a1<<count;
349 z0Ptr = mix((a0 << count | (a1 >> ((-count) & 31))), a0, count == 0);
350 }
351
352 /* Packs the sign `zSign', the exponent `zExp', and the significand formed by
353 * the concatenation of `zFrac0' and `zFrac1' into a double-precision floating-
354 * point value, returning the result. After being shifted into the proper
355 * positions, the three fields `zSign', `zExp', and `zFrac0' are simply added
356 * together to form the most significant 32 bits of the result. This means
357 * that any integer portion of `zFrac0' will be added into the exponent. Since
358 * a properly normalized significand will have an integer portion equal to 1,
359 * the `zExp' input should be 1 less than the desired result exponent whenever
360 * `zFrac0' and `zFrac1' concatenated form a complete, normalized significand.
361 */
362 uint64_t
363 __packFloat64(uint zSign, int zExp, uint zFrac0, uint zFrac1)
364 {
365 uvec2 z;
366
367 z.y = (zSign << 31) + (uint(zExp) << 20) + zFrac0;
368 z.x = zFrac1;
369 return packUint2x32(z);
370 }
371
372 /* Takes an abstract floating-point value having sign `zSign', exponent `zExp',
373 * and extended significand formed by the concatenation of `zFrac0', `zFrac1',
374 * and `zFrac2', and returns the proper double-precision floating-point value
375 * corresponding to the abstract input. Ordinarily, the abstract value is
376 * simply rounded and packed into the double-precision format, with the inexact
377 * exception raised if the abstract input cannot be represented exactly.
378 * However, if the abstract value is too large, the overflow and inexact
379 * exceptions are raised and an infinity or maximal finite value is returned.
380 * If the abstract value is too small, the input value is rounded to a
381 * subnormal number, and the underflow and inexact exceptions are raised if the
382 * abstract input cannot be represented exactly as a subnormal double-precision
383 * floating-point number.
384 * The input significand must be normalized or smaller. If the input
385 * significand is not normalized, `zExp' must be 0; in that case, the result
386 * returned is a subnormal number, and it must not require rounding. In the
387 * usual case that the input significand is normalized, `zExp' must be 1 less
388 * than the "true" floating-point exponent. The handling of underflow and
389 * overflow follows the IEEE Standard for Floating-Point Arithmetic.
390 */
391 uint64_t
392 __roundAndPackFloat64(uint zSign,
393 int zExp,
394 uint zFrac0,
395 uint zFrac1,
396 uint zFrac2)
397 {
398 bool roundNearestEven;
399 bool increment;
400
401 roundNearestEven = FLOAT_ROUNDING_MODE == FLOAT_ROUND_NEAREST_EVEN;
402 increment = int(zFrac2) < 0;
403 if (!roundNearestEven) {
404 if (FLOAT_ROUNDING_MODE == FLOAT_ROUND_TO_ZERO) {
405 increment = false;
406 } else {
407 if (zSign != 0u) {
408 increment = (FLOAT_ROUNDING_MODE == FLOAT_ROUND_DOWN) &&
409 (zFrac2 != 0u);
410 } else {
411 increment = (FLOAT_ROUNDING_MODE == FLOAT_ROUND_UP) &&
412 (zFrac2 != 0u);
413 }
414 }
415 }
416 if (0x7FD <= zExp) {
417 if ((0x7FD < zExp) ||
418 ((zExp == 0x7FD) &&
419 (0x001FFFFFu == zFrac0 && 0xFFFFFFFFu == zFrac1) &&
420 increment)) {
421 if ((FLOAT_ROUNDING_MODE == FLOAT_ROUND_TO_ZERO) ||
422 ((zSign != 0u) && (FLOAT_ROUNDING_MODE == FLOAT_ROUND_UP)) ||
423 ((zSign == 0u) && (FLOAT_ROUNDING_MODE == FLOAT_ROUND_DOWN))) {
424 return __packFloat64(zSign, 0x7FE, 0x000FFFFFu, 0xFFFFFFFFu);
425 }
426 return __packFloat64(zSign, 0x7FF, 0u, 0u);
427 }
428 if (zExp < 0) {
429 __shift64ExtraRightJamming(
430 zFrac0, zFrac1, zFrac2, -zExp, zFrac0, zFrac1, zFrac2);
431 zExp = 0;
432 if (roundNearestEven) {
433 increment = zFrac2 < 0u;
434 } else {
435 if (zSign != 0u) {
436 increment = (FLOAT_ROUNDING_MODE == FLOAT_ROUND_DOWN) &&
437 (zFrac2 != 0u);
438 } else {
439 increment = (FLOAT_ROUNDING_MODE == FLOAT_ROUND_UP) &&
440 (zFrac2 != 0u);
441 }
442 }
443 }
444 }
445 if (increment) {
446 __add64(zFrac0, zFrac1, 0u, 1u, zFrac0, zFrac1);
447 zFrac1 &= ~((zFrac2 + uint(zFrac2 == 0u)) & uint(roundNearestEven));
448 } else {
449 zExp = mix(zExp, 0, (zFrac0 | zFrac1) == 0u);
450 }
451 return __packFloat64(zSign, zExp, zFrac0, zFrac1);
452 }
453
454 uint64_t
455 __roundAndPackUInt64(uint zSign, uint zFrac0, uint zFrac1, uint zFrac2)
456 {
457 bool roundNearestEven;
458 bool increment;
459 uint64_t default_nan = 0xFFFFFFFFFFFFFFFFUL;
460
461 roundNearestEven = FLOAT_ROUNDING_MODE == FLOAT_ROUND_NEAREST_EVEN;
462
463 if (zFrac2 >= 0x80000000u)
464 increment = false;
465
466 if (!roundNearestEven) {
467 if (zSign != 0u) {
468 if ((FLOAT_ROUNDING_MODE == FLOAT_ROUND_DOWN) && (zFrac2 != 0u)) {
469 increment = false;
470 }
471 } else {
472 increment = (FLOAT_ROUNDING_MODE == FLOAT_ROUND_UP) &&
473 (zFrac2 != 0u);
474 }
475 }
476
477 if (increment) {
478 __add64(zFrac0, zFrac1, 0u, 1u, zFrac0, zFrac1);
479 if ((zFrac0 | zFrac1) != 0u)
480 zFrac1 &= ~(1u) + uint(zFrac2 == 0u) & uint(roundNearestEven);
481 }
482 return mix(packUint2x32(uvec2(zFrac1, zFrac0)), default_nan,
483 (zSign !=0u && (zFrac0 | zFrac1) != 0u));
484 }
485
486 /* Returns the number of leading 0 bits before the most-significant 1 bit of
487 * `a'. If `a' is zero, 32 is returned.
488 */
489 int
490 __countLeadingZeros32(uint a)
491 {
492 int shiftCount;
493 shiftCount = mix(31 - findMSB(a), 32, a == 0u);
494 return shiftCount;
495 }
496
497 /* Takes an abstract floating-point value having sign `zSign', exponent `zExp',
498 * and significand formed by the concatenation of `zSig0' and `zSig1', and
499 * returns the proper double-precision floating-point value corresponding
500 * to the abstract input. This routine is just like `__roundAndPackFloat64'
501 * except that the input significand has fewer bits and does not have to be
502 * normalized. In all cases, `zExp' must be 1 less than the "true" floating-
503 * point exponent.
504 */
505 uint64_t
506 __normalizeRoundAndPackFloat64(uint zSign,
507 int zExp,
508 uint zFrac0,
509 uint zFrac1)
510 {
511 int shiftCount;
512 uint zFrac2;
513
514 if (zFrac0 == 0u) {
515 zExp -= 32;
516 zFrac0 = zFrac1;
517 zFrac1 = 0u;
518 }
519
520 shiftCount = __countLeadingZeros32(zFrac0) - 11;
521 if (0 <= shiftCount) {
522 zFrac2 = 0u;
523 __shortShift64Left(zFrac0, zFrac1, shiftCount, zFrac0, zFrac1);
524 } else {
525 __shift64ExtraRightJamming(
526 zFrac0, zFrac1, 0u, -shiftCount, zFrac0, zFrac1, zFrac2);
527 }
528 zExp -= shiftCount;
529 return __roundAndPackFloat64(zSign, zExp, zFrac0, zFrac1, zFrac2);
530 }
531
532 /* Takes two double-precision floating-point values `a' and `b', one of which
533 * is a NaN, and returns the appropriate NaN result.
534 */
535 uint64_t
536 __propagateFloat64NaN(uint64_t __a, uint64_t __b)
537 {
538 bool aIsNaN = __is_nan(__a);
539 bool bIsNaN = __is_nan(__b);
540 uvec2 a = unpackUint2x32(__a);
541 uvec2 b = unpackUint2x32(__b);
542 a.y |= 0x00080000u;
543 b.y |= 0x00080000u;
544
545 return packUint2x32(mix(b, mix(a, b, bvec2(bIsNaN, bIsNaN)), bvec2(aIsNaN, aIsNaN)));
546 }
547
548 /* Returns the result of adding the double-precision floating-point values
549 * `a' and `b'. The operation is performed according to the IEEE Standard for
550 * Floating-Point Arithmetic.
551 */
552 uint64_t
553 __fadd64(uint64_t a, uint64_t b)
554 {
555 uint aSign = __extractFloat64Sign(a);
556 uint bSign = __extractFloat64Sign(b);
557 uint aFracLo = __extractFloat64FracLo(a);
558 uint aFracHi = __extractFloat64FracHi(a);
559 uint bFracLo = __extractFloat64FracLo(b);
560 uint bFracHi = __extractFloat64FracHi(b);
561 int aExp = __extractFloat64Exp(a);
562 int bExp = __extractFloat64Exp(b);
563 uint zFrac0 = 0u;
564 uint zFrac1 = 0u;
565 int expDiff = aExp - bExp;
566 if (aSign == bSign) {
567 uint zFrac2 = 0u;
568 int zExp;
569 bool orig_exp_diff_is_zero = (expDiff == 0);
570
571 if (orig_exp_diff_is_zero) {
572 if (aExp == 0x7FF) {
573 bool propagate = (aFracHi | aFracLo | bFracHi | bFracLo) != 0u;
574 return mix(a, __propagateFloat64NaN(a, b), propagate);
575 }
576 __add64(aFracHi, aFracLo, bFracHi, bFracLo, zFrac0, zFrac1);
577 if (aExp == 0)
578 return __packFloat64(aSign, 0, zFrac0, zFrac1);
579 zFrac2 = 0u;
580 zFrac0 |= 0x00200000u;
581 zExp = aExp;
582 __shift64ExtraRightJamming(
583 zFrac0, zFrac1, zFrac2, 1, zFrac0, zFrac1, zFrac2);
584 } else if (0 < expDiff) {
585 if (aExp == 0x7FF) {
586 bool propagate = (aFracHi | aFracLo) != 0u;
587 return mix(a, __propagateFloat64NaN(a, b), propagate);
588 }
589
590 expDiff = mix(expDiff, expDiff - 1, bExp == 0);
591 bFracHi = mix(bFracHi | 0x00100000u, bFracHi, bExp == 0);
592 __shift64ExtraRightJamming(
593 bFracHi, bFracLo, 0u, expDiff, bFracHi, bFracLo, zFrac2);
594 zExp = aExp;
595 } else if (expDiff < 0) {
596 if (bExp == 0x7FF) {
597 bool propagate = (bFracHi | bFracLo) != 0u;
598 return mix(__packFloat64(aSign, 0x7ff, 0u, 0u), __propagateFloat64NaN(a, b), propagate);
599 }
600 expDiff = mix(expDiff, expDiff + 1, aExp == 0);
601 aFracHi = mix(aFracHi | 0x00100000u, aFracHi, aExp == 0);
602 __shift64ExtraRightJamming(
603 aFracHi, aFracLo, 0u, - expDiff, aFracHi, aFracLo, zFrac2);
604 zExp = bExp;
605 }
606 if (!orig_exp_diff_is_zero) {
607 aFracHi |= 0x00100000u;
608 __add64(aFracHi, aFracLo, bFracHi, bFracLo, zFrac0, zFrac1);
609 --zExp;
610 if (!(zFrac0 < 0x00200000u)) {
611 __shift64ExtraRightJamming(zFrac0, zFrac1, zFrac2, 1, zFrac0, zFrac1, zFrac2);
612 ++zExp;
613 }
614 }
615 return __roundAndPackFloat64(aSign, zExp, zFrac0, zFrac1, zFrac2);
616
617 } else {
618 int zExp;
619
620 __shortShift64Left(aFracHi, aFracLo, 10, aFracHi, aFracLo);
621 __shortShift64Left(bFracHi, bFracLo, 10, bFracHi, bFracLo);
622 if (0 < expDiff) {
623 if (aExp == 0x7FF) {
624 bool propagate = (aFracHi | aFracLo) != 0u;
625 return mix(a, __propagateFloat64NaN(a, b), propagate);
626 }
627 expDiff = mix(expDiff, expDiff - 1, bExp == 0);
628 bFracHi = mix(bFracHi | 0x40000000u, bFracHi, bExp == 0);
629 __shift64RightJamming(bFracHi, bFracLo, expDiff, bFracHi, bFracLo);
630 aFracHi |= 0x40000000u;
631 __sub64(aFracHi, aFracLo, bFracHi, bFracLo, zFrac0, zFrac1);
632 zExp = aExp;
633 --zExp;
634 return __normalizeRoundAndPackFloat64(aSign, zExp - 10, zFrac0, zFrac1);
635 }
636 if (expDiff < 0) {
637 if (bExp == 0x7FF) {
638 bool propagate = (bFracHi | bFracLo) != 0u;
639 return mix(__packFloat64(aSign ^ 1u, 0x7ff, 0u, 0u), __propagateFloat64NaN(a, b), propagate);
640 }
641 expDiff = mix(expDiff, expDiff + 1, aExp == 0);
642 aFracHi = mix(aFracHi | 0x40000000u, aFracHi, aExp == 0);
643 __shift64RightJamming(aFracHi, aFracLo, - expDiff, aFracHi, aFracLo);
644 bFracHi |= 0x40000000u;
645 __sub64(bFracHi, bFracLo, aFracHi, aFracLo, zFrac0, zFrac1);
646 zExp = bExp;
647 aSign ^= 1u;
648 --zExp;
649 return __normalizeRoundAndPackFloat64(aSign, zExp - 10, zFrac0, zFrac1);
650 }
651 if (aExp == 0x7FF) {
652 bool propagate = (aFracHi | aFracLo | bFracHi | bFracLo) != 0u;
653 return mix(0xFFFFFFFFFFFFFFFFUL, __propagateFloat64NaN(a, b), propagate);
654 }
655 bExp = mix(bExp, 1, aExp == 0);
656 aExp = mix(aExp, 1, aExp == 0);
657 bool zexp_normal = false;
658 bool blta = true;
659 if (bFracHi < aFracHi) {
660 __sub64(aFracHi, aFracLo, bFracHi, bFracLo, zFrac0, zFrac1);
661 zexp_normal = true;
662 }
663 else if (aFracHi < bFracHi) {
664 __sub64(bFracHi, bFracLo, aFracHi, aFracLo, zFrac0, zFrac1);
665 blta = false;
666 zexp_normal = true;
667 }
668 else if (bFracLo < aFracLo) {
669 __sub64(aFracHi, aFracLo, bFracHi, bFracLo, zFrac0, zFrac1);
670 zexp_normal = true;
671 }
672 else if (aFracLo < bFracLo) {
673 __sub64(bFracHi, bFracLo, aFracHi, aFracLo, zFrac0, zFrac1);
674 blta = false;
675 zexp_normal = true;
676 }
677 zExp = mix(bExp, aExp, blta);
678 aSign = mix(aSign ^ 1u, aSign, blta);
679 uint64_t retval_0 = __packFloat64(uint(FLOAT_ROUNDING_MODE == FLOAT_ROUND_DOWN), 0, 0u, 0u);
680 uint64_t retval_1 = __normalizeRoundAndPackFloat64(aSign, zExp - 11, zFrac0, zFrac1);
681 return mix(retval_0, retval_1, zexp_normal);
682 }
683 }
684
685 /* Multiplies `a' by `b' to obtain a 64-bit product. The product is broken
686 * into two 32-bit pieces which are stored at the locations pointed to by
687 * `z0Ptr' and `z1Ptr'.
688 */
689 void
690 __mul32To64(uint a, uint b, out uint z0Ptr, out uint z1Ptr)
691 {
692 uint aLow = a & 0x0000FFFFu;
693 uint aHigh = a>>16;
694 uint bLow = b & 0x0000FFFFu;
695 uint bHigh = b>>16;
696 uint z1 = aLow * bLow;
697 uint zMiddleA = aLow * bHigh;
698 uint zMiddleB = aHigh * bLow;
699 uint z0 = aHigh * bHigh;
700 zMiddleA += zMiddleB;
701 z0 += ((uint(zMiddleA < zMiddleB)) << 16) + (zMiddleA >> 16);
702 zMiddleA <<= 16;
703 z1 += zMiddleA;
704 z0 += uint(z1 < zMiddleA);
705 z1Ptr = z1;
706 z0Ptr = z0;
707 }
708
709 /* Multiplies the 64-bit value formed by concatenating `a0' and `a1' to the
710 * 64-bit value formed by concatenating `b0' and `b1' to obtain a 128-bit
711 * product. The product is broken into four 32-bit pieces which are stored at
712 * the locations pointed to by `z0Ptr', `z1Ptr', `z2Ptr', and `z3Ptr'.
713 */
714 void
715 __mul64To128(uint a0, uint a1, uint b0, uint b1,
716 out uint z0Ptr,
717 out uint z1Ptr,
718 out uint z2Ptr,
719 out uint z3Ptr)
720 {
721 uint z0 = 0u;
722 uint z1 = 0u;
723 uint z2 = 0u;
724 uint z3 = 0u;
725 uint more1 = 0u;
726 uint more2 = 0u;
727
728 __mul32To64(a1, b1, z2, z3);
729 __mul32To64(a1, b0, z1, more2);
730 __add64(z1, more2, 0u, z2, z1, z2);
731 __mul32To64(a0, b0, z0, more1);
732 __add64(z0, more1, 0u, z1, z0, z1);
733 __mul32To64(a0, b1, more1, more2);
734 __add64(more1, more2, 0u, z2, more1, z2);
735 __add64(z0, z1, 0u, more1, z0, z1);
736 z3Ptr = z3;
737 z2Ptr = z2;
738 z1Ptr = z1;
739 z0Ptr = z0;
740 }
741
742 /* Normalizes the subnormal double-precision floating-point value represented
743 * by the denormalized significand formed by the concatenation of `aFrac0' and
744 * `aFrac1'. The normalized exponent is stored at the location pointed to by
745 * `zExpPtr'. The most significant 21 bits of the normalized significand are
746 * stored at the location pointed to by `zFrac0Ptr', and the least significant
747 * 32 bits of the normalized significand are stored at the location pointed to
748 * by `zFrac1Ptr'.
749 */
750 void
751 __normalizeFloat64Subnormal(uint aFrac0, uint aFrac1,
752 out int zExpPtr,
753 out uint zFrac0Ptr,
754 out uint zFrac1Ptr)
755 {
756 int shiftCount;
757 uint temp_zfrac0, temp_zfrac1;
758 shiftCount = __countLeadingZeros32(mix(aFrac0, aFrac1, aFrac0 == 0u)) - 11;
759 zExpPtr = mix(1 - shiftCount, -shiftCount - 31, aFrac0 == 0u);
760
761 temp_zfrac0 = mix(aFrac1<<shiftCount, aFrac1>>(-shiftCount), shiftCount < 0);
762 temp_zfrac1 = mix(0u, aFrac1<<(shiftCount & 31), shiftCount < 0);
763
764 __shortShift64Left(aFrac0, aFrac1, shiftCount, zFrac0Ptr, zFrac1Ptr);
765
766 zFrac0Ptr = mix(zFrac0Ptr, temp_zfrac0, aFrac0 == 0);
767 zFrac1Ptr = mix(zFrac1Ptr, temp_zfrac1, aFrac0 == 0);
768 }
769
770 /* Returns the result of multiplying the double-precision floating-point values
771 * `a' and `b'. The operation is performed according to the IEEE Standard for
772 * Floating-Point Arithmetic.
773 */
774 uint64_t
775 __fmul64(uint64_t a, uint64_t b)
776 {
777 uint zFrac0 = 0u;
778 uint zFrac1 = 0u;
779 uint zFrac2 = 0u;
780 uint zFrac3 = 0u;
781 int zExp;
782
783 uint aFracLo = __extractFloat64FracLo(a);
784 uint aFracHi = __extractFloat64FracHi(a);
785 uint bFracLo = __extractFloat64FracLo(b);
786 uint bFracHi = __extractFloat64FracHi(b);
787 int aExp = __extractFloat64Exp(a);
788 uint aSign = __extractFloat64Sign(a);
789 int bExp = __extractFloat64Exp(b);
790 uint bSign = __extractFloat64Sign(b);
791 uint zSign = aSign ^ bSign;
792 if (aExp == 0x7FF) {
793 if (((aFracHi | aFracLo) != 0u) ||
794 ((bExp == 0x7FF) && ((bFracHi | bFracLo) != 0u))) {
795 return __propagateFloat64NaN(a, b);
796 }
797 if ((uint(bExp) | bFracHi | bFracLo) == 0u)
798 return 0xFFFFFFFFFFFFFFFFUL;
799 return __packFloat64(zSign, 0x7FF, 0u, 0u);
800 }
801 if (bExp == 0x7FF) {
802 if ((bFracHi | bFracLo) != 0u)
803 return __propagateFloat64NaN(a, b);
804 if ((uint(aExp) | aFracHi | aFracLo) == 0u)
805 return 0xFFFFFFFFFFFFFFFFUL;
806 return __packFloat64(zSign, 0x7FF, 0u, 0u);
807 }
808 if (aExp == 0) {
809 if ((aFracHi | aFracLo) == 0u)
810 return __packFloat64(zSign, 0, 0u, 0u);
811 __normalizeFloat64Subnormal(aFracHi, aFracLo, aExp, aFracHi, aFracLo);
812 }
813 if (bExp == 0) {
814 if ((bFracHi | bFracLo) == 0u)
815 return __packFloat64(zSign, 0, 0u, 0u);
816 __normalizeFloat64Subnormal(bFracHi, bFracLo, bExp, bFracHi, bFracLo);
817 }
818 zExp = aExp + bExp - 0x400;
819 aFracHi |= 0x00100000u;
820 __shortShift64Left(bFracHi, bFracLo, 12, bFracHi, bFracLo);
821 __mul64To128(
822 aFracHi, aFracLo, bFracHi, bFracLo, zFrac0, zFrac1, zFrac2, zFrac3);
823 __add64(zFrac0, zFrac1, aFracHi, aFracLo, zFrac0, zFrac1);
824 zFrac2 |= uint(zFrac3 != 0u);
825 if (0x00200000u <= zFrac0) {
826 __shift64ExtraRightJamming(
827 zFrac0, zFrac1, zFrac2, 1, zFrac0, zFrac1, zFrac2);
828 ++zExp;
829 }
830 return __roundAndPackFloat64(zSign, zExp, zFrac0, zFrac1, zFrac2);
831 }
832
833 uint64_t
834 __ffma64(uint64_t a, uint64_t b, uint64_t c)
835 {
836 return __fadd64(__fmul64(a, b), c);
837 }
838
839 /* Shifts the 64-bit value formed by concatenating `a0' and `a1' right by the
840 * number of bits given in `count'. Any bits shifted off are lost. The value
841 * of `count' can be arbitrarily large; in particular, if `count' is greater
842 * than 64, the result will be 0. The result is broken into two 32-bit pieces
843 * which are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
844 */
845 void
846 __shift64Right(uint a0, uint a1,
847 int count,
848 out uint z0Ptr,
849 out uint z1Ptr)
850 {
851 uint z0;
852 uint z1;
853 int negCount = (-count) & 31;
854
855 z0 = 0u;
856 z0 = mix(z0, (a0 >> count), count < 32);
857 z0 = mix(z0, a0, count == 0);
858
859 z1 = mix(0u, (a0 >> (count & 31)), count < 64);
860 z1 = mix(z1, (a0<<negCount) | (a1>>count), count < 32);
861 z1 = mix(z1, a0, count == 0);
862
863 z1Ptr = z1;
864 z0Ptr = z0;
865 }
866
867 /* Returns the result of converting the double-precision floating-point value
868 * `a' to the unsigned integer format. The conversion is performed according
869 * to the IEEE Standard for Floating-Point Arithmetic.
870 */
871 uint
872 __fp64_to_uint(uint64_t a)
873 {
874 uint aFracLo = __extractFloat64FracLo(a);
875 uint aFracHi = __extractFloat64FracHi(a);
876 int aExp = __extractFloat64Exp(a);
877 uint aSign = __extractFloat64Sign(a);
878
879 if ((aExp == 0x7FF) && ((aFracHi | aFracLo) != 0u))
880 return 0xFFFFFFFFu;
881
882 aFracHi |= mix(0u, 0x00100000u, aExp != 0);
883
884 int shiftDist = 0x427 - aExp;
885 if (0 < shiftDist)
886 __shift64RightJamming(aFracHi, aFracLo, shiftDist, aFracHi, aFracLo);
887
888 if ((aFracHi & 0xFFFFF000u) != 0u)
889 return mix(~0u, 0u, (aSign != 0u));
890
891 uint z = 0u;
892 uint zero = 0u;
893 __shift64Right(aFracHi, aFracLo, 12, zero, z);
894
895 uint expt = mix(~0u, 0u, (aSign != 0u));
896
897 return mix(z, expt, (aSign != 0u) && (z != 0u));
898 }
899
900 uint64_t
901 __uint_to_fp64(uint a)
902 {
903 if (a == 0u)
904 return 0ul;
905
906 int shiftDist = __countLeadingZeros32(a) + 21;
907
908 uint aHigh = 0u;
909 uint aLow = 0u;
910 int negCount = (- shiftDist) & 31;
911
912 aHigh = mix(0u, a<< shiftDist - 32, shiftDist < 64);
913 aLow = 0u;
914 aHigh = mix(aHigh, 0u, shiftDist == 0);
915 aLow = mix(aLow, a, shiftDist ==0);
916 aHigh = mix(aHigh, a >> negCount, shiftDist < 32);
917 aLow = mix(aLow, a << shiftDist, shiftDist < 32);
918
919 return __packFloat64(0u, 0x432 - shiftDist, aHigh, aLow);
920 }
921
922 uint64_t
923 __uint64_to_fp64(uint64_t a)
924 {
925 if (a == 0u)
926 return 0ul;
927
928 uvec2 aFrac = unpackUint2x32(a);
929 uint aFracLo = __extractFloat64FracLo(a);
930 uint aFracHi = __extractFloat64FracHi(a);
931
932 if ((aFracHi & 0x80000000u) != 0u) {
933 __shift64RightJamming(aFracHi, aFracLo, 1, aFracHi, aFracLo);
934 return __roundAndPackFloat64(0, 0x433, aFracHi, aFracLo, 0u);
935 } else {
936 return __normalizeRoundAndPackFloat64(0, 0x432, aFrac.y, aFrac.x);
937 }
938 }
939
940 uint64_t
941 __int64_to_fp64(int64_t a)
942 {
943 if (a==0)
944 return 0ul;
945
946 uint64_t absA = mix(uint64_t(a), uint64_t(-a), a < 0);
947 uint aFracHi = __extractFloat64FracHi(absA);
948 uvec2 aFrac = unpackUint2x32(absA);
949 uint zSign = uint(a < 0);
950
951 if ((aFracHi & 0x80000000u) != 0u) {
952 return mix(0ul, __packFloat64(1, 0x434, 0u, 0u), a < 0);
953 }
954
955 return __normalizeRoundAndPackFloat64(zSign, 0x432, aFrac.y, aFrac.x);
956 }
957
958 /* Returns the result of converting the double-precision floating-point value
959 * `a' to the 32-bit two's complement integer format. The conversion is
960 * performed according to the IEEE Standard for Floating-Point Arithmetic---
961 * which means in particular that the conversion is rounded according to the
962 * current rounding mode. If `a' is a NaN, the largest positive integer is
963 * returned. Otherwise, if the conversion overflows, the largest integer with
964 * the same sign as `a' is returned.
965 */
966 int
967 __fp64_to_int(uint64_t a)
968 {
969 uint aFracLo = __extractFloat64FracLo(a);
970 uint aFracHi = __extractFloat64FracHi(a);
971 int aExp = __extractFloat64Exp(a);
972 uint aSign = __extractFloat64Sign(a);
973
974 uint absZ = 0u;
975 uint aFracExtra = 0u;
976 int shiftCount = aExp - 0x413;
977
978 if (0 <= shiftCount) {
979 if (0x41E < aExp) {
980 if ((aExp == 0x7FF) && bool(aFracHi | aFracLo))
981 aSign = 0u;
982 return mix(0x7FFFFFFF, 0x80000000, bool(aSign));
983 }
984 __shortShift64Left(aFracHi | 0x00100000u, aFracLo, shiftCount, absZ, aFracExtra);
985 } else {
986 if (aExp < 0x3FF)
987 return 0;
988
989 aFracHi |= 0x00100000u;
990 aFracExtra = ( aFracHi << (shiftCount & 31)) | aFracLo;
991 absZ = aFracHi >> (- shiftCount);
992 }
993
994 int z = mix(int(absZ), -int(absZ), (aSign != 0u));
995 int nan = mix(0x7FFFFFFF, 0x80000000, bool(aSign));
996 return mix(z, nan, bool(aSign ^ uint(z < 0)) && bool(z));
997 }
998
999 /* Returns the result of converting the 32-bit two's complement integer `a'
1000 * to the double-precision floating-point format. The conversion is performed
1001 * according to the IEEE Standard for Floating-Point Arithmetic.
1002 */
1003 uint64_t
1004 __int_to_fp64(int a)
1005 {
1006 uint zFrac0 = 0u;
1007 uint zFrac1 = 0u;
1008 if (a==0)
1009 return __packFloat64(0u, 0, 0u, 0u);
1010 uint zSign = uint(a < 0);
1011 uint absA = mix(uint(a), uint(-a), a < 0);
1012 int shiftCount = __countLeadingZeros32(absA) - 11;
1013 if (0 <= shiftCount) {
1014 zFrac0 = absA << shiftCount;
1015 zFrac1 = 0u;
1016 } else {
1017 __shift64Right(absA, 0u, -shiftCount, zFrac0, zFrac1);
1018 }
1019 return __packFloat64(zSign, 0x412 - shiftCount, zFrac0, zFrac1);
1020 }
1021
1022 bool
1023 __fp64_to_bool(uint64_t a)
1024 {
1025 return !__feq64_nonnan(__fabs64(a), 0ul);
1026 }
1027
1028 uint64_t
1029 __bool_to_fp64(bool a)
1030 {
1031 return __int_to_fp64(int(a));
1032 }
1033
1034 /* Packs the sign `zSign', exponent `zExp', and significand `zFrac' into a
1035 * single-precision floating-point value, returning the result. After being
1036 * shifted into the proper positions, the three fields are simply added
1037 * together to form the result. This means that any integer portion of `zSig'
1038 * will be added into the exponent. Since a properly normalized significand
1039 * will have an integer portion equal to 1, the `zExp' input should be 1 less
1040 * than the desired result exponent whenever `zFrac' is a complete, normalized
1041 * significand.
1042 */
1043 float
1044 __packFloat32(uint zSign, int zExp, uint zFrac)
1045 {
1046 return uintBitsToFloat((zSign<<31) + (uint(zExp)<<23) + zFrac);
1047 }
1048
1049 /* Takes an abstract floating-point value having sign `zSign', exponent `zExp',
1050 * and significand `zFrac', and returns the proper single-precision floating-
1051 * point value corresponding to the abstract input. Ordinarily, the abstract
1052 * value is simply rounded and packed into the single-precision format, with
1053 * the inexact exception raised if the abstract input cannot be represented
1054 * exactly. However, if the abstract value is too large, the overflow and
1055 * inexact exceptions are raised and an infinity or maximal finite value is
1056 * returned. If the abstract value is too small, the input value is rounded to
1057 * a subnormal number, and the underflow and inexact exceptions are raised if
1058 * the abstract input cannot be represented exactly as a subnormal single-
1059 * precision floating-point number.
1060 * The input significand `zFrac' has its binary point between bits 30
1061 * and 29, which is 7 bits to the left of the usual location. This shifted
1062 * significand must be normalized or smaller. If `zFrac' is not normalized,
1063 * `zExp' must be 0; in that case, the result returned is a subnormal number,
1064 * and it must not require rounding. In the usual case that `zFrac' is
1065 * normalized, `zExp' must be 1 less than the "true" floating-point exponent.
1066 * The handling of underflow and overflow follows the IEEE Standard for
1067 * Floating-Point Arithmetic.
1068 */
1069 float
1070 __roundAndPackFloat32(uint zSign, int zExp, uint zFrac)
1071 {
1072 bool roundNearestEven;
1073 int roundIncrement;
1074 int roundBits;
1075
1076 roundNearestEven = FLOAT_ROUNDING_MODE == FLOAT_ROUND_NEAREST_EVEN;
1077 roundIncrement = 0x40;
1078 if (!roundNearestEven) {
1079 if (FLOAT_ROUNDING_MODE == FLOAT_ROUND_TO_ZERO) {
1080 roundIncrement = 0;
1081 } else {
1082 roundIncrement = 0x7F;
1083 if (zSign != 0u) {
1084 if (FLOAT_ROUNDING_MODE == FLOAT_ROUND_UP)
1085 roundIncrement = 0;
1086 } else {
1087 if (FLOAT_ROUNDING_MODE == FLOAT_ROUND_DOWN)
1088 roundIncrement = 0;
1089 }
1090 }
1091 }
1092 roundBits = int(zFrac & 0x7Fu);
1093 if (0xFDu <= uint(zExp)) {
1094 if ((0xFD < zExp) || ((zExp == 0xFD) && (int(zFrac) + roundIncrement) < 0))
1095 return __packFloat32(zSign, 0xFF, 0u) - float(roundIncrement == 0);
1096 int count = -zExp;
1097 bool zexp_lt0 = zExp < 0;
1098 uint zFrac_lt0 = mix(uint(zFrac != 0u), (zFrac>>count) | uint((zFrac<<((-count) & 31)) != 0u), (-zExp) < 32);
1099 zFrac = mix(zFrac, zFrac_lt0, zexp_lt0);
1100 roundBits = mix(roundBits, int(zFrac) & 0x7f, zexp_lt0);
1101 zExp = mix(zExp, 0, zexp_lt0);
1102 }
1103 zFrac = (zFrac + uint(roundIncrement))>>7;
1104 zFrac &= ~uint(((roundBits ^ 0x40) == 0) && roundNearestEven);
1105
1106 return __packFloat32(zSign, mix(zExp, 0, zFrac == 0u), zFrac);
1107 }
1108
1109 /* Returns the result of converting the double-precision floating-point value
1110 * `a' to the single-precision floating-point format. The conversion is
1111 * performed according to the IEEE Standard for Floating-Point Arithmetic.
1112 */
1113 float
1114 __fp64_to_fp32(uint64_t __a)
1115 {
1116 uvec2 a = unpackUint2x32(__a);
1117 uint zFrac = 0u;
1118 uint allZero = 0u;
1119
1120 uint aFracLo = __extractFloat64FracLo(__a);
1121 uint aFracHi = __extractFloat64FracHi(__a);
1122 int aExp = __extractFloat64Exp(__a);
1123 uint aSign = __extractFloat64Sign(__a);
1124 if (aExp == 0x7FF) {
1125 __shortShift64Left(a.y, a.x, 12, a.y, a.x);
1126 float rval = uintBitsToFloat((aSign<<31) | 0x7FC00000u | (a.y>>9));
1127 rval = mix(__packFloat32(aSign, 0xFF, 0u), rval, (aFracHi | aFracLo) != 0u);
1128 return rval;
1129 }
1130 __shift64RightJamming(aFracHi, aFracLo, 22, allZero, zFrac);
1131 zFrac = mix(zFrac, zFrac | 0x40000000u, aExp != 0);
1132 return __roundAndPackFloat32(aSign, aExp - 0x381, zFrac);
1133 }
1134
1135 float
1136 __uint64_to_fp32(uint64_t __a)
1137 {
1138 uint zFrac = 0u;
1139 uvec2 aFrac = unpackUint2x32(__a);
1140 int shiftCount = __countLeadingZeros32(mix(aFrac.y, aFrac.x, aFrac.y == 0u));
1141 shiftCount -= mix(40, 8, aFrac.y == 0u);
1142
1143 if (0 <= shiftCount) {
1144 __shortShift64Left(aFrac.y, aFrac.x, shiftCount, aFrac.y, aFrac.x);
1145 bool is_zero = (aFrac.y | aFrac.x) == 0u;
1146 return mix(__packFloat32(0u, 0x95 - shiftCount, aFrac.x), 0, is_zero);
1147 }
1148
1149 shiftCount += 7;
1150 __shift64RightJamming(aFrac.y, aFrac.x, -shiftCount, aFrac.y, aFrac.x);
1151 zFrac = mix(aFrac.x<<shiftCount, aFrac.x, shiftCount < 0);
1152 return __roundAndPackFloat32(0u, 0x9C - shiftCount, zFrac);
1153 }
1154
1155 float
1156 __int64_to_fp32(int64_t __a)
1157 {
1158 uint zFrac = 0u;
1159 uint aSign = uint(__a < 0);
1160 uint64_t absA = mix(uint64_t(__a), uint64_t(-__a), __a < 0);
1161 uvec2 aFrac = unpackUint2x32(absA);
1162 int shiftCount = __countLeadingZeros32(mix(aFrac.y, aFrac.x, aFrac.y == 0u));
1163 shiftCount -= mix(40, 8, aFrac.y == 0u);
1164
1165 if (0 <= shiftCount) {
1166 __shortShift64Left(aFrac.y, aFrac.x, shiftCount, aFrac.y, aFrac.x);
1167 bool is_zero = (aFrac.y | aFrac.x) == 0u;
1168 return mix(__packFloat32(aSign, 0x95 - shiftCount, aFrac.x), 0, absA == 0u);
1169 }
1170
1171 shiftCount += 7;
1172 __shift64RightJamming(aFrac.y, aFrac.x, -shiftCount, aFrac.y, aFrac.x);
1173 zFrac = mix(aFrac.x<<shiftCount, aFrac.x, shiftCount < 0);
1174 return __roundAndPackFloat32(aSign, 0x9C - shiftCount, zFrac);
1175 }
1176
1177 /* Returns the result of converting the single-precision floating-point value
1178 * `a' to the double-precision floating-point format.
1179 */
1180 uint64_t
1181 __fp32_to_fp64(float f)
1182 {
1183 uint a = floatBitsToUint(f);
1184 uint aFrac = a & 0x007FFFFFu;
1185 int aExp = int((a>>23) & 0xFFu);
1186 uint aSign = a>>31;
1187 uint zFrac0 = 0u;
1188 uint zFrac1 = 0u;
1189
1190 if (aExp == 0xFF) {
1191 if (aFrac != 0u) {
1192 uint nanLo = 0u;
1193 uint nanHi = a<<9;
1194 __shift64Right(nanHi, nanLo, 12, nanHi, nanLo);
1195 nanHi |= ((aSign<<31) | 0x7FF80000u);
1196 return packUint2x32(uvec2(nanLo, nanHi));
1197 }
1198 return __packFloat64(aSign, 0x7FF, 0u, 0u);
1199 }
1200
1201 if (aExp == 0) {
1202 if (aFrac == 0u)
1203 return __packFloat64(aSign, 0, 0u, 0u);
1204 /* Normalize subnormal */
1205 int shiftCount = __countLeadingZeros32(aFrac) - 8;
1206 aFrac <<= shiftCount;
1207 aExp = 1 - shiftCount;
1208 --aExp;
1209 }
1210
1211 __shift64Right(aFrac, 0u, 3, zFrac0, zFrac1);
1212 return __packFloat64(aSign, aExp + 0x380, zFrac0, zFrac1);
1213 }
1214
1215 /* Adds the 96-bit value formed by concatenating `a0', `a1', and `a2' to the
1216 * 96-bit value formed by concatenating `b0', `b1', and `b2'. Addition is
1217 * modulo 2^96, so any carry out is lost. The result is broken into three
1218 * 32-bit pieces which are stored at the locations pointed to by `z0Ptr',
1219 * `z1Ptr', and `z2Ptr'.
1220 */
1221 void
1222 __add96(uint a0, uint a1, uint a2,
1223 uint b0, uint b1, uint b2,
1224 out uint z0Ptr,
1225 out uint z1Ptr,
1226 out uint z2Ptr)
1227 {
1228 uint z2 = a2 + b2;
1229 uint carry1 = uint(z2 < a2);
1230 uint z1 = a1 + b1;
1231 uint carry0 = uint(z1 < a1);
1232 uint z0 = a0 + b0;
1233 z1 += carry1;
1234 z0 += uint(z1 < carry1);
1235 z0 += carry0;
1236 z2Ptr = z2;
1237 z1Ptr = z1;
1238 z0Ptr = z0;
1239 }
1240
1241 /* Subtracts the 96-bit value formed by concatenating `b0', `b1', and `b2' from
1242 * the 96-bit value formed by concatenating `a0', `a1', and `a2'. Subtraction
1243 * is modulo 2^96, so any borrow out (carry out) is lost. The result is broken
1244 * into three 32-bit pieces which are stored at the locations pointed to by
1245 * `z0Ptr', `z1Ptr', and `z2Ptr'.
1246 */
1247 void
1248 __sub96(uint a0, uint a1, uint a2,
1249 uint b0, uint b1, uint b2,
1250 out uint z0Ptr,
1251 out uint z1Ptr,
1252 out uint z2Ptr)
1253 {
1254 uint z2 = a2 - b2;
1255 uint borrow1 = uint(a2 < b2);
1256 uint z1 = a1 - b1;
1257 uint borrow0 = uint(a1 < b1);
1258 uint z0 = a0 - b0;
1259 z0 -= uint(z1 < borrow1);
1260 z1 -= borrow1;
1261 z0 -= borrow0;
1262 z2Ptr = z2;
1263 z1Ptr = z1;
1264 z0Ptr = z0;
1265 }
1266
1267 /* Returns an approximation to the 32-bit integer quotient obtained by dividing
1268 * `b' into the 64-bit value formed by concatenating `a0' and `a1'. The
1269 * divisor `b' must be at least 2^31. If q is the exact quotient truncated
1270 * toward zero, the approximation returned lies between q and q + 2 inclusive.
1271 * If the exact quotient q is larger than 32 bits, the maximum positive 32-bit
1272 * unsigned integer is returned.
1273 */
1274 uint
1275 __estimateDiv64To32(uint a0, uint a1, uint b)
1276 {
1277 uint b0;
1278 uint b1;
1279 uint rem0 = 0u;
1280 uint rem1 = 0u;
1281 uint term0 = 0u;
1282 uint term1 = 0u;
1283 uint z;
1284
1285 if (b <= a0)
1286 return 0xFFFFFFFFu;
1287 b0 = b>>16;
1288 z = (b0<<16 <= a0) ? 0xFFFF0000u : (a0 / b0)<<16;
1289 __mul32To64(b, z, term0, term1);
1290 __sub64(a0, a1, term0, term1, rem0, rem1);
1291 while (int(rem0) < 0) {
1292 z -= 0x10000u;
1293 b1 = b<<16;
1294 __add64(rem0, rem1, b0, b1, rem0, rem1);
1295 }
1296 rem0 = (rem0<<16) | (rem1>>16);
1297 z |= (b0<<16 <= rem0) ? 0xFFFFu : rem0 / b0;
1298 return z;
1299 }
1300
1301 uint
1302 __sqrtOddAdjustments(int index)
1303 {
1304 uint res = 0u;
1305 if (index == 0)
1306 res = 0x0004u;
1307 if (index == 1)
1308 res = 0x0022u;
1309 if (index == 2)
1310 res = 0x005Du;
1311 if (index == 3)
1312 res = 0x00B1u;
1313 if (index == 4)
1314 res = 0x011Du;
1315 if (index == 5)
1316 res = 0x019Fu;
1317 if (index == 6)
1318 res = 0x0236u;
1319 if (index == 7)
1320 res = 0x02E0u;
1321 if (index == 8)
1322 res = 0x039Cu;
1323 if (index == 9)
1324 res = 0x0468u;
1325 if (index == 10)
1326 res = 0x0545u;
1327 if (index == 11)
1328 res = 0x631u;
1329 if (index == 12)
1330 res = 0x072Bu;
1331 if (index == 13)
1332 res = 0x0832u;
1333 if (index == 14)
1334 res = 0x0946u;
1335 if (index == 15)
1336 res = 0x0A67u;
1337
1338 return res;
1339 }
1340
1341 uint
1342 __sqrtEvenAdjustments(int index)
1343 {
1344 uint res = 0u;
1345 if (index == 0)
1346 res = 0x0A2Du;
1347 if (index == 1)
1348 res = 0x08AFu;
1349 if (index == 2)
1350 res = 0x075Au;
1351 if (index == 3)
1352 res = 0x0629u;
1353 if (index == 4)
1354 res = 0x051Au;
1355 if (index == 5)
1356 res = 0x0429u;
1357 if (index == 6)
1358 res = 0x0356u;
1359 if (index == 7)
1360 res = 0x029Eu;
1361 if (index == 8)
1362 res = 0x0200u;
1363 if (index == 9)
1364 res = 0x0179u;
1365 if (index == 10)
1366 res = 0x0109u;
1367 if (index == 11)
1368 res = 0x00AFu;
1369 if (index == 12)
1370 res = 0x0068u;
1371 if (index == 13)
1372 res = 0x0034u;
1373 if (index == 14)
1374 res = 0x0012u;
1375 if (index == 15)
1376 res = 0x0002u;
1377
1378 return res;
1379 }
1380
1381 /* Returns an approximation to the square root of the 32-bit significand given
1382 * by `a'. Considered as an integer, `a' must be at least 2^31. If bit 0 of
1383 * `aExp' (the least significant bit) is 1, the integer returned approximates
1384 * 2^31*sqrt(`a'/2^31), where `a' is considered an integer. If bit 0 of `aExp'
1385 * is 0, the integer returned approximates 2^31*sqrt(`a'/2^30). In either
1386 * case, the approximation returned lies strictly within +/-2 of the exact
1387 * value.
1388 */
1389 uint
1390 __estimateSqrt32(int aExp, uint a)
1391 {
1392 uint z;
1393
1394 int index = int(a>>27 & 15u);
1395 if ((aExp & 1) != 0) {
1396 z = 0x4000u + (a>>17) - __sqrtOddAdjustments(index);
1397 z = ((a / z)<<14) + (z<<15);
1398 a >>= 1;
1399 } else {
1400 z = 0x8000u + (a>>17) - __sqrtEvenAdjustments(index);
1401 z = a / z + z;
1402 z = (0x20000u <= z) ? 0xFFFF8000u : (z<<15);
1403 if (z <= a)
1404 return uint(int(a)>>1);
1405 }
1406 return ((__estimateDiv64To32(a, 0u, z))>>1) + (z>>1);
1407 }
1408
1409 /* Returns the square root of the double-precision floating-point value `a'.
1410 * The operation is performed according to the IEEE Standard for Floating-Point
1411 * Arithmetic.
1412 */
1413 uint64_t
1414 __fsqrt64(uint64_t a)
1415 {
1416 uint zFrac0 = 0u;
1417 uint zFrac1 = 0u;
1418 uint zFrac2 = 0u;
1419 uint doubleZFrac0 = 0u;
1420 uint rem0 = 0u;
1421 uint rem1 = 0u;
1422 uint rem2 = 0u;
1423 uint rem3 = 0u;
1424 uint term0 = 0u;
1425 uint term1 = 0u;
1426 uint term2 = 0u;
1427 uint term3 = 0u;
1428 uint64_t default_nan = 0xFFFFFFFFFFFFFFFFUL;
1429
1430 uint aFracLo = __extractFloat64FracLo(a);
1431 uint aFracHi = __extractFloat64FracHi(a);
1432 int aExp = __extractFloat64Exp(a);
1433 uint aSign = __extractFloat64Sign(a);
1434 if (aExp == 0x7FF) {
1435 if ((aFracHi | aFracLo) != 0u)
1436 return __propagateFloat64NaN(a, a);
1437 if (aSign == 0u)
1438 return a;
1439 return default_nan;
1440 }
1441 if (aSign != 0u) {
1442 if ((uint(aExp) | aFracHi | aFracLo) == 0u)
1443 return a;
1444 return default_nan;
1445 }
1446 if (aExp == 0) {
1447 if ((aFracHi | aFracLo) == 0u)
1448 return __packFloat64(0u, 0, 0u, 0u);
1449 __normalizeFloat64Subnormal(aFracHi, aFracLo, aExp, aFracHi, aFracLo);
1450 }
1451 int zExp = ((aExp - 0x3FF)>>1) + 0x3FE;
1452 aFracHi |= 0x00100000u;
1453 __shortShift64Left(aFracHi, aFracLo, 11, term0, term1);
1454 zFrac0 = (__estimateSqrt32(aExp, term0)>>1) + 1u;
1455 if (zFrac0 == 0u)
1456 zFrac0 = 0x7FFFFFFFu;
1457 doubleZFrac0 = zFrac0 + zFrac0;
1458 __shortShift64Left(aFracHi, aFracLo, 9 - (aExp & 1), aFracHi, aFracLo);
1459 __mul32To64(zFrac0, zFrac0, term0, term1);
1460 __sub64(aFracHi, aFracLo, term0, term1, rem0, rem1);
1461 while (int(rem0) < 0) {
1462 --zFrac0;
1463 doubleZFrac0 -= 2u;
1464 __add64(rem0, rem1, 0u, doubleZFrac0 | 1u, rem0, rem1);
1465 }
1466 zFrac1 = __estimateDiv64To32(rem1, 0u, doubleZFrac0);
1467 if ((zFrac1 & 0x1FFu) <= 5u) {
1468 if (zFrac1 == 0u)
1469 zFrac1 = 1u;
1470 __mul32To64(doubleZFrac0, zFrac1, term1, term2);
1471 __sub64(rem1, 0u, term1, term2, rem1, rem2);
1472 __mul32To64(zFrac1, zFrac1, term2, term3);
1473 __sub96(rem1, rem2, 0u, 0u, term2, term3, rem1, rem2, rem3);
1474 while (int(rem1) < 0) {
1475 --zFrac1;
1476 __shortShift64Left(0u, zFrac1, 1, term2, term3);
1477 term3 |= 1u;
1478 term2 |= doubleZFrac0;
1479 __add96(rem1, rem2, rem3, 0u, term2, term3, rem1, rem2, rem3);
1480 }
1481 zFrac1 |= uint((rem1 | rem2 | rem3) != 0u);
1482 }
1483 __shift64ExtraRightJamming(zFrac0, zFrac1, 0u, 10, zFrac0, zFrac1, zFrac2);
1484 return __roundAndPackFloat64(0u, zExp, zFrac0, zFrac1, zFrac2);
1485 }
1486
1487 uint64_t
1488 __ftrunc64(uint64_t __a)
1489 {
1490 uvec2 a = unpackUint2x32(__a);
1491 int aExp = __extractFloat64Exp(__a);
1492 uint zLo;
1493 uint zHi;
1494
1495 int unbiasedExp = aExp - 1023;
1496 int fracBits = 52 - unbiasedExp;
1497 uint maskLo = mix(~0u << fracBits, 0u, fracBits >= 32);
1498 uint maskHi = mix(~0u << (fracBits - 32), ~0u, fracBits < 33);
1499 zLo = maskLo & a.x;
1500 zHi = maskHi & a.y;
1501
1502 zLo = mix(zLo, 0u, unbiasedExp < 0);
1503 zHi = mix(zHi, 0u, unbiasedExp < 0);
1504 zLo = mix(zLo, a.x, unbiasedExp > 52);
1505 zHi = mix(zHi, a.y, unbiasedExp > 52);
1506 return packUint2x32(uvec2(zLo, zHi));
1507 }
1508
1509 uint64_t
1510 __ffloor64(uint64_t a)
1511 {
1512 bool is_positive = __fge64(a, 0ul);
1513 uint64_t tr = __ftrunc64(a);
1514
1515 if (is_positive || __feq64(tr, a)) {
1516 return tr;
1517 } else {
1518 return __fadd64(tr, 0xbff0000000000000ul /* -1.0 */);
1519 }
1520 }
1521
1522 uint64_t
1523 __fround64(uint64_t __a)
1524 {
1525 uvec2 a = unpackUint2x32(__a);
1526 int unbiasedExp = __extractFloat64Exp(__a) - 1023;
1527 uint aHi = a.y;
1528 uint aLo = a.x;
1529
1530 if (unbiasedExp < 20) {
1531 if (unbiasedExp < 0) {
1532 aHi &= 0x80000000u;
1533 if (unbiasedExp == -1 && aLo != 0u)
1534 aHi |= (1023u << 20);
1535 aLo = 0u;
1536 } else {
1537 uint maskExp = 0x000FFFFFu >> unbiasedExp;
1538 /* a is an integral value */
1539 if (((aHi & maskExp) == 0u) && (aLo == 0u))
1540 return __a;
1541
1542 aHi += 0x00080000u >> unbiasedExp;
1543 aHi &= ~maskExp;
1544 aLo = 0u;
1545 }
1546 } else if (unbiasedExp > 51 || unbiasedExp == 1024) {
1547 return __a;
1548 } else {
1549 uint maskExp = 0xFFFFFFFFu >> (unbiasedExp - 20);
1550 if ((aLo & maskExp) == 0u)
1551 return __a;
1552 uint tmp = aLo + (1u << (51 - unbiasedExp));
1553 if(tmp < aLo)
1554 aHi += 1u;
1555 aLo = tmp;
1556 aLo &= ~maskExp;
1557 }
1558
1559 a.x = aLo;
1560 a.y = aHi;
1561 return packUint2x32(a);
1562 }
1563
1564 uint64_t
1565 __fmin64(uint64_t a, uint64_t b)
1566 {
1567 if (__is_nan(a)) return b;
1568 if (__is_nan(b)) return a;
1569
1570 if (__flt64_nonnan(a, b)) return a;
1571 return b;
1572 }
1573
1574 uint64_t
1575 __fmax64(uint64_t a, uint64_t b)
1576 {
1577 if (__is_nan(a)) return b;
1578 if (__is_nan(b)) return a;
1579
1580 if (__flt64_nonnan(a, b)) return b;
1581 return a;
1582 }
1583
1584 uint64_t
1585 __ffract64(uint64_t a)
1586 {
1587 return __fadd64(a, __fneg64(__ffloor64(a)));
1588 }