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