]> git.proxmox.com Git - mirror_qemu.git/blame - fpu/softfloat-specialize.c.inc
MAINTAINERS: update virtio-gpu entry.
[mirror_qemu.git] / fpu / softfloat-specialize.c.inc
CommitLineData
8d725fac
AF
1/*
2 * QEMU float support
3 *
16017c48
PM
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.
8d725fac 16 */
158142c2 17
a7d1ac78
PM
18/*
19===============================================================================
158142c2 20This C source fragment is part of the SoftFloat IEC/IEEE Floating-point
a7d1ac78 21Arithmetic Package, Release 2a.
158142c2
FB
22
23Written by John R. Hauser. This work was made possible in part by the
24International Computer Science Institute, located at Suite 600, 1947 Center
25Street, Berkeley, California 94704. Funding was partially provided by the
26National Science Foundation under grant MIP-9311980. The original version
27of this code was written as part of a project to build a fixed-point vector
28processor in collaboration with the University of California at Berkeley,
29overseen by Profs. Nelson Morgan and John Wawrzynek. More information
a7d1ac78 30is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
158142c2
FB
31arithmetic/SoftFloat.html'.
32
a7d1ac78
PM
33THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
34has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
35TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
36PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
37AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
158142c2
FB
38
39Derivative works are acceptable, even for commercial purposes, so long as
a7d1ac78
PM
40(1) they include prominent notice that the work is derivative, and (2) they
41include prominent notice akin to these four paragraphs for those parts of
42this code that are retained.
158142c2 43
a7d1ac78
PM
44===============================================================================
45*/
158142c2 46
16017c48
PM
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
cc43c692
MF
82/*
83 * Define whether architecture deviates from IEEE in not supporting
213ff4e6
MF
84 * signaling NaNs (so all NaNs are treated as quiet).
85 */
cc43c692
MF
86static inline bool no_signaling_nans(float_status *status)
87{
03385dfd 88#if defined(TARGET_XTENSA)
cc43c692
MF
89 return status->no_signaling_nans;
90#else
91 return false;
213ff4e6 92#endif
cc43c692 93}
213ff4e6 94
03385dfd
RH
95/* Define how the architecture discriminates signaling NaNs.
96 * This done with the most significant bit of the fraction.
97 * In IEEE 754-1985 this was implementation defined, but in IEEE 754-2008
98 * the msb must be zero. MIPS is (so far) unique in supporting both the
99 * 2008 revision and backward compatibility with their original choice.
100 * Thus for MIPS we must make the choice at runtime.
101 */
c120391c 102static inline bool snan_bit_is_one(float_status *status)
03385dfd
RH
103{
104#if defined(TARGET_MIPS)
105 return status->snan_bit_is_one;
43692239 106#elif defined(TARGET_HPPA) || defined(TARGET_SH4)
03385dfd
RH
107 return 1;
108#else
109 return 0;
110#endif
111}
112
298b468e
RH
113/*----------------------------------------------------------------------------
114| For the deconstructed floating-point with fraction FRAC, return true
115| if the fraction represents a signalling NaN; otherwise false.
116*----------------------------------------------------------------------------*/
117
118static bool parts_is_snan_frac(uint64_t frac, float_status *status)
119{
cc43c692
MF
120 if (no_signaling_nans(status)) {
121 return false;
122 } else {
123 bool msb = extract64(frac, DECOMPOSED_BINARY_POINT - 1, 1);
124 return msb == snan_bit_is_one(status);
125 }
298b468e
RH
126}
127
f7e598e2
RH
128/*----------------------------------------------------------------------------
129| The pattern for a default generated deconstructed floating-point NaN.
130*----------------------------------------------------------------------------*/
131
0fc07cad 132static void parts64_default_nan(FloatParts64 *p, float_status *status)
f7e598e2
RH
133{
134 bool sign = 0;
135 uint64_t frac;
136
137#if defined(TARGET_SPARC) || defined(TARGET_M68K)
8fb3d902 138 /* !snan_bit_is_one, set all bits */
f7e598e2 139 frac = (1ULL << DECOMPOSED_BINARY_POINT) - 1;
8fb3d902
RH
140#elif defined(TARGET_I386) || defined(TARGET_X86_64) \
141 || defined(TARGET_MICROBLAZE)
142 /* !snan_bit_is_one, set sign and msb */
f7e598e2 143 frac = 1ULL << (DECOMPOSED_BINARY_POINT - 1);
8fb3d902 144 sign = 1;
f7e598e2 145#elif defined(TARGET_HPPA)
8fb3d902 146 /* snan_bit_is_one, set msb-1. */
f7e598e2 147 frac = 1ULL << (DECOMPOSED_BINARY_POINT - 2);
c0336c87
TS
148#elif defined(TARGET_HEXAGON)
149 sign = 1;
150 frac = ~0ULL;
f7e598e2 151#else
43692239
MA
152 /*
153 * This case is true for Alpha, ARM, MIPS, OpenRISC, PPC, RISC-V,
154 * S390, SH4, TriCore, and Xtensa. Our other supported targets,
155 * CRIS, Nios2, and Tile, do not have floating-point.
8fb3d902 156 */
03385dfd 157 if (snan_bit_is_one(status)) {
8fb3d902 158 /* set all bits other than msb */
f7e598e2
RH
159 frac = (1ULL << (DECOMPOSED_BINARY_POINT - 1)) - 1;
160 } else {
8fb3d902 161 /* set msb */
f7e598e2 162 frac = 1ULL << (DECOMPOSED_BINARY_POINT - 1);
f7e598e2
RH
163 }
164#endif
165
0fc07cad 166 *p = (FloatParts64) {
f7e598e2
RH
167 .cls = float_class_qnan,
168 .sign = sign,
169 .exp = INT_MAX,
170 .frac = frac
171 };
172}
173
e9034ea8
RH
174static void parts128_default_nan(FloatParts128 *p, float_status *status)
175{
176 /*
177 * Extrapolate from the choices made by parts64_default_nan to fill
178 * in the quad-floating format. If the low bit is set, assume we
179 * want to set all non-snan bits.
180 */
181 FloatParts64 p64;
182 parts64_default_nan(&p64, status);
183
184 *p = (FloatParts128) {
185 .cls = float_class_qnan,
186 .sign = p64.sign,
187 .exp = INT_MAX,
188 .frac_hi = p64.frac,
189 .frac_lo = -(p64.frac & 1)
190 };
191}
192
0bcfbcbe
RH
193/*----------------------------------------------------------------------------
194| Returns a quiet NaN from a signalling NaN for the deconstructed
195| floating-point parts.
196*----------------------------------------------------------------------------*/
197
92ff426d 198static uint64_t parts_silence_nan_frac(uint64_t frac, float_status *status)
0bcfbcbe 199{
cc43c692 200 g_assert(!no_signaling_nans(status));
a777d603
RH
201 g_assert(!status->default_nan_mode);
202
203 /* The only snan_bit_is_one target without default_nan_mode is HPPA. */
03385dfd 204 if (snan_bit_is_one(status)) {
92ff426d
RH
205 frac &= ~(1ULL << (DECOMPOSED_BINARY_POINT - 1));
206 frac |= 1ULL << (DECOMPOSED_BINARY_POINT - 2);
0bcfbcbe 207 } else {
92ff426d 208 frac |= 1ULL << (DECOMPOSED_BINARY_POINT - 1);
0bcfbcbe 209 }
92ff426d
RH
210 return frac;
211}
212
213static void parts64_silence_nan(FloatParts64 *p, float_status *status)
214{
215 p->frac = parts_silence_nan_frac(p->frac, status);
216 p->cls = float_class_qnan;
0bcfbcbe
RH
217}
218
0018b1f4
RH
219static void parts128_silence_nan(FloatParts128 *p, float_status *status)
220{
221 p->frac_hi = parts_silence_nan_frac(p->frac_hi, status);
222 p->cls = float_class_qnan;
223}
224
789ec7ce
PB
225/*----------------------------------------------------------------------------
226| The pattern for a default generated extended double-precision NaN.
227*----------------------------------------------------------------------------*/
af39bc8c
AM
228floatx80 floatx80_default_nan(float_status *status)
229{
230 floatx80 r;
0218a16e
RH
231
232 /* None of the targets that have snan_bit_is_one use floatx80. */
233 assert(!snan_bit_is_one(status));
e5b0cbe8 234#if defined(TARGET_M68K)
f7e81a94 235 r.low = UINT64_C(0xFFFFFFFFFFFFFFFF);
e5b0cbe8
LV
236 r.high = 0x7FFF;
237#else
0218a16e 238 /* X86 */
f7e81a94 239 r.low = UINT64_C(0xC000000000000000);
0218a16e 240 r.high = 0xFFFF;
e5b0cbe8 241#endif
af39bc8c
AM
242 return r;
243}
789ec7ce 244
0f605c88
LV
245/*----------------------------------------------------------------------------
246| The pattern for a default generated extended double-precision inf.
247*----------------------------------------------------------------------------*/
248
249#define floatx80_infinity_high 0x7FFF
250#if defined(TARGET_M68K)
f7e81a94 251#define floatx80_infinity_low UINT64_C(0x0000000000000000)
0f605c88 252#else
f7e81a94 253#define floatx80_infinity_low UINT64_C(0x8000000000000000)
0f605c88
LV
254#endif
255
256const floatx80 floatx80_infinity
257 = make_floatx80_init(floatx80_infinity_high, floatx80_infinity_low);
258
bb4d4bb3
PM
259/*----------------------------------------------------------------------------
260| Returns 1 if the half-precision floating-point value `a' is a quiet
261| NaN; otherwise returns 0.
262*----------------------------------------------------------------------------*/
263
150c7a91 264bool float16_is_quiet_nan(float16 a_, float_status *status)
bb4d4bb3 265{
cc43c692
MF
266 if (no_signaling_nans(status)) {
267 return float16_is_any_nan(a_);
af39bc8c 268 } else {
cc43c692
MF
269 uint16_t a = float16_val(a_);
270 if (snan_bit_is_one(status)) {
271 return (((a >> 9) & 0x3F) == 0x3E) && (a & 0x1FF);
272 } else {
273
274 return ((a >> 9) & 0x3F) == 0x3F;
275 }
af39bc8c 276 }
bb4d4bb3
PM
277}
278
5ebf5f4b
LZ
279/*----------------------------------------------------------------------------
280| Returns 1 if the bfloat16 value `a' is a quiet
281| NaN; otherwise returns 0.
282*----------------------------------------------------------------------------*/
283
284bool bfloat16_is_quiet_nan(bfloat16 a_, float_status *status)
285{
286 if (no_signaling_nans(status)) {
287 return bfloat16_is_any_nan(a_);
288 } else {
289 uint16_t a = a_;
290 if (snan_bit_is_one(status)) {
291 return (((a >> 6) & 0x1FF) == 0x1FE) && (a & 0x3F);
292 } else {
293 return ((a >> 6) & 0x1FF) == 0x1FF;
294 }
295 }
296}
297
bb4d4bb3
PM
298/*----------------------------------------------------------------------------
299| Returns 1 if the half-precision floating-point value `a' is a signaling
300| NaN; otherwise returns 0.
301*----------------------------------------------------------------------------*/
302
150c7a91 303bool float16_is_signaling_nan(float16 a_, float_status *status)
bb4d4bb3 304{
cc43c692
MF
305 if (no_signaling_nans(status)) {
306 return 0;
af39bc8c 307 } else {
cc43c692
MF
308 uint16_t a = float16_val(a_);
309 if (snan_bit_is_one(status)) {
310 return ((a >> 9) & 0x3F) == 0x3F;
311 } else {
312 return (((a >> 9) & 0x3F) == 0x3E) && (a & 0x1FF);
313 }
af39bc8c 314 }
bca52234 315}
bb4d4bb3 316
5ebf5f4b
LZ
317/*----------------------------------------------------------------------------
318| Returns 1 if the bfloat16 value `a' is a signaling
319| NaN; otherwise returns 0.
320*----------------------------------------------------------------------------*/
321
322bool bfloat16_is_signaling_nan(bfloat16 a_, float_status *status)
323{
324 if (no_signaling_nans(status)) {
325 return 0;
326 } else {
327 uint16_t a = a_;
328 if (snan_bit_is_one(status)) {
329 return ((a >> 6) & 0x1FF) == 0x1FF;
330 } else {
331 return (((a >> 6) & 0x1FF) == 0x1FE) && (a & 0x3F);
332 }
333 }
334}
335
158142c2 336/*----------------------------------------------------------------------------
5a6932d5
TS
337| Returns 1 if the single-precision floating-point value `a' is a quiet
338| NaN; otherwise returns 0.
158142c2
FB
339*----------------------------------------------------------------------------*/
340
150c7a91 341bool float32_is_quiet_nan(float32 a_, float_status *status)
158142c2 342{
cc43c692
MF
343 if (no_signaling_nans(status)) {
344 return float32_is_any_nan(a_);
af39bc8c 345 } else {
cc43c692
MF
346 uint32_t a = float32_val(a_);
347 if (snan_bit_is_one(status)) {
348 return (((a >> 22) & 0x1FF) == 0x1FE) && (a & 0x003FFFFF);
349 } else {
350 return ((uint32_t)(a << 1) >= 0xFF800000);
351 }
af39bc8c 352 }
158142c2
FB
353}
354
355/*----------------------------------------------------------------------------
356| Returns 1 if the single-precision floating-point value `a' is a signaling
357| NaN; otherwise returns 0.
358*----------------------------------------------------------------------------*/
359
150c7a91 360bool float32_is_signaling_nan(float32 a_, float_status *status)
158142c2 361{
cc43c692
MF
362 if (no_signaling_nans(status)) {
363 return 0;
af39bc8c 364 } else {
cc43c692
MF
365 uint32_t a = float32_val(a_);
366 if (snan_bit_is_one(status)) {
367 return ((uint32_t)(a << 1) >= 0xFF800000);
368 } else {
369 return (((a >> 22) & 0x1FF) == 0x1FE) && (a & 0x003FFFFF);
370 }
af39bc8c 371 }
bca52234 372}
158142c2 373
354f211b
PM
374/*----------------------------------------------------------------------------
375| Select which NaN to propagate for a two-input operation.
376| IEEE754 doesn't specify all the details of this, so the
377| algorithm is target-specific.
378| The routine is passed various bits of information about the
379| two NaNs and should return 0 to select NaN a and 1 for NaN b.
380| Note that signalling NaNs are always squashed to quiet NaNs
4885312f 381| by the caller, by calling floatXX_silence_nan() before
1f398e08 382| returning them.
354f211b
PM
383|
384| aIsLargerSignificand is only valid if both a and b are NaNs
385| of some kind, and is true if a has the larger significand,
386| or if both a and b have the same significand but a is
387| positive but b is negative. It is only needed for the x87
388| tie-break rule.
389*----------------------------------------------------------------------------*/
390
4f251cfd 391static int pickNaN(FloatClass a_cls, FloatClass b_cls,
913602e3 392 bool aIsLargerSignificand, float_status *status)
011da610 393{
4f251cfd 394#if defined(TARGET_ARM) || defined(TARGET_MIPS) || defined(TARGET_HPPA)
13894527
AB
395 /* ARM mandated NaN propagation rules (see FPProcessNaNs()), take
396 * the first of:
011da610
PM
397 * 1. A if it is signaling
398 * 2. B if it is signaling
399 * 3. A (quiet)
400 * 4. B (quiet)
401 * A signaling NaN is always quietened before returning it.
402 */
084d19ba
AJ
403 /* According to MIPS specifications, if one of the two operands is
404 * a sNaN, a new qNaN has to be generated. This is done in
4885312f 405 * floatXX_silence_nan(). For qNaN inputs the specifications
084d19ba
AJ
406 * says: "When possible, this QNaN result is one of the operand QNaN
407 * values." In practice it seems that most implementations choose
408 * the first operand if both operands are qNaN. In short this gives
409 * the following rules:
410 * 1. A if it is signaling
411 * 2. B if it is signaling
412 * 3. A (quiet)
413 * 4. B (quiet)
414 * A signaling NaN is always silenced before returning it.
415 */
4f251cfd 416 if (is_snan(a_cls)) {
084d19ba 417 return 0;
4f251cfd 418 } else if (is_snan(b_cls)) {
084d19ba 419 return 1;
4f251cfd 420 } else if (is_qnan(a_cls)) {
084d19ba
AJ
421 return 0;
422 } else {
423 return 1;
424 }
913602e3 425#elif defined(TARGET_PPC) || defined(TARGET_M68K)
e024e881
AJ
426 /* PowerPC propagation rules:
427 * 1. A if it sNaN or qNaN
428 * 2. B if it sNaN or qNaN
429 * A signaling NaN is always silenced before returning it.
430 */
e5b0cbe8
LV
431 /* M68000 FAMILY PROGRAMMER'S REFERENCE MANUAL
432 * 3.4 FLOATING-POINT INSTRUCTION DETAILS
433 * If either operand, but not both operands, of an operation is a
434 * nonsignaling NaN, then that NaN is returned as the result. If both
435 * operands are nonsignaling NaNs, then the destination operand
436 * nonsignaling NaN is returned as the result.
437 * If either operand to an operation is a signaling NaN (SNaN), then the
438 * SNaN bit is set in the FPSR EXC byte. If the SNaN exception enable bit
439 * is set in the FPCR ENABLE byte, then the exception is taken and the
440 * destination is not modified. If the SNaN exception enable bit is not
441 * set, setting the SNaN bit in the operand to a one converts the SNaN to
442 * a nonsignaling NaN. The operation then continues as described in the
443 * preceding paragraph for nonsignaling NaNs.
444 */
4f251cfd
RH
445 if (is_nan(a_cls)) {
446 return 0;
e5b0cbe8 447 } else {
4f251cfd 448 return 1;
e5b0cbe8 449 }
913602e3
MF
450#elif defined(TARGET_XTENSA)
451 /*
452 * Xtensa has two NaN propagation modes.
453 * Which one is active is controlled by float_status::use_first_nan.
454 */
455 if (status->use_first_nan) {
456 if (is_nan(a_cls)) {
457 return 0;
458 } else {
459 return 1;
460 }
461 } else {
462 if (is_nan(b_cls)) {
463 return 1;
464 } else {
465 return 0;
466 }
467 }
011da610 468#else
354f211b
PM
469 /* This implements x87 NaN propagation rules:
470 * SNaN + QNaN => return the QNaN
471 * two SNaNs => return the one with the larger significand, silenced
472 * two QNaNs => return the one with the larger significand
473 * SNaN and a non-NaN => return the SNaN, silenced
474 * QNaN and a non-NaN => return the QNaN
475 *
476 * If we get down to comparing significands and they are the same,
477 * return the NaN with the positive sign bit (if any).
478 */
4f251cfd
RH
479 if (is_snan(a_cls)) {
480 if (is_snan(b_cls)) {
354f211b
PM
481 return aIsLargerSignificand ? 0 : 1;
482 }
4f251cfd
RH
483 return is_qnan(b_cls) ? 1 : 0;
484 } else if (is_qnan(a_cls)) {
485 if (is_snan(b_cls) || !is_qnan(b_cls)) {
354f211b 486 return 0;
a59eaea6 487 } else {
354f211b
PM
488 return aIsLargerSignificand ? 0 : 1;
489 }
490 } else {
491 return 1;
492 }
011da610 493#endif
4f251cfd 494}
354f211b 495
369be8f6
PM
496/*----------------------------------------------------------------------------
497| Select which NaN to propagate for a three-input operation.
498| For the moment we assume that no CPU needs the 'larger significand'
499| information.
500| Return values : 0 : a; 1 : b; 2 : c; 3 : default-NaN
501*----------------------------------------------------------------------------*/
3bd2dec1
RH
502static int pickNaNMulAdd(FloatClass a_cls, FloatClass b_cls, FloatClass c_cls,
503 bool infzero, float_status *status)
369be8f6 504{
3bd2dec1 505#if defined(TARGET_ARM)
369be8f6
PM
506 /* For ARM, the (inf,zero,qnan) case sets InvalidOp and returns
507 * the default NaN
508 */
3bd2dec1 509 if (infzero && is_qnan(c_cls)) {
ff32e16e 510 float_raise(float_flag_invalid, status);
369be8f6
PM
511 return 3;
512 }
513
514 /* This looks different from the ARM ARM pseudocode, because the ARM ARM
515 * puts the operands to a fused mac operation (a*b)+c in the order c,a,b.
516 */
3bd2dec1 517 if (is_snan(c_cls)) {
369be8f6 518 return 2;
3bd2dec1 519 } else if (is_snan(a_cls)) {
369be8f6 520 return 0;
3bd2dec1 521 } else if (is_snan(b_cls)) {
369be8f6 522 return 1;
3bd2dec1 523 } else if (is_qnan(c_cls)) {
369be8f6 524 return 2;
3bd2dec1 525 } else if (is_qnan(a_cls)) {
369be8f6
PM
526 return 0;
527 } else {
528 return 1;
529 }
bbc1dede 530#elif defined(TARGET_MIPS)
03385dfd 531 if (snan_bit_is_one(status)) {
7ca96e1a
MM
532 /*
533 * For MIPS systems that conform to IEEE754-1985, the (inf,zero,nan)
534 * case sets InvalidOp and returns the default NaN
535 */
536 if (infzero) {
537 float_raise(float_flag_invalid, status);
538 return 3;
539 }
c27644f0 540 /* Prefer sNaN over qNaN, in the a, b, c order. */
3bd2dec1 541 if (is_snan(a_cls)) {
c27644f0 542 return 0;
3bd2dec1 543 } else if (is_snan(b_cls)) {
c27644f0 544 return 1;
3bd2dec1 545 } else if (is_snan(c_cls)) {
c27644f0 546 return 2;
3bd2dec1 547 } else if (is_qnan(a_cls)) {
c27644f0 548 return 0;
3bd2dec1 549 } else if (is_qnan(b_cls)) {
c27644f0
AM
550 return 1;
551 } else {
552 return 2;
553 }
bbc1dede 554 } else {
7ca96e1a
MM
555 /*
556 * For MIPS systems that conform to IEEE754-2008, the (inf,zero,nan)
557 * case sets InvalidOp and returns the input value 'c'
558 */
559 if (infzero) {
560 float_raise(float_flag_invalid, status);
561 return 2;
562 }
c27644f0 563 /* Prefer sNaN over qNaN, in the c, a, b order. */
3bd2dec1 564 if (is_snan(c_cls)) {
c27644f0 565 return 2;
3bd2dec1 566 } else if (is_snan(a_cls)) {
c27644f0 567 return 0;
3bd2dec1 568 } else if (is_snan(b_cls)) {
c27644f0 569 return 1;
3bd2dec1 570 } else if (is_qnan(c_cls)) {
c27644f0 571 return 2;
3bd2dec1 572 } else if (is_qnan(a_cls)) {
c27644f0
AM
573 return 0;
574 } else {
575 return 1;
576 }
bbc1dede 577 }
369be8f6 578#elif defined(TARGET_PPC)
369be8f6
PM
579 /* For PPC, the (inf,zero,qnan) case sets InvalidOp, but we prefer
580 * to return an input NaN if we have one (ie c) rather than generating
581 * a default NaN
582 */
583 if (infzero) {
ff32e16e 584 float_raise(float_flag_invalid, status);
369be8f6
PM
585 return 2;
586 }
587
588 /* If fRA is a NaN return it; otherwise if fRB is a NaN return it;
589 * otherwise return fRC. Note that muladd on PPC is (fRA * fRC) + frB
590 */
3bd2dec1 591 if (is_nan(a_cls)) {
369be8f6 592 return 0;
3bd2dec1 593 } else if (is_nan(c_cls)) {
369be8f6
PM
594 return 2;
595 } else {
596 return 1;
597 }
3a7f7757
FC
598#elif defined(TARGET_RISCV)
599 /* For RISC-V, InvalidOp is set when multiplicands are Inf and zero */
600 if (infzero) {
601 float_raise(float_flag_invalid, status);
602 }
603 return 3; /* default NaN */
fbcc38e4
MF
604#elif defined(TARGET_XTENSA)
605 /*
606 * For Xtensa, the (inf,zero,nan) case sets InvalidOp and returns
607 * an input NaN if we have one (ie c).
608 */
609 if (infzero) {
610 float_raise(float_flag_invalid, status);
611 return 2;
612 }
613 if (status->use_first_nan) {
614 if (is_nan(a_cls)) {
615 return 0;
616 } else if (is_nan(b_cls)) {
617 return 1;
618 } else {
619 return 2;
620 }
621 } else {
622 if (is_nan(c_cls)) {
623 return 2;
624 } else if (is_nan(b_cls)) {
625 return 1;
626 } else {
627 return 0;
628 }
629 }
369be8f6 630#else
3bd2dec1
RH
631 /* A default implementation: prefer a to b to c.
632 * This is unlikely to actually match any real implementation.
633 */
634 if (is_nan(a_cls)) {
369be8f6 635 return 0;
3bd2dec1 636 } else if (is_nan(b_cls)) {
369be8f6
PM
637 return 1;
638 } else {
639 return 2;
640 }
369be8f6 641#endif
3bd2dec1 642}
369be8f6 643
158142c2 644/*----------------------------------------------------------------------------
5a6932d5
TS
645| Returns 1 if the double-precision floating-point value `a' is a quiet
646| NaN; otherwise returns 0.
158142c2
FB
647*----------------------------------------------------------------------------*/
648
150c7a91 649bool float64_is_quiet_nan(float64 a_, float_status *status)
158142c2 650{
cc43c692
MF
651 if (no_signaling_nans(status)) {
652 return float64_is_any_nan(a_);
af39bc8c 653 } else {
cc43c692
MF
654 uint64_t a = float64_val(a_);
655 if (snan_bit_is_one(status)) {
656 return (((a >> 51) & 0xFFF) == 0xFFE)
657 && (a & 0x0007FFFFFFFFFFFFULL);
658 } else {
659 return ((a << 1) >= 0xFFF0000000000000ULL);
660 }
af39bc8c 661 }
158142c2
FB
662}
663
664/*----------------------------------------------------------------------------
665| Returns 1 if the double-precision floating-point value `a' is a signaling
666| NaN; otherwise returns 0.
667*----------------------------------------------------------------------------*/
668
150c7a91 669bool float64_is_signaling_nan(float64 a_, float_status *status)
158142c2 670{
cc43c692
MF
671 if (no_signaling_nans(status)) {
672 return 0;
af39bc8c 673 } else {
cc43c692
MF
674 uint64_t a = float64_val(a_);
675 if (snan_bit_is_one(status)) {
676 return ((a << 1) >= 0xFFF0000000000000ULL);
677 } else {
678 return (((a >> 51) & 0xFFF) == 0xFFE)
679 && (a & UINT64_C(0x0007FFFFFFFFFFFF));
680 }
af39bc8c 681 }
bca52234 682}
158142c2 683
158142c2
FB
684/*----------------------------------------------------------------------------
685| Returns 1 if the extended double-precision floating-point value `a' is a
de4af5f7
AJ
686| quiet NaN; otherwise returns 0. This slightly differs from the same
687| function for other types as floatx80 has an explicit bit.
158142c2
FB
688*----------------------------------------------------------------------------*/
689
af39bc8c 690int floatx80_is_quiet_nan(floatx80 a, float_status *status)
158142c2 691{
cc43c692
MF
692 if (no_signaling_nans(status)) {
693 return floatx80_is_any_nan(a);
af39bc8c 694 } else {
cc43c692
MF
695 if (snan_bit_is_one(status)) {
696 uint64_t aLow;
697
698 aLow = a.low & ~0x4000000000000000ULL;
699 return ((a.high & 0x7FFF) == 0x7FFF)
700 && (aLow << 1)
701 && (a.low == aLow);
702 } else {
703 return ((a.high & 0x7FFF) == 0x7FFF)
704 && (UINT64_C(0x8000000000000000) <= ((uint64_t)(a.low << 1)));
705 }
af39bc8c 706 }
158142c2
FB
707}
708
709/*----------------------------------------------------------------------------
710| Returns 1 if the extended double-precision floating-point value `a' is a
de4af5f7
AJ
711| signaling NaN; otherwise returns 0. This slightly differs from the same
712| function for other types as floatx80 has an explicit bit.
158142c2
FB
713*----------------------------------------------------------------------------*/
714
af39bc8c 715int floatx80_is_signaling_nan(floatx80 a, float_status *status)
158142c2 716{
cc43c692
MF
717 if (no_signaling_nans(status)) {
718 return 0;
af39bc8c 719 } else {
cc43c692
MF
720 if (snan_bit_is_one(status)) {
721 return ((a.high & 0x7FFF) == 0x7FFF)
722 && ((a.low << 1) >= 0x8000000000000000ULL);
723 } else {
724 uint64_t aLow;
158142c2 725
cc43c692
MF
726 aLow = a.low & ~UINT64_C(0x4000000000000000);
727 return ((a.high & 0x7FFF) == 0x7FFF)
728 && (uint64_t)(aLow << 1)
729 && (a.low == aLow);
730 }
af39bc8c 731 }
bca52234 732}
158142c2 733
d619bb98
RH
734/*----------------------------------------------------------------------------
735| Returns a quiet NaN from a signalling NaN for the extended double-precision
736| floating point value `a'.
737*----------------------------------------------------------------------------*/
738
739floatx80 floatx80_silence_nan(floatx80 a, float_status *status)
740{
377ed926
RH
741 /* None of the targets that have snan_bit_is_one use floatx80. */
742 assert(!snan_bit_is_one(status));
f7e81a94 743 a.low |= UINT64_C(0xC000000000000000);
377ed926 744 return a;
d619bb98
RH
745}
746
158142c2
FB
747/*----------------------------------------------------------------------------
748| Takes two extended double-precision floating-point values `a' and `b', one
749| of which is a NaN, and returns the appropriate NaN result. If either `a' or
750| `b' is a signaling NaN, the invalid exception is raised.
751*----------------------------------------------------------------------------*/
752
88857aca 753floatx80 propagateFloatx80NaN(floatx80 a, floatx80 b, float_status *status)
158142c2 754{
c120391c 755 bool aIsLargerSignificand;
4f251cfd
RH
756 FloatClass a_cls, b_cls;
757
758 /* This is not complete, but is good enough for pickNaN. */
759 a_cls = (!floatx80_is_any_nan(a)
760 ? float_class_normal
761 : floatx80_is_signaling_nan(a, status)
762 ? float_class_snan
763 : float_class_qnan);
764 b_cls = (!floatx80_is_any_nan(b)
765 ? float_class_normal
766 : floatx80_is_signaling_nan(b, status)
767 ? float_class_snan
768 : float_class_qnan);
769
770 if (is_snan(a_cls) || is_snan(b_cls)) {
ff32e16e
PM
771 float_raise(float_flag_invalid, status);
772 }
354f211b 773
a2f2d288 774 if (status->default_nan_mode) {
af39bc8c 775 return floatx80_default_nan(status);
10201602
AJ
776 }
777
354f211b
PM
778 if (a.low < b.low) {
779 aIsLargerSignificand = 0;
780 } else if (b.low < a.low) {
781 aIsLargerSignificand = 1;
782 } else {
783 aIsLargerSignificand = (a.high < b.high) ? 1 : 0;
158142c2 784 }
354f211b 785
913602e3 786 if (pickNaN(a_cls, b_cls, aIsLargerSignificand, status)) {
4f251cfd 787 if (is_snan(b_cls)) {
4885312f
RH
788 return floatx80_silence_nan(b, status);
789 }
790 return b;
354f211b 791 } else {
4f251cfd 792 if (is_snan(a_cls)) {
4885312f
RH
793 return floatx80_silence_nan(a, status);
794 }
795 return a;
158142c2 796 }
158142c2
FB
797}
798
158142c2 799/*----------------------------------------------------------------------------
5a6932d5
TS
800| Returns 1 if the quadruple-precision floating-point value `a' is a quiet
801| NaN; otherwise returns 0.
158142c2
FB
802*----------------------------------------------------------------------------*/
803
150c7a91 804bool float128_is_quiet_nan(float128 a, float_status *status)
158142c2 805{
cc43c692
MF
806 if (no_signaling_nans(status)) {
807 return float128_is_any_nan(a);
af39bc8c 808 } else {
cc43c692
MF
809 if (snan_bit_is_one(status)) {
810 return (((a.high >> 47) & 0xFFFF) == 0xFFFE)
811 && (a.low || (a.high & 0x00007FFFFFFFFFFFULL));
812 } else {
813 return ((a.high << 1) >= 0xFFFF000000000000ULL)
814 && (a.low || (a.high & 0x0000FFFFFFFFFFFFULL));
815 }
af39bc8c 816 }
158142c2
FB
817}
818
819/*----------------------------------------------------------------------------
820| Returns 1 if the quadruple-precision floating-point value `a' is a
821| signaling NaN; otherwise returns 0.
822*----------------------------------------------------------------------------*/
823
150c7a91 824bool float128_is_signaling_nan(float128 a, float_status *status)
158142c2 825{
cc43c692
MF
826 if (no_signaling_nans(status)) {
827 return 0;
af39bc8c 828 } else {
cc43c692
MF
829 if (snan_bit_is_one(status)) {
830 return ((a.high << 1) >= 0xFFFF000000000000ULL)
831 && (a.low || (a.high & 0x0000FFFFFFFFFFFFULL));
832 } else {
833 return (((a.high >> 47) & 0xFFFF) == 0xFFFE)
834 && (a.low || (a.high & UINT64_C(0x00007FFFFFFFFFFF)));
835 }
af39bc8c 836 }
bca52234 837}