]> git.proxmox.com Git - mirror_qemu.git/blame - include/fpu/softfloat.h
fpu: add mechanism to check for invalid long double formats
[mirror_qemu.git] / include / fpu / softfloat.h
CommitLineData
8d725fac
AF
1/*
2 * QEMU float support
3 *
16017c48
PM
4 * The code in this source file is derived from release 2a of the SoftFloat
5 * IEC/IEEE Floating-point Arithmetic Package. Those parts of the code (and
6 * some later contributions) are provided under that license, as detailed below.
7 * It has subsequently been modified by contributors to the QEMU Project,
8 * so some portions are provided under:
9 * the SoftFloat-2a license
10 * the BSD license
11 * GPL-v2-or-later
12 *
13 * Any future contributions to this file after December 1st 2014 will be
14 * taken to be licensed under the Softfloat-2a license unless specifically
15 * indicated otherwise.
8d725fac
AF
16 */
17
a7d1ac78
PM
18/*
19===============================================================================
20This C header file is part of the SoftFloat IEC/IEEE Floating-point
21Arithmetic Package, Release 2a.
158142c2
FB
22
23Written by John R. Hauser. This work was made possible in part by the
24International Computer Science Institute, located at Suite 600, 1947 Center
25Street, Berkeley, California 94704. Funding was partially provided by the
26National Science Foundation under grant MIP-9311980. The original version
27of this code was written as part of a project to build a fixed-point vector
28processor in collaboration with the University of California at Berkeley,
29overseen by Profs. Nelson Morgan and John Wawrzynek. More information
a7d1ac78 30is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
158142c2
FB
31arithmetic/SoftFloat.html'.
32
a7d1ac78
PM
33THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
34has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
35TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
36PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
37AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
158142c2
FB
38
39Derivative works are acceptable, even for commercial purposes, so long as
a7d1ac78
PM
40(1) they include prominent notice that the work is derivative, and (2) they
41include prominent notice akin to these four paragraphs for those parts of
42this code that are retained.
158142c2 43
a7d1ac78
PM
44===============================================================================
45*/
158142c2 46
16017c48
PM
47/* BSD licensing:
48 * Copyright (c) 2006, Fabrice Bellard
49 * All rights reserved.
50 *
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions are met:
53 *
54 * 1. Redistributions of source code must retain the above copyright notice,
55 * this list of conditions and the following disclaimer.
56 *
57 * 2. Redistributions in binary form must reproduce the above copyright notice,
58 * this list of conditions and the following disclaimer in the documentation
59 * and/or other materials provided with the distribution.
60 *
61 * 3. Neither the name of the copyright holder nor the names of its contributors
62 * may be used to endorse or promote products derived from this software without
63 * specific prior written permission.
64 *
65 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
66 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
69 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
70 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
71 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
72 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
73 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
74 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
75 * THE POSSIBILITY OF SUCH DAMAGE.
76 */
77
78/* Portions of this work are licensed under the terms of the GNU GPL,
79 * version 2 or later. See the COPYING file in the top-level directory.
80 */
81
158142c2
FB
82#ifndef SOFTFLOAT_H
83#define SOFTFLOAT_H
84
75b5a697 85#if defined(CONFIG_SOLARIS) && defined(CONFIG_NEEDS_LIBSUNMATH)
0475a5ca
TS
86#include <sunmath.h>
87#endif
88
158142c2 89
d341d9f3
PM
90/* This 'flag' type must be able to hold at least 0 and 1. It should
91 * probably be replaced with 'bool' but the uses would need to be audited
92 * to check that they weren't accidentally relying on it being a larger type.
93 */
750afe93 94typedef uint8_t flag;
158142c2 95
158142c2 96#define LIT64( a ) a##LL
158142c2 97
1d6bda35
FB
98/*----------------------------------------------------------------------------
99| Software IEC/IEEE floating-point ordering relations
100*----------------------------------------------------------------------------*/
101enum {
102 float_relation_less = -1,
103 float_relation_equal = 0,
104 float_relation_greater = 1,
105 float_relation_unordered = 2
106};
107
158142c2
FB
108/*----------------------------------------------------------------------------
109| Software IEC/IEEE floating-point types.
110*----------------------------------------------------------------------------*/
f090c9d4
PB
111/* Use structures for soft-float types. This prevents accidentally mixing
112 them with native int/float types. A sufficiently clever compiler and
113 sane ABI should be able to see though these structs. However
114 x86/gcc 3.x seems to struggle a bit, so leave them disabled by default. */
115//#define USE_SOFTFLOAT_STRUCT_TYPES
116#ifdef USE_SOFTFLOAT_STRUCT_TYPES
bb4d4bb3
PM
117typedef struct {
118 uint16_t v;
119} float16;
120#define float16_val(x) (((float16)(x)).v)
121#define make_float16(x) __extension__ ({ float16 f16_val = {x}; f16_val; })
d5138cf4 122#define const_float16(x) { x }
f090c9d4
PB
123typedef struct {
124 uint32_t v;
125} float32;
126/* The cast ensures an error if the wrong type is passed. */
127#define float32_val(x) (((float32)(x)).v)
128#define make_float32(x) __extension__ ({ float32 f32_val = {x}; f32_val; })
d5138cf4 129#define const_float32(x) { x }
f090c9d4
PB
130typedef struct {
131 uint64_t v;
132} float64;
133#define float64_val(x) (((float64)(x)).v)
134#define make_float64(x) __extension__ ({ float64 f64_val = {x}; f64_val; })
d5138cf4 135#define const_float64(x) { x }
f090c9d4 136#else
bb4d4bb3 137typedef uint16_t float16;
158142c2
FB
138typedef uint32_t float32;
139typedef uint64_t float64;
bb4d4bb3 140#define float16_val(x) (x)
f090c9d4
PB
141#define float32_val(x) (x)
142#define float64_val(x) (x)
bb4d4bb3 143#define make_float16(x) (x)
f090c9d4
PB
144#define make_float32(x) (x)
145#define make_float64(x) (x)
d5138cf4
PM
146#define const_float16(x) (x)
147#define const_float32(x) (x)
148#define const_float64(x) (x)
f090c9d4 149#endif
158142c2
FB
150typedef struct {
151 uint64_t low;
152 uint16_t high;
153} floatx80;
f3218a8d 154#define make_floatx80(exp, mant) ((floatx80) { mant, exp })
3bf7e40a 155#define make_floatx80_init(exp, mant) { .low = mant, .high = exp }
158142c2 156typedef struct {
e2542fe2 157#ifdef HOST_WORDS_BIGENDIAN
158142c2
FB
158 uint64_t high, low;
159#else
160 uint64_t low, high;
161#endif
162} float128;
789ec7ce 163#define make_float128(high_, low_) ((float128) { .high = high_, .low = low_ })
3bf7e40a 164#define make_float128_init(high_, low_) { .high = high_, .low = low_ }
158142c2
FB
165
166/*----------------------------------------------------------------------------
167| Software IEC/IEEE floating-point underflow tininess-detection mode.
168*----------------------------------------------------------------------------*/
169enum {
170 float_tininess_after_rounding = 0,
171 float_tininess_before_rounding = 1
172};
173
174/*----------------------------------------------------------------------------
175| Software IEC/IEEE floating-point rounding mode.
176*----------------------------------------------------------------------------*/
177enum {
178 float_round_nearest_even = 0,
179 float_round_down = 1,
180 float_round_up = 2,
f9288a76
PM
181 float_round_to_zero = 3,
182 float_round_ties_away = 4,
158142c2
FB
183};
184
185/*----------------------------------------------------------------------------
186| Software IEC/IEEE floating-point exception flags.
187*----------------------------------------------------------------------------*/
188enum {
189 float_flag_invalid = 1,
190 float_flag_divbyzero = 4,
191 float_flag_overflow = 8,
192 float_flag_underflow = 16,
37d18660 193 float_flag_inexact = 32,
e6afc87f
PM
194 float_flag_input_denormal = 64,
195 float_flag_output_denormal = 128
158142c2
FB
196};
197
198typedef struct float_status {
199 signed char float_detect_tininess;
200 signed char float_rounding_mode;
dfd60767 201 uint8_t float_exception_flags;
158142c2 202 signed char floatx80_rounding_precision;
37d18660 203 /* should denormalised results go to zero and set the inexact flag? */
fe76d976 204 flag flush_to_zero;
37d18660
PM
205 /* should denormalised inputs go to zero and set the input_denormal flag? */
206 flag flush_inputs_to_zero;
5c7908ed 207 flag default_nan_mode;
af39bc8c 208 flag snan_bit_is_one;
158142c2
FB
209} float_status;
210
e5a41ffa 211static inline void set_float_detect_tininess(int val, float_status *status)
c29aca44 212{
a2f2d288 213 status->float_detect_tininess = val;
c29aca44 214}
e5a41ffa 215static inline void set_float_rounding_mode(int val, float_status *status)
879d096b 216{
a2f2d288 217 status->float_rounding_mode = val;
879d096b 218}
e5a41ffa 219static inline void set_float_exception_flags(int val, float_status *status)
879d096b 220{
a2f2d288 221 status->float_exception_flags = val;
879d096b 222}
e5a41ffa
PM
223static inline void set_floatx80_rounding_precision(int val,
224 float_status *status)
879d096b 225{
a2f2d288 226 status->floatx80_rounding_precision = val;
879d096b 227}
e5a41ffa 228static inline void set_flush_to_zero(flag val, float_status *status)
fe76d976 229{
a2f2d288 230 status->flush_to_zero = val;
fe76d976 231}
e5a41ffa 232static inline void set_flush_inputs_to_zero(flag val, float_status *status)
37d18660 233{
a2f2d288 234 status->flush_inputs_to_zero = val;
37d18660 235}
e5a41ffa 236static inline void set_default_nan_mode(flag val, float_status *status)
5c7908ed 237{
a2f2d288 238 status->default_nan_mode = val;
5c7908ed 239}
af39bc8c
AM
240static inline void set_snan_bit_is_one(flag val, float_status *status)
241{
242 status->snan_bit_is_one = val;
243}
a49db98d 244static inline int get_float_detect_tininess(float_status *status)
879d096b 245{
a2f2d288 246 return status->float_detect_tininess;
879d096b 247}
a49db98d 248static inline int get_float_rounding_mode(float_status *status)
879d096b 249{
a2f2d288 250 return status->float_rounding_mode;
879d096b 251}
a49db98d 252static inline int get_float_exception_flags(float_status *status)
1d6bda35 253{
a2f2d288 254 return status->float_exception_flags;
1d6bda35 255}
a49db98d 256static inline int get_floatx80_rounding_precision(float_status *status)
879d096b 257{
a2f2d288 258 return status->floatx80_rounding_precision;
879d096b 259}
a49db98d 260static inline flag get_flush_to_zero(float_status *status)
879d096b 261{
a2f2d288 262 return status->flush_to_zero;
879d096b 263}
a49db98d 264static inline flag get_flush_inputs_to_zero(float_status *status)
879d096b 265{
a2f2d288 266 return status->flush_inputs_to_zero;
879d096b 267}
a49db98d 268static inline flag get_default_nan_mode(float_status *status)
879d096b 269{
a2f2d288 270 return status->default_nan_mode;
879d096b 271}
158142c2
FB
272
273/*----------------------------------------------------------------------------
274| Routine to raise any or all of the software IEC/IEEE floating-point
275| exception flags.
276*----------------------------------------------------------------------------*/
dfd60767 277void float_raise(uint8_t flags, float_status *status);
158142c2 278
7baeabce
AB
279/*----------------------------------------------------------------------------
280| If `a' is denormal and we are in flush-to-zero mode then set the
281| input-denormal exception and return zero. Otherwise just return the value.
282*----------------------------------------------------------------------------*/
e5a41ffa
PM
283float32 float32_squash_input_denormal(float32 a, float_status *status);
284float64 float64_squash_input_denormal(float64 a, float_status *status);
7baeabce 285
369be8f6
PM
286/*----------------------------------------------------------------------------
287| Options to indicate which negations to perform in float*_muladd()
288| Using these differs from negating an input or output before calling
289| the muladd function in that this means that a NaN doesn't have its
290| sign bit inverted before it is propagated.
67d43538
PM
291| We also support halving the result before rounding, as a special
292| case to support the ARM fused-sqrt-step instruction FRSQRTS.
369be8f6
PM
293*----------------------------------------------------------------------------*/
294enum {
295 float_muladd_negate_c = 1,
296 float_muladd_negate_product = 2,
66176802 297 float_muladd_negate_result = 4,
67d43538 298 float_muladd_halve_result = 8,
369be8f6
PM
299};
300
158142c2
FB
301/*----------------------------------------------------------------------------
302| Software IEC/IEEE integer-to-floating-point conversion routines.
303*----------------------------------------------------------------------------*/
e5a41ffa
PM
304float32 int32_to_float32(int32_t, float_status *status);
305float64 int32_to_float64(int32_t, float_status *status);
306float32 uint32_to_float32(uint32_t, float_status *status);
307float64 uint32_to_float64(uint32_t, float_status *status);
308floatx80 int32_to_floatx80(int32_t, float_status *status);
309float128 int32_to_float128(int32_t, float_status *status);
310float32 int64_to_float32(int64_t, float_status *status);
311float64 int64_to_float64(int64_t, float_status *status);
312floatx80 int64_to_floatx80(int64_t, float_status *status);
313float128 int64_to_float128(int64_t, float_status *status);
314float32 uint64_to_float32(uint64_t, float_status *status);
315float64 uint64_to_float64(uint64_t, float_status *status);
316float128 uint64_to_float128(uint64_t, float_status *status);
158142c2 317
8afbdaba 318/* We provide the int16 versions for symmetry of API with float-to-int */
e5a41ffa 319static inline float32 int16_to_float32(int16_t v, float_status *status)
8afbdaba 320{
ff32e16e 321 return int32_to_float32(v, status);
8afbdaba
PM
322}
323
e5a41ffa 324static inline float32 uint16_to_float32(uint16_t v, float_status *status)
8afbdaba 325{
ff32e16e 326 return uint32_to_float32(v, status);
8afbdaba
PM
327}
328
e5a41ffa 329static inline float64 int16_to_float64(int16_t v, float_status *status)
8afbdaba 330{
ff32e16e 331 return int32_to_float64(v, status);
8afbdaba
PM
332}
333
e5a41ffa 334static inline float64 uint16_to_float64(uint16_t v, float_status *status)
8afbdaba 335{
ff32e16e 336 return uint32_to_float64(v, status);
8afbdaba
PM
337}
338
60011498
PB
339/*----------------------------------------------------------------------------
340| Software half-precision conversion routines.
341*----------------------------------------------------------------------------*/
e5a41ffa
PM
342float16 float32_to_float16(float32, flag, float_status *status);
343float32 float16_to_float32(float16, flag, float_status *status);
344float16 float64_to_float16(float64 a, flag ieee, float_status *status);
345float64 float16_to_float64(float16 a, flag ieee, float_status *status);
bb4d4bb3
PM
346
347/*----------------------------------------------------------------------------
348| Software half-precision operations.
349*----------------------------------------------------------------------------*/
af39bc8c
AM
350int float16_is_quiet_nan(float16, float_status *status);
351int float16_is_signaling_nan(float16, float_status *status);
352float16 float16_maybe_silence_nan(float16, float_status *status);
60011498 353
a49db98d 354static inline int float16_is_any_nan(float16 a)
213ff4e6
MF
355{
356 return ((float16_val(a) & ~0x8000) > 0x7c00);
357}
358
8559666d
CL
359/*----------------------------------------------------------------------------
360| The pattern for a default generated half-precision NaN.
361*----------------------------------------------------------------------------*/
af39bc8c 362float16 float16_default_nan(float_status *status);
8559666d 363
158142c2
FB
364/*----------------------------------------------------------------------------
365| Software IEC/IEEE single-precision conversion routines.
366*----------------------------------------------------------------------------*/
0bb721d7
PM
367int16_t float32_to_int16(float32, float_status *status);
368uint16_t float32_to_uint16(float32, float_status *status);
369int16_t float32_to_int16_round_to_zero(float32, float_status *status);
370uint16_t float32_to_uint16_round_to_zero(float32, float_status *status);
f4014512
PM
371int32_t float32_to_int32(float32, float_status *status);
372int32_t float32_to_int32_round_to_zero(float32, float_status *status);
3a87d009
PM
373uint32_t float32_to_uint32(float32, float_status *status);
374uint32_t float32_to_uint32_round_to_zero(float32, float_status *status);
f42c2224 375int64_t float32_to_int64(float32, float_status *status);
182f42fd
PM
376uint64_t float32_to_uint64(float32, float_status *status);
377uint64_t float32_to_uint64_round_to_zero(float32, float_status *status);
f42c2224 378int64_t float32_to_int64_round_to_zero(float32, float_status *status);
e5a41ffa
PM
379float64 float32_to_float64(float32, float_status *status);
380floatx80 float32_to_floatx80(float32, float_status *status);
381float128 float32_to_float128(float32, float_status *status);
158142c2
FB
382
383/*----------------------------------------------------------------------------
384| Software IEC/IEEE single-precision operations.
385*----------------------------------------------------------------------------*/
e5a41ffa
PM
386float32 float32_round_to_int(float32, float_status *status);
387float32 float32_add(float32, float32, float_status *status);
388float32 float32_sub(float32, float32, float_status *status);
389float32 float32_mul(float32, float32, float_status *status);
390float32 float32_div(float32, float32, float_status *status);
391float32 float32_rem(float32, float32, float_status *status);
392float32 float32_muladd(float32, float32, float32, int, float_status *status);
393float32 float32_sqrt(float32, float_status *status);
394float32 float32_exp2(float32, float_status *status);
395float32 float32_log2(float32, float_status *status);
396int float32_eq(float32, float32, float_status *status);
397int float32_le(float32, float32, float_status *status);
398int float32_lt(float32, float32, float_status *status);
399int float32_unordered(float32, float32, float_status *status);
400int float32_eq_quiet(float32, float32, float_status *status);
401int float32_le_quiet(float32, float32, float_status *status);
402int float32_lt_quiet(float32, float32, float_status *status);
403int float32_unordered_quiet(float32, float32, float_status *status);
404int float32_compare(float32, float32, float_status *status);
405int float32_compare_quiet(float32, float32, float_status *status);
406float32 float32_min(float32, float32, float_status *status);
407float32 float32_max(float32, float32, float_status *status);
408float32 float32_minnum(float32, float32, float_status *status);
409float32 float32_maxnum(float32, float32, float_status *status);
410float32 float32_minnummag(float32, float32, float_status *status);
411float32 float32_maxnummag(float32, float32, float_status *status);
af39bc8c
AM
412int float32_is_quiet_nan(float32, float_status *status);
413int float32_is_signaling_nan(float32, float_status *status);
414float32 float32_maybe_silence_nan(float32, float_status *status);
e5a41ffa 415float32 float32_scalbn(float32, int, float_status *status);
158142c2 416
a49db98d 417static inline float32 float32_abs(float32 a)
1d6bda35 418{
37d18660
PM
419 /* Note that abs does *not* handle NaN specially, nor does
420 * it flush denormal inputs to zero.
421 */
f090c9d4 422 return make_float32(float32_val(a) & 0x7fffffff);
1d6bda35
FB
423}
424
a49db98d 425static inline float32 float32_chs(float32 a)
1d6bda35 426{
37d18660
PM
427 /* Note that chs does *not* handle NaN specially, nor does
428 * it flush denormal inputs to zero.
429 */
f090c9d4 430 return make_float32(float32_val(a) ^ 0x80000000);
1d6bda35
FB
431}
432
a49db98d 433static inline int float32_is_infinity(float32 a)
c52ab6f5 434{
dadd71a7 435 return (float32_val(a) & 0x7fffffff) == 0x7f800000;
c52ab6f5
AJ
436}
437
a49db98d 438static inline int float32_is_neg(float32 a)
c52ab6f5
AJ
439{
440 return float32_val(a) >> 31;
441}
442
a49db98d 443static inline int float32_is_zero(float32 a)
c52ab6f5
AJ
444{
445 return (float32_val(a) & 0x7fffffff) == 0;
446}
447
a49db98d 448static inline int float32_is_any_nan(float32 a)
21d6ebde
PM
449{
450 return ((float32_val(a) & ~(1 << 31)) > 0x7f800000UL);
451}
452
a49db98d 453static inline int float32_is_zero_or_denormal(float32 a)
6f3300ad
PM
454{
455 return (float32_val(a) & 0x7f800000) == 0;
456}
457
a49db98d 458static inline float32 float32_set_sign(float32 a, int sign)
c30fe7df
CL
459{
460 return make_float32((float32_val(a) & 0x7fffffff) | (sign << 31));
461}
462
f090c9d4 463#define float32_zero make_float32(0)
196cfc89 464#define float32_one make_float32(0x3f800000)
8229c991 465#define float32_ln2 make_float32(0x3f317218)
c4b4c77a 466#define float32_pi make_float32(0x40490fdb)
c30fe7df
CL
467#define float32_half make_float32(0x3f000000)
468#define float32_infinity make_float32(0x7f800000)
f090c9d4 469
8559666d
CL
470
471/*----------------------------------------------------------------------------
472| The pattern for a default generated single-precision NaN.
473*----------------------------------------------------------------------------*/
af39bc8c 474float32 float32_default_nan(float_status *status);
8559666d 475
158142c2
FB
476/*----------------------------------------------------------------------------
477| Software IEC/IEEE double-precision conversion routines.
478*----------------------------------------------------------------------------*/
0bb721d7
PM
479int16_t float64_to_int16(float64, float_status *status);
480uint16_t float64_to_uint16(float64, float_status *status);
481int16_t float64_to_int16_round_to_zero(float64, float_status *status);
482uint16_t float64_to_uint16_round_to_zero(float64, float_status *status);
f4014512
PM
483int32_t float64_to_int32(float64, float_status *status);
484int32_t float64_to_int32_round_to_zero(float64, float_status *status);
3a87d009
PM
485uint32_t float64_to_uint32(float64, float_status *status);
486uint32_t float64_to_uint32_round_to_zero(float64, float_status *status);
f42c2224
PM
487int64_t float64_to_int64(float64, float_status *status);
488int64_t float64_to_int64_round_to_zero(float64, float_status *status);
182f42fd
PM
489uint64_t float64_to_uint64(float64 a, float_status *status);
490uint64_t float64_to_uint64_round_to_zero(float64 a, float_status *status);
e5a41ffa
PM
491float32 float64_to_float32(float64, float_status *status);
492floatx80 float64_to_floatx80(float64, float_status *status);
493float128 float64_to_float128(float64, float_status *status);
158142c2
FB
494
495/*----------------------------------------------------------------------------
496| Software IEC/IEEE double-precision operations.
497*----------------------------------------------------------------------------*/
e5a41ffa
PM
498float64 float64_round_to_int(float64, float_status *status);
499float64 float64_trunc_to_int(float64, float_status *status);
500float64 float64_add(float64, float64, float_status *status);
501float64 float64_sub(float64, float64, float_status *status);
502float64 float64_mul(float64, float64, float_status *status);
503float64 float64_div(float64, float64, float_status *status);
504float64 float64_rem(float64, float64, float_status *status);
505float64 float64_muladd(float64, float64, float64, int, float_status *status);
506float64 float64_sqrt(float64, float_status *status);
507float64 float64_log2(float64, float_status *status);
508int float64_eq(float64, float64, float_status *status);
509int float64_le(float64, float64, float_status *status);
510int float64_lt(float64, float64, float_status *status);
511int float64_unordered(float64, float64, float_status *status);
512int float64_eq_quiet(float64, float64, float_status *status);
513int float64_le_quiet(float64, float64, float_status *status);
514int float64_lt_quiet(float64, float64, float_status *status);
515int float64_unordered_quiet(float64, float64, float_status *status);
516int float64_compare(float64, float64, float_status *status);
517int float64_compare_quiet(float64, float64, float_status *status);
518float64 float64_min(float64, float64, float_status *status);
519float64 float64_max(float64, float64, float_status *status);
520float64 float64_minnum(float64, float64, float_status *status);
521float64 float64_maxnum(float64, float64, float_status *status);
522float64 float64_minnummag(float64, float64, float_status *status);
523float64 float64_maxnummag(float64, float64, float_status *status);
af39bc8c
AM
524int float64_is_quiet_nan(float64 a, float_status *status);
525int float64_is_signaling_nan(float64, float_status *status);
526float64 float64_maybe_silence_nan(float64, float_status *status);
e5a41ffa 527float64 float64_scalbn(float64, int, float_status *status);
158142c2 528
a49db98d 529static inline float64 float64_abs(float64 a)
1d6bda35 530{
37d18660
PM
531 /* Note that abs does *not* handle NaN specially, nor does
532 * it flush denormal inputs to zero.
533 */
f090c9d4 534 return make_float64(float64_val(a) & 0x7fffffffffffffffLL);
1d6bda35
FB
535}
536
a49db98d 537static inline float64 float64_chs(float64 a)
1d6bda35 538{
37d18660
PM
539 /* Note that chs does *not* handle NaN specially, nor does
540 * it flush denormal inputs to zero.
541 */
f090c9d4 542 return make_float64(float64_val(a) ^ 0x8000000000000000LL);
1d6bda35
FB
543}
544
a49db98d 545static inline int float64_is_infinity(float64 a)
c52ab6f5
AJ
546{
547 return (float64_val(a) & 0x7fffffffffffffffLL ) == 0x7ff0000000000000LL;
548}
549
a49db98d 550static inline int float64_is_neg(float64 a)
c52ab6f5
AJ
551{
552 return float64_val(a) >> 63;
553}
554
a49db98d 555static inline int float64_is_zero(float64 a)
c52ab6f5
AJ
556{
557 return (float64_val(a) & 0x7fffffffffffffffLL) == 0;
558}
559
a49db98d 560static inline int float64_is_any_nan(float64 a)
21d6ebde
PM
561{
562 return ((float64_val(a) & ~(1ULL << 63)) > 0x7ff0000000000000ULL);
563}
564
a49db98d 565static inline int float64_is_zero_or_denormal(float64 a)
587eabfa
AJ
566{
567 return (float64_val(a) & 0x7ff0000000000000LL) == 0;
568}
569
a49db98d 570static inline float64 float64_set_sign(float64 a, int sign)
c30fe7df
CL
571{
572 return make_float64((float64_val(a) & 0x7fffffffffffffffULL)
573 | ((int64_t)sign << 63));
574}
575
f090c9d4 576#define float64_zero make_float64(0)
196cfc89 577#define float64_one make_float64(0x3ff0000000000000LL)
8229c991 578#define float64_ln2 make_float64(0x3fe62e42fefa39efLL)
c4b4c77a 579#define float64_pi make_float64(0x400921fb54442d18LL)
c30fe7df
CL
580#define float64_half make_float64(0x3fe0000000000000LL)
581#define float64_infinity make_float64(0x7ff0000000000000LL)
f090c9d4 582
8559666d
CL
583/*----------------------------------------------------------------------------
584| The pattern for a default generated double-precision NaN.
585*----------------------------------------------------------------------------*/
af39bc8c 586float64 float64_default_nan(float_status *status);
8559666d 587
158142c2
FB
588/*----------------------------------------------------------------------------
589| Software IEC/IEEE extended double-precision conversion routines.
590*----------------------------------------------------------------------------*/
f4014512
PM
591int32_t floatx80_to_int32(floatx80, float_status *status);
592int32_t floatx80_to_int32_round_to_zero(floatx80, float_status *status);
f42c2224
PM
593int64_t floatx80_to_int64(floatx80, float_status *status);
594int64_t floatx80_to_int64_round_to_zero(floatx80, float_status *status);
e5a41ffa
PM
595float32 floatx80_to_float32(floatx80, float_status *status);
596float64 floatx80_to_float64(floatx80, float_status *status);
597float128 floatx80_to_float128(floatx80, float_status *status);
158142c2
FB
598
599/*----------------------------------------------------------------------------
600| Software IEC/IEEE extended double-precision operations.
601*----------------------------------------------------------------------------*/
e5a41ffa
PM
602floatx80 floatx80_round_to_int(floatx80, float_status *status);
603floatx80 floatx80_add(floatx80, floatx80, float_status *status);
604floatx80 floatx80_sub(floatx80, floatx80, float_status *status);
605floatx80 floatx80_mul(floatx80, floatx80, float_status *status);
606floatx80 floatx80_div(floatx80, floatx80, float_status *status);
607floatx80 floatx80_rem(floatx80, floatx80, float_status *status);
608floatx80 floatx80_sqrt(floatx80, float_status *status);
609int floatx80_eq(floatx80, floatx80, float_status *status);
610int floatx80_le(floatx80, floatx80, float_status *status);
611int floatx80_lt(floatx80, floatx80, float_status *status);
612int floatx80_unordered(floatx80, floatx80, float_status *status);
613int floatx80_eq_quiet(floatx80, floatx80, float_status *status);
614int floatx80_le_quiet(floatx80, floatx80, float_status *status);
615int floatx80_lt_quiet(floatx80, floatx80, float_status *status);
616int floatx80_unordered_quiet(floatx80, floatx80, float_status *status);
617int floatx80_compare(floatx80, floatx80, float_status *status);
618int floatx80_compare_quiet(floatx80, floatx80, float_status *status);
af39bc8c
AM
619int floatx80_is_quiet_nan(floatx80, float_status *status);
620int floatx80_is_signaling_nan(floatx80, float_status *status);
621floatx80 floatx80_maybe_silence_nan(floatx80, float_status *status);
e5a41ffa 622floatx80 floatx80_scalbn(floatx80, int, float_status *status);
158142c2 623
a49db98d 624static inline floatx80 floatx80_abs(floatx80 a)
1d6bda35
FB
625{
626 a.high &= 0x7fff;
627 return a;
628}
629
a49db98d 630static inline floatx80 floatx80_chs(floatx80 a)
1d6bda35
FB
631{
632 a.high ^= 0x8000;
633 return a;
634}
635
a49db98d 636static inline int floatx80_is_infinity(floatx80 a)
c52ab6f5 637{
b76235e4 638 return (a.high & 0x7fff) == 0x7fff && a.low == 0x8000000000000000LL;
c52ab6f5
AJ
639}
640
a49db98d 641static inline int floatx80_is_neg(floatx80 a)
c52ab6f5
AJ
642{
643 return a.high >> 15;
644}
645
a49db98d 646static inline int floatx80_is_zero(floatx80 a)
c52ab6f5
AJ
647{
648 return (a.high & 0x7fff) == 0 && a.low == 0;
649}
650
a49db98d 651static inline int floatx80_is_zero_or_denormal(floatx80 a)
587eabfa
AJ
652{
653 return (a.high & 0x7fff) == 0;
654}
655
a49db98d 656static inline int floatx80_is_any_nan(floatx80 a)
2bed652f
PM
657{
658 return ((a.high & 0x7fff) == 0x7fff) && (a.low<<1);
659}
660
d1eb8f2a
AD
661/*----------------------------------------------------------------------------
662| Return whether the given value is an invalid floatx80 encoding.
663| Invalid floatx80 encodings arise when the integer bit is not set, but
664| the exponent is not zero. The only times the integer bit is permitted to
665| be zero is in subnormal numbers and the value zero.
666| This includes what the Intel software developer's manual calls pseudo-NaNs,
667| pseudo-infinities and un-normal numbers. It does not include
668| pseudo-denormals, which must still be correctly handled as inputs even
669| if they are never generated as outputs.
670*----------------------------------------------------------------------------*/
671static inline bool floatx80_invalid_encoding(floatx80 a)
672{
673 return (a.low & (1ULL << 63)) == 0 && (a.high & 0x7FFF) != 0;
674}
675
f3218a8d
AJ
676#define floatx80_zero make_floatx80(0x0000, 0x0000000000000000LL)
677#define floatx80_one make_floatx80(0x3fff, 0x8000000000000000LL)
678#define floatx80_ln2 make_floatx80(0x3ffe, 0xb17217f7d1cf79acLL)
c4b4c77a 679#define floatx80_pi make_floatx80(0x4000, 0xc90fdaa22168c235LL)
f3218a8d
AJ
680#define floatx80_half make_floatx80(0x3ffe, 0x8000000000000000LL)
681#define floatx80_infinity make_floatx80(0x7fff, 0x8000000000000000LL)
682
8559666d 683/*----------------------------------------------------------------------------
789ec7ce 684| The pattern for a default generated extended double-precision NaN.
8559666d 685*----------------------------------------------------------------------------*/
af39bc8c 686floatx80 floatx80_default_nan(float_status *status);
8559666d 687
158142c2
FB
688/*----------------------------------------------------------------------------
689| Software IEC/IEEE quadruple-precision conversion routines.
690*----------------------------------------------------------------------------*/
f4014512
PM
691int32_t float128_to_int32(float128, float_status *status);
692int32_t float128_to_int32_round_to_zero(float128, float_status *status);
f42c2224
PM
693int64_t float128_to_int64(float128, float_status *status);
694int64_t float128_to_int64_round_to_zero(float128, float_status *status);
e5a41ffa
PM
695float32 float128_to_float32(float128, float_status *status);
696float64 float128_to_float64(float128, float_status *status);
697floatx80 float128_to_floatx80(float128, float_status *status);
158142c2
FB
698
699/*----------------------------------------------------------------------------
700| Software IEC/IEEE quadruple-precision operations.
701*----------------------------------------------------------------------------*/
e5a41ffa
PM
702float128 float128_round_to_int(float128, float_status *status);
703float128 float128_add(float128, float128, float_status *status);
704float128 float128_sub(float128, float128, float_status *status);
705float128 float128_mul(float128, float128, float_status *status);
706float128 float128_div(float128, float128, float_status *status);
707float128 float128_rem(float128, float128, float_status *status);
708float128 float128_sqrt(float128, float_status *status);
709int float128_eq(float128, float128, float_status *status);
710int float128_le(float128, float128, float_status *status);
711int float128_lt(float128, float128, float_status *status);
712int float128_unordered(float128, float128, float_status *status);
713int float128_eq_quiet(float128, float128, float_status *status);
714int float128_le_quiet(float128, float128, float_status *status);
715int float128_lt_quiet(float128, float128, float_status *status);
716int float128_unordered_quiet(float128, float128, float_status *status);
717int float128_compare(float128, float128, float_status *status);
718int float128_compare_quiet(float128, float128, float_status *status);
af39bc8c
AM
719int float128_is_quiet_nan(float128, float_status *status);
720int float128_is_signaling_nan(float128, float_status *status);
721float128 float128_maybe_silence_nan(float128, float_status *status);
e5a41ffa 722float128 float128_scalbn(float128, int, float_status *status);
158142c2 723
a49db98d 724static inline float128 float128_abs(float128 a)
1d6bda35
FB
725{
726 a.high &= 0x7fffffffffffffffLL;
727 return a;
728}
729
a49db98d 730static inline float128 float128_chs(float128 a)
1d6bda35
FB
731{
732 a.high ^= 0x8000000000000000LL;
733 return a;
734}
735
a49db98d 736static inline int float128_is_infinity(float128 a)
c52ab6f5
AJ
737{
738 return (a.high & 0x7fffffffffffffffLL) == 0x7fff000000000000LL && a.low == 0;
739}
740
a49db98d 741static inline int float128_is_neg(float128 a)
c52ab6f5
AJ
742{
743 return a.high >> 63;
744}
745
a49db98d 746static inline int float128_is_zero(float128 a)
c52ab6f5
AJ
747{
748 return (a.high & 0x7fffffffffffffffLL) == 0 && a.low == 0;
749}
750
a49db98d 751static inline int float128_is_zero_or_denormal(float128 a)
587eabfa
AJ
752{
753 return (a.high & 0x7fff000000000000LL) == 0;
754}
755
a49db98d 756static inline int float128_is_any_nan(float128 a)
2bed652f
PM
757{
758 return ((a.high >> 48) & 0x7fff) == 0x7fff &&
759 ((a.low != 0) || ((a.high & 0xffffffffffffLL) != 0));
760}
761
1e397ead
RH
762#define float128_zero make_float128(0, 0)
763
8559666d 764/*----------------------------------------------------------------------------
789ec7ce 765| The pattern for a default generated quadruple-precision NaN.
8559666d 766*----------------------------------------------------------------------------*/
af39bc8c 767float128 float128_default_nan(float_status *status);
8559666d 768
175de524 769#endif /* SOFTFLOAT_H */