qapi: Improve reporting of redefinition
[qemu/armbru.git] / tests / tcg / multiarch / float_helpers.c
blob8ee7903c785530618aeddacf87c7f4de79e87d0b
1 /*
2 * Common Float Helpers
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__
17 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <inttypes.h>
21 #include <math.h>
22 #include <float.h>
23 #include <fenv.h>
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 */
40 0xfc00, /* -Inf */
41 0xfbff, /* -Max */
42 0xc000, /* -2 */
43 0xbc00, /* -1 */
44 0x8001, /* -MIN subnormal */
45 0x8000, /* -0 */
46 0x0000, /* +0 */
47 0x0001, /* MIN subnormal */
48 0x3c00, /* 1 */
49 0x7bff, /* Max */
50 0x7c00, /* Inf */
51 0x7c01, /* NaN / AHP */
52 0x7cff, /* NaN / AHP */
53 0x7fff, /* NaN / AHP +Max*/
56 static const int num_f16 = ARRAY_SIZE(f16_numbers);
58 int get_num_f16(void)
60 return num_f16;
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)
71 char *fmt;
72 asprintf(&fmt, "f16(%#04x)", num);
73 return fmt;
77 * Single Precision Numbers
80 #ifndef SNANF
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 (""))
86 # endif
87 #endif
89 static float f32_numbers[] = {
90 -SNANF,
91 -NAN,
92 -INFINITY,
93 -FLT_MAX,
94 -0x1.1874b2p+103,
95 -0x1.c0bab6p+99,
96 -0x1.31f75p-40,
97 -0x1.505444p-66,
98 -FLT_MIN,
99 0.0,
100 FLT_MIN,
101 0x1p-25,
102 0x1.ffffe6p-25, /* min positive FP16 subnormal */
103 0x1.ff801ap-15, /* max subnormal FP16 */
104 0x1.00000cp-14, /* min positive normal FP16 */
105 1.0,
106 0x1.004p+0, /* smallest float after 1.0 FP16 */
107 2.0,
108 M_E, M_PI,
109 0x1.ffbep+15,
110 0x1.ffcp+15, /* max FP16 */
111 0x1.ffc2p+15,
112 0x1.ffbfp+16,
113 0x1.ffcp+16, /* max AFP */
114 0x1.ffc1p+16,
115 0x1.c0bab6p+99,
116 FLT_MAX,
117 INFINITY,
118 NAN,
119 SNANF
122 static const int num_f32 = ARRAY_SIZE(f32_numbers);
124 int get_num_f32(void)
126 return num_f32;
129 float get_f32(int i)
131 return f32_numbers[i % num_f32];
134 char *fmt_f32(float num)
136 uint32_t single_as_hex = *(uint32_t *) &num;
137 char *fmt;
138 asprintf(&fmt, "f32(%02.20a:%#010x)", num, single_as_hex);
139 return fmt;
143 /* This allows us to initialise some doubles as pure hex */
144 typedef union {
145 double d;
146 uint64_t h;
147 } test_doubles;
149 static test_doubles f64_numbers[] = {
150 {SNAN},
151 {-NAN},
152 {-INFINITY},
153 {-DBL_MAX},
154 {-FLT_MAX-1.0},
155 {-FLT_MAX},
156 {-1.111E+31},
157 {-1.111E+30}, /* half prec */
158 {-2.0}, {-1.0},
159 {-DBL_MIN},
160 {-FLT_MIN},
161 {0.0},
162 {FLT_MIN},
163 {2.98023224e-08},
164 {5.96046E-8}, /* min positive FP16 subnormal */
165 {6.09756E-5}, /* max subnormal FP16 */
166 {6.10352E-5}, /* min positive normal FP16 */
167 {1.0},
168 {1.0009765625}, /* smallest float after 1.0 FP16 */
169 {DBL_MIN},
170 {1.3789972848607228e-308},
171 {1.4914738736681624e-308},
172 {1.0}, {2.0},
173 {M_E}, {M_PI},
174 {65503.0},
175 {65504.0}, /* max FP16 */
176 {65505.0},
177 {131007.0},
178 {131008.0}, /* max AFP */
179 {131009.0},
180 {.h = 0x41dfffffffc00000 }, /* to int = 0x7fffffff */
181 {FLT_MAX},
182 {FLT_MAX + 1.0},
183 {DBL_MAX},
184 {INFINITY},
185 {NAN},
186 {.h = 0x7ff0000000000001}, /* SNAN */
187 {SNAN},
190 static const int num_f64 = ARRAY_SIZE(f64_numbers);
192 int get_num_f64(void)
194 return num_f64;
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;
205 char *fmt;
206 asprintf(&fmt, "f64(%02.20a:%#020" PRIx64 ")", num, double_as_hex);
207 return fmt;
211 * Float flags
213 char *fmt_flags(void)
215 int flags = fetestexcept(FE_ALL_EXCEPT);
216 char *fmt;
218 if (flags) {
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" : "");
225 } else {
226 asprintf(&fmt, "OK");
229 return fmt;