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