/src/qemu/fpu/softfloat-specialize.c.inc
Line | Count | Source |
1 | | /* |
2 | | * QEMU float support |
3 | | * |
4 | | * The code in this source file is derived from release 2a of the SoftFloat |
5 | | * IEC/IEEE Floating-point Arithmetic Package. Those parts of the code (and |
6 | | * some later contributions) are provided under that license, as detailed below. |
7 | | * It has subsequently been modified by contributors to the QEMU Project, |
8 | | * so some portions are provided under: |
9 | | * the SoftFloat-2a license |
10 | | * the BSD license |
11 | | * GPL-v2-or-later |
12 | | * |
13 | | * Any future contributions to this file after December 1st 2014 will be |
14 | | * taken to be licensed under the Softfloat-2a license unless specifically |
15 | | * indicated otherwise. |
16 | | */ |
17 | | |
18 | | /* |
19 | | =============================================================================== |
20 | | This C source fragment is part of the SoftFloat IEC/IEEE Floating-point |
21 | | Arithmetic Package, Release 2a. |
22 | | |
23 | | Written by John R. Hauser. This work was made possible in part by the |
24 | | International Computer Science Institute, located at Suite 600, 1947 Center |
25 | | Street, Berkeley, California 94704. Funding was partially provided by the |
26 | | National Science Foundation under grant MIP-9311980. The original version |
27 | | of this code was written as part of a project to build a fixed-point vector |
28 | | processor in collaboration with the University of California at Berkeley, |
29 | | overseen by Profs. Nelson Morgan and John Wawrzynek. More information |
30 | | is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/ |
31 | | arithmetic/SoftFloat.html'. |
32 | | |
33 | | THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort |
34 | | has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT |
35 | | TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO |
36 | | PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY |
37 | | AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE. |
38 | | |
39 | | Derivative works are acceptable, even for commercial purposes, so long as |
40 | | (1) they include prominent notice that the work is derivative, and (2) they |
41 | | include prominent notice akin to these four paragraphs for those parts of |
42 | | this code that are retained. |
43 | | |
44 | | =============================================================================== |
45 | | */ |
46 | | |
47 | | /* BSD licensing: |
48 | | * Copyright (c) 2006, Fabrice Bellard |
49 | | * All rights reserved. |
50 | | * |
51 | | * Redistribution and use in source and binary forms, with or without |
52 | | * modification, are permitted provided that the following conditions are met: |
53 | | * |
54 | | * 1. Redistributions of source code must retain the above copyright notice, |
55 | | * this list of conditions and the following disclaimer. |
56 | | * |
57 | | * 2. Redistributions in binary form must reproduce the above copyright notice, |
58 | | * this list of conditions and the following disclaimer in the documentation |
59 | | * and/or other materials provided with the distribution. |
60 | | * |
61 | | * 3. Neither the name of the copyright holder nor the names of its contributors |
62 | | * may be used to endorse or promote products derived from this software without |
63 | | * specific prior written permission. |
64 | | * |
65 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
66 | | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
67 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
68 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
69 | | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
70 | | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
71 | | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
72 | | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
73 | | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
74 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
75 | | * THE POSSIBILITY OF SUCH DAMAGE. |
76 | | */ |
77 | | |
78 | | /* Portions of this work are licensed under the terms of the GNU GPL, |
79 | | * version 2 or later. See the COPYING file in the top-level directory. |
80 | | */ |
81 | | |
82 | | static inline bool get_tininess_before_rounding(const float_status *status) |
83 | 0 | { |
84 | 0 | return status->tininess_before_rounding; |
85 | 0 | } |
86 | | |
87 | | static inline bool get_ftz_before_rounding(const float_status *status) |
88 | 0 | { |
89 | 0 | return status->ftz_before_rounding; |
90 | 0 | } |
91 | | |
92 | | static inline uint8_t get_float_default_nan_pattern(const float_status *status) |
93 | 0 | { |
94 | 0 | return status->default_nan_pattern; |
95 | 0 | } |
96 | | |
97 | | static inline bool get_float_rebias_overflow(const float_status *status) |
98 | 0 | { |
99 | 0 | return status->rebias_overflow; |
100 | 0 | } |
101 | | |
102 | | static inline bool get_float_rebias_underflow(const float_status *status) |
103 | 0 | { |
104 | 0 | return status->rebias_underflow; |
105 | 0 | } |
106 | | |
107 | | static inline bool get_float_e4m3_nan_is_snan(const float_status *status) |
108 | 0 | { |
109 | 0 | return status->e4m3_nan_is_snan; |
110 | 0 | } |
111 | | |
112 | | /*---------------------------------------------------------------------------- |
113 | | | For the deconstructed floating-point with fraction FRAC, return true |
114 | | | if the fraction represents a signalling NaN; otherwise false. |
115 | | *----------------------------------------------------------------------------*/ |
116 | | |
117 | | static bool frac_msb_is_snan(bool msb, float_status *status) |
118 | 0 | { |
119 | 0 | switch (get_snan_rule(status)) { |
120 | 0 | case float_snan_never: |
121 | 0 | return false; |
122 | 0 | case float_snan_bit_is_one: |
123 | 0 | return msb; |
124 | 0 | case float_snan_bit_is_zero: |
125 | 0 | return !msb; |
126 | 0 | } |
127 | 0 | g_assert_not_reached(); |
128 | 0 | } |
129 | | |
130 | | static bool parts_is_snan_frac(uint64_t frac, float_status *status) |
131 | 0 | { |
132 | 0 | bool msb = extract64(frac, DECOMPOSED_BINARY_POINT - 1, 1); |
133 | 0 | return frac_msb_is_snan(msb, status); |
134 | 0 | } |
135 | | |
136 | | /*---------------------------------------------------------------------------- |
137 | | | The pattern for a default generated deconstructed floating-point NaN. |
138 | | *----------------------------------------------------------------------------*/ |
139 | | |
140 | | FloatParts64 parts64_default_nan(float_status *status) |
141 | 0 | { |
142 | 0 | bool sign = 0; |
143 | 0 | uint64_t frac; |
144 | 0 | uint8_t dnan_pattern = get_float_default_nan_pattern(status); |
145 | |
|
146 | 0 | assert(dnan_pattern != 0); |
147 | |
|
148 | 0 | sign = dnan_pattern >> 7; |
149 | | /* |
150 | | * Place default_nan_pattern [6:0] into bits [62:56], |
151 | | * and replecate bit [0] down into [55:0] |
152 | | */ |
153 | 0 | frac = deposit64(0, DECOMPOSED_BINARY_POINT - 7, 7, dnan_pattern); |
154 | 0 | frac = deposit64(frac, 0, DECOMPOSED_BINARY_POINT - 7, -(dnan_pattern & 1)); |
155 | |
|
156 | 0 | return (FloatParts64) { |
157 | 0 | .cls = float_class_qnan, |
158 | 0 | .sign = sign, |
159 | 0 | .exp = INT_MAX, |
160 | 0 | .frac = frac |
161 | 0 | }; |
162 | 0 | } |
163 | | |
164 | | FloatParts128 parts128_default_nan(float_status *status) |
165 | 0 | { |
166 | | /* |
167 | | * Extrapolate from the choices made by parts64_default_nan to fill |
168 | | * in the quad-floating format. If the low bit is set, assume we |
169 | | * want to set all non-snan bits. |
170 | | */ |
171 | 0 | FloatParts64 p64 = parts64_default_nan(status); |
172 | |
|
173 | 0 | return (FloatParts128) { |
174 | 0 | .cls = float_class_qnan, |
175 | 0 | .sign = p64.sign, |
176 | 0 | .exp = INT_MAX, |
177 | 0 | .frac_hi = p64.frac, |
178 | 0 | .frac_lo = -(p64.frac & 1) |
179 | 0 | }; |
180 | 0 | } |
181 | | |
182 | | /*---------------------------------------------------------------------------- |
183 | | | Returns a quiet NaN from a signalling NaN for the deconstructed |
184 | | | floating-point parts. |
185 | | *----------------------------------------------------------------------------*/ |
186 | | |
187 | | static uint64_t parts_silence_nan_frac(uint64_t frac, float_status *status) |
188 | 0 | { |
189 | 0 | switch (get_snan_rule(status)) { |
190 | 0 | case float_snan_bit_is_zero: |
191 | 0 | frac |= 1ULL << (DECOMPOSED_BINARY_POINT - 1); |
192 | 0 | break; |
193 | 0 | case float_snan_bit_is_one: |
194 | | /* The only snan_bit_is_one target without default_nan_mode is HPPA. */ |
195 | 0 | frac &= ~(1ULL << (DECOMPOSED_BINARY_POINT - 1)); |
196 | 0 | frac |= 1ULL << (DECOMPOSED_BINARY_POINT - 2); |
197 | 0 | break; |
198 | 0 | case float_snan_never: |
199 | 0 | default: |
200 | 0 | g_assert_not_reached(); |
201 | 0 | } |
202 | 0 | return frac; |
203 | 0 | } |
204 | | |
205 | | static FloatParts64 parts64_silence_nan(const FloatParts64 *p, |
206 | | float_status *status) |
207 | 0 | { |
208 | 0 | FloatParts64 r = *p; |
209 | |
|
210 | 0 | r.frac = parts_silence_nan_frac(r.frac, status); |
211 | 0 | r.cls = float_class_qnan; |
212 | 0 | return r; |
213 | 0 | } |
214 | | |
215 | | static FloatParts128 parts128_silence_nan(const FloatParts128 *p, |
216 | | float_status *status) |
217 | 0 | { |
218 | 0 | FloatParts128 r = *p; |
219 | |
|
220 | 0 | r.frac_hi = parts_silence_nan_frac(r.frac_hi, status); |
221 | 0 | r.cls = float_class_qnan; |
222 | 0 | return r; |
223 | 0 | } |
224 | | |
225 | | /*---------------------------------------------------------------------------- |
226 | | | The pattern for a default generated extended double-precision NaN. |
227 | | *----------------------------------------------------------------------------*/ |
228 | | floatx80 floatx80_default_nan(float_status *status) |
229 | 0 | { |
230 | | /* |
231 | | * Extrapolate from the choices made by parts64_default_nan to fill |
232 | | * in the floatx80 format. We assume that floatx80's explicit |
233 | | * integer bit is always set (this is true for i386 and m68k, |
234 | | * which are the only real users of this format). |
235 | | */ |
236 | 0 | FloatParts64 p64 = parts64_default_nan(status); |
237 | |
|
238 | 0 | return (floatx80) { |
239 | 0 | .high = 0x7FFF | (p64.sign << 15), |
240 | 0 | .low = (1ULL << DECOMPOSED_BINARY_POINT) | p64.frac, |
241 | 0 | }; |
242 | 0 | } |
243 | | |
244 | | /*---------------------------------------------------------------------------- |
245 | | | The pattern for a default generated extended double-precision inf. |
246 | | *----------------------------------------------------------------------------*/ |
247 | | |
248 | | floatx80 floatx80_default_inf(bool zSign, float_status *status) |
249 | 0 | { |
250 | | /* |
251 | | * Whether the Integer bit is set in the default Infinity is |
252 | | * target dependent. |
253 | | */ |
254 | 0 | bool z = get_floatx80_behaviour(status) & floatx80_default_inf_int_bit_is_zero; |
255 | 0 | return packFloatx80(zSign, 0x7fff, z ? 0 : (1ULL << 63)); |
256 | 0 | } |
257 | | |
258 | | /*---------------------------------------------------------------------------- |
259 | | | Determine if a float16 NaN is signaling NaN. |
260 | | *----------------------------------------------------------------------------*/ |
261 | | |
262 | | static bool float16_nan_is_snan(float16 a, float_status *status) |
263 | 0 | { |
264 | 0 | return frac_msb_is_snan((a >> 9) & 1, status); |
265 | 0 | } |
266 | | |
267 | | /*---------------------------------------------------------------------------- |
268 | | | Returns 1 if the half-precision floating-point value `a' is a quiet |
269 | | | NaN; otherwise returns 0. |
270 | | *----------------------------------------------------------------------------*/ |
271 | | |
272 | | bool float16_is_quiet_nan(float16 a_, float_status *status) |
273 | 0 | { |
274 | 0 | return float16_is_any_nan(a_) && !float16_nan_is_snan(a_, status); |
275 | 0 | } |
276 | | |
277 | | /*---------------------------------------------------------------------------- |
278 | | | Returns 1 if the half-precision floating-point value `a' is a signaling |
279 | | | NaN; otherwise returns 0. |
280 | | *----------------------------------------------------------------------------*/ |
281 | | |
282 | | bool float16_is_signaling_nan(float16 a_, float_status *status) |
283 | 0 | { |
284 | 0 | return float16_is_any_nan(a_) && float16_nan_is_snan(a_, status); |
285 | 0 | } |
286 | | |
287 | | /*---------------------------------------------------------------------------- |
288 | | | Determine if a bfloat16 NaN is signaling NaN. |
289 | | *----------------------------------------------------------------------------*/ |
290 | | |
291 | | static bool bfloat16_nan_is_snan(bfloat16 a, float_status *status) |
292 | 0 | { |
293 | 0 | return frac_msb_is_snan((a >> 6) & 1, status); |
294 | 0 | } |
295 | | |
296 | | /*---------------------------------------------------------------------------- |
297 | | | Returns 1 if the bfloat16 value `a' is a quiet NaN; otherwise returns 0. |
298 | | *----------------------------------------------------------------------------*/ |
299 | | |
300 | | bool bfloat16_is_quiet_nan(bfloat16 a_, float_status *status) |
301 | 0 | { |
302 | 0 | return bfloat16_is_any_nan(a_) && !bfloat16_nan_is_snan(a_, status); |
303 | 0 | } |
304 | | |
305 | | /*---------------------------------------------------------------------------- |
306 | | | Returns 1 if the bfloat16 value `a' is a signaling NaN; otherwise returns 0. |
307 | | *----------------------------------------------------------------------------*/ |
308 | | |
309 | | bool bfloat16_is_signaling_nan(bfloat16 a_, float_status *status) |
310 | 0 | { |
311 | 0 | return bfloat16_is_any_nan(a_) && bfloat16_nan_is_snan(a_, status); |
312 | 0 | } |
313 | | |
314 | | /*---------------------------------------------------------------------------- |
315 | | | Determine if a float32 NaN is signaling NaN. |
316 | | *----------------------------------------------------------------------------*/ |
317 | | |
318 | | static bool float32_nan_is_snan(float32 a, float_status *status) |
319 | 0 | { |
320 | 0 | return frac_msb_is_snan((a >> 22) & 1, status); |
321 | 0 | } |
322 | | |
323 | | /*---------------------------------------------------------------------------- |
324 | | | Returns 1 if the single-precision floating-point value `a' is a quiet |
325 | | | NaN; otherwise returns 0. |
326 | | *----------------------------------------------------------------------------*/ |
327 | | |
328 | | bool float32_is_quiet_nan(float32 a_, float_status *status) |
329 | 0 | { |
330 | 0 | return float32_is_any_nan(a_) && !float32_nan_is_snan(a_, status); |
331 | 0 | } |
332 | | |
333 | | /*---------------------------------------------------------------------------- |
334 | | | Returns 1 if the single-precision floating-point value `a' is a signaling |
335 | | | NaN; otherwise returns 0. |
336 | | *----------------------------------------------------------------------------*/ |
337 | | |
338 | | bool float32_is_signaling_nan(float32 a_, float_status *status) |
339 | 0 | { |
340 | 0 | return float32_is_any_nan(a_) && float32_nan_is_snan(a_, status); |
341 | 0 | } |
342 | | |
343 | | /*---------------------------------------------------------------------------- |
344 | | | Determine if a float64 NaN is signaling NaN. |
345 | | *----------------------------------------------------------------------------*/ |
346 | | |
347 | | static bool float64_nan_is_snan(float64 a, float_status *status) |
348 | 0 | { |
349 | 0 | return frac_msb_is_snan((a >> 51) & 1, status); |
350 | 0 | } |
351 | | |
352 | | /*---------------------------------------------------------------------------- |
353 | | | Returns 1 if the double-precision floating-point value `a' is a quiet |
354 | | | NaN; otherwise returns 0. |
355 | | *----------------------------------------------------------------------------*/ |
356 | | |
357 | | bool float64_is_quiet_nan(float64 a_, float_status *status) |
358 | 0 | { |
359 | 0 | return float64_is_any_nan(a_) && !float64_nan_is_snan(a_, status); |
360 | 0 | } |
361 | | |
362 | | /*---------------------------------------------------------------------------- |
363 | | | Returns 1 if the double-precision floating-point value `a' is a signaling |
364 | | | NaN; otherwise returns 0. |
365 | | *----------------------------------------------------------------------------*/ |
366 | | |
367 | | bool float64_is_signaling_nan(float64 a_, float_status *status) |
368 | 0 | { |
369 | 0 | return float64_is_any_nan(a_) && float64_nan_is_snan(a_, status); |
370 | 0 | } |
371 | | |
372 | | /*---------------------------------------------------------------------------- |
373 | | | Determine if a floatx80 NaN is signaling NaN. |
374 | | | The MSB of frac differs from the same function for other types as floatx80 |
375 | | | has an explicit bit. |
376 | | *----------------------------------------------------------------------------*/ |
377 | | |
378 | | static bool floatx80_nan_is_snan(floatx80 a, float_status *status) |
379 | 0 | { |
380 | 0 | return frac_msb_is_snan((a.low >> 62) & 1, status); |
381 | 0 | } |
382 | | |
383 | | /*---------------------------------------------------------------------------- |
384 | | | Returns 1 if the extended double-precision floating-point value `a' is a |
385 | | | quiet NaN; otherwise returns 0. |
386 | | *----------------------------------------------------------------------------*/ |
387 | | |
388 | | bool floatx80_is_quiet_nan(floatx80 a, float_status *status) |
389 | 0 | { |
390 | 0 | return floatx80_is_any_nan(a) && !floatx80_nan_is_snan(a, status); |
391 | 0 | } |
392 | | |
393 | | /*---------------------------------------------------------------------------- |
394 | | | Returns 1 if the extended double-precision floating-point value `a' is a |
395 | | | signaling NaN; otherwise returns 0. |
396 | | *----------------------------------------------------------------------------*/ |
397 | | |
398 | | bool floatx80_is_signaling_nan(floatx80 a, float_status *status) |
399 | 0 | { |
400 | 0 | return floatx80_is_any_nan(a) && floatx80_nan_is_snan(a, status); |
401 | 0 | } |
402 | | |
403 | | /*---------------------------------------------------------------------------- |
404 | | | Returns a quiet NaN from a signalling NaN for the extended double-precision |
405 | | | floating point value `a'. |
406 | | *----------------------------------------------------------------------------*/ |
407 | | |
408 | | floatx80 floatx80_silence_nan(floatx80 a, float_status *status) |
409 | 0 | { |
410 | | /* None of the targets that have snan_bit_is_one use floatx80. */ |
411 | 0 | assert(get_snan_rule(status) == float_snan_bit_is_zero); |
412 | 0 | a.low |= UINT64_C(0xC000000000000000); |
413 | 0 | return a; |
414 | 0 | } |
415 | | |
416 | | /*---------------------------------------------------------------------------- |
417 | | | Determine if a float128 NaN is signaling NaN. |
418 | | *----------------------------------------------------------------------------*/ |
419 | | |
420 | | static bool float128_nan_is_snan(float128 a, float_status *status) |
421 | 0 | { |
422 | 0 | return frac_msb_is_snan((a.high >> 47) & 1, status); |
423 | 0 | } |
424 | | |
425 | | /*---------------------------------------------------------------------------- |
426 | | | Returns 1 if the quadruple-precision floating-point value `a' is a quiet |
427 | | | NaN; otherwise returns 0. |
428 | | *----------------------------------------------------------------------------*/ |
429 | | |
430 | | bool float128_is_quiet_nan(float128 a, float_status *status) |
431 | 0 | { |
432 | 0 | return float128_is_any_nan(a) && !float128_nan_is_snan(a, status); |
433 | 0 | } |
434 | | |
435 | | /*---------------------------------------------------------------------------- |
436 | | | Returns 1 if the quadruple-precision floating-point value `a' is a |
437 | | | signaling NaN; otherwise returns 0. |
438 | | *----------------------------------------------------------------------------*/ |
439 | | |
440 | | bool float128_is_signaling_nan(float128 a, float_status *status) |
441 | 0 | { |
442 | 0 | return float128_is_any_nan(a) && float128_nan_is_snan(a, status); |
443 | 0 | } |