soft-fp64: Don't open-code umulExtended
[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 uint64_t
222 __fsat64(uint64_t __a)
223 {
224 if (__flt64(__a, 0ul))
225 return 0ul;
226
227 if (__fge64(__a, 0x3FF0000000000000ul /* 1.0 */))
228 return 0x3FF0000000000000ul;
229
230 return __a;
231 }
232
233 /* Adds the 64-bit value formed by concatenating `a0' and `a1' to the 64-bit
234 * value formed by concatenating `b0' and `b1'. Addition is modulo 2^64, so
235 * any carry out is lost. The result is broken into two 32-bit pieces which
236 * are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
237 */
238 void
239 __add64(uint a0, uint a1, uint b0, uint b1,
240 out uint z0Ptr,
241 out uint z1Ptr)
242 {
243 uint z1 = a1 + b1;
244 z1Ptr = z1;
245 z0Ptr = a0 + b0 + uint(z1 < a1);
246 }
247
248
249 /* Subtracts the 64-bit value formed by concatenating `b0' and `b1' from the
250 * 64-bit value formed by concatenating `a0' and `a1'. Subtraction is modulo
251 * 2^64, so any borrow out (carry out) is lost. The result is broken into two
252 * 32-bit pieces which are stored at the locations pointed to by `z0Ptr' and
253 * `z1Ptr'.
254 */
255 void
256 __sub64(uint a0, uint a1, uint b0, uint b1,
257 out uint z0Ptr,
258 out uint z1Ptr)
259 {
260 z1Ptr = a1 - b1;
261 z0Ptr = a0 - b0 - uint(a1 < b1);
262 }
263
264 /* Shifts the 64-bit value formed by concatenating `a0' and `a1' right by the
265 * number of bits given in `count'. If any nonzero bits are shifted off, they
266 * are "jammed" into the least significant bit of the result by setting the
267 * least significant bit to 1. The value of `count' can be arbitrarily large;
268 * in particular, if `count' is greater than 64, the result will be either 0
269 * or 1, depending on whether the concatenation of `a0' and `a1' is zero or
270 * nonzero. The result is broken into two 32-bit pieces which are stored at
271 * the locations pointed to by `z0Ptr' and `z1Ptr'.
272 */
273 void
274 __shift64RightJamming(uint a0,
275 uint a1,
276 int count,
277 out uint z0Ptr,
278 out uint z1Ptr)
279 {
280 uint z0;
281 uint z1;
282 int negCount = (-count) & 31;
283
284 z0 = mix(0u, a0, count == 0);
285 z0 = mix(z0, (a0 >> count), count < 32);
286
287 z1 = uint((a0 | a1) != 0u); /* count >= 64 */
288 uint z1_lt64 = (a0>>(count & 31)) | uint(((a0<<negCount) | a1) != 0u);
289 z1 = mix(z1, z1_lt64, count < 64);
290 z1 = mix(z1, (a0 | uint(a1 != 0u)), count == 32);
291 uint z1_lt32 = (a0<<negCount) | (a1>>count) | uint ((a1<<negCount) != 0u);
292 z1 = mix(z1, z1_lt32, count < 32);
293 z1 = mix(z1, a1, count == 0);
294 z1Ptr = z1;
295 z0Ptr = z0;
296 }
297
298 /* Shifts the 96-bit value formed by concatenating `a0', `a1', and `a2' right
299 * by 32 _plus_ the number of bits given in `count'. The shifted result is
300 * at most 64 nonzero bits; these are broken into two 32-bit pieces which are
301 * stored at the locations pointed to by `z0Ptr' and `z1Ptr'. The bits shifted
302 * off form a third 32-bit result as follows: The _last_ bit shifted off is
303 * the most-significant bit of the extra result, and the other 31 bits of the
304 * extra result are all zero if and only if _all_but_the_last_ bits shifted off
305 * were all zero. This extra result is stored in the location pointed to by
306 * `z2Ptr'. The value of `count' can be arbitrarily large.
307 * (This routine makes more sense if `a0', `a1', and `a2' are considered
308 * to form a fixed-point value with binary point between `a1' and `a2'. This
309 * fixed-point value is shifted right by the number of bits given in `count',
310 * and the integer part of the result is returned at the locations pointed to
311 * by `z0Ptr' and `z1Ptr'. The fractional part of the result may be slightly
312 * corrupted as described above, and is returned at the location pointed to by
313 * `z2Ptr'.)
314 */
315 void
316 __shift64ExtraRightJamming(uint a0, uint a1, uint a2,
317 int count,
318 out uint z0Ptr,
319 out uint z1Ptr,
320 out uint z2Ptr)
321 {
322 uint z0 = 0u;
323 uint z1;
324 uint z2;
325 int negCount = (-count) & 31;
326
327 z2 = mix(uint(a0 != 0u), a0, count == 64);
328 z2 = mix(z2, a0 << negCount, count < 64);
329 z2 = mix(z2, a1 << negCount, count < 32);
330
331 z1 = mix(0u, (a0 >> (count & 31)), count < 64);
332 z1 = mix(z1, (a0<<negCount) | (a1>>count), count < 32);
333
334 a2 = mix(a2 | a1, a2, count < 32);
335 z0 = mix(z0, a0 >> count, count < 32);
336 z2 |= uint(a2 != 0u);
337
338 z0 = mix(z0, 0u, (count == 32));
339 z1 = mix(z1, a0, (count == 32));
340 z2 = mix(z2, a1, (count == 32));
341 z0 = mix(z0, a0, (count == 0));
342 z1 = mix(z1, a1, (count == 0));
343 z2 = mix(z2, a2, (count == 0));
344 z2Ptr = z2;
345 z1Ptr = z1;
346 z0Ptr = z0;
347 }
348
349 /* Shifts the 64-bit value formed by concatenating `a0' and `a1' left by the
350 * number of bits given in `count'. Any bits shifted off are lost. The value
351 * of `count' must be less than 32. The result is broken into two 32-bit
352 * pieces which are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
353 */
354 void
355 __shortShift64Left(uint a0, uint a1,
356 int count,
357 out uint z0Ptr,
358 out uint z1Ptr)
359 {
360 z1Ptr = a1<<count;
361 z0Ptr = mix((a0 << count | (a1 >> ((-count) & 31))), a0, count == 0);
362 }
363
364 /* Packs the sign `zSign', the exponent `zExp', and the significand formed by
365 * the concatenation of `zFrac0' and `zFrac1' into a double-precision floating-
366 * point value, returning the result. After being shifted into the proper
367 * positions, the three fields `zSign', `zExp', and `zFrac0' are simply added
368 * together to form the most significant 32 bits of the result. This means
369 * that any integer portion of `zFrac0' will be added into the exponent. Since
370 * a properly normalized significand will have an integer portion equal to 1,
371 * the `zExp' input should be 1 less than the desired result exponent whenever
372 * `zFrac0' and `zFrac1' concatenated form a complete, normalized significand.
373 */
374 uint64_t
375 __packFloat64(uint zSign, int zExp, uint zFrac0, uint zFrac1)
376 {
377 uvec2 z;
378
379 z.y = (zSign << 31) + (uint(zExp) << 20) + zFrac0;
380 z.x = zFrac1;
381 return packUint2x32(z);
382 }
383
384 /* Takes an abstract floating-point value having sign `zSign', exponent `zExp',
385 * and extended significand formed by the concatenation of `zFrac0', `zFrac1',
386 * and `zFrac2', and returns the proper double-precision floating-point value
387 * corresponding to the abstract input. Ordinarily, the abstract value is
388 * simply rounded and packed into the double-precision format, with the inexact
389 * exception raised if the abstract input cannot be represented exactly.
390 * However, if the abstract value is too large, the overflow and inexact
391 * exceptions are raised and an infinity or maximal finite value is returned.
392 * If the abstract value is too small, the input value is rounded to a
393 * subnormal number, and the underflow and inexact exceptions are raised if the
394 * abstract input cannot be represented exactly as a subnormal double-precision
395 * floating-point number.
396 * The input significand must be normalized or smaller. If the input
397 * significand is not normalized, `zExp' must be 0; in that case, the result
398 * returned is a subnormal number, and it must not require rounding. In the
399 * usual case that the input significand is normalized, `zExp' must be 1 less
400 * than the "true" floating-point exponent. The handling of underflow and
401 * overflow follows the IEEE Standard for Floating-Point Arithmetic.
402 */
403 uint64_t
404 __roundAndPackFloat64(uint zSign,
405 int zExp,
406 uint zFrac0,
407 uint zFrac1,
408 uint zFrac2)
409 {
410 bool roundNearestEven;
411 bool increment;
412
413 roundNearestEven = FLOAT_ROUNDING_MODE == FLOAT_ROUND_NEAREST_EVEN;
414 increment = int(zFrac2) < 0;
415 if (!roundNearestEven) {
416 if (FLOAT_ROUNDING_MODE == FLOAT_ROUND_TO_ZERO) {
417 increment = false;
418 } else {
419 if (zSign != 0u) {
420 increment = (FLOAT_ROUNDING_MODE == FLOAT_ROUND_DOWN) &&
421 (zFrac2 != 0u);
422 } else {
423 increment = (FLOAT_ROUNDING_MODE == FLOAT_ROUND_UP) &&
424 (zFrac2 != 0u);
425 }
426 }
427 }
428 if (0x7FD <= zExp) {
429 if ((0x7FD < zExp) ||
430 ((zExp == 0x7FD) &&
431 (0x001FFFFFu == zFrac0 && 0xFFFFFFFFu == zFrac1) &&
432 increment)) {
433 if ((FLOAT_ROUNDING_MODE == FLOAT_ROUND_TO_ZERO) ||
434 ((zSign != 0u) && (FLOAT_ROUNDING_MODE == FLOAT_ROUND_UP)) ||
435 ((zSign == 0u) && (FLOAT_ROUNDING_MODE == FLOAT_ROUND_DOWN))) {
436 return __packFloat64(zSign, 0x7FE, 0x000FFFFFu, 0xFFFFFFFFu);
437 }
438 return __packFloat64(zSign, 0x7FF, 0u, 0u);
439 }
440 if (zExp < 0) {
441 __shift64ExtraRightJamming(
442 zFrac0, zFrac1, zFrac2, -zExp, zFrac0, zFrac1, zFrac2);
443 zExp = 0;
444 if (roundNearestEven) {
445 increment = zFrac2 < 0u;
446 } else {
447 if (zSign != 0u) {
448 increment = (FLOAT_ROUNDING_MODE == FLOAT_ROUND_DOWN) &&
449 (zFrac2 != 0u);
450 } else {
451 increment = (FLOAT_ROUNDING_MODE == FLOAT_ROUND_UP) &&
452 (zFrac2 != 0u);
453 }
454 }
455 }
456 }
457 if (increment) {
458 __add64(zFrac0, zFrac1, 0u, 1u, zFrac0, zFrac1);
459 zFrac1 &= ~((zFrac2 + uint(zFrac2 == 0u)) & uint(roundNearestEven));
460 } else {
461 zExp = mix(zExp, 0, (zFrac0 | zFrac1) == 0u);
462 }
463 return __packFloat64(zSign, zExp, zFrac0, zFrac1);
464 }
465
466 uint64_t
467 __roundAndPackUInt64(uint zSign, uint zFrac0, uint zFrac1, uint zFrac2)
468 {
469 bool roundNearestEven;
470 bool increment;
471 uint64_t default_nan = 0xFFFFFFFFFFFFFFFFUL;
472
473 roundNearestEven = FLOAT_ROUNDING_MODE == FLOAT_ROUND_NEAREST_EVEN;
474
475 if (zFrac2 >= 0x80000000u)
476 increment = false;
477
478 if (!roundNearestEven) {
479 if (zSign != 0u) {
480 if ((FLOAT_ROUNDING_MODE == FLOAT_ROUND_DOWN) && (zFrac2 != 0u)) {
481 increment = false;
482 }
483 } else {
484 increment = (FLOAT_ROUNDING_MODE == FLOAT_ROUND_UP) &&
485 (zFrac2 != 0u);
486 }
487 }
488
489 if (increment) {
490 __add64(zFrac0, zFrac1, 0u, 1u, zFrac0, zFrac1);
491 if ((zFrac0 | zFrac1) != 0u)
492 zFrac1 &= ~(1u) + uint(zFrac2 == 0u) & uint(roundNearestEven);
493 }
494 return mix(packUint2x32(uvec2(zFrac1, zFrac0)), default_nan,
495 (zSign !=0u && (zFrac0 | zFrac1) != 0u));
496 }
497
498 int64_t
499 __roundAndPackInt64(uint zSign, uint zFrac0, uint zFrac1, uint zFrac2)
500 {
501 bool roundNearestEven;
502 bool increment;
503 int64_t default_NegNaN = -0x7FFFFFFFFFFFFFFEL;
504 int64_t default_PosNaN = 0xFFFFFFFFFFFFFFFFL;
505
506 roundNearestEven = FLOAT_ROUNDING_MODE == FLOAT_ROUND_NEAREST_EVEN;
507
508 if (zFrac2 >= 0x80000000u)
509 increment = false;
510
511 if (!roundNearestEven) {
512 if (zSign != 0u) {
513 increment = ((FLOAT_ROUNDING_MODE == FLOAT_ROUND_DOWN) &&
514 (zFrac2 != 0u));
515 } else {
516 increment = (FLOAT_ROUNDING_MODE == FLOAT_ROUND_UP) &&
517 (zFrac2 != 0u);
518 }
519 }
520
521 if (increment) {
522 __add64(zFrac0, zFrac1, 0u, 1u, zFrac0, zFrac1);
523 if ((zFrac0 | zFrac1) != 0u)
524 zFrac1 &= ~(1u) + uint(zFrac2 == 0u) & uint(roundNearestEven);
525 }
526
527 int64_t absZ = mix(int64_t(packUint2x32(uvec2(zFrac1, zFrac0))),
528 -int64_t(packUint2x32(uvec2(zFrac1, zFrac0))),
529 (zSign != 0u));
530 int64_t nan = mix(default_PosNaN, default_NegNaN, bool(zSign));
531 return mix(absZ, nan, bool(zSign ^ uint(absZ < 0)) && bool(absZ));
532 }
533
534 /* Returns the number of leading 0 bits before the most-significant 1 bit of
535 * `a'. If `a' is zero, 32 is returned.
536 */
537 int
538 __countLeadingZeros32(uint a)
539 {
540 int shiftCount;
541 shiftCount = mix(31 - findMSB(a), 32, a == 0u);
542 return shiftCount;
543 }
544
545 /* Takes an abstract floating-point value having sign `zSign', exponent `zExp',
546 * and significand formed by the concatenation of `zSig0' and `zSig1', and
547 * returns the proper double-precision floating-point value corresponding
548 * to the abstract input. This routine is just like `__roundAndPackFloat64'
549 * except that the input significand has fewer bits and does not have to be
550 * normalized. In all cases, `zExp' must be 1 less than the "true" floating-
551 * point exponent.
552 */
553 uint64_t
554 __normalizeRoundAndPackFloat64(uint zSign,
555 int zExp,
556 uint zFrac0,
557 uint zFrac1)
558 {
559 int shiftCount;
560 uint zFrac2;
561
562 if (zFrac0 == 0u) {
563 zExp -= 32;
564 zFrac0 = zFrac1;
565 zFrac1 = 0u;
566 }
567
568 shiftCount = __countLeadingZeros32(zFrac0) - 11;
569 if (0 <= shiftCount) {
570 zFrac2 = 0u;
571 __shortShift64Left(zFrac0, zFrac1, shiftCount, zFrac0, zFrac1);
572 } else {
573 __shift64ExtraRightJamming(
574 zFrac0, zFrac1, 0u, -shiftCount, zFrac0, zFrac1, zFrac2);
575 }
576 zExp -= shiftCount;
577 return __roundAndPackFloat64(zSign, zExp, zFrac0, zFrac1, zFrac2);
578 }
579
580 /* Takes two double-precision floating-point values `a' and `b', one of which
581 * is a NaN, and returns the appropriate NaN result.
582 */
583 uint64_t
584 __propagateFloat64NaN(uint64_t __a, uint64_t __b)
585 {
586 bool aIsNaN = __is_nan(__a);
587 bool bIsNaN = __is_nan(__b);
588 uvec2 a = unpackUint2x32(__a);
589 uvec2 b = unpackUint2x32(__b);
590 a.y |= 0x00080000u;
591 b.y |= 0x00080000u;
592
593 return packUint2x32(mix(b, mix(a, b, bvec2(bIsNaN, bIsNaN)), bvec2(aIsNaN, aIsNaN)));
594 }
595
596 /* Returns the result of adding the double-precision floating-point values
597 * `a' and `b'. The operation is performed according to the IEEE Standard for
598 * Floating-Point Arithmetic.
599 */
600 uint64_t
601 __fadd64(uint64_t a, uint64_t b)
602 {
603 uint aSign = __extractFloat64Sign(a);
604 uint bSign = __extractFloat64Sign(b);
605 uint aFracLo = __extractFloat64FracLo(a);
606 uint aFracHi = __extractFloat64FracHi(a);
607 uint bFracLo = __extractFloat64FracLo(b);
608 uint bFracHi = __extractFloat64FracHi(b);
609 int aExp = __extractFloat64Exp(a);
610 int bExp = __extractFloat64Exp(b);
611 uint zFrac0 = 0u;
612 uint zFrac1 = 0u;
613 int expDiff = aExp - bExp;
614 if (aSign == bSign) {
615 uint zFrac2 = 0u;
616 int zExp;
617 bool orig_exp_diff_is_zero = (expDiff == 0);
618
619 if (orig_exp_diff_is_zero) {
620 if (aExp == 0x7FF) {
621 bool propagate = (aFracHi | aFracLo | bFracHi | bFracLo) != 0u;
622 return mix(a, __propagateFloat64NaN(a, b), propagate);
623 }
624 __add64(aFracHi, aFracLo, bFracHi, bFracLo, zFrac0, zFrac1);
625 if (aExp == 0)
626 return __packFloat64(aSign, 0, zFrac0, zFrac1);
627 zFrac2 = 0u;
628 zFrac0 |= 0x00200000u;
629 zExp = aExp;
630 __shift64ExtraRightJamming(
631 zFrac0, zFrac1, zFrac2, 1, zFrac0, zFrac1, zFrac2);
632 } else if (0 < expDiff) {
633 if (aExp == 0x7FF) {
634 bool propagate = (aFracHi | aFracLo) != 0u;
635 return mix(a, __propagateFloat64NaN(a, b), propagate);
636 }
637
638 expDiff = mix(expDiff, expDiff - 1, bExp == 0);
639 bFracHi = mix(bFracHi | 0x00100000u, bFracHi, bExp == 0);
640 __shift64ExtraRightJamming(
641 bFracHi, bFracLo, 0u, expDiff, bFracHi, bFracLo, zFrac2);
642 zExp = aExp;
643 } else if (expDiff < 0) {
644 if (bExp == 0x7FF) {
645 bool propagate = (bFracHi | bFracLo) != 0u;
646 return mix(__packFloat64(aSign, 0x7ff, 0u, 0u), __propagateFloat64NaN(a, b), propagate);
647 }
648 expDiff = mix(expDiff, expDiff + 1, aExp == 0);
649 aFracHi = mix(aFracHi | 0x00100000u, aFracHi, aExp == 0);
650 __shift64ExtraRightJamming(
651 aFracHi, aFracLo, 0u, - expDiff, aFracHi, aFracLo, zFrac2);
652 zExp = bExp;
653 }
654 if (!orig_exp_diff_is_zero) {
655 aFracHi |= 0x00100000u;
656 __add64(aFracHi, aFracLo, bFracHi, bFracLo, zFrac0, zFrac1);
657 --zExp;
658 if (!(zFrac0 < 0x00200000u)) {
659 __shift64ExtraRightJamming(zFrac0, zFrac1, zFrac2, 1, zFrac0, zFrac1, zFrac2);
660 ++zExp;
661 }
662 }
663 return __roundAndPackFloat64(aSign, zExp, zFrac0, zFrac1, zFrac2);
664
665 } else {
666 int zExp;
667
668 __shortShift64Left(aFracHi, aFracLo, 10, aFracHi, aFracLo);
669 __shortShift64Left(bFracHi, bFracLo, 10, bFracHi, bFracLo);
670 if (0 < expDiff) {
671 if (aExp == 0x7FF) {
672 bool propagate = (aFracHi | aFracLo) != 0u;
673 return mix(a, __propagateFloat64NaN(a, b), propagate);
674 }
675 expDiff = mix(expDiff, expDiff - 1, bExp == 0);
676 bFracHi = mix(bFracHi | 0x40000000u, bFracHi, bExp == 0);
677 __shift64RightJamming(bFracHi, bFracLo, expDiff, bFracHi, bFracLo);
678 aFracHi |= 0x40000000u;
679 __sub64(aFracHi, aFracLo, bFracHi, bFracLo, zFrac0, zFrac1);
680 zExp = aExp;
681 --zExp;
682 return __normalizeRoundAndPackFloat64(aSign, zExp - 10, zFrac0, zFrac1);
683 }
684 if (expDiff < 0) {
685 if (bExp == 0x7FF) {
686 bool propagate = (bFracHi | bFracLo) != 0u;
687 return mix(__packFloat64(aSign ^ 1u, 0x7ff, 0u, 0u), __propagateFloat64NaN(a, b), propagate);
688 }
689 expDiff = mix(expDiff, expDiff + 1, aExp == 0);
690 aFracHi = mix(aFracHi | 0x40000000u, aFracHi, aExp == 0);
691 __shift64RightJamming(aFracHi, aFracLo, - expDiff, aFracHi, aFracLo);
692 bFracHi |= 0x40000000u;
693 __sub64(bFracHi, bFracLo, aFracHi, aFracLo, zFrac0, zFrac1);
694 zExp = bExp;
695 aSign ^= 1u;
696 --zExp;
697 return __normalizeRoundAndPackFloat64(aSign, zExp - 10, zFrac0, zFrac1);
698 }
699 if (aExp == 0x7FF) {
700 bool propagate = (aFracHi | aFracLo | bFracHi | bFracLo) != 0u;
701 return mix(0xFFFFFFFFFFFFFFFFUL, __propagateFloat64NaN(a, b), propagate);
702 }
703 bExp = mix(bExp, 1, aExp == 0);
704 aExp = mix(aExp, 1, aExp == 0);
705 bool zexp_normal = false;
706 bool blta = true;
707 if (bFracHi < aFracHi) {
708 __sub64(aFracHi, aFracLo, bFracHi, bFracLo, zFrac0, zFrac1);
709 zexp_normal = true;
710 }
711 else if (aFracHi < bFracHi) {
712 __sub64(bFracHi, bFracLo, aFracHi, aFracLo, zFrac0, zFrac1);
713 blta = false;
714 zexp_normal = true;
715 }
716 else if (bFracLo < aFracLo) {
717 __sub64(aFracHi, aFracLo, bFracHi, bFracLo, zFrac0, zFrac1);
718 zexp_normal = true;
719 }
720 else if (aFracLo < bFracLo) {
721 __sub64(bFracHi, bFracLo, aFracHi, aFracLo, zFrac0, zFrac1);
722 blta = false;
723 zexp_normal = true;
724 }
725 zExp = mix(bExp, aExp, blta);
726 aSign = mix(aSign ^ 1u, aSign, blta);
727 uint64_t retval_0 = __packFloat64(uint(FLOAT_ROUNDING_MODE == FLOAT_ROUND_DOWN), 0, 0u, 0u);
728 uint64_t retval_1 = __normalizeRoundAndPackFloat64(aSign, zExp - 11, zFrac0, zFrac1);
729 return mix(retval_0, retval_1, zexp_normal);
730 }
731 }
732
733 /* Multiplies the 64-bit value formed by concatenating `a0' and `a1' to the
734 * 64-bit value formed by concatenating `b0' and `b1' to obtain a 128-bit
735 * product. The product is broken into four 32-bit pieces which are stored at
736 * the locations pointed to by `z0Ptr', `z1Ptr', `z2Ptr', and `z3Ptr'.
737 */
738 void
739 __mul64To128(uint a0, uint a1, uint b0, uint b1,
740 out uint z0Ptr,
741 out uint z1Ptr,
742 out uint z2Ptr,
743 out uint z3Ptr)
744 {
745 uint z0 = 0u;
746 uint z1 = 0u;
747 uint z2 = 0u;
748 uint z3 = 0u;
749 uint more1 = 0u;
750 uint more2 = 0u;
751
752 umulExtended(a1, b1, z2, z3);
753 umulExtended(a1, b0, z1, more2);
754 __add64(z1, more2, 0u, z2, z1, z2);
755 umulExtended(a0, b0, z0, more1);
756 __add64(z0, more1, 0u, z1, z0, z1);
757 umulExtended(a0, b1, more1, more2);
758 __add64(more1, more2, 0u, z2, more1, z2);
759 __add64(z0, z1, 0u, more1, z0, z1);
760 z3Ptr = z3;
761 z2Ptr = z2;
762 z1Ptr = z1;
763 z0Ptr = z0;
764 }
765
766 /* Normalizes the subnormal double-precision floating-point value represented
767 * by the denormalized significand formed by the concatenation of `aFrac0' and
768 * `aFrac1'. The normalized exponent is stored at the location pointed to by
769 * `zExpPtr'. The most significant 21 bits of the normalized significand are
770 * stored at the location pointed to by `zFrac0Ptr', and the least significant
771 * 32 bits of the normalized significand are stored at the location pointed to
772 * by `zFrac1Ptr'.
773 */
774 void
775 __normalizeFloat64Subnormal(uint aFrac0, uint aFrac1,
776 out int zExpPtr,
777 out uint zFrac0Ptr,
778 out uint zFrac1Ptr)
779 {
780 int shiftCount;
781 uint temp_zfrac0, temp_zfrac1;
782 shiftCount = __countLeadingZeros32(mix(aFrac0, aFrac1, aFrac0 == 0u)) - 11;
783 zExpPtr = mix(1 - shiftCount, -shiftCount - 31, aFrac0 == 0u);
784
785 temp_zfrac0 = mix(aFrac1<<shiftCount, aFrac1>>(-shiftCount), shiftCount < 0);
786 temp_zfrac1 = mix(0u, aFrac1<<(shiftCount & 31), shiftCount < 0);
787
788 __shortShift64Left(aFrac0, aFrac1, shiftCount, zFrac0Ptr, zFrac1Ptr);
789
790 zFrac0Ptr = mix(zFrac0Ptr, temp_zfrac0, aFrac0 == 0);
791 zFrac1Ptr = mix(zFrac1Ptr, temp_zfrac1, aFrac0 == 0);
792 }
793
794 /* Returns the result of multiplying the double-precision floating-point values
795 * `a' and `b'. The operation is performed according to the IEEE Standard for
796 * Floating-Point Arithmetic.
797 */
798 uint64_t
799 __fmul64(uint64_t a, uint64_t b)
800 {
801 uint zFrac0 = 0u;
802 uint zFrac1 = 0u;
803 uint zFrac2 = 0u;
804 uint zFrac3 = 0u;
805 int zExp;
806
807 uint aFracLo = __extractFloat64FracLo(a);
808 uint aFracHi = __extractFloat64FracHi(a);
809 uint bFracLo = __extractFloat64FracLo(b);
810 uint bFracHi = __extractFloat64FracHi(b);
811 int aExp = __extractFloat64Exp(a);
812 uint aSign = __extractFloat64Sign(a);
813 int bExp = __extractFloat64Exp(b);
814 uint bSign = __extractFloat64Sign(b);
815 uint zSign = aSign ^ bSign;
816 if (aExp == 0x7FF) {
817 if (((aFracHi | aFracLo) != 0u) ||
818 ((bExp == 0x7FF) && ((bFracHi | bFracLo) != 0u))) {
819 return __propagateFloat64NaN(a, b);
820 }
821 if ((uint(bExp) | bFracHi | bFracLo) == 0u)
822 return 0xFFFFFFFFFFFFFFFFUL;
823 return __packFloat64(zSign, 0x7FF, 0u, 0u);
824 }
825 if (bExp == 0x7FF) {
826 if ((bFracHi | bFracLo) != 0u)
827 return __propagateFloat64NaN(a, b);
828 if ((uint(aExp) | aFracHi | aFracLo) == 0u)
829 return 0xFFFFFFFFFFFFFFFFUL;
830 return __packFloat64(zSign, 0x7FF, 0u, 0u);
831 }
832 if (aExp == 0) {
833 if ((aFracHi | aFracLo) == 0u)
834 return __packFloat64(zSign, 0, 0u, 0u);
835 __normalizeFloat64Subnormal(aFracHi, aFracLo, aExp, aFracHi, aFracLo);
836 }
837 if (bExp == 0) {
838 if ((bFracHi | bFracLo) == 0u)
839 return __packFloat64(zSign, 0, 0u, 0u);
840 __normalizeFloat64Subnormal(bFracHi, bFracLo, bExp, bFracHi, bFracLo);
841 }
842 zExp = aExp + bExp - 0x400;
843 aFracHi |= 0x00100000u;
844 __shortShift64Left(bFracHi, bFracLo, 12, bFracHi, bFracLo);
845 __mul64To128(
846 aFracHi, aFracLo, bFracHi, bFracLo, zFrac0, zFrac1, zFrac2, zFrac3);
847 __add64(zFrac0, zFrac1, aFracHi, aFracLo, zFrac0, zFrac1);
848 zFrac2 |= uint(zFrac3 != 0u);
849 if (0x00200000u <= zFrac0) {
850 __shift64ExtraRightJamming(
851 zFrac0, zFrac1, zFrac2, 1, zFrac0, zFrac1, zFrac2);
852 ++zExp;
853 }
854 return __roundAndPackFloat64(zSign, zExp, zFrac0, zFrac1, zFrac2);
855 }
856
857 uint64_t
858 __ffma64(uint64_t a, uint64_t b, uint64_t c)
859 {
860 return __fadd64(__fmul64(a, b), c);
861 }
862
863 /* Shifts the 64-bit value formed by concatenating `a0' and `a1' right by the
864 * number of bits given in `count'. Any bits shifted off are lost. The value
865 * of `count' can be arbitrarily large; in particular, if `count' is greater
866 * than 64, the result will be 0. The result is broken into two 32-bit pieces
867 * which are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
868 */
869 void
870 __shift64Right(uint a0, uint a1,
871 int count,
872 out uint z0Ptr,
873 out uint z1Ptr)
874 {
875 uint z0;
876 uint z1;
877 int negCount = (-count) & 31;
878
879 z0 = 0u;
880 z0 = mix(z0, (a0 >> count), count < 32);
881 z0 = mix(z0, a0, count == 0);
882
883 z1 = mix(0u, (a0 >> (count & 31)), count < 64);
884 z1 = mix(z1, (a0<<negCount) | (a1>>count), count < 32);
885 z1 = mix(z1, a0, count == 0);
886
887 z1Ptr = z1;
888 z0Ptr = z0;
889 }
890
891 /* Returns the result of converting the double-precision floating-point value
892 * `a' to the unsigned integer format. The conversion is performed according
893 * to the IEEE Standard for Floating-Point Arithmetic.
894 */
895 uint
896 __fp64_to_uint(uint64_t a)
897 {
898 uint aFracLo = __extractFloat64FracLo(a);
899 uint aFracHi = __extractFloat64FracHi(a);
900 int aExp = __extractFloat64Exp(a);
901 uint aSign = __extractFloat64Sign(a);
902
903 if ((aExp == 0x7FF) && ((aFracHi | aFracLo) != 0u))
904 return 0xFFFFFFFFu;
905
906 aFracHi |= mix(0u, 0x00100000u, aExp != 0);
907
908 int shiftDist = 0x427 - aExp;
909 if (0 < shiftDist)
910 __shift64RightJamming(aFracHi, aFracLo, shiftDist, aFracHi, aFracLo);
911
912 if ((aFracHi & 0xFFFFF000u) != 0u)
913 return mix(~0u, 0u, (aSign != 0u));
914
915 uint z = 0u;
916 uint zero = 0u;
917 __shift64Right(aFracHi, aFracLo, 12, zero, z);
918
919 uint expt = mix(~0u, 0u, (aSign != 0u));
920
921 return mix(z, expt, (aSign != 0u) && (z != 0u));
922 }
923
924 uint64_t
925 __uint_to_fp64(uint a)
926 {
927 if (a == 0u)
928 return 0ul;
929
930 int shiftDist = __countLeadingZeros32(a) + 21;
931
932 uint aHigh = 0u;
933 uint aLow = 0u;
934 int negCount = (- shiftDist) & 31;
935
936 aHigh = mix(0u, a<< shiftDist - 32, shiftDist < 64);
937 aLow = 0u;
938 aHigh = mix(aHigh, 0u, shiftDist == 0);
939 aLow = mix(aLow, a, shiftDist ==0);
940 aHigh = mix(aHigh, a >> negCount, shiftDist < 32);
941 aLow = mix(aLow, a << shiftDist, shiftDist < 32);
942
943 return __packFloat64(0u, 0x432 - shiftDist, aHigh, aLow);
944 }
945
946 uint64_t
947 __uint64_to_fp64(uint64_t a)
948 {
949 if (a == 0u)
950 return 0ul;
951
952 uvec2 aFrac = unpackUint2x32(a);
953 uint aFracLo = __extractFloat64FracLo(a);
954 uint aFracHi = __extractFloat64FracHi(a);
955
956 if ((aFracHi & 0x80000000u) != 0u) {
957 __shift64RightJamming(aFracHi, aFracLo, 1, aFracHi, aFracLo);
958 return __roundAndPackFloat64(0, 0x433, aFracHi, aFracLo, 0u);
959 } else {
960 return __normalizeRoundAndPackFloat64(0, 0x432, aFrac.y, aFrac.x);
961 }
962 }
963
964 uint64_t
965 __fp64_to_uint64(uint64_t a)
966 {
967 uint aFracLo = __extractFloat64FracLo(a);
968 uint aFracHi = __extractFloat64FracHi(a);
969 int aExp = __extractFloat64Exp(a);
970 uint aSign = __extractFloat64Sign(a);
971 uint zFrac2 = 0u;
972 uint64_t default_nan = 0xFFFFFFFFFFFFFFFFUL;
973
974 aFracHi = mix(aFracHi, aFracHi | 0x00100000u, aExp != 0);
975 int shiftCount = 0x433 - aExp;
976
977 if ( shiftCount <= 0 ) {
978 if (shiftCount < -11 && aExp == 0x7FF) {
979 if ((aFracHi | aFracLo) != 0u)
980 return __propagateFloat64NaN(a, a);
981 return mix(default_nan, a, aSign == 0u);
982 }
983 __shortShift64Left(aFracHi, aFracLo, -shiftCount, aFracHi, aFracLo);
984 } else {
985 __shift64ExtraRightJamming(aFracHi, aFracLo, zFrac2, shiftCount,
986 aFracHi, aFracLo, zFrac2);
987 }
988 return __roundAndPackUInt64(aSign, aFracHi, aFracLo, zFrac2);
989 }
990
991 int64_t
992 __fp64_to_int64(uint64_t a)
993 {
994 uint zFrac2 = 0u;
995 uint aFracLo = __extractFloat64FracLo(a);
996 uint aFracHi = __extractFloat64FracHi(a);
997 int aExp = __extractFloat64Exp(a);
998 uint aSign = __extractFloat64Sign(a);
999 int64_t default_NegNaN = -0x7FFFFFFFFFFFFFFEL;
1000 int64_t default_PosNaN = 0xFFFFFFFFFFFFFFFFL;
1001
1002 aFracHi = mix(aFracHi, aFracHi | 0x00100000u, aExp != 0);
1003 int shiftCount = 0x433 - aExp;
1004
1005 if (shiftCount <= 0) {
1006 if (shiftCount < -11 && aExp == 0x7FF) {
1007 if ((aFracHi | aFracLo) != 0u)
1008 return default_NegNaN;
1009 return mix(default_NegNaN, default_PosNaN, aSign == 0u);
1010 }
1011 __shortShift64Left(aFracHi, aFracLo, -shiftCount, aFracHi, aFracLo);
1012 } else {
1013 __shift64ExtraRightJamming(aFracHi, aFracLo, zFrac2, shiftCount,
1014 aFracHi, aFracLo, zFrac2);
1015 }
1016
1017 return __roundAndPackInt64(aSign, aFracHi, aFracLo, zFrac2);
1018 }
1019
1020 uint64_t
1021 __fp32_to_uint64(float f)
1022 {
1023 uint a = floatBitsToUint(f);
1024 uint aFrac = a & 0x007FFFFFu;
1025 int aExp = int((a>>23) & 0xFFu);
1026 uint aSign = a>>31;
1027 uint zFrac0 = 0u;
1028 uint zFrac1 = 0u;
1029 uint zFrac2 = 0u;
1030 uint64_t default_nan = 0xFFFFFFFFFFFFFFFFUL;
1031 int shiftCount = 0xBE - aExp;
1032
1033 if (shiftCount <0) {
1034 if (aExp == 0xFF)
1035 return default_nan;
1036 }
1037
1038 aFrac = mix(aFrac, aFrac | 0x00800000u, aExp != 0);
1039 __shortShift64Left(aFrac, 0, 40, zFrac0, zFrac1);
1040
1041 if (shiftCount != 0) {
1042 __shift64ExtraRightJamming(zFrac0, zFrac1, zFrac2, shiftCount,
1043 zFrac0, zFrac1, zFrac2);
1044 }
1045
1046 return __roundAndPackUInt64(aSign, zFrac0, zFrac1, zFrac2);
1047 }
1048
1049 int64_t
1050 __fp32_to_int64(float f)
1051 {
1052 uint a = floatBitsToUint(f);
1053 uint aFrac = a & 0x007FFFFFu;
1054 int aExp = int((a>>23) & 0xFFu);
1055 uint aSign = a>>31;
1056 uint zFrac0 = 0u;
1057 uint zFrac1 = 0u;
1058 uint zFrac2 = 0u;
1059 int64_t default_NegNaN = -0x7FFFFFFFFFFFFFFEL;
1060 int64_t default_PosNaN = 0xFFFFFFFFFFFFFFFFL;
1061 int shiftCount = 0xBE - aExp;
1062
1063 if (shiftCount <0) {
1064 if (aExp == 0xFF && aFrac != 0u)
1065 return default_NegNaN;
1066 return mix(default_NegNaN, default_PosNaN, aSign == 0u);
1067 }
1068
1069 aFrac = mix(aFrac, aFrac | 0x00800000u, aExp != 0);
1070 __shortShift64Left(aFrac, 0, 40, zFrac0, zFrac1);
1071
1072 if (shiftCount != 0) {
1073 __shift64ExtraRightJamming(zFrac0, zFrac1, zFrac2, shiftCount,
1074 zFrac0, zFrac1, zFrac2);
1075 }
1076
1077 return __roundAndPackInt64(aSign, zFrac0, zFrac1, zFrac2);
1078 }
1079
1080 uint64_t
1081 __int64_to_fp64(int64_t a)
1082 {
1083 if (a==0)
1084 return 0ul;
1085
1086 uint64_t absA = mix(uint64_t(a), uint64_t(-a), a < 0);
1087 uint aFracHi = __extractFloat64FracHi(absA);
1088 uvec2 aFrac = unpackUint2x32(absA);
1089 uint zSign = uint(a < 0);
1090
1091 if ((aFracHi & 0x80000000u) != 0u) {
1092 return mix(0ul, __packFloat64(1, 0x434, 0u, 0u), a < 0);
1093 }
1094
1095 return __normalizeRoundAndPackFloat64(zSign, 0x432, aFrac.y, aFrac.x);
1096 }
1097
1098 /* Returns the result of converting the double-precision floating-point value
1099 * `a' to the 32-bit two's complement integer format. The conversion is
1100 * performed according to the IEEE Standard for Floating-Point Arithmetic---
1101 * which means in particular that the conversion is rounded according to the
1102 * current rounding mode. If `a' is a NaN, the largest positive integer is
1103 * returned. Otherwise, if the conversion overflows, the largest integer with
1104 * the same sign as `a' is returned.
1105 */
1106 int
1107 __fp64_to_int(uint64_t a)
1108 {
1109 uint aFracLo = __extractFloat64FracLo(a);
1110 uint aFracHi = __extractFloat64FracHi(a);
1111 int aExp = __extractFloat64Exp(a);
1112 uint aSign = __extractFloat64Sign(a);
1113
1114 uint absZ = 0u;
1115 uint aFracExtra = 0u;
1116 int shiftCount = aExp - 0x413;
1117
1118 if (0 <= shiftCount) {
1119 if (0x41E < aExp) {
1120 if ((aExp == 0x7FF) && bool(aFracHi | aFracLo))
1121 aSign = 0u;
1122 return mix(0x7FFFFFFF, 0x80000000, bool(aSign));
1123 }
1124 __shortShift64Left(aFracHi | 0x00100000u, aFracLo, shiftCount, absZ, aFracExtra);
1125 } else {
1126 if (aExp < 0x3FF)
1127 return 0;
1128
1129 aFracHi |= 0x00100000u;
1130 aFracExtra = ( aFracHi << (shiftCount & 31)) | aFracLo;
1131 absZ = aFracHi >> (- shiftCount);
1132 }
1133
1134 int z = mix(int(absZ), -int(absZ), (aSign != 0u));
1135 int nan = mix(0x7FFFFFFF, 0x80000000, bool(aSign));
1136 return mix(z, nan, bool(aSign ^ uint(z < 0)) && bool(z));
1137 }
1138
1139 /* Returns the result of converting the 32-bit two's complement integer `a'
1140 * to the double-precision floating-point format. The conversion is performed
1141 * according to the IEEE Standard for Floating-Point Arithmetic.
1142 */
1143 uint64_t
1144 __int_to_fp64(int a)
1145 {
1146 uint zFrac0 = 0u;
1147 uint zFrac1 = 0u;
1148 if (a==0)
1149 return __packFloat64(0u, 0, 0u, 0u);
1150 uint zSign = uint(a < 0);
1151 uint absA = mix(uint(a), uint(-a), a < 0);
1152 int shiftCount = __countLeadingZeros32(absA) - 11;
1153 if (0 <= shiftCount) {
1154 zFrac0 = absA << shiftCount;
1155 zFrac1 = 0u;
1156 } else {
1157 __shift64Right(absA, 0u, -shiftCount, zFrac0, zFrac1);
1158 }
1159 return __packFloat64(zSign, 0x412 - shiftCount, zFrac0, zFrac1);
1160 }
1161
1162 bool
1163 __fp64_to_bool(uint64_t a)
1164 {
1165 return !__feq64_nonnan(__fabs64(a), 0ul);
1166 }
1167
1168 uint64_t
1169 __bool_to_fp64(bool a)
1170 {
1171 return packUint2x32(uvec2(0x00000000u, uint(-int(a) & 0x3ff00000)));
1172 }
1173
1174 /* Packs the sign `zSign', exponent `zExp', and significand `zFrac' into a
1175 * single-precision floating-point value, returning the result. After being
1176 * shifted into the proper positions, the three fields are simply added
1177 * together to form the result. This means that any integer portion of `zSig'
1178 * will be added into the exponent. Since a properly normalized significand
1179 * will have an integer portion equal to 1, the `zExp' input should be 1 less
1180 * than the desired result exponent whenever `zFrac' is a complete, normalized
1181 * significand.
1182 */
1183 float
1184 __packFloat32(uint zSign, int zExp, uint zFrac)
1185 {
1186 return uintBitsToFloat((zSign<<31) + (uint(zExp)<<23) + zFrac);
1187 }
1188
1189 /* Takes an abstract floating-point value having sign `zSign', exponent `zExp',
1190 * and significand `zFrac', and returns the proper single-precision floating-
1191 * point value corresponding to the abstract input. Ordinarily, the abstract
1192 * value is simply rounded and packed into the single-precision format, with
1193 * the inexact exception raised if the abstract input cannot be represented
1194 * exactly. However, if the abstract value is too large, the overflow and
1195 * inexact exceptions are raised and an infinity or maximal finite value is
1196 * returned. If the abstract value is too small, the input value is rounded to
1197 * a subnormal number, and the underflow and inexact exceptions are raised if
1198 * the abstract input cannot be represented exactly as a subnormal single-
1199 * precision floating-point number.
1200 * The input significand `zFrac' has its binary point between bits 30
1201 * and 29, which is 7 bits to the left of the usual location. This shifted
1202 * significand must be normalized or smaller. If `zFrac' is not normalized,
1203 * `zExp' must be 0; in that case, the result returned is a subnormal number,
1204 * and it must not require rounding. In the usual case that `zFrac' is
1205 * normalized, `zExp' must be 1 less than the "true" floating-point exponent.
1206 * The handling of underflow and overflow follows the IEEE Standard for
1207 * Floating-Point Arithmetic.
1208 */
1209 float
1210 __roundAndPackFloat32(uint zSign, int zExp, uint zFrac)
1211 {
1212 bool roundNearestEven;
1213 int roundIncrement;
1214 int roundBits;
1215
1216 roundNearestEven = FLOAT_ROUNDING_MODE == FLOAT_ROUND_NEAREST_EVEN;
1217 roundIncrement = 0x40;
1218 if (!roundNearestEven) {
1219 if (FLOAT_ROUNDING_MODE == FLOAT_ROUND_TO_ZERO) {
1220 roundIncrement = 0;
1221 } else {
1222 roundIncrement = 0x7F;
1223 if (zSign != 0u) {
1224 if (FLOAT_ROUNDING_MODE == FLOAT_ROUND_UP)
1225 roundIncrement = 0;
1226 } else {
1227 if (FLOAT_ROUNDING_MODE == FLOAT_ROUND_DOWN)
1228 roundIncrement = 0;
1229 }
1230 }
1231 }
1232 roundBits = int(zFrac & 0x7Fu);
1233 if (0xFDu <= uint(zExp)) {
1234 if ((0xFD < zExp) || ((zExp == 0xFD) && (int(zFrac) + roundIncrement) < 0))
1235 return __packFloat32(zSign, 0xFF, 0u) - float(roundIncrement == 0);
1236 int count = -zExp;
1237 bool zexp_lt0 = zExp < 0;
1238 uint zFrac_lt0 = mix(uint(zFrac != 0u), (zFrac>>count) | uint((zFrac<<((-count) & 31)) != 0u), (-zExp) < 32);
1239 zFrac = mix(zFrac, zFrac_lt0, zexp_lt0);
1240 roundBits = mix(roundBits, int(zFrac) & 0x7f, zexp_lt0);
1241 zExp = mix(zExp, 0, zexp_lt0);
1242 }
1243 zFrac = (zFrac + uint(roundIncrement))>>7;
1244 zFrac &= ~uint(((roundBits ^ 0x40) == 0) && roundNearestEven);
1245
1246 return __packFloat32(zSign, mix(zExp, 0, zFrac == 0u), zFrac);
1247 }
1248
1249 /* Returns the result of converting the double-precision floating-point value
1250 * `a' to the single-precision floating-point format. The conversion is
1251 * performed according to the IEEE Standard for Floating-Point Arithmetic.
1252 */
1253 float
1254 __fp64_to_fp32(uint64_t __a)
1255 {
1256 uvec2 a = unpackUint2x32(__a);
1257 uint zFrac = 0u;
1258 uint allZero = 0u;
1259
1260 uint aFracLo = __extractFloat64FracLo(__a);
1261 uint aFracHi = __extractFloat64FracHi(__a);
1262 int aExp = __extractFloat64Exp(__a);
1263 uint aSign = __extractFloat64Sign(__a);
1264 if (aExp == 0x7FF) {
1265 __shortShift64Left(a.y, a.x, 12, a.y, a.x);
1266 float rval = uintBitsToFloat((aSign<<31) | 0x7FC00000u | (a.y>>9));
1267 rval = mix(__packFloat32(aSign, 0xFF, 0u), rval, (aFracHi | aFracLo) != 0u);
1268 return rval;
1269 }
1270 __shift64RightJamming(aFracHi, aFracLo, 22, allZero, zFrac);
1271 zFrac = mix(zFrac, zFrac | 0x40000000u, aExp != 0);
1272 return __roundAndPackFloat32(aSign, aExp - 0x381, zFrac);
1273 }
1274
1275 float
1276 __uint64_to_fp32(uint64_t __a)
1277 {
1278 uvec2 aFrac = unpackUint2x32(__a);
1279 int shiftCount = mix(__countLeadingZeros32(aFrac.y) - 33,
1280 __countLeadingZeros32(aFrac.x) - 1,
1281 aFrac.y == 0u);
1282
1283 if (0 <= shiftCount)
1284 __shortShift64Left(aFrac.y, aFrac.x, shiftCount, aFrac.y, aFrac.x);
1285 else
1286 __shift64RightJamming(aFrac.y, aFrac.x, -shiftCount, aFrac.y, aFrac.x);
1287
1288 return __roundAndPackFloat32(0u, 0x9C - shiftCount, aFrac.x);
1289 }
1290
1291 float
1292 __int64_to_fp32(int64_t __a)
1293 {
1294 uint aSign = uint(__a < 0);
1295 uint64_t absA = mix(uint64_t(__a), uint64_t(-__a), __a < 0);
1296 uvec2 aFrac = unpackUint2x32(absA);
1297 int shiftCount = mix(__countLeadingZeros32(aFrac.y) - 33,
1298 __countLeadingZeros32(aFrac.x) - 1,
1299 aFrac.y == 0u);
1300
1301 if (0 <= shiftCount)
1302 __shortShift64Left(aFrac.y, aFrac.x, shiftCount, aFrac.y, aFrac.x);
1303 else
1304 __shift64RightJamming(aFrac.y, aFrac.x, -shiftCount, aFrac.y, aFrac.x);
1305
1306 return __roundAndPackFloat32(aSign, 0x9C - shiftCount, aFrac.x);
1307 }
1308
1309 /* Returns the result of converting the single-precision floating-point value
1310 * `a' to the double-precision floating-point format.
1311 */
1312 uint64_t
1313 __fp32_to_fp64(float f)
1314 {
1315 uint a = floatBitsToUint(f);
1316 uint aFrac = a & 0x007FFFFFu;
1317 int aExp = int((a>>23) & 0xFFu);
1318 uint aSign = a>>31;
1319 uint zFrac0 = 0u;
1320 uint zFrac1 = 0u;
1321
1322 if (aExp == 0xFF) {
1323 if (aFrac != 0u) {
1324 uint nanLo = 0u;
1325 uint nanHi = a<<9;
1326 __shift64Right(nanHi, nanLo, 12, nanHi, nanLo);
1327 nanHi |= ((aSign<<31) | 0x7FF80000u);
1328 return packUint2x32(uvec2(nanLo, nanHi));
1329 }
1330 return __packFloat64(aSign, 0x7FF, 0u, 0u);
1331 }
1332
1333 if (aExp == 0) {
1334 if (aFrac == 0u)
1335 return __packFloat64(aSign, 0, 0u, 0u);
1336 /* Normalize subnormal */
1337 int shiftCount = __countLeadingZeros32(aFrac) - 8;
1338 aFrac <<= shiftCount;
1339 aExp = 1 - shiftCount;
1340 --aExp;
1341 }
1342
1343 __shift64Right(aFrac, 0u, 3, zFrac0, zFrac1);
1344 return __packFloat64(aSign, aExp + 0x380, zFrac0, zFrac1);
1345 }
1346
1347 /* Adds the 96-bit value formed by concatenating `a0', `a1', and `a2' to the
1348 * 96-bit value formed by concatenating `b0', `b1', and `b2'. Addition is
1349 * modulo 2^96, so any carry out is lost. The result is broken into three
1350 * 32-bit pieces which are stored at the locations pointed to by `z0Ptr',
1351 * `z1Ptr', and `z2Ptr'.
1352 */
1353 void
1354 __add96(uint a0, uint a1, uint a2,
1355 uint b0, uint b1, uint b2,
1356 out uint z0Ptr,
1357 out uint z1Ptr,
1358 out uint z2Ptr)
1359 {
1360 uint z2 = a2 + b2;
1361 uint carry1 = uint(z2 < a2);
1362 uint z1 = a1 + b1;
1363 uint carry0 = uint(z1 < a1);
1364 uint z0 = a0 + b0;
1365 z1 += carry1;
1366 z0 += uint(z1 < carry1);
1367 z0 += carry0;
1368 z2Ptr = z2;
1369 z1Ptr = z1;
1370 z0Ptr = z0;
1371 }
1372
1373 /* Subtracts the 96-bit value formed by concatenating `b0', `b1', and `b2' from
1374 * the 96-bit value formed by concatenating `a0', `a1', and `a2'. Subtraction
1375 * is modulo 2^96, so any borrow out (carry out) is lost. The result is broken
1376 * into three 32-bit pieces which are stored at the locations pointed to by
1377 * `z0Ptr', `z1Ptr', and `z2Ptr'.
1378 */
1379 void
1380 __sub96(uint a0, uint a1, uint a2,
1381 uint b0, uint b1, uint b2,
1382 out uint z0Ptr,
1383 out uint z1Ptr,
1384 out uint z2Ptr)
1385 {
1386 uint z2 = a2 - b2;
1387 uint borrow1 = uint(a2 < b2);
1388 uint z1 = a1 - b1;
1389 uint borrow0 = uint(a1 < b1);
1390 uint z0 = a0 - b0;
1391 z0 -= uint(z1 < borrow1);
1392 z1 -= borrow1;
1393 z0 -= borrow0;
1394 z2Ptr = z2;
1395 z1Ptr = z1;
1396 z0Ptr = z0;
1397 }
1398
1399 /* Returns an approximation to the 32-bit integer quotient obtained by dividing
1400 * `b' into the 64-bit value formed by concatenating `a0' and `a1'. The
1401 * divisor `b' must be at least 2^31. If q is the exact quotient truncated
1402 * toward zero, the approximation returned lies between q and q + 2 inclusive.
1403 * If the exact quotient q is larger than 32 bits, the maximum positive 32-bit
1404 * unsigned integer is returned.
1405 */
1406 uint
1407 __estimateDiv64To32(uint a0, uint a1, uint b)
1408 {
1409 uint b0;
1410 uint b1;
1411 uint rem0 = 0u;
1412 uint rem1 = 0u;
1413 uint term0 = 0u;
1414 uint term1 = 0u;
1415 uint z;
1416
1417 if (b <= a0)
1418 return 0xFFFFFFFFu;
1419 b0 = b>>16;
1420 z = (b0<<16 <= a0) ? 0xFFFF0000u : (a0 / b0)<<16;
1421 umulExtended(b, z, term0, term1);
1422 __sub64(a0, a1, term0, term1, rem0, rem1);
1423 while (int(rem0) < 0) {
1424 z -= 0x10000u;
1425 b1 = b<<16;
1426 __add64(rem0, rem1, b0, b1, rem0, rem1);
1427 }
1428 rem0 = (rem0<<16) | (rem1>>16);
1429 z |= (b0<<16 <= rem0) ? 0xFFFFu : rem0 / b0;
1430 return z;
1431 }
1432
1433 uint
1434 __sqrtOddAdjustments(int index)
1435 {
1436 uint res = 0u;
1437 if (index == 0)
1438 res = 0x0004u;
1439 if (index == 1)
1440 res = 0x0022u;
1441 if (index == 2)
1442 res = 0x005Du;
1443 if (index == 3)
1444 res = 0x00B1u;
1445 if (index == 4)
1446 res = 0x011Du;
1447 if (index == 5)
1448 res = 0x019Fu;
1449 if (index == 6)
1450 res = 0x0236u;
1451 if (index == 7)
1452 res = 0x02E0u;
1453 if (index == 8)
1454 res = 0x039Cu;
1455 if (index == 9)
1456 res = 0x0468u;
1457 if (index == 10)
1458 res = 0x0545u;
1459 if (index == 11)
1460 res = 0x631u;
1461 if (index == 12)
1462 res = 0x072Bu;
1463 if (index == 13)
1464 res = 0x0832u;
1465 if (index == 14)
1466 res = 0x0946u;
1467 if (index == 15)
1468 res = 0x0A67u;
1469
1470 return res;
1471 }
1472
1473 uint
1474 __sqrtEvenAdjustments(int index)
1475 {
1476 uint res = 0u;
1477 if (index == 0)
1478 res = 0x0A2Du;
1479 if (index == 1)
1480 res = 0x08AFu;
1481 if (index == 2)
1482 res = 0x075Au;
1483 if (index == 3)
1484 res = 0x0629u;
1485 if (index == 4)
1486 res = 0x051Au;
1487 if (index == 5)
1488 res = 0x0429u;
1489 if (index == 6)
1490 res = 0x0356u;
1491 if (index == 7)
1492 res = 0x029Eu;
1493 if (index == 8)
1494 res = 0x0200u;
1495 if (index == 9)
1496 res = 0x0179u;
1497 if (index == 10)
1498 res = 0x0109u;
1499 if (index == 11)
1500 res = 0x00AFu;
1501 if (index == 12)
1502 res = 0x0068u;
1503 if (index == 13)
1504 res = 0x0034u;
1505 if (index == 14)
1506 res = 0x0012u;
1507 if (index == 15)
1508 res = 0x0002u;
1509
1510 return res;
1511 }
1512
1513 /* Returns an approximation to the square root of the 32-bit significand given
1514 * by `a'. Considered as an integer, `a' must be at least 2^31. If bit 0 of
1515 * `aExp' (the least significant bit) is 1, the integer returned approximates
1516 * 2^31*sqrt(`a'/2^31), where `a' is considered an integer. If bit 0 of `aExp'
1517 * is 0, the integer returned approximates 2^31*sqrt(`a'/2^30). In either
1518 * case, the approximation returned lies strictly within +/-2 of the exact
1519 * value.
1520 */
1521 uint
1522 __estimateSqrt32(int aExp, uint a)
1523 {
1524 uint z;
1525
1526 int index = int(a>>27 & 15u);
1527 if ((aExp & 1) != 0) {
1528 z = 0x4000u + (a>>17) - __sqrtOddAdjustments(index);
1529 z = ((a / z)<<14) + (z<<15);
1530 a >>= 1;
1531 } else {
1532 z = 0x8000u + (a>>17) - __sqrtEvenAdjustments(index);
1533 z = a / z + z;
1534 z = (0x20000u <= z) ? 0xFFFF8000u : (z<<15);
1535 if (z <= a)
1536 return uint(int(a)>>1);
1537 }
1538 return ((__estimateDiv64To32(a, 0u, z))>>1) + (z>>1);
1539 }
1540
1541 /* Returns the square root of the double-precision floating-point value `a'.
1542 * The operation is performed according to the IEEE Standard for Floating-Point
1543 * Arithmetic.
1544 */
1545 uint64_t
1546 __fsqrt64(uint64_t a)
1547 {
1548 uint zFrac0 = 0u;
1549 uint zFrac1 = 0u;
1550 uint zFrac2 = 0u;
1551 uint doubleZFrac0 = 0u;
1552 uint rem0 = 0u;
1553 uint rem1 = 0u;
1554 uint rem2 = 0u;
1555 uint rem3 = 0u;
1556 uint term0 = 0u;
1557 uint term1 = 0u;
1558 uint term2 = 0u;
1559 uint term3 = 0u;
1560 uint64_t default_nan = 0xFFFFFFFFFFFFFFFFUL;
1561
1562 uint aFracLo = __extractFloat64FracLo(a);
1563 uint aFracHi = __extractFloat64FracHi(a);
1564 int aExp = __extractFloat64Exp(a);
1565 uint aSign = __extractFloat64Sign(a);
1566 if (aExp == 0x7FF) {
1567 if ((aFracHi | aFracLo) != 0u)
1568 return __propagateFloat64NaN(a, a);
1569 if (aSign == 0u)
1570 return a;
1571 return default_nan;
1572 }
1573 if (aSign != 0u) {
1574 if ((uint(aExp) | aFracHi | aFracLo) == 0u)
1575 return a;
1576 return default_nan;
1577 }
1578 if (aExp == 0) {
1579 if ((aFracHi | aFracLo) == 0u)
1580 return __packFloat64(0u, 0, 0u, 0u);
1581 __normalizeFloat64Subnormal(aFracHi, aFracLo, aExp, aFracHi, aFracLo);
1582 }
1583 int zExp = ((aExp - 0x3FF)>>1) + 0x3FE;
1584 aFracHi |= 0x00100000u;
1585 __shortShift64Left(aFracHi, aFracLo, 11, term0, term1);
1586 zFrac0 = (__estimateSqrt32(aExp, term0)>>1) + 1u;
1587 if (zFrac0 == 0u)
1588 zFrac0 = 0x7FFFFFFFu;
1589 doubleZFrac0 = zFrac0 + zFrac0;
1590 __shortShift64Left(aFracHi, aFracLo, 9 - (aExp & 1), aFracHi, aFracLo);
1591 umulExtended(zFrac0, zFrac0, term0, term1);
1592 __sub64(aFracHi, aFracLo, term0, term1, rem0, rem1);
1593 while (int(rem0) < 0) {
1594 --zFrac0;
1595 doubleZFrac0 -= 2u;
1596 __add64(rem0, rem1, 0u, doubleZFrac0 | 1u, rem0, rem1);
1597 }
1598 zFrac1 = __estimateDiv64To32(rem1, 0u, doubleZFrac0);
1599 if ((zFrac1 & 0x1FFu) <= 5u) {
1600 if (zFrac1 == 0u)
1601 zFrac1 = 1u;
1602 umulExtended(doubleZFrac0, zFrac1, term1, term2);
1603 __sub64(rem1, 0u, term1, term2, rem1, rem2);
1604 umulExtended(zFrac1, zFrac1, term2, term3);
1605 __sub96(rem1, rem2, 0u, 0u, term2, term3, rem1, rem2, rem3);
1606 while (int(rem1) < 0) {
1607 --zFrac1;
1608 __shortShift64Left(0u, zFrac1, 1, term2, term3);
1609 term3 |= 1u;
1610 term2 |= doubleZFrac0;
1611 __add96(rem1, rem2, rem3, 0u, term2, term3, rem1, rem2, rem3);
1612 }
1613 zFrac1 |= uint((rem1 | rem2 | rem3) != 0u);
1614 }
1615 __shift64ExtraRightJamming(zFrac0, zFrac1, 0u, 10, zFrac0, zFrac1, zFrac2);
1616 return __roundAndPackFloat64(0u, zExp, zFrac0, zFrac1, zFrac2);
1617 }
1618
1619 uint64_t
1620 __ftrunc64(uint64_t __a)
1621 {
1622 uvec2 a = unpackUint2x32(__a);
1623 int aExp = __extractFloat64Exp(__a);
1624 uint zLo;
1625 uint zHi;
1626
1627 int unbiasedExp = aExp - 1023;
1628 int fracBits = 52 - unbiasedExp;
1629 uint maskLo = mix(~0u << fracBits, 0u, fracBits >= 32);
1630 uint maskHi = mix(~0u << (fracBits - 32), ~0u, fracBits < 33);
1631 zLo = maskLo & a.x;
1632 zHi = maskHi & a.y;
1633
1634 zLo = mix(zLo, 0u, unbiasedExp < 0);
1635 zHi = mix(zHi, 0u, unbiasedExp < 0);
1636 zLo = mix(zLo, a.x, unbiasedExp > 52);
1637 zHi = mix(zHi, a.y, unbiasedExp > 52);
1638 return packUint2x32(uvec2(zLo, zHi));
1639 }
1640
1641 uint64_t
1642 __ffloor64(uint64_t a)
1643 {
1644 bool is_positive = __fge64(a, 0ul);
1645 uint64_t tr = __ftrunc64(a);
1646
1647 if (is_positive || __feq64(tr, a)) {
1648 return tr;
1649 } else {
1650 return __fadd64(tr, 0xbff0000000000000ul /* -1.0 */);
1651 }
1652 }
1653
1654 uint64_t
1655 __fround64(uint64_t __a)
1656 {
1657 uvec2 a = unpackUint2x32(__a);
1658 int unbiasedExp = __extractFloat64Exp(__a) - 1023;
1659 uint aHi = a.y;
1660 uint aLo = a.x;
1661
1662 if (unbiasedExp < 20) {
1663 if (unbiasedExp < 0) {
1664 if ((aHi & 0x80000000u) != 0u && aLo == 0u) {
1665 return 0;
1666 }
1667 aHi &= 0x80000000u;
1668 if ((a.y & 0x000FFFFFu) == 0u && a.x == 0u) {
1669 aLo = 0u;
1670 return packUint2x32(uvec2(aLo, aHi));
1671 }
1672 aHi = mix(aHi, (aHi | 0x3FF00000u), unbiasedExp == -1);
1673 aLo = 0u;
1674 } else {
1675 uint maskExp = 0x000FFFFFu >> unbiasedExp;
1676 uint lastBit = maskExp + 1;
1677 aHi += 0x00080000u >> unbiasedExp;
1678 if ((aHi & maskExp) == 0u)
1679 aHi &= ~lastBit;
1680 aHi &= ~maskExp;
1681 aLo = 0u;
1682 }
1683 } else if (unbiasedExp > 51 || unbiasedExp == 1024) {
1684 return __a;
1685 } else {
1686 uint maskExp = 0xFFFFFFFFu >> (unbiasedExp - 20);
1687 if ((aLo & maskExp) == 0u)
1688 return __a;
1689 uint tmp = aLo + (1u << (51 - unbiasedExp));
1690 if(tmp < aLo)
1691 aHi += 1u;
1692 aLo = tmp;
1693 aLo &= ~maskExp;
1694 }
1695
1696 return packUint2x32(uvec2(aLo, aHi));
1697 }
1698
1699 uint64_t
1700 __fmin64(uint64_t a, uint64_t b)
1701 {
1702 if (__is_nan(a)) return b;
1703 if (__is_nan(b)) return a;
1704
1705 if (__flt64_nonnan(a, b)) return a;
1706 return b;
1707 }
1708
1709 uint64_t
1710 __fmax64(uint64_t a, uint64_t b)
1711 {
1712 if (__is_nan(a)) return b;
1713 if (__is_nan(b)) return a;
1714
1715 if (__flt64_nonnan(a, b)) return b;
1716 return a;
1717 }
1718
1719 uint64_t
1720 __ffract64(uint64_t a)
1721 {
1722 return __fadd64(a, __fneg64(__ffloor64(a)));
1723 }