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