scsi-disk: change disk serial length from 20 to 36
[qemu.git] / fpu / softfloat.c
blob9b1eccff24f342ccd5af6249a76ab51e6b6d4a31
1 /*
2 * QEMU float support
4 * The code in this source file is derived from release 2a of the SoftFloat
5 * IEC/IEEE Floating-point Arithmetic Package. Those parts of the code (and
6 * some later contributions) are provided under that license, as detailed below.
7 * It has subsequently been modified by contributors to the QEMU Project,
8 * so some portions are provided under:
9 * the SoftFloat-2a license
10 * the BSD license
11 * GPL-v2-or-later
13 * Any future contributions to this file after December 1st 2014 will be
14 * taken to be licensed under the Softfloat-2a license unless specifically
15 * indicated otherwise.
19 ===============================================================================
20 This C source file is part of the SoftFloat IEC/IEEE Floating-point
21 Arithmetic Package, Release 2a.
23 Written by John R. Hauser. This work was made possible in part by the
24 International Computer Science Institute, located at Suite 600, 1947 Center
25 Street, Berkeley, California 94704. Funding was partially provided by the
26 National Science Foundation under grant MIP-9311980. The original version
27 of this code was written as part of a project to build a fixed-point vector
28 processor in collaboration with the University of California at Berkeley,
29 overseen by Profs. Nelson Morgan and John Wawrzynek. More information
30 is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
31 arithmetic/SoftFloat.html'.
33 THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
34 has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
35 TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
36 PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
37 AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
39 Derivative works are acceptable, even for commercial purposes, so long as
40 (1) they include prominent notice that the work is derivative, and (2) they
41 include prominent notice akin to these four paragraphs for those parts of
42 this code that are retained.
44 ===============================================================================
47 /* BSD licensing:
48 * Copyright (c) 2006, Fabrice Bellard
49 * All rights reserved.
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions are met:
54 * 1. Redistributions of source code must retain the above copyright notice,
55 * this list of conditions and the following disclaimer.
57 * 2. Redistributions in binary form must reproduce the above copyright notice,
58 * this list of conditions and the following disclaimer in the documentation
59 * and/or other materials provided with the distribution.
61 * 3. Neither the name of the copyright holder nor the names of its contributors
62 * may be used to endorse or promote products derived from this software without
63 * specific prior written permission.
65 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
66 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
69 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
70 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
71 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
72 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
73 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
74 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
75 * THE POSSIBILITY OF SUCH DAMAGE.
78 /* Portions of this work are licensed under the terms of the GNU GPL,
79 * version 2 or later. See the COPYING file in the top-level directory.
82 /* softfloat (and in particular the code in softfloat-specialize.h) is
83 * target-dependent and needs the TARGET_* macros.
85 #include "qemu/osdep.h"
87 #include "fpu/softfloat.h"
89 /* We only need stdlib for abort() */
91 /*----------------------------------------------------------------------------
92 | Primitive arithmetic functions, including multi-word arithmetic, and
93 | division and square root approximations. (Can be specialized to target if
94 | desired.)
95 *----------------------------------------------------------------------------*/
96 #include "softfloat-macros.h"
98 /*----------------------------------------------------------------------------
99 | Functions and definitions to determine: (1) whether tininess for underflow
100 | is detected before or after rounding by default, (2) what (if anything)
101 | happens when exceptions are raised, (3) how signaling NaNs are distinguished
102 | from quiet NaNs, (4) the default generated quiet NaNs, and (5) how NaNs
103 | are propagated from function inputs to output. These details are target-
104 | specific.
105 *----------------------------------------------------------------------------*/
106 #include "softfloat-specialize.h"
108 /*----------------------------------------------------------------------------
109 | Returns the fraction bits of the half-precision floating-point value `a'.
110 *----------------------------------------------------------------------------*/
112 static inline uint32_t extractFloat16Frac(float16 a)
114 return float16_val(a) & 0x3ff;
117 /*----------------------------------------------------------------------------
118 | Returns the exponent bits of the half-precision floating-point value `a'.
119 *----------------------------------------------------------------------------*/
121 static inline int extractFloat16Exp(float16 a)
123 return (float16_val(a) >> 10) & 0x1f;
126 /*----------------------------------------------------------------------------
127 | Returns the sign bit of the single-precision floating-point value `a'.
128 *----------------------------------------------------------------------------*/
130 static inline flag extractFloat16Sign(float16 a)
132 return float16_val(a)>>15;
135 /*----------------------------------------------------------------------------
136 | Takes a 64-bit fixed-point value `absZ' with binary point between bits 6
137 | and 7, and returns the properly rounded 32-bit integer corresponding to the
138 | input. If `zSign' is 1, the input is negated before being converted to an
139 | integer. Bit 63 of `absZ' must be zero. Ordinarily, the fixed-point input
140 | is simply rounded to an integer, with the inexact exception raised if the
141 | input cannot be represented exactly as an integer. However, if the fixed-
142 | point input is too large, the invalid exception is raised and the largest
143 | positive or negative integer is returned.
144 *----------------------------------------------------------------------------*/
146 static int32_t roundAndPackInt32(flag zSign, uint64_t absZ, float_status *status)
148 int8_t roundingMode;
149 flag roundNearestEven;
150 int8_t roundIncrement, roundBits;
151 int32_t z;
153 roundingMode = status->float_rounding_mode;
154 roundNearestEven = ( roundingMode == float_round_nearest_even );
155 switch (roundingMode) {
156 case float_round_nearest_even:
157 case float_round_ties_away:
158 roundIncrement = 0x40;
159 break;
160 case float_round_to_zero:
161 roundIncrement = 0;
162 break;
163 case float_round_up:
164 roundIncrement = zSign ? 0 : 0x7f;
165 break;
166 case float_round_down:
167 roundIncrement = zSign ? 0x7f : 0;
168 break;
169 default:
170 abort();
172 roundBits = absZ & 0x7F;
173 absZ = ( absZ + roundIncrement )>>7;
174 absZ &= ~ ( ( ( roundBits ^ 0x40 ) == 0 ) & roundNearestEven );
175 z = absZ;
176 if ( zSign ) z = - z;
177 if ( ( absZ>>32 ) || ( z && ( ( z < 0 ) ^ zSign ) ) ) {
178 float_raise(float_flag_invalid, status);
179 return zSign ? (int32_t) 0x80000000 : 0x7FFFFFFF;
181 if (roundBits) {
182 status->float_exception_flags |= float_flag_inexact;
184 return z;
188 /*----------------------------------------------------------------------------
189 | Takes the 128-bit fixed-point value formed by concatenating `absZ0' and
190 | `absZ1', with binary point between bits 63 and 64 (between the input words),
191 | and returns the properly rounded 64-bit integer corresponding to the input.
192 | If `zSign' is 1, the input is negated before being converted to an integer.
193 | Ordinarily, the fixed-point input is simply rounded to an integer, with
194 | the inexact exception raised if the input cannot be represented exactly as
195 | an integer. However, if the fixed-point input is too large, the invalid
196 | exception is raised and the largest positive or negative integer is
197 | returned.
198 *----------------------------------------------------------------------------*/
200 static int64_t roundAndPackInt64(flag zSign, uint64_t absZ0, uint64_t absZ1,
201 float_status *status)
203 int8_t roundingMode;
204 flag roundNearestEven, increment;
205 int64_t z;
207 roundingMode = status->float_rounding_mode;
208 roundNearestEven = ( roundingMode == float_round_nearest_even );
209 switch (roundingMode) {
210 case float_round_nearest_even:
211 case float_round_ties_away:
212 increment = ((int64_t) absZ1 < 0);
213 break;
214 case float_round_to_zero:
215 increment = 0;
216 break;
217 case float_round_up:
218 increment = !zSign && absZ1;
219 break;
220 case float_round_down:
221 increment = zSign && absZ1;
222 break;
223 default:
224 abort();
226 if ( increment ) {
227 ++absZ0;
228 if ( absZ0 == 0 ) goto overflow;
229 absZ0 &= ~ ( ( (uint64_t) ( absZ1<<1 ) == 0 ) & roundNearestEven );
231 z = absZ0;
232 if ( zSign ) z = - z;
233 if ( z && ( ( z < 0 ) ^ zSign ) ) {
234 overflow:
235 float_raise(float_flag_invalid, status);
236 return
237 zSign ? (int64_t) LIT64( 0x8000000000000000 )
238 : LIT64( 0x7FFFFFFFFFFFFFFF );
240 if (absZ1) {
241 status->float_exception_flags |= float_flag_inexact;
243 return z;
247 /*----------------------------------------------------------------------------
248 | Takes the 128-bit fixed-point value formed by concatenating `absZ0' and
249 | `absZ1', with binary point between bits 63 and 64 (between the input words),
250 | and returns the properly rounded 64-bit unsigned integer corresponding to the
251 | input. Ordinarily, the fixed-point input is simply rounded to an integer,
252 | with the inexact exception raised if the input cannot be represented exactly
253 | as an integer. However, if the fixed-point input is too large, the invalid
254 | exception is raised and the largest unsigned integer is returned.
255 *----------------------------------------------------------------------------*/
257 static int64_t roundAndPackUint64(flag zSign, uint64_t absZ0,
258 uint64_t absZ1, float_status *status)
260 int8_t roundingMode;
261 flag roundNearestEven, increment;
263 roundingMode = status->float_rounding_mode;
264 roundNearestEven = (roundingMode == float_round_nearest_even);
265 switch (roundingMode) {
266 case float_round_nearest_even:
267 case float_round_ties_away:
268 increment = ((int64_t)absZ1 < 0);
269 break;
270 case float_round_to_zero:
271 increment = 0;
272 break;
273 case float_round_up:
274 increment = !zSign && absZ1;
275 break;
276 case float_round_down:
277 increment = zSign && absZ1;
278 break;
279 default:
280 abort();
282 if (increment) {
283 ++absZ0;
284 if (absZ0 == 0) {
285 float_raise(float_flag_invalid, status);
286 return LIT64(0xFFFFFFFFFFFFFFFF);
288 absZ0 &= ~(((uint64_t)(absZ1<<1) == 0) & roundNearestEven);
291 if (zSign && absZ0) {
292 float_raise(float_flag_invalid, status);
293 return 0;
296 if (absZ1) {
297 status->float_exception_flags |= float_flag_inexact;
299 return absZ0;
302 /*----------------------------------------------------------------------------
303 | Returns the fraction bits of the single-precision floating-point value `a'.
304 *----------------------------------------------------------------------------*/
306 static inline uint32_t extractFloat32Frac( float32 a )
309 return float32_val(a) & 0x007FFFFF;
313 /*----------------------------------------------------------------------------
314 | Returns the exponent bits of the single-precision floating-point value `a'.
315 *----------------------------------------------------------------------------*/
317 static inline int extractFloat32Exp(float32 a)
320 return ( float32_val(a)>>23 ) & 0xFF;
324 /*----------------------------------------------------------------------------
325 | Returns the sign bit of the single-precision floating-point value `a'.
326 *----------------------------------------------------------------------------*/
328 static inline flag extractFloat32Sign( float32 a )
331 return float32_val(a)>>31;
335 /*----------------------------------------------------------------------------
336 | If `a' is denormal and we are in flush-to-zero mode then set the
337 | input-denormal exception and return zero. Otherwise just return the value.
338 *----------------------------------------------------------------------------*/
339 float32 float32_squash_input_denormal(float32 a, float_status *status)
341 if (status->flush_inputs_to_zero) {
342 if (extractFloat32Exp(a) == 0 && extractFloat32Frac(a) != 0) {
343 float_raise(float_flag_input_denormal, status);
344 return make_float32(float32_val(a) & 0x80000000);
347 return a;
350 /*----------------------------------------------------------------------------
351 | Normalizes the subnormal single-precision floating-point value represented
352 | by the denormalized significand `aSig'. The normalized exponent and
353 | significand are stored at the locations pointed to by `zExpPtr' and
354 | `zSigPtr', respectively.
355 *----------------------------------------------------------------------------*/
357 static void
358 normalizeFloat32Subnormal(uint32_t aSig, int *zExpPtr, uint32_t *zSigPtr)
360 int8_t shiftCount;
362 shiftCount = countLeadingZeros32( aSig ) - 8;
363 *zSigPtr = aSig<<shiftCount;
364 *zExpPtr = 1 - shiftCount;
368 /*----------------------------------------------------------------------------
369 | Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
370 | single-precision floating-point value, returning the result. After being
371 | shifted into the proper positions, the three fields are simply added
372 | together to form the result. This means that any integer portion of `zSig'
373 | will be added into the exponent. Since a properly normalized significand
374 | will have an integer portion equal to 1, the `zExp' input should be 1 less
375 | than the desired result exponent whenever `zSig' is a complete, normalized
376 | significand.
377 *----------------------------------------------------------------------------*/
379 static inline float32 packFloat32(flag zSign, int zExp, uint32_t zSig)
382 return make_float32(
383 ( ( (uint32_t) zSign )<<31 ) + ( ( (uint32_t) zExp )<<23 ) + zSig);
387 /*----------------------------------------------------------------------------
388 | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
389 | and significand `zSig', and returns the proper single-precision floating-
390 | point value corresponding to the abstract input. Ordinarily, the abstract
391 | value is simply rounded and packed into the single-precision format, with
392 | the inexact exception raised if the abstract input cannot be represented
393 | exactly. However, if the abstract value is too large, the overflow and
394 | inexact exceptions are raised and an infinity or maximal finite value is
395 | returned. If the abstract value is too small, the input value is rounded to
396 | a subnormal number, and the underflow and inexact exceptions are raised if
397 | the abstract input cannot be represented exactly as a subnormal single-
398 | precision floating-point number.
399 | The input significand `zSig' has its binary point between bits 30
400 | and 29, which is 7 bits to the left of the usual location. This shifted
401 | significand must be normalized or smaller. If `zSig' is not normalized,
402 | `zExp' must be 0; in that case, the result returned is a subnormal number,
403 | and it must not require rounding. In the usual case that `zSig' is
404 | normalized, `zExp' must be 1 less than the ``true'' floating-point exponent.
405 | The handling of underflow and overflow follows the IEC/IEEE Standard for
406 | Binary Floating-Point Arithmetic.
407 *----------------------------------------------------------------------------*/
409 static float32 roundAndPackFloat32(flag zSign, int zExp, uint32_t zSig,
410 float_status *status)
412 int8_t roundingMode;
413 flag roundNearestEven;
414 int8_t roundIncrement, roundBits;
415 flag isTiny;
417 roundingMode = status->float_rounding_mode;
418 roundNearestEven = ( roundingMode == float_round_nearest_even );
419 switch (roundingMode) {
420 case float_round_nearest_even:
421 case float_round_ties_away:
422 roundIncrement = 0x40;
423 break;
424 case float_round_to_zero:
425 roundIncrement = 0;
426 break;
427 case float_round_up:
428 roundIncrement = zSign ? 0 : 0x7f;
429 break;
430 case float_round_down:
431 roundIncrement = zSign ? 0x7f : 0;
432 break;
433 default:
434 abort();
435 break;
437 roundBits = zSig & 0x7F;
438 if ( 0xFD <= (uint16_t) zExp ) {
439 if ( ( 0xFD < zExp )
440 || ( ( zExp == 0xFD )
441 && ( (int32_t) ( zSig + roundIncrement ) < 0 ) )
443 float_raise(float_flag_overflow | float_flag_inexact, status);
444 return packFloat32( zSign, 0xFF, - ( roundIncrement == 0 ));
446 if ( zExp < 0 ) {
447 if (status->flush_to_zero) {
448 float_raise(float_flag_output_denormal, status);
449 return packFloat32(zSign, 0, 0);
451 isTiny =
452 (status->float_detect_tininess
453 == float_tininess_before_rounding)
454 || ( zExp < -1 )
455 || ( zSig + roundIncrement < 0x80000000 );
456 shift32RightJamming( zSig, - zExp, &zSig );
457 zExp = 0;
458 roundBits = zSig & 0x7F;
459 if (isTiny && roundBits) {
460 float_raise(float_flag_underflow, status);
464 if (roundBits) {
465 status->float_exception_flags |= float_flag_inexact;
467 zSig = ( zSig + roundIncrement )>>7;
468 zSig &= ~ ( ( ( roundBits ^ 0x40 ) == 0 ) & roundNearestEven );
469 if ( zSig == 0 ) zExp = 0;
470 return packFloat32( zSign, zExp, zSig );
474 /*----------------------------------------------------------------------------
475 | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
476 | and significand `zSig', and returns the proper single-precision floating-
477 | point value corresponding to the abstract input. This routine is just like
478 | `roundAndPackFloat32' except that `zSig' does not have to be normalized.
479 | Bit 31 of `zSig' must be zero, and `zExp' must be 1 less than the ``true''
480 | floating-point exponent.
481 *----------------------------------------------------------------------------*/
483 static float32
484 normalizeRoundAndPackFloat32(flag zSign, int zExp, uint32_t zSig,
485 float_status *status)
487 int8_t shiftCount;
489 shiftCount = countLeadingZeros32( zSig ) - 1;
490 return roundAndPackFloat32(zSign, zExp - shiftCount, zSig<<shiftCount,
491 status);
495 /*----------------------------------------------------------------------------
496 | Returns the fraction bits of the double-precision floating-point value `a'.
497 *----------------------------------------------------------------------------*/
499 static inline uint64_t extractFloat64Frac( float64 a )
502 return float64_val(a) & LIT64( 0x000FFFFFFFFFFFFF );
506 /*----------------------------------------------------------------------------
507 | Returns the exponent bits of the double-precision floating-point value `a'.
508 *----------------------------------------------------------------------------*/
510 static inline int extractFloat64Exp(float64 a)
513 return ( float64_val(a)>>52 ) & 0x7FF;
517 /*----------------------------------------------------------------------------
518 | Returns the sign bit of the double-precision floating-point value `a'.
519 *----------------------------------------------------------------------------*/
521 static inline flag extractFloat64Sign( float64 a )
524 return float64_val(a)>>63;
528 /*----------------------------------------------------------------------------
529 | If `a' is denormal and we are in flush-to-zero mode then set the
530 | input-denormal exception and return zero. Otherwise just return the value.
531 *----------------------------------------------------------------------------*/
532 float64 float64_squash_input_denormal(float64 a, float_status *status)
534 if (status->flush_inputs_to_zero) {
535 if (extractFloat64Exp(a) == 0 && extractFloat64Frac(a) != 0) {
536 float_raise(float_flag_input_denormal, status);
537 return make_float64(float64_val(a) & (1ULL << 63));
540 return a;
543 /*----------------------------------------------------------------------------
544 | Normalizes the subnormal double-precision floating-point value represented
545 | by the denormalized significand `aSig'. The normalized exponent and
546 | significand are stored at the locations pointed to by `zExpPtr' and
547 | `zSigPtr', respectively.
548 *----------------------------------------------------------------------------*/
550 static void
551 normalizeFloat64Subnormal(uint64_t aSig, int *zExpPtr, uint64_t *zSigPtr)
553 int8_t shiftCount;
555 shiftCount = countLeadingZeros64( aSig ) - 11;
556 *zSigPtr = aSig<<shiftCount;
557 *zExpPtr = 1 - shiftCount;
561 /*----------------------------------------------------------------------------
562 | Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
563 | double-precision floating-point value, returning the result. After being
564 | shifted into the proper positions, the three fields are simply added
565 | together to form the result. This means that any integer portion of `zSig'
566 | will be added into the exponent. Since a properly normalized significand
567 | will have an integer portion equal to 1, the `zExp' input should be 1 less
568 | than the desired result exponent whenever `zSig' is a complete, normalized
569 | significand.
570 *----------------------------------------------------------------------------*/
572 static inline float64 packFloat64(flag zSign, int zExp, uint64_t zSig)
575 return make_float64(
576 ( ( (uint64_t) zSign )<<63 ) + ( ( (uint64_t) zExp )<<52 ) + zSig);
580 /*----------------------------------------------------------------------------
581 | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
582 | and significand `zSig', and returns the proper double-precision floating-
583 | point value corresponding to the abstract input. Ordinarily, the abstract
584 | value is simply rounded and packed into the double-precision format, with
585 | the inexact exception raised if the abstract input cannot be represented
586 | exactly. However, if the abstract value is too large, the overflow and
587 | inexact exceptions are raised and an infinity or maximal finite value is
588 | returned. If the abstract value is too small, the input value is rounded to
589 | a subnormal number, and the underflow and inexact exceptions are raised if
590 | the abstract input cannot be represented exactly as a subnormal double-
591 | precision floating-point number.
592 | The input significand `zSig' has its binary point between bits 62
593 | and 61, which is 10 bits to the left of the usual location. This shifted
594 | significand must be normalized or smaller. If `zSig' is not normalized,
595 | `zExp' must be 0; in that case, the result returned is a subnormal number,
596 | and it must not require rounding. In the usual case that `zSig' is
597 | normalized, `zExp' must be 1 less than the ``true'' floating-point exponent.
598 | The handling of underflow and overflow follows the IEC/IEEE Standard for
599 | Binary Floating-Point Arithmetic.
600 *----------------------------------------------------------------------------*/
602 static float64 roundAndPackFloat64(flag zSign, int zExp, uint64_t zSig,
603 float_status *status)
605 int8_t roundingMode;
606 flag roundNearestEven;
607 int roundIncrement, roundBits;
608 flag isTiny;
610 roundingMode = status->float_rounding_mode;
611 roundNearestEven = ( roundingMode == float_round_nearest_even );
612 switch (roundingMode) {
613 case float_round_nearest_even:
614 case float_round_ties_away:
615 roundIncrement = 0x200;
616 break;
617 case float_round_to_zero:
618 roundIncrement = 0;
619 break;
620 case float_round_up:
621 roundIncrement = zSign ? 0 : 0x3ff;
622 break;
623 case float_round_down:
624 roundIncrement = zSign ? 0x3ff : 0;
625 break;
626 default:
627 abort();
629 roundBits = zSig & 0x3FF;
630 if ( 0x7FD <= (uint16_t) zExp ) {
631 if ( ( 0x7FD < zExp )
632 || ( ( zExp == 0x7FD )
633 && ( (int64_t) ( zSig + roundIncrement ) < 0 ) )
635 float_raise(float_flag_overflow | float_flag_inexact, status);
636 return packFloat64( zSign, 0x7FF, - ( roundIncrement == 0 ));
638 if ( zExp < 0 ) {
639 if (status->flush_to_zero) {
640 float_raise(float_flag_output_denormal, status);
641 return packFloat64(zSign, 0, 0);
643 isTiny =
644 (status->float_detect_tininess
645 == float_tininess_before_rounding)
646 || ( zExp < -1 )
647 || ( zSig + roundIncrement < LIT64( 0x8000000000000000 ) );
648 shift64RightJamming( zSig, - zExp, &zSig );
649 zExp = 0;
650 roundBits = zSig & 0x3FF;
651 if (isTiny && roundBits) {
652 float_raise(float_flag_underflow, status);
656 if (roundBits) {
657 status->float_exception_flags |= float_flag_inexact;
659 zSig = ( zSig + roundIncrement )>>10;
660 zSig &= ~ ( ( ( roundBits ^ 0x200 ) == 0 ) & roundNearestEven );
661 if ( zSig == 0 ) zExp = 0;
662 return packFloat64( zSign, zExp, zSig );
666 /*----------------------------------------------------------------------------
667 | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
668 | and significand `zSig', and returns the proper double-precision floating-
669 | point value corresponding to the abstract input. This routine is just like
670 | `roundAndPackFloat64' except that `zSig' does not have to be normalized.
671 | Bit 63 of `zSig' must be zero, and `zExp' must be 1 less than the ``true''
672 | floating-point exponent.
673 *----------------------------------------------------------------------------*/
675 static float64
676 normalizeRoundAndPackFloat64(flag zSign, int zExp, uint64_t zSig,
677 float_status *status)
679 int8_t shiftCount;
681 shiftCount = countLeadingZeros64( zSig ) - 1;
682 return roundAndPackFloat64(zSign, zExp - shiftCount, zSig<<shiftCount,
683 status);
687 /*----------------------------------------------------------------------------
688 | Returns the fraction bits of the extended double-precision floating-point
689 | value `a'.
690 *----------------------------------------------------------------------------*/
692 static inline uint64_t extractFloatx80Frac( floatx80 a )
695 return a.low;
699 /*----------------------------------------------------------------------------
700 | Returns the exponent bits of the extended double-precision floating-point
701 | value `a'.
702 *----------------------------------------------------------------------------*/
704 static inline int32_t extractFloatx80Exp( floatx80 a )
707 return a.high & 0x7FFF;
711 /*----------------------------------------------------------------------------
712 | Returns the sign bit of the extended double-precision floating-point value
713 | `a'.
714 *----------------------------------------------------------------------------*/
716 static inline flag extractFloatx80Sign( floatx80 a )
719 return a.high>>15;
723 /*----------------------------------------------------------------------------
724 | Normalizes the subnormal extended double-precision floating-point value
725 | represented by the denormalized significand `aSig'. The normalized exponent
726 | and significand are stored at the locations pointed to by `zExpPtr' and
727 | `zSigPtr', respectively.
728 *----------------------------------------------------------------------------*/
730 static void
731 normalizeFloatx80Subnormal( uint64_t aSig, int32_t *zExpPtr, uint64_t *zSigPtr )
733 int8_t shiftCount;
735 shiftCount = countLeadingZeros64( aSig );
736 *zSigPtr = aSig<<shiftCount;
737 *zExpPtr = 1 - shiftCount;
741 /*----------------------------------------------------------------------------
742 | Packs the sign `zSign', exponent `zExp', and significand `zSig' into an
743 | extended double-precision floating-point value, returning the result.
744 *----------------------------------------------------------------------------*/
746 static inline floatx80 packFloatx80( flag zSign, int32_t zExp, uint64_t zSig )
748 floatx80 z;
750 z.low = zSig;
751 z.high = ( ( (uint16_t) zSign )<<15 ) + zExp;
752 return z;
756 /*----------------------------------------------------------------------------
757 | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
758 | and extended significand formed by the concatenation of `zSig0' and `zSig1',
759 | and returns the proper extended double-precision floating-point value
760 | corresponding to the abstract input. Ordinarily, the abstract value is
761 | rounded and packed into the extended double-precision format, with the
762 | inexact exception raised if the abstract input cannot be represented
763 | exactly. However, if the abstract value is too large, the overflow and
764 | inexact exceptions are raised and an infinity or maximal finite value is
765 | returned. If the abstract value is too small, the input value is rounded to
766 | a subnormal number, and the underflow and inexact exceptions are raised if
767 | the abstract input cannot be represented exactly as a subnormal extended
768 | double-precision floating-point number.
769 | If `roundingPrecision' is 32 or 64, the result is rounded to the same
770 | number of bits as single or double precision, respectively. Otherwise, the
771 | result is rounded to the full precision of the extended double-precision
772 | format.
773 | The input significand must be normalized or smaller. If the input
774 | significand is not normalized, `zExp' must be 0; in that case, the result
775 | returned is a subnormal number, and it must not require rounding. The
776 | handling of underflow and overflow follows the IEC/IEEE Standard for Binary
777 | Floating-Point Arithmetic.
778 *----------------------------------------------------------------------------*/
780 static floatx80 roundAndPackFloatx80(int8_t roundingPrecision, flag zSign,
781 int32_t zExp, uint64_t zSig0, uint64_t zSig1,
782 float_status *status)
784 int8_t roundingMode;
785 flag roundNearestEven, increment, isTiny;
786 int64_t roundIncrement, roundMask, roundBits;
788 roundingMode = status->float_rounding_mode;
789 roundNearestEven = ( roundingMode == float_round_nearest_even );
790 if ( roundingPrecision == 80 ) goto precision80;
791 if ( roundingPrecision == 64 ) {
792 roundIncrement = LIT64( 0x0000000000000400 );
793 roundMask = LIT64( 0x00000000000007FF );
795 else if ( roundingPrecision == 32 ) {
796 roundIncrement = LIT64( 0x0000008000000000 );
797 roundMask = LIT64( 0x000000FFFFFFFFFF );
799 else {
800 goto precision80;
802 zSig0 |= ( zSig1 != 0 );
803 switch (roundingMode) {
804 case float_round_nearest_even:
805 case float_round_ties_away:
806 break;
807 case float_round_to_zero:
808 roundIncrement = 0;
809 break;
810 case float_round_up:
811 roundIncrement = zSign ? 0 : roundMask;
812 break;
813 case float_round_down:
814 roundIncrement = zSign ? roundMask : 0;
815 break;
816 default:
817 abort();
819 roundBits = zSig0 & roundMask;
820 if ( 0x7FFD <= (uint32_t) ( zExp - 1 ) ) {
821 if ( ( 0x7FFE < zExp )
822 || ( ( zExp == 0x7FFE ) && ( zSig0 + roundIncrement < zSig0 ) )
824 goto overflow;
826 if ( zExp <= 0 ) {
827 if (status->flush_to_zero) {
828 float_raise(float_flag_output_denormal, status);
829 return packFloatx80(zSign, 0, 0);
831 isTiny =
832 (status->float_detect_tininess
833 == float_tininess_before_rounding)
834 || ( zExp < 0 )
835 || ( zSig0 <= zSig0 + roundIncrement );
836 shift64RightJamming( zSig0, 1 - zExp, &zSig0 );
837 zExp = 0;
838 roundBits = zSig0 & roundMask;
839 if (isTiny && roundBits) {
840 float_raise(float_flag_underflow, status);
842 if (roundBits) {
843 status->float_exception_flags |= float_flag_inexact;
845 zSig0 += roundIncrement;
846 if ( (int64_t) zSig0 < 0 ) zExp = 1;
847 roundIncrement = roundMask + 1;
848 if ( roundNearestEven && ( roundBits<<1 == roundIncrement ) ) {
849 roundMask |= roundIncrement;
851 zSig0 &= ~ roundMask;
852 return packFloatx80( zSign, zExp, zSig0 );
855 if (roundBits) {
856 status->float_exception_flags |= float_flag_inexact;
858 zSig0 += roundIncrement;
859 if ( zSig0 < roundIncrement ) {
860 ++zExp;
861 zSig0 = LIT64( 0x8000000000000000 );
863 roundIncrement = roundMask + 1;
864 if ( roundNearestEven && ( roundBits<<1 == roundIncrement ) ) {
865 roundMask |= roundIncrement;
867 zSig0 &= ~ roundMask;
868 if ( zSig0 == 0 ) zExp = 0;
869 return packFloatx80( zSign, zExp, zSig0 );
870 precision80:
871 switch (roundingMode) {
872 case float_round_nearest_even:
873 case float_round_ties_away:
874 increment = ((int64_t)zSig1 < 0);
875 break;
876 case float_round_to_zero:
877 increment = 0;
878 break;
879 case float_round_up:
880 increment = !zSign && zSig1;
881 break;
882 case float_round_down:
883 increment = zSign && zSig1;
884 break;
885 default:
886 abort();
888 if ( 0x7FFD <= (uint32_t) ( zExp - 1 ) ) {
889 if ( ( 0x7FFE < zExp )
890 || ( ( zExp == 0x7FFE )
891 && ( zSig0 == LIT64( 0xFFFFFFFFFFFFFFFF ) )
892 && increment
895 roundMask = 0;
896 overflow:
897 float_raise(float_flag_overflow | float_flag_inexact, status);
898 if ( ( roundingMode == float_round_to_zero )
899 || ( zSign && ( roundingMode == float_round_up ) )
900 || ( ! zSign && ( roundingMode == float_round_down ) )
902 return packFloatx80( zSign, 0x7FFE, ~ roundMask );
904 return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
906 if ( zExp <= 0 ) {
907 isTiny =
908 (status->float_detect_tininess
909 == float_tininess_before_rounding)
910 || ( zExp < 0 )
911 || ! increment
912 || ( zSig0 < LIT64( 0xFFFFFFFFFFFFFFFF ) );
913 shift64ExtraRightJamming( zSig0, zSig1, 1 - zExp, &zSig0, &zSig1 );
914 zExp = 0;
915 if (isTiny && zSig1) {
916 float_raise(float_flag_underflow, status);
918 if (zSig1) {
919 status->float_exception_flags |= float_flag_inexact;
921 switch (roundingMode) {
922 case float_round_nearest_even:
923 case float_round_ties_away:
924 increment = ((int64_t)zSig1 < 0);
925 break;
926 case float_round_to_zero:
927 increment = 0;
928 break;
929 case float_round_up:
930 increment = !zSign && zSig1;
931 break;
932 case float_round_down:
933 increment = zSign && zSig1;
934 break;
935 default:
936 abort();
938 if ( increment ) {
939 ++zSig0;
940 zSig0 &=
941 ~ ( ( (uint64_t) ( zSig1<<1 ) == 0 ) & roundNearestEven );
942 if ( (int64_t) zSig0 < 0 ) zExp = 1;
944 return packFloatx80( zSign, zExp, zSig0 );
947 if (zSig1) {
948 status->float_exception_flags |= float_flag_inexact;
950 if ( increment ) {
951 ++zSig0;
952 if ( zSig0 == 0 ) {
953 ++zExp;
954 zSig0 = LIT64( 0x8000000000000000 );
956 else {
957 zSig0 &= ~ ( ( (uint64_t) ( zSig1<<1 ) == 0 ) & roundNearestEven );
960 else {
961 if ( zSig0 == 0 ) zExp = 0;
963 return packFloatx80( zSign, zExp, zSig0 );
967 /*----------------------------------------------------------------------------
968 | Takes an abstract floating-point value having sign `zSign', exponent
969 | `zExp', and significand formed by the concatenation of `zSig0' and `zSig1',
970 | and returns the proper extended double-precision floating-point value
971 | corresponding to the abstract input. This routine is just like
972 | `roundAndPackFloatx80' except that the input significand does not have to be
973 | normalized.
974 *----------------------------------------------------------------------------*/
976 static floatx80 normalizeRoundAndPackFloatx80(int8_t roundingPrecision,
977 flag zSign, int32_t zExp,
978 uint64_t zSig0, uint64_t zSig1,
979 float_status *status)
981 int8_t shiftCount;
983 if ( zSig0 == 0 ) {
984 zSig0 = zSig1;
985 zSig1 = 0;
986 zExp -= 64;
988 shiftCount = countLeadingZeros64( zSig0 );
989 shortShift128Left( zSig0, zSig1, shiftCount, &zSig0, &zSig1 );
990 zExp -= shiftCount;
991 return roundAndPackFloatx80(roundingPrecision, zSign, zExp,
992 zSig0, zSig1, status);
996 /*----------------------------------------------------------------------------
997 | Returns the least-significant 64 fraction bits of the quadruple-precision
998 | floating-point value `a'.
999 *----------------------------------------------------------------------------*/
1001 static inline uint64_t extractFloat128Frac1( float128 a )
1004 return a.low;
1008 /*----------------------------------------------------------------------------
1009 | Returns the most-significant 48 fraction bits of the quadruple-precision
1010 | floating-point value `a'.
1011 *----------------------------------------------------------------------------*/
1013 static inline uint64_t extractFloat128Frac0( float128 a )
1016 return a.high & LIT64( 0x0000FFFFFFFFFFFF );
1020 /*----------------------------------------------------------------------------
1021 | Returns the exponent bits of the quadruple-precision floating-point value
1022 | `a'.
1023 *----------------------------------------------------------------------------*/
1025 static inline int32_t extractFloat128Exp( float128 a )
1028 return ( a.high>>48 ) & 0x7FFF;
1032 /*----------------------------------------------------------------------------
1033 | Returns the sign bit of the quadruple-precision floating-point value `a'.
1034 *----------------------------------------------------------------------------*/
1036 static inline flag extractFloat128Sign( float128 a )
1039 return a.high>>63;
1043 /*----------------------------------------------------------------------------
1044 | Normalizes the subnormal quadruple-precision floating-point value
1045 | represented by the denormalized significand formed by the concatenation of
1046 | `aSig0' and `aSig1'. The normalized exponent is stored at the location
1047 | pointed to by `zExpPtr'. The most significant 49 bits of the normalized
1048 | significand are stored at the location pointed to by `zSig0Ptr', and the
1049 | least significant 64 bits of the normalized significand are stored at the
1050 | location pointed to by `zSig1Ptr'.
1051 *----------------------------------------------------------------------------*/
1053 static void
1054 normalizeFloat128Subnormal(
1055 uint64_t aSig0,
1056 uint64_t aSig1,
1057 int32_t *zExpPtr,
1058 uint64_t *zSig0Ptr,
1059 uint64_t *zSig1Ptr
1062 int8_t shiftCount;
1064 if ( aSig0 == 0 ) {
1065 shiftCount = countLeadingZeros64( aSig1 ) - 15;
1066 if ( shiftCount < 0 ) {
1067 *zSig0Ptr = aSig1>>( - shiftCount );
1068 *zSig1Ptr = aSig1<<( shiftCount & 63 );
1070 else {
1071 *zSig0Ptr = aSig1<<shiftCount;
1072 *zSig1Ptr = 0;
1074 *zExpPtr = - shiftCount - 63;
1076 else {
1077 shiftCount = countLeadingZeros64( aSig0 ) - 15;
1078 shortShift128Left( aSig0, aSig1, shiftCount, zSig0Ptr, zSig1Ptr );
1079 *zExpPtr = 1 - shiftCount;
1084 /*----------------------------------------------------------------------------
1085 | Packs the sign `zSign', the exponent `zExp', and the significand formed
1086 | by the concatenation of `zSig0' and `zSig1' into a quadruple-precision
1087 | floating-point value, returning the result. After being shifted into the
1088 | proper positions, the three fields `zSign', `zExp', and `zSig0' are simply
1089 | added together to form the most significant 32 bits of the result. This
1090 | means that any integer portion of `zSig0' will be added into the exponent.
1091 | Since a properly normalized significand will have an integer portion equal
1092 | to 1, the `zExp' input should be 1 less than the desired result exponent
1093 | whenever `zSig0' and `zSig1' concatenated form a complete, normalized
1094 | significand.
1095 *----------------------------------------------------------------------------*/
1097 static inline float128
1098 packFloat128( flag zSign, int32_t zExp, uint64_t zSig0, uint64_t zSig1 )
1100 float128 z;
1102 z.low = zSig1;
1103 z.high = ( ( (uint64_t) zSign )<<63 ) + ( ( (uint64_t) zExp )<<48 ) + zSig0;
1104 return z;
1108 /*----------------------------------------------------------------------------
1109 | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
1110 | and extended significand formed by the concatenation of `zSig0', `zSig1',
1111 | and `zSig2', and returns the proper quadruple-precision floating-point value
1112 | corresponding to the abstract input. Ordinarily, the abstract value is
1113 | simply rounded and packed into the quadruple-precision format, with the
1114 | inexact exception raised if the abstract input cannot be represented
1115 | exactly. However, if the abstract value is too large, the overflow and
1116 | inexact exceptions are raised and an infinity or maximal finite value is
1117 | returned. If the abstract value is too small, the input value is rounded to
1118 | a subnormal number, and the underflow and inexact exceptions are raised if
1119 | the abstract input cannot be represented exactly as a subnormal quadruple-
1120 | precision floating-point number.
1121 | The input significand must be normalized or smaller. If the input
1122 | significand is not normalized, `zExp' must be 0; in that case, the result
1123 | returned is a subnormal number, and it must not require rounding. In the
1124 | usual case that the input significand is normalized, `zExp' must be 1 less
1125 | than the ``true'' floating-point exponent. The handling of underflow and
1126 | overflow follows the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
1127 *----------------------------------------------------------------------------*/
1129 static float128 roundAndPackFloat128(flag zSign, int32_t zExp,
1130 uint64_t zSig0, uint64_t zSig1,
1131 uint64_t zSig2, float_status *status)
1133 int8_t roundingMode;
1134 flag roundNearestEven, increment, isTiny;
1136 roundingMode = status->float_rounding_mode;
1137 roundNearestEven = ( roundingMode == float_round_nearest_even );
1138 switch (roundingMode) {
1139 case float_round_nearest_even:
1140 case float_round_ties_away:
1141 increment = ((int64_t)zSig2 < 0);
1142 break;
1143 case float_round_to_zero:
1144 increment = 0;
1145 break;
1146 case float_round_up:
1147 increment = !zSign && zSig2;
1148 break;
1149 case float_round_down:
1150 increment = zSign && zSig2;
1151 break;
1152 default:
1153 abort();
1155 if ( 0x7FFD <= (uint32_t) zExp ) {
1156 if ( ( 0x7FFD < zExp )
1157 || ( ( zExp == 0x7FFD )
1158 && eq128(
1159 LIT64( 0x0001FFFFFFFFFFFF ),
1160 LIT64( 0xFFFFFFFFFFFFFFFF ),
1161 zSig0,
1162 zSig1
1164 && increment
1167 float_raise(float_flag_overflow | float_flag_inexact, status);
1168 if ( ( roundingMode == float_round_to_zero )
1169 || ( zSign && ( roundingMode == float_round_up ) )
1170 || ( ! zSign && ( roundingMode == float_round_down ) )
1172 return
1173 packFloat128(
1174 zSign,
1175 0x7FFE,
1176 LIT64( 0x0000FFFFFFFFFFFF ),
1177 LIT64( 0xFFFFFFFFFFFFFFFF )
1180 return packFloat128( zSign, 0x7FFF, 0, 0 );
1182 if ( zExp < 0 ) {
1183 if (status->flush_to_zero) {
1184 float_raise(float_flag_output_denormal, status);
1185 return packFloat128(zSign, 0, 0, 0);
1187 isTiny =
1188 (status->float_detect_tininess
1189 == float_tininess_before_rounding)
1190 || ( zExp < -1 )
1191 || ! increment
1192 || lt128(
1193 zSig0,
1194 zSig1,
1195 LIT64( 0x0001FFFFFFFFFFFF ),
1196 LIT64( 0xFFFFFFFFFFFFFFFF )
1198 shift128ExtraRightJamming(
1199 zSig0, zSig1, zSig2, - zExp, &zSig0, &zSig1, &zSig2 );
1200 zExp = 0;
1201 if (isTiny && zSig2) {
1202 float_raise(float_flag_underflow, status);
1204 switch (roundingMode) {
1205 case float_round_nearest_even:
1206 case float_round_ties_away:
1207 increment = ((int64_t)zSig2 < 0);
1208 break;
1209 case float_round_to_zero:
1210 increment = 0;
1211 break;
1212 case float_round_up:
1213 increment = !zSign && zSig2;
1214 break;
1215 case float_round_down:
1216 increment = zSign && zSig2;
1217 break;
1218 default:
1219 abort();
1223 if (zSig2) {
1224 status->float_exception_flags |= float_flag_inexact;
1226 if ( increment ) {
1227 add128( zSig0, zSig1, 0, 1, &zSig0, &zSig1 );
1228 zSig1 &= ~ ( ( zSig2 + zSig2 == 0 ) & roundNearestEven );
1230 else {
1231 if ( ( zSig0 | zSig1 ) == 0 ) zExp = 0;
1233 return packFloat128( zSign, zExp, zSig0, zSig1 );
1237 /*----------------------------------------------------------------------------
1238 | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
1239 | and significand formed by the concatenation of `zSig0' and `zSig1', and
1240 | returns the proper quadruple-precision floating-point value corresponding
1241 | to the abstract input. This routine is just like `roundAndPackFloat128'
1242 | except that the input significand has fewer bits and does not have to be
1243 | normalized. In all cases, `zExp' must be 1 less than the ``true'' floating-
1244 | point exponent.
1245 *----------------------------------------------------------------------------*/
1247 static float128 normalizeRoundAndPackFloat128(flag zSign, int32_t zExp,
1248 uint64_t zSig0, uint64_t zSig1,
1249 float_status *status)
1251 int8_t shiftCount;
1252 uint64_t zSig2;
1254 if ( zSig0 == 0 ) {
1255 zSig0 = zSig1;
1256 zSig1 = 0;
1257 zExp -= 64;
1259 shiftCount = countLeadingZeros64( zSig0 ) - 15;
1260 if ( 0 <= shiftCount ) {
1261 zSig2 = 0;
1262 shortShift128Left( zSig0, zSig1, shiftCount, &zSig0, &zSig1 );
1264 else {
1265 shift128ExtraRightJamming(
1266 zSig0, zSig1, 0, - shiftCount, &zSig0, &zSig1, &zSig2 );
1268 zExp -= shiftCount;
1269 return roundAndPackFloat128(zSign, zExp, zSig0, zSig1, zSig2, status);
1273 /*----------------------------------------------------------------------------
1274 | Returns the result of converting the 32-bit two's complement integer `a'
1275 | to the single-precision floating-point format. The conversion is performed
1276 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
1277 *----------------------------------------------------------------------------*/
1279 float32 int32_to_float32(int32_t a, float_status *status)
1281 flag zSign;
1283 if ( a == 0 ) return float32_zero;
1284 if ( a == (int32_t) 0x80000000 ) return packFloat32( 1, 0x9E, 0 );
1285 zSign = ( a < 0 );
1286 return normalizeRoundAndPackFloat32(zSign, 0x9C, zSign ? -a : a, status);
1289 /*----------------------------------------------------------------------------
1290 | Returns the result of converting the 32-bit two's complement integer `a'
1291 | to the double-precision floating-point format. The conversion is performed
1292 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
1293 *----------------------------------------------------------------------------*/
1295 float64 int32_to_float64(int32_t a, float_status *status)
1297 flag zSign;
1298 uint32_t absA;
1299 int8_t shiftCount;
1300 uint64_t zSig;
1302 if ( a == 0 ) return float64_zero;
1303 zSign = ( a < 0 );
1304 absA = zSign ? - a : a;
1305 shiftCount = countLeadingZeros32( absA ) + 21;
1306 zSig = absA;
1307 return packFloat64( zSign, 0x432 - shiftCount, zSig<<shiftCount );
1311 /*----------------------------------------------------------------------------
1312 | Returns the result of converting the 32-bit two's complement integer `a'
1313 | to the extended double-precision floating-point format. The conversion
1314 | is performed according to the IEC/IEEE Standard for Binary Floating-Point
1315 | Arithmetic.
1316 *----------------------------------------------------------------------------*/
1318 floatx80 int32_to_floatx80(int32_t a, float_status *status)
1320 flag zSign;
1321 uint32_t absA;
1322 int8_t shiftCount;
1323 uint64_t zSig;
1325 if ( a == 0 ) return packFloatx80( 0, 0, 0 );
1326 zSign = ( a < 0 );
1327 absA = zSign ? - a : a;
1328 shiftCount = countLeadingZeros32( absA ) + 32;
1329 zSig = absA;
1330 return packFloatx80( zSign, 0x403E - shiftCount, zSig<<shiftCount );
1334 /*----------------------------------------------------------------------------
1335 | Returns the result of converting the 32-bit two's complement integer `a' to
1336 | the quadruple-precision floating-point format. The conversion is performed
1337 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
1338 *----------------------------------------------------------------------------*/
1340 float128 int32_to_float128(int32_t a, float_status *status)
1342 flag zSign;
1343 uint32_t absA;
1344 int8_t shiftCount;
1345 uint64_t zSig0;
1347 if ( a == 0 ) return packFloat128( 0, 0, 0, 0 );
1348 zSign = ( a < 0 );
1349 absA = zSign ? - a : a;
1350 shiftCount = countLeadingZeros32( absA ) + 17;
1351 zSig0 = absA;
1352 return packFloat128( zSign, 0x402E - shiftCount, zSig0<<shiftCount, 0 );
1356 /*----------------------------------------------------------------------------
1357 | Returns the result of converting the 64-bit two's complement integer `a'
1358 | to the single-precision floating-point format. The conversion is performed
1359 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
1360 *----------------------------------------------------------------------------*/
1362 float32 int64_to_float32(int64_t a, float_status *status)
1364 flag zSign;
1365 uint64_t absA;
1366 int8_t shiftCount;
1368 if ( a == 0 ) return float32_zero;
1369 zSign = ( a < 0 );
1370 absA = zSign ? - a : a;
1371 shiftCount = countLeadingZeros64( absA ) - 40;
1372 if ( 0 <= shiftCount ) {
1373 return packFloat32( zSign, 0x95 - shiftCount, absA<<shiftCount );
1375 else {
1376 shiftCount += 7;
1377 if ( shiftCount < 0 ) {
1378 shift64RightJamming( absA, - shiftCount, &absA );
1380 else {
1381 absA <<= shiftCount;
1383 return roundAndPackFloat32(zSign, 0x9C - shiftCount, absA, status);
1388 /*----------------------------------------------------------------------------
1389 | Returns the result of converting the 64-bit two's complement integer `a'
1390 | to the double-precision floating-point format. The conversion is performed
1391 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
1392 *----------------------------------------------------------------------------*/
1394 float64 int64_to_float64(int64_t a, float_status *status)
1396 flag zSign;
1398 if ( a == 0 ) return float64_zero;
1399 if ( a == (int64_t) LIT64( 0x8000000000000000 ) ) {
1400 return packFloat64( 1, 0x43E, 0 );
1402 zSign = ( a < 0 );
1403 return normalizeRoundAndPackFloat64(zSign, 0x43C, zSign ? -a : a, status);
1406 /*----------------------------------------------------------------------------
1407 | Returns the result of converting the 64-bit two's complement integer `a'
1408 | to the extended double-precision floating-point format. The conversion
1409 | is performed according to the IEC/IEEE Standard for Binary Floating-Point
1410 | Arithmetic.
1411 *----------------------------------------------------------------------------*/
1413 floatx80 int64_to_floatx80(int64_t a, float_status *status)
1415 flag zSign;
1416 uint64_t absA;
1417 int8_t shiftCount;
1419 if ( a == 0 ) return packFloatx80( 0, 0, 0 );
1420 zSign = ( a < 0 );
1421 absA = zSign ? - a : a;
1422 shiftCount = countLeadingZeros64( absA );
1423 return packFloatx80( zSign, 0x403E - shiftCount, absA<<shiftCount );
1427 /*----------------------------------------------------------------------------
1428 | Returns the result of converting the 64-bit two's complement integer `a' to
1429 | the quadruple-precision floating-point format. The conversion is performed
1430 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
1431 *----------------------------------------------------------------------------*/
1433 float128 int64_to_float128(int64_t a, float_status *status)
1435 flag zSign;
1436 uint64_t absA;
1437 int8_t shiftCount;
1438 int32_t zExp;
1439 uint64_t zSig0, zSig1;
1441 if ( a == 0 ) return packFloat128( 0, 0, 0, 0 );
1442 zSign = ( a < 0 );
1443 absA = zSign ? - a : a;
1444 shiftCount = countLeadingZeros64( absA ) + 49;
1445 zExp = 0x406E - shiftCount;
1446 if ( 64 <= shiftCount ) {
1447 zSig1 = 0;
1448 zSig0 = absA;
1449 shiftCount -= 64;
1451 else {
1452 zSig1 = absA;
1453 zSig0 = 0;
1455 shortShift128Left( zSig0, zSig1, shiftCount, &zSig0, &zSig1 );
1456 return packFloat128( zSign, zExp, zSig0, zSig1 );
1460 /*----------------------------------------------------------------------------
1461 | Returns the result of converting the 64-bit unsigned integer `a'
1462 | to the single-precision floating-point format. The conversion is performed
1463 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
1464 *----------------------------------------------------------------------------*/
1466 float32 uint64_to_float32(uint64_t a, float_status *status)
1468 int shiftcount;
1470 if (a == 0) {
1471 return float32_zero;
1474 /* Determine (left) shift needed to put first set bit into bit posn 23
1475 * (since packFloat32() expects the binary point between bits 23 and 22);
1476 * this is the fast case for smallish numbers.
1478 shiftcount = countLeadingZeros64(a) - 40;
1479 if (shiftcount >= 0) {
1480 return packFloat32(0, 0x95 - shiftcount, a << shiftcount);
1482 /* Otherwise we need to do a round-and-pack. roundAndPackFloat32()
1483 * expects the binary point between bits 30 and 29, hence the + 7.
1485 shiftcount += 7;
1486 if (shiftcount < 0) {
1487 shift64RightJamming(a, -shiftcount, &a);
1488 } else {
1489 a <<= shiftcount;
1492 return roundAndPackFloat32(0, 0x9c - shiftcount, a, status);
1495 /*----------------------------------------------------------------------------
1496 | Returns the result of converting the 64-bit unsigned integer `a'
1497 | to the double-precision floating-point format. The conversion is performed
1498 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
1499 *----------------------------------------------------------------------------*/
1501 float64 uint64_to_float64(uint64_t a, float_status *status)
1503 int exp = 0x43C;
1504 int shiftcount;
1506 if (a == 0) {
1507 return float64_zero;
1510 shiftcount = countLeadingZeros64(a) - 1;
1511 if (shiftcount < 0) {
1512 shift64RightJamming(a, -shiftcount, &a);
1513 } else {
1514 a <<= shiftcount;
1516 return roundAndPackFloat64(0, exp - shiftcount, a, status);
1519 /*----------------------------------------------------------------------------
1520 | Returns the result of converting the 64-bit unsigned integer `a'
1521 | to the quadruple-precision floating-point format. The conversion is performed
1522 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
1523 *----------------------------------------------------------------------------*/
1525 float128 uint64_to_float128(uint64_t a, float_status *status)
1527 if (a == 0) {
1528 return float128_zero;
1530 return normalizeRoundAndPackFloat128(0, 0x406E, a, 0, status);
1533 /*----------------------------------------------------------------------------
1534 | Returns the result of converting the single-precision floating-point value
1535 | `a' to the 32-bit two's complement integer format. The conversion is
1536 | performed according to the IEC/IEEE Standard for Binary Floating-Point
1537 | Arithmetic---which means in particular that the conversion is rounded
1538 | according to the current rounding mode. If `a' is a NaN, the largest
1539 | positive integer is returned. Otherwise, if the conversion overflows, the
1540 | largest integer with the same sign as `a' is returned.
1541 *----------------------------------------------------------------------------*/
1543 int32_t float32_to_int32(float32 a, float_status *status)
1545 flag aSign;
1546 int aExp;
1547 int shiftCount;
1548 uint32_t aSig;
1549 uint64_t aSig64;
1551 a = float32_squash_input_denormal(a, status);
1552 aSig = extractFloat32Frac( a );
1553 aExp = extractFloat32Exp( a );
1554 aSign = extractFloat32Sign( a );
1555 if ( ( aExp == 0xFF ) && aSig ) aSign = 0;
1556 if ( aExp ) aSig |= 0x00800000;
1557 shiftCount = 0xAF - aExp;
1558 aSig64 = aSig;
1559 aSig64 <<= 32;
1560 if ( 0 < shiftCount ) shift64RightJamming( aSig64, shiftCount, &aSig64 );
1561 return roundAndPackInt32(aSign, aSig64, status);
1565 /*----------------------------------------------------------------------------
1566 | Returns the result of converting the single-precision floating-point value
1567 | `a' to the 32-bit two's complement integer format. The conversion is
1568 | performed according to the IEC/IEEE Standard for Binary Floating-Point
1569 | Arithmetic, except that the conversion is always rounded toward zero.
1570 | If `a' is a NaN, the largest positive integer is returned. Otherwise, if
1571 | the conversion overflows, the largest integer with the same sign as `a' is
1572 | returned.
1573 *----------------------------------------------------------------------------*/
1575 int32_t float32_to_int32_round_to_zero(float32 a, float_status *status)
1577 flag aSign;
1578 int aExp;
1579 int shiftCount;
1580 uint32_t aSig;
1581 int32_t z;
1582 a = float32_squash_input_denormal(a, status);
1584 aSig = extractFloat32Frac( a );
1585 aExp = extractFloat32Exp( a );
1586 aSign = extractFloat32Sign( a );
1587 shiftCount = aExp - 0x9E;
1588 if ( 0 <= shiftCount ) {
1589 if ( float32_val(a) != 0xCF000000 ) {
1590 float_raise(float_flag_invalid, status);
1591 if ( ! aSign || ( ( aExp == 0xFF ) && aSig ) ) return 0x7FFFFFFF;
1593 return (int32_t) 0x80000000;
1595 else if ( aExp <= 0x7E ) {
1596 if (aExp | aSig) {
1597 status->float_exception_flags |= float_flag_inexact;
1599 return 0;
1601 aSig = ( aSig | 0x00800000 )<<8;
1602 z = aSig>>( - shiftCount );
1603 if ( (uint32_t) ( aSig<<( shiftCount & 31 ) ) ) {
1604 status->float_exception_flags |= float_flag_inexact;
1606 if ( aSign ) z = - z;
1607 return z;
1611 /*----------------------------------------------------------------------------
1612 | Returns the result of converting the single-precision floating-point value
1613 | `a' to the 16-bit two's complement integer format. The conversion is
1614 | performed according to the IEC/IEEE Standard for Binary Floating-Point
1615 | Arithmetic, except that the conversion is always rounded toward zero.
1616 | If `a' is a NaN, the largest positive integer is returned. Otherwise, if
1617 | the conversion overflows, the largest integer with the same sign as `a' is
1618 | returned.
1619 *----------------------------------------------------------------------------*/
1621 int16_t float32_to_int16_round_to_zero(float32 a, float_status *status)
1623 flag aSign;
1624 int aExp;
1625 int shiftCount;
1626 uint32_t aSig;
1627 int32_t z;
1629 aSig = extractFloat32Frac( a );
1630 aExp = extractFloat32Exp( a );
1631 aSign = extractFloat32Sign( a );
1632 shiftCount = aExp - 0x8E;
1633 if ( 0 <= shiftCount ) {
1634 if ( float32_val(a) != 0xC7000000 ) {
1635 float_raise(float_flag_invalid, status);
1636 if ( ! aSign || ( ( aExp == 0xFF ) && aSig ) ) {
1637 return 0x7FFF;
1640 return (int32_t) 0xffff8000;
1642 else if ( aExp <= 0x7E ) {
1643 if ( aExp | aSig ) {
1644 status->float_exception_flags |= float_flag_inexact;
1646 return 0;
1648 shiftCount -= 0x10;
1649 aSig = ( aSig | 0x00800000 )<<8;
1650 z = aSig>>( - shiftCount );
1651 if ( (uint32_t) ( aSig<<( shiftCount & 31 ) ) ) {
1652 status->float_exception_flags |= float_flag_inexact;
1654 if ( aSign ) {
1655 z = - z;
1657 return z;
1661 /*----------------------------------------------------------------------------
1662 | Returns the result of converting the single-precision floating-point value
1663 | `a' to the 64-bit two's complement integer format. The conversion is
1664 | performed according to the IEC/IEEE Standard for Binary Floating-Point
1665 | Arithmetic---which means in particular that the conversion is rounded
1666 | according to the current rounding mode. If `a' is a NaN, the largest
1667 | positive integer is returned. Otherwise, if the conversion overflows, the
1668 | largest integer with the same sign as `a' is returned.
1669 *----------------------------------------------------------------------------*/
1671 int64_t float32_to_int64(float32 a, float_status *status)
1673 flag aSign;
1674 int aExp;
1675 int shiftCount;
1676 uint32_t aSig;
1677 uint64_t aSig64, aSigExtra;
1678 a = float32_squash_input_denormal(a, status);
1680 aSig = extractFloat32Frac( a );
1681 aExp = extractFloat32Exp( a );
1682 aSign = extractFloat32Sign( a );
1683 shiftCount = 0xBE - aExp;
1684 if ( shiftCount < 0 ) {
1685 float_raise(float_flag_invalid, status);
1686 if ( ! aSign || ( ( aExp == 0xFF ) && aSig ) ) {
1687 return LIT64( 0x7FFFFFFFFFFFFFFF );
1689 return (int64_t) LIT64( 0x8000000000000000 );
1691 if ( aExp ) aSig |= 0x00800000;
1692 aSig64 = aSig;
1693 aSig64 <<= 40;
1694 shift64ExtraRightJamming( aSig64, 0, shiftCount, &aSig64, &aSigExtra );
1695 return roundAndPackInt64(aSign, aSig64, aSigExtra, status);
1699 /*----------------------------------------------------------------------------
1700 | Returns the result of converting the single-precision floating-point value
1701 | `a' to the 64-bit unsigned integer format. The conversion is
1702 | performed according to the IEC/IEEE Standard for Binary Floating-Point
1703 | Arithmetic---which means in particular that the conversion is rounded
1704 | according to the current rounding mode. If `a' is a NaN, the largest
1705 | unsigned integer is returned. Otherwise, if the conversion overflows, the
1706 | largest unsigned integer is returned. If the 'a' is negative, the result
1707 | is rounded and zero is returned; values that do not round to zero will
1708 | raise the inexact exception flag.
1709 *----------------------------------------------------------------------------*/
1711 uint64_t float32_to_uint64(float32 a, float_status *status)
1713 flag aSign;
1714 int aExp;
1715 int shiftCount;
1716 uint32_t aSig;
1717 uint64_t aSig64, aSigExtra;
1718 a = float32_squash_input_denormal(a, status);
1720 aSig = extractFloat32Frac(a);
1721 aExp = extractFloat32Exp(a);
1722 aSign = extractFloat32Sign(a);
1723 if ((aSign) && (aExp > 126)) {
1724 float_raise(float_flag_invalid, status);
1725 if (float32_is_any_nan(a)) {
1726 return LIT64(0xFFFFFFFFFFFFFFFF);
1727 } else {
1728 return 0;
1731 shiftCount = 0xBE - aExp;
1732 if (aExp) {
1733 aSig |= 0x00800000;
1735 if (shiftCount < 0) {
1736 float_raise(float_flag_invalid, status);
1737 return LIT64(0xFFFFFFFFFFFFFFFF);
1740 aSig64 = aSig;
1741 aSig64 <<= 40;
1742 shift64ExtraRightJamming(aSig64, 0, shiftCount, &aSig64, &aSigExtra);
1743 return roundAndPackUint64(aSign, aSig64, aSigExtra, status);
1746 /*----------------------------------------------------------------------------
1747 | Returns the result of converting the single-precision floating-point value
1748 | `a' to the 64-bit unsigned integer format. The conversion is
1749 | performed according to the IEC/IEEE Standard for Binary Floating-Point
1750 | Arithmetic, except that the conversion is always rounded toward zero. If
1751 | `a' is a NaN, the largest unsigned integer is returned. Otherwise, if the
1752 | conversion overflows, the largest unsigned integer is returned. If the
1753 | 'a' is negative, the result is rounded and zero is returned; values that do
1754 | not round to zero will raise the inexact flag.
1755 *----------------------------------------------------------------------------*/
1757 uint64_t float32_to_uint64_round_to_zero(float32 a, float_status *status)
1759 signed char current_rounding_mode = status->float_rounding_mode;
1760 set_float_rounding_mode(float_round_to_zero, status);
1761 int64_t v = float32_to_uint64(a, status);
1762 set_float_rounding_mode(current_rounding_mode, status);
1763 return v;
1766 /*----------------------------------------------------------------------------
1767 | Returns the result of converting the single-precision floating-point value
1768 | `a' to the 64-bit two's complement integer format. The conversion is
1769 | performed according to the IEC/IEEE Standard for Binary Floating-Point
1770 | Arithmetic, except that the conversion is always rounded toward zero. If
1771 | `a' is a NaN, the largest positive integer is returned. Otherwise, if the
1772 | conversion overflows, the largest integer with the same sign as `a' is
1773 | returned.
1774 *----------------------------------------------------------------------------*/
1776 int64_t float32_to_int64_round_to_zero(float32 a, float_status *status)
1778 flag aSign;
1779 int aExp;
1780 int shiftCount;
1781 uint32_t aSig;
1782 uint64_t aSig64;
1783 int64_t z;
1784 a = float32_squash_input_denormal(a, status);
1786 aSig = extractFloat32Frac( a );
1787 aExp = extractFloat32Exp( a );
1788 aSign = extractFloat32Sign( a );
1789 shiftCount = aExp - 0xBE;
1790 if ( 0 <= shiftCount ) {
1791 if ( float32_val(a) != 0xDF000000 ) {
1792 float_raise(float_flag_invalid, status);
1793 if ( ! aSign || ( ( aExp == 0xFF ) && aSig ) ) {
1794 return LIT64( 0x7FFFFFFFFFFFFFFF );
1797 return (int64_t) LIT64( 0x8000000000000000 );
1799 else if ( aExp <= 0x7E ) {
1800 if (aExp | aSig) {
1801 status->float_exception_flags |= float_flag_inexact;
1803 return 0;
1805 aSig64 = aSig | 0x00800000;
1806 aSig64 <<= 40;
1807 z = aSig64>>( - shiftCount );
1808 if ( (uint64_t) ( aSig64<<( shiftCount & 63 ) ) ) {
1809 status->float_exception_flags |= float_flag_inexact;
1811 if ( aSign ) z = - z;
1812 return z;
1816 /*----------------------------------------------------------------------------
1817 | Returns the result of converting the single-precision floating-point value
1818 | `a' to the double-precision floating-point format. The conversion is
1819 | performed according to the IEC/IEEE Standard for Binary Floating-Point
1820 | Arithmetic.
1821 *----------------------------------------------------------------------------*/
1823 float64 float32_to_float64(float32 a, float_status *status)
1825 flag aSign;
1826 int aExp;
1827 uint32_t aSig;
1828 a = float32_squash_input_denormal(a, status);
1830 aSig = extractFloat32Frac( a );
1831 aExp = extractFloat32Exp( a );
1832 aSign = extractFloat32Sign( a );
1833 if ( aExp == 0xFF ) {
1834 if (aSig) {
1835 return commonNaNToFloat64(float32ToCommonNaN(a, status), status);
1837 return packFloat64( aSign, 0x7FF, 0 );
1839 if ( aExp == 0 ) {
1840 if ( aSig == 0 ) return packFloat64( aSign, 0, 0 );
1841 normalizeFloat32Subnormal( aSig, &aExp, &aSig );
1842 --aExp;
1844 return packFloat64( aSign, aExp + 0x380, ( (uint64_t) aSig )<<29 );
1848 /*----------------------------------------------------------------------------
1849 | Returns the result of converting the single-precision floating-point value
1850 | `a' to the extended double-precision floating-point format. The conversion
1851 | is performed according to the IEC/IEEE Standard for Binary Floating-Point
1852 | Arithmetic.
1853 *----------------------------------------------------------------------------*/
1855 floatx80 float32_to_floatx80(float32 a, float_status *status)
1857 flag aSign;
1858 int aExp;
1859 uint32_t aSig;
1861 a = float32_squash_input_denormal(a, status);
1862 aSig = extractFloat32Frac( a );
1863 aExp = extractFloat32Exp( a );
1864 aSign = extractFloat32Sign( a );
1865 if ( aExp == 0xFF ) {
1866 if (aSig) {
1867 return commonNaNToFloatx80(float32ToCommonNaN(a, status), status);
1869 return packFloatx80( aSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
1871 if ( aExp == 0 ) {
1872 if ( aSig == 0 ) return packFloatx80( aSign, 0, 0 );
1873 normalizeFloat32Subnormal( aSig, &aExp, &aSig );
1875 aSig |= 0x00800000;
1876 return packFloatx80( aSign, aExp + 0x3F80, ( (uint64_t) aSig )<<40 );
1880 /*----------------------------------------------------------------------------
1881 | Returns the result of converting the single-precision floating-point value
1882 | `a' to the double-precision floating-point format. The conversion is
1883 | performed according to the IEC/IEEE Standard for Binary Floating-Point
1884 | Arithmetic.
1885 *----------------------------------------------------------------------------*/
1887 float128 float32_to_float128(float32 a, float_status *status)
1889 flag aSign;
1890 int aExp;
1891 uint32_t aSig;
1893 a = float32_squash_input_denormal(a, status);
1894 aSig = extractFloat32Frac( a );
1895 aExp = extractFloat32Exp( a );
1896 aSign = extractFloat32Sign( a );
1897 if ( aExp == 0xFF ) {
1898 if (aSig) {
1899 return commonNaNToFloat128(float32ToCommonNaN(a, status), status);
1901 return packFloat128( aSign, 0x7FFF, 0, 0 );
1903 if ( aExp == 0 ) {
1904 if ( aSig == 0 ) return packFloat128( aSign, 0, 0, 0 );
1905 normalizeFloat32Subnormal( aSig, &aExp, &aSig );
1906 --aExp;
1908 return packFloat128( aSign, aExp + 0x3F80, ( (uint64_t) aSig )<<25, 0 );
1912 /*----------------------------------------------------------------------------
1913 | Rounds the single-precision floating-point value `a' to an integer, and
1914 | returns the result as a single-precision floating-point value. The
1915 | operation is performed according to the IEC/IEEE Standard for Binary
1916 | Floating-Point Arithmetic.
1917 *----------------------------------------------------------------------------*/
1919 float32 float32_round_to_int(float32 a, float_status *status)
1921 flag aSign;
1922 int aExp;
1923 uint32_t lastBitMask, roundBitsMask;
1924 uint32_t z;
1925 a = float32_squash_input_denormal(a, status);
1927 aExp = extractFloat32Exp( a );
1928 if ( 0x96 <= aExp ) {
1929 if ( ( aExp == 0xFF ) && extractFloat32Frac( a ) ) {
1930 return propagateFloat32NaN(a, a, status);
1932 return a;
1934 if ( aExp <= 0x7E ) {
1935 if ( (uint32_t) ( float32_val(a)<<1 ) == 0 ) return a;
1936 status->float_exception_flags |= float_flag_inexact;
1937 aSign = extractFloat32Sign( a );
1938 switch (status->float_rounding_mode) {
1939 case float_round_nearest_even:
1940 if ( ( aExp == 0x7E ) && extractFloat32Frac( a ) ) {
1941 return packFloat32( aSign, 0x7F, 0 );
1943 break;
1944 case float_round_ties_away:
1945 if (aExp == 0x7E) {
1946 return packFloat32(aSign, 0x7F, 0);
1948 break;
1949 case float_round_down:
1950 return make_float32(aSign ? 0xBF800000 : 0);
1951 case float_round_up:
1952 return make_float32(aSign ? 0x80000000 : 0x3F800000);
1954 return packFloat32( aSign, 0, 0 );
1956 lastBitMask = 1;
1957 lastBitMask <<= 0x96 - aExp;
1958 roundBitsMask = lastBitMask - 1;
1959 z = float32_val(a);
1960 switch (status->float_rounding_mode) {
1961 case float_round_nearest_even:
1962 z += lastBitMask>>1;
1963 if ((z & roundBitsMask) == 0) {
1964 z &= ~lastBitMask;
1966 break;
1967 case float_round_ties_away:
1968 z += lastBitMask >> 1;
1969 break;
1970 case float_round_to_zero:
1971 break;
1972 case float_round_up:
1973 if (!extractFloat32Sign(make_float32(z))) {
1974 z += roundBitsMask;
1976 break;
1977 case float_round_down:
1978 if (extractFloat32Sign(make_float32(z))) {
1979 z += roundBitsMask;
1981 break;
1982 default:
1983 abort();
1985 z &= ~ roundBitsMask;
1986 if (z != float32_val(a)) {
1987 status->float_exception_flags |= float_flag_inexact;
1989 return make_float32(z);
1993 /*----------------------------------------------------------------------------
1994 | Returns the result of adding the absolute values of the single-precision
1995 | floating-point values `a' and `b'. If `zSign' is 1, the sum is negated
1996 | before being returned. `zSign' is ignored if the result is a NaN.
1997 | The addition is performed according to the IEC/IEEE Standard for Binary
1998 | Floating-Point Arithmetic.
1999 *----------------------------------------------------------------------------*/
2001 static float32 addFloat32Sigs(float32 a, float32 b, flag zSign,
2002 float_status *status)
2004 int aExp, bExp, zExp;
2005 uint32_t aSig, bSig, zSig;
2006 int expDiff;
2008 aSig = extractFloat32Frac( a );
2009 aExp = extractFloat32Exp( a );
2010 bSig = extractFloat32Frac( b );
2011 bExp = extractFloat32Exp( b );
2012 expDiff = aExp - bExp;
2013 aSig <<= 6;
2014 bSig <<= 6;
2015 if ( 0 < expDiff ) {
2016 if ( aExp == 0xFF ) {
2017 if (aSig) {
2018 return propagateFloat32NaN(a, b, status);
2020 return a;
2022 if ( bExp == 0 ) {
2023 --expDiff;
2025 else {
2026 bSig |= 0x20000000;
2028 shift32RightJamming( bSig, expDiff, &bSig );
2029 zExp = aExp;
2031 else if ( expDiff < 0 ) {
2032 if ( bExp == 0xFF ) {
2033 if (bSig) {
2034 return propagateFloat32NaN(a, b, status);
2036 return packFloat32( zSign, 0xFF, 0 );
2038 if ( aExp == 0 ) {
2039 ++expDiff;
2041 else {
2042 aSig |= 0x20000000;
2044 shift32RightJamming( aSig, - expDiff, &aSig );
2045 zExp = bExp;
2047 else {
2048 if ( aExp == 0xFF ) {
2049 if (aSig | bSig) {
2050 return propagateFloat32NaN(a, b, status);
2052 return a;
2054 if ( aExp == 0 ) {
2055 if (status->flush_to_zero) {
2056 if (aSig | bSig) {
2057 float_raise(float_flag_output_denormal, status);
2059 return packFloat32(zSign, 0, 0);
2061 return packFloat32( zSign, 0, ( aSig + bSig )>>6 );
2063 zSig = 0x40000000 + aSig + bSig;
2064 zExp = aExp;
2065 goto roundAndPack;
2067 aSig |= 0x20000000;
2068 zSig = ( aSig + bSig )<<1;
2069 --zExp;
2070 if ( (int32_t) zSig < 0 ) {
2071 zSig = aSig + bSig;
2072 ++zExp;
2074 roundAndPack:
2075 return roundAndPackFloat32(zSign, zExp, zSig, status);
2079 /*----------------------------------------------------------------------------
2080 | Returns the result of subtracting the absolute values of the single-
2081 | precision floating-point values `a' and `b'. If `zSign' is 1, the
2082 | difference is negated before being returned. `zSign' is ignored if the
2083 | result is a NaN. The subtraction is performed according to the IEC/IEEE
2084 | Standard for Binary Floating-Point Arithmetic.
2085 *----------------------------------------------------------------------------*/
2087 static float32 subFloat32Sigs(float32 a, float32 b, flag zSign,
2088 float_status *status)
2090 int aExp, bExp, zExp;
2091 uint32_t aSig, bSig, zSig;
2092 int expDiff;
2094 aSig = extractFloat32Frac( a );
2095 aExp = extractFloat32Exp( a );
2096 bSig = extractFloat32Frac( b );
2097 bExp = extractFloat32Exp( b );
2098 expDiff = aExp - bExp;
2099 aSig <<= 7;
2100 bSig <<= 7;
2101 if ( 0 < expDiff ) goto aExpBigger;
2102 if ( expDiff < 0 ) goto bExpBigger;
2103 if ( aExp == 0xFF ) {
2104 if (aSig | bSig) {
2105 return propagateFloat32NaN(a, b, status);
2107 float_raise(float_flag_invalid, status);
2108 return float32_default_nan(status);
2110 if ( aExp == 0 ) {
2111 aExp = 1;
2112 bExp = 1;
2114 if ( bSig < aSig ) goto aBigger;
2115 if ( aSig < bSig ) goto bBigger;
2116 return packFloat32(status->float_rounding_mode == float_round_down, 0, 0);
2117 bExpBigger:
2118 if ( bExp == 0xFF ) {
2119 if (bSig) {
2120 return propagateFloat32NaN(a, b, status);
2122 return packFloat32( zSign ^ 1, 0xFF, 0 );
2124 if ( aExp == 0 ) {
2125 ++expDiff;
2127 else {
2128 aSig |= 0x40000000;
2130 shift32RightJamming( aSig, - expDiff, &aSig );
2131 bSig |= 0x40000000;
2132 bBigger:
2133 zSig = bSig - aSig;
2134 zExp = bExp;
2135 zSign ^= 1;
2136 goto normalizeRoundAndPack;
2137 aExpBigger:
2138 if ( aExp == 0xFF ) {
2139 if (aSig) {
2140 return propagateFloat32NaN(a, b, status);
2142 return a;
2144 if ( bExp == 0 ) {
2145 --expDiff;
2147 else {
2148 bSig |= 0x40000000;
2150 shift32RightJamming( bSig, expDiff, &bSig );
2151 aSig |= 0x40000000;
2152 aBigger:
2153 zSig = aSig - bSig;
2154 zExp = aExp;
2155 normalizeRoundAndPack:
2156 --zExp;
2157 return normalizeRoundAndPackFloat32(zSign, zExp, zSig, status);
2161 /*----------------------------------------------------------------------------
2162 | Returns the result of adding the single-precision floating-point values `a'
2163 | and `b'. The operation is performed according to the IEC/IEEE Standard for
2164 | Binary Floating-Point Arithmetic.
2165 *----------------------------------------------------------------------------*/
2167 float32 float32_add(float32 a, float32 b, float_status *status)
2169 flag aSign, bSign;
2170 a = float32_squash_input_denormal(a, status);
2171 b = float32_squash_input_denormal(b, status);
2173 aSign = extractFloat32Sign( a );
2174 bSign = extractFloat32Sign( b );
2175 if ( aSign == bSign ) {
2176 return addFloat32Sigs(a, b, aSign, status);
2178 else {
2179 return subFloat32Sigs(a, b, aSign, status);
2184 /*----------------------------------------------------------------------------
2185 | Returns the result of subtracting the single-precision floating-point values
2186 | `a' and `b'. The operation is performed according to the IEC/IEEE Standard
2187 | for Binary Floating-Point Arithmetic.
2188 *----------------------------------------------------------------------------*/
2190 float32 float32_sub(float32 a, float32 b, float_status *status)
2192 flag aSign, bSign;
2193 a = float32_squash_input_denormal(a, status);
2194 b = float32_squash_input_denormal(b, status);
2196 aSign = extractFloat32Sign( a );
2197 bSign = extractFloat32Sign( b );
2198 if ( aSign == bSign ) {
2199 return subFloat32Sigs(a, b, aSign, status);
2201 else {
2202 return addFloat32Sigs(a, b, aSign, status);
2207 /*----------------------------------------------------------------------------
2208 | Returns the result of multiplying the single-precision floating-point values
2209 | `a' and `b'. The operation is performed according to the IEC/IEEE Standard
2210 | for Binary Floating-Point Arithmetic.
2211 *----------------------------------------------------------------------------*/
2213 float32 float32_mul(float32 a, float32 b, float_status *status)
2215 flag aSign, bSign, zSign;
2216 int aExp, bExp, zExp;
2217 uint32_t aSig, bSig;
2218 uint64_t zSig64;
2219 uint32_t zSig;
2221 a = float32_squash_input_denormal(a, status);
2222 b = float32_squash_input_denormal(b, status);
2224 aSig = extractFloat32Frac( a );
2225 aExp = extractFloat32Exp( a );
2226 aSign = extractFloat32Sign( a );
2227 bSig = extractFloat32Frac( b );
2228 bExp = extractFloat32Exp( b );
2229 bSign = extractFloat32Sign( b );
2230 zSign = aSign ^ bSign;
2231 if ( aExp == 0xFF ) {
2232 if ( aSig || ( ( bExp == 0xFF ) && bSig ) ) {
2233 return propagateFloat32NaN(a, b, status);
2235 if ( ( bExp | bSig ) == 0 ) {
2236 float_raise(float_flag_invalid, status);
2237 return float32_default_nan(status);
2239 return packFloat32( zSign, 0xFF, 0 );
2241 if ( bExp == 0xFF ) {
2242 if (bSig) {
2243 return propagateFloat32NaN(a, b, status);
2245 if ( ( aExp | aSig ) == 0 ) {
2246 float_raise(float_flag_invalid, status);
2247 return float32_default_nan(status);
2249 return packFloat32( zSign, 0xFF, 0 );
2251 if ( aExp == 0 ) {
2252 if ( aSig == 0 ) return packFloat32( zSign, 0, 0 );
2253 normalizeFloat32Subnormal( aSig, &aExp, &aSig );
2255 if ( bExp == 0 ) {
2256 if ( bSig == 0 ) return packFloat32( zSign, 0, 0 );
2257 normalizeFloat32Subnormal( bSig, &bExp, &bSig );
2259 zExp = aExp + bExp - 0x7F;
2260 aSig = ( aSig | 0x00800000 )<<7;
2261 bSig = ( bSig | 0x00800000 )<<8;
2262 shift64RightJamming( ( (uint64_t) aSig ) * bSig, 32, &zSig64 );
2263 zSig = zSig64;
2264 if ( 0 <= (int32_t) ( zSig<<1 ) ) {
2265 zSig <<= 1;
2266 --zExp;
2268 return roundAndPackFloat32(zSign, zExp, zSig, status);
2272 /*----------------------------------------------------------------------------
2273 | Returns the result of dividing the single-precision floating-point value `a'
2274 | by the corresponding value `b'. The operation is performed according to the
2275 | IEC/IEEE Standard for Binary Floating-Point Arithmetic.
2276 *----------------------------------------------------------------------------*/
2278 float32 float32_div(float32 a, float32 b, float_status *status)
2280 flag aSign, bSign, zSign;
2281 int aExp, bExp, zExp;
2282 uint32_t aSig, bSig, zSig;
2283 a = float32_squash_input_denormal(a, status);
2284 b = float32_squash_input_denormal(b, status);
2286 aSig = extractFloat32Frac( a );
2287 aExp = extractFloat32Exp( a );
2288 aSign = extractFloat32Sign( a );
2289 bSig = extractFloat32Frac( b );
2290 bExp = extractFloat32Exp( b );
2291 bSign = extractFloat32Sign( b );
2292 zSign = aSign ^ bSign;
2293 if ( aExp == 0xFF ) {
2294 if (aSig) {
2295 return propagateFloat32NaN(a, b, status);
2297 if ( bExp == 0xFF ) {
2298 if (bSig) {
2299 return propagateFloat32NaN(a, b, status);
2301 float_raise(float_flag_invalid, status);
2302 return float32_default_nan(status);
2304 return packFloat32( zSign, 0xFF, 0 );
2306 if ( bExp == 0xFF ) {
2307 if (bSig) {
2308 return propagateFloat32NaN(a, b, status);
2310 return packFloat32( zSign, 0, 0 );
2312 if ( bExp == 0 ) {
2313 if ( bSig == 0 ) {
2314 if ( ( aExp | aSig ) == 0 ) {
2315 float_raise(float_flag_invalid, status);
2316 return float32_default_nan(status);
2318 float_raise(float_flag_divbyzero, status);
2319 return packFloat32( zSign, 0xFF, 0 );
2321 normalizeFloat32Subnormal( bSig, &bExp, &bSig );
2323 if ( aExp == 0 ) {
2324 if ( aSig == 0 ) return packFloat32( zSign, 0, 0 );
2325 normalizeFloat32Subnormal( aSig, &aExp, &aSig );
2327 zExp = aExp - bExp + 0x7D;
2328 aSig = ( aSig | 0x00800000 )<<7;
2329 bSig = ( bSig | 0x00800000 )<<8;
2330 if ( bSig <= ( aSig + aSig ) ) {
2331 aSig >>= 1;
2332 ++zExp;
2334 zSig = ( ( (uint64_t) aSig )<<32 ) / bSig;
2335 if ( ( zSig & 0x3F ) == 0 ) {
2336 zSig |= ( (uint64_t) bSig * zSig != ( (uint64_t) aSig )<<32 );
2338 return roundAndPackFloat32(zSign, zExp, zSig, status);
2342 /*----------------------------------------------------------------------------
2343 | Returns the remainder of the single-precision floating-point value `a'
2344 | with respect to the corresponding value `b'. The operation is performed
2345 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
2346 *----------------------------------------------------------------------------*/
2348 float32 float32_rem(float32 a, float32 b, float_status *status)
2350 flag aSign, zSign;
2351 int aExp, bExp, expDiff;
2352 uint32_t aSig, bSig;
2353 uint32_t q;
2354 uint64_t aSig64, bSig64, q64;
2355 uint32_t alternateASig;
2356 int32_t sigMean;
2357 a = float32_squash_input_denormal(a, status);
2358 b = float32_squash_input_denormal(b, status);
2360 aSig = extractFloat32Frac( a );
2361 aExp = extractFloat32Exp( a );
2362 aSign = extractFloat32Sign( a );
2363 bSig = extractFloat32Frac( b );
2364 bExp = extractFloat32Exp( b );
2365 if ( aExp == 0xFF ) {
2366 if ( aSig || ( ( bExp == 0xFF ) && bSig ) ) {
2367 return propagateFloat32NaN(a, b, status);
2369 float_raise(float_flag_invalid, status);
2370 return float32_default_nan(status);
2372 if ( bExp == 0xFF ) {
2373 if (bSig) {
2374 return propagateFloat32NaN(a, b, status);
2376 return a;
2378 if ( bExp == 0 ) {
2379 if ( bSig == 0 ) {
2380 float_raise(float_flag_invalid, status);
2381 return float32_default_nan(status);
2383 normalizeFloat32Subnormal( bSig, &bExp, &bSig );
2385 if ( aExp == 0 ) {
2386 if ( aSig == 0 ) return a;
2387 normalizeFloat32Subnormal( aSig, &aExp, &aSig );
2389 expDiff = aExp - bExp;
2390 aSig |= 0x00800000;
2391 bSig |= 0x00800000;
2392 if ( expDiff < 32 ) {
2393 aSig <<= 8;
2394 bSig <<= 8;
2395 if ( expDiff < 0 ) {
2396 if ( expDiff < -1 ) return a;
2397 aSig >>= 1;
2399 q = ( bSig <= aSig );
2400 if ( q ) aSig -= bSig;
2401 if ( 0 < expDiff ) {
2402 q = ( ( (uint64_t) aSig )<<32 ) / bSig;
2403 q >>= 32 - expDiff;
2404 bSig >>= 2;
2405 aSig = ( ( aSig>>1 )<<( expDiff - 1 ) ) - bSig * q;
2407 else {
2408 aSig >>= 2;
2409 bSig >>= 2;
2412 else {
2413 if ( bSig <= aSig ) aSig -= bSig;
2414 aSig64 = ( (uint64_t) aSig )<<40;
2415 bSig64 = ( (uint64_t) bSig )<<40;
2416 expDiff -= 64;
2417 while ( 0 < expDiff ) {
2418 q64 = estimateDiv128To64( aSig64, 0, bSig64 );
2419 q64 = ( 2 < q64 ) ? q64 - 2 : 0;
2420 aSig64 = - ( ( bSig * q64 )<<38 );
2421 expDiff -= 62;
2423 expDiff += 64;
2424 q64 = estimateDiv128To64( aSig64, 0, bSig64 );
2425 q64 = ( 2 < q64 ) ? q64 - 2 : 0;
2426 q = q64>>( 64 - expDiff );
2427 bSig <<= 6;
2428 aSig = ( ( aSig64>>33 )<<( expDiff - 1 ) ) - bSig * q;
2430 do {
2431 alternateASig = aSig;
2432 ++q;
2433 aSig -= bSig;
2434 } while ( 0 <= (int32_t) aSig );
2435 sigMean = aSig + alternateASig;
2436 if ( ( sigMean < 0 ) || ( ( sigMean == 0 ) && ( q & 1 ) ) ) {
2437 aSig = alternateASig;
2439 zSign = ( (int32_t) aSig < 0 );
2440 if ( zSign ) aSig = - aSig;
2441 return normalizeRoundAndPackFloat32(aSign ^ zSign, bExp, aSig, status);
2444 /*----------------------------------------------------------------------------
2445 | Returns the result of multiplying the single-precision floating-point values
2446 | `a' and `b' then adding 'c', with no intermediate rounding step after the
2447 | multiplication. The operation is performed according to the IEC/IEEE
2448 | Standard for Binary Floating-Point Arithmetic 754-2008.
2449 | The flags argument allows the caller to select negation of the
2450 | addend, the intermediate product, or the final result. (The difference
2451 | between this and having the caller do a separate negation is that negating
2452 | externally will flip the sign bit on NaNs.)
2453 *----------------------------------------------------------------------------*/
2455 float32 float32_muladd(float32 a, float32 b, float32 c, int flags,
2456 float_status *status)
2458 flag aSign, bSign, cSign, zSign;
2459 int aExp, bExp, cExp, pExp, zExp, expDiff;
2460 uint32_t aSig, bSig, cSig;
2461 flag pInf, pZero, pSign;
2462 uint64_t pSig64, cSig64, zSig64;
2463 uint32_t pSig;
2464 int shiftcount;
2465 flag signflip, infzero;
2467 a = float32_squash_input_denormal(a, status);
2468 b = float32_squash_input_denormal(b, status);
2469 c = float32_squash_input_denormal(c, status);
2470 aSig = extractFloat32Frac(a);
2471 aExp = extractFloat32Exp(a);
2472 aSign = extractFloat32Sign(a);
2473 bSig = extractFloat32Frac(b);
2474 bExp = extractFloat32Exp(b);
2475 bSign = extractFloat32Sign(b);
2476 cSig = extractFloat32Frac(c);
2477 cExp = extractFloat32Exp(c);
2478 cSign = extractFloat32Sign(c);
2480 infzero = ((aExp == 0 && aSig == 0 && bExp == 0xff && bSig == 0) ||
2481 (aExp == 0xff && aSig == 0 && bExp == 0 && bSig == 0));
2483 /* It is implementation-defined whether the cases of (0,inf,qnan)
2484 * and (inf,0,qnan) raise InvalidOperation or not (and what QNaN
2485 * they return if they do), so we have to hand this information
2486 * off to the target-specific pick-a-NaN routine.
2488 if (((aExp == 0xff) && aSig) ||
2489 ((bExp == 0xff) && bSig) ||
2490 ((cExp == 0xff) && cSig)) {
2491 return propagateFloat32MulAddNaN(a, b, c, infzero, status);
2494 if (infzero) {
2495 float_raise(float_flag_invalid, status);
2496 return float32_default_nan(status);
2499 if (flags & float_muladd_negate_c) {
2500 cSign ^= 1;
2503 signflip = (flags & float_muladd_negate_result) ? 1 : 0;
2505 /* Work out the sign and type of the product */
2506 pSign = aSign ^ bSign;
2507 if (flags & float_muladd_negate_product) {
2508 pSign ^= 1;
2510 pInf = (aExp == 0xff) || (bExp == 0xff);
2511 pZero = ((aExp | aSig) == 0) || ((bExp | bSig) == 0);
2513 if (cExp == 0xff) {
2514 if (pInf && (pSign ^ cSign)) {
2515 /* addition of opposite-signed infinities => InvalidOperation */
2516 float_raise(float_flag_invalid, status);
2517 return float32_default_nan(status);
2519 /* Otherwise generate an infinity of the same sign */
2520 return packFloat32(cSign ^ signflip, 0xff, 0);
2523 if (pInf) {
2524 return packFloat32(pSign ^ signflip, 0xff, 0);
2527 if (pZero) {
2528 if (cExp == 0) {
2529 if (cSig == 0) {
2530 /* Adding two exact zeroes */
2531 if (pSign == cSign) {
2532 zSign = pSign;
2533 } else if (status->float_rounding_mode == float_round_down) {
2534 zSign = 1;
2535 } else {
2536 zSign = 0;
2538 return packFloat32(zSign ^ signflip, 0, 0);
2540 /* Exact zero plus a denorm */
2541 if (status->flush_to_zero) {
2542 float_raise(float_flag_output_denormal, status);
2543 return packFloat32(cSign ^ signflip, 0, 0);
2546 /* Zero plus something non-zero : just return the something */
2547 if (flags & float_muladd_halve_result) {
2548 if (cExp == 0) {
2549 normalizeFloat32Subnormal(cSig, &cExp, &cSig);
2551 /* Subtract one to halve, and one again because roundAndPackFloat32
2552 * wants one less than the true exponent.
2554 cExp -= 2;
2555 cSig = (cSig | 0x00800000) << 7;
2556 return roundAndPackFloat32(cSign ^ signflip, cExp, cSig, status);
2558 return packFloat32(cSign ^ signflip, cExp, cSig);
2561 if (aExp == 0) {
2562 normalizeFloat32Subnormal(aSig, &aExp, &aSig);
2564 if (bExp == 0) {
2565 normalizeFloat32Subnormal(bSig, &bExp, &bSig);
2568 /* Calculate the actual result a * b + c */
2570 /* Multiply first; this is easy. */
2571 /* NB: we subtract 0x7e where float32_mul() subtracts 0x7f
2572 * because we want the true exponent, not the "one-less-than"
2573 * flavour that roundAndPackFloat32() takes.
2575 pExp = aExp + bExp - 0x7e;
2576 aSig = (aSig | 0x00800000) << 7;
2577 bSig = (bSig | 0x00800000) << 8;
2578 pSig64 = (uint64_t)aSig * bSig;
2579 if ((int64_t)(pSig64 << 1) >= 0) {
2580 pSig64 <<= 1;
2581 pExp--;
2584 zSign = pSign ^ signflip;
2586 /* Now pSig64 is the significand of the multiply, with the explicit bit in
2587 * position 62.
2589 if (cExp == 0) {
2590 if (!cSig) {
2591 /* Throw out the special case of c being an exact zero now */
2592 shift64RightJamming(pSig64, 32, &pSig64);
2593 pSig = pSig64;
2594 if (flags & float_muladd_halve_result) {
2595 pExp--;
2597 return roundAndPackFloat32(zSign, pExp - 1,
2598 pSig, status);
2600 normalizeFloat32Subnormal(cSig, &cExp, &cSig);
2603 cSig64 = (uint64_t)cSig << (62 - 23);
2604 cSig64 |= LIT64(0x4000000000000000);
2605 expDiff = pExp - cExp;
2607 if (pSign == cSign) {
2608 /* Addition */
2609 if (expDiff > 0) {
2610 /* scale c to match p */
2611 shift64RightJamming(cSig64, expDiff, &cSig64);
2612 zExp = pExp;
2613 } else if (expDiff < 0) {
2614 /* scale p to match c */
2615 shift64RightJamming(pSig64, -expDiff, &pSig64);
2616 zExp = cExp;
2617 } else {
2618 /* no scaling needed */
2619 zExp = cExp;
2621 /* Add significands and make sure explicit bit ends up in posn 62 */
2622 zSig64 = pSig64 + cSig64;
2623 if ((int64_t)zSig64 < 0) {
2624 shift64RightJamming(zSig64, 1, &zSig64);
2625 } else {
2626 zExp--;
2628 } else {
2629 /* Subtraction */
2630 if (expDiff > 0) {
2631 shift64RightJamming(cSig64, expDiff, &cSig64);
2632 zSig64 = pSig64 - cSig64;
2633 zExp = pExp;
2634 } else if (expDiff < 0) {
2635 shift64RightJamming(pSig64, -expDiff, &pSig64);
2636 zSig64 = cSig64 - pSig64;
2637 zExp = cExp;
2638 zSign ^= 1;
2639 } else {
2640 zExp = pExp;
2641 if (cSig64 < pSig64) {
2642 zSig64 = pSig64 - cSig64;
2643 } else if (pSig64 < cSig64) {
2644 zSig64 = cSig64 - pSig64;
2645 zSign ^= 1;
2646 } else {
2647 /* Exact zero */
2648 zSign = signflip;
2649 if (status->float_rounding_mode == float_round_down) {
2650 zSign ^= 1;
2652 return packFloat32(zSign, 0, 0);
2655 --zExp;
2656 /* Normalize to put the explicit bit back into bit 62. */
2657 shiftcount = countLeadingZeros64(zSig64) - 1;
2658 zSig64 <<= shiftcount;
2659 zExp -= shiftcount;
2661 if (flags & float_muladd_halve_result) {
2662 zExp--;
2665 shift64RightJamming(zSig64, 32, &zSig64);
2666 return roundAndPackFloat32(zSign, zExp, zSig64, status);
2670 /*----------------------------------------------------------------------------
2671 | Returns the square root of the single-precision floating-point value `a'.
2672 | The operation is performed according to the IEC/IEEE Standard for Binary
2673 | Floating-Point Arithmetic.
2674 *----------------------------------------------------------------------------*/
2676 float32 float32_sqrt(float32 a, float_status *status)
2678 flag aSign;
2679 int aExp, zExp;
2680 uint32_t aSig, zSig;
2681 uint64_t rem, term;
2682 a = float32_squash_input_denormal(a, status);
2684 aSig = extractFloat32Frac( a );
2685 aExp = extractFloat32Exp( a );
2686 aSign = extractFloat32Sign( a );
2687 if ( aExp == 0xFF ) {
2688 if (aSig) {
2689 return propagateFloat32NaN(a, float32_zero, status);
2691 if ( ! aSign ) return a;
2692 float_raise(float_flag_invalid, status);
2693 return float32_default_nan(status);
2695 if ( aSign ) {
2696 if ( ( aExp | aSig ) == 0 ) return a;
2697 float_raise(float_flag_invalid, status);
2698 return float32_default_nan(status);
2700 if ( aExp == 0 ) {
2701 if ( aSig == 0 ) return float32_zero;
2702 normalizeFloat32Subnormal( aSig, &aExp, &aSig );
2704 zExp = ( ( aExp - 0x7F )>>1 ) + 0x7E;
2705 aSig = ( aSig | 0x00800000 )<<8;
2706 zSig = estimateSqrt32( aExp, aSig ) + 2;
2707 if ( ( zSig & 0x7F ) <= 5 ) {
2708 if ( zSig < 2 ) {
2709 zSig = 0x7FFFFFFF;
2710 goto roundAndPack;
2712 aSig >>= aExp & 1;
2713 term = ( (uint64_t) zSig ) * zSig;
2714 rem = ( ( (uint64_t) aSig )<<32 ) - term;
2715 while ( (int64_t) rem < 0 ) {
2716 --zSig;
2717 rem += ( ( (uint64_t) zSig )<<1 ) | 1;
2719 zSig |= ( rem != 0 );
2721 shift32RightJamming( zSig, 1, &zSig );
2722 roundAndPack:
2723 return roundAndPackFloat32(0, zExp, zSig, status);
2727 /*----------------------------------------------------------------------------
2728 | Returns the binary exponential of the single-precision floating-point value
2729 | `a'. The operation is performed according to the IEC/IEEE Standard for
2730 | Binary Floating-Point Arithmetic.
2732 | Uses the following identities:
2734 | 1. -------------------------------------------------------------------------
2735 | x x*ln(2)
2736 | 2 = e
2738 | 2. -------------------------------------------------------------------------
2739 | 2 3 4 5 n
2740 | x x x x x x x
2741 | e = 1 + --- + --- + --- + --- + --- + ... + --- + ...
2742 | 1! 2! 3! 4! 5! n!
2743 *----------------------------------------------------------------------------*/
2745 static const float64 float32_exp2_coefficients[15] =
2747 const_float64( 0x3ff0000000000000ll ), /* 1 */
2748 const_float64( 0x3fe0000000000000ll ), /* 2 */
2749 const_float64( 0x3fc5555555555555ll ), /* 3 */
2750 const_float64( 0x3fa5555555555555ll ), /* 4 */
2751 const_float64( 0x3f81111111111111ll ), /* 5 */
2752 const_float64( 0x3f56c16c16c16c17ll ), /* 6 */
2753 const_float64( 0x3f2a01a01a01a01all ), /* 7 */
2754 const_float64( 0x3efa01a01a01a01all ), /* 8 */
2755 const_float64( 0x3ec71de3a556c734ll ), /* 9 */
2756 const_float64( 0x3e927e4fb7789f5cll ), /* 10 */
2757 const_float64( 0x3e5ae64567f544e4ll ), /* 11 */
2758 const_float64( 0x3e21eed8eff8d898ll ), /* 12 */
2759 const_float64( 0x3de6124613a86d09ll ), /* 13 */
2760 const_float64( 0x3da93974a8c07c9dll ), /* 14 */
2761 const_float64( 0x3d6ae7f3e733b81fll ), /* 15 */
2764 float32 float32_exp2(float32 a, float_status *status)
2766 flag aSign;
2767 int aExp;
2768 uint32_t aSig;
2769 float64 r, x, xn;
2770 int i;
2771 a = float32_squash_input_denormal(a, status);
2773 aSig = extractFloat32Frac( a );
2774 aExp = extractFloat32Exp( a );
2775 aSign = extractFloat32Sign( a );
2777 if ( aExp == 0xFF) {
2778 if (aSig) {
2779 return propagateFloat32NaN(a, float32_zero, status);
2781 return (aSign) ? float32_zero : a;
2783 if (aExp == 0) {
2784 if (aSig == 0) return float32_one;
2787 float_raise(float_flag_inexact, status);
2789 /* ******************************* */
2790 /* using float64 for approximation */
2791 /* ******************************* */
2792 x = float32_to_float64(a, status);
2793 x = float64_mul(x, float64_ln2, status);
2795 xn = x;
2796 r = float64_one;
2797 for (i = 0 ; i < 15 ; i++) {
2798 float64 f;
2800 f = float64_mul(xn, float32_exp2_coefficients[i], status);
2801 r = float64_add(r, f, status);
2803 xn = float64_mul(xn, x, status);
2806 return float64_to_float32(r, status);
2809 /*----------------------------------------------------------------------------
2810 | Returns the binary log of the single-precision floating-point value `a'.
2811 | The operation is performed according to the IEC/IEEE Standard for Binary
2812 | Floating-Point Arithmetic.
2813 *----------------------------------------------------------------------------*/
2814 float32 float32_log2(float32 a, float_status *status)
2816 flag aSign, zSign;
2817 int aExp;
2818 uint32_t aSig, zSig, i;
2820 a = float32_squash_input_denormal(a, status);
2821 aSig = extractFloat32Frac( a );
2822 aExp = extractFloat32Exp( a );
2823 aSign = extractFloat32Sign( a );
2825 if ( aExp == 0 ) {
2826 if ( aSig == 0 ) return packFloat32( 1, 0xFF, 0 );
2827 normalizeFloat32Subnormal( aSig, &aExp, &aSig );
2829 if ( aSign ) {
2830 float_raise(float_flag_invalid, status);
2831 return float32_default_nan(status);
2833 if ( aExp == 0xFF ) {
2834 if (aSig) {
2835 return propagateFloat32NaN(a, float32_zero, status);
2837 return a;
2840 aExp -= 0x7F;
2841 aSig |= 0x00800000;
2842 zSign = aExp < 0;
2843 zSig = aExp << 23;
2845 for (i = 1 << 22; i > 0; i >>= 1) {
2846 aSig = ( (uint64_t)aSig * aSig ) >> 23;
2847 if ( aSig & 0x01000000 ) {
2848 aSig >>= 1;
2849 zSig |= i;
2853 if ( zSign )
2854 zSig = -zSig;
2856 return normalizeRoundAndPackFloat32(zSign, 0x85, zSig, status);
2859 /*----------------------------------------------------------------------------
2860 | Returns 1 if the single-precision floating-point value `a' is equal to
2861 | the corresponding value `b', and 0 otherwise. The invalid exception is
2862 | raised if either operand is a NaN. Otherwise, the comparison is performed
2863 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
2864 *----------------------------------------------------------------------------*/
2866 int float32_eq(float32 a, float32 b, float_status *status)
2868 uint32_t av, bv;
2869 a = float32_squash_input_denormal(a, status);
2870 b = float32_squash_input_denormal(b, status);
2872 if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
2873 || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
2875 float_raise(float_flag_invalid, status);
2876 return 0;
2878 av = float32_val(a);
2879 bv = float32_val(b);
2880 return ( av == bv ) || ( (uint32_t) ( ( av | bv )<<1 ) == 0 );
2883 /*----------------------------------------------------------------------------
2884 | Returns 1 if the single-precision floating-point value `a' is less than
2885 | or equal to the corresponding value `b', and 0 otherwise. The invalid
2886 | exception is raised if either operand is a NaN. The comparison is performed
2887 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
2888 *----------------------------------------------------------------------------*/
2890 int float32_le(float32 a, float32 b, float_status *status)
2892 flag aSign, bSign;
2893 uint32_t av, bv;
2894 a = float32_squash_input_denormal(a, status);
2895 b = float32_squash_input_denormal(b, status);
2897 if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
2898 || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
2900 float_raise(float_flag_invalid, status);
2901 return 0;
2903 aSign = extractFloat32Sign( a );
2904 bSign = extractFloat32Sign( b );
2905 av = float32_val(a);
2906 bv = float32_val(b);
2907 if ( aSign != bSign ) return aSign || ( (uint32_t) ( ( av | bv )<<1 ) == 0 );
2908 return ( av == bv ) || ( aSign ^ ( av < bv ) );
2912 /*----------------------------------------------------------------------------
2913 | Returns 1 if the single-precision floating-point value `a' is less than
2914 | the corresponding value `b', and 0 otherwise. The invalid exception is
2915 | raised if either operand is a NaN. The comparison is performed according
2916 | to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
2917 *----------------------------------------------------------------------------*/
2919 int float32_lt(float32 a, float32 b, float_status *status)
2921 flag aSign, bSign;
2922 uint32_t av, bv;
2923 a = float32_squash_input_denormal(a, status);
2924 b = float32_squash_input_denormal(b, status);
2926 if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
2927 || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
2929 float_raise(float_flag_invalid, status);
2930 return 0;
2932 aSign = extractFloat32Sign( a );
2933 bSign = extractFloat32Sign( b );
2934 av = float32_val(a);
2935 bv = float32_val(b);
2936 if ( aSign != bSign ) return aSign && ( (uint32_t) ( ( av | bv )<<1 ) != 0 );
2937 return ( av != bv ) && ( aSign ^ ( av < bv ) );
2941 /*----------------------------------------------------------------------------
2942 | Returns 1 if the single-precision floating-point values `a' and `b' cannot
2943 | be compared, and 0 otherwise. The invalid exception is raised if either
2944 | operand is a NaN. The comparison is performed according to the IEC/IEEE
2945 | Standard for Binary Floating-Point Arithmetic.
2946 *----------------------------------------------------------------------------*/
2948 int float32_unordered(float32 a, float32 b, float_status *status)
2950 a = float32_squash_input_denormal(a, status);
2951 b = float32_squash_input_denormal(b, status);
2953 if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
2954 || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
2956 float_raise(float_flag_invalid, status);
2957 return 1;
2959 return 0;
2962 /*----------------------------------------------------------------------------
2963 | Returns 1 if the single-precision floating-point value `a' is equal to
2964 | the corresponding value `b', and 0 otherwise. Quiet NaNs do not cause an
2965 | exception. The comparison is performed according to the IEC/IEEE Standard
2966 | for Binary Floating-Point Arithmetic.
2967 *----------------------------------------------------------------------------*/
2969 int float32_eq_quiet(float32 a, float32 b, float_status *status)
2971 a = float32_squash_input_denormal(a, status);
2972 b = float32_squash_input_denormal(b, status);
2974 if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
2975 || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
2977 if (float32_is_signaling_nan(a, status)
2978 || float32_is_signaling_nan(b, status)) {
2979 float_raise(float_flag_invalid, status);
2981 return 0;
2983 return ( float32_val(a) == float32_val(b) ) ||
2984 ( (uint32_t) ( ( float32_val(a) | float32_val(b) )<<1 ) == 0 );
2987 /*----------------------------------------------------------------------------
2988 | Returns 1 if the single-precision floating-point value `a' is less than or
2989 | equal to the corresponding value `b', and 0 otherwise. Quiet NaNs do not
2990 | cause an exception. Otherwise, the comparison is performed according to the
2991 | IEC/IEEE Standard for Binary Floating-Point Arithmetic.
2992 *----------------------------------------------------------------------------*/
2994 int float32_le_quiet(float32 a, float32 b, float_status *status)
2996 flag aSign, bSign;
2997 uint32_t av, bv;
2998 a = float32_squash_input_denormal(a, status);
2999 b = float32_squash_input_denormal(b, status);
3001 if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
3002 || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
3004 if (float32_is_signaling_nan(a, status)
3005 || float32_is_signaling_nan(b, status)) {
3006 float_raise(float_flag_invalid, status);
3008 return 0;
3010 aSign = extractFloat32Sign( a );
3011 bSign = extractFloat32Sign( b );
3012 av = float32_val(a);
3013 bv = float32_val(b);
3014 if ( aSign != bSign ) return aSign || ( (uint32_t) ( ( av | bv )<<1 ) == 0 );
3015 return ( av == bv ) || ( aSign ^ ( av < bv ) );
3019 /*----------------------------------------------------------------------------
3020 | Returns 1 if the single-precision floating-point value `a' is less than
3021 | the corresponding value `b', and 0 otherwise. Quiet NaNs do not cause an
3022 | exception. Otherwise, the comparison is performed according to the IEC/IEEE
3023 | Standard for Binary Floating-Point Arithmetic.
3024 *----------------------------------------------------------------------------*/
3026 int float32_lt_quiet(float32 a, float32 b, float_status *status)
3028 flag aSign, bSign;
3029 uint32_t av, bv;
3030 a = float32_squash_input_denormal(a, status);
3031 b = float32_squash_input_denormal(b, status);
3033 if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
3034 || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
3036 if (float32_is_signaling_nan(a, status)
3037 || float32_is_signaling_nan(b, status)) {
3038 float_raise(float_flag_invalid, status);
3040 return 0;
3042 aSign = extractFloat32Sign( a );
3043 bSign = extractFloat32Sign( b );
3044 av = float32_val(a);
3045 bv = float32_val(b);
3046 if ( aSign != bSign ) return aSign && ( (uint32_t) ( ( av | bv )<<1 ) != 0 );
3047 return ( av != bv ) && ( aSign ^ ( av < bv ) );
3051 /*----------------------------------------------------------------------------
3052 | Returns 1 if the single-precision floating-point values `a' and `b' cannot
3053 | be compared, and 0 otherwise. Quiet NaNs do not cause an exception. The
3054 | comparison is performed according to the IEC/IEEE Standard for Binary
3055 | Floating-Point Arithmetic.
3056 *----------------------------------------------------------------------------*/
3058 int float32_unordered_quiet(float32 a, float32 b, float_status *status)
3060 a = float32_squash_input_denormal(a, status);
3061 b = float32_squash_input_denormal(b, status);
3063 if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) )
3064 || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) )
3066 if (float32_is_signaling_nan(a, status)
3067 || float32_is_signaling_nan(b, status)) {
3068 float_raise(float_flag_invalid, status);
3070 return 1;
3072 return 0;
3075 /*----------------------------------------------------------------------------
3076 | Returns the result of converting the double-precision floating-point value
3077 | `a' to the 32-bit two's complement integer format. The conversion is
3078 | performed according to the IEC/IEEE Standard for Binary Floating-Point
3079 | Arithmetic---which means in particular that the conversion is rounded
3080 | according to the current rounding mode. If `a' is a NaN, the largest
3081 | positive integer is returned. Otherwise, if the conversion overflows, the
3082 | largest integer with the same sign as `a' is returned.
3083 *----------------------------------------------------------------------------*/
3085 int32_t float64_to_int32(float64 a, float_status *status)
3087 flag aSign;
3088 int aExp;
3089 int shiftCount;
3090 uint64_t aSig;
3091 a = float64_squash_input_denormal(a, status);
3093 aSig = extractFloat64Frac( a );
3094 aExp = extractFloat64Exp( a );
3095 aSign = extractFloat64Sign( a );
3096 if ( ( aExp == 0x7FF ) && aSig ) aSign = 0;
3097 if ( aExp ) aSig |= LIT64( 0x0010000000000000 );
3098 shiftCount = 0x42C - aExp;
3099 if ( 0 < shiftCount ) shift64RightJamming( aSig, shiftCount, &aSig );
3100 return roundAndPackInt32(aSign, aSig, status);
3104 /*----------------------------------------------------------------------------
3105 | Returns the result of converting the double-precision floating-point value
3106 | `a' to the 32-bit two's complement integer format. The conversion is
3107 | performed according to the IEC/IEEE Standard for Binary Floating-Point
3108 | Arithmetic, except that the conversion is always rounded toward zero.
3109 | If `a' is a NaN, the largest positive integer is returned. Otherwise, if
3110 | the conversion overflows, the largest integer with the same sign as `a' is
3111 | returned.
3112 *----------------------------------------------------------------------------*/
3114 int32_t float64_to_int32_round_to_zero(float64 a, float_status *status)
3116 flag aSign;
3117 int aExp;
3118 int shiftCount;
3119 uint64_t aSig, savedASig;
3120 int32_t z;
3121 a = float64_squash_input_denormal(a, status);
3123 aSig = extractFloat64Frac( a );
3124 aExp = extractFloat64Exp( a );
3125 aSign = extractFloat64Sign( a );
3126 if ( 0x41E < aExp ) {
3127 if ( ( aExp == 0x7FF ) && aSig ) aSign = 0;
3128 goto invalid;
3130 else if ( aExp < 0x3FF ) {
3131 if (aExp || aSig) {
3132 status->float_exception_flags |= float_flag_inexact;
3134 return 0;
3136 aSig |= LIT64( 0x0010000000000000 );
3137 shiftCount = 0x433 - aExp;
3138 savedASig = aSig;
3139 aSig >>= shiftCount;
3140 z = aSig;
3141 if ( aSign ) z = - z;
3142 if ( ( z < 0 ) ^ aSign ) {
3143 invalid:
3144 float_raise(float_flag_invalid, status);
3145 return aSign ? (int32_t) 0x80000000 : 0x7FFFFFFF;
3147 if ( ( aSig<<shiftCount ) != savedASig ) {
3148 status->float_exception_flags |= float_flag_inexact;
3150 return z;
3154 /*----------------------------------------------------------------------------
3155 | Returns the result of converting the double-precision floating-point value
3156 | `a' to the 16-bit two's complement integer format. The conversion is
3157 | performed according to the IEC/IEEE Standard for Binary Floating-Point
3158 | Arithmetic, except that the conversion is always rounded toward zero.
3159 | If `a' is a NaN, the largest positive integer is returned. Otherwise, if
3160 | the conversion overflows, the largest integer with the same sign as `a' is
3161 | returned.
3162 *----------------------------------------------------------------------------*/
3164 int16_t float64_to_int16_round_to_zero(float64 a, float_status *status)
3166 flag aSign;
3167 int aExp;
3168 int shiftCount;
3169 uint64_t aSig, savedASig;
3170 int32_t z;
3172 aSig = extractFloat64Frac( a );
3173 aExp = extractFloat64Exp( a );
3174 aSign = extractFloat64Sign( a );
3175 if ( 0x40E < aExp ) {
3176 if ( ( aExp == 0x7FF ) && aSig ) {
3177 aSign = 0;
3179 goto invalid;
3181 else if ( aExp < 0x3FF ) {
3182 if ( aExp || aSig ) {
3183 status->float_exception_flags |= float_flag_inexact;
3185 return 0;
3187 aSig |= LIT64( 0x0010000000000000 );
3188 shiftCount = 0x433 - aExp;
3189 savedASig = aSig;
3190 aSig >>= shiftCount;
3191 z = aSig;
3192 if ( aSign ) {
3193 z = - z;
3195 if ( ( (int16_t)z < 0 ) ^ aSign ) {
3196 invalid:
3197 float_raise(float_flag_invalid, status);
3198 return aSign ? (int32_t) 0xffff8000 : 0x7FFF;
3200 if ( ( aSig<<shiftCount ) != savedASig ) {
3201 status->float_exception_flags |= float_flag_inexact;
3203 return z;
3206 /*----------------------------------------------------------------------------
3207 | Returns the result of converting the double-precision floating-point value
3208 | `a' to the 64-bit two's complement integer format. The conversion is
3209 | performed according to the IEC/IEEE Standard for Binary Floating-Point
3210 | Arithmetic---which means in particular that the conversion is rounded
3211 | according to the current rounding mode. If `a' is a NaN, the largest
3212 | positive integer is returned. Otherwise, if the conversion overflows, the
3213 | largest integer with the same sign as `a' is returned.
3214 *----------------------------------------------------------------------------*/
3216 int64_t float64_to_int64(float64 a, float_status *status)
3218 flag aSign;
3219 int aExp;
3220 int shiftCount;
3221 uint64_t aSig, aSigExtra;
3222 a = float64_squash_input_denormal(a, status);
3224 aSig = extractFloat64Frac( a );
3225 aExp = extractFloat64Exp( a );
3226 aSign = extractFloat64Sign( a );
3227 if ( aExp ) aSig |= LIT64( 0x0010000000000000 );
3228 shiftCount = 0x433 - aExp;
3229 if ( shiftCount <= 0 ) {
3230 if ( 0x43E < aExp ) {
3231 float_raise(float_flag_invalid, status);
3232 if ( ! aSign
3233 || ( ( aExp == 0x7FF )
3234 && ( aSig != LIT64( 0x0010000000000000 ) ) )
3236 return LIT64( 0x7FFFFFFFFFFFFFFF );
3238 return (int64_t) LIT64( 0x8000000000000000 );
3240 aSigExtra = 0;
3241 aSig <<= - shiftCount;
3243 else {
3244 shift64ExtraRightJamming( aSig, 0, shiftCount, &aSig, &aSigExtra );
3246 return roundAndPackInt64(aSign, aSig, aSigExtra, status);
3250 /*----------------------------------------------------------------------------
3251 | Returns the result of converting the double-precision floating-point value
3252 | `a' to the 64-bit two's complement integer format. The conversion is
3253 | performed according to the IEC/IEEE Standard for Binary Floating-Point
3254 | Arithmetic, except that the conversion is always rounded toward zero.
3255 | If `a' is a NaN, the largest positive integer is returned. Otherwise, if
3256 | the conversion overflows, the largest integer with the same sign as `a' is
3257 | returned.
3258 *----------------------------------------------------------------------------*/
3260 int64_t float64_to_int64_round_to_zero(float64 a, float_status *status)
3262 flag aSign;
3263 int aExp;
3264 int shiftCount;
3265 uint64_t aSig;
3266 int64_t z;
3267 a = float64_squash_input_denormal(a, status);
3269 aSig = extractFloat64Frac( a );
3270 aExp = extractFloat64Exp( a );
3271 aSign = extractFloat64Sign( a );
3272 if ( aExp ) aSig |= LIT64( 0x0010000000000000 );
3273 shiftCount = aExp - 0x433;
3274 if ( 0 <= shiftCount ) {
3275 if ( 0x43E <= aExp ) {
3276 if ( float64_val(a) != LIT64( 0xC3E0000000000000 ) ) {
3277 float_raise(float_flag_invalid, status);
3278 if ( ! aSign
3279 || ( ( aExp == 0x7FF )
3280 && ( aSig != LIT64( 0x0010000000000000 ) ) )
3282 return LIT64( 0x7FFFFFFFFFFFFFFF );
3285 return (int64_t) LIT64( 0x8000000000000000 );
3287 z = aSig<<shiftCount;
3289 else {
3290 if ( aExp < 0x3FE ) {
3291 if (aExp | aSig) {
3292 status->float_exception_flags |= float_flag_inexact;
3294 return 0;
3296 z = aSig>>( - shiftCount );
3297 if ( (uint64_t) ( aSig<<( shiftCount & 63 ) ) ) {
3298 status->float_exception_flags |= float_flag_inexact;
3301 if ( aSign ) z = - z;
3302 return z;
3306 /*----------------------------------------------------------------------------
3307 | Returns the result of converting the double-precision floating-point value
3308 | `a' to the single-precision floating-point format. The conversion is
3309 | performed according to the IEC/IEEE Standard for Binary Floating-Point
3310 | Arithmetic.
3311 *----------------------------------------------------------------------------*/
3313 float32 float64_to_float32(float64 a, float_status *status)
3315 flag aSign;
3316 int aExp;
3317 uint64_t aSig;
3318 uint32_t zSig;
3319 a = float64_squash_input_denormal(a, status);
3321 aSig = extractFloat64Frac( a );
3322 aExp = extractFloat64Exp( a );
3323 aSign = extractFloat64Sign( a );
3324 if ( aExp == 0x7FF ) {
3325 if (aSig) {
3326 return commonNaNToFloat32(float64ToCommonNaN(a, status), status);
3328 return packFloat32( aSign, 0xFF, 0 );
3330 shift64RightJamming( aSig, 22, &aSig );
3331 zSig = aSig;
3332 if ( aExp || zSig ) {
3333 zSig |= 0x40000000;
3334 aExp -= 0x381;
3336 return roundAndPackFloat32(aSign, aExp, zSig, status);
3341 /*----------------------------------------------------------------------------
3342 | Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
3343 | half-precision floating-point value, returning the result. After being
3344 | shifted into the proper positions, the three fields are simply added
3345 | together to form the result. This means that any integer portion of `zSig'
3346 | will be added into the exponent. Since a properly normalized significand
3347 | will have an integer portion equal to 1, the `zExp' input should be 1 less
3348 | than the desired result exponent whenever `zSig' is a complete, normalized
3349 | significand.
3350 *----------------------------------------------------------------------------*/
3351 static float16 packFloat16(flag zSign, int zExp, uint16_t zSig)
3353 return make_float16(
3354 (((uint32_t)zSign) << 15) + (((uint32_t)zExp) << 10) + zSig);
3357 /*----------------------------------------------------------------------------
3358 | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
3359 | and significand `zSig', and returns the proper half-precision floating-
3360 | point value corresponding to the abstract input. Ordinarily, the abstract
3361 | value is simply rounded and packed into the half-precision format, with
3362 | the inexact exception raised if the abstract input cannot be represented
3363 | exactly. However, if the abstract value is too large, the overflow and
3364 | inexact exceptions are raised and an infinity or maximal finite value is
3365 | returned. If the abstract value is too small, the input value is rounded to
3366 | a subnormal number, and the underflow and inexact exceptions are raised if
3367 | the abstract input cannot be represented exactly as a subnormal half-
3368 | precision floating-point number.
3369 | The `ieee' flag indicates whether to use IEEE standard half precision, or
3370 | ARM-style "alternative representation", which omits the NaN and Inf
3371 | encodings in order to raise the maximum representable exponent by one.
3372 | The input significand `zSig' has its binary point between bits 22
3373 | and 23, which is 13 bits to the left of the usual location. This shifted
3374 | significand must be normalized or smaller. If `zSig' is not normalized,
3375 | `zExp' must be 0; in that case, the result returned is a subnormal number,
3376 | and it must not require rounding. In the usual case that `zSig' is
3377 | normalized, `zExp' must be 1 less than the ``true'' floating-point exponent.
3378 | Note the slightly odd position of the binary point in zSig compared with the
3379 | other roundAndPackFloat functions. This should probably be fixed if we
3380 | need to implement more float16 routines than just conversion.
3381 | The handling of underflow and overflow follows the IEC/IEEE Standard for
3382 | Binary Floating-Point Arithmetic.
3383 *----------------------------------------------------------------------------*/
3385 static float16 roundAndPackFloat16(flag zSign, int zExp,
3386 uint32_t zSig, flag ieee,
3387 float_status *status)
3389 int maxexp = ieee ? 29 : 30;
3390 uint32_t mask;
3391 uint32_t increment;
3392 bool rounding_bumps_exp;
3393 bool is_tiny = false;
3395 /* Calculate the mask of bits of the mantissa which are not
3396 * representable in half-precision and will be lost.
3398 if (zExp < 1) {
3399 /* Will be denormal in halfprec */
3400 mask = 0x00ffffff;
3401 if (zExp >= -11) {
3402 mask >>= 11 + zExp;
3404 } else {
3405 /* Normal number in halfprec */
3406 mask = 0x00001fff;
3409 switch (status->float_rounding_mode) {
3410 case float_round_nearest_even:
3411 increment = (mask + 1) >> 1;
3412 if ((zSig & mask) == increment) {
3413 increment = zSig & (increment << 1);
3415 break;
3416 case float_round_ties_away:
3417 increment = (mask + 1) >> 1;
3418 break;
3419 case float_round_up:
3420 increment = zSign ? 0 : mask;
3421 break;
3422 case float_round_down:
3423 increment = zSign ? mask : 0;
3424 break;
3425 default: /* round_to_zero */
3426 increment = 0;
3427 break;
3430 rounding_bumps_exp = (zSig + increment >= 0x01000000);
3432 if (zExp > maxexp || (zExp == maxexp && rounding_bumps_exp)) {
3433 if (ieee) {
3434 float_raise(float_flag_overflow | float_flag_inexact, status);
3435 return packFloat16(zSign, 0x1f, 0);
3436 } else {
3437 float_raise(float_flag_invalid, status);
3438 return packFloat16(zSign, 0x1f, 0x3ff);
3442 if (zExp < 0) {
3443 /* Note that flush-to-zero does not affect half-precision results */
3444 is_tiny =
3445 (status->float_detect_tininess == float_tininess_before_rounding)
3446 || (zExp < -1)
3447 || (!rounding_bumps_exp);
3449 if (zSig & mask) {
3450 float_raise(float_flag_inexact, status);
3451 if (is_tiny) {
3452 float_raise(float_flag_underflow, status);
3456 zSig += increment;
3457 if (rounding_bumps_exp) {
3458 zSig >>= 1;
3459 zExp++;
3462 if (zExp < -10) {
3463 return packFloat16(zSign, 0, 0);
3465 if (zExp < 0) {
3466 zSig >>= -zExp;
3467 zExp = 0;
3469 return packFloat16(zSign, zExp, zSig >> 13);
3472 static void normalizeFloat16Subnormal(uint32_t aSig, int *zExpPtr,
3473 uint32_t *zSigPtr)
3475 int8_t shiftCount = countLeadingZeros32(aSig) - 21;
3476 *zSigPtr = aSig << shiftCount;
3477 *zExpPtr = 1 - shiftCount;
3480 /* Half precision floats come in two formats: standard IEEE and "ARM" format.
3481 The latter gains extra exponent range by omitting the NaN/Inf encodings. */
3483 float32 float16_to_float32(float16 a, flag ieee, float_status *status)
3485 flag aSign;
3486 int aExp;
3487 uint32_t aSig;
3489 aSign = extractFloat16Sign(a);
3490 aExp = extractFloat16Exp(a);
3491 aSig = extractFloat16Frac(a);
3493 if (aExp == 0x1f && ieee) {
3494 if (aSig) {
3495 return commonNaNToFloat32(float16ToCommonNaN(a, status), status);
3497 return packFloat32(aSign, 0xff, 0);
3499 if (aExp == 0) {
3500 if (aSig == 0) {
3501 return packFloat32(aSign, 0, 0);
3504 normalizeFloat16Subnormal(aSig, &aExp, &aSig);
3505 aExp--;
3507 return packFloat32( aSign, aExp + 0x70, aSig << 13);
3510 float16 float32_to_float16(float32 a, flag ieee, float_status *status)
3512 flag aSign;
3513 int aExp;
3514 uint32_t aSig;
3516 a = float32_squash_input_denormal(a, status);
3518 aSig = extractFloat32Frac( a );
3519 aExp = extractFloat32Exp( a );
3520 aSign = extractFloat32Sign( a );
3521 if ( aExp == 0xFF ) {
3522 if (aSig) {
3523 /* Input is a NaN */
3524 if (!ieee) {
3525 float_raise(float_flag_invalid, status);
3526 return packFloat16(aSign, 0, 0);
3528 return commonNaNToFloat16(
3529 float32ToCommonNaN(a, status), status);
3531 /* Infinity */
3532 if (!ieee) {
3533 float_raise(float_flag_invalid, status);
3534 return packFloat16(aSign, 0x1f, 0x3ff);
3536 return packFloat16(aSign, 0x1f, 0);
3538 if (aExp == 0 && aSig == 0) {
3539 return packFloat16(aSign, 0, 0);
3541 /* Decimal point between bits 22 and 23. Note that we add the 1 bit
3542 * even if the input is denormal; however this is harmless because
3543 * the largest possible single-precision denormal is still smaller
3544 * than the smallest representable half-precision denormal, and so we
3545 * will end up ignoring aSig and returning via the "always return zero"
3546 * codepath.
3548 aSig |= 0x00800000;
3549 aExp -= 0x71;
3551 return roundAndPackFloat16(aSign, aExp, aSig, ieee, status);
3554 float64 float16_to_float64(float16 a, flag ieee, float_status *status)
3556 flag aSign;
3557 int aExp;
3558 uint32_t aSig;
3560 aSign = extractFloat16Sign(a);
3561 aExp = extractFloat16Exp(a);
3562 aSig = extractFloat16Frac(a);
3564 if (aExp == 0x1f && ieee) {
3565 if (aSig) {
3566 return commonNaNToFloat64(
3567 float16ToCommonNaN(a, status), status);
3569 return packFloat64(aSign, 0x7ff, 0);
3571 if (aExp == 0) {
3572 if (aSig == 0) {
3573 return packFloat64(aSign, 0, 0);
3576 normalizeFloat16Subnormal(aSig, &aExp, &aSig);
3577 aExp--;
3579 return packFloat64(aSign, aExp + 0x3f0, ((uint64_t)aSig) << 42);
3582 float16 float64_to_float16(float64 a, flag ieee, float_status *status)
3584 flag aSign;
3585 int aExp;
3586 uint64_t aSig;
3587 uint32_t zSig;
3589 a = float64_squash_input_denormal(a, status);
3591 aSig = extractFloat64Frac(a);
3592 aExp = extractFloat64Exp(a);
3593 aSign = extractFloat64Sign(a);
3594 if (aExp == 0x7FF) {
3595 if (aSig) {
3596 /* Input is a NaN */
3597 if (!ieee) {
3598 float_raise(float_flag_invalid, status);
3599 return packFloat16(aSign, 0, 0);
3601 return commonNaNToFloat16(
3602 float64ToCommonNaN(a, status), status);
3604 /* Infinity */
3605 if (!ieee) {
3606 float_raise(float_flag_invalid, status);
3607 return packFloat16(aSign, 0x1f, 0x3ff);
3609 return packFloat16(aSign, 0x1f, 0);
3611 shift64RightJamming(aSig, 29, &aSig);
3612 zSig = aSig;
3613 if (aExp == 0 && zSig == 0) {
3614 return packFloat16(aSign, 0, 0);
3616 /* Decimal point between bits 22 and 23. Note that we add the 1 bit
3617 * even if the input is denormal; however this is harmless because
3618 * the largest possible single-precision denormal is still smaller
3619 * than the smallest representable half-precision denormal, and so we
3620 * will end up ignoring aSig and returning via the "always return zero"
3621 * codepath.
3623 zSig |= 0x00800000;
3624 aExp -= 0x3F1;
3626 return roundAndPackFloat16(aSign, aExp, zSig, ieee, status);
3629 /*----------------------------------------------------------------------------
3630 | Returns the result of converting the double-precision floating-point value
3631 | `a' to the extended double-precision floating-point format. The conversion
3632 | is performed according to the IEC/IEEE Standard for Binary Floating-Point
3633 | Arithmetic.
3634 *----------------------------------------------------------------------------*/
3636 floatx80 float64_to_floatx80(float64 a, float_status *status)
3638 flag aSign;
3639 int aExp;
3640 uint64_t aSig;
3642 a = float64_squash_input_denormal(a, status);
3643 aSig = extractFloat64Frac( a );
3644 aExp = extractFloat64Exp( a );
3645 aSign = extractFloat64Sign( a );
3646 if ( aExp == 0x7FF ) {
3647 if (aSig) {
3648 return commonNaNToFloatx80(float64ToCommonNaN(a, status), status);
3650 return packFloatx80( aSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
3652 if ( aExp == 0 ) {
3653 if ( aSig == 0 ) return packFloatx80( aSign, 0, 0 );
3654 normalizeFloat64Subnormal( aSig, &aExp, &aSig );
3656 return
3657 packFloatx80(
3658 aSign, aExp + 0x3C00, ( aSig | LIT64( 0x0010000000000000 ) )<<11 );
3662 /*----------------------------------------------------------------------------
3663 | Returns the result of converting the double-precision floating-point value
3664 | `a' to the quadruple-precision floating-point format. The conversion is
3665 | performed according to the IEC/IEEE Standard for Binary Floating-Point
3666 | Arithmetic.
3667 *----------------------------------------------------------------------------*/
3669 float128 float64_to_float128(float64 a, float_status *status)
3671 flag aSign;
3672 int aExp;
3673 uint64_t aSig, zSig0, zSig1;
3675 a = float64_squash_input_denormal(a, status);
3676 aSig = extractFloat64Frac( a );
3677 aExp = extractFloat64Exp( a );
3678 aSign = extractFloat64Sign( a );
3679 if ( aExp == 0x7FF ) {
3680 if (aSig) {
3681 return commonNaNToFloat128(float64ToCommonNaN(a, status), status);
3683 return packFloat128( aSign, 0x7FFF, 0, 0 );
3685 if ( aExp == 0 ) {
3686 if ( aSig == 0 ) return packFloat128( aSign, 0, 0, 0 );
3687 normalizeFloat64Subnormal( aSig, &aExp, &aSig );
3688 --aExp;
3690 shift128Right( aSig, 0, 4, &zSig0, &zSig1 );
3691 return packFloat128( aSign, aExp + 0x3C00, zSig0, zSig1 );
3695 /*----------------------------------------------------------------------------
3696 | Rounds the double-precision floating-point value `a' to an integer, and
3697 | returns the result as a double-precision floating-point value. The
3698 | operation is performed according to the IEC/IEEE Standard for Binary
3699 | Floating-Point Arithmetic.
3700 *----------------------------------------------------------------------------*/
3702 float64 float64_round_to_int(float64 a, float_status *status)
3704 flag aSign;
3705 int aExp;
3706 uint64_t lastBitMask, roundBitsMask;
3707 uint64_t z;
3708 a = float64_squash_input_denormal(a, status);
3710 aExp = extractFloat64Exp( a );
3711 if ( 0x433 <= aExp ) {
3712 if ( ( aExp == 0x7FF ) && extractFloat64Frac( a ) ) {
3713 return propagateFloat64NaN(a, a, status);
3715 return a;
3717 if ( aExp < 0x3FF ) {
3718 if ( (uint64_t) ( float64_val(a)<<1 ) == 0 ) return a;
3719 status->float_exception_flags |= float_flag_inexact;
3720 aSign = extractFloat64Sign( a );
3721 switch (status->float_rounding_mode) {
3722 case float_round_nearest_even:
3723 if ( ( aExp == 0x3FE ) && extractFloat64Frac( a ) ) {
3724 return packFloat64( aSign, 0x3FF, 0 );
3726 break;
3727 case float_round_ties_away:
3728 if (aExp == 0x3FE) {
3729 return packFloat64(aSign, 0x3ff, 0);
3731 break;
3732 case float_round_down:
3733 return make_float64(aSign ? LIT64( 0xBFF0000000000000 ) : 0);
3734 case float_round_up:
3735 return make_float64(
3736 aSign ? LIT64( 0x8000000000000000 ) : LIT64( 0x3FF0000000000000 ));
3738 return packFloat64( aSign, 0, 0 );
3740 lastBitMask = 1;
3741 lastBitMask <<= 0x433 - aExp;
3742 roundBitsMask = lastBitMask - 1;
3743 z = float64_val(a);
3744 switch (status->float_rounding_mode) {
3745 case float_round_nearest_even:
3746 z += lastBitMask >> 1;
3747 if ((z & roundBitsMask) == 0) {
3748 z &= ~lastBitMask;
3750 break;
3751 case float_round_ties_away:
3752 z += lastBitMask >> 1;
3753 break;
3754 case float_round_to_zero:
3755 break;
3756 case float_round_up:
3757 if (!extractFloat64Sign(make_float64(z))) {
3758 z += roundBitsMask;
3760 break;
3761 case float_round_down:
3762 if (extractFloat64Sign(make_float64(z))) {
3763 z += roundBitsMask;
3765 break;
3766 default:
3767 abort();
3769 z &= ~ roundBitsMask;
3770 if (z != float64_val(a)) {
3771 status->float_exception_flags |= float_flag_inexact;
3773 return make_float64(z);
3777 float64 float64_trunc_to_int(float64 a, float_status *status)
3779 int oldmode;
3780 float64 res;
3781 oldmode = status->float_rounding_mode;
3782 status->float_rounding_mode = float_round_to_zero;
3783 res = float64_round_to_int(a, status);
3784 status->float_rounding_mode = oldmode;
3785 return res;
3788 /*----------------------------------------------------------------------------
3789 | Returns the result of adding the absolute values of the double-precision
3790 | floating-point values `a' and `b'. If `zSign' is 1, the sum is negated
3791 | before being returned. `zSign' is ignored if the result is a NaN.
3792 | The addition is performed according to the IEC/IEEE Standard for Binary
3793 | Floating-Point Arithmetic.
3794 *----------------------------------------------------------------------------*/
3796 static float64 addFloat64Sigs(float64 a, float64 b, flag zSign,
3797 float_status *status)
3799 int aExp, bExp, zExp;
3800 uint64_t aSig, bSig, zSig;
3801 int expDiff;
3803 aSig = extractFloat64Frac( a );
3804 aExp = extractFloat64Exp( a );
3805 bSig = extractFloat64Frac( b );
3806 bExp = extractFloat64Exp( b );
3807 expDiff = aExp - bExp;
3808 aSig <<= 9;
3809 bSig <<= 9;
3810 if ( 0 < expDiff ) {
3811 if ( aExp == 0x7FF ) {
3812 if (aSig) {
3813 return propagateFloat64NaN(a, b, status);
3815 return a;
3817 if ( bExp == 0 ) {
3818 --expDiff;
3820 else {
3821 bSig |= LIT64( 0x2000000000000000 );
3823 shift64RightJamming( bSig, expDiff, &bSig );
3824 zExp = aExp;
3826 else if ( expDiff < 0 ) {
3827 if ( bExp == 0x7FF ) {
3828 if (bSig) {
3829 return propagateFloat64NaN(a, b, status);
3831 return packFloat64( zSign, 0x7FF, 0 );
3833 if ( aExp == 0 ) {
3834 ++expDiff;
3836 else {
3837 aSig |= LIT64( 0x2000000000000000 );
3839 shift64RightJamming( aSig, - expDiff, &aSig );
3840 zExp = bExp;
3842 else {
3843 if ( aExp == 0x7FF ) {
3844 if (aSig | bSig) {
3845 return propagateFloat64NaN(a, b, status);
3847 return a;
3849 if ( aExp == 0 ) {
3850 if (status->flush_to_zero) {
3851 if (aSig | bSig) {
3852 float_raise(float_flag_output_denormal, status);
3854 return packFloat64(zSign, 0, 0);
3856 return packFloat64( zSign, 0, ( aSig + bSig )>>9 );
3858 zSig = LIT64( 0x4000000000000000 ) + aSig + bSig;
3859 zExp = aExp;
3860 goto roundAndPack;
3862 aSig |= LIT64( 0x2000000000000000 );
3863 zSig = ( aSig + bSig )<<1;
3864 --zExp;
3865 if ( (int64_t) zSig < 0 ) {
3866 zSig = aSig + bSig;
3867 ++zExp;
3869 roundAndPack:
3870 return roundAndPackFloat64(zSign, zExp, zSig, status);
3874 /*----------------------------------------------------------------------------
3875 | Returns the result of subtracting the absolute values of the double-
3876 | precision floating-point values `a' and `b'. If `zSign' is 1, the
3877 | difference is negated before being returned. `zSign' is ignored if the
3878 | result is a NaN. The subtraction is performed according to the IEC/IEEE
3879 | Standard for Binary Floating-Point Arithmetic.
3880 *----------------------------------------------------------------------------*/
3882 static float64 subFloat64Sigs(float64 a, float64 b, flag zSign,
3883 float_status *status)
3885 int aExp, bExp, zExp;
3886 uint64_t aSig, bSig, zSig;
3887 int expDiff;
3889 aSig = extractFloat64Frac( a );
3890 aExp = extractFloat64Exp( a );
3891 bSig = extractFloat64Frac( b );
3892 bExp = extractFloat64Exp( b );
3893 expDiff = aExp - bExp;
3894 aSig <<= 10;
3895 bSig <<= 10;
3896 if ( 0 < expDiff ) goto aExpBigger;
3897 if ( expDiff < 0 ) goto bExpBigger;
3898 if ( aExp == 0x7FF ) {
3899 if (aSig | bSig) {
3900 return propagateFloat64NaN(a, b, status);
3902 float_raise(float_flag_invalid, status);
3903 return float64_default_nan(status);
3905 if ( aExp == 0 ) {
3906 aExp = 1;
3907 bExp = 1;
3909 if ( bSig < aSig ) goto aBigger;
3910 if ( aSig < bSig ) goto bBigger;
3911 return packFloat64(status->float_rounding_mode == float_round_down, 0, 0);
3912 bExpBigger:
3913 if ( bExp == 0x7FF ) {
3914 if (bSig) {
3915 return propagateFloat64NaN(a, b, status);
3917 return packFloat64( zSign ^ 1, 0x7FF, 0 );
3919 if ( aExp == 0 ) {
3920 ++expDiff;
3922 else {
3923 aSig |= LIT64( 0x4000000000000000 );
3925 shift64RightJamming( aSig, - expDiff, &aSig );
3926 bSig |= LIT64( 0x4000000000000000 );
3927 bBigger:
3928 zSig = bSig - aSig;
3929 zExp = bExp;
3930 zSign ^= 1;
3931 goto normalizeRoundAndPack;
3932 aExpBigger:
3933 if ( aExp == 0x7FF ) {
3934 if (aSig) {
3935 return propagateFloat64NaN(a, b, status);
3937 return a;
3939 if ( bExp == 0 ) {
3940 --expDiff;
3942 else {
3943 bSig |= LIT64( 0x4000000000000000 );
3945 shift64RightJamming( bSig, expDiff, &bSig );
3946 aSig |= LIT64( 0x4000000000000000 );
3947 aBigger:
3948 zSig = aSig - bSig;
3949 zExp = aExp;
3950 normalizeRoundAndPack:
3951 --zExp;
3952 return normalizeRoundAndPackFloat64(zSign, zExp, zSig, status);
3956 /*----------------------------------------------------------------------------
3957 | Returns the result of adding the double-precision floating-point values `a'
3958 | and `b'. The operation is performed according to the IEC/IEEE Standard for
3959 | Binary Floating-Point Arithmetic.
3960 *----------------------------------------------------------------------------*/
3962 float64 float64_add(float64 a, float64 b, float_status *status)
3964 flag aSign, bSign;
3965 a = float64_squash_input_denormal(a, status);
3966 b = float64_squash_input_denormal(b, status);
3968 aSign = extractFloat64Sign( a );
3969 bSign = extractFloat64Sign( b );
3970 if ( aSign == bSign ) {
3971 return addFloat64Sigs(a, b, aSign, status);
3973 else {
3974 return subFloat64Sigs(a, b, aSign, status);
3979 /*----------------------------------------------------------------------------
3980 | Returns the result of subtracting the double-precision floating-point values
3981 | `a' and `b'. The operation is performed according to the IEC/IEEE Standard
3982 | for Binary Floating-Point Arithmetic.
3983 *----------------------------------------------------------------------------*/
3985 float64 float64_sub(float64 a, float64 b, float_status *status)
3987 flag aSign, bSign;
3988 a = float64_squash_input_denormal(a, status);
3989 b = float64_squash_input_denormal(b, status);
3991 aSign = extractFloat64Sign( a );
3992 bSign = extractFloat64Sign( b );
3993 if ( aSign == bSign ) {
3994 return subFloat64Sigs(a, b, aSign, status);
3996 else {
3997 return addFloat64Sigs(a, b, aSign, status);
4002 /*----------------------------------------------------------------------------
4003 | Returns the result of multiplying the double-precision floating-point values
4004 | `a' and `b'. The operation is performed according to the IEC/IEEE Standard
4005 | for Binary Floating-Point Arithmetic.
4006 *----------------------------------------------------------------------------*/
4008 float64 float64_mul(float64 a, float64 b, float_status *status)
4010 flag aSign, bSign, zSign;
4011 int aExp, bExp, zExp;
4012 uint64_t aSig, bSig, zSig0, zSig1;
4014 a = float64_squash_input_denormal(a, status);
4015 b = float64_squash_input_denormal(b, status);
4017 aSig = extractFloat64Frac( a );
4018 aExp = extractFloat64Exp( a );
4019 aSign = extractFloat64Sign( a );
4020 bSig = extractFloat64Frac( b );
4021 bExp = extractFloat64Exp( b );
4022 bSign = extractFloat64Sign( b );
4023 zSign = aSign ^ bSign;
4024 if ( aExp == 0x7FF ) {
4025 if ( aSig || ( ( bExp == 0x7FF ) && bSig ) ) {
4026 return propagateFloat64NaN(a, b, status);
4028 if ( ( bExp | bSig ) == 0 ) {
4029 float_raise(float_flag_invalid, status);
4030 return float64_default_nan(status);
4032 return packFloat64( zSign, 0x7FF, 0 );
4034 if ( bExp == 0x7FF ) {
4035 if (bSig) {
4036 return propagateFloat64NaN(a, b, status);
4038 if ( ( aExp | aSig ) == 0 ) {
4039 float_raise(float_flag_invalid, status);
4040 return float64_default_nan(status);
4042 return packFloat64( zSign, 0x7FF, 0 );
4044 if ( aExp == 0 ) {
4045 if ( aSig == 0 ) return packFloat64( zSign, 0, 0 );
4046 normalizeFloat64Subnormal( aSig, &aExp, &aSig );
4048 if ( bExp == 0 ) {
4049 if ( bSig == 0 ) return packFloat64( zSign, 0, 0 );
4050 normalizeFloat64Subnormal( bSig, &bExp, &bSig );
4052 zExp = aExp + bExp - 0x3FF;
4053 aSig = ( aSig | LIT64( 0x0010000000000000 ) )<<10;
4054 bSig = ( bSig | LIT64( 0x0010000000000000 ) )<<11;
4055 mul64To128( aSig, bSig, &zSig0, &zSig1 );
4056 zSig0 |= ( zSig1 != 0 );
4057 if ( 0 <= (int64_t) ( zSig0<<1 ) ) {
4058 zSig0 <<= 1;
4059 --zExp;
4061 return roundAndPackFloat64(zSign, zExp, zSig0, status);
4065 /*----------------------------------------------------------------------------
4066 | Returns the result of dividing the double-precision floating-point value `a'
4067 | by the corresponding value `b'. The operation is performed according to
4068 | the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
4069 *----------------------------------------------------------------------------*/
4071 float64 float64_div(float64 a, float64 b, float_status *status)
4073 flag aSign, bSign, zSign;
4074 int aExp, bExp, zExp;
4075 uint64_t aSig, bSig, zSig;
4076 uint64_t rem0, rem1;
4077 uint64_t term0, term1;
4078 a = float64_squash_input_denormal(a, status);
4079 b = float64_squash_input_denormal(b, status);
4081 aSig = extractFloat64Frac( a );
4082 aExp = extractFloat64Exp( a );
4083 aSign = extractFloat64Sign( a );
4084 bSig = extractFloat64Frac( b );
4085 bExp = extractFloat64Exp( b );
4086 bSign = extractFloat64Sign( b );
4087 zSign = aSign ^ bSign;
4088 if ( aExp == 0x7FF ) {
4089 if (aSig) {
4090 return propagateFloat64NaN(a, b, status);
4092 if ( bExp == 0x7FF ) {
4093 if (bSig) {
4094 return propagateFloat64NaN(a, b, status);
4096 float_raise(float_flag_invalid, status);
4097 return float64_default_nan(status);
4099 return packFloat64( zSign, 0x7FF, 0 );
4101 if ( bExp == 0x7FF ) {
4102 if (bSig) {
4103 return propagateFloat64NaN(a, b, status);
4105 return packFloat64( zSign, 0, 0 );
4107 if ( bExp == 0 ) {
4108 if ( bSig == 0 ) {
4109 if ( ( aExp | aSig ) == 0 ) {
4110 float_raise(float_flag_invalid, status);
4111 return float64_default_nan(status);
4113 float_raise(float_flag_divbyzero, status);
4114 return packFloat64( zSign, 0x7FF, 0 );
4116 normalizeFloat64Subnormal( bSig, &bExp, &bSig );
4118 if ( aExp == 0 ) {
4119 if ( aSig == 0 ) return packFloat64( zSign, 0, 0 );
4120 normalizeFloat64Subnormal( aSig, &aExp, &aSig );
4122 zExp = aExp - bExp + 0x3FD;
4123 aSig = ( aSig | LIT64( 0x0010000000000000 ) )<<10;
4124 bSig = ( bSig | LIT64( 0x0010000000000000 ) )<<11;
4125 if ( bSig <= ( aSig + aSig ) ) {
4126 aSig >>= 1;
4127 ++zExp;
4129 zSig = estimateDiv128To64( aSig, 0, bSig );
4130 if ( ( zSig & 0x1FF ) <= 2 ) {
4131 mul64To128( bSig, zSig, &term0, &term1 );
4132 sub128( aSig, 0, term0, term1, &rem0, &rem1 );
4133 while ( (int64_t) rem0 < 0 ) {
4134 --zSig;
4135 add128( rem0, rem1, 0, bSig, &rem0, &rem1 );
4137 zSig |= ( rem1 != 0 );
4139 return roundAndPackFloat64(zSign, zExp, zSig, status);
4143 /*----------------------------------------------------------------------------
4144 | Returns the remainder of the double-precision floating-point value `a'
4145 | with respect to the corresponding value `b'. The operation is performed
4146 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
4147 *----------------------------------------------------------------------------*/
4149 float64 float64_rem(float64 a, float64 b, float_status *status)
4151 flag aSign, zSign;
4152 int aExp, bExp, expDiff;
4153 uint64_t aSig, bSig;
4154 uint64_t q, alternateASig;
4155 int64_t sigMean;
4157 a = float64_squash_input_denormal(a, status);
4158 b = float64_squash_input_denormal(b, status);
4159 aSig = extractFloat64Frac( a );
4160 aExp = extractFloat64Exp( a );
4161 aSign = extractFloat64Sign( a );
4162 bSig = extractFloat64Frac( b );
4163 bExp = extractFloat64Exp( b );
4164 if ( aExp == 0x7FF ) {
4165 if ( aSig || ( ( bExp == 0x7FF ) && bSig ) ) {
4166 return propagateFloat64NaN(a, b, status);
4168 float_raise(float_flag_invalid, status);
4169 return float64_default_nan(status);
4171 if ( bExp == 0x7FF ) {
4172 if (bSig) {
4173 return propagateFloat64NaN(a, b, status);
4175 return a;
4177 if ( bExp == 0 ) {
4178 if ( bSig == 0 ) {
4179 float_raise(float_flag_invalid, status);
4180 return float64_default_nan(status);
4182 normalizeFloat64Subnormal( bSig, &bExp, &bSig );
4184 if ( aExp == 0 ) {
4185 if ( aSig == 0 ) return a;
4186 normalizeFloat64Subnormal( aSig, &aExp, &aSig );
4188 expDiff = aExp - bExp;
4189 aSig = ( aSig | LIT64( 0x0010000000000000 ) )<<11;
4190 bSig = ( bSig | LIT64( 0x0010000000000000 ) )<<11;
4191 if ( expDiff < 0 ) {
4192 if ( expDiff < -1 ) return a;
4193 aSig >>= 1;
4195 q = ( bSig <= aSig );
4196 if ( q ) aSig -= bSig;
4197 expDiff -= 64;
4198 while ( 0 < expDiff ) {
4199 q = estimateDiv128To64( aSig, 0, bSig );
4200 q = ( 2 < q ) ? q - 2 : 0;
4201 aSig = - ( ( bSig>>2 ) * q );
4202 expDiff -= 62;
4204 expDiff += 64;
4205 if ( 0 < expDiff ) {
4206 q = estimateDiv128To64( aSig, 0, bSig );
4207 q = ( 2 < q ) ? q - 2 : 0;
4208 q >>= 64 - expDiff;
4209 bSig >>= 2;
4210 aSig = ( ( aSig>>1 )<<( expDiff - 1 ) ) - bSig * q;
4212 else {
4213 aSig >>= 2;
4214 bSig >>= 2;
4216 do {
4217 alternateASig = aSig;
4218 ++q;
4219 aSig -= bSig;
4220 } while ( 0 <= (int64_t) aSig );
4221 sigMean = aSig + alternateASig;
4222 if ( ( sigMean < 0 ) || ( ( sigMean == 0 ) && ( q & 1 ) ) ) {
4223 aSig = alternateASig;
4225 zSign = ( (int64_t) aSig < 0 );
4226 if ( zSign ) aSig = - aSig;
4227 return normalizeRoundAndPackFloat64(aSign ^ zSign, bExp, aSig, status);
4231 /*----------------------------------------------------------------------------
4232 | Returns the result of multiplying the double-precision floating-point values
4233 | `a' and `b' then adding 'c', with no intermediate rounding step after the
4234 | multiplication. The operation is performed according to the IEC/IEEE
4235 | Standard for Binary Floating-Point Arithmetic 754-2008.
4236 | The flags argument allows the caller to select negation of the
4237 | addend, the intermediate product, or the final result. (The difference
4238 | between this and having the caller do a separate negation is that negating
4239 | externally will flip the sign bit on NaNs.)
4240 *----------------------------------------------------------------------------*/
4242 float64 float64_muladd(float64 a, float64 b, float64 c, int flags,
4243 float_status *status)
4245 flag aSign, bSign, cSign, zSign;
4246 int aExp, bExp, cExp, pExp, zExp, expDiff;
4247 uint64_t aSig, bSig, cSig;
4248 flag pInf, pZero, pSign;
4249 uint64_t pSig0, pSig1, cSig0, cSig1, zSig0, zSig1;
4250 int shiftcount;
4251 flag signflip, infzero;
4253 a = float64_squash_input_denormal(a, status);
4254 b = float64_squash_input_denormal(b, status);
4255 c = float64_squash_input_denormal(c, status);
4256 aSig = extractFloat64Frac(a);
4257 aExp = extractFloat64Exp(a);
4258 aSign = extractFloat64Sign(a);
4259 bSig = extractFloat64Frac(b);
4260 bExp = extractFloat64Exp(b);
4261 bSign = extractFloat64Sign(b);
4262 cSig = extractFloat64Frac(c);
4263 cExp = extractFloat64Exp(c);
4264 cSign = extractFloat64Sign(c);
4266 infzero = ((aExp == 0 && aSig == 0 && bExp == 0x7ff && bSig == 0) ||
4267 (aExp == 0x7ff && aSig == 0 && bExp == 0 && bSig == 0));
4269 /* It is implementation-defined whether the cases of (0,inf,qnan)
4270 * and (inf,0,qnan) raise InvalidOperation or not (and what QNaN
4271 * they return if they do), so we have to hand this information
4272 * off to the target-specific pick-a-NaN routine.
4274 if (((aExp == 0x7ff) && aSig) ||
4275 ((bExp == 0x7ff) && bSig) ||
4276 ((cExp == 0x7ff) && cSig)) {
4277 return propagateFloat64MulAddNaN(a, b, c, infzero, status);
4280 if (infzero) {
4281 float_raise(float_flag_invalid, status);
4282 return float64_default_nan(status);
4285 if (flags & float_muladd_negate_c) {
4286 cSign ^= 1;
4289 signflip = (flags & float_muladd_negate_result) ? 1 : 0;
4291 /* Work out the sign and type of the product */
4292 pSign = aSign ^ bSign;
4293 if (flags & float_muladd_negate_product) {
4294 pSign ^= 1;
4296 pInf = (aExp == 0x7ff) || (bExp == 0x7ff);
4297 pZero = ((aExp | aSig) == 0) || ((bExp | bSig) == 0);
4299 if (cExp == 0x7ff) {
4300 if (pInf && (pSign ^ cSign)) {
4301 /* addition of opposite-signed infinities => InvalidOperation */
4302 float_raise(float_flag_invalid, status);
4303 return float64_default_nan(status);
4305 /* Otherwise generate an infinity of the same sign */
4306 return packFloat64(cSign ^ signflip, 0x7ff, 0);
4309 if (pInf) {
4310 return packFloat64(pSign ^ signflip, 0x7ff, 0);
4313 if (pZero) {
4314 if (cExp == 0) {
4315 if (cSig == 0) {
4316 /* Adding two exact zeroes */
4317 if (pSign == cSign) {
4318 zSign = pSign;
4319 } else if (status->float_rounding_mode == float_round_down) {
4320 zSign = 1;
4321 } else {
4322 zSign = 0;
4324 return packFloat64(zSign ^ signflip, 0, 0);
4326 /* Exact zero plus a denorm */
4327 if (status->flush_to_zero) {
4328 float_raise(float_flag_output_denormal, status);
4329 return packFloat64(cSign ^ signflip, 0, 0);
4332 /* Zero plus something non-zero : just return the something */
4333 if (flags & float_muladd_halve_result) {
4334 if (cExp == 0) {
4335 normalizeFloat64Subnormal(cSig, &cExp, &cSig);
4337 /* Subtract one to halve, and one again because roundAndPackFloat64
4338 * wants one less than the true exponent.
4340 cExp -= 2;
4341 cSig = (cSig | 0x0010000000000000ULL) << 10;
4342 return roundAndPackFloat64(cSign ^ signflip, cExp, cSig, status);
4344 return packFloat64(cSign ^ signflip, cExp, cSig);
4347 if (aExp == 0) {
4348 normalizeFloat64Subnormal(aSig, &aExp, &aSig);
4350 if (bExp == 0) {
4351 normalizeFloat64Subnormal(bSig, &bExp, &bSig);
4354 /* Calculate the actual result a * b + c */
4356 /* Multiply first; this is easy. */
4357 /* NB: we subtract 0x3fe where float64_mul() subtracts 0x3ff
4358 * because we want the true exponent, not the "one-less-than"
4359 * flavour that roundAndPackFloat64() takes.
4361 pExp = aExp + bExp - 0x3fe;
4362 aSig = (aSig | LIT64(0x0010000000000000))<<10;
4363 bSig = (bSig | LIT64(0x0010000000000000))<<11;
4364 mul64To128(aSig, bSig, &pSig0, &pSig1);
4365 if ((int64_t)(pSig0 << 1) >= 0) {
4366 shortShift128Left(pSig0, pSig1, 1, &pSig0, &pSig1);
4367 pExp--;
4370 zSign = pSign ^ signflip;
4372 /* Now [pSig0:pSig1] is the significand of the multiply, with the explicit
4373 * bit in position 126.
4375 if (cExp == 0) {
4376 if (!cSig) {
4377 /* Throw out the special case of c being an exact zero now */
4378 shift128RightJamming(pSig0, pSig1, 64, &pSig0, &pSig1);
4379 if (flags & float_muladd_halve_result) {
4380 pExp--;
4382 return roundAndPackFloat64(zSign, pExp - 1,
4383 pSig1, status);
4385 normalizeFloat64Subnormal(cSig, &cExp, &cSig);
4388 /* Shift cSig and add the explicit bit so [cSig0:cSig1] is the
4389 * significand of the addend, with the explicit bit in position 126.
4391 cSig0 = cSig << (126 - 64 - 52);
4392 cSig1 = 0;
4393 cSig0 |= LIT64(0x4000000000000000);
4394 expDiff = pExp - cExp;
4396 if (pSign == cSign) {
4397 /* Addition */
4398 if (expDiff > 0) {
4399 /* scale c to match p */
4400 shift128RightJamming(cSig0, cSig1, expDiff, &cSig0, &cSig1);
4401 zExp = pExp;
4402 } else if (expDiff < 0) {
4403 /* scale p to match c */
4404 shift128RightJamming(pSig0, pSig1, -expDiff, &pSig0, &pSig1);
4405 zExp = cExp;
4406 } else {
4407 /* no scaling needed */
4408 zExp = cExp;
4410 /* Add significands and make sure explicit bit ends up in posn 126 */
4411 add128(pSig0, pSig1, cSig0, cSig1, &zSig0, &zSig1);
4412 if ((int64_t)zSig0 < 0) {
4413 shift128RightJamming(zSig0, zSig1, 1, &zSig0, &zSig1);
4414 } else {
4415 zExp--;
4417 shift128RightJamming(zSig0, zSig1, 64, &zSig0, &zSig1);
4418 if (flags & float_muladd_halve_result) {
4419 zExp--;
4421 return roundAndPackFloat64(zSign, zExp, zSig1, status);
4422 } else {
4423 /* Subtraction */
4424 if (expDiff > 0) {
4425 shift128RightJamming(cSig0, cSig1, expDiff, &cSig0, &cSig1);
4426 sub128(pSig0, pSig1, cSig0, cSig1, &zSig0, &zSig1);
4427 zExp = pExp;
4428 } else if (expDiff < 0) {
4429 shift128RightJamming(pSig0, pSig1, -expDiff, &pSig0, &pSig1);
4430 sub128(cSig0, cSig1, pSig0, pSig1, &zSig0, &zSig1);
4431 zExp = cExp;
4432 zSign ^= 1;
4433 } else {
4434 zExp = pExp;
4435 if (lt128(cSig0, cSig1, pSig0, pSig1)) {
4436 sub128(pSig0, pSig1, cSig0, cSig1, &zSig0, &zSig1);
4437 } else if (lt128(pSig0, pSig1, cSig0, cSig1)) {
4438 sub128(cSig0, cSig1, pSig0, pSig1, &zSig0, &zSig1);
4439 zSign ^= 1;
4440 } else {
4441 /* Exact zero */
4442 zSign = signflip;
4443 if (status->float_rounding_mode == float_round_down) {
4444 zSign ^= 1;
4446 return packFloat64(zSign, 0, 0);
4449 --zExp;
4450 /* Do the equivalent of normalizeRoundAndPackFloat64() but
4451 * starting with the significand in a pair of uint64_t.
4453 if (zSig0) {
4454 shiftcount = countLeadingZeros64(zSig0) - 1;
4455 shortShift128Left(zSig0, zSig1, shiftcount, &zSig0, &zSig1);
4456 if (zSig1) {
4457 zSig0 |= 1;
4459 zExp -= shiftcount;
4460 } else {
4461 shiftcount = countLeadingZeros64(zSig1);
4462 if (shiftcount == 0) {
4463 zSig0 = (zSig1 >> 1) | (zSig1 & 1);
4464 zExp -= 63;
4465 } else {
4466 shiftcount--;
4467 zSig0 = zSig1 << shiftcount;
4468 zExp -= (shiftcount + 64);
4471 if (flags & float_muladd_halve_result) {
4472 zExp--;
4474 return roundAndPackFloat64(zSign, zExp, zSig0, status);
4478 /*----------------------------------------------------------------------------
4479 | Returns the square root of the double-precision floating-point value `a'.
4480 | The operation is performed according to the IEC/IEEE Standard for Binary
4481 | Floating-Point Arithmetic.
4482 *----------------------------------------------------------------------------*/
4484 float64 float64_sqrt(float64 a, float_status *status)
4486 flag aSign;
4487 int aExp, zExp;
4488 uint64_t aSig, zSig, doubleZSig;
4489 uint64_t rem0, rem1, term0, term1;
4490 a = float64_squash_input_denormal(a, status);
4492 aSig = extractFloat64Frac( a );
4493 aExp = extractFloat64Exp( a );
4494 aSign = extractFloat64Sign( a );
4495 if ( aExp == 0x7FF ) {
4496 if (aSig) {
4497 return propagateFloat64NaN(a, a, status);
4499 if ( ! aSign ) return a;
4500 float_raise(float_flag_invalid, status);
4501 return float64_default_nan(status);
4503 if ( aSign ) {
4504 if ( ( aExp | aSig ) == 0 ) return a;
4505 float_raise(float_flag_invalid, status);
4506 return float64_default_nan(status);
4508 if ( aExp == 0 ) {
4509 if ( aSig == 0 ) return float64_zero;
4510 normalizeFloat64Subnormal( aSig, &aExp, &aSig );
4512 zExp = ( ( aExp - 0x3FF )>>1 ) + 0x3FE;
4513 aSig |= LIT64( 0x0010000000000000 );
4514 zSig = estimateSqrt32( aExp, aSig>>21 );
4515 aSig <<= 9 - ( aExp & 1 );
4516 zSig = estimateDiv128To64( aSig, 0, zSig<<32 ) + ( zSig<<30 );
4517 if ( ( zSig & 0x1FF ) <= 5 ) {
4518 doubleZSig = zSig<<1;
4519 mul64To128( zSig, zSig, &term0, &term1 );
4520 sub128( aSig, 0, term0, term1, &rem0, &rem1 );
4521 while ( (int64_t) rem0 < 0 ) {
4522 --zSig;
4523 doubleZSig -= 2;
4524 add128( rem0, rem1, zSig>>63, doubleZSig | 1, &rem0, &rem1 );
4526 zSig |= ( ( rem0 | rem1 ) != 0 );
4528 return roundAndPackFloat64(0, zExp, zSig, status);
4532 /*----------------------------------------------------------------------------
4533 | Returns the binary log of the double-precision floating-point value `a'.
4534 | The operation is performed according to the IEC/IEEE Standard for Binary
4535 | Floating-Point Arithmetic.
4536 *----------------------------------------------------------------------------*/
4537 float64 float64_log2(float64 a, float_status *status)
4539 flag aSign, zSign;
4540 int aExp;
4541 uint64_t aSig, aSig0, aSig1, zSig, i;
4542 a = float64_squash_input_denormal(a, status);
4544 aSig = extractFloat64Frac( a );
4545 aExp = extractFloat64Exp( a );
4546 aSign = extractFloat64Sign( a );
4548 if ( aExp == 0 ) {
4549 if ( aSig == 0 ) return packFloat64( 1, 0x7FF, 0 );
4550 normalizeFloat64Subnormal( aSig, &aExp, &aSig );
4552 if ( aSign ) {
4553 float_raise(float_flag_invalid, status);
4554 return float64_default_nan(status);
4556 if ( aExp == 0x7FF ) {
4557 if (aSig) {
4558 return propagateFloat64NaN(a, float64_zero, status);
4560 return a;
4563 aExp -= 0x3FF;
4564 aSig |= LIT64( 0x0010000000000000 );
4565 zSign = aExp < 0;
4566 zSig = (uint64_t)aExp << 52;
4567 for (i = 1LL << 51; i > 0; i >>= 1) {
4568 mul64To128( aSig, aSig, &aSig0, &aSig1 );
4569 aSig = ( aSig0 << 12 ) | ( aSig1 >> 52 );
4570 if ( aSig & LIT64( 0x0020000000000000 ) ) {
4571 aSig >>= 1;
4572 zSig |= i;
4576 if ( zSign )
4577 zSig = -zSig;
4578 return normalizeRoundAndPackFloat64(zSign, 0x408, zSig, status);
4581 /*----------------------------------------------------------------------------
4582 | Returns 1 if the double-precision floating-point value `a' is equal to the
4583 | corresponding value `b', and 0 otherwise. The invalid exception is raised
4584 | if either operand is a NaN. Otherwise, the comparison is performed
4585 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
4586 *----------------------------------------------------------------------------*/
4588 int float64_eq(float64 a, float64 b, float_status *status)
4590 uint64_t av, bv;
4591 a = float64_squash_input_denormal(a, status);
4592 b = float64_squash_input_denormal(b, status);
4594 if ( ( ( extractFloat64Exp( a ) == 0x7FF ) && extractFloat64Frac( a ) )
4595 || ( ( extractFloat64Exp( b ) == 0x7FF ) && extractFloat64Frac( b ) )
4597 float_raise(float_flag_invalid, status);
4598 return 0;
4600 av = float64_val(a);
4601 bv = float64_val(b);
4602 return ( av == bv ) || ( (uint64_t) ( ( av | bv )<<1 ) == 0 );
4606 /*----------------------------------------------------------------------------
4607 | Returns 1 if the double-precision floating-point value `a' is less than or
4608 | equal to the corresponding value `b', and 0 otherwise. The invalid
4609 | exception is raised if either operand is a NaN. The comparison is performed
4610 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
4611 *----------------------------------------------------------------------------*/
4613 int float64_le(float64 a, float64 b, float_status *status)
4615 flag aSign, bSign;
4616 uint64_t av, bv;
4617 a = float64_squash_input_denormal(a, status);
4618 b = float64_squash_input_denormal(b, status);
4620 if ( ( ( extractFloat64Exp( a ) == 0x7FF ) && extractFloat64Frac( a ) )
4621 || ( ( extractFloat64Exp( b ) == 0x7FF ) && extractFloat64Frac( b ) )
4623 float_raise(float_flag_invalid, status);
4624 return 0;
4626 aSign = extractFloat64Sign( a );
4627 bSign = extractFloat64Sign( b );
4628 av = float64_val(a);
4629 bv = float64_val(b);
4630 if ( aSign != bSign ) return aSign || ( (uint64_t) ( ( av | bv )<<1 ) == 0 );
4631 return ( av == bv ) || ( aSign ^ ( av < bv ) );
4635 /*----------------------------------------------------------------------------
4636 | Returns 1 if the double-precision floating-point value `a' is less than
4637 | the corresponding value `b', and 0 otherwise. The invalid exception is
4638 | raised if either operand is a NaN. The comparison is performed according
4639 | to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
4640 *----------------------------------------------------------------------------*/
4642 int float64_lt(float64 a, float64 b, float_status *status)
4644 flag aSign, bSign;
4645 uint64_t av, bv;
4647 a = float64_squash_input_denormal(a, status);
4648 b = float64_squash_input_denormal(b, status);
4649 if ( ( ( extractFloat64Exp( a ) == 0x7FF ) && extractFloat64Frac( a ) )
4650 || ( ( extractFloat64Exp( b ) == 0x7FF ) && extractFloat64Frac( b ) )
4652 float_raise(float_flag_invalid, status);
4653 return 0;
4655 aSign = extractFloat64Sign( a );
4656 bSign = extractFloat64Sign( b );
4657 av = float64_val(a);
4658 bv = float64_val(b);
4659 if ( aSign != bSign ) return aSign && ( (uint64_t) ( ( av | bv )<<1 ) != 0 );
4660 return ( av != bv ) && ( aSign ^ ( av < bv ) );
4664 /*----------------------------------------------------------------------------
4665 | Returns 1 if the double-precision floating-point values `a' and `b' cannot
4666 | be compared, and 0 otherwise. The invalid exception is raised if either
4667 | operand is a NaN. The comparison is performed according to the IEC/IEEE
4668 | Standard for Binary Floating-Point Arithmetic.
4669 *----------------------------------------------------------------------------*/
4671 int float64_unordered(float64 a, float64 b, float_status *status)
4673 a = float64_squash_input_denormal(a, status);
4674 b = float64_squash_input_denormal(b, status);
4676 if ( ( ( extractFloat64Exp( a ) == 0x7FF ) && extractFloat64Frac( a ) )
4677 || ( ( extractFloat64Exp( b ) == 0x7FF ) && extractFloat64Frac( b ) )
4679 float_raise(float_flag_invalid, status);
4680 return 1;
4682 return 0;
4685 /*----------------------------------------------------------------------------
4686 | Returns 1 if the double-precision floating-point value `a' is equal to the
4687 | corresponding value `b', and 0 otherwise. Quiet NaNs do not cause an
4688 | exception.The comparison is performed according to the IEC/IEEE Standard
4689 | for Binary Floating-Point Arithmetic.
4690 *----------------------------------------------------------------------------*/
4692 int float64_eq_quiet(float64 a, float64 b, float_status *status)
4694 uint64_t av, bv;
4695 a = float64_squash_input_denormal(a, status);
4696 b = float64_squash_input_denormal(b, status);
4698 if ( ( ( extractFloat64Exp( a ) == 0x7FF ) && extractFloat64Frac( a ) )
4699 || ( ( extractFloat64Exp( b ) == 0x7FF ) && extractFloat64Frac( b ) )
4701 if (float64_is_signaling_nan(a, status)
4702 || float64_is_signaling_nan(b, status)) {
4703 float_raise(float_flag_invalid, status);
4705 return 0;
4707 av = float64_val(a);
4708 bv = float64_val(b);
4709 return ( av == bv ) || ( (uint64_t) ( ( av | bv )<<1 ) == 0 );
4713 /*----------------------------------------------------------------------------
4714 | Returns 1 if the double-precision floating-point value `a' is less than or
4715 | equal to the corresponding value `b', and 0 otherwise. Quiet NaNs do not
4716 | cause an exception. Otherwise, the comparison is performed according to the
4717 | IEC/IEEE Standard for Binary Floating-Point Arithmetic.
4718 *----------------------------------------------------------------------------*/
4720 int float64_le_quiet(float64 a, float64 b, float_status *status)
4722 flag aSign, bSign;
4723 uint64_t av, bv;
4724 a = float64_squash_input_denormal(a, status);
4725 b = float64_squash_input_denormal(b, status);
4727 if ( ( ( extractFloat64Exp( a ) == 0x7FF ) && extractFloat64Frac( a ) )
4728 || ( ( extractFloat64Exp( b ) == 0x7FF ) && extractFloat64Frac( b ) )
4730 if (float64_is_signaling_nan(a, status)
4731 || float64_is_signaling_nan(b, status)) {
4732 float_raise(float_flag_invalid, status);
4734 return 0;
4736 aSign = extractFloat64Sign( a );
4737 bSign = extractFloat64Sign( b );
4738 av = float64_val(a);
4739 bv = float64_val(b);
4740 if ( aSign != bSign ) return aSign || ( (uint64_t) ( ( av | bv )<<1 ) == 0 );
4741 return ( av == bv ) || ( aSign ^ ( av < bv ) );
4745 /*----------------------------------------------------------------------------
4746 | Returns 1 if the double-precision floating-point value `a' is less than
4747 | the corresponding value `b', and 0 otherwise. Quiet NaNs do not cause an
4748 | exception. Otherwise, the comparison is performed according to the IEC/IEEE
4749 | Standard for Binary Floating-Point Arithmetic.
4750 *----------------------------------------------------------------------------*/
4752 int float64_lt_quiet(float64 a, float64 b, float_status *status)
4754 flag aSign, bSign;
4755 uint64_t av, bv;
4756 a = float64_squash_input_denormal(a, status);
4757 b = float64_squash_input_denormal(b, status);
4759 if ( ( ( extractFloat64Exp( a ) == 0x7FF ) && extractFloat64Frac( a ) )
4760 || ( ( extractFloat64Exp( b ) == 0x7FF ) && extractFloat64Frac( b ) )
4762 if (float64_is_signaling_nan(a, status)
4763 || float64_is_signaling_nan(b, status)) {
4764 float_raise(float_flag_invalid, status);
4766 return 0;
4768 aSign = extractFloat64Sign( a );
4769 bSign = extractFloat64Sign( b );
4770 av = float64_val(a);
4771 bv = float64_val(b);
4772 if ( aSign != bSign ) return aSign && ( (uint64_t) ( ( av | bv )<<1 ) != 0 );
4773 return ( av != bv ) && ( aSign ^ ( av < bv ) );
4777 /*----------------------------------------------------------------------------
4778 | Returns 1 if the double-precision floating-point values `a' and `b' cannot
4779 | be compared, and 0 otherwise. Quiet NaNs do not cause an exception. The
4780 | comparison is performed according to the IEC/IEEE Standard for Binary
4781 | Floating-Point Arithmetic.
4782 *----------------------------------------------------------------------------*/
4784 int float64_unordered_quiet(float64 a, float64 b, float_status *status)
4786 a = float64_squash_input_denormal(a, status);
4787 b = float64_squash_input_denormal(b, status);
4789 if ( ( ( extractFloat64Exp( a ) == 0x7FF ) && extractFloat64Frac( a ) )
4790 || ( ( extractFloat64Exp( b ) == 0x7FF ) && extractFloat64Frac( b ) )
4792 if (float64_is_signaling_nan(a, status)
4793 || float64_is_signaling_nan(b, status)) {
4794 float_raise(float_flag_invalid, status);
4796 return 1;
4798 return 0;
4801 /*----------------------------------------------------------------------------
4802 | Returns the result of converting the extended double-precision floating-
4803 | point value `a' to the 32-bit two's complement integer format. The
4804 | conversion is performed according to the IEC/IEEE Standard for Binary
4805 | Floating-Point Arithmetic---which means in particular that the conversion
4806 | is rounded according to the current rounding mode. If `a' is a NaN, the
4807 | largest positive integer is returned. Otherwise, if the conversion
4808 | overflows, the largest integer with the same sign as `a' is returned.
4809 *----------------------------------------------------------------------------*/
4811 int32_t floatx80_to_int32(floatx80 a, float_status *status)
4813 flag aSign;
4814 int32_t aExp, shiftCount;
4815 uint64_t aSig;
4817 aSig = extractFloatx80Frac( a );
4818 aExp = extractFloatx80Exp( a );
4819 aSign = extractFloatx80Sign( a );
4820 if ( ( aExp == 0x7FFF ) && (uint64_t) ( aSig<<1 ) ) aSign = 0;
4821 shiftCount = 0x4037 - aExp;
4822 if ( shiftCount <= 0 ) shiftCount = 1;
4823 shift64RightJamming( aSig, shiftCount, &aSig );
4824 return roundAndPackInt32(aSign, aSig, status);
4828 /*----------------------------------------------------------------------------
4829 | Returns the result of converting the extended double-precision floating-
4830 | point value `a' to the 32-bit two's complement integer format. The
4831 | conversion is performed according to the IEC/IEEE Standard for Binary
4832 | Floating-Point Arithmetic, except that the conversion is always rounded
4833 | toward zero. If `a' is a NaN, the largest positive integer is returned.
4834 | Otherwise, if the conversion overflows, the largest integer with the same
4835 | sign as `a' is returned.
4836 *----------------------------------------------------------------------------*/
4838 int32_t floatx80_to_int32_round_to_zero(floatx80 a, float_status *status)
4840 flag aSign;
4841 int32_t aExp, shiftCount;
4842 uint64_t aSig, savedASig;
4843 int32_t z;
4845 aSig = extractFloatx80Frac( a );
4846 aExp = extractFloatx80Exp( a );
4847 aSign = extractFloatx80Sign( a );
4848 if ( 0x401E < aExp ) {
4849 if ( ( aExp == 0x7FFF ) && (uint64_t) ( aSig<<1 ) ) aSign = 0;
4850 goto invalid;
4852 else if ( aExp < 0x3FFF ) {
4853 if (aExp || aSig) {
4854 status->float_exception_flags |= float_flag_inexact;
4856 return 0;
4858 shiftCount = 0x403E - aExp;
4859 savedASig = aSig;
4860 aSig >>= shiftCount;
4861 z = aSig;
4862 if ( aSign ) z = - z;
4863 if ( ( z < 0 ) ^ aSign ) {
4864 invalid:
4865 float_raise(float_flag_invalid, status);
4866 return aSign ? (int32_t) 0x80000000 : 0x7FFFFFFF;
4868 if ( ( aSig<<shiftCount ) != savedASig ) {
4869 status->float_exception_flags |= float_flag_inexact;
4871 return z;
4875 /*----------------------------------------------------------------------------
4876 | Returns the result of converting the extended double-precision floating-
4877 | point value `a' to the 64-bit two's complement integer format. The
4878 | conversion is performed according to the IEC/IEEE Standard for Binary
4879 | Floating-Point Arithmetic---which means in particular that the conversion
4880 | is rounded according to the current rounding mode. If `a' is a NaN,
4881 | the largest positive integer is returned. Otherwise, if the conversion
4882 | overflows, the largest integer with the same sign as `a' is returned.
4883 *----------------------------------------------------------------------------*/
4885 int64_t floatx80_to_int64(floatx80 a, float_status *status)
4887 flag aSign;
4888 int32_t aExp, shiftCount;
4889 uint64_t aSig, aSigExtra;
4891 aSig = extractFloatx80Frac( a );
4892 aExp = extractFloatx80Exp( a );
4893 aSign = extractFloatx80Sign( a );
4894 shiftCount = 0x403E - aExp;
4895 if ( shiftCount <= 0 ) {
4896 if ( shiftCount ) {
4897 float_raise(float_flag_invalid, status);
4898 if ( ! aSign
4899 || ( ( aExp == 0x7FFF )
4900 && ( aSig != LIT64( 0x8000000000000000 ) ) )
4902 return LIT64( 0x7FFFFFFFFFFFFFFF );
4904 return (int64_t) LIT64( 0x8000000000000000 );
4906 aSigExtra = 0;
4908 else {
4909 shift64ExtraRightJamming( aSig, 0, shiftCount, &aSig, &aSigExtra );
4911 return roundAndPackInt64(aSign, aSig, aSigExtra, status);
4915 /*----------------------------------------------------------------------------
4916 | Returns the result of converting the extended double-precision floating-
4917 | point value `a' to the 64-bit two's complement integer format. The
4918 | conversion is performed according to the IEC/IEEE Standard for Binary
4919 | Floating-Point Arithmetic, except that the conversion is always rounded
4920 | toward zero. If `a' is a NaN, the largest positive integer is returned.
4921 | Otherwise, if the conversion overflows, the largest integer with the same
4922 | sign as `a' is returned.
4923 *----------------------------------------------------------------------------*/
4925 int64_t floatx80_to_int64_round_to_zero(floatx80 a, float_status *status)
4927 flag aSign;
4928 int32_t aExp, shiftCount;
4929 uint64_t aSig;
4930 int64_t z;
4932 aSig = extractFloatx80Frac( a );
4933 aExp = extractFloatx80Exp( a );
4934 aSign = extractFloatx80Sign( a );
4935 shiftCount = aExp - 0x403E;
4936 if ( 0 <= shiftCount ) {
4937 aSig &= LIT64( 0x7FFFFFFFFFFFFFFF );
4938 if ( ( a.high != 0xC03E ) || aSig ) {
4939 float_raise(float_flag_invalid, status);
4940 if ( ! aSign || ( ( aExp == 0x7FFF ) && aSig ) ) {
4941 return LIT64( 0x7FFFFFFFFFFFFFFF );
4944 return (int64_t) LIT64( 0x8000000000000000 );
4946 else if ( aExp < 0x3FFF ) {
4947 if (aExp | aSig) {
4948 status->float_exception_flags |= float_flag_inexact;
4950 return 0;
4952 z = aSig>>( - shiftCount );
4953 if ( (uint64_t) ( aSig<<( shiftCount & 63 ) ) ) {
4954 status->float_exception_flags |= float_flag_inexact;
4956 if ( aSign ) z = - z;
4957 return z;
4961 /*----------------------------------------------------------------------------
4962 | Returns the result of converting the extended double-precision floating-
4963 | point value `a' to the single-precision floating-point format. The
4964 | conversion is performed according to the IEC/IEEE Standard for Binary
4965 | Floating-Point Arithmetic.
4966 *----------------------------------------------------------------------------*/
4968 float32 floatx80_to_float32(floatx80 a, float_status *status)
4970 flag aSign;
4971 int32_t aExp;
4972 uint64_t aSig;
4974 aSig = extractFloatx80Frac( a );
4975 aExp = extractFloatx80Exp( a );
4976 aSign = extractFloatx80Sign( a );
4977 if ( aExp == 0x7FFF ) {
4978 if ( (uint64_t) ( aSig<<1 ) ) {
4979 return commonNaNToFloat32(floatx80ToCommonNaN(a, status), status);
4981 return packFloat32( aSign, 0xFF, 0 );
4983 shift64RightJamming( aSig, 33, &aSig );
4984 if ( aExp || aSig ) aExp -= 0x3F81;
4985 return roundAndPackFloat32(aSign, aExp, aSig, status);
4989 /*----------------------------------------------------------------------------
4990 | Returns the result of converting the extended double-precision floating-
4991 | point value `a' to the double-precision floating-point format. The
4992 | conversion is performed according to the IEC/IEEE Standard for Binary
4993 | Floating-Point Arithmetic.
4994 *----------------------------------------------------------------------------*/
4996 float64 floatx80_to_float64(floatx80 a, float_status *status)
4998 flag aSign;
4999 int32_t aExp;
5000 uint64_t aSig, zSig;
5002 aSig = extractFloatx80Frac( a );
5003 aExp = extractFloatx80Exp( a );
5004 aSign = extractFloatx80Sign( a );
5005 if ( aExp == 0x7FFF ) {
5006 if ( (uint64_t) ( aSig<<1 ) ) {
5007 return commonNaNToFloat64(floatx80ToCommonNaN(a, status), status);
5009 return packFloat64( aSign, 0x7FF, 0 );
5011 shift64RightJamming( aSig, 1, &zSig );
5012 if ( aExp || aSig ) aExp -= 0x3C01;
5013 return roundAndPackFloat64(aSign, aExp, zSig, status);
5017 /*----------------------------------------------------------------------------
5018 | Returns the result of converting the extended double-precision floating-
5019 | point value `a' to the quadruple-precision floating-point format. The
5020 | conversion is performed according to the IEC/IEEE Standard for Binary
5021 | Floating-Point Arithmetic.
5022 *----------------------------------------------------------------------------*/
5024 float128 floatx80_to_float128(floatx80 a, float_status *status)
5026 flag aSign;
5027 int aExp;
5028 uint64_t aSig, zSig0, zSig1;
5030 aSig = extractFloatx80Frac( a );
5031 aExp = extractFloatx80Exp( a );
5032 aSign = extractFloatx80Sign( a );
5033 if ( ( aExp == 0x7FFF ) && (uint64_t) ( aSig<<1 ) ) {
5034 return commonNaNToFloat128(floatx80ToCommonNaN(a, status), status);
5036 shift128Right( aSig<<1, 0, 16, &zSig0, &zSig1 );
5037 return packFloat128( aSign, aExp, zSig0, zSig1 );
5041 /*----------------------------------------------------------------------------
5042 | Rounds the extended double-precision floating-point value `a' to an integer,
5043 | and returns the result as an extended quadruple-precision floating-point
5044 | value. The operation is performed according to the IEC/IEEE Standard for
5045 | Binary Floating-Point Arithmetic.
5046 *----------------------------------------------------------------------------*/
5048 floatx80 floatx80_round_to_int(floatx80 a, float_status *status)
5050 flag aSign;
5051 int32_t aExp;
5052 uint64_t lastBitMask, roundBitsMask;
5053 floatx80 z;
5055 aExp = extractFloatx80Exp( a );
5056 if ( 0x403E <= aExp ) {
5057 if ( ( aExp == 0x7FFF ) && (uint64_t) ( extractFloatx80Frac( a )<<1 ) ) {
5058 return propagateFloatx80NaN(a, a, status);
5060 return a;
5062 if ( aExp < 0x3FFF ) {
5063 if ( ( aExp == 0 )
5064 && ( (uint64_t) ( extractFloatx80Frac( a )<<1 ) == 0 ) ) {
5065 return a;
5067 status->float_exception_flags |= float_flag_inexact;
5068 aSign = extractFloatx80Sign( a );
5069 switch (status->float_rounding_mode) {
5070 case float_round_nearest_even:
5071 if ( ( aExp == 0x3FFE ) && (uint64_t) ( extractFloatx80Frac( a )<<1 )
5073 return
5074 packFloatx80( aSign, 0x3FFF, LIT64( 0x8000000000000000 ) );
5076 break;
5077 case float_round_ties_away:
5078 if (aExp == 0x3FFE) {
5079 return packFloatx80(aSign, 0x3FFF, LIT64(0x8000000000000000));
5081 break;
5082 case float_round_down:
5083 return
5084 aSign ?
5085 packFloatx80( 1, 0x3FFF, LIT64( 0x8000000000000000 ) )
5086 : packFloatx80( 0, 0, 0 );
5087 case float_round_up:
5088 return
5089 aSign ? packFloatx80( 1, 0, 0 )
5090 : packFloatx80( 0, 0x3FFF, LIT64( 0x8000000000000000 ) );
5092 return packFloatx80( aSign, 0, 0 );
5094 lastBitMask = 1;
5095 lastBitMask <<= 0x403E - aExp;
5096 roundBitsMask = lastBitMask - 1;
5097 z = a;
5098 switch (status->float_rounding_mode) {
5099 case float_round_nearest_even:
5100 z.low += lastBitMask>>1;
5101 if ((z.low & roundBitsMask) == 0) {
5102 z.low &= ~lastBitMask;
5104 break;
5105 case float_round_ties_away:
5106 z.low += lastBitMask >> 1;
5107 break;
5108 case float_round_to_zero:
5109 break;
5110 case float_round_up:
5111 if (!extractFloatx80Sign(z)) {
5112 z.low += roundBitsMask;
5114 break;
5115 case float_round_down:
5116 if (extractFloatx80Sign(z)) {
5117 z.low += roundBitsMask;
5119 break;
5120 default:
5121 abort();
5123 z.low &= ~ roundBitsMask;
5124 if ( z.low == 0 ) {
5125 ++z.high;
5126 z.low = LIT64( 0x8000000000000000 );
5128 if (z.low != a.low) {
5129 status->float_exception_flags |= float_flag_inexact;
5131 return z;
5135 /*----------------------------------------------------------------------------
5136 | Returns the result of adding the absolute values of the extended double-
5137 | precision floating-point values `a' and `b'. If `zSign' is 1, the sum is
5138 | negated before being returned. `zSign' is ignored if the result is a NaN.
5139 | The addition is performed according to the IEC/IEEE Standard for Binary
5140 | Floating-Point Arithmetic.
5141 *----------------------------------------------------------------------------*/
5143 static floatx80 addFloatx80Sigs(floatx80 a, floatx80 b, flag zSign,
5144 float_status *status)
5146 int32_t aExp, bExp, zExp;
5147 uint64_t aSig, bSig, zSig0, zSig1;
5148 int32_t expDiff;
5150 aSig = extractFloatx80Frac( a );
5151 aExp = extractFloatx80Exp( a );
5152 bSig = extractFloatx80Frac( b );
5153 bExp = extractFloatx80Exp( b );
5154 expDiff = aExp - bExp;
5155 if ( 0 < expDiff ) {
5156 if ( aExp == 0x7FFF ) {
5157 if ((uint64_t)(aSig << 1)) {
5158 return propagateFloatx80NaN(a, b, status);
5160 return a;
5162 if ( bExp == 0 ) --expDiff;
5163 shift64ExtraRightJamming( bSig, 0, expDiff, &bSig, &zSig1 );
5164 zExp = aExp;
5166 else if ( expDiff < 0 ) {
5167 if ( bExp == 0x7FFF ) {
5168 if ((uint64_t)(bSig << 1)) {
5169 return propagateFloatx80NaN(a, b, status);
5171 return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
5173 if ( aExp == 0 ) ++expDiff;
5174 shift64ExtraRightJamming( aSig, 0, - expDiff, &aSig, &zSig1 );
5175 zExp = bExp;
5177 else {
5178 if ( aExp == 0x7FFF ) {
5179 if ( (uint64_t) ( ( aSig | bSig )<<1 ) ) {
5180 return propagateFloatx80NaN(a, b, status);
5182 return a;
5184 zSig1 = 0;
5185 zSig0 = aSig + bSig;
5186 if ( aExp == 0 ) {
5187 normalizeFloatx80Subnormal( zSig0, &zExp, &zSig0 );
5188 goto roundAndPack;
5190 zExp = aExp;
5191 goto shiftRight1;
5193 zSig0 = aSig + bSig;
5194 if ( (int64_t) zSig0 < 0 ) goto roundAndPack;
5195 shiftRight1:
5196 shift64ExtraRightJamming( zSig0, zSig1, 1, &zSig0, &zSig1 );
5197 zSig0 |= LIT64( 0x8000000000000000 );
5198 ++zExp;
5199 roundAndPack:
5200 return roundAndPackFloatx80(status->floatx80_rounding_precision,
5201 zSign, zExp, zSig0, zSig1, status);
5204 /*----------------------------------------------------------------------------
5205 | Returns the result of subtracting the absolute values of the extended
5206 | double-precision floating-point values `a' and `b'. If `zSign' is 1, the
5207 | difference is negated before being returned. `zSign' is ignored if the
5208 | result is a NaN. The subtraction is performed according to the IEC/IEEE
5209 | Standard for Binary Floating-Point Arithmetic.
5210 *----------------------------------------------------------------------------*/
5212 static floatx80 subFloatx80Sigs(floatx80 a, floatx80 b, flag zSign,
5213 float_status *status)
5215 int32_t aExp, bExp, zExp;
5216 uint64_t aSig, bSig, zSig0, zSig1;
5217 int32_t expDiff;
5219 aSig = extractFloatx80Frac( a );
5220 aExp = extractFloatx80Exp( a );
5221 bSig = extractFloatx80Frac( b );
5222 bExp = extractFloatx80Exp( b );
5223 expDiff = aExp - bExp;
5224 if ( 0 < expDiff ) goto aExpBigger;
5225 if ( expDiff < 0 ) goto bExpBigger;
5226 if ( aExp == 0x7FFF ) {
5227 if ( (uint64_t) ( ( aSig | bSig )<<1 ) ) {
5228 return propagateFloatx80NaN(a, b, status);
5230 float_raise(float_flag_invalid, status);
5231 return floatx80_default_nan(status);
5233 if ( aExp == 0 ) {
5234 aExp = 1;
5235 bExp = 1;
5237 zSig1 = 0;
5238 if ( bSig < aSig ) goto aBigger;
5239 if ( aSig < bSig ) goto bBigger;
5240 return packFloatx80(status->float_rounding_mode == float_round_down, 0, 0);
5241 bExpBigger:
5242 if ( bExp == 0x7FFF ) {
5243 if ((uint64_t)(bSig << 1)) {
5244 return propagateFloatx80NaN(a, b, status);
5246 return packFloatx80( zSign ^ 1, 0x7FFF, LIT64( 0x8000000000000000 ) );
5248 if ( aExp == 0 ) ++expDiff;
5249 shift128RightJamming( aSig, 0, - expDiff, &aSig, &zSig1 );
5250 bBigger:
5251 sub128( bSig, 0, aSig, zSig1, &zSig0, &zSig1 );
5252 zExp = bExp;
5253 zSign ^= 1;
5254 goto normalizeRoundAndPack;
5255 aExpBigger:
5256 if ( aExp == 0x7FFF ) {
5257 if ((uint64_t)(aSig << 1)) {
5258 return propagateFloatx80NaN(a, b, status);
5260 return a;
5262 if ( bExp == 0 ) --expDiff;
5263 shift128RightJamming( bSig, 0, expDiff, &bSig, &zSig1 );
5264 aBigger:
5265 sub128( aSig, 0, bSig, zSig1, &zSig0, &zSig1 );
5266 zExp = aExp;
5267 normalizeRoundAndPack:
5268 return normalizeRoundAndPackFloatx80(status->floatx80_rounding_precision,
5269 zSign, zExp, zSig0, zSig1, status);
5272 /*----------------------------------------------------------------------------
5273 | Returns the result of adding the extended double-precision floating-point
5274 | values `a' and `b'. The operation is performed according to the IEC/IEEE
5275 | Standard for Binary Floating-Point Arithmetic.
5276 *----------------------------------------------------------------------------*/
5278 floatx80 floatx80_add(floatx80 a, floatx80 b, float_status *status)
5280 flag aSign, bSign;
5282 aSign = extractFloatx80Sign( a );
5283 bSign = extractFloatx80Sign( b );
5284 if ( aSign == bSign ) {
5285 return addFloatx80Sigs(a, b, aSign, status);
5287 else {
5288 return subFloatx80Sigs(a, b, aSign, status);
5293 /*----------------------------------------------------------------------------
5294 | Returns the result of subtracting the extended double-precision floating-
5295 | point values `a' and `b'. The operation is performed according to the
5296 | IEC/IEEE Standard for Binary Floating-Point Arithmetic.
5297 *----------------------------------------------------------------------------*/
5299 floatx80 floatx80_sub(floatx80 a, floatx80 b, float_status *status)
5301 flag aSign, bSign;
5303 aSign = extractFloatx80Sign( a );
5304 bSign = extractFloatx80Sign( b );
5305 if ( aSign == bSign ) {
5306 return subFloatx80Sigs(a, b, aSign, status);
5308 else {
5309 return addFloatx80Sigs(a, b, aSign, status);
5314 /*----------------------------------------------------------------------------
5315 | Returns the result of multiplying the extended double-precision floating-
5316 | point values `a' and `b'. The operation is performed according to the
5317 | IEC/IEEE Standard for Binary Floating-Point Arithmetic.
5318 *----------------------------------------------------------------------------*/
5320 floatx80 floatx80_mul(floatx80 a, floatx80 b, float_status *status)
5322 flag aSign, bSign, zSign;
5323 int32_t aExp, bExp, zExp;
5324 uint64_t aSig, bSig, zSig0, zSig1;
5326 aSig = extractFloatx80Frac( a );
5327 aExp = extractFloatx80Exp( a );
5328 aSign = extractFloatx80Sign( a );
5329 bSig = extractFloatx80Frac( b );
5330 bExp = extractFloatx80Exp( b );
5331 bSign = extractFloatx80Sign( b );
5332 zSign = aSign ^ bSign;
5333 if ( aExp == 0x7FFF ) {
5334 if ( (uint64_t) ( aSig<<1 )
5335 || ( ( bExp == 0x7FFF ) && (uint64_t) ( bSig<<1 ) ) ) {
5336 return propagateFloatx80NaN(a, b, status);
5338 if ( ( bExp | bSig ) == 0 ) goto invalid;
5339 return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
5341 if ( bExp == 0x7FFF ) {
5342 if ((uint64_t)(bSig << 1)) {
5343 return propagateFloatx80NaN(a, b, status);
5345 if ( ( aExp | aSig ) == 0 ) {
5346 invalid:
5347 float_raise(float_flag_invalid, status);
5348 return floatx80_default_nan(status);
5350 return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
5352 if ( aExp == 0 ) {
5353 if ( aSig == 0 ) return packFloatx80( zSign, 0, 0 );
5354 normalizeFloatx80Subnormal( aSig, &aExp, &aSig );
5356 if ( bExp == 0 ) {
5357 if ( bSig == 0 ) return packFloatx80( zSign, 0, 0 );
5358 normalizeFloatx80Subnormal( bSig, &bExp, &bSig );
5360 zExp = aExp + bExp - 0x3FFE;
5361 mul64To128( aSig, bSig, &zSig0, &zSig1 );
5362 if ( 0 < (int64_t) zSig0 ) {
5363 shortShift128Left( zSig0, zSig1, 1, &zSig0, &zSig1 );
5364 --zExp;
5366 return roundAndPackFloatx80(status->floatx80_rounding_precision,
5367 zSign, zExp, zSig0, zSig1, status);
5370 /*----------------------------------------------------------------------------
5371 | Returns the result of dividing the extended double-precision floating-point
5372 | value `a' by the corresponding value `b'. The operation is performed
5373 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
5374 *----------------------------------------------------------------------------*/
5376 floatx80 floatx80_div(floatx80 a, floatx80 b, float_status *status)
5378 flag aSign, bSign, zSign;
5379 int32_t aExp, bExp, zExp;
5380 uint64_t aSig, bSig, zSig0, zSig1;
5381 uint64_t rem0, rem1, rem2, term0, term1, term2;
5383 aSig = extractFloatx80Frac( a );
5384 aExp = extractFloatx80Exp( a );
5385 aSign = extractFloatx80Sign( a );
5386 bSig = extractFloatx80Frac( b );
5387 bExp = extractFloatx80Exp( b );
5388 bSign = extractFloatx80Sign( b );
5389 zSign = aSign ^ bSign;
5390 if ( aExp == 0x7FFF ) {
5391 if ((uint64_t)(aSig << 1)) {
5392 return propagateFloatx80NaN(a, b, status);
5394 if ( bExp == 0x7FFF ) {
5395 if ((uint64_t)(bSig << 1)) {
5396 return propagateFloatx80NaN(a, b, status);
5398 goto invalid;
5400 return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
5402 if ( bExp == 0x7FFF ) {
5403 if ((uint64_t)(bSig << 1)) {
5404 return propagateFloatx80NaN(a, b, status);
5406 return packFloatx80( zSign, 0, 0 );
5408 if ( bExp == 0 ) {
5409 if ( bSig == 0 ) {
5410 if ( ( aExp | aSig ) == 0 ) {
5411 invalid:
5412 float_raise(float_flag_invalid, status);
5413 return floatx80_default_nan(status);
5415 float_raise(float_flag_divbyzero, status);
5416 return packFloatx80( zSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
5418 normalizeFloatx80Subnormal( bSig, &bExp, &bSig );
5420 if ( aExp == 0 ) {
5421 if ( aSig == 0 ) return packFloatx80( zSign, 0, 0 );
5422 normalizeFloatx80Subnormal( aSig, &aExp, &aSig );
5424 zExp = aExp - bExp + 0x3FFE;
5425 rem1 = 0;
5426 if ( bSig <= aSig ) {
5427 shift128Right( aSig, 0, 1, &aSig, &rem1 );
5428 ++zExp;
5430 zSig0 = estimateDiv128To64( aSig, rem1, bSig );
5431 mul64To128( bSig, zSig0, &term0, &term1 );
5432 sub128( aSig, rem1, term0, term1, &rem0, &rem1 );
5433 while ( (int64_t) rem0 < 0 ) {
5434 --zSig0;
5435 add128( rem0, rem1, 0, bSig, &rem0, &rem1 );
5437 zSig1 = estimateDiv128To64( rem1, 0, bSig );
5438 if ( (uint64_t) ( zSig1<<1 ) <= 8 ) {
5439 mul64To128( bSig, zSig1, &term1, &term2 );
5440 sub128( rem1, 0, term1, term2, &rem1, &rem2 );
5441 while ( (int64_t) rem1 < 0 ) {
5442 --zSig1;
5443 add128( rem1, rem2, 0, bSig, &rem1, &rem2 );
5445 zSig1 |= ( ( rem1 | rem2 ) != 0 );
5447 return roundAndPackFloatx80(status->floatx80_rounding_precision,
5448 zSign, zExp, zSig0, zSig1, status);
5451 /*----------------------------------------------------------------------------
5452 | Returns the remainder of the extended double-precision floating-point value
5453 | `a' with respect to the corresponding value `b'. The operation is performed
5454 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
5455 *----------------------------------------------------------------------------*/
5457 floatx80 floatx80_rem(floatx80 a, floatx80 b, float_status *status)
5459 flag aSign, zSign;
5460 int32_t aExp, bExp, expDiff;
5461 uint64_t aSig0, aSig1, bSig;
5462 uint64_t q, term0, term1, alternateASig0, alternateASig1;
5464 aSig0 = extractFloatx80Frac( a );
5465 aExp = extractFloatx80Exp( a );
5466 aSign = extractFloatx80Sign( a );
5467 bSig = extractFloatx80Frac( b );
5468 bExp = extractFloatx80Exp( b );
5469 if ( aExp == 0x7FFF ) {
5470 if ( (uint64_t) ( aSig0<<1 )
5471 || ( ( bExp == 0x7FFF ) && (uint64_t) ( bSig<<1 ) ) ) {
5472 return propagateFloatx80NaN(a, b, status);
5474 goto invalid;
5476 if ( bExp == 0x7FFF ) {
5477 if ((uint64_t)(bSig << 1)) {
5478 return propagateFloatx80NaN(a, b, status);
5480 return a;
5482 if ( bExp == 0 ) {
5483 if ( bSig == 0 ) {
5484 invalid:
5485 float_raise(float_flag_invalid, status);
5486 return floatx80_default_nan(status);
5488 normalizeFloatx80Subnormal( bSig, &bExp, &bSig );
5490 if ( aExp == 0 ) {
5491 if ( (uint64_t) ( aSig0<<1 ) == 0 ) return a;
5492 normalizeFloatx80Subnormal( aSig0, &aExp, &aSig0 );
5494 bSig |= LIT64( 0x8000000000000000 );
5495 zSign = aSign;
5496 expDiff = aExp - bExp;
5497 aSig1 = 0;
5498 if ( expDiff < 0 ) {
5499 if ( expDiff < -1 ) return a;
5500 shift128Right( aSig0, 0, 1, &aSig0, &aSig1 );
5501 expDiff = 0;
5503 q = ( bSig <= aSig0 );
5504 if ( q ) aSig0 -= bSig;
5505 expDiff -= 64;
5506 while ( 0 < expDiff ) {
5507 q = estimateDiv128To64( aSig0, aSig1, bSig );
5508 q = ( 2 < q ) ? q - 2 : 0;
5509 mul64To128( bSig, q, &term0, &term1 );
5510 sub128( aSig0, aSig1, term0, term1, &aSig0, &aSig1 );
5511 shortShift128Left( aSig0, aSig1, 62, &aSig0, &aSig1 );
5512 expDiff -= 62;
5514 expDiff += 64;
5515 if ( 0 < expDiff ) {
5516 q = estimateDiv128To64( aSig0, aSig1, bSig );
5517 q = ( 2 < q ) ? q - 2 : 0;
5518 q >>= 64 - expDiff;
5519 mul64To128( bSig, q<<( 64 - expDiff ), &term0, &term1 );
5520 sub128( aSig0, aSig1, term0, term1, &aSig0, &aSig1 );
5521 shortShift128Left( 0, bSig, 64 - expDiff, &term0, &term1 );
5522 while ( le128( term0, term1, aSig0, aSig1 ) ) {
5523 ++q;
5524 sub128( aSig0, aSig1, term0, term1, &aSig0, &aSig1 );
5527 else {
5528 term1 = 0;
5529 term0 = bSig;
5531 sub128( term0, term1, aSig0, aSig1, &alternateASig0, &alternateASig1 );
5532 if ( lt128( alternateASig0, alternateASig1, aSig0, aSig1 )
5533 || ( eq128( alternateASig0, alternateASig1, aSig0, aSig1 )
5534 && ( q & 1 ) )
5536 aSig0 = alternateASig0;
5537 aSig1 = alternateASig1;
5538 zSign = ! zSign;
5540 return
5541 normalizeRoundAndPackFloatx80(
5542 80, zSign, bExp + expDiff, aSig0, aSig1, status);
5546 /*----------------------------------------------------------------------------
5547 | Returns the square root of the extended double-precision floating-point
5548 | value `a'. The operation is performed according to the IEC/IEEE Standard
5549 | for Binary Floating-Point Arithmetic.
5550 *----------------------------------------------------------------------------*/
5552 floatx80 floatx80_sqrt(floatx80 a, float_status *status)
5554 flag aSign;
5555 int32_t aExp, zExp;
5556 uint64_t aSig0, aSig1, zSig0, zSig1, doubleZSig0;
5557 uint64_t rem0, rem1, rem2, rem3, term0, term1, term2, term3;
5559 aSig0 = extractFloatx80Frac( a );
5560 aExp = extractFloatx80Exp( a );
5561 aSign = extractFloatx80Sign( a );
5562 if ( aExp == 0x7FFF ) {
5563 if ((uint64_t)(aSig0 << 1)) {
5564 return propagateFloatx80NaN(a, a, status);
5566 if ( ! aSign ) return a;
5567 goto invalid;
5569 if ( aSign ) {
5570 if ( ( aExp | aSig0 ) == 0 ) return a;
5571 invalid:
5572 float_raise(float_flag_invalid, status);
5573 return floatx80_default_nan(status);
5575 if ( aExp == 0 ) {
5576 if ( aSig0 == 0 ) return packFloatx80( 0, 0, 0 );
5577 normalizeFloatx80Subnormal( aSig0, &aExp, &aSig0 );
5579 zExp = ( ( aExp - 0x3FFF )>>1 ) + 0x3FFF;
5580 zSig0 = estimateSqrt32( aExp, aSig0>>32 );
5581 shift128Right( aSig0, 0, 2 + ( aExp & 1 ), &aSig0, &aSig1 );
5582 zSig0 = estimateDiv128To64( aSig0, aSig1, zSig0<<32 ) + ( zSig0<<30 );
5583 doubleZSig0 = zSig0<<1;
5584 mul64To128( zSig0, zSig0, &term0, &term1 );
5585 sub128( aSig0, aSig1, term0, term1, &rem0, &rem1 );
5586 while ( (int64_t) rem0 < 0 ) {
5587 --zSig0;
5588 doubleZSig0 -= 2;
5589 add128( rem0, rem1, zSig0>>63, doubleZSig0 | 1, &rem0, &rem1 );
5591 zSig1 = estimateDiv128To64( rem1, 0, doubleZSig0 );
5592 if ( ( zSig1 & LIT64( 0x3FFFFFFFFFFFFFFF ) ) <= 5 ) {
5593 if ( zSig1 == 0 ) zSig1 = 1;
5594 mul64To128( doubleZSig0, zSig1, &term1, &term2 );
5595 sub128( rem1, 0, term1, term2, &rem1, &rem2 );
5596 mul64To128( zSig1, zSig1, &term2, &term3 );
5597 sub192( rem1, rem2, 0, 0, term2, term3, &rem1, &rem2, &rem3 );
5598 while ( (int64_t) rem1 < 0 ) {
5599 --zSig1;
5600 shortShift128Left( 0, zSig1, 1, &term2, &term3 );
5601 term3 |= 1;
5602 term2 |= doubleZSig0;
5603 add192( rem1, rem2, rem3, 0, term2, term3, &rem1, &rem2, &rem3 );
5605 zSig1 |= ( ( rem1 | rem2 | rem3 ) != 0 );
5607 shortShift128Left( 0, zSig1, 1, &zSig0, &zSig1 );
5608 zSig0 |= doubleZSig0;
5609 return roundAndPackFloatx80(status->floatx80_rounding_precision,
5610 0, zExp, zSig0, zSig1, status);
5613 /*----------------------------------------------------------------------------
5614 | Returns 1 if the extended double-precision floating-point value `a' is equal
5615 | to the corresponding value `b', and 0 otherwise. The invalid exception is
5616 | raised if either operand is a NaN. Otherwise, the comparison is performed
5617 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
5618 *----------------------------------------------------------------------------*/
5620 int floatx80_eq(floatx80 a, floatx80 b, float_status *status)
5623 if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
5624 && (uint64_t) ( extractFloatx80Frac( a )<<1 ) )
5625 || ( ( extractFloatx80Exp( b ) == 0x7FFF )
5626 && (uint64_t) ( extractFloatx80Frac( b )<<1 ) )
5628 float_raise(float_flag_invalid, status);
5629 return 0;
5631 return
5632 ( a.low == b.low )
5633 && ( ( a.high == b.high )
5634 || ( ( a.low == 0 )
5635 && ( (uint16_t) ( ( a.high | b.high )<<1 ) == 0 ) )
5640 /*----------------------------------------------------------------------------
5641 | Returns 1 if the extended double-precision floating-point value `a' is
5642 | less than or equal to the corresponding value `b', and 0 otherwise. The
5643 | invalid exception is raised if either operand is a NaN. The comparison is
5644 | performed according to the IEC/IEEE Standard for Binary Floating-Point
5645 | Arithmetic.
5646 *----------------------------------------------------------------------------*/
5648 int floatx80_le(floatx80 a, floatx80 b, float_status *status)
5650 flag aSign, bSign;
5652 if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
5653 && (uint64_t) ( extractFloatx80Frac( a )<<1 ) )
5654 || ( ( extractFloatx80Exp( b ) == 0x7FFF )
5655 && (uint64_t) ( extractFloatx80Frac( b )<<1 ) )
5657 float_raise(float_flag_invalid, status);
5658 return 0;
5660 aSign = extractFloatx80Sign( a );
5661 bSign = extractFloatx80Sign( b );
5662 if ( aSign != bSign ) {
5663 return
5664 aSign
5665 || ( ( ( (uint16_t) ( ( a.high | b.high )<<1 ) ) | a.low | b.low )
5666 == 0 );
5668 return
5669 aSign ? le128( b.high, b.low, a.high, a.low )
5670 : le128( a.high, a.low, b.high, b.low );
5674 /*----------------------------------------------------------------------------
5675 | Returns 1 if the extended double-precision floating-point value `a' is
5676 | less than the corresponding value `b', and 0 otherwise. The invalid
5677 | exception is raised if either operand is a NaN. The comparison is performed
5678 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
5679 *----------------------------------------------------------------------------*/
5681 int floatx80_lt(floatx80 a, floatx80 b, float_status *status)
5683 flag aSign, bSign;
5685 if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
5686 && (uint64_t) ( extractFloatx80Frac( a )<<1 ) )
5687 || ( ( extractFloatx80Exp( b ) == 0x7FFF )
5688 && (uint64_t) ( extractFloatx80Frac( b )<<1 ) )
5690 float_raise(float_flag_invalid, status);
5691 return 0;
5693 aSign = extractFloatx80Sign( a );
5694 bSign = extractFloatx80Sign( b );
5695 if ( aSign != bSign ) {
5696 return
5697 aSign
5698 && ( ( ( (uint16_t) ( ( a.high | b.high )<<1 ) ) | a.low | b.low )
5699 != 0 );
5701 return
5702 aSign ? lt128( b.high, b.low, a.high, a.low )
5703 : lt128( a.high, a.low, b.high, b.low );
5707 /*----------------------------------------------------------------------------
5708 | Returns 1 if the extended double-precision floating-point values `a' and `b'
5709 | cannot be compared, and 0 otherwise. The invalid exception is raised if
5710 | either operand is a NaN. The comparison is performed according to the
5711 | IEC/IEEE Standard for Binary Floating-Point Arithmetic.
5712 *----------------------------------------------------------------------------*/
5713 int floatx80_unordered(floatx80 a, floatx80 b, float_status *status)
5715 if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
5716 && (uint64_t) ( extractFloatx80Frac( a )<<1 ) )
5717 || ( ( extractFloatx80Exp( b ) == 0x7FFF )
5718 && (uint64_t) ( extractFloatx80Frac( b )<<1 ) )
5720 float_raise(float_flag_invalid, status);
5721 return 1;
5723 return 0;
5726 /*----------------------------------------------------------------------------
5727 | Returns 1 if the extended double-precision floating-point value `a' is
5728 | equal to the corresponding value `b', and 0 otherwise. Quiet NaNs do not
5729 | cause an exception. The comparison is performed according to the IEC/IEEE
5730 | Standard for Binary Floating-Point Arithmetic.
5731 *----------------------------------------------------------------------------*/
5733 int floatx80_eq_quiet(floatx80 a, floatx80 b, float_status *status)
5736 if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
5737 && (uint64_t) ( extractFloatx80Frac( a )<<1 ) )
5738 || ( ( extractFloatx80Exp( b ) == 0x7FFF )
5739 && (uint64_t) ( extractFloatx80Frac( b )<<1 ) )
5741 if (floatx80_is_signaling_nan(a, status)
5742 || floatx80_is_signaling_nan(b, status)) {
5743 float_raise(float_flag_invalid, status);
5745 return 0;
5747 return
5748 ( a.low == b.low )
5749 && ( ( a.high == b.high )
5750 || ( ( a.low == 0 )
5751 && ( (uint16_t) ( ( a.high | b.high )<<1 ) == 0 ) )
5756 /*----------------------------------------------------------------------------
5757 | Returns 1 if the extended double-precision floating-point value `a' is less
5758 | than or equal to the corresponding value `b', and 0 otherwise. Quiet NaNs
5759 | do not cause an exception. Otherwise, the comparison is performed according
5760 | to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
5761 *----------------------------------------------------------------------------*/
5763 int floatx80_le_quiet(floatx80 a, floatx80 b, float_status *status)
5765 flag aSign, bSign;
5767 if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
5768 && (uint64_t) ( extractFloatx80Frac( a )<<1 ) )
5769 || ( ( extractFloatx80Exp( b ) == 0x7FFF )
5770 && (uint64_t) ( extractFloatx80Frac( b )<<1 ) )
5772 if (floatx80_is_signaling_nan(a, status)
5773 || floatx80_is_signaling_nan(b, status)) {
5774 float_raise(float_flag_invalid, status);
5776 return 0;
5778 aSign = extractFloatx80Sign( a );
5779 bSign = extractFloatx80Sign( b );
5780 if ( aSign != bSign ) {
5781 return
5782 aSign
5783 || ( ( ( (uint16_t) ( ( a.high | b.high )<<1 ) ) | a.low | b.low )
5784 == 0 );
5786 return
5787 aSign ? le128( b.high, b.low, a.high, a.low )
5788 : le128( a.high, a.low, b.high, b.low );
5792 /*----------------------------------------------------------------------------
5793 | Returns 1 if the extended double-precision floating-point value `a' is less
5794 | than the corresponding value `b', and 0 otherwise. Quiet NaNs do not cause
5795 | an exception. Otherwise, the comparison is performed according to the
5796 | IEC/IEEE Standard for Binary Floating-Point Arithmetic.
5797 *----------------------------------------------------------------------------*/
5799 int floatx80_lt_quiet(floatx80 a, floatx80 b, float_status *status)
5801 flag aSign, bSign;
5803 if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
5804 && (uint64_t) ( extractFloatx80Frac( a )<<1 ) )
5805 || ( ( extractFloatx80Exp( b ) == 0x7FFF )
5806 && (uint64_t) ( extractFloatx80Frac( b )<<1 ) )
5808 if (floatx80_is_signaling_nan(a, status)
5809 || floatx80_is_signaling_nan(b, status)) {
5810 float_raise(float_flag_invalid, status);
5812 return 0;
5814 aSign = extractFloatx80Sign( a );
5815 bSign = extractFloatx80Sign( b );
5816 if ( aSign != bSign ) {
5817 return
5818 aSign
5819 && ( ( ( (uint16_t) ( ( a.high | b.high )<<1 ) ) | a.low | b.low )
5820 != 0 );
5822 return
5823 aSign ? lt128( b.high, b.low, a.high, a.low )
5824 : lt128( a.high, a.low, b.high, b.low );
5828 /*----------------------------------------------------------------------------
5829 | Returns 1 if the extended double-precision floating-point values `a' and `b'
5830 | cannot be compared, and 0 otherwise. Quiet NaNs do not cause an exception.
5831 | The comparison is performed according to the IEC/IEEE Standard for Binary
5832 | Floating-Point Arithmetic.
5833 *----------------------------------------------------------------------------*/
5834 int floatx80_unordered_quiet(floatx80 a, floatx80 b, float_status *status)
5836 if ( ( ( extractFloatx80Exp( a ) == 0x7FFF )
5837 && (uint64_t) ( extractFloatx80Frac( a )<<1 ) )
5838 || ( ( extractFloatx80Exp( b ) == 0x7FFF )
5839 && (uint64_t) ( extractFloatx80Frac( b )<<1 ) )
5841 if (floatx80_is_signaling_nan(a, status)
5842 || floatx80_is_signaling_nan(b, status)) {
5843 float_raise(float_flag_invalid, status);
5845 return 1;
5847 return 0;
5850 /*----------------------------------------------------------------------------
5851 | Returns the result of converting the quadruple-precision floating-point
5852 | value `a' to the 32-bit two's complement integer format. The conversion
5853 | is performed according to the IEC/IEEE Standard for Binary Floating-Point
5854 | Arithmetic---which means in particular that the conversion is rounded
5855 | according to the current rounding mode. If `a' is a NaN, the largest
5856 | positive integer is returned. Otherwise, if the conversion overflows, the
5857 | largest integer with the same sign as `a' is returned.
5858 *----------------------------------------------------------------------------*/
5860 int32_t float128_to_int32(float128 a, float_status *status)
5862 flag aSign;
5863 int32_t aExp, shiftCount;
5864 uint64_t aSig0, aSig1;
5866 aSig1 = extractFloat128Frac1( a );
5867 aSig0 = extractFloat128Frac0( a );
5868 aExp = extractFloat128Exp( a );
5869 aSign = extractFloat128Sign( a );
5870 if ( ( aExp == 0x7FFF ) && ( aSig0 | aSig1 ) ) aSign = 0;
5871 if ( aExp ) aSig0 |= LIT64( 0x0001000000000000 );
5872 aSig0 |= ( aSig1 != 0 );
5873 shiftCount = 0x4028 - aExp;
5874 if ( 0 < shiftCount ) shift64RightJamming( aSig0, shiftCount, &aSig0 );
5875 return roundAndPackInt32(aSign, aSig0, status);
5879 /*----------------------------------------------------------------------------
5880 | Returns the result of converting the quadruple-precision floating-point
5881 | value `a' to the 32-bit two's complement integer format. The conversion
5882 | is performed according to the IEC/IEEE Standard for Binary Floating-Point
5883 | Arithmetic, except that the conversion is always rounded toward zero. If
5884 | `a' is a NaN, the largest positive integer is returned. Otherwise, if the
5885 | conversion overflows, the largest integer with the same sign as `a' is
5886 | returned.
5887 *----------------------------------------------------------------------------*/
5889 int32_t float128_to_int32_round_to_zero(float128 a, float_status *status)
5891 flag aSign;
5892 int32_t aExp, shiftCount;
5893 uint64_t aSig0, aSig1, savedASig;
5894 int32_t z;
5896 aSig1 = extractFloat128Frac1( a );
5897 aSig0 = extractFloat128Frac0( a );
5898 aExp = extractFloat128Exp( a );
5899 aSign = extractFloat128Sign( a );
5900 aSig0 |= ( aSig1 != 0 );
5901 if ( 0x401E < aExp ) {
5902 if ( ( aExp == 0x7FFF ) && aSig0 ) aSign = 0;
5903 goto invalid;
5905 else if ( aExp < 0x3FFF ) {
5906 if (aExp || aSig0) {
5907 status->float_exception_flags |= float_flag_inexact;
5909 return 0;
5911 aSig0 |= LIT64( 0x0001000000000000 );
5912 shiftCount = 0x402F - aExp;
5913 savedASig = aSig0;
5914 aSig0 >>= shiftCount;
5915 z = aSig0;
5916 if ( aSign ) z = - z;
5917 if ( ( z < 0 ) ^ aSign ) {
5918 invalid:
5919 float_raise(float_flag_invalid, status);
5920 return aSign ? (int32_t) 0x80000000 : 0x7FFFFFFF;
5922 if ( ( aSig0<<shiftCount ) != savedASig ) {
5923 status->float_exception_flags |= float_flag_inexact;
5925 return z;
5929 /*----------------------------------------------------------------------------
5930 | Returns the result of converting the quadruple-precision floating-point
5931 | value `a' to the 64-bit two's complement integer format. The conversion
5932 | is performed according to the IEC/IEEE Standard for Binary Floating-Point
5933 | Arithmetic---which means in particular that the conversion is rounded
5934 | according to the current rounding mode. If `a' is a NaN, the largest
5935 | positive integer is returned. Otherwise, if the conversion overflows, the
5936 | largest integer with the same sign as `a' is returned.
5937 *----------------------------------------------------------------------------*/
5939 int64_t float128_to_int64(float128 a, float_status *status)
5941 flag aSign;
5942 int32_t aExp, shiftCount;
5943 uint64_t aSig0, aSig1;
5945 aSig1 = extractFloat128Frac1( a );
5946 aSig0 = extractFloat128Frac0( a );
5947 aExp = extractFloat128Exp( a );
5948 aSign = extractFloat128Sign( a );
5949 if ( aExp ) aSig0 |= LIT64( 0x0001000000000000 );
5950 shiftCount = 0x402F - aExp;
5951 if ( shiftCount <= 0 ) {
5952 if ( 0x403E < aExp ) {
5953 float_raise(float_flag_invalid, status);
5954 if ( ! aSign
5955 || ( ( aExp == 0x7FFF )
5956 && ( aSig1 || ( aSig0 != LIT64( 0x0001000000000000 ) ) )
5959 return LIT64( 0x7FFFFFFFFFFFFFFF );
5961 return (int64_t) LIT64( 0x8000000000000000 );
5963 shortShift128Left( aSig0, aSig1, - shiftCount, &aSig0, &aSig1 );
5965 else {
5966 shift64ExtraRightJamming( aSig0, aSig1, shiftCount, &aSig0, &aSig1 );
5968 return roundAndPackInt64(aSign, aSig0, aSig1, status);
5972 /*----------------------------------------------------------------------------
5973 | Returns the result of converting the quadruple-precision floating-point
5974 | value `a' to the 64-bit two's complement integer format. The conversion
5975 | is performed according to the IEC/IEEE Standard for Binary Floating-Point
5976 | Arithmetic, except that the conversion is always rounded toward zero.
5977 | If `a' is a NaN, the largest positive integer is returned. Otherwise, if
5978 | the conversion overflows, the largest integer with the same sign as `a' is
5979 | returned.
5980 *----------------------------------------------------------------------------*/
5982 int64_t float128_to_int64_round_to_zero(float128 a, float_status *status)
5984 flag aSign;
5985 int32_t aExp, shiftCount;
5986 uint64_t aSig0, aSig1;
5987 int64_t z;
5989 aSig1 = extractFloat128Frac1( a );
5990 aSig0 = extractFloat128Frac0( a );
5991 aExp = extractFloat128Exp( a );
5992 aSign = extractFloat128Sign( a );
5993 if ( aExp ) aSig0 |= LIT64( 0x0001000000000000 );
5994 shiftCount = aExp - 0x402F;
5995 if ( 0 < shiftCount ) {
5996 if ( 0x403E <= aExp ) {
5997 aSig0 &= LIT64( 0x0000FFFFFFFFFFFF );
5998 if ( ( a.high == LIT64( 0xC03E000000000000 ) )
5999 && ( aSig1 < LIT64( 0x0002000000000000 ) ) ) {
6000 if (aSig1) {
6001 status->float_exception_flags |= float_flag_inexact;
6004 else {
6005 float_raise(float_flag_invalid, status);
6006 if ( ! aSign || ( ( aExp == 0x7FFF ) && ( aSig0 | aSig1 ) ) ) {
6007 return LIT64( 0x7FFFFFFFFFFFFFFF );
6010 return (int64_t) LIT64( 0x8000000000000000 );
6012 z = ( aSig0<<shiftCount ) | ( aSig1>>( ( - shiftCount ) & 63 ) );
6013 if ( (uint64_t) ( aSig1<<shiftCount ) ) {
6014 status->float_exception_flags |= float_flag_inexact;
6017 else {
6018 if ( aExp < 0x3FFF ) {
6019 if ( aExp | aSig0 | aSig1 ) {
6020 status->float_exception_flags |= float_flag_inexact;
6022 return 0;
6024 z = aSig0>>( - shiftCount );
6025 if ( aSig1
6026 || ( shiftCount && (uint64_t) ( aSig0<<( shiftCount & 63 ) ) ) ) {
6027 status->float_exception_flags |= float_flag_inexact;
6030 if ( aSign ) z = - z;
6031 return z;
6035 /*----------------------------------------------------------------------------
6036 | Returns the result of converting the quadruple-precision floating-point
6037 | value `a' to the single-precision floating-point format. The conversion
6038 | is performed according to the IEC/IEEE Standard for Binary Floating-Point
6039 | Arithmetic.
6040 *----------------------------------------------------------------------------*/
6042 float32 float128_to_float32(float128 a, float_status *status)
6044 flag aSign;
6045 int32_t aExp;
6046 uint64_t aSig0, aSig1;
6047 uint32_t zSig;
6049 aSig1 = extractFloat128Frac1( a );
6050 aSig0 = extractFloat128Frac0( a );
6051 aExp = extractFloat128Exp( a );
6052 aSign = extractFloat128Sign( a );
6053 if ( aExp == 0x7FFF ) {
6054 if ( aSig0 | aSig1 ) {
6055 return commonNaNToFloat32(float128ToCommonNaN(a, status), status);
6057 return packFloat32( aSign, 0xFF, 0 );
6059 aSig0 |= ( aSig1 != 0 );
6060 shift64RightJamming( aSig0, 18, &aSig0 );
6061 zSig = aSig0;
6062 if ( aExp || zSig ) {
6063 zSig |= 0x40000000;
6064 aExp -= 0x3F81;
6066 return roundAndPackFloat32(aSign, aExp, zSig, status);
6070 /*----------------------------------------------------------------------------
6071 | Returns the result of converting the quadruple-precision floating-point
6072 | value `a' to the double-precision floating-point format. The conversion
6073 | is performed according to the IEC/IEEE Standard for Binary Floating-Point
6074 | Arithmetic.
6075 *----------------------------------------------------------------------------*/
6077 float64 float128_to_float64(float128 a, float_status *status)
6079 flag aSign;
6080 int32_t aExp;
6081 uint64_t aSig0, aSig1;
6083 aSig1 = extractFloat128Frac1( a );
6084 aSig0 = extractFloat128Frac0( a );
6085 aExp = extractFloat128Exp( a );
6086 aSign = extractFloat128Sign( a );
6087 if ( aExp == 0x7FFF ) {
6088 if ( aSig0 | aSig1 ) {
6089 return commonNaNToFloat64(float128ToCommonNaN(a, status), status);
6091 return packFloat64( aSign, 0x7FF, 0 );
6093 shortShift128Left( aSig0, aSig1, 14, &aSig0, &aSig1 );
6094 aSig0 |= ( aSig1 != 0 );
6095 if ( aExp || aSig0 ) {
6096 aSig0 |= LIT64( 0x4000000000000000 );
6097 aExp -= 0x3C01;
6099 return roundAndPackFloat64(aSign, aExp, aSig0, status);
6103 /*----------------------------------------------------------------------------
6104 | Returns the result of converting the quadruple-precision floating-point
6105 | value `a' to the extended double-precision floating-point format. The
6106 | conversion is performed according to the IEC/IEEE Standard for Binary
6107 | Floating-Point Arithmetic.
6108 *----------------------------------------------------------------------------*/
6110 floatx80 float128_to_floatx80(float128 a, float_status *status)
6112 flag aSign;
6113 int32_t aExp;
6114 uint64_t aSig0, aSig1;
6116 aSig1 = extractFloat128Frac1( a );
6117 aSig0 = extractFloat128Frac0( a );
6118 aExp = extractFloat128Exp( a );
6119 aSign = extractFloat128Sign( a );
6120 if ( aExp == 0x7FFF ) {
6121 if ( aSig0 | aSig1 ) {
6122 return commonNaNToFloatx80(float128ToCommonNaN(a, status), status);
6124 return packFloatx80( aSign, 0x7FFF, LIT64( 0x8000000000000000 ) );
6126 if ( aExp == 0 ) {
6127 if ( ( aSig0 | aSig1 ) == 0 ) return packFloatx80( aSign, 0, 0 );
6128 normalizeFloat128Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 );
6130 else {
6131 aSig0 |= LIT64( 0x0001000000000000 );
6133 shortShift128Left( aSig0, aSig1, 15, &aSig0, &aSig1 );
6134 return roundAndPackFloatx80(80, aSign, aExp, aSig0, aSig1, status);
6138 /*----------------------------------------------------------------------------
6139 | Rounds the quadruple-precision floating-point value `a' to an integer, and
6140 | returns the result as a quadruple-precision floating-point value. The
6141 | operation is performed according to the IEC/IEEE Standard for Binary
6142 | Floating-Point Arithmetic.
6143 *----------------------------------------------------------------------------*/
6145 float128 float128_round_to_int(float128 a, float_status *status)
6147 flag aSign;
6148 int32_t aExp;
6149 uint64_t lastBitMask, roundBitsMask;
6150 float128 z;
6152 aExp = extractFloat128Exp( a );
6153 if ( 0x402F <= aExp ) {
6154 if ( 0x406F <= aExp ) {
6155 if ( ( aExp == 0x7FFF )
6156 && ( extractFloat128Frac0( a ) | extractFloat128Frac1( a ) )
6158 return propagateFloat128NaN(a, a, status);
6160 return a;
6162 lastBitMask = 1;
6163 lastBitMask = ( lastBitMask<<( 0x406E - aExp ) )<<1;
6164 roundBitsMask = lastBitMask - 1;
6165 z = a;
6166 switch (status->float_rounding_mode) {
6167 case float_round_nearest_even:
6168 if ( lastBitMask ) {
6169 add128( z.high, z.low, 0, lastBitMask>>1, &z.high, &z.low );
6170 if ( ( z.low & roundBitsMask ) == 0 ) z.low &= ~ lastBitMask;
6172 else {
6173 if ( (int64_t) z.low < 0 ) {
6174 ++z.high;
6175 if ( (uint64_t) ( z.low<<1 ) == 0 ) z.high &= ~1;
6178 break;
6179 case float_round_ties_away:
6180 if (lastBitMask) {
6181 add128(z.high, z.low, 0, lastBitMask >> 1, &z.high, &z.low);
6182 } else {
6183 if ((int64_t) z.low < 0) {
6184 ++z.high;
6187 break;
6188 case float_round_to_zero:
6189 break;
6190 case float_round_up:
6191 if (!extractFloat128Sign(z)) {
6192 add128(z.high, z.low, 0, roundBitsMask, &z.high, &z.low);
6194 break;
6195 case float_round_down:
6196 if (extractFloat128Sign(z)) {
6197 add128(z.high, z.low, 0, roundBitsMask, &z.high, &z.low);
6199 break;
6200 default:
6201 abort();
6203 z.low &= ~ roundBitsMask;
6205 else {
6206 if ( aExp < 0x3FFF ) {
6207 if ( ( ( (uint64_t) ( a.high<<1 ) ) | a.low ) == 0 ) return a;
6208 status->float_exception_flags |= float_flag_inexact;
6209 aSign = extractFloat128Sign( a );
6210 switch (status->float_rounding_mode) {
6211 case float_round_nearest_even:
6212 if ( ( aExp == 0x3FFE )
6213 && ( extractFloat128Frac0( a )
6214 | extractFloat128Frac1( a ) )
6216 return packFloat128( aSign, 0x3FFF, 0, 0 );
6218 break;
6219 case float_round_ties_away:
6220 if (aExp == 0x3FFE) {
6221 return packFloat128(aSign, 0x3FFF, 0, 0);
6223 break;
6224 case float_round_down:
6225 return
6226 aSign ? packFloat128( 1, 0x3FFF, 0, 0 )
6227 : packFloat128( 0, 0, 0, 0 );
6228 case float_round_up:
6229 return
6230 aSign ? packFloat128( 1, 0, 0, 0 )
6231 : packFloat128( 0, 0x3FFF, 0, 0 );
6233 return packFloat128( aSign, 0, 0, 0 );
6235 lastBitMask = 1;
6236 lastBitMask <<= 0x402F - aExp;
6237 roundBitsMask = lastBitMask - 1;
6238 z.low = 0;
6239 z.high = a.high;
6240 switch (status->float_rounding_mode) {
6241 case float_round_nearest_even:
6242 z.high += lastBitMask>>1;
6243 if ( ( ( z.high & roundBitsMask ) | a.low ) == 0 ) {
6244 z.high &= ~ lastBitMask;
6246 break;
6247 case float_round_ties_away:
6248 z.high += lastBitMask>>1;
6249 break;
6250 case float_round_to_zero:
6251 break;
6252 case float_round_up:
6253 if (!extractFloat128Sign(z)) {
6254 z.high |= ( a.low != 0 );
6255 z.high += roundBitsMask;
6257 break;
6258 case float_round_down:
6259 if (extractFloat128Sign(z)) {
6260 z.high |= (a.low != 0);
6261 z.high += roundBitsMask;
6263 break;
6264 default:
6265 abort();
6267 z.high &= ~ roundBitsMask;
6269 if ( ( z.low != a.low ) || ( z.high != a.high ) ) {
6270 status->float_exception_flags |= float_flag_inexact;
6272 return z;
6276 /*----------------------------------------------------------------------------
6277 | Returns the result of adding the absolute values of the quadruple-precision
6278 | floating-point values `a' and `b'. If `zSign' is 1, the sum is negated
6279 | before being returned. `zSign' is ignored if the result is a NaN.
6280 | The addition is performed according to the IEC/IEEE Standard for Binary
6281 | Floating-Point Arithmetic.
6282 *----------------------------------------------------------------------------*/
6284 static float128 addFloat128Sigs(float128 a, float128 b, flag zSign,
6285 float_status *status)
6287 int32_t aExp, bExp, zExp;
6288 uint64_t aSig0, aSig1, bSig0, bSig1, zSig0, zSig1, zSig2;
6289 int32_t expDiff;
6291 aSig1 = extractFloat128Frac1( a );
6292 aSig0 = extractFloat128Frac0( a );
6293 aExp = extractFloat128Exp( a );
6294 bSig1 = extractFloat128Frac1( b );
6295 bSig0 = extractFloat128Frac0( b );
6296 bExp = extractFloat128Exp( b );
6297 expDiff = aExp - bExp;
6298 if ( 0 < expDiff ) {
6299 if ( aExp == 0x7FFF ) {
6300 if (aSig0 | aSig1) {
6301 return propagateFloat128NaN(a, b, status);
6303 return a;
6305 if ( bExp == 0 ) {
6306 --expDiff;
6308 else {
6309 bSig0 |= LIT64( 0x0001000000000000 );
6311 shift128ExtraRightJamming(
6312 bSig0, bSig1, 0, expDiff, &bSig0, &bSig1, &zSig2 );
6313 zExp = aExp;
6315 else if ( expDiff < 0 ) {
6316 if ( bExp == 0x7FFF ) {
6317 if (bSig0 | bSig1) {
6318 return propagateFloat128NaN(a, b, status);
6320 return packFloat128( zSign, 0x7FFF, 0, 0 );
6322 if ( aExp == 0 ) {
6323 ++expDiff;
6325 else {
6326 aSig0 |= LIT64( 0x0001000000000000 );
6328 shift128ExtraRightJamming(
6329 aSig0, aSig1, 0, - expDiff, &aSig0, &aSig1, &zSig2 );
6330 zExp = bExp;
6332 else {
6333 if ( aExp == 0x7FFF ) {
6334 if ( aSig0 | aSig1 | bSig0 | bSig1 ) {
6335 return propagateFloat128NaN(a, b, status);
6337 return a;
6339 add128( aSig0, aSig1, bSig0, bSig1, &zSig0, &zSig1 );
6340 if ( aExp == 0 ) {
6341 if (status->flush_to_zero) {
6342 if (zSig0 | zSig1) {
6343 float_raise(float_flag_output_denormal, status);
6345 return packFloat128(zSign, 0, 0, 0);
6347 return packFloat128( zSign, 0, zSig0, zSig1 );
6349 zSig2 = 0;
6350 zSig0 |= LIT64( 0x0002000000000000 );
6351 zExp = aExp;
6352 goto shiftRight1;
6354 aSig0 |= LIT64( 0x0001000000000000 );
6355 add128( aSig0, aSig1, bSig0, bSig1, &zSig0, &zSig1 );
6356 --zExp;
6357 if ( zSig0 < LIT64( 0x0002000000000000 ) ) goto roundAndPack;
6358 ++zExp;
6359 shiftRight1:
6360 shift128ExtraRightJamming(
6361 zSig0, zSig1, zSig2, 1, &zSig0, &zSig1, &zSig2 );
6362 roundAndPack:
6363 return roundAndPackFloat128(zSign, zExp, zSig0, zSig1, zSig2, status);
6367 /*----------------------------------------------------------------------------
6368 | Returns the result of subtracting the absolute values of the quadruple-
6369 | precision floating-point values `a' and `b'. If `zSign' is 1, the
6370 | difference is negated before being returned. `zSign' is ignored if the
6371 | result is a NaN. The subtraction is performed according to the IEC/IEEE
6372 | Standard for Binary Floating-Point Arithmetic.
6373 *----------------------------------------------------------------------------*/
6375 static float128 subFloat128Sigs(float128 a, float128 b, flag zSign,
6376 float_status *status)
6378 int32_t aExp, bExp, zExp;
6379 uint64_t aSig0, aSig1, bSig0, bSig1, zSig0, zSig1;
6380 int32_t expDiff;
6382 aSig1 = extractFloat128Frac1( a );
6383 aSig0 = extractFloat128Frac0( a );
6384 aExp = extractFloat128Exp( a );
6385 bSig1 = extractFloat128Frac1( b );
6386 bSig0 = extractFloat128Frac0( b );
6387 bExp = extractFloat128Exp( b );
6388 expDiff = aExp - bExp;
6389 shortShift128Left( aSig0, aSig1, 14, &aSig0, &aSig1 );
6390 shortShift128Left( bSig0, bSig1, 14, &bSig0, &bSig1 );
6391 if ( 0 < expDiff ) goto aExpBigger;
6392 if ( expDiff < 0 ) goto bExpBigger;
6393 if ( aExp == 0x7FFF ) {
6394 if ( aSig0 | aSig1 | bSig0 | bSig1 ) {
6395 return propagateFloat128NaN(a, b, status);
6397 float_raise(float_flag_invalid, status);
6398 return float128_default_nan(status);
6400 if ( aExp == 0 ) {
6401 aExp = 1;
6402 bExp = 1;
6404 if ( bSig0 < aSig0 ) goto aBigger;
6405 if ( aSig0 < bSig0 ) goto bBigger;
6406 if ( bSig1 < aSig1 ) goto aBigger;
6407 if ( aSig1 < bSig1 ) goto bBigger;
6408 return packFloat128(status->float_rounding_mode == float_round_down,
6409 0, 0, 0);
6410 bExpBigger:
6411 if ( bExp == 0x7FFF ) {
6412 if (bSig0 | bSig1) {
6413 return propagateFloat128NaN(a, b, status);
6415 return packFloat128( zSign ^ 1, 0x7FFF, 0, 0 );
6417 if ( aExp == 0 ) {
6418 ++expDiff;
6420 else {
6421 aSig0 |= LIT64( 0x4000000000000000 );
6423 shift128RightJamming( aSig0, aSig1, - expDiff, &aSig0, &aSig1 );
6424 bSig0 |= LIT64( 0x4000000000000000 );
6425 bBigger:
6426 sub128( bSig0, bSig1, aSig0, aSig1, &zSig0, &zSig1 );
6427 zExp = bExp;
6428 zSign ^= 1;
6429 goto normalizeRoundAndPack;
6430 aExpBigger:
6431 if ( aExp == 0x7FFF ) {
6432 if (aSig0 | aSig1) {
6433 return propagateFloat128NaN(a, b, status);
6435 return a;
6437 if ( bExp == 0 ) {
6438 --expDiff;
6440 else {
6441 bSig0 |= LIT64( 0x4000000000000000 );
6443 shift128RightJamming( bSig0, bSig1, expDiff, &bSig0, &bSig1 );
6444 aSig0 |= LIT64( 0x4000000000000000 );
6445 aBigger:
6446 sub128( aSig0, aSig1, bSig0, bSig1, &zSig0, &zSig1 );
6447 zExp = aExp;
6448 normalizeRoundAndPack:
6449 --zExp;
6450 return normalizeRoundAndPackFloat128(zSign, zExp - 14, zSig0, zSig1,
6451 status);
6455 /*----------------------------------------------------------------------------
6456 | Returns the result of adding the quadruple-precision floating-point values
6457 | `a' and `b'. The operation is performed according to the IEC/IEEE Standard
6458 | for Binary Floating-Point Arithmetic.
6459 *----------------------------------------------------------------------------*/
6461 float128 float128_add(float128 a, float128 b, float_status *status)
6463 flag aSign, bSign;
6465 aSign = extractFloat128Sign( a );
6466 bSign = extractFloat128Sign( b );
6467 if ( aSign == bSign ) {
6468 return addFloat128Sigs(a, b, aSign, status);
6470 else {
6471 return subFloat128Sigs(a, b, aSign, status);
6476 /*----------------------------------------------------------------------------
6477 | Returns the result of subtracting the quadruple-precision floating-point
6478 | values `a' and `b'. The operation is performed according to the IEC/IEEE
6479 | Standard for Binary Floating-Point Arithmetic.
6480 *----------------------------------------------------------------------------*/
6482 float128 float128_sub(float128 a, float128 b, float_status *status)
6484 flag aSign, bSign;
6486 aSign = extractFloat128Sign( a );
6487 bSign = extractFloat128Sign( b );
6488 if ( aSign == bSign ) {
6489 return subFloat128Sigs(a, b, aSign, status);
6491 else {
6492 return addFloat128Sigs(a, b, aSign, status);
6497 /*----------------------------------------------------------------------------
6498 | Returns the result of multiplying the quadruple-precision floating-point
6499 | values `a' and `b'. The operation is performed according to the IEC/IEEE
6500 | Standard for Binary Floating-Point Arithmetic.
6501 *----------------------------------------------------------------------------*/
6503 float128 float128_mul(float128 a, float128 b, float_status *status)
6505 flag aSign, bSign, zSign;
6506 int32_t aExp, bExp, zExp;
6507 uint64_t aSig0, aSig1, bSig0, bSig1, zSig0, zSig1, zSig2, zSig3;
6509 aSig1 = extractFloat128Frac1( a );
6510 aSig0 = extractFloat128Frac0( a );
6511 aExp = extractFloat128Exp( a );
6512 aSign = extractFloat128Sign( a );
6513 bSig1 = extractFloat128Frac1( b );
6514 bSig0 = extractFloat128Frac0( b );
6515 bExp = extractFloat128Exp( b );
6516 bSign = extractFloat128Sign( b );
6517 zSign = aSign ^ bSign;
6518 if ( aExp == 0x7FFF ) {
6519 if ( ( aSig0 | aSig1 )
6520 || ( ( bExp == 0x7FFF ) && ( bSig0 | bSig1 ) ) ) {
6521 return propagateFloat128NaN(a, b, status);
6523 if ( ( bExp | bSig0 | bSig1 ) == 0 ) goto invalid;
6524 return packFloat128( zSign, 0x7FFF, 0, 0 );
6526 if ( bExp == 0x7FFF ) {
6527 if (bSig0 | bSig1) {
6528 return propagateFloat128NaN(a, b, status);
6530 if ( ( aExp | aSig0 | aSig1 ) == 0 ) {
6531 invalid:
6532 float_raise(float_flag_invalid, status);
6533 return float128_default_nan(status);
6535 return packFloat128( zSign, 0x7FFF, 0, 0 );
6537 if ( aExp == 0 ) {
6538 if ( ( aSig0 | aSig1 ) == 0 ) return packFloat128( zSign, 0, 0, 0 );
6539 normalizeFloat128Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 );
6541 if ( bExp == 0 ) {
6542 if ( ( bSig0 | bSig1 ) == 0 ) return packFloat128( zSign, 0, 0, 0 );
6543 normalizeFloat128Subnormal( bSig0, bSig1, &bExp, &bSig0, &bSig1 );
6545 zExp = aExp + bExp - 0x4000;
6546 aSig0 |= LIT64( 0x0001000000000000 );
6547 shortShift128Left( bSig0, bSig1, 16, &bSig0, &bSig1 );
6548 mul128To256( aSig0, aSig1, bSig0, bSig1, &zSig0, &zSig1, &zSig2, &zSig3 );
6549 add128( zSig0, zSig1, aSig0, aSig1, &zSig0, &zSig1 );
6550 zSig2 |= ( zSig3 != 0 );
6551 if ( LIT64( 0x0002000000000000 ) <= zSig0 ) {
6552 shift128ExtraRightJamming(
6553 zSig0, zSig1, zSig2, 1, &zSig0, &zSig1, &zSig2 );
6554 ++zExp;
6556 return roundAndPackFloat128(zSign, zExp, zSig0, zSig1, zSig2, status);
6560 /*----------------------------------------------------------------------------
6561 | Returns the result of dividing the quadruple-precision floating-point value
6562 | `a' by the corresponding value `b'. The operation is performed according to
6563 | the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
6564 *----------------------------------------------------------------------------*/
6566 float128 float128_div(float128 a, float128 b, float_status *status)
6568 flag aSign, bSign, zSign;
6569 int32_t aExp, bExp, zExp;
6570 uint64_t aSig0, aSig1, bSig0, bSig1, zSig0, zSig1, zSig2;
6571 uint64_t rem0, rem1, rem2, rem3, term0, term1, term2, term3;
6573 aSig1 = extractFloat128Frac1( a );
6574 aSig0 = extractFloat128Frac0( a );
6575 aExp = extractFloat128Exp( a );
6576 aSign = extractFloat128Sign( a );
6577 bSig1 = extractFloat128Frac1( b );
6578 bSig0 = extractFloat128Frac0( b );
6579 bExp = extractFloat128Exp( b );
6580 bSign = extractFloat128Sign( b );
6581 zSign = aSign ^ bSign;
6582 if ( aExp == 0x7FFF ) {
6583 if (aSig0 | aSig1) {
6584 return propagateFloat128NaN(a, b, status);
6586 if ( bExp == 0x7FFF ) {
6587 if (bSig0 | bSig1) {
6588 return propagateFloat128NaN(a, b, status);
6590 goto invalid;
6592 return packFloat128( zSign, 0x7FFF, 0, 0 );
6594 if ( bExp == 0x7FFF ) {
6595 if (bSig0 | bSig1) {
6596 return propagateFloat128NaN(a, b, status);
6598 return packFloat128( zSign, 0, 0, 0 );
6600 if ( bExp == 0 ) {
6601 if ( ( bSig0 | bSig1 ) == 0 ) {
6602 if ( ( aExp | aSig0 | aSig1 ) == 0 ) {
6603 invalid:
6604 float_raise(float_flag_invalid, status);
6605 return float128_default_nan(status);
6607 float_raise(float_flag_divbyzero, status);
6608 return packFloat128( zSign, 0x7FFF, 0, 0 );
6610 normalizeFloat128Subnormal( bSig0, bSig1, &bExp, &bSig0, &bSig1 );
6612 if ( aExp == 0 ) {
6613 if ( ( aSig0 | aSig1 ) == 0 ) return packFloat128( zSign, 0, 0, 0 );
6614 normalizeFloat128Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 );
6616 zExp = aExp - bExp + 0x3FFD;
6617 shortShift128Left(
6618 aSig0 | LIT64( 0x0001000000000000 ), aSig1, 15, &aSig0, &aSig1 );
6619 shortShift128Left(
6620 bSig0 | LIT64( 0x0001000000000000 ), bSig1, 15, &bSig0, &bSig1 );
6621 if ( le128( bSig0, bSig1, aSig0, aSig1 ) ) {
6622 shift128Right( aSig0, aSig1, 1, &aSig0, &aSig1 );
6623 ++zExp;
6625 zSig0 = estimateDiv128To64( aSig0, aSig1, bSig0 );
6626 mul128By64To192( bSig0, bSig1, zSig0, &term0, &term1, &term2 );
6627 sub192( aSig0, aSig1, 0, term0, term1, term2, &rem0, &rem1, &rem2 );
6628 while ( (int64_t) rem0 < 0 ) {
6629 --zSig0;
6630 add192( rem0, rem1, rem2, 0, bSig0, bSig1, &rem0, &rem1, &rem2 );
6632 zSig1 = estimateDiv128To64( rem1, rem2, bSig0 );
6633 if ( ( zSig1 & 0x3FFF ) <= 4 ) {
6634 mul128By64To192( bSig0, bSig1, zSig1, &term1, &term2, &term3 );
6635 sub192( rem1, rem2, 0, term1, term2, term3, &rem1, &rem2, &rem3 );
6636 while ( (int64_t) rem1 < 0 ) {
6637 --zSig1;
6638 add192( rem1, rem2, rem3, 0, bSig0, bSig1, &rem1, &rem2, &rem3 );
6640 zSig1 |= ( ( rem1 | rem2 | rem3 ) != 0 );
6642 shift128ExtraRightJamming( zSig0, zSig1, 0, 15, &zSig0, &zSig1, &zSig2 );
6643 return roundAndPackFloat128(zSign, zExp, zSig0, zSig1, zSig2, status);
6647 /*----------------------------------------------------------------------------
6648 | Returns the remainder of the quadruple-precision floating-point value `a'
6649 | with respect to the corresponding value `b'. The operation is performed
6650 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
6651 *----------------------------------------------------------------------------*/
6653 float128 float128_rem(float128 a, float128 b, float_status *status)
6655 flag aSign, zSign;
6656 int32_t aExp, bExp, expDiff;
6657 uint64_t aSig0, aSig1, bSig0, bSig1, q, term0, term1, term2;
6658 uint64_t allZero, alternateASig0, alternateASig1, sigMean1;
6659 int64_t sigMean0;
6661 aSig1 = extractFloat128Frac1( a );
6662 aSig0 = extractFloat128Frac0( a );
6663 aExp = extractFloat128Exp( a );
6664 aSign = extractFloat128Sign( a );
6665 bSig1 = extractFloat128Frac1( b );
6666 bSig0 = extractFloat128Frac0( b );
6667 bExp = extractFloat128Exp( b );
6668 if ( aExp == 0x7FFF ) {
6669 if ( ( aSig0 | aSig1 )
6670 || ( ( bExp == 0x7FFF ) && ( bSig0 | bSig1 ) ) ) {
6671 return propagateFloat128NaN(a, b, status);
6673 goto invalid;
6675 if ( bExp == 0x7FFF ) {
6676 if (bSig0 | bSig1) {
6677 return propagateFloat128NaN(a, b, status);
6679 return a;
6681 if ( bExp == 0 ) {
6682 if ( ( bSig0 | bSig1 ) == 0 ) {
6683 invalid:
6684 float_raise(float_flag_invalid, status);
6685 return float128_default_nan(status);
6687 normalizeFloat128Subnormal( bSig0, bSig1, &bExp, &bSig0, &bSig1 );
6689 if ( aExp == 0 ) {
6690 if ( ( aSig0 | aSig1 ) == 0 ) return a;
6691 normalizeFloat128Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 );
6693 expDiff = aExp - bExp;
6694 if ( expDiff < -1 ) return a;
6695 shortShift128Left(
6696 aSig0 | LIT64( 0x0001000000000000 ),
6697 aSig1,
6698 15 - ( expDiff < 0 ),
6699 &aSig0,
6700 &aSig1
6702 shortShift128Left(
6703 bSig0 | LIT64( 0x0001000000000000 ), bSig1, 15, &bSig0, &bSig1 );
6704 q = le128( bSig0, bSig1, aSig0, aSig1 );
6705 if ( q ) sub128( aSig0, aSig1, bSig0, bSig1, &aSig0, &aSig1 );
6706 expDiff -= 64;
6707 while ( 0 < expDiff ) {
6708 q = estimateDiv128To64( aSig0, aSig1, bSig0 );
6709 q = ( 4 < q ) ? q - 4 : 0;
6710 mul128By64To192( bSig0, bSig1, q, &term0, &term1, &term2 );
6711 shortShift192Left( term0, term1, term2, 61, &term1, &term2, &allZero );
6712 shortShift128Left( aSig0, aSig1, 61, &aSig0, &allZero );
6713 sub128( aSig0, 0, term1, term2, &aSig0, &aSig1 );
6714 expDiff -= 61;
6716 if ( -64 < expDiff ) {
6717 q = estimateDiv128To64( aSig0, aSig1, bSig0 );
6718 q = ( 4 < q ) ? q - 4 : 0;
6719 q >>= - expDiff;
6720 shift128Right( bSig0, bSig1, 12, &bSig0, &bSig1 );
6721 expDiff += 52;
6722 if ( expDiff < 0 ) {
6723 shift128Right( aSig0, aSig1, - expDiff, &aSig0, &aSig1 );
6725 else {
6726 shortShift128Left( aSig0, aSig1, expDiff, &aSig0, &aSig1 );
6728 mul128By64To192( bSig0, bSig1, q, &term0, &term1, &term2 );
6729 sub128( aSig0, aSig1, term1, term2, &aSig0, &aSig1 );
6731 else {
6732 shift128Right( aSig0, aSig1, 12, &aSig0, &aSig1 );
6733 shift128Right( bSig0, bSig1, 12, &bSig0, &bSig1 );
6735 do {
6736 alternateASig0 = aSig0;
6737 alternateASig1 = aSig1;
6738 ++q;
6739 sub128( aSig0, aSig1, bSig0, bSig1, &aSig0, &aSig1 );
6740 } while ( 0 <= (int64_t) aSig0 );
6741 add128(
6742 aSig0, aSig1, alternateASig0, alternateASig1, (uint64_t *)&sigMean0, &sigMean1 );
6743 if ( ( sigMean0 < 0 )
6744 || ( ( ( sigMean0 | sigMean1 ) == 0 ) && ( q & 1 ) ) ) {
6745 aSig0 = alternateASig0;
6746 aSig1 = alternateASig1;
6748 zSign = ( (int64_t) aSig0 < 0 );
6749 if ( zSign ) sub128( 0, 0, aSig0, aSig1, &aSig0, &aSig1 );
6750 return normalizeRoundAndPackFloat128(aSign ^ zSign, bExp - 4, aSig0, aSig1,
6751 status);
6754 /*----------------------------------------------------------------------------
6755 | Returns the square root of the quadruple-precision floating-point value `a'.
6756 | The operation is performed according to the IEC/IEEE Standard for Binary
6757 | Floating-Point Arithmetic.
6758 *----------------------------------------------------------------------------*/
6760 float128 float128_sqrt(float128 a, float_status *status)
6762 flag aSign;
6763 int32_t aExp, zExp;
6764 uint64_t aSig0, aSig1, zSig0, zSig1, zSig2, doubleZSig0;
6765 uint64_t rem0, rem1, rem2, rem3, term0, term1, term2, term3;
6767 aSig1 = extractFloat128Frac1( a );
6768 aSig0 = extractFloat128Frac0( a );
6769 aExp = extractFloat128Exp( a );
6770 aSign = extractFloat128Sign( a );
6771 if ( aExp == 0x7FFF ) {
6772 if (aSig0 | aSig1) {
6773 return propagateFloat128NaN(a, a, status);
6775 if ( ! aSign ) return a;
6776 goto invalid;
6778 if ( aSign ) {
6779 if ( ( aExp | aSig0 | aSig1 ) == 0 ) return a;
6780 invalid:
6781 float_raise(float_flag_invalid, status);
6782 return float128_default_nan(status);
6784 if ( aExp == 0 ) {
6785 if ( ( aSig0 | aSig1 ) == 0 ) return packFloat128( 0, 0, 0, 0 );
6786 normalizeFloat128Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 );
6788 zExp = ( ( aExp - 0x3FFF )>>1 ) + 0x3FFE;
6789 aSig0 |= LIT64( 0x0001000000000000 );
6790 zSig0 = estimateSqrt32( aExp, aSig0>>17 );
6791 shortShift128Left( aSig0, aSig1, 13 - ( aExp & 1 ), &aSig0, &aSig1 );
6792 zSig0 = estimateDiv128To64( aSig0, aSig1, zSig0<<32 ) + ( zSig0<<30 );
6793 doubleZSig0 = zSig0<<1;
6794 mul64To128( zSig0, zSig0, &term0, &term1 );
6795 sub128( aSig0, aSig1, term0, term1, &rem0, &rem1 );
6796 while ( (int64_t) rem0 < 0 ) {
6797 --zSig0;
6798 doubleZSig0 -= 2;
6799 add128( rem0, rem1, zSig0>>63, doubleZSig0 | 1, &rem0, &rem1 );
6801 zSig1 = estimateDiv128To64( rem1, 0, doubleZSig0 );
6802 if ( ( zSig1 & 0x1FFF ) <= 5 ) {
6803 if ( zSig1 == 0 ) zSig1 = 1;
6804 mul64To128( doubleZSig0, zSig1, &term1, &term2 );
6805 sub128( rem1, 0, term1, term2, &rem1, &rem2 );
6806 mul64To128( zSig1, zSig1, &term2, &term3 );
6807 sub192( rem1, rem2, 0, 0, term2, term3, &rem1, &rem2, &rem3 );
6808 while ( (int64_t) rem1 < 0 ) {
6809 --zSig1;
6810 shortShift128Left( 0, zSig1, 1, &term2, &term3 );
6811 term3 |= 1;
6812 term2 |= doubleZSig0;
6813 add192( rem1, rem2, rem3, 0, term2, term3, &rem1, &rem2, &rem3 );
6815 zSig1 |= ( ( rem1 | rem2 | rem3 ) != 0 );
6817 shift128ExtraRightJamming( zSig0, zSig1, 0, 14, &zSig0, &zSig1, &zSig2 );
6818 return roundAndPackFloat128(0, zExp, zSig0, zSig1, zSig2, status);
6822 /*----------------------------------------------------------------------------
6823 | Returns 1 if the quadruple-precision floating-point value `a' is equal to
6824 | the corresponding value `b', and 0 otherwise. The invalid exception is
6825 | raised if either operand is a NaN. Otherwise, the comparison is performed
6826 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
6827 *----------------------------------------------------------------------------*/
6829 int float128_eq(float128 a, float128 b, float_status *status)
6832 if ( ( ( extractFloat128Exp( a ) == 0x7FFF )
6833 && ( extractFloat128Frac0( a ) | extractFloat128Frac1( a ) ) )
6834 || ( ( extractFloat128Exp( b ) == 0x7FFF )
6835 && ( extractFloat128Frac0( b ) | extractFloat128Frac1( b ) ) )
6837 float_raise(float_flag_invalid, status);
6838 return 0;
6840 return
6841 ( a.low == b.low )
6842 && ( ( a.high == b.high )
6843 || ( ( a.low == 0 )
6844 && ( (uint64_t) ( ( a.high | b.high )<<1 ) == 0 ) )
6849 /*----------------------------------------------------------------------------
6850 | Returns 1 if the quadruple-precision floating-point value `a' is less than
6851 | or equal to the corresponding value `b', and 0 otherwise. The invalid
6852 | exception is raised if either operand is a NaN. The comparison is performed
6853 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
6854 *----------------------------------------------------------------------------*/
6856 int float128_le(float128 a, float128 b, float_status *status)
6858 flag aSign, bSign;
6860 if ( ( ( extractFloat128Exp( a ) == 0x7FFF )
6861 && ( extractFloat128Frac0( a ) | extractFloat128Frac1( a ) ) )
6862 || ( ( extractFloat128Exp( b ) == 0x7FFF )
6863 && ( extractFloat128Frac0( b ) | extractFloat128Frac1( b ) ) )
6865 float_raise(float_flag_invalid, status);
6866 return 0;
6868 aSign = extractFloat128Sign( a );
6869 bSign = extractFloat128Sign( b );
6870 if ( aSign != bSign ) {
6871 return
6872 aSign
6873 || ( ( ( (uint64_t) ( ( a.high | b.high )<<1 ) ) | a.low | b.low )
6874 == 0 );
6876 return
6877 aSign ? le128( b.high, b.low, a.high, a.low )
6878 : le128( a.high, a.low, b.high, b.low );
6882 /*----------------------------------------------------------------------------
6883 | Returns 1 if the quadruple-precision floating-point value `a' is less than
6884 | the corresponding value `b', and 0 otherwise. The invalid exception is
6885 | raised if either operand is a NaN. The comparison is performed according
6886 | to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
6887 *----------------------------------------------------------------------------*/
6889 int float128_lt(float128 a, float128 b, float_status *status)
6891 flag aSign, bSign;
6893 if ( ( ( extractFloat128Exp( a ) == 0x7FFF )
6894 && ( extractFloat128Frac0( a ) | extractFloat128Frac1( a ) ) )
6895 || ( ( extractFloat128Exp( b ) == 0x7FFF )
6896 && ( extractFloat128Frac0( b ) | extractFloat128Frac1( b ) ) )
6898 float_raise(float_flag_invalid, status);
6899 return 0;
6901 aSign = extractFloat128Sign( a );
6902 bSign = extractFloat128Sign( b );
6903 if ( aSign != bSign ) {
6904 return
6905 aSign
6906 && ( ( ( (uint64_t) ( ( a.high | b.high )<<1 ) ) | a.low | b.low )
6907 != 0 );
6909 return
6910 aSign ? lt128( b.high, b.low, a.high, a.low )
6911 : lt128( a.high, a.low, b.high, b.low );
6915 /*----------------------------------------------------------------------------
6916 | Returns 1 if the quadruple-precision floating-point values `a' and `b' cannot
6917 | be compared, and 0 otherwise. The invalid exception is raised if either
6918 | operand is a NaN. The comparison is performed according to the IEC/IEEE
6919 | Standard for Binary Floating-Point Arithmetic.
6920 *----------------------------------------------------------------------------*/
6922 int float128_unordered(float128 a, float128 b, float_status *status)
6924 if ( ( ( extractFloat128Exp( a ) == 0x7FFF )
6925 && ( extractFloat128Frac0( a ) | extractFloat128Frac1( a ) ) )
6926 || ( ( extractFloat128Exp( b ) == 0x7FFF )
6927 && ( extractFloat128Frac0( b ) | extractFloat128Frac1( b ) ) )
6929 float_raise(float_flag_invalid, status);
6930 return 1;
6932 return 0;
6935 /*----------------------------------------------------------------------------
6936 | Returns 1 if the quadruple-precision floating-point value `a' is equal to
6937 | the corresponding value `b', and 0 otherwise. Quiet NaNs do not cause an
6938 | exception. The comparison is performed according to the IEC/IEEE Standard
6939 | for Binary Floating-Point Arithmetic.
6940 *----------------------------------------------------------------------------*/
6942 int float128_eq_quiet(float128 a, float128 b, float_status *status)
6945 if ( ( ( extractFloat128Exp( a ) == 0x7FFF )
6946 && ( extractFloat128Frac0( a ) | extractFloat128Frac1( a ) ) )
6947 || ( ( extractFloat128Exp( b ) == 0x7FFF )
6948 && ( extractFloat128Frac0( b ) | extractFloat128Frac1( b ) ) )
6950 if (float128_is_signaling_nan(a, status)
6951 || float128_is_signaling_nan(b, status)) {
6952 float_raise(float_flag_invalid, status);
6954 return 0;
6956 return
6957 ( a.low == b.low )
6958 && ( ( a.high == b.high )
6959 || ( ( a.low == 0 )
6960 && ( (uint64_t) ( ( a.high | b.high )<<1 ) == 0 ) )
6965 /*----------------------------------------------------------------------------
6966 | Returns 1 if the quadruple-precision floating-point value `a' is less than
6967 | or equal to the corresponding value `b', and 0 otherwise. Quiet NaNs do not
6968 | cause an exception. Otherwise, the comparison is performed according to the
6969 | IEC/IEEE Standard for Binary Floating-Point Arithmetic.
6970 *----------------------------------------------------------------------------*/
6972 int float128_le_quiet(float128 a, float128 b, float_status *status)
6974 flag aSign, bSign;
6976 if ( ( ( extractFloat128Exp( a ) == 0x7FFF )
6977 && ( extractFloat128Frac0( a ) | extractFloat128Frac1( a ) ) )
6978 || ( ( extractFloat128Exp( b ) == 0x7FFF )
6979 && ( extractFloat128Frac0( b ) | extractFloat128Frac1( b ) ) )
6981 if (float128_is_signaling_nan(a, status)
6982 || float128_is_signaling_nan(b, status)) {
6983 float_raise(float_flag_invalid, status);
6985 return 0;
6987 aSign = extractFloat128Sign( a );
6988 bSign = extractFloat128Sign( b );
6989 if ( aSign != bSign ) {
6990 return
6991 aSign
6992 || ( ( ( (uint64_t) ( ( a.high | b.high )<<1 ) ) | a.low | b.low )
6993 == 0 );
6995 return
6996 aSign ? le128( b.high, b.low, a.high, a.low )
6997 : le128( a.high, a.low, b.high, b.low );
7001 /*----------------------------------------------------------------------------
7002 | Returns 1 if the quadruple-precision floating-point value `a' is less than
7003 | the corresponding value `b', and 0 otherwise. Quiet NaNs do not cause an
7004 | exception. Otherwise, the comparison is performed according to the IEC/IEEE
7005 | Standard for Binary Floating-Point Arithmetic.
7006 *----------------------------------------------------------------------------*/
7008 int float128_lt_quiet(float128 a, float128 b, float_status *status)
7010 flag aSign, bSign;
7012 if ( ( ( extractFloat128Exp( a ) == 0x7FFF )
7013 && ( extractFloat128Frac0( a ) | extractFloat128Frac1( a ) ) )
7014 || ( ( extractFloat128Exp( b ) == 0x7FFF )
7015 && ( extractFloat128Frac0( b ) | extractFloat128Frac1( b ) ) )
7017 if (float128_is_signaling_nan(a, status)
7018 || float128_is_signaling_nan(b, status)) {
7019 float_raise(float_flag_invalid, status);
7021 return 0;
7023 aSign = extractFloat128Sign( a );
7024 bSign = extractFloat128Sign( b );
7025 if ( aSign != bSign ) {
7026 return
7027 aSign
7028 && ( ( ( (uint64_t) ( ( a.high | b.high )<<1 ) ) | a.low | b.low )
7029 != 0 );
7031 return
7032 aSign ? lt128( b.high, b.low, a.high, a.low )
7033 : lt128( a.high, a.low, b.high, b.low );
7037 /*----------------------------------------------------------------------------
7038 | Returns 1 if the quadruple-precision floating-point values `a' and `b' cannot
7039 | be compared, and 0 otherwise. Quiet NaNs do not cause an exception. The
7040 | comparison is performed according to the IEC/IEEE Standard for Binary
7041 | Floating-Point Arithmetic.
7042 *----------------------------------------------------------------------------*/
7044 int float128_unordered_quiet(float128 a, float128 b, float_status *status)
7046 if ( ( ( extractFloat128Exp( a ) == 0x7FFF )
7047 && ( extractFloat128Frac0( a ) | extractFloat128Frac1( a ) ) )
7048 || ( ( extractFloat128Exp( b ) == 0x7FFF )
7049 && ( extractFloat128Frac0( b ) | extractFloat128Frac1( b ) ) )
7051 if (float128_is_signaling_nan(a, status)
7052 || float128_is_signaling_nan(b, status)) {
7053 float_raise(float_flag_invalid, status);
7055 return 1;
7057 return 0;
7060 /* misc functions */
7061 float32 uint32_to_float32(uint32_t a, float_status *status)
7063 return int64_to_float32(a, status);
7066 float64 uint32_to_float64(uint32_t a, float_status *status)
7068 return int64_to_float64(a, status);
7071 uint32_t float32_to_uint32(float32 a, float_status *status)
7073 int64_t v;
7074 uint32_t res;
7075 int old_exc_flags = get_float_exception_flags(status);
7077 v = float32_to_int64(a, status);
7078 if (v < 0) {
7079 res = 0;
7080 } else if (v > 0xffffffff) {
7081 res = 0xffffffff;
7082 } else {
7083 return v;
7085 set_float_exception_flags(old_exc_flags, status);
7086 float_raise(float_flag_invalid, status);
7087 return res;
7090 uint32_t float32_to_uint32_round_to_zero(float32 a, float_status *status)
7092 int64_t v;
7093 uint32_t res;
7094 int old_exc_flags = get_float_exception_flags(status);
7096 v = float32_to_int64_round_to_zero(a, status);
7097 if (v < 0) {
7098 res = 0;
7099 } else if (v > 0xffffffff) {
7100 res = 0xffffffff;
7101 } else {
7102 return v;
7104 set_float_exception_flags(old_exc_flags, status);
7105 float_raise(float_flag_invalid, status);
7106 return res;
7109 int16_t float32_to_int16(float32 a, float_status *status)
7111 int32_t v;
7112 int16_t res;
7113 int old_exc_flags = get_float_exception_flags(status);
7115 v = float32_to_int32(a, status);
7116 if (v < -0x8000) {
7117 res = -0x8000;
7118 } else if (v > 0x7fff) {
7119 res = 0x7fff;
7120 } else {
7121 return v;
7124 set_float_exception_flags(old_exc_flags, status);
7125 float_raise(float_flag_invalid, status);
7126 return res;
7129 uint16_t float32_to_uint16(float32 a, float_status *status)
7131 int32_t v;
7132 uint16_t res;
7133 int old_exc_flags = get_float_exception_flags(status);
7135 v = float32_to_int32(a, status);
7136 if (v < 0) {
7137 res = 0;
7138 } else if (v > 0xffff) {
7139 res = 0xffff;
7140 } else {
7141 return v;
7144 set_float_exception_flags(old_exc_flags, status);
7145 float_raise(float_flag_invalid, status);
7146 return res;
7149 uint16_t float32_to_uint16_round_to_zero(float32 a, float_status *status)
7151 int64_t v;
7152 uint16_t res;
7153 int old_exc_flags = get_float_exception_flags(status);
7155 v = float32_to_int64_round_to_zero(a, status);
7156 if (v < 0) {
7157 res = 0;
7158 } else if (v > 0xffff) {
7159 res = 0xffff;
7160 } else {
7161 return v;
7163 set_float_exception_flags(old_exc_flags, status);
7164 float_raise(float_flag_invalid, status);
7165 return res;
7168 uint32_t float64_to_uint32(float64 a, float_status *status)
7170 uint64_t v;
7171 uint32_t res;
7172 int old_exc_flags = get_float_exception_flags(status);
7174 v = float64_to_uint64(a, status);
7175 if (v > 0xffffffff) {
7176 res = 0xffffffff;
7177 } else {
7178 return v;
7180 set_float_exception_flags(old_exc_flags, status);
7181 float_raise(float_flag_invalid, status);
7182 return res;
7185 uint32_t float64_to_uint32_round_to_zero(float64 a, float_status *status)
7187 uint64_t v;
7188 uint32_t res;
7189 int old_exc_flags = get_float_exception_flags(status);
7191 v = float64_to_uint64_round_to_zero(a, status);
7192 if (v > 0xffffffff) {
7193 res = 0xffffffff;
7194 } else {
7195 return v;
7197 set_float_exception_flags(old_exc_flags, status);
7198 float_raise(float_flag_invalid, status);
7199 return res;
7202 int16_t float64_to_int16(float64 a, float_status *status)
7204 int64_t v;
7205 int16_t res;
7206 int old_exc_flags = get_float_exception_flags(status);
7208 v = float64_to_int32(a, status);
7209 if (v < -0x8000) {
7210 res = -0x8000;
7211 } else if (v > 0x7fff) {
7212 res = 0x7fff;
7213 } else {
7214 return v;
7217 set_float_exception_flags(old_exc_flags, status);
7218 float_raise(float_flag_invalid, status);
7219 return res;
7222 uint16_t float64_to_uint16(float64 a, float_status *status)
7224 int64_t v;
7225 uint16_t res;
7226 int old_exc_flags = get_float_exception_flags(status);
7228 v = float64_to_int32(a, status);
7229 if (v < 0) {
7230 res = 0;
7231 } else if (v > 0xffff) {
7232 res = 0xffff;
7233 } else {
7234 return v;
7237 set_float_exception_flags(old_exc_flags, status);
7238 float_raise(float_flag_invalid, status);
7239 return res;
7242 uint16_t float64_to_uint16_round_to_zero(float64 a, float_status *status)
7244 int64_t v;
7245 uint16_t res;
7246 int old_exc_flags = get_float_exception_flags(status);
7248 v = float64_to_int64_round_to_zero(a, status);
7249 if (v < 0) {
7250 res = 0;
7251 } else if (v > 0xffff) {
7252 res = 0xffff;
7253 } else {
7254 return v;
7256 set_float_exception_flags(old_exc_flags, status);
7257 float_raise(float_flag_invalid, status);
7258 return res;
7261 /*----------------------------------------------------------------------------
7262 | Returns the result of converting the double-precision floating-point value
7263 | `a' to the 64-bit unsigned integer format. The conversion is
7264 | performed according to the IEC/IEEE Standard for Binary Floating-Point
7265 | Arithmetic---which means in particular that the conversion is rounded
7266 | according to the current rounding mode. If `a' is a NaN, the largest
7267 | positive integer is returned. If the conversion overflows, the
7268 | largest unsigned integer is returned. If 'a' is negative, the value is
7269 | rounded and zero is returned; negative values that do not round to zero
7270 | will raise the inexact exception.
7271 *----------------------------------------------------------------------------*/
7273 uint64_t float64_to_uint64(float64 a, float_status *status)
7275 flag aSign;
7276 int aExp;
7277 int shiftCount;
7278 uint64_t aSig, aSigExtra;
7279 a = float64_squash_input_denormal(a, status);
7281 aSig = extractFloat64Frac(a);
7282 aExp = extractFloat64Exp(a);
7283 aSign = extractFloat64Sign(a);
7284 if (aSign && (aExp > 1022)) {
7285 float_raise(float_flag_invalid, status);
7286 if (float64_is_any_nan(a)) {
7287 return LIT64(0xFFFFFFFFFFFFFFFF);
7288 } else {
7289 return 0;
7292 if (aExp) {
7293 aSig |= LIT64(0x0010000000000000);
7295 shiftCount = 0x433 - aExp;
7296 if (shiftCount <= 0) {
7297 if (0x43E < aExp) {
7298 float_raise(float_flag_invalid, status);
7299 return LIT64(0xFFFFFFFFFFFFFFFF);
7301 aSigExtra = 0;
7302 aSig <<= -shiftCount;
7303 } else {
7304 shift64ExtraRightJamming(aSig, 0, shiftCount, &aSig, &aSigExtra);
7306 return roundAndPackUint64(aSign, aSig, aSigExtra, status);
7309 uint64_t float64_to_uint64_round_to_zero(float64 a, float_status *status)
7311 signed char current_rounding_mode = status->float_rounding_mode;
7312 set_float_rounding_mode(float_round_to_zero, status);
7313 int64_t v = float64_to_uint64(a, status);
7314 set_float_rounding_mode(current_rounding_mode, status);
7315 return v;
7318 #define COMPARE(s, nan_exp) \
7319 static inline int float ## s ## _compare_internal(float ## s a, float ## s b,\
7320 int is_quiet, float_status *status) \
7322 flag aSign, bSign; \
7323 uint ## s ## _t av, bv; \
7324 a = float ## s ## _squash_input_denormal(a, status); \
7325 b = float ## s ## _squash_input_denormal(b, status); \
7327 if (( ( extractFloat ## s ## Exp( a ) == nan_exp ) && \
7328 extractFloat ## s ## Frac( a ) ) || \
7329 ( ( extractFloat ## s ## Exp( b ) == nan_exp ) && \
7330 extractFloat ## s ## Frac( b ) )) { \
7331 if (!is_quiet || \
7332 float ## s ## _is_signaling_nan(a, status) || \
7333 float ## s ## _is_signaling_nan(b, status)) { \
7334 float_raise(float_flag_invalid, status); \
7336 return float_relation_unordered; \
7338 aSign = extractFloat ## s ## Sign( a ); \
7339 bSign = extractFloat ## s ## Sign( b ); \
7340 av = float ## s ## _val(a); \
7341 bv = float ## s ## _val(b); \
7342 if ( aSign != bSign ) { \
7343 if ( (uint ## s ## _t) ( ( av | bv )<<1 ) == 0 ) { \
7344 /* zero case */ \
7345 return float_relation_equal; \
7346 } else { \
7347 return 1 - (2 * aSign); \
7349 } else { \
7350 if (av == bv) { \
7351 return float_relation_equal; \
7352 } else { \
7353 return 1 - 2 * (aSign ^ ( av < bv )); \
7358 int float ## s ## _compare(float ## s a, float ## s b, float_status *status) \
7360 return float ## s ## _compare_internal(a, b, 0, status); \
7363 int float ## s ## _compare_quiet(float ## s a, float ## s b, \
7364 float_status *status) \
7366 return float ## s ## _compare_internal(a, b, 1, status); \
7369 COMPARE(32, 0xff)
7370 COMPARE(64, 0x7ff)
7372 static inline int floatx80_compare_internal(floatx80 a, floatx80 b,
7373 int is_quiet, float_status *status)
7375 flag aSign, bSign;
7377 if (( ( extractFloatx80Exp( a ) == 0x7fff ) &&
7378 ( extractFloatx80Frac( a )<<1 ) ) ||
7379 ( ( extractFloatx80Exp( b ) == 0x7fff ) &&
7380 ( extractFloatx80Frac( b )<<1 ) )) {
7381 if (!is_quiet ||
7382 floatx80_is_signaling_nan(a, status) ||
7383 floatx80_is_signaling_nan(b, status)) {
7384 float_raise(float_flag_invalid, status);
7386 return float_relation_unordered;
7388 aSign = extractFloatx80Sign( a );
7389 bSign = extractFloatx80Sign( b );
7390 if ( aSign != bSign ) {
7392 if ( ( ( (uint16_t) ( ( a.high | b.high ) << 1 ) ) == 0) &&
7393 ( ( a.low | b.low ) == 0 ) ) {
7394 /* zero case */
7395 return float_relation_equal;
7396 } else {
7397 return 1 - (2 * aSign);
7399 } else {
7400 if (a.low == b.low && a.high == b.high) {
7401 return float_relation_equal;
7402 } else {
7403 return 1 - 2 * (aSign ^ ( lt128( a.high, a.low, b.high, b.low ) ));
7408 int floatx80_compare(floatx80 a, floatx80 b, float_status *status)
7410 return floatx80_compare_internal(a, b, 0, status);
7413 int floatx80_compare_quiet(floatx80 a, floatx80 b, float_status *status)
7415 return floatx80_compare_internal(a, b, 1, status);
7418 static inline int float128_compare_internal(float128 a, float128 b,
7419 int is_quiet, float_status *status)
7421 flag aSign, bSign;
7423 if (( ( extractFloat128Exp( a ) == 0x7fff ) &&
7424 ( extractFloat128Frac0( a ) | extractFloat128Frac1( a ) ) ) ||
7425 ( ( extractFloat128Exp( b ) == 0x7fff ) &&
7426 ( extractFloat128Frac0( b ) | extractFloat128Frac1( b ) ) )) {
7427 if (!is_quiet ||
7428 float128_is_signaling_nan(a, status) ||
7429 float128_is_signaling_nan(b, status)) {
7430 float_raise(float_flag_invalid, status);
7432 return float_relation_unordered;
7434 aSign = extractFloat128Sign( a );
7435 bSign = extractFloat128Sign( b );
7436 if ( aSign != bSign ) {
7437 if ( ( ( ( a.high | b.high )<<1 ) | a.low | b.low ) == 0 ) {
7438 /* zero case */
7439 return float_relation_equal;
7440 } else {
7441 return 1 - (2 * aSign);
7443 } else {
7444 if (a.low == b.low && a.high == b.high) {
7445 return float_relation_equal;
7446 } else {
7447 return 1 - 2 * (aSign ^ ( lt128( a.high, a.low, b.high, b.low ) ));
7452 int float128_compare(float128 a, float128 b, float_status *status)
7454 return float128_compare_internal(a, b, 0, status);
7457 int float128_compare_quiet(float128 a, float128 b, float_status *status)
7459 return float128_compare_internal(a, b, 1, status);
7462 /* min() and max() functions. These can't be implemented as
7463 * 'compare and pick one input' because that would mishandle
7464 * NaNs and +0 vs -0.
7466 * minnum() and maxnum() functions. These are similar to the min()
7467 * and max() functions but if one of the arguments is a QNaN and
7468 * the other is numerical then the numerical argument is returned.
7469 * minnum() and maxnum correspond to the IEEE 754-2008 minNum()
7470 * and maxNum() operations. min() and max() are the typical min/max
7471 * semantics provided by many CPUs which predate that specification.
7473 * minnummag() and maxnummag() functions correspond to minNumMag()
7474 * and minNumMag() from the IEEE-754 2008.
7476 #define MINMAX(s) \
7477 static inline float ## s float ## s ## _minmax(float ## s a, float ## s b, \
7478 int ismin, int isieee, \
7479 int ismag, \
7480 float_status *status) \
7482 flag aSign, bSign; \
7483 uint ## s ## _t av, bv, aav, abv; \
7484 a = float ## s ## _squash_input_denormal(a, status); \
7485 b = float ## s ## _squash_input_denormal(b, status); \
7486 if (float ## s ## _is_any_nan(a) || \
7487 float ## s ## _is_any_nan(b)) { \
7488 if (isieee) { \
7489 if (float ## s ## _is_quiet_nan(a, status) && \
7490 !float ## s ##_is_any_nan(b)) { \
7491 return b; \
7492 } else if (float ## s ## _is_quiet_nan(b, status) && \
7493 !float ## s ## _is_any_nan(a)) { \
7494 return a; \
7497 return propagateFloat ## s ## NaN(a, b, status); \
7499 aSign = extractFloat ## s ## Sign(a); \
7500 bSign = extractFloat ## s ## Sign(b); \
7501 av = float ## s ## _val(a); \
7502 bv = float ## s ## _val(b); \
7503 if (ismag) { \
7504 aav = float ## s ## _abs(av); \
7505 abv = float ## s ## _abs(bv); \
7506 if (aav != abv) { \
7507 if (ismin) { \
7508 return (aav < abv) ? a : b; \
7509 } else { \
7510 return (aav < abv) ? b : a; \
7514 if (aSign != bSign) { \
7515 if (ismin) { \
7516 return aSign ? a : b; \
7517 } else { \
7518 return aSign ? b : a; \
7520 } else { \
7521 if (ismin) { \
7522 return (aSign ^ (av < bv)) ? a : b; \
7523 } else { \
7524 return (aSign ^ (av < bv)) ? b : a; \
7529 float ## s float ## s ## _min(float ## s a, float ## s b, \
7530 float_status *status) \
7532 return float ## s ## _minmax(a, b, 1, 0, 0, status); \
7535 float ## s float ## s ## _max(float ## s a, float ## s b, \
7536 float_status *status) \
7538 return float ## s ## _minmax(a, b, 0, 0, 0, status); \
7541 float ## s float ## s ## _minnum(float ## s a, float ## s b, \
7542 float_status *status) \
7544 return float ## s ## _minmax(a, b, 1, 1, 0, status); \
7547 float ## s float ## s ## _maxnum(float ## s a, float ## s b, \
7548 float_status *status) \
7550 return float ## s ## _minmax(a, b, 0, 1, 0, status); \
7553 float ## s float ## s ## _minnummag(float ## s a, float ## s b, \
7554 float_status *status) \
7556 return float ## s ## _minmax(a, b, 1, 1, 1, status); \
7559 float ## s float ## s ## _maxnummag(float ## s a, float ## s b, \
7560 float_status *status) \
7562 return float ## s ## _minmax(a, b, 0, 1, 1, status); \
7565 MINMAX(32)
7566 MINMAX(64)
7569 /* Multiply A by 2 raised to the power N. */
7570 float32 float32_scalbn(float32 a, int n, float_status *status)
7572 flag aSign;
7573 int16_t aExp;
7574 uint32_t aSig;
7576 a = float32_squash_input_denormal(a, status);
7577 aSig = extractFloat32Frac( a );
7578 aExp = extractFloat32Exp( a );
7579 aSign = extractFloat32Sign( a );
7581 if ( aExp == 0xFF ) {
7582 if ( aSig ) {
7583 return propagateFloat32NaN(a, a, status);
7585 return a;
7587 if (aExp != 0) {
7588 aSig |= 0x00800000;
7589 } else if (aSig == 0) {
7590 return a;
7591 } else {
7592 aExp++;
7595 if (n > 0x200) {
7596 n = 0x200;
7597 } else if (n < -0x200) {
7598 n = -0x200;
7601 aExp += n - 1;
7602 aSig <<= 7;
7603 return normalizeRoundAndPackFloat32(aSign, aExp, aSig, status);
7606 float64 float64_scalbn(float64 a, int n, float_status *status)
7608 flag aSign;
7609 int16_t aExp;
7610 uint64_t aSig;
7612 a = float64_squash_input_denormal(a, status);
7613 aSig = extractFloat64Frac( a );
7614 aExp = extractFloat64Exp( a );
7615 aSign = extractFloat64Sign( a );
7617 if ( aExp == 0x7FF ) {
7618 if ( aSig ) {
7619 return propagateFloat64NaN(a, a, status);
7621 return a;
7623 if (aExp != 0) {
7624 aSig |= LIT64( 0x0010000000000000 );
7625 } else if (aSig == 0) {
7626 return a;
7627 } else {
7628 aExp++;
7631 if (n > 0x1000) {
7632 n = 0x1000;
7633 } else if (n < -0x1000) {
7634 n = -0x1000;
7637 aExp += n - 1;
7638 aSig <<= 10;
7639 return normalizeRoundAndPackFloat64(aSign, aExp, aSig, status);
7642 floatx80 floatx80_scalbn(floatx80 a, int n, float_status *status)
7644 flag aSign;
7645 int32_t aExp;
7646 uint64_t aSig;
7648 aSig = extractFloatx80Frac( a );
7649 aExp = extractFloatx80Exp( a );
7650 aSign = extractFloatx80Sign( a );
7652 if ( aExp == 0x7FFF ) {
7653 if ( aSig<<1 ) {
7654 return propagateFloatx80NaN(a, a, status);
7656 return a;
7659 if (aExp == 0) {
7660 if (aSig == 0) {
7661 return a;
7663 aExp++;
7666 if (n > 0x10000) {
7667 n = 0x10000;
7668 } else if (n < -0x10000) {
7669 n = -0x10000;
7672 aExp += n;
7673 return normalizeRoundAndPackFloatx80(status->floatx80_rounding_precision,
7674 aSign, aExp, aSig, 0, status);
7677 float128 float128_scalbn(float128 a, int n, float_status *status)
7679 flag aSign;
7680 int32_t aExp;
7681 uint64_t aSig0, aSig1;
7683 aSig1 = extractFloat128Frac1( a );
7684 aSig0 = extractFloat128Frac0( a );
7685 aExp = extractFloat128Exp( a );
7686 aSign = extractFloat128Sign( a );
7687 if ( aExp == 0x7FFF ) {
7688 if ( aSig0 | aSig1 ) {
7689 return propagateFloat128NaN(a, a, status);
7691 return a;
7693 if (aExp != 0) {
7694 aSig0 |= LIT64( 0x0001000000000000 );
7695 } else if (aSig0 == 0 && aSig1 == 0) {
7696 return a;
7697 } else {
7698 aExp++;
7701 if (n > 0x10000) {
7702 n = 0x10000;
7703 } else if (n < -0x10000) {
7704 n = -0x10000;
7707 aExp += n - 1;
7708 return normalizeRoundAndPackFloat128( aSign, aExp, aSig0, aSig1
7709 , status);