]> git.proxmox.com Git - mirror_qemu.git/blob - fpu/softfloat-specialize.h
Merge remote branch 'kwolf/for-anthony' into staging
[mirror_qemu.git] / fpu / softfloat-specialize.h
1
2 /*============================================================================
3
4 This C source fragment is part of the SoftFloat IEC/IEEE Floating-point
5 Arithmetic Package, Release 2b.
6
7 Written by John R. Hauser. This work was made possible in part by the
8 International Computer Science Institute, located at Suite 600, 1947 Center
9 Street, Berkeley, California 94704. Funding was partially provided by the
10 National Science Foundation under grant MIP-9311980. The original version
11 of this code was written as part of a project to build a fixed-point vector
12 processor in collaboration with the University of California at Berkeley,
13 overseen by Profs. Nelson Morgan and John Wawrzynek. More information
14 is available through the Web page `http://www.cs.berkeley.edu/~jhauser/
15 arithmetic/SoftFloat.html'.
16
17 THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort has
18 been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT TIMES
19 RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO PERSONS
20 AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ALL LOSSES,
21 COSTS, OR OTHER PROBLEMS THEY INCUR DUE TO THE SOFTWARE, AND WHO FURTHERMORE
22 EFFECTIVELY INDEMNIFY JOHN HAUSER AND THE INTERNATIONAL COMPUTER SCIENCE
23 INSTITUTE (possibly via similar legal warning) AGAINST ALL LOSSES, COSTS, OR
24 OTHER PROBLEMS INCURRED BY THEIR CUSTOMERS AND CLIENTS DUE TO THE SOFTWARE.
25
26 Derivative works are acceptable, even for commercial purposes, so long as
27 (1) the source code for the derivative work includes prominent notice that
28 the work is derivative, and (2) the source code includes prominent notice with
29 these four paragraphs for those parts of this code that are retained.
30
31 =============================================================================*/
32
33 #if defined(TARGET_MIPS) || defined(TARGET_SH4)
34 #define SNAN_BIT_IS_ONE 1
35 #else
36 #define SNAN_BIT_IS_ONE 0
37 #endif
38
39 /*----------------------------------------------------------------------------
40 | Raises the exceptions specified by `flags'. Floating-point traps can be
41 | defined here if desired. It is currently not possible for such a trap
42 | to substitute a result value. If traps are not implemented, this routine
43 | should be simply `float_exception_flags |= flags;'.
44 *----------------------------------------------------------------------------*/
45
46 void float_raise( int8 flags STATUS_PARAM )
47 {
48 STATUS(float_exception_flags) |= flags;
49 }
50
51 /*----------------------------------------------------------------------------
52 | Internal canonical NaN format.
53 *----------------------------------------------------------------------------*/
54 typedef struct {
55 flag sign;
56 bits64 high, low;
57 } commonNaNT;
58
59 /*----------------------------------------------------------------------------
60 | The pattern for a default generated half-precision NaN.
61 *----------------------------------------------------------------------------*/
62 #if defined(TARGET_ARM)
63 #define float16_default_nan make_float16(0x7E00)
64 #elif SNAN_BIT_IS_ONE
65 #define float16_default_nan make_float16(0x7DFF)
66 #else
67 #define float16_default_nan make_float16(0xFE00)
68 #endif
69
70 /*----------------------------------------------------------------------------
71 | Returns 1 if the half-precision floating-point value `a' is a quiet
72 | NaN; otherwise returns 0.
73 *----------------------------------------------------------------------------*/
74
75 int float16_is_quiet_nan(float16 a_)
76 {
77 uint16_t a = float16_val(a_);
78 #if SNAN_BIT_IS_ONE
79 return (((a >> 9) & 0x3F) == 0x3E) && (a & 0x1FF);
80 #else
81 return ((a & ~0x8000) >= 0x7c80);
82 #endif
83 }
84
85 /*----------------------------------------------------------------------------
86 | Returns 1 if the half-precision floating-point value `a' is a signaling
87 | NaN; otherwise returns 0.
88 *----------------------------------------------------------------------------*/
89
90 int float16_is_signaling_nan(float16 a_)
91 {
92 uint16_t a = float16_val(a_);
93 #if SNAN_BIT_IS_ONE
94 return ((a & ~0x8000) >= 0x7c80);
95 #else
96 return (((a >> 9) & 0x3F) == 0x3E) && (a & 0x1FF);
97 #endif
98 }
99
100 /*----------------------------------------------------------------------------
101 | Returns a quiet NaN if the half-precision floating point value `a' is a
102 | signaling NaN; otherwise returns `a'.
103 *----------------------------------------------------------------------------*/
104 float16 float16_maybe_silence_nan(float16 a_)
105 {
106 if (float16_is_signaling_nan(a_)) {
107 #if SNAN_BIT_IS_ONE
108 # if defined(TARGET_MIPS) || defined(TARGET_SH4)
109 return float16_default_nan;
110 # else
111 # error Rules for silencing a signaling NaN are target-specific
112 # endif
113 #else
114 uint16_t a = float16_val(a_);
115 a |= (1 << 9);
116 return make_float16(a);
117 #endif
118 }
119 return a_;
120 }
121
122 /*----------------------------------------------------------------------------
123 | Returns the result of converting the half-precision floating-point NaN
124 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
125 | exception is raised.
126 *----------------------------------------------------------------------------*/
127
128 static commonNaNT float16ToCommonNaN( float16 a STATUS_PARAM )
129 {
130 commonNaNT z;
131
132 if ( float16_is_signaling_nan( a ) ) float_raise( float_flag_invalid STATUS_VAR );
133 z.sign = float16_val(a) >> 15;
134 z.low = 0;
135 z.high = ((bits64) float16_val(a))<<54;
136 return z;
137 }
138
139 /*----------------------------------------------------------------------------
140 | Returns the result of converting the canonical NaN `a' to the half-
141 | precision floating-point format.
142 *----------------------------------------------------------------------------*/
143
144 static float16 commonNaNToFloat16(commonNaNT a STATUS_PARAM)
145 {
146 uint16_t mantissa = a.high>>54;
147
148 if (STATUS(default_nan_mode)) {
149 return float16_default_nan;
150 }
151
152 if (mantissa) {
153 return make_float16(((((uint16_t) a.sign) << 15)
154 | (0x1F << 10) | mantissa));
155 } else {
156 return float16_default_nan;
157 }
158 }
159
160 /*----------------------------------------------------------------------------
161 | The pattern for a default generated single-precision NaN.
162 *----------------------------------------------------------------------------*/
163 #if defined(TARGET_SPARC)
164 #define float32_default_nan make_float32(0x7FFFFFFF)
165 #elif defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_ALPHA)
166 #define float32_default_nan make_float32(0x7FC00000)
167 #elif SNAN_BIT_IS_ONE
168 #define float32_default_nan make_float32(0x7FBFFFFF)
169 #else
170 #define float32_default_nan make_float32(0xFFC00000)
171 #endif
172
173 /*----------------------------------------------------------------------------
174 | Returns 1 if the single-precision floating-point value `a' is a quiet
175 | NaN; otherwise returns 0.
176 *----------------------------------------------------------------------------*/
177
178 int float32_is_quiet_nan( float32 a_ )
179 {
180 uint32_t a = float32_val(a_);
181 #if SNAN_BIT_IS_ONE
182 return ( ( ( a>>22 ) & 0x1FF ) == 0x1FE ) && ( a & 0x003FFFFF );
183 #else
184 return ( 0xFF800000 <= (bits32) ( a<<1 ) );
185 #endif
186 }
187
188 /*----------------------------------------------------------------------------
189 | Returns 1 if the single-precision floating-point value `a' is a signaling
190 | NaN; otherwise returns 0.
191 *----------------------------------------------------------------------------*/
192
193 int float32_is_signaling_nan( float32 a_ )
194 {
195 uint32_t a = float32_val(a_);
196 #if SNAN_BIT_IS_ONE
197 return ( 0xFF800000 <= (bits32) ( a<<1 ) );
198 #else
199 return ( ( ( a>>22 ) & 0x1FF ) == 0x1FE ) && ( a & 0x003FFFFF );
200 #endif
201 }
202
203 /*----------------------------------------------------------------------------
204 | Returns a quiet NaN if the single-precision floating point value `a' is a
205 | signaling NaN; otherwise returns `a'.
206 *----------------------------------------------------------------------------*/
207
208 float32 float32_maybe_silence_nan( float32 a_ )
209 {
210 if (float32_is_signaling_nan(a_)) {
211 #if SNAN_BIT_IS_ONE
212 # if defined(TARGET_MIPS) || defined(TARGET_SH4)
213 return float32_default_nan;
214 # else
215 # error Rules for silencing a signaling NaN are target-specific
216 # endif
217 #else
218 bits32 a = float32_val(a_);
219 a |= (1 << 22);
220 return make_float32(a);
221 #endif
222 }
223 return a_;
224 }
225
226 /*----------------------------------------------------------------------------
227 | Returns the result of converting the single-precision floating-point NaN
228 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
229 | exception is raised.
230 *----------------------------------------------------------------------------*/
231
232 static commonNaNT float32ToCommonNaN( float32 a STATUS_PARAM )
233 {
234 commonNaNT z;
235
236 if ( float32_is_signaling_nan( a ) ) float_raise( float_flag_invalid STATUS_VAR );
237 z.sign = float32_val(a)>>31;
238 z.low = 0;
239 z.high = ( (bits64) float32_val(a) )<<41;
240 return z;
241 }
242
243 /*----------------------------------------------------------------------------
244 | Returns the result of converting the canonical NaN `a' to the single-
245 | precision floating-point format.
246 *----------------------------------------------------------------------------*/
247
248 static float32 commonNaNToFloat32( commonNaNT a STATUS_PARAM)
249 {
250 bits32 mantissa = a.high>>41;
251
252 if ( STATUS(default_nan_mode) ) {
253 return float32_default_nan;
254 }
255
256 if ( mantissa )
257 return make_float32(
258 ( ( (bits32) a.sign )<<31 ) | 0x7F800000 | ( a.high>>41 ) );
259 else
260 return float32_default_nan;
261 }
262
263 /*----------------------------------------------------------------------------
264 | Select which NaN to propagate for a two-input operation.
265 | IEEE754 doesn't specify all the details of this, so the
266 | algorithm is target-specific.
267 | The routine is passed various bits of information about the
268 | two NaNs and should return 0 to select NaN a and 1 for NaN b.
269 | Note that signalling NaNs are always squashed to quiet NaNs
270 | by the caller, by calling floatXX_maybe_silence_nan() before
271 | returning them.
272 |
273 | aIsLargerSignificand is only valid if both a and b are NaNs
274 | of some kind, and is true if a has the larger significand,
275 | or if both a and b have the same significand but a is
276 | positive but b is negative. It is only needed for the x87
277 | tie-break rule.
278 *----------------------------------------------------------------------------*/
279
280 #if defined(TARGET_ARM)
281 static int pickNaN(flag aIsQNaN, flag aIsSNaN, flag bIsQNaN, flag bIsSNaN,
282 flag aIsLargerSignificand)
283 {
284 /* ARM mandated NaN propagation rules: take the first of:
285 * 1. A if it is signaling
286 * 2. B if it is signaling
287 * 3. A (quiet)
288 * 4. B (quiet)
289 * A signaling NaN is always quietened before returning it.
290 */
291 if (aIsSNaN) {
292 return 0;
293 } else if (bIsSNaN) {
294 return 1;
295 } else if (aIsQNaN) {
296 return 0;
297 } else {
298 return 1;
299 }
300 }
301 #elif defined(TARGET_MIPS)
302 static int pickNaN(flag aIsQNaN, flag aIsSNaN, flag bIsQNaN, flag bIsSNaN,
303 flag aIsLargerSignificand)
304 {
305 /* According to MIPS specifications, if one of the two operands is
306 * a sNaN, a new qNaN has to be generated. This is done in
307 * floatXX_maybe_silence_nan(). For qNaN inputs the specifications
308 * says: "When possible, this QNaN result is one of the operand QNaN
309 * values." In practice it seems that most implementations choose
310 * the first operand if both operands are qNaN. In short this gives
311 * the following rules:
312 * 1. A if it is signaling
313 * 2. B if it is signaling
314 * 3. A (quiet)
315 * 4. B (quiet)
316 * A signaling NaN is always silenced before returning it.
317 */
318 if (aIsSNaN) {
319 return 0;
320 } else if (bIsSNaN) {
321 return 1;
322 } else if (aIsQNaN) {
323 return 0;
324 } else {
325 return 1;
326 }
327 }
328 #elif defined(TARGET_PPC)
329 static int pickNaN(flag aIsQNaN, flag aIsSNaN, flag bIsQNaN, flag bIsSNaN,
330 flag aIsLargerSignificand)
331 {
332 /* PowerPC propagation rules:
333 * 1. A if it sNaN or qNaN
334 * 2. B if it sNaN or qNaN
335 * A signaling NaN is always silenced before returning it.
336 */
337 if (aIsSNaN || aIsQNaN) {
338 return 0;
339 } else {
340 return 1;
341 }
342 }
343 #else
344 static int pickNaN(flag aIsQNaN, flag aIsSNaN, flag bIsQNaN, flag bIsSNaN,
345 flag aIsLargerSignificand)
346 {
347 /* This implements x87 NaN propagation rules:
348 * SNaN + QNaN => return the QNaN
349 * two SNaNs => return the one with the larger significand, silenced
350 * two QNaNs => return the one with the larger significand
351 * SNaN and a non-NaN => return the SNaN, silenced
352 * QNaN and a non-NaN => return the QNaN
353 *
354 * If we get down to comparing significands and they are the same,
355 * return the NaN with the positive sign bit (if any).
356 */
357 if (aIsSNaN) {
358 if (bIsSNaN) {
359 return aIsLargerSignificand ? 0 : 1;
360 }
361 return bIsQNaN ? 1 : 0;
362 }
363 else if (aIsQNaN) {
364 if (bIsSNaN || !bIsQNaN)
365 return 0;
366 else {
367 return aIsLargerSignificand ? 0 : 1;
368 }
369 } else {
370 return 1;
371 }
372 }
373 #endif
374
375 /*----------------------------------------------------------------------------
376 | Takes two single-precision floating-point values `a' and `b', one of which
377 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
378 | signaling NaN, the invalid exception is raised.
379 *----------------------------------------------------------------------------*/
380
381 static float32 propagateFloat32NaN( float32 a, float32 b STATUS_PARAM)
382 {
383 flag aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN;
384 flag aIsLargerSignificand;
385 bits32 av, bv;
386
387 aIsQuietNaN = float32_is_quiet_nan( a );
388 aIsSignalingNaN = float32_is_signaling_nan( a );
389 bIsQuietNaN = float32_is_quiet_nan( b );
390 bIsSignalingNaN = float32_is_signaling_nan( b );
391 av = float32_val(a);
392 bv = float32_val(b);
393
394 if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_invalid STATUS_VAR);
395
396 if ( STATUS(default_nan_mode) )
397 return float32_default_nan;
398
399 if ((bits32)(av<<1) < (bits32)(bv<<1)) {
400 aIsLargerSignificand = 0;
401 } else if ((bits32)(bv<<1) < (bits32)(av<<1)) {
402 aIsLargerSignificand = 1;
403 } else {
404 aIsLargerSignificand = (av < bv) ? 1 : 0;
405 }
406
407 if (pickNaN(aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN,
408 aIsLargerSignificand)) {
409 return float32_maybe_silence_nan(b);
410 } else {
411 return float32_maybe_silence_nan(a);
412 }
413 }
414
415 /*----------------------------------------------------------------------------
416 | The pattern for a default generated double-precision NaN.
417 *----------------------------------------------------------------------------*/
418 #if defined(TARGET_SPARC)
419 #define float64_default_nan make_float64(LIT64( 0x7FFFFFFFFFFFFFFF ))
420 #elif defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_ALPHA)
421 #define float64_default_nan make_float64(LIT64( 0x7FF8000000000000 ))
422 #elif SNAN_BIT_IS_ONE
423 #define float64_default_nan make_float64(LIT64( 0x7FF7FFFFFFFFFFFF ))
424 #else
425 #define float64_default_nan make_float64(LIT64( 0xFFF8000000000000 ))
426 #endif
427
428 /*----------------------------------------------------------------------------
429 | Returns 1 if the double-precision floating-point value `a' is a quiet
430 | NaN; otherwise returns 0.
431 *----------------------------------------------------------------------------*/
432
433 int float64_is_quiet_nan( float64 a_ )
434 {
435 bits64 a = float64_val(a_);
436 #if SNAN_BIT_IS_ONE
437 return
438 ( ( ( a>>51 ) & 0xFFF ) == 0xFFE )
439 && ( a & LIT64( 0x0007FFFFFFFFFFFF ) );
440 #else
441 return ( LIT64( 0xFFF0000000000000 ) <= (bits64) ( a<<1 ) );
442 #endif
443 }
444
445 /*----------------------------------------------------------------------------
446 | Returns 1 if the double-precision floating-point value `a' is a signaling
447 | NaN; otherwise returns 0.
448 *----------------------------------------------------------------------------*/
449
450 int float64_is_signaling_nan( float64 a_ )
451 {
452 bits64 a = float64_val(a_);
453 #if SNAN_BIT_IS_ONE
454 return ( LIT64( 0xFFF0000000000000 ) <= (bits64) ( a<<1 ) );
455 #else
456 return
457 ( ( ( a>>51 ) & 0xFFF ) == 0xFFE )
458 && ( a & LIT64( 0x0007FFFFFFFFFFFF ) );
459 #endif
460 }
461
462 /*----------------------------------------------------------------------------
463 | Returns a quiet NaN if the double-precision floating point value `a' is a
464 | signaling NaN; otherwise returns `a'.
465 *----------------------------------------------------------------------------*/
466
467 float64 float64_maybe_silence_nan( float64 a_ )
468 {
469 if (float64_is_signaling_nan(a_)) {
470 #if SNAN_BIT_IS_ONE
471 # if defined(TARGET_MIPS) || defined(TARGET_SH4)
472 return float64_default_nan;
473 # else
474 # error Rules for silencing a signaling NaN are target-specific
475 # endif
476 #else
477 bits64 a = float64_val(a_);
478 a |= LIT64( 0x0008000000000000 );
479 return make_float64(a);
480 #endif
481 }
482 return a_;
483 }
484
485 /*----------------------------------------------------------------------------
486 | Returns the result of converting the double-precision floating-point NaN
487 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
488 | exception is raised.
489 *----------------------------------------------------------------------------*/
490
491 static commonNaNT float64ToCommonNaN( float64 a STATUS_PARAM)
492 {
493 commonNaNT z;
494
495 if ( float64_is_signaling_nan( a ) ) float_raise( float_flag_invalid STATUS_VAR);
496 z.sign = float64_val(a)>>63;
497 z.low = 0;
498 z.high = float64_val(a)<<12;
499 return z;
500 }
501
502 /*----------------------------------------------------------------------------
503 | Returns the result of converting the canonical NaN `a' to the double-
504 | precision floating-point format.
505 *----------------------------------------------------------------------------*/
506
507 static float64 commonNaNToFloat64( commonNaNT a STATUS_PARAM)
508 {
509 bits64 mantissa = a.high>>12;
510
511 if ( STATUS(default_nan_mode) ) {
512 return float64_default_nan;
513 }
514
515 if ( mantissa )
516 return make_float64(
517 ( ( (bits64) a.sign )<<63 )
518 | LIT64( 0x7FF0000000000000 )
519 | ( a.high>>12 ));
520 else
521 return float64_default_nan;
522 }
523
524 /*----------------------------------------------------------------------------
525 | Takes two double-precision floating-point values `a' and `b', one of which
526 | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
527 | signaling NaN, the invalid exception is raised.
528 *----------------------------------------------------------------------------*/
529
530 static float64 propagateFloat64NaN( float64 a, float64 b STATUS_PARAM)
531 {
532 flag aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN;
533 flag aIsLargerSignificand;
534 bits64 av, bv;
535
536 aIsQuietNaN = float64_is_quiet_nan( a );
537 aIsSignalingNaN = float64_is_signaling_nan( a );
538 bIsQuietNaN = float64_is_quiet_nan( b );
539 bIsSignalingNaN = float64_is_signaling_nan( b );
540 av = float64_val(a);
541 bv = float64_val(b);
542
543 if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_invalid STATUS_VAR);
544
545 if ( STATUS(default_nan_mode) )
546 return float64_default_nan;
547
548 if ((bits64)(av<<1) < (bits64)(bv<<1)) {
549 aIsLargerSignificand = 0;
550 } else if ((bits64)(bv<<1) < (bits64)(av<<1)) {
551 aIsLargerSignificand = 1;
552 } else {
553 aIsLargerSignificand = (av < bv) ? 1 : 0;
554 }
555
556 if (pickNaN(aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN,
557 aIsLargerSignificand)) {
558 return float64_maybe_silence_nan(b);
559 } else {
560 return float64_maybe_silence_nan(a);
561 }
562 }
563
564 #ifdef FLOATX80
565
566 /*----------------------------------------------------------------------------
567 | The pattern for a default generated extended double-precision NaN. The
568 | `high' and `low' values hold the most- and least-significant bits,
569 | respectively.
570 *----------------------------------------------------------------------------*/
571 #if SNAN_BIT_IS_ONE
572 #define floatx80_default_nan_high 0x7FFF
573 #define floatx80_default_nan_low LIT64( 0xBFFFFFFFFFFFFFFF )
574 #else
575 #define floatx80_default_nan_high 0xFFFF
576 #define floatx80_default_nan_low LIT64( 0xC000000000000000 )
577 #endif
578
579 /*----------------------------------------------------------------------------
580 | Returns 1 if the extended double-precision floating-point value `a' is a
581 | quiet NaN; otherwise returns 0. This slightly differs from the same
582 | function for other types as floatx80 has an explicit bit.
583 *----------------------------------------------------------------------------*/
584
585 int floatx80_is_quiet_nan( floatx80 a )
586 {
587 #if SNAN_BIT_IS_ONE
588 bits64 aLow;
589
590 aLow = a.low & ~ LIT64( 0x4000000000000000 );
591 return
592 ( ( a.high & 0x7FFF ) == 0x7FFF )
593 && (bits64) ( aLow<<1 )
594 && ( a.low == aLow );
595 #else
596 return ( ( a.high & 0x7FFF ) == 0x7FFF )
597 && (LIT64( 0x8000000000000000 ) <= ((bits64) ( a.low<<1 )));
598 #endif
599 }
600
601 /*----------------------------------------------------------------------------
602 | Returns 1 if the extended double-precision floating-point value `a' is a
603 | signaling NaN; otherwise returns 0. This slightly differs from the same
604 | function for other types as floatx80 has an explicit bit.
605 *----------------------------------------------------------------------------*/
606
607 int floatx80_is_signaling_nan( floatx80 a )
608 {
609 #if SNAN_BIT_IS_ONE
610 return ( ( a.high & 0x7FFF ) == 0x7FFF )
611 && (LIT64( 0x8000000000000000 ) <= ((bits64) ( a.low<<1 )));
612 #else
613 bits64 aLow;
614
615 aLow = a.low & ~ LIT64( 0x4000000000000000 );
616 return
617 ( ( a.high & 0x7FFF ) == 0x7FFF )
618 && (bits64) ( aLow<<1 )
619 && ( a.low == aLow );
620 #endif
621 }
622
623 /*----------------------------------------------------------------------------
624 | Returns a quiet NaN if the extended double-precision floating point value
625 | `a' is a signaling NaN; otherwise returns `a'.
626 *----------------------------------------------------------------------------*/
627
628 floatx80 floatx80_maybe_silence_nan( floatx80 a )
629 {
630 if (floatx80_is_signaling_nan(a)) {
631 #if SNAN_BIT_IS_ONE
632 # if defined(TARGET_MIPS) || defined(TARGET_SH4)
633 a.low = floatx80_default_nan_low;
634 a.high = floatx80_default_nan_high;
635 # else
636 # error Rules for silencing a signaling NaN are target-specific
637 # endif
638 #else
639 a.low |= LIT64( 0xC000000000000000 );
640 return a;
641 #endif
642 }
643 return a;
644 }
645
646 /*----------------------------------------------------------------------------
647 | Returns the result of converting the extended double-precision floating-
648 | point NaN `a' to the canonical NaN format. If `a' is a signaling NaN, the
649 | invalid exception is raised.
650 *----------------------------------------------------------------------------*/
651
652 static commonNaNT floatx80ToCommonNaN( floatx80 a STATUS_PARAM)
653 {
654 commonNaNT z;
655
656 if ( floatx80_is_signaling_nan( a ) ) float_raise( float_flag_invalid STATUS_VAR);
657 z.sign = a.high>>15;
658 z.low = 0;
659 z.high = a.low;
660 return z;
661 }
662
663 /*----------------------------------------------------------------------------
664 | Returns the result of converting the canonical NaN `a' to the extended
665 | double-precision floating-point format.
666 *----------------------------------------------------------------------------*/
667
668 static floatx80 commonNaNToFloatx80( commonNaNT a STATUS_PARAM)
669 {
670 floatx80 z;
671
672 if ( STATUS(default_nan_mode) ) {
673 z.low = floatx80_default_nan_low;
674 z.high = floatx80_default_nan_high;
675 return z;
676 }
677
678 if (a.high)
679 z.low = a.high;
680 else
681 z.low = floatx80_default_nan_low;
682 z.high = ( ( (bits16) a.sign )<<15 ) | 0x7FFF;
683 return z;
684 }
685
686 /*----------------------------------------------------------------------------
687 | Takes two extended double-precision floating-point values `a' and `b', one
688 | of which is a NaN, and returns the appropriate NaN result. If either `a' or
689 | `b' is a signaling NaN, the invalid exception is raised.
690 *----------------------------------------------------------------------------*/
691
692 static floatx80 propagateFloatx80NaN( floatx80 a, floatx80 b STATUS_PARAM)
693 {
694 flag aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN;
695 flag aIsLargerSignificand;
696
697 aIsQuietNaN = floatx80_is_quiet_nan( a );
698 aIsSignalingNaN = floatx80_is_signaling_nan( a );
699 bIsQuietNaN = floatx80_is_quiet_nan( b );
700 bIsSignalingNaN = floatx80_is_signaling_nan( b );
701
702 if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_invalid STATUS_VAR);
703
704 if ( STATUS(default_nan_mode) ) {
705 a.low = floatx80_default_nan_low;
706 a.high = floatx80_default_nan_high;
707 return a;
708 }
709
710 if (a.low < b.low) {
711 aIsLargerSignificand = 0;
712 } else if (b.low < a.low) {
713 aIsLargerSignificand = 1;
714 } else {
715 aIsLargerSignificand = (a.high < b.high) ? 1 : 0;
716 }
717
718 if (pickNaN(aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN,
719 aIsLargerSignificand)) {
720 return floatx80_maybe_silence_nan(b);
721 } else {
722 return floatx80_maybe_silence_nan(a);
723 }
724 }
725
726 #endif
727
728 #ifdef FLOAT128
729
730 /*----------------------------------------------------------------------------
731 | The pattern for a default generated quadruple-precision NaN. The `high' and
732 | `low' values hold the most- and least-significant bits, respectively.
733 *----------------------------------------------------------------------------*/
734 #if SNAN_BIT_IS_ONE
735 #define float128_default_nan_high LIT64( 0x7FFF7FFFFFFFFFFF )
736 #define float128_default_nan_low LIT64( 0xFFFFFFFFFFFFFFFF )
737 #else
738 #define float128_default_nan_high LIT64( 0xFFFF800000000000 )
739 #define float128_default_nan_low LIT64( 0x0000000000000000 )
740 #endif
741
742 /*----------------------------------------------------------------------------
743 | Returns 1 if the quadruple-precision floating-point value `a' is a quiet
744 | NaN; otherwise returns 0.
745 *----------------------------------------------------------------------------*/
746
747 int float128_is_quiet_nan( float128 a )
748 {
749 #if SNAN_BIT_IS_ONE
750 return
751 ( ( ( a.high>>47 ) & 0xFFFF ) == 0xFFFE )
752 && ( a.low || ( a.high & LIT64( 0x00007FFFFFFFFFFF ) ) );
753 #else
754 return
755 ( LIT64( 0xFFFE000000000000 ) <= (bits64) ( a.high<<1 ) )
756 && ( a.low || ( a.high & LIT64( 0x0000FFFFFFFFFFFF ) ) );
757 #endif
758 }
759
760 /*----------------------------------------------------------------------------
761 | Returns 1 if the quadruple-precision floating-point value `a' is a
762 | signaling NaN; otherwise returns 0.
763 *----------------------------------------------------------------------------*/
764
765 int float128_is_signaling_nan( float128 a )
766 {
767 #if SNAN_BIT_IS_ONE
768 return
769 ( LIT64( 0xFFFE000000000000 ) <= (bits64) ( a.high<<1 ) )
770 && ( a.low || ( a.high & LIT64( 0x0000FFFFFFFFFFFF ) ) );
771 #else
772 return
773 ( ( ( a.high>>47 ) & 0xFFFF ) == 0xFFFE )
774 && ( a.low || ( a.high & LIT64( 0x00007FFFFFFFFFFF ) ) );
775 #endif
776 }
777
778 /*----------------------------------------------------------------------------
779 | Returns a quiet NaN if the quadruple-precision floating point value `a' is
780 | a signaling NaN; otherwise returns `a'.
781 *----------------------------------------------------------------------------*/
782
783 float128 float128_maybe_silence_nan( float128 a )
784 {
785 if (float128_is_signaling_nan(a)) {
786 #if SNAN_BIT_IS_ONE
787 # if defined(TARGET_MIPS) || defined(TARGET_SH4)
788 a.low = float128_default_nan_low;
789 a.high = float128_default_nan_high;
790 # else
791 # error Rules for silencing a signaling NaN are target-specific
792 # endif
793 #else
794 a.high |= LIT64( 0x0000800000000000 );
795 return a;
796 #endif
797 }
798 return a;
799 }
800
801 /*----------------------------------------------------------------------------
802 | Returns the result of converting the quadruple-precision floating-point NaN
803 | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
804 | exception is raised.
805 *----------------------------------------------------------------------------*/
806
807 static commonNaNT float128ToCommonNaN( float128 a STATUS_PARAM)
808 {
809 commonNaNT z;
810
811 if ( float128_is_signaling_nan( a ) ) float_raise( float_flag_invalid STATUS_VAR);
812 z.sign = a.high>>63;
813 shortShift128Left( a.high, a.low, 16, &z.high, &z.low );
814 return z;
815 }
816
817 /*----------------------------------------------------------------------------
818 | Returns the result of converting the canonical NaN `a' to the quadruple-
819 | precision floating-point format.
820 *----------------------------------------------------------------------------*/
821
822 static float128 commonNaNToFloat128( commonNaNT a STATUS_PARAM)
823 {
824 float128 z;
825
826 if ( STATUS(default_nan_mode) ) {
827 z.low = float128_default_nan_low;
828 z.high = float128_default_nan_high;
829 return z;
830 }
831
832 shift128Right( a.high, a.low, 16, &z.high, &z.low );
833 z.high |= ( ( (bits64) a.sign )<<63 ) | LIT64( 0x7FFF000000000000 );
834 return z;
835 }
836
837 /*----------------------------------------------------------------------------
838 | Takes two quadruple-precision floating-point values `a' and `b', one of
839 | which is a NaN, and returns the appropriate NaN result. If either `a' or
840 | `b' is a signaling NaN, the invalid exception is raised.
841 *----------------------------------------------------------------------------*/
842
843 static float128 propagateFloat128NaN( float128 a, float128 b STATUS_PARAM)
844 {
845 flag aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN;
846 flag aIsLargerSignificand;
847
848 aIsQuietNaN = float128_is_quiet_nan( a );
849 aIsSignalingNaN = float128_is_signaling_nan( a );
850 bIsQuietNaN = float128_is_quiet_nan( b );
851 bIsSignalingNaN = float128_is_signaling_nan( b );
852
853 if ( aIsSignalingNaN | bIsSignalingNaN ) float_raise( float_flag_invalid STATUS_VAR);
854
855 if ( STATUS(default_nan_mode) ) {
856 a.low = float128_default_nan_low;
857 a.high = float128_default_nan_high;
858 return a;
859 }
860
861 if (lt128(a.high<<1, a.low, b.high<<1, b.low)) {
862 aIsLargerSignificand = 0;
863 } else if (lt128(b.high<<1, b.low, a.high<<1, a.low)) {
864 aIsLargerSignificand = 1;
865 } else {
866 aIsLargerSignificand = (a.high < b.high) ? 1 : 0;
867 }
868
869 if (pickNaN(aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN,
870 aIsLargerSignificand)) {
871 return float128_maybe_silence_nan(b);
872 } else {
873 return float128_maybe_silence_nan(a);
874 }
875 }
876
877 #endif