]> git.proxmox.com Git - mirror_qemu.git/blob - fpu/softfloat-specialize.h
981d66573ec7a2929d87094262a50fb2b98b6d3d
[mirror_qemu.git] / fpu / softfloat-specialize.h
1 /*
2 * QEMU float support
3 *
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
12 *
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.
16 */
17
18 /*
19 ===============================================================================
20 This C source fragment is part of the SoftFloat IEC/IEEE Floating-point
21 Arithmetic Package, Release 2a.
22
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'.
32
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.
38
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.
43
44 ===============================================================================
45 */
46
47 /* BSD licensing:
48 * Copyright (c) 2006, Fabrice Bellard
49 * All rights reserved.
50 *
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions are met:
53 *
54 * 1. Redistributions of source code must retain the above copyright notice,
55 * this list of conditions and the following disclaimer.
56 *
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.
60 *
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.
64 *
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.
76 */
77
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.
80 */
81
82 #if defined(TARGET_XTENSA)
83 /* Define for architectures which deviate from IEEE in not supporting
84 * signaling NaNs (so all NaNs are treated as quiet).
85 */
86 #define NO_SIGNALING_NANS 1
87 #endif
88
89 /*----------------------------------------------------------------------------
90 | The pattern for a default generated half-precision NaN.
91 *----------------------------------------------------------------------------*/
92 float16 float16_default_nan(float_status *status)
93 {
94 #if defined(TARGET_ARM)
95 return const_float16(0x7E00);
96 #else
97 if (status->snan_bit_is_one) {
98 return const_float16(0x7DFF);
99 } else {
100 return const_float16(0xFE00);
101 }
102 #endif
103 }
104
105 /*----------------------------------------------------------------------------
106 | The pattern for a default generated single-precision NaN.
107 *----------------------------------------------------------------------------*/
108 float32 float32_default_nan(float_status *status)
109 {
110 #if defined(TARGET_SPARC)
111 return const_float32(0x7FFFFFFF);
112 #elif defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_ALPHA) || \
113 defined(TARGET_XTENSA) || defined(TARGET_S390X) || defined(TARGET_TRICORE)
114 return const_float32(0x7FC00000);
115 #else
116 if (status->snan_bit_is_one) {
117 return const_float32(0x7FBFFFFF);
118 } else {
119 return const_float32(0xFFC00000);
120 }
121 #endif
122 }
123
124 /*----------------------------------------------------------------------------
125 | The pattern for a default generated double-precision NaN.
126 *----------------------------------------------------------------------------*/
127 float64 float64_default_nan(float_status *status)
128 {
129 #if defined(TARGET_SPARC)
130 return const_float64(LIT64(0x7FFFFFFFFFFFFFFF));
131 #elif defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_ALPHA) || \
132 defined(TARGET_S390X)
133 return const_float64(LIT64(0x7FF8000000000000));
134 #else
135 if (status->snan_bit_is_one) {
136 return const_float64(LIT64(0x7FF7FFFFFFFFFFFF));
137 } else {
138 return const_float64(LIT64(0xFFF8000000000000));
139 }
140 #endif
141 }
142
143 /*----------------------------------------------------------------------------
144 | The pattern for a default generated extended double-precision NaN.
145 *----------------------------------------------------------------------------*/
146 floatx80 floatx80_default_nan(float_status *status)
147 {
148 floatx80 r;
149
150 if (status->snan_bit_is_one) {
151 r.low = LIT64(0xBFFFFFFFFFFFFFFF);
152 r.high = 0x7FFF;
153 } else {
154 r.low = LIT64(0xC000000000000000);
155 r.high = 0xFFFF;
156 }
157 return r;
158 }
159
160 /*----------------------------------------------------------------------------
161 | The pattern for a default generated quadruple-precision NaN.
162 *----------------------------------------------------------------------------*/
163 float128 float128_default_nan(float_status *status)
164 {
165 float128 r;
166
167 if (status->snan_bit_is_one) {
168 r.low = LIT64(0xFFFFFFFFFFFFFFFF);
169 r.high = LIT64(0x7FFF7FFFFFFFFFFF);
170 } else {
171 r.low = LIT64(0x0000000000000000);
172 #if defined(TARGET_S390X)
173 r.high = LIT64(0x7FFF800000000000);
174 #else
175 r.high = LIT64(0xFFFF800000000000);
176 #endif
177 }
178 return r;
179 }
180
181 /*----------------------------------------------------------------------------
182 | Raises the exceptions specified by `flags'. Floating-point traps can be
183 | defined here if desired. It is currently not possible for such a trap
184 | to substitute a result value. If traps are not implemented, this routine
185 | should be simply `float_exception_flags |= flags;'.
186 *----------------------------------------------------------------------------*/
187
188 void float_raise(int8_t flags, float_status *status)
189 {
190 status->float_exception_flags |= flags;
191 }
192
193 /*----------------------------------------------------------------------------
194 | Internal canonical NaN format.
195 *----------------------------------------------------------------------------*/
196 typedef struct {
197 flag sign;
198 uint64_t high, low;
199 } commonNaNT;
200
201 #ifdef NO_SIGNALING_NANS
202 int float16_is_quiet_nan(float16 a_, float_status *status)
203 {
204 return float16_is_any_nan(a_);
205 }
206
207 int float16_is_signaling_nan(float16 a_, float_status *status)
208 {
209 return 0;
210 }
211 #else
212 /*----------------------------------------------------------------------------
213 | Returns 1 if the half-precision floating-point value `a' is a quiet
214 | NaN; otherwise returns 0.
215 *----------------------------------------------------------------------------*/
216
217 int float16_is_quiet_nan(float16 a_, float_status *status)
218 {
219 uint16_t a = float16_val(a_);
220 if (status->snan_bit_is_one) {
221 return (((a >> 9) & 0x3F) == 0x3E) && (a & 0x1FF);
222 } else {
223 return ((a & ~0x8000) >= 0x7C80);
224 }
225 }
226
227 /*----------------------------------------------------------------------------
228 | Returns 1 if the half-precision floating-point value `a' is a signaling
229 | NaN; otherwise returns 0.
230 *----------------------------------------------------------------------------*/
231
232 int float16_is_signaling_nan(float16 a_, float_status *status)
233 {
234 uint16_t a = float16_val(a_);
235 if (status->snan_bit_is_one) {
236 return ((a & ~0x8000) >= 0x7C80);
237 } else {
238 return (((a >> 9) & 0x3F) == 0x3E) && (a & 0x1FF);
239 }
240 }
241 #endif
242
243 /*----------------------------------------------------------------------------
244 | Returns a quiet NaN if the half-precision floating point value `a' is a
245 | signaling NaN; otherwise returns `a'.
246 *----------------------------------------------------------------------------*/
247 float16 float16_maybe_silence_nan(float16 a_, float_status *status)
248 {
249 if (float16_is_signaling_nan(a_, status)) {
250 if (status->snan_bit_is_one) {
251 return float16_default_nan(status);
252 } else {
253 uint16_t a = float16_val(a_);
254 a |= (1 << 9);
255 return make_float16(a);
256 }
257 }
258 return a_;
259 }
260
261 /*----------------------------------------------------------------------------
262 | Returns the result of converting the half-precision floating-point NaN
263 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
264 | exception is raised.
265 *----------------------------------------------------------------------------*/
266
267 static commonNaNT float16ToCommonNaN(float16 a, float_status *status)
268 {
269 commonNaNT z;
270
271 if (float16_is_signaling_nan(a, status)) {
272 float_raise(float_flag_invalid, status);
273 }
274 z.sign = float16_val(a) >> 15;
275 z.low = 0;
276 z.high = ((uint64_t) float16_val(a)) << 54;
277 return z;
278 }
279
280 /*----------------------------------------------------------------------------
281 | Returns the result of converting the canonical NaN `a' to the half-
282 | precision floating-point format.
283 *----------------------------------------------------------------------------*/
284
285 static float16 commonNaNToFloat16(commonNaNT a, float_status *status)
286 {
287 uint16_t mantissa = a.high >> 54;
288
289 if (status->default_nan_mode) {
290 return float16_default_nan(status);
291 }
292
293 if (mantissa) {
294 return make_float16(((((uint16_t) a.sign) << 15)
295 | (0x1F << 10) | mantissa));
296 } else {
297 return float16_default_nan(status);
298 }
299 }
300
301 #ifdef NO_SIGNALING_NANS
302 int float32_is_quiet_nan(float32 a_, float_status *status)
303 {
304 return float32_is_any_nan(a_);
305 }
306
307 int float32_is_signaling_nan(float32 a_, float_status *status)
308 {
309 return 0;
310 }
311 #else
312 /*----------------------------------------------------------------------------
313 | Returns 1 if the single-precision floating-point value `a' is a quiet
314 | NaN; otherwise returns 0.
315 *----------------------------------------------------------------------------*/
316
317 int float32_is_quiet_nan(float32 a_, float_status *status)
318 {
319 uint32_t a = float32_val(a_);
320 if (status->snan_bit_is_one) {
321 return (((a >> 22) & 0x1FF) == 0x1FE) && (a & 0x003FFFFF);
322 } else {
323 return ((uint32_t)(a << 1) >= 0xFF800000);
324 }
325 }
326
327 /*----------------------------------------------------------------------------
328 | Returns 1 if the single-precision floating-point value `a' is a signaling
329 | NaN; otherwise returns 0.
330 *----------------------------------------------------------------------------*/
331
332 int float32_is_signaling_nan(float32 a_, float_status *status)
333 {
334 uint32_t a = float32_val(a_);
335 if (status->snan_bit_is_one) {
336 return ((uint32_t)(a << 1) >= 0xFF800000);
337 } else {
338 return (((a >> 22) & 0x1FF) == 0x1FE) && (a & 0x003FFFFF);
339 }
340 }
341 #endif
342
343 /*----------------------------------------------------------------------------
344 | Returns a quiet NaN if the single-precision floating point value `a' is a
345 | signaling NaN; otherwise returns `a'.
346 *----------------------------------------------------------------------------*/
347
348 float32 float32_maybe_silence_nan(float32 a_, float_status *status)
349 {
350 if (float32_is_signaling_nan(a_, status)) {
351 if (status->snan_bit_is_one) {
352 return float32_default_nan(status);
353 } else {
354 uint32_t a = float32_val(a_);
355 a |= (1 << 22);
356 return make_float32(a);
357 }
358 }
359 return a_;
360 }
361
362 /*----------------------------------------------------------------------------
363 | Returns the result of converting the single-precision floating-point NaN
364 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
365 | exception is raised.
366 *----------------------------------------------------------------------------*/
367
368 static commonNaNT float32ToCommonNaN(float32 a, float_status *status)
369 {
370 commonNaNT z;
371
372 if (float32_is_signaling_nan(a, status)) {
373 float_raise(float_flag_invalid, status);
374 }
375 z.sign = float32_val(a) >> 31;
376 z.low = 0;
377 z.high = ((uint64_t)float32_val(a)) << 41;
378 return z;
379 }
380
381 /*----------------------------------------------------------------------------
382 | Returns the result of converting the canonical NaN `a' to the single-
383 | precision floating-point format.
384 *----------------------------------------------------------------------------*/
385
386 static float32 commonNaNToFloat32(commonNaNT a, float_status *status)
387 {
388 uint32_t mantissa = a.high >> 41;
389
390 if (status->default_nan_mode) {
391 return float32_default_nan(status);
392 }
393
394 if (mantissa) {
395 return make_float32(
396 (((uint32_t)a.sign) << 31) | 0x7F800000 | (a.high >> 41));
397 } else {
398 return float32_default_nan(status);
399 }
400 }
401
402 /*----------------------------------------------------------------------------
403 | Select which NaN to propagate for a two-input operation.
404 | IEEE754 doesn't specify all the details of this, so the
405 | algorithm is target-specific.
406 | The routine is passed various bits of information about the
407 | two NaNs and should return 0 to select NaN a and 1 for NaN b.
408 | Note that signalling NaNs are always squashed to quiet NaNs
409 | by the caller, by calling floatXX_maybe_silence_nan() before
410 | returning them.
411 |
412 | aIsLargerSignificand is only valid if both a and b are NaNs
413 | of some kind, and is true if a has the larger significand,
414 | or if both a and b have the same significand but a is
415 | positive but b is negative. It is only needed for the x87
416 | tie-break rule.
417 *----------------------------------------------------------------------------*/
418
419 #if defined(TARGET_ARM)
420 static int pickNaN(flag aIsQNaN, flag aIsSNaN, flag bIsQNaN, flag bIsSNaN,
421 flag aIsLargerSignificand)
422 {
423 /* ARM mandated NaN propagation rules: take the first of:
424 * 1. A if it is signaling
425 * 2. B if it is signaling
426 * 3. A (quiet)
427 * 4. B (quiet)
428 * A signaling NaN is always quietened before returning it.
429 */
430 if (aIsSNaN) {
431 return 0;
432 } else if (bIsSNaN) {
433 return 1;
434 } else if (aIsQNaN) {
435 return 0;
436 } else {
437 return 1;
438 }
439 }
440 #elif defined(TARGET_MIPS)
441 static int pickNaN(flag aIsQNaN, flag aIsSNaN, flag bIsQNaN, flag bIsSNaN,
442 flag aIsLargerSignificand)
443 {
444 /* According to MIPS specifications, if one of the two operands is
445 * a sNaN, a new qNaN has to be generated. This is done in
446 * floatXX_maybe_silence_nan(). For qNaN inputs the specifications
447 * says: "When possible, this QNaN result is one of the operand QNaN
448 * values." In practice it seems that most implementations choose
449 * the first operand if both operands are qNaN. In short this gives
450 * the following rules:
451 * 1. A if it is signaling
452 * 2. B if it is signaling
453 * 3. A (quiet)
454 * 4. B (quiet)
455 * A signaling NaN is always silenced before returning it.
456 */
457 if (aIsSNaN) {
458 return 0;
459 } else if (bIsSNaN) {
460 return 1;
461 } else if (aIsQNaN) {
462 return 0;
463 } else {
464 return 1;
465 }
466 }
467 #elif defined(TARGET_PPC) || defined(TARGET_XTENSA)
468 static int pickNaN(flag aIsQNaN, flag aIsSNaN, flag bIsQNaN, flag bIsSNaN,
469 flag aIsLargerSignificand)
470 {
471 /* PowerPC propagation rules:
472 * 1. A if it sNaN or qNaN
473 * 2. B if it sNaN or qNaN
474 * A signaling NaN is always silenced before returning it.
475 */
476 if (aIsSNaN || aIsQNaN) {
477 return 0;
478 } else {
479 return 1;
480 }
481 }
482 #else
483 static int pickNaN(flag aIsQNaN, flag aIsSNaN, flag bIsQNaN, flag bIsSNaN,
484 flag aIsLargerSignificand)
485 {
486 /* This implements x87 NaN propagation rules:
487 * SNaN + QNaN => return the QNaN
488 * two SNaNs => return the one with the larger significand, silenced
489 * two QNaNs => return the one with the larger significand
490 * SNaN and a non-NaN => return the SNaN, silenced
491 * QNaN and a non-NaN => return the QNaN
492 *
493 * If we get down to comparing significands and they are the same,
494 * return the NaN with the positive sign bit (if any).
495 */
496 if (aIsSNaN) {
497 if (bIsSNaN) {
498 return aIsLargerSignificand ? 0 : 1;
499 }
500 return bIsQNaN ? 1 : 0;
501 } else if (aIsQNaN) {
502 if (bIsSNaN || !bIsQNaN) {
503 return 0;
504 } else {
505 return aIsLargerSignificand ? 0 : 1;
506 }
507 } else {
508 return 1;
509 }
510 }
511 #endif
512
513 /*----------------------------------------------------------------------------
514 | Select which NaN to propagate for a three-input operation.
515 | For the moment we assume that no CPU needs the 'larger significand'
516 | information.
517 | Return values : 0 : a; 1 : b; 2 : c; 3 : default-NaN
518 *----------------------------------------------------------------------------*/
519 #if defined(TARGET_ARM)
520 static int pickNaNMulAdd(flag aIsQNaN, flag aIsSNaN, flag bIsQNaN, flag bIsSNaN,
521 flag cIsQNaN, flag cIsSNaN, flag infzero,
522 float_status *status)
523 {
524 /* For ARM, the (inf,zero,qnan) case sets InvalidOp and returns
525 * the default NaN
526 */
527 if (infzero && cIsQNaN) {
528 float_raise(float_flag_invalid, status);
529 return 3;
530 }
531
532 /* This looks different from the ARM ARM pseudocode, because the ARM ARM
533 * puts the operands to a fused mac operation (a*b)+c in the order c,a,b.
534 */
535 if (cIsSNaN) {
536 return 2;
537 } else if (aIsSNaN) {
538 return 0;
539 } else if (bIsSNaN) {
540 return 1;
541 } else if (cIsQNaN) {
542 return 2;
543 } else if (aIsQNaN) {
544 return 0;
545 } else {
546 return 1;
547 }
548 }
549 #elif defined(TARGET_MIPS)
550 static int pickNaNMulAdd(flag aIsQNaN, flag aIsSNaN, flag bIsQNaN, flag bIsSNaN,
551 flag cIsQNaN, flag cIsSNaN, flag infzero,
552 float_status *status)
553 {
554 /* For MIPS, the (inf,zero,qnan) case sets InvalidOp and returns
555 * the default NaN
556 */
557 if (infzero) {
558 float_raise(float_flag_invalid, status);
559 return 3;
560 }
561
562 /* Prefer sNaN over qNaN, in the a, b, c order. */
563 if (aIsSNaN) {
564 return 0;
565 } else if (bIsSNaN) {
566 return 1;
567 } else if (cIsSNaN) {
568 return 2;
569 } else if (aIsQNaN) {
570 return 0;
571 } else if (bIsQNaN) {
572 return 1;
573 } else {
574 return 2;
575 }
576 }
577 #elif defined(TARGET_PPC)
578 static int pickNaNMulAdd(flag aIsQNaN, flag aIsSNaN, flag bIsQNaN, flag bIsSNaN,
579 flag cIsQNaN, flag cIsSNaN, flag infzero,
580 float_status *status)
581 {
582 /* For PPC, the (inf,zero,qnan) case sets InvalidOp, but we prefer
583 * to return an input NaN if we have one (ie c) rather than generating
584 * a default NaN
585 */
586 if (infzero) {
587 float_raise(float_flag_invalid, status);
588 return 2;
589 }
590
591 /* If fRA is a NaN return it; otherwise if fRB is a NaN return it;
592 * otherwise return fRC. Note that muladd on PPC is (fRA * fRC) + frB
593 */
594 if (aIsSNaN || aIsQNaN) {
595 return 0;
596 } else if (cIsSNaN || cIsQNaN) {
597 return 2;
598 } else {
599 return 1;
600 }
601 }
602 #else
603 /* A default implementation: prefer a to b to c.
604 * This is unlikely to actually match any real implementation.
605 */
606 static int pickNaNMulAdd(flag aIsQNaN, flag aIsSNaN, flag bIsQNaN, flag bIsSNaN,
607 flag cIsQNaN, flag cIsSNaN, flag infzero,
608 float_status *status)
609 {
610 if (aIsSNaN || aIsQNaN) {
611 return 0;
612 } else if (bIsSNaN || bIsQNaN) {
613 return 1;
614 } else {
615 return 2;
616 }
617 }
618 #endif
619
620 /*----------------------------------------------------------------------------
621 | Takes two single-precision floating-point values `a' and `b', one of which
622 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
623 | signaling NaN, the invalid exception is raised.
624 *----------------------------------------------------------------------------*/
625
626 static float32 propagateFloat32NaN(float32 a, float32 b, float_status *status)
627 {
628 flag aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN;
629 flag aIsLargerSignificand;
630 uint32_t av, bv;
631
632 aIsQuietNaN = float32_is_quiet_nan(a, status);
633 aIsSignalingNaN = float32_is_signaling_nan(a, status);
634 bIsQuietNaN = float32_is_quiet_nan(b, status);
635 bIsSignalingNaN = float32_is_signaling_nan(b, status);
636 av = float32_val(a);
637 bv = float32_val(b);
638
639 if (aIsSignalingNaN | bIsSignalingNaN) {
640 float_raise(float_flag_invalid, status);
641 }
642
643 if (status->default_nan_mode) {
644 return float32_default_nan(status);
645 }
646
647 if ((uint32_t)(av << 1) < (uint32_t)(bv << 1)) {
648 aIsLargerSignificand = 0;
649 } else if ((uint32_t)(bv << 1) < (uint32_t)(av << 1)) {
650 aIsLargerSignificand = 1;
651 } else {
652 aIsLargerSignificand = (av < bv) ? 1 : 0;
653 }
654
655 if (pickNaN(aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN,
656 aIsLargerSignificand)) {
657 return float32_maybe_silence_nan(b, status);
658 } else {
659 return float32_maybe_silence_nan(a, status);
660 }
661 }
662
663 /*----------------------------------------------------------------------------
664 | Takes three single-precision floating-point values `a', `b' and `c', one of
665 | which is a NaN, and returns the appropriate NaN result. If any of `a',
666 | `b' or `c' is a signaling NaN, the invalid exception is raised.
667 | The input infzero indicates whether a*b was 0*inf or inf*0 (in which case
668 | obviously c is a NaN, and whether to propagate c or some other NaN is
669 | implementation defined).
670 *----------------------------------------------------------------------------*/
671
672 static float32 propagateFloat32MulAddNaN(float32 a, float32 b,
673 float32 c, flag infzero,
674 float_status *status)
675 {
676 flag aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN,
677 cIsQuietNaN, cIsSignalingNaN;
678 int which;
679
680 aIsQuietNaN = float32_is_quiet_nan(a, status);
681 aIsSignalingNaN = float32_is_signaling_nan(a, status);
682 bIsQuietNaN = float32_is_quiet_nan(b, status);
683 bIsSignalingNaN = float32_is_signaling_nan(b, status);
684 cIsQuietNaN = float32_is_quiet_nan(c, status);
685 cIsSignalingNaN = float32_is_signaling_nan(c, status);
686
687 if (aIsSignalingNaN | bIsSignalingNaN | cIsSignalingNaN) {
688 float_raise(float_flag_invalid, status);
689 }
690
691 which = pickNaNMulAdd(aIsQuietNaN, aIsSignalingNaN,
692 bIsQuietNaN, bIsSignalingNaN,
693 cIsQuietNaN, cIsSignalingNaN, infzero, status);
694
695 if (status->default_nan_mode) {
696 /* Note that this check is after pickNaNMulAdd so that function
697 * has an opportunity to set the Invalid flag.
698 */
699 return float32_default_nan(status);
700 }
701
702 switch (which) {
703 case 0:
704 return float32_maybe_silence_nan(a, status);
705 case 1:
706 return float32_maybe_silence_nan(b, status);
707 case 2:
708 return float32_maybe_silence_nan(c, status);
709 case 3:
710 default:
711 return float32_default_nan(status);
712 }
713 }
714
715 #ifdef NO_SIGNALING_NANS
716 int float64_is_quiet_nan(float64 a_, float_status *status)
717 {
718 return float64_is_any_nan(a_);
719 }
720
721 int float64_is_signaling_nan(float64 a_, float_status *status)
722 {
723 return 0;
724 }
725 #else
726 /*----------------------------------------------------------------------------
727 | Returns 1 if the double-precision floating-point value `a' is a quiet
728 | NaN; otherwise returns 0.
729 *----------------------------------------------------------------------------*/
730
731 int float64_is_quiet_nan(float64 a_, float_status *status)
732 {
733 uint64_t a = float64_val(a_);
734 if (status->snan_bit_is_one) {
735 return (((a >> 51) & 0xFFF) == 0xFFE)
736 && (a & 0x0007FFFFFFFFFFFFULL);
737 } else {
738 return ((a << 1) >= 0xFFF0000000000000ULL);
739 }
740 }
741
742 /*----------------------------------------------------------------------------
743 | Returns 1 if the double-precision floating-point value `a' is a signaling
744 | NaN; otherwise returns 0.
745 *----------------------------------------------------------------------------*/
746
747 int float64_is_signaling_nan(float64 a_, float_status *status)
748 {
749 uint64_t a = float64_val(a_);
750 if (status->snan_bit_is_one) {
751 return ((a << 1) >= 0xFFF0000000000000ULL);
752 } else {
753 return (((a >> 51) & 0xFFF) == 0xFFE)
754 && (a & LIT64(0x0007FFFFFFFFFFFF));
755 }
756 }
757 #endif
758
759 /*----------------------------------------------------------------------------
760 | Returns a quiet NaN if the double-precision floating point value `a' is a
761 | signaling NaN; otherwise returns `a'.
762 *----------------------------------------------------------------------------*/
763
764 float64 float64_maybe_silence_nan(float64 a_, float_status *status)
765 {
766 if (float64_is_signaling_nan(a_, status)) {
767 if (status->snan_bit_is_one) {
768 return float64_default_nan(status);
769 } else {
770 uint64_t a = float64_val(a_);
771 a |= LIT64(0x0008000000000000);
772 return make_float64(a);
773 }
774 }
775 return a_;
776 }
777
778 /*----------------------------------------------------------------------------
779 | Returns the result of converting the double-precision floating-point NaN
780 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
781 | exception is raised.
782 *----------------------------------------------------------------------------*/
783
784 static commonNaNT float64ToCommonNaN(float64 a, float_status *status)
785 {
786 commonNaNT z;
787
788 if (float64_is_signaling_nan(a, status)) {
789 float_raise(float_flag_invalid, status);
790 }
791 z.sign = float64_val(a) >> 63;
792 z.low = 0;
793 z.high = float64_val(a) << 12;
794 return z;
795 }
796
797 /*----------------------------------------------------------------------------
798 | Returns the result of converting the canonical NaN `a' to the double-
799 | precision floating-point format.
800 *----------------------------------------------------------------------------*/
801
802 static float64 commonNaNToFloat64(commonNaNT a, float_status *status)
803 {
804 uint64_t mantissa = a.high >> 12;
805
806 if (status->default_nan_mode) {
807 return float64_default_nan(status);
808 }
809
810 if (mantissa) {
811 return make_float64(
812 (((uint64_t) a.sign) << 63)
813 | LIT64(0x7FF0000000000000)
814 | (a.high >> 12));
815 } else {
816 return float64_default_nan(status);
817 }
818 }
819
820 /*----------------------------------------------------------------------------
821 | Takes two double-precision floating-point values `a' and `b', one of which
822 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
823 | signaling NaN, the invalid exception is raised.
824 *----------------------------------------------------------------------------*/
825
826 static float64 propagateFloat64NaN(float64 a, float64 b, float_status *status)
827 {
828 flag aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN;
829 flag aIsLargerSignificand;
830 uint64_t av, bv;
831
832 aIsQuietNaN = float64_is_quiet_nan(a, status);
833 aIsSignalingNaN = float64_is_signaling_nan(a, status);
834 bIsQuietNaN = float64_is_quiet_nan(b, status);
835 bIsSignalingNaN = float64_is_signaling_nan(b, status);
836 av = float64_val(a);
837 bv = float64_val(b);
838
839 if (aIsSignalingNaN | bIsSignalingNaN) {
840 float_raise(float_flag_invalid, status);
841 }
842
843 if (status->default_nan_mode) {
844 return float64_default_nan(status);
845 }
846
847 if ((uint64_t)(av << 1) < (uint64_t)(bv << 1)) {
848 aIsLargerSignificand = 0;
849 } else if ((uint64_t)(bv << 1) < (uint64_t)(av << 1)) {
850 aIsLargerSignificand = 1;
851 } else {
852 aIsLargerSignificand = (av < bv) ? 1 : 0;
853 }
854
855 if (pickNaN(aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN,
856 aIsLargerSignificand)) {
857 return float64_maybe_silence_nan(b, status);
858 } else {
859 return float64_maybe_silence_nan(a, status);
860 }
861 }
862
863 /*----------------------------------------------------------------------------
864 | Takes three double-precision floating-point values `a', `b' and `c', one of
865 | which is a NaN, and returns the appropriate NaN result. If any of `a',
866 | `b' or `c' is a signaling NaN, the invalid exception is raised.
867 | The input infzero indicates whether a*b was 0*inf or inf*0 (in which case
868 | obviously c is a NaN, and whether to propagate c or some other NaN is
869 | implementation defined).
870 *----------------------------------------------------------------------------*/
871
872 static float64 propagateFloat64MulAddNaN(float64 a, float64 b,
873 float64 c, flag infzero,
874 float_status *status)
875 {
876 flag aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN,
877 cIsQuietNaN, cIsSignalingNaN;
878 int which;
879
880 aIsQuietNaN = float64_is_quiet_nan(a, status);
881 aIsSignalingNaN = float64_is_signaling_nan(a, status);
882 bIsQuietNaN = float64_is_quiet_nan(b, status);
883 bIsSignalingNaN = float64_is_signaling_nan(b, status);
884 cIsQuietNaN = float64_is_quiet_nan(c, status);
885 cIsSignalingNaN = float64_is_signaling_nan(c, status);
886
887 if (aIsSignalingNaN | bIsSignalingNaN | cIsSignalingNaN) {
888 float_raise(float_flag_invalid, status);
889 }
890
891 which = pickNaNMulAdd(aIsQuietNaN, aIsSignalingNaN,
892 bIsQuietNaN, bIsSignalingNaN,
893 cIsQuietNaN, cIsSignalingNaN, infzero, status);
894
895 if (status->default_nan_mode) {
896 /* Note that this check is after pickNaNMulAdd so that function
897 * has an opportunity to set the Invalid flag.
898 */
899 return float64_default_nan(status);
900 }
901
902 switch (which) {
903 case 0:
904 return float64_maybe_silence_nan(a, status);
905 case 1:
906 return float64_maybe_silence_nan(b, status);
907 case 2:
908 return float64_maybe_silence_nan(c, status);
909 case 3:
910 default:
911 return float64_default_nan(status);
912 }
913 }
914
915 #ifdef NO_SIGNALING_NANS
916 int floatx80_is_quiet_nan(floatx80 a_, float_status *status)
917 {
918 return floatx80_is_any_nan(a_);
919 }
920
921 int floatx80_is_signaling_nan(floatx80 a_, float_status *status)
922 {
923 return 0;
924 }
925 #else
926 /*----------------------------------------------------------------------------
927 | Returns 1 if the extended double-precision floating-point value `a' is a
928 | quiet NaN; otherwise returns 0. This slightly differs from the same
929 | function for other types as floatx80 has an explicit bit.
930 *----------------------------------------------------------------------------*/
931
932 int floatx80_is_quiet_nan(floatx80 a, float_status *status)
933 {
934 if (status->snan_bit_is_one) {
935 uint64_t aLow;
936
937 aLow = a.low & ~0x4000000000000000ULL;
938 return ((a.high & 0x7FFF) == 0x7FFF)
939 && (aLow << 1)
940 && (a.low == aLow);
941 } else {
942 return ((a.high & 0x7FFF) == 0x7FFF)
943 && (LIT64(0x8000000000000000) <= ((uint64_t)(a.low << 1)));
944 }
945 }
946
947 /*----------------------------------------------------------------------------
948 | Returns 1 if the extended double-precision floating-point value `a' is a
949 | signaling NaN; otherwise returns 0. This slightly differs from the same
950 | function for other types as floatx80 has an explicit bit.
951 *----------------------------------------------------------------------------*/
952
953 int floatx80_is_signaling_nan(floatx80 a, float_status *status)
954 {
955 if (status->snan_bit_is_one) {
956 return ((a.high & 0x7FFF) == 0x7FFF)
957 && ((a.low << 1) >= 0x8000000000000000ULL);
958 } else {
959 uint64_t aLow;
960
961 aLow = a.low & ~LIT64(0x4000000000000000);
962 return ((a.high & 0x7FFF) == 0x7FFF)
963 && (uint64_t)(aLow << 1)
964 && (a.low == aLow);
965 }
966 }
967 #endif
968
969 /*----------------------------------------------------------------------------
970 | Returns a quiet NaN if the extended double-precision floating point value
971 | `a' is a signaling NaN; otherwise returns `a'.
972 *----------------------------------------------------------------------------*/
973
974 floatx80 floatx80_maybe_silence_nan(floatx80 a, float_status *status)
975 {
976 if (floatx80_is_signaling_nan(a, status)) {
977 if (status->snan_bit_is_one) {
978 a = floatx80_default_nan(status);
979 } else {
980 a.low |= LIT64(0xC000000000000000);
981 return a;
982 }
983 }
984 return a;
985 }
986
987 /*----------------------------------------------------------------------------
988 | Returns the result of converting the extended double-precision floating-
989 | point NaN `a' to the canonical NaN format. If `a' is a signaling NaN, the
990 | invalid exception is raised.
991 *----------------------------------------------------------------------------*/
992
993 static commonNaNT floatx80ToCommonNaN(floatx80 a, float_status *status)
994 {
995 floatx80 dflt;
996 commonNaNT z;
997
998 if (floatx80_is_signaling_nan(a, status)) {
999 float_raise(float_flag_invalid, status);
1000 }
1001 if (a.low >> 63) {
1002 z.sign = a.high >> 15;
1003 z.low = 0;
1004 z.high = a.low << 1;
1005 } else {
1006 dflt = floatx80_default_nan(status);
1007 z.sign = dflt.high >> 15;
1008 z.low = 0;
1009 z.high = dflt.low << 1;
1010 }
1011 return z;
1012 }
1013
1014 /*----------------------------------------------------------------------------
1015 | Returns the result of converting the canonical NaN `a' to the extended
1016 | double-precision floating-point format.
1017 *----------------------------------------------------------------------------*/
1018
1019 static floatx80 commonNaNToFloatx80(commonNaNT a, float_status *status)
1020 {
1021 floatx80 z;
1022
1023 if (status->default_nan_mode) {
1024 return floatx80_default_nan(status);
1025 }
1026
1027 if (a.high >> 1) {
1028 z.low = LIT64(0x8000000000000000) | a.high >> 1;
1029 z.high = (((uint16_t)a.sign) << 15) | 0x7FFF;
1030 } else {
1031 z = floatx80_default_nan(status);
1032 }
1033 return z;
1034 }
1035
1036 /*----------------------------------------------------------------------------
1037 | Takes two extended double-precision floating-point values `a' and `b', one
1038 | of which is a NaN, and returns the appropriate NaN result. If either `a' or
1039 | `b' is a signaling NaN, the invalid exception is raised.
1040 *----------------------------------------------------------------------------*/
1041
1042 static floatx80 propagateFloatx80NaN(floatx80 a, floatx80 b,
1043 float_status *status)
1044 {
1045 flag aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN;
1046 flag aIsLargerSignificand;
1047
1048 aIsQuietNaN = floatx80_is_quiet_nan(a, status);
1049 aIsSignalingNaN = floatx80_is_signaling_nan(a, status);
1050 bIsQuietNaN = floatx80_is_quiet_nan(b, status);
1051 bIsSignalingNaN = floatx80_is_signaling_nan(b, status);
1052
1053 if (aIsSignalingNaN | bIsSignalingNaN) {
1054 float_raise(float_flag_invalid, status);
1055 }
1056
1057 if (status->default_nan_mode) {
1058 return floatx80_default_nan(status);
1059 }
1060
1061 if (a.low < b.low) {
1062 aIsLargerSignificand = 0;
1063 } else if (b.low < a.low) {
1064 aIsLargerSignificand = 1;
1065 } else {
1066 aIsLargerSignificand = (a.high < b.high) ? 1 : 0;
1067 }
1068
1069 if (pickNaN(aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN,
1070 aIsLargerSignificand)) {
1071 return floatx80_maybe_silence_nan(b, status);
1072 } else {
1073 return floatx80_maybe_silence_nan(a, status);
1074 }
1075 }
1076
1077 #ifdef NO_SIGNALING_NANS
1078 int float128_is_quiet_nan(float128 a_, float_status *status)
1079 {
1080 return float128_is_any_nan(a_);
1081 }
1082
1083 int float128_is_signaling_nan(float128 a_, float_status *status)
1084 {
1085 return 0;
1086 }
1087 #else
1088 /*----------------------------------------------------------------------------
1089 | Returns 1 if the quadruple-precision floating-point value `a' is a quiet
1090 | NaN; otherwise returns 0.
1091 *----------------------------------------------------------------------------*/
1092
1093 int float128_is_quiet_nan(float128 a, float_status *status)
1094 {
1095 if (status->snan_bit_is_one) {
1096 return (((a.high >> 47) & 0xFFFF) == 0xFFFE)
1097 && (a.low || (a.high & 0x00007FFFFFFFFFFFULL));
1098 } else {
1099 return ((a.high << 1) >= 0xFFFF000000000000ULL)
1100 && (a.low || (a.high & 0x0000FFFFFFFFFFFFULL));
1101 }
1102 }
1103
1104 /*----------------------------------------------------------------------------
1105 | Returns 1 if the quadruple-precision floating-point value `a' is a
1106 | signaling NaN; otherwise returns 0.
1107 *----------------------------------------------------------------------------*/
1108
1109 int float128_is_signaling_nan(float128 a, float_status *status)
1110 {
1111 if (status->snan_bit_is_one) {
1112 return ((a.high << 1) >= 0xFFFF000000000000ULL)
1113 && (a.low || (a.high & 0x0000FFFFFFFFFFFFULL));
1114 } else {
1115 return (((a.high >> 47) & 0xFFFF) == 0xFFFE)
1116 && (a.low || (a.high & LIT64(0x00007FFFFFFFFFFF)));
1117 }
1118 }
1119 #endif
1120
1121 /*----------------------------------------------------------------------------
1122 | Returns a quiet NaN if the quadruple-precision floating point value `a' is
1123 | a signaling NaN; otherwise returns `a'.
1124 *----------------------------------------------------------------------------*/
1125
1126 float128 float128_maybe_silence_nan(float128 a, float_status *status)
1127 {
1128 if (float128_is_signaling_nan(a, status)) {
1129 if (status->snan_bit_is_one) {
1130 a = float128_default_nan(status);
1131 } else {
1132 a.high |= LIT64(0x0000800000000000);
1133 return a;
1134 }
1135 }
1136 return a;
1137 }
1138
1139 /*----------------------------------------------------------------------------
1140 | Returns the result of converting the quadruple-precision floating-point NaN
1141 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
1142 | exception is raised.
1143 *----------------------------------------------------------------------------*/
1144
1145 static commonNaNT float128ToCommonNaN(float128 a, float_status *status)
1146 {
1147 commonNaNT z;
1148
1149 if (float128_is_signaling_nan(a, status)) {
1150 float_raise(float_flag_invalid, status);
1151 }
1152 z.sign = a.high >> 63;
1153 shortShift128Left(a.high, a.low, 16, &z.high, &z.low);
1154 return z;
1155 }
1156
1157 /*----------------------------------------------------------------------------
1158 | Returns the result of converting the canonical NaN `a' to the quadruple-
1159 | precision floating-point format.
1160 *----------------------------------------------------------------------------*/
1161
1162 static float128 commonNaNToFloat128(commonNaNT a, float_status *status)
1163 {
1164 float128 z;
1165
1166 if (status->default_nan_mode) {
1167 return float128_default_nan(status);
1168 }
1169
1170 shift128Right(a.high, a.low, 16, &z.high, &z.low);
1171 z.high |= (((uint64_t)a.sign) << 63) | LIT64(0x7FFF000000000000);
1172 return z;
1173 }
1174
1175 /*----------------------------------------------------------------------------
1176 | Takes two quadruple-precision floating-point values `a' and `b', one of
1177 | which is a NaN, and returns the appropriate NaN result. If either `a' or
1178 | `b' is a signaling NaN, the invalid exception is raised.
1179 *----------------------------------------------------------------------------*/
1180
1181 static float128 propagateFloat128NaN(float128 a, float128 b,
1182 float_status *status)
1183 {
1184 flag aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN;
1185 flag aIsLargerSignificand;
1186
1187 aIsQuietNaN = float128_is_quiet_nan(a, status);
1188 aIsSignalingNaN = float128_is_signaling_nan(a, status);
1189 bIsQuietNaN = float128_is_quiet_nan(b, status);
1190 bIsSignalingNaN = float128_is_signaling_nan(b, status);
1191
1192 if (aIsSignalingNaN | bIsSignalingNaN) {
1193 float_raise(float_flag_invalid, status);
1194 }
1195
1196 if (status->default_nan_mode) {
1197 return float128_default_nan(status);
1198 }
1199
1200 if (lt128(a.high << 1, a.low, b.high << 1, b.low)) {
1201 aIsLargerSignificand = 0;
1202 } else if (lt128(b.high << 1, b.low, a.high << 1, a.low)) {
1203 aIsLargerSignificand = 1;
1204 } else {
1205 aIsLargerSignificand = (a.high < b.high) ? 1 : 0;
1206 }
1207
1208 if (pickNaN(aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN,
1209 aIsLargerSignificand)) {
1210 return float128_maybe_silence_nan(b, status);
1211 } else {
1212 return float128_maybe_silence_nan(a, status);
1213 }
1214 }