4 * This contains a series of useful utility routines and a set of
5 * floating point constants useful for exercising the edge cases in
6 * floating point tests.
8 * Copyright (c) 2019 Linaro
10 * SPDX-License-Identifier: GPL-3.0-or-later
13 /* we want additional float type definitions */
14 #define __STDC_WANT_IEC_60559_BFP_EXT__
15 #define __STDC_WANT_IEC_60559_TYPES_EXT__
25 #include "float_helpers.h"
27 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
30 * Half Precision Numbers
32 * Not yet well standardised so we return a plain uint16_t for now.
35 /* no handy defines for these numbers */
36 static uint16_t f16_numbers
[] = {
37 0xffff, /* -NaN / AHP -Max */
38 0xfcff, /* -NaN / AHP */
39 0xfc01, /* -NaN / AHP */
44 0x8001, /* -MIN subnormal */
47 0x0001, /* MIN subnormal */
51 0x7c01, /* NaN / AHP */
52 0x7cff, /* NaN / AHP */
53 0x7fff, /* NaN / AHP +Max*/
56 static const int num_f16
= ARRAY_SIZE(f16_numbers
);
63 uint16_t get_f16(int i
)
65 return f16_numbers
[i
% num_f16
];
68 /* only display as hex */
69 char *fmt_16(uint16_t num
)
72 asprintf(&fmt
, "f16(%#04x)", num
);
77 * Single Precision Numbers
81 /* Signaling NaN macros, if supported. */
82 # if __GNUC_PREREQ(3, 3)
83 # define SNANF (__builtin_nansf (""))
84 # define SNAN (__builtin_nans (""))
85 # define SNANL (__builtin_nansl (""))
89 static float f32_numbers
[] = {
102 0x1.ffffe6p
-25, /* min positive FP16 subnormal */
103 0x1.ff801ap
-15, /* max subnormal FP16 */
104 0x1.00000cp
-14, /* min positive normal FP16 */
106 0x1.004p
+0, /* smallest float after 1.0 FP16 */
110 0x1.ffcp
+15, /* max FP16 */
113 0x1.ffcp
+16, /* max AFP */
122 static const int num_f32
= ARRAY_SIZE(f32_numbers
);
124 int get_num_f32(void)
131 return f32_numbers
[i
% num_f32
];
134 char *fmt_f32(float num
)
136 uint32_t single_as_hex
= *(uint32_t *) &num
;
138 asprintf(&fmt
, "f32(%02.20a:%#010x)", num
, single_as_hex
);
143 /* This allows us to initialise some doubles as pure hex */
149 static test_doubles f64_numbers
[] = {
157 {-1.111E+30}, /* half prec */
164 {5.96046E-8}, /* min positive FP16 subnormal */
165 {6.09756E-5}, /* max subnormal FP16 */
166 {6.10352E-5}, /* min positive normal FP16 */
168 {1.0009765625}, /* smallest float after 1.0 FP16 */
170 {1.3789972848607228e-308},
171 {1.4914738736681624e-308},
175 {65504.0}, /* max FP16 */
178 {131008.0}, /* max AFP */
180 {.h
= 0x41dfffffffc00000 }, /* to int = 0x7fffffff */
186 {.h
= 0x7ff0000000000001}, /* SNAN */
190 static const int num_f64
= ARRAY_SIZE(f64_numbers
);
192 int get_num_f64(void)
197 double get_f64(int i
)
199 return f64_numbers
[i
% num_f64
].d
;
202 char *fmt_f64(double num
)
204 uint64_t double_as_hex
= *(uint64_t *) &num
;
206 asprintf(&fmt
, "f64(%02.20a:%#020" PRIx64
")", num
, double_as_hex
);
213 char *fmt_flags(void)
215 int flags
= fetestexcept(FE_ALL_EXCEPT
);
219 asprintf(&fmt
, "%s%s%s%s%s",
220 flags
& FE_OVERFLOW
? "OVERFLOW " : "",
221 flags
& FE_UNDERFLOW
? "UNDERFLOW " : "",
222 flags
& FE_DIVBYZERO
? "DIV0 " : "",
223 flags
& FE_INEXACT
? "INEXACT " : "",
224 flags
& FE_INVALID
? "INVALID" : "");
226 asprintf(&fmt
, "OK");