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