Statistics
| Branch: | Revision:

root / fpu / softfloat-specialize.h @ 067404be

History | View | Annotate | Download (38.3 kB)

1 8d725fac Andreas Färber
/*
2 8d725fac Andreas Färber
 * QEMU float support
3 8d725fac Andreas Färber
 *
4 8d725fac Andreas Färber
 * Derived from SoftFloat.
5 8d725fac Andreas Färber
 */
6 158142c2 bellard
7 158142c2 bellard
/*============================================================================
8 158142c2 bellard

9 158142c2 bellard
This C source fragment is part of the SoftFloat IEC/IEEE Floating-point
10 158142c2 bellard
Arithmetic Package, Release 2b.
11 158142c2 bellard

12 158142c2 bellard
Written by John R. Hauser.  This work was made possible in part by the
13 158142c2 bellard
International Computer Science Institute, located at Suite 600, 1947 Center
14 158142c2 bellard
Street, Berkeley, California 94704.  Funding was partially provided by the
15 158142c2 bellard
National Science Foundation under grant MIP-9311980.  The original version
16 158142c2 bellard
of this code was written as part of a project to build a fixed-point vector
17 158142c2 bellard
processor in collaboration with the University of California at Berkeley,
18 158142c2 bellard
overseen by Profs. Nelson Morgan and John Wawrzynek.  More information
19 158142c2 bellard
is available through the Web page `http://www.cs.berkeley.edu/~jhauser/
20 158142c2 bellard
arithmetic/SoftFloat.html'.
21 158142c2 bellard

22 158142c2 bellard
THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE.  Although reasonable effort has
23 158142c2 bellard
been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT TIMES
24 158142c2 bellard
RESULT IN INCORRECT BEHAVIOR.  USE OF THIS SOFTWARE IS RESTRICTED TO PERSONS
25 158142c2 bellard
AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ALL LOSSES,
26 158142c2 bellard
COSTS, OR OTHER PROBLEMS THEY INCUR DUE TO THE SOFTWARE, AND WHO FURTHERMORE
27 158142c2 bellard
EFFECTIVELY INDEMNIFY JOHN HAUSER AND THE INTERNATIONAL COMPUTER SCIENCE
28 158142c2 bellard
INSTITUTE (possibly via similar legal warning) AGAINST ALL LOSSES, COSTS, OR
29 158142c2 bellard
OTHER PROBLEMS INCURRED BY THEIR CUSTOMERS AND CLIENTS DUE TO THE SOFTWARE.
30 158142c2 bellard

31 158142c2 bellard
Derivative works are acceptable, even for commercial purposes, so long as
32 158142c2 bellard
(1) the source code for the derivative work includes prominent notice that
33 158142c2 bellard
the work is derivative, and (2) the source code includes prominent notice with
34 158142c2 bellard
these four paragraphs for those parts of this code that are retained.
35 158142c2 bellard

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