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