xref: /qemu/include/fpu/softfloat.h (revision ab9056ff)
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 header file 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 #ifndef SOFTFLOAT_H
83 #define SOFTFLOAT_H
84 
85 /*----------------------------------------------------------------------------
86 | Software IEC/IEEE floating-point ordering relations
87 *----------------------------------------------------------------------------*/
88 enum {
89     float_relation_less      = -1,
90     float_relation_equal     =  0,
91     float_relation_greater   =  1,
92     float_relation_unordered =  2
93 };
94 
95 #include "fpu/softfloat-types.h"
96 #include "fpu/softfloat-helpers.h"
97 
98 /*----------------------------------------------------------------------------
99 | Routine to raise any or all of the software IEC/IEEE floating-point
100 | exception flags.
101 *----------------------------------------------------------------------------*/
102 void float_raise(uint8_t flags, float_status *status);
103 
104 /*----------------------------------------------------------------------------
105 | If `a' is denormal and we are in flush-to-zero mode then set the
106 | input-denormal exception and return zero. Otherwise just return the value.
107 *----------------------------------------------------------------------------*/
108 float16 float16_squash_input_denormal(float16 a, float_status *status);
109 float32 float32_squash_input_denormal(float32 a, float_status *status);
110 float64 float64_squash_input_denormal(float64 a, float_status *status);
111 
112 /*----------------------------------------------------------------------------
113 | Options to indicate which negations to perform in float*_muladd()
114 | Using these differs from negating an input or output before calling
115 | the muladd function in that this means that a NaN doesn't have its
116 | sign bit inverted before it is propagated.
117 | We also support halving the result before rounding, as a special
118 | case to support the ARM fused-sqrt-step instruction FRSQRTS.
119 *----------------------------------------------------------------------------*/
120 enum {
121     float_muladd_negate_c = 1,
122     float_muladd_negate_product = 2,
123     float_muladd_negate_result = 4,
124     float_muladd_halve_result = 8,
125 };
126 
127 /*----------------------------------------------------------------------------
128 | Software IEC/IEEE integer-to-floating-point conversion routines.
129 *----------------------------------------------------------------------------*/
130 
131 float16 int16_to_float16_scalbn(int16_t a, int, float_status *status);
132 float16 int32_to_float16_scalbn(int32_t a, int, float_status *status);
133 float16 int64_to_float16_scalbn(int64_t a, int, float_status *status);
134 float16 uint16_to_float16_scalbn(uint16_t a, int, float_status *status);
135 float16 uint32_to_float16_scalbn(uint32_t a, int, float_status *status);
136 float16 uint64_to_float16_scalbn(uint64_t a, int, float_status *status);
137 
138 float16 int16_to_float16(int16_t a, float_status *status);
139 float16 int32_to_float16(int32_t a, float_status *status);
140 float16 int64_to_float16(int64_t a, float_status *status);
141 float16 uint16_to_float16(uint16_t a, float_status *status);
142 float16 uint32_to_float16(uint32_t a, float_status *status);
143 float16 uint64_to_float16(uint64_t a, float_status *status);
144 
145 float32 int16_to_float32_scalbn(int16_t, int, float_status *status);
146 float32 int32_to_float32_scalbn(int32_t, int, float_status *status);
147 float32 int64_to_float32_scalbn(int64_t, int, float_status *status);
148 float32 uint16_to_float32_scalbn(uint16_t, int, float_status *status);
149 float32 uint32_to_float32_scalbn(uint32_t, int, float_status *status);
150 float32 uint64_to_float32_scalbn(uint64_t, int, float_status *status);
151 
152 float32 int16_to_float32(int16_t, float_status *status);
153 float32 int32_to_float32(int32_t, float_status *status);
154 float32 int64_to_float32(int64_t, float_status *status);
155 float32 uint16_to_float32(uint16_t, float_status *status);
156 float32 uint32_to_float32(uint32_t, float_status *status);
157 float32 uint64_to_float32(uint64_t, float_status *status);
158 
159 float64 int16_to_float64_scalbn(int16_t, int, float_status *status);
160 float64 int32_to_float64_scalbn(int32_t, int, float_status *status);
161 float64 int64_to_float64_scalbn(int64_t, int, float_status *status);
162 float64 uint16_to_float64_scalbn(uint16_t, int, float_status *status);
163 float64 uint32_to_float64_scalbn(uint32_t, int, float_status *status);
164 float64 uint64_to_float64_scalbn(uint64_t, int, float_status *status);
165 
166 float64 int16_to_float64(int16_t, float_status *status);
167 float64 int32_to_float64(int32_t, float_status *status);
168 float64 int64_to_float64(int64_t, float_status *status);
169 float64 uint16_to_float64(uint16_t, float_status *status);
170 float64 uint32_to_float64(uint32_t, float_status *status);
171 float64 uint64_to_float64(uint64_t, float_status *status);
172 
173 floatx80 int32_to_floatx80(int32_t, float_status *status);
174 floatx80 int64_to_floatx80(int64_t, float_status *status);
175 
176 float128 int32_to_float128(int32_t, float_status *status);
177 float128 int64_to_float128(int64_t, float_status *status);
178 float128 uint64_to_float128(uint64_t, float_status *status);
179 
180 /*----------------------------------------------------------------------------
181 | Software half-precision conversion routines.
182 *----------------------------------------------------------------------------*/
183 
184 float16 float32_to_float16(float32, bool ieee, float_status *status);
185 float32 float16_to_float32(float16, bool ieee, float_status *status);
186 float16 float64_to_float16(float64 a, bool ieee, float_status *status);
187 float64 float16_to_float64(float16 a, bool ieee, float_status *status);
188 
189 int16_t float16_to_int16_scalbn(float16, int, int, float_status *status);
190 int32_t float16_to_int32_scalbn(float16, int, int, float_status *status);
191 int64_t float16_to_int64_scalbn(float16, int, int, float_status *status);
192 
193 int16_t float16_to_int16(float16, float_status *status);
194 int32_t float16_to_int32(float16, float_status *status);
195 int64_t float16_to_int64(float16, float_status *status);
196 
197 int16_t float16_to_int16_round_to_zero(float16, float_status *status);
198 int32_t float16_to_int32_round_to_zero(float16, float_status *status);
199 int64_t float16_to_int64_round_to_zero(float16, float_status *status);
200 
201 uint16_t float16_to_uint16_scalbn(float16 a, int, int, float_status *status);
202 uint32_t float16_to_uint32_scalbn(float16 a, int, int, float_status *status);
203 uint64_t float16_to_uint64_scalbn(float16 a, int, int, float_status *status);
204 
205 uint16_t float16_to_uint16(float16 a, float_status *status);
206 uint32_t float16_to_uint32(float16 a, float_status *status);
207 uint64_t float16_to_uint64(float16 a, float_status *status);
208 
209 uint16_t float16_to_uint16_round_to_zero(float16 a, float_status *status);
210 uint32_t float16_to_uint32_round_to_zero(float16 a, float_status *status);
211 uint64_t float16_to_uint64_round_to_zero(float16 a, float_status *status);
212 
213 /*----------------------------------------------------------------------------
214 | Software half-precision operations.
215 *----------------------------------------------------------------------------*/
216 
217 float16 float16_round_to_int(float16, float_status *status);
218 float16 float16_add(float16, float16, float_status *status);
219 float16 float16_sub(float16, float16, float_status *status);
220 float16 float16_mul(float16, float16, float_status *status);
221 float16 float16_muladd(float16, float16, float16, int, float_status *status);
222 float16 float16_div(float16, float16, float_status *status);
223 float16 float16_scalbn(float16, int, float_status *status);
224 float16 float16_min(float16, float16, float_status *status);
225 float16 float16_max(float16, float16, float_status *status);
226 float16 float16_minnum(float16, float16, float_status *status);
227 float16 float16_maxnum(float16, float16, float_status *status);
228 float16 float16_minnummag(float16, float16, float_status *status);
229 float16 float16_maxnummag(float16, float16, float_status *status);
230 float16 float16_sqrt(float16, float_status *status);
231 int float16_compare(float16, float16, float_status *status);
232 int float16_compare_quiet(float16, float16, float_status *status);
233 
234 int float16_is_quiet_nan(float16, float_status *status);
235 int float16_is_signaling_nan(float16, float_status *status);
236 float16 float16_silence_nan(float16, float_status *status);
237 
238 static inline int float16_is_any_nan(float16 a)
239 {
240     return ((float16_val(a) & ~0x8000) > 0x7c00);
241 }
242 
243 static inline int float16_is_neg(float16 a)
244 {
245     return float16_val(a) >> 15;
246 }
247 
248 static inline int float16_is_infinity(float16 a)
249 {
250     return (float16_val(a) & 0x7fff) == 0x7c00;
251 }
252 
253 static inline int float16_is_zero(float16 a)
254 {
255     return (float16_val(a) & 0x7fff) == 0;
256 }
257 
258 static inline int float16_is_zero_or_denormal(float16 a)
259 {
260     return (float16_val(a) & 0x7c00) == 0;
261 }
262 
263 static inline float16 float16_abs(float16 a)
264 {
265     /* Note that abs does *not* handle NaN specially, nor does
266      * it flush denormal inputs to zero.
267      */
268     return make_float16(float16_val(a) & 0x7fff);
269 }
270 
271 static inline float16 float16_chs(float16 a)
272 {
273     /* Note that chs does *not* handle NaN specially, nor does
274      * it flush denormal inputs to zero.
275      */
276     return make_float16(float16_val(a) ^ 0x8000);
277 }
278 
279 static inline float16 float16_set_sign(float16 a, int sign)
280 {
281     return make_float16((float16_val(a) & 0x7fff) | (sign << 15));
282 }
283 
284 #define float16_zero make_float16(0)
285 #define float16_half make_float16(0x3800)
286 #define float16_one make_float16(0x3c00)
287 #define float16_one_point_five make_float16(0x3e00)
288 #define float16_two make_float16(0x4000)
289 #define float16_three make_float16(0x4200)
290 #define float16_infinity make_float16(0x7c00)
291 
292 /*----------------------------------------------------------------------------
293 | The pattern for a default generated half-precision NaN.
294 *----------------------------------------------------------------------------*/
295 float16 float16_default_nan(float_status *status);
296 
297 /*----------------------------------------------------------------------------
298 | Software IEC/IEEE single-precision conversion routines.
299 *----------------------------------------------------------------------------*/
300 
301 int16_t float32_to_int16_scalbn(float32, int, int, float_status *status);
302 int32_t float32_to_int32_scalbn(float32, int, int, float_status *status);
303 int64_t float32_to_int64_scalbn(float32, int, int, float_status *status);
304 
305 int16_t float32_to_int16(float32, float_status *status);
306 int32_t float32_to_int32(float32, float_status *status);
307 int64_t float32_to_int64(float32, float_status *status);
308 
309 int16_t float32_to_int16_round_to_zero(float32, float_status *status);
310 int32_t float32_to_int32_round_to_zero(float32, float_status *status);
311 int64_t float32_to_int64_round_to_zero(float32, float_status *status);
312 
313 uint16_t float32_to_uint16_scalbn(float32, int, int, float_status *status);
314 uint32_t float32_to_uint32_scalbn(float32, int, int, float_status *status);
315 uint64_t float32_to_uint64_scalbn(float32, int, int, float_status *status);
316 
317 uint16_t float32_to_uint16(float32, float_status *status);
318 uint32_t float32_to_uint32(float32, float_status *status);
319 uint64_t float32_to_uint64(float32, float_status *status);
320 
321 uint16_t float32_to_uint16_round_to_zero(float32, float_status *status);
322 uint32_t float32_to_uint32_round_to_zero(float32, float_status *status);
323 uint64_t float32_to_uint64_round_to_zero(float32, float_status *status);
324 
325 float64 float32_to_float64(float32, float_status *status);
326 floatx80 float32_to_floatx80(float32, float_status *status);
327 float128 float32_to_float128(float32, float_status *status);
328 
329 /*----------------------------------------------------------------------------
330 | Software IEC/IEEE single-precision operations.
331 *----------------------------------------------------------------------------*/
332 float32 float32_round_to_int(float32, float_status *status);
333 float32 float32_add(float32, float32, float_status *status);
334 float32 float32_sub(float32, float32, float_status *status);
335 float32 float32_mul(float32, float32, float_status *status);
336 float32 float32_div(float32, float32, float_status *status);
337 float32 float32_rem(float32, float32, float_status *status);
338 float32 float32_muladd(float32, float32, float32, int, float_status *status);
339 float32 float32_sqrt(float32, float_status *status);
340 float32 float32_exp2(float32, float_status *status);
341 float32 float32_log2(float32, float_status *status);
342 int float32_eq(float32, float32, float_status *status);
343 int float32_le(float32, float32, float_status *status);
344 int float32_lt(float32, float32, float_status *status);
345 int float32_unordered(float32, float32, float_status *status);
346 int float32_eq_quiet(float32, float32, float_status *status);
347 int float32_le_quiet(float32, float32, float_status *status);
348 int float32_lt_quiet(float32, float32, float_status *status);
349 int float32_unordered_quiet(float32, float32, float_status *status);
350 int float32_compare(float32, float32, float_status *status);
351 int float32_compare_quiet(float32, float32, float_status *status);
352 float32 float32_min(float32, float32, float_status *status);
353 float32 float32_max(float32, float32, float_status *status);
354 float32 float32_minnum(float32, float32, float_status *status);
355 float32 float32_maxnum(float32, float32, float_status *status);
356 float32 float32_minnummag(float32, float32, float_status *status);
357 float32 float32_maxnummag(float32, float32, float_status *status);
358 int float32_is_quiet_nan(float32, float_status *status);
359 int float32_is_signaling_nan(float32, float_status *status);
360 float32 float32_silence_nan(float32, float_status *status);
361 float32 float32_scalbn(float32, int, float_status *status);
362 
363 static inline float32 float32_abs(float32 a)
364 {
365     /* Note that abs does *not* handle NaN specially, nor does
366      * it flush denormal inputs to zero.
367      */
368     return make_float32(float32_val(a) & 0x7fffffff);
369 }
370 
371 static inline float32 float32_chs(float32 a)
372 {
373     /* Note that chs does *not* handle NaN specially, nor does
374      * it flush denormal inputs to zero.
375      */
376     return make_float32(float32_val(a) ^ 0x80000000);
377 }
378 
379 static inline int float32_is_infinity(float32 a)
380 {
381     return (float32_val(a) & 0x7fffffff) == 0x7f800000;
382 }
383 
384 static inline int float32_is_neg(float32 a)
385 {
386     return float32_val(a) >> 31;
387 }
388 
389 static inline int float32_is_zero(float32 a)
390 {
391     return (float32_val(a) & 0x7fffffff) == 0;
392 }
393 
394 static inline int float32_is_any_nan(float32 a)
395 {
396     return ((float32_val(a) & ~(1 << 31)) > 0x7f800000UL);
397 }
398 
399 static inline int float32_is_zero_or_denormal(float32 a)
400 {
401     return (float32_val(a) & 0x7f800000) == 0;
402 }
403 
404 static inline bool float32_is_normal(float32 a)
405 {
406     return (((float32_val(a) >> 23) + 1) & 0xff) >= 2;
407 }
408 
409 static inline bool float32_is_denormal(float32 a)
410 {
411     return float32_is_zero_or_denormal(a) && !float32_is_zero(a);
412 }
413 
414 static inline bool float32_is_zero_or_normal(float32 a)
415 {
416     return float32_is_normal(a) || float32_is_zero(a);
417 }
418 
419 static inline float32 float32_set_sign(float32 a, int sign)
420 {
421     return make_float32((float32_val(a) & 0x7fffffff) | (sign << 31));
422 }
423 
424 #define float32_zero make_float32(0)
425 #define float32_half make_float32(0x3f000000)
426 #define float32_one make_float32(0x3f800000)
427 #define float32_one_point_five make_float32(0x3fc00000)
428 #define float32_two make_float32(0x40000000)
429 #define float32_three make_float32(0x40400000)
430 #define float32_infinity make_float32(0x7f800000)
431 
432 /*----------------------------------------------------------------------------
433 | Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
434 | single-precision floating-point value, returning the result.  After being
435 | shifted into the proper positions, the three fields are simply added
436 | together to form the result.  This means that any integer portion of `zSig'
437 | will be added into the exponent.  Since a properly normalized significand
438 | will have an integer portion equal to 1, the `zExp' input should be 1 less
439 | than the desired result exponent whenever `zSig' is a complete, normalized
440 | significand.
441 *----------------------------------------------------------------------------*/
442 
443 static inline float32 packFloat32(flag zSign, int zExp, uint32_t zSig)
444 {
445     return make_float32(
446           (((uint32_t)zSign) << 31) + (((uint32_t)zExp) << 23) + zSig);
447 }
448 
449 /*----------------------------------------------------------------------------
450 | The pattern for a default generated single-precision NaN.
451 *----------------------------------------------------------------------------*/
452 float32 float32_default_nan(float_status *status);
453 
454 /*----------------------------------------------------------------------------
455 | Software IEC/IEEE double-precision conversion routines.
456 *----------------------------------------------------------------------------*/
457 
458 int16_t float64_to_int16_scalbn(float64, int, int, float_status *status);
459 int32_t float64_to_int32_scalbn(float64, int, int, float_status *status);
460 int64_t float64_to_int64_scalbn(float64, int, int, float_status *status);
461 
462 int16_t float64_to_int16(float64, float_status *status);
463 int32_t float64_to_int32(float64, float_status *status);
464 int64_t float64_to_int64(float64, float_status *status);
465 
466 int16_t float64_to_int16_round_to_zero(float64, float_status *status);
467 int32_t float64_to_int32_round_to_zero(float64, float_status *status);
468 int64_t float64_to_int64_round_to_zero(float64, float_status *status);
469 
470 uint16_t float64_to_uint16_scalbn(float64, int, int, float_status *status);
471 uint32_t float64_to_uint32_scalbn(float64, int, int, float_status *status);
472 uint64_t float64_to_uint64_scalbn(float64, int, int, float_status *status);
473 
474 uint16_t float64_to_uint16(float64, float_status *status);
475 uint32_t float64_to_uint32(float64, float_status *status);
476 uint64_t float64_to_uint64(float64, float_status *status);
477 
478 uint16_t float64_to_uint16_round_to_zero(float64, float_status *status);
479 uint32_t float64_to_uint32_round_to_zero(float64, float_status *status);
480 uint64_t float64_to_uint64_round_to_zero(float64, float_status *status);
481 
482 float32 float64_to_float32(float64, float_status *status);
483 floatx80 float64_to_floatx80(float64, float_status *status);
484 float128 float64_to_float128(float64, float_status *status);
485 
486 /*----------------------------------------------------------------------------
487 | Software IEC/IEEE double-precision operations.
488 *----------------------------------------------------------------------------*/
489 float64 float64_round_to_int(float64, float_status *status);
490 float64 float64_add(float64, float64, float_status *status);
491 float64 float64_sub(float64, float64, float_status *status);
492 float64 float64_mul(float64, float64, float_status *status);
493 float64 float64_div(float64, float64, float_status *status);
494 float64 float64_rem(float64, float64, float_status *status);
495 float64 float64_muladd(float64, float64, float64, int, float_status *status);
496 float64 float64_sqrt(float64, float_status *status);
497 float64 float64_log2(float64, float_status *status);
498 int float64_eq(float64, float64, float_status *status);
499 int float64_le(float64, float64, float_status *status);
500 int float64_lt(float64, float64, float_status *status);
501 int float64_unordered(float64, float64, float_status *status);
502 int float64_eq_quiet(float64, float64, float_status *status);
503 int float64_le_quiet(float64, float64, float_status *status);
504 int float64_lt_quiet(float64, float64, float_status *status);
505 int float64_unordered_quiet(float64, float64, float_status *status);
506 int float64_compare(float64, float64, float_status *status);
507 int float64_compare_quiet(float64, float64, float_status *status);
508 float64 float64_min(float64, float64, float_status *status);
509 float64 float64_max(float64, float64, float_status *status);
510 float64 float64_minnum(float64, float64, float_status *status);
511 float64 float64_maxnum(float64, float64, float_status *status);
512 float64 float64_minnummag(float64, float64, float_status *status);
513 float64 float64_maxnummag(float64, float64, float_status *status);
514 int float64_is_quiet_nan(float64 a, float_status *status);
515 int float64_is_signaling_nan(float64, float_status *status);
516 float64 float64_silence_nan(float64, float_status *status);
517 float64 float64_scalbn(float64, int, float_status *status);
518 
519 static inline float64 float64_abs(float64 a)
520 {
521     /* Note that abs does *not* handle NaN specially, nor does
522      * it flush denormal inputs to zero.
523      */
524     return make_float64(float64_val(a) & 0x7fffffffffffffffLL);
525 }
526 
527 static inline float64 float64_chs(float64 a)
528 {
529     /* Note that chs does *not* handle NaN specially, nor does
530      * it flush denormal inputs to zero.
531      */
532     return make_float64(float64_val(a) ^ 0x8000000000000000LL);
533 }
534 
535 static inline int float64_is_infinity(float64 a)
536 {
537     return (float64_val(a) & 0x7fffffffffffffffLL ) == 0x7ff0000000000000LL;
538 }
539 
540 static inline int float64_is_neg(float64 a)
541 {
542     return float64_val(a) >> 63;
543 }
544 
545 static inline int float64_is_zero(float64 a)
546 {
547     return (float64_val(a) & 0x7fffffffffffffffLL) == 0;
548 }
549 
550 static inline int float64_is_any_nan(float64 a)
551 {
552     return ((float64_val(a) & ~(1ULL << 63)) > 0x7ff0000000000000ULL);
553 }
554 
555 static inline int float64_is_zero_or_denormal(float64 a)
556 {
557     return (float64_val(a) & 0x7ff0000000000000LL) == 0;
558 }
559 
560 static inline bool float64_is_normal(float64 a)
561 {
562     return (((float64_val(a) >> 52) + 1) & 0x7ff) >= 2;
563 }
564 
565 static inline bool float64_is_denormal(float64 a)
566 {
567     return float64_is_zero_or_denormal(a) && !float64_is_zero(a);
568 }
569 
570 static inline bool float64_is_zero_or_normal(float64 a)
571 {
572     return float64_is_normal(a) || float64_is_zero(a);
573 }
574 
575 static inline float64 float64_set_sign(float64 a, int sign)
576 {
577     return make_float64((float64_val(a) & 0x7fffffffffffffffULL)
578                         | ((int64_t)sign << 63));
579 }
580 
581 #define float64_zero make_float64(0)
582 #define float64_half make_float64(0x3fe0000000000000LL)
583 #define float64_one make_float64(0x3ff0000000000000LL)
584 #define float64_one_point_five make_float64(0x3FF8000000000000ULL)
585 #define float64_two make_float64(0x4000000000000000ULL)
586 #define float64_three make_float64(0x4008000000000000ULL)
587 #define float64_ln2 make_float64(0x3fe62e42fefa39efLL)
588 #define float64_infinity make_float64(0x7ff0000000000000LL)
589 
590 /*----------------------------------------------------------------------------
591 | The pattern for a default generated double-precision NaN.
592 *----------------------------------------------------------------------------*/
593 float64 float64_default_nan(float_status *status);
594 
595 /*----------------------------------------------------------------------------
596 | Software IEC/IEEE extended double-precision conversion routines.
597 *----------------------------------------------------------------------------*/
598 int32_t floatx80_to_int32(floatx80, float_status *status);
599 int32_t floatx80_to_int32_round_to_zero(floatx80, float_status *status);
600 int64_t floatx80_to_int64(floatx80, float_status *status);
601 int64_t floatx80_to_int64_round_to_zero(floatx80, float_status *status);
602 float32 floatx80_to_float32(floatx80, float_status *status);
603 float64 floatx80_to_float64(floatx80, float_status *status);
604 float128 floatx80_to_float128(floatx80, float_status *status);
605 
606 /*----------------------------------------------------------------------------
607 | The pattern for an extended double-precision inf.
608 *----------------------------------------------------------------------------*/
609 extern const floatx80 floatx80_infinity;
610 
611 /*----------------------------------------------------------------------------
612 | Software IEC/IEEE extended double-precision operations.
613 *----------------------------------------------------------------------------*/
614 floatx80 floatx80_round(floatx80 a, float_status *status);
615 floatx80 floatx80_round_to_int(floatx80, float_status *status);
616 floatx80 floatx80_add(floatx80, floatx80, float_status *status);
617 floatx80 floatx80_sub(floatx80, floatx80, float_status *status);
618 floatx80 floatx80_mul(floatx80, floatx80, float_status *status);
619 floatx80 floatx80_div(floatx80, floatx80, float_status *status);
620 floatx80 floatx80_rem(floatx80, floatx80, float_status *status);
621 floatx80 floatx80_sqrt(floatx80, float_status *status);
622 int floatx80_eq(floatx80, floatx80, float_status *status);
623 int floatx80_le(floatx80, floatx80, float_status *status);
624 int floatx80_lt(floatx80, floatx80, float_status *status);
625 int floatx80_unordered(floatx80, floatx80, float_status *status);
626 int floatx80_eq_quiet(floatx80, floatx80, float_status *status);
627 int floatx80_le_quiet(floatx80, floatx80, float_status *status);
628 int floatx80_lt_quiet(floatx80, floatx80, float_status *status);
629 int floatx80_unordered_quiet(floatx80, floatx80, float_status *status);
630 int floatx80_compare(floatx80, floatx80, float_status *status);
631 int floatx80_compare_quiet(floatx80, floatx80, float_status *status);
632 int floatx80_is_quiet_nan(floatx80, float_status *status);
633 int floatx80_is_signaling_nan(floatx80, float_status *status);
634 floatx80 floatx80_silence_nan(floatx80, float_status *status);
635 floatx80 floatx80_scalbn(floatx80, int, float_status *status);
636 
637 static inline floatx80 floatx80_abs(floatx80 a)
638 {
639     a.high &= 0x7fff;
640     return a;
641 }
642 
643 static inline floatx80 floatx80_chs(floatx80 a)
644 {
645     a.high ^= 0x8000;
646     return a;
647 }
648 
649 static inline int floatx80_is_infinity(floatx80 a)
650 {
651 #if defined(TARGET_M68K)
652     return (a.high & 0x7fff) == floatx80_infinity.high && !(a.low << 1);
653 #else
654     return (a.high & 0x7fff) == floatx80_infinity.high &&
655                        a.low == floatx80_infinity.low;
656 #endif
657 }
658 
659 static inline int floatx80_is_neg(floatx80 a)
660 {
661     return a.high >> 15;
662 }
663 
664 static inline int floatx80_is_zero(floatx80 a)
665 {
666     return (a.high & 0x7fff) == 0 && a.low == 0;
667 }
668 
669 static inline int floatx80_is_zero_or_denormal(floatx80 a)
670 {
671     return (a.high & 0x7fff) == 0;
672 }
673 
674 static inline int floatx80_is_any_nan(floatx80 a)
675 {
676     return ((a.high & 0x7fff) == 0x7fff) && (a.low<<1);
677 }
678 
679 /*----------------------------------------------------------------------------
680 | Return whether the given value is an invalid floatx80 encoding.
681 | Invalid floatx80 encodings arise when the integer bit is not set, but
682 | the exponent is not zero. The only times the integer bit is permitted to
683 | be zero is in subnormal numbers and the value zero.
684 | This includes what the Intel software developer's manual calls pseudo-NaNs,
685 | pseudo-infinities and un-normal numbers. It does not include
686 | pseudo-denormals, which must still be correctly handled as inputs even
687 | if they are never generated as outputs.
688 *----------------------------------------------------------------------------*/
689 static inline bool floatx80_invalid_encoding(floatx80 a)
690 {
691     return (a.low & (1ULL << 63)) == 0 && (a.high & 0x7FFF) != 0;
692 }
693 
694 #define floatx80_zero make_floatx80(0x0000, 0x0000000000000000LL)
695 #define floatx80_one make_floatx80(0x3fff, 0x8000000000000000LL)
696 #define floatx80_ln2 make_floatx80(0x3ffe, 0xb17217f7d1cf79acLL)
697 #define floatx80_pi make_floatx80(0x4000, 0xc90fdaa22168c235LL)
698 #define floatx80_half make_floatx80(0x3ffe, 0x8000000000000000LL)
699 
700 /*----------------------------------------------------------------------------
701 | Returns the fraction bits of the extended double-precision floating-point
702 | value `a'.
703 *----------------------------------------------------------------------------*/
704 
705 static inline uint64_t extractFloatx80Frac(floatx80 a)
706 {
707     return a.low;
708 }
709 
710 /*----------------------------------------------------------------------------
711 | Returns the exponent bits of the extended double-precision floating-point
712 | value `a'.
713 *----------------------------------------------------------------------------*/
714 
715 static inline int32_t extractFloatx80Exp(floatx80 a)
716 {
717     return a.high & 0x7FFF;
718 }
719 
720 /*----------------------------------------------------------------------------
721 | Returns the sign bit of the extended double-precision floating-point value
722 | `a'.
723 *----------------------------------------------------------------------------*/
724 
725 static inline flag extractFloatx80Sign(floatx80 a)
726 {
727     return a.high >> 15;
728 }
729 
730 /*----------------------------------------------------------------------------
731 | Packs the sign `zSign', exponent `zExp', and significand `zSig' into an
732 | extended double-precision floating-point value, returning the result.
733 *----------------------------------------------------------------------------*/
734 
735 static inline floatx80 packFloatx80(flag zSign, int32_t zExp, uint64_t zSig)
736 {
737     floatx80 z;
738 
739     z.low = zSig;
740     z.high = (((uint16_t)zSign) << 15) + zExp;
741     return z;
742 }
743 
744 /*----------------------------------------------------------------------------
745 | Normalizes the subnormal extended double-precision floating-point value
746 | represented by the denormalized significand `aSig'.  The normalized exponent
747 | and significand are stored at the locations pointed to by `zExpPtr' and
748 | `zSigPtr', respectively.
749 *----------------------------------------------------------------------------*/
750 
751 void normalizeFloatx80Subnormal(uint64_t aSig, int32_t *zExpPtr,
752                                 uint64_t *zSigPtr);
753 
754 /*----------------------------------------------------------------------------
755 | Takes two extended double-precision floating-point values `a' and `b', one
756 | of which is a NaN, and returns the appropriate NaN result.  If either `a' or
757 | `b' is a signaling NaN, the invalid exception is raised.
758 *----------------------------------------------------------------------------*/
759 
760 floatx80 propagateFloatx80NaN(floatx80 a, floatx80 b, float_status *status);
761 
762 /*----------------------------------------------------------------------------
763 | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
764 | and extended significand formed by the concatenation of `zSig0' and `zSig1',
765 | and returns the proper extended double-precision floating-point value
766 | corresponding to the abstract input.  Ordinarily, the abstract value is
767 | rounded and packed into the extended double-precision format, with the
768 | inexact exception raised if the abstract input cannot be represented
769 | exactly.  However, if the abstract value is too large, the overflow and
770 | inexact exceptions are raised and an infinity or maximal finite value is
771 | returned.  If the abstract value is too small, the input value is rounded to
772 | a subnormal number, and the underflow and inexact exceptions are raised if
773 | the abstract input cannot be represented exactly as a subnormal extended
774 | double-precision floating-point number.
775 |     If `roundingPrecision' is 32 or 64, the result is rounded to the same
776 | number of bits as single or double precision, respectively.  Otherwise, the
777 | result is rounded to the full precision of the extended double-precision
778 | format.
779 |     The input significand must be normalized or smaller.  If the input
780 | significand is not normalized, `zExp' must be 0; in that case, the result
781 | returned is a subnormal number, and it must not require rounding.  The
782 | handling of underflow and overflow follows the IEC/IEEE Standard for Binary
783 | Floating-Point Arithmetic.
784 *----------------------------------------------------------------------------*/
785 
786 floatx80 roundAndPackFloatx80(int8_t roundingPrecision, flag zSign,
787                               int32_t zExp, uint64_t zSig0, uint64_t zSig1,
788                               float_status *status);
789 
790 /*----------------------------------------------------------------------------
791 | Takes an abstract floating-point value having sign `zSign', exponent
792 | `zExp', and significand formed by the concatenation of `zSig0' and `zSig1',
793 | and returns the proper extended double-precision floating-point value
794 | corresponding to the abstract input.  This routine is just like
795 | `roundAndPackFloatx80' except that the input significand does not have to be
796 | normalized.
797 *----------------------------------------------------------------------------*/
798 
799 floatx80 normalizeRoundAndPackFloatx80(int8_t roundingPrecision,
800                                        flag zSign, int32_t zExp,
801                                        uint64_t zSig0, uint64_t zSig1,
802                                        float_status *status);
803 
804 /*----------------------------------------------------------------------------
805 | The pattern for a default generated extended double-precision NaN.
806 *----------------------------------------------------------------------------*/
807 floatx80 floatx80_default_nan(float_status *status);
808 
809 /*----------------------------------------------------------------------------
810 | Software IEC/IEEE quadruple-precision conversion routines.
811 *----------------------------------------------------------------------------*/
812 int32_t float128_to_int32(float128, float_status *status);
813 int32_t float128_to_int32_round_to_zero(float128, float_status *status);
814 int64_t float128_to_int64(float128, float_status *status);
815 int64_t float128_to_int64_round_to_zero(float128, float_status *status);
816 uint64_t float128_to_uint64(float128, float_status *status);
817 uint64_t float128_to_uint64_round_to_zero(float128, float_status *status);
818 uint32_t float128_to_uint32(float128, float_status *status);
819 uint32_t float128_to_uint32_round_to_zero(float128, float_status *status);
820 float32 float128_to_float32(float128, float_status *status);
821 float64 float128_to_float64(float128, float_status *status);
822 floatx80 float128_to_floatx80(float128, float_status *status);
823 
824 /*----------------------------------------------------------------------------
825 | Software IEC/IEEE quadruple-precision operations.
826 *----------------------------------------------------------------------------*/
827 float128 float128_round_to_int(float128, float_status *status);
828 float128 float128_add(float128, float128, float_status *status);
829 float128 float128_sub(float128, float128, float_status *status);
830 float128 float128_mul(float128, float128, float_status *status);
831 float128 float128_div(float128, float128, float_status *status);
832 float128 float128_rem(float128, float128, float_status *status);
833 float128 float128_sqrt(float128, float_status *status);
834 int float128_eq(float128, float128, float_status *status);
835 int float128_le(float128, float128, float_status *status);
836 int float128_lt(float128, float128, float_status *status);
837 int float128_unordered(float128, float128, float_status *status);
838 int float128_eq_quiet(float128, float128, float_status *status);
839 int float128_le_quiet(float128, float128, float_status *status);
840 int float128_lt_quiet(float128, float128, float_status *status);
841 int float128_unordered_quiet(float128, float128, float_status *status);
842 int float128_compare(float128, float128, float_status *status);
843 int float128_compare_quiet(float128, float128, float_status *status);
844 int float128_is_quiet_nan(float128, float_status *status);
845 int float128_is_signaling_nan(float128, float_status *status);
846 float128 float128_silence_nan(float128, float_status *status);
847 float128 float128_scalbn(float128, int, float_status *status);
848 
849 static inline float128 float128_abs(float128 a)
850 {
851     a.high &= 0x7fffffffffffffffLL;
852     return a;
853 }
854 
855 static inline float128 float128_chs(float128 a)
856 {
857     a.high ^= 0x8000000000000000LL;
858     return a;
859 }
860 
861 static inline int float128_is_infinity(float128 a)
862 {
863     return (a.high & 0x7fffffffffffffffLL) == 0x7fff000000000000LL && a.low == 0;
864 }
865 
866 static inline int float128_is_neg(float128 a)
867 {
868     return a.high >> 63;
869 }
870 
871 static inline int float128_is_zero(float128 a)
872 {
873     return (a.high & 0x7fffffffffffffffLL) == 0 && a.low == 0;
874 }
875 
876 static inline int float128_is_zero_or_denormal(float128 a)
877 {
878     return (a.high & 0x7fff000000000000LL) == 0;
879 }
880 
881 static inline bool float128_is_normal(float128 a)
882 {
883     return (((a.high >> 48) + 1) & 0x7fff) >= 2;
884 }
885 
886 static inline bool float128_is_denormal(float128 a)
887 {
888     return float128_is_zero_or_denormal(a) && !float128_is_zero(a);
889 }
890 
891 static inline int float128_is_any_nan(float128 a)
892 {
893     return ((a.high >> 48) & 0x7fff) == 0x7fff &&
894         ((a.low != 0) || ((a.high & 0xffffffffffffLL) != 0));
895 }
896 
897 #define float128_zero make_float128(0, 0)
898 
899 /*----------------------------------------------------------------------------
900 | The pattern for a default generated quadruple-precision NaN.
901 *----------------------------------------------------------------------------*/
902 float128 float128_default_nan(float_status *status);
903 
904 #endif /* SOFTFLOAT_H */
905