| 1 | /* |
| 2 | * Test Floating Point Conversion |
| 3 | */ |
| 4 | |
| 5 | /* we want additional float type definitions */ |
| 6 | #define __STDC_WANT_IEC_60559_BFP_EXT__ |
| 7 | #define __STDC_WANT_IEC_60559_TYPES_EXT__ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <inttypes.h> |
| 11 | #include <math.h> |
| 12 | #include <float.h> |
| 13 | #include <fenv.h> |
| 14 | |
| 15 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 16 | |
| 17 | static char flag_str[256]; |
| 18 | |
| 19 | static char *get_flag_state(int flags) |
| 20 | { |
| 21 | if (flags) { |
| 22 | snprintf(flag_str, sizeof(flag_str), "%s %s %s %s %s", |
| 23 | flags & FE_OVERFLOW ? "OVERFLOW" : "", |
| 24 | flags & FE_UNDERFLOW ? "UNDERFLOW" : "", |
| 25 | flags & FE_DIVBYZERO ? "DIV0" : "", |
| 26 | flags & FE_INEXACT ? "INEXACT" : "", |
| 27 | flags & FE_INVALID ? "INVALID" : ""); |
| 28 | } else { |
| 29 | snprintf(flag_str, sizeof(flag_str), "OK"); |
| 30 | } |
| 31 | |
| 32 | return flag_str; |
| 33 | } |
| 34 | |
| 35 | static void print_double_number(int i, double num) |
| 36 | { |
| 37 | uint64_t double_as_hex = *(uint64_t *) # |
| 38 | int flags = fetestexcept(FE_ALL_EXCEPT); |
| 39 | char *fstr = get_flag_state(flags); |
| 40 | |
| 41 | printf("%02d DOUBLE: %02.20e / %#020" PRIx64 " (%#x => %s)\n", |
| 42 | i, num, double_as_hex, flags, fstr); |
| 43 | } |
| 44 | |
| 45 | static void print_single_number(int i, float num) |
| 46 | { |
| 47 | uint32_t single_as_hex = *(uint32_t *) # |
| 48 | int flags = fetestexcept(FE_ALL_EXCEPT); |
| 49 | char *fstr = get_flag_state(flags); |
| 50 | |
| 51 | printf("%02d SINGLE: %02.20e / %#010x (%#x => %s)\n", |
| 52 | i, num, single_as_hex, flags, fstr); |
| 53 | } |
| 54 | |
| 55 | static void print_half_number(int i, uint16_t num) |
| 56 | { |
| 57 | int flags = fetestexcept(FE_ALL_EXCEPT); |
| 58 | char *fstr = get_flag_state(flags); |
| 59 | |
| 60 | printf("%02d HALF: %#04x (%#x => %s)\n", |
| 61 | i, num, flags, fstr); |
| 62 | } |
| 63 | |
| 64 | static void print_int64(int i, int64_t num) |
| 65 | { |
| 66 | uint64_t int64_as_hex = *(uint64_t *) # |
| 67 | int flags = fetestexcept(FE_ALL_EXCEPT); |
| 68 | char *fstr = get_flag_state(flags); |
| 69 | |
| 70 | printf("%02d INT64: %20" PRId64 "/%#020" PRIx64 " (%#x => %s)\n", |
| 71 | i, num, int64_as_hex, flags, fstr); |
| 72 | } |
| 73 | |
| 74 | #ifndef SNANF |
| 75 | /* Signaling NaN macros, if supported. */ |
| 76 | # define SNANF (__builtin_nansf ("")) |
| 77 | # define SNAN (__builtin_nans ("")) |
| 78 | # define SNANL (__builtin_nansl ("")) |
| 79 | #endif |
| 80 | |
| 81 | float single_numbers[] = { -SNANF, |
| 82 | -NAN, |
| 83 | -INFINITY, |
| 84 | -FLT_MAX, |
| 85 | -1.111E+31, |
| 86 | -1.111E+30, |
| 87 | -1.08700982e-12, |
| 88 | -1.78051176e-20, |
| 89 | -FLT_MIN, |
| 90 | 0.0, |
| 91 | FLT_MIN, |
| 92 | 2.98023224e-08, |
| 93 | 5.96046E-8, /* min positive FP16 subnormal */ |
| 94 | 6.09756E-5, /* max subnormal FP16 */ |
| 95 | 6.10352E-5, /* min positive normal FP16 */ |
| 96 | 1.0, |
| 97 | 1.0009765625, /* smallest float after 1.0 FP16 */ |
| 98 | 2.0, |
| 99 | M_E, M_PI, |
| 100 | 65503.0, |
| 101 | 65504.0, /* max FP16 */ |
| 102 | 65505.0, |
| 103 | 131007.0, |
| 104 | 131008.0, /* max AFP */ |
| 105 | 131009.0, |
| 106 | 1.111E+30, |
| 107 | FLT_MAX, |
| 108 | INFINITY, |
| 109 | NAN, |
| 110 | SNANF }; |
| 111 | |
| 112 | static void convert_single_to_half(void) |
| 113 | { |
| 114 | int i; |
| 115 | |
| 116 | printf("Converting single-precision to half-precision\n"); |
| 117 | |
| 118 | for (i = 0; i < ARRAY_SIZE(single_numbers); ++i) { |
| 119 | float input = single_numbers[i]; |
| 120 | |
| 121 | feclearexcept(FE_ALL_EXCEPT); |
| 122 | |
| 123 | print_single_number(i, input); |
| 124 | #if defined(__arm__) |
| 125 | uint32_t output; |
| 126 | asm("vcvtb.f16.f32 %0, %1" : "=t" (output) : "x" (input)); |
| 127 | #else |
| 128 | uint16_t output; |
| 129 | asm("fcvt %h0, %s1" : "=w" (output) : "w" (input)); |
| 130 | #endif |
| 131 | print_half_number(i, output); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | static void convert_single_to_double(void) |
| 136 | { |
| 137 | int i; |
| 138 | |
| 139 | printf("Converting single-precision to double-precision\n"); |
| 140 | |
| 141 | for (i = 0; i < ARRAY_SIZE(single_numbers); ++i) { |
| 142 | float input = single_numbers[i]; |
| 143 | /* uint64_t output; */ |
| 144 | double output; |
| 145 | |
| 146 | feclearexcept(FE_ALL_EXCEPT); |
| 147 | |
| 148 | print_single_number(i, input); |
| 149 | #if defined(__arm__) |
| 150 | asm("vcvt.f64.f32 %P0, %1" : "=w" (output) : "t" (input)); |
| 151 | #else |
| 152 | asm("fcvt %d0, %s1" : "=w" (output) : "w" (input)); |
| 153 | #endif |
| 154 | print_double_number(i, output); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | static void convert_single_to_integer(void) |
| 159 | { |
| 160 | int i; |
| 161 | |
| 162 | printf("Converting single-precision to integer\n"); |
| 163 | |
| 164 | for (i = 0; i < ARRAY_SIZE(single_numbers); ++i) { |
| 165 | float input = single_numbers[i]; |
| 166 | int64_t output; |
| 167 | |
| 168 | feclearexcept(FE_ALL_EXCEPT); |
| 169 | |
| 170 | print_single_number(i, input); |
| 171 | #if defined(__arm__) |
| 172 | /* asm("vcvt.s32.f32 %s0, %s1" : "=t" (output) : "t" (input)); */ |
| 173 | output = input; |
| 174 | #else |
| 175 | #ifdef FPRCVT |
| 176 | asm("fcvtzs d0, %s1\r\n" |
| 177 | "fmov %0, d0" : |
| 178 | "=r" (output) : "w" (input)); |
| 179 | #else |
| 180 | asm("fcvtzs %0, %s1" : "=r" (output) : "w" (input)); |
| 181 | #endif |
| 182 | #endif |
| 183 | print_int64(i, output); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /* This allows us to initialise some doubles as pure hex */ |
| 188 | typedef union { |
| 189 | double d; |
| 190 | uint64_t h; |
| 191 | } test_doubles; |
| 192 | |
| 193 | test_doubles double_numbers[] = { |
| 194 | {SNAN}, |
| 195 | {-NAN}, |
| 196 | {-INFINITY}, |
| 197 | {-DBL_MAX}, |
| 198 | {-FLT_MAX-1.0}, |
| 199 | {-FLT_MAX}, |
| 200 | {-1.111E+31}, |
| 201 | {-1.111E+30}, /* half prec */ |
| 202 | {-2.0}, {-1.0}, |
| 203 | {-DBL_MIN}, |
| 204 | {-FLT_MIN}, |
| 205 | {0.0}, |
| 206 | {FLT_MIN}, |
| 207 | {2.98023224e-08}, |
| 208 | {5.96046E-8}, /* min positive FP16 subnormal */ |
| 209 | {6.09756E-5}, /* max subnormal FP16 */ |
| 210 | {6.10352E-5}, /* min positive normal FP16 */ |
| 211 | {1.0}, |
| 212 | {1.0009765625}, /* smallest float after 1.0 FP16 */ |
| 213 | {DBL_MIN}, |
| 214 | {1.3789972848607228e-308}, |
| 215 | {1.4914738736681624e-308}, |
| 216 | {1.0}, {2.0}, |
| 217 | {M_E}, {M_PI}, |
| 218 | {65503.0}, |
| 219 | {65504.0}, /* max FP16 */ |
| 220 | {65505.0}, |
| 221 | {131007.0}, |
| 222 | {131008.0}, /* max AFP */ |
| 223 | {131009.0}, |
| 224 | {.h = 0x41dfffffffc00000 }, /* to int = 0x7fffffff */ |
| 225 | {FLT_MAX}, |
| 226 | {FLT_MAX + 1.0}, |
| 227 | {DBL_MAX}, |
| 228 | {INFINITY}, |
| 229 | {NAN}, |
| 230 | {.h = 0x7ff0000000000001}, /* SNAN */ |
| 231 | {SNAN}, |
| 232 | }; |
| 233 | |
| 234 | static void convert_double_to_half(void) |
| 235 | { |
| 236 | int i; |
| 237 | |
| 238 | printf("Converting double-precision to half-precision\n"); |
| 239 | |
| 240 | for (i = 0; i < ARRAY_SIZE(double_numbers); ++i) { |
| 241 | double input = double_numbers[i].d; |
| 242 | uint16_t output; |
| 243 | |
| 244 | feclearexcept(FE_ALL_EXCEPT); |
| 245 | |
| 246 | print_double_number(i, input); |
| 247 | |
| 248 | /* as we don't have _Float16 support */ |
| 249 | #if defined(__arm__) |
| 250 | /* asm("vcvtb.f16.f64 %0, %P1" : "=t" (output) : "x" (input)); */ |
| 251 | output = input; |
| 252 | #else |
| 253 | asm("fcvt %h0, %d1" : "=w" (output) : "w" (input)); |
| 254 | #endif |
| 255 | print_half_number(i, output); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | static void convert_double_to_single(void) |
| 260 | { |
| 261 | int i; |
| 262 | |
| 263 | printf("Converting double-precision to single-precision\n"); |
| 264 | |
| 265 | for (i = 0; i < ARRAY_SIZE(double_numbers); ++i) { |
| 266 | double input = double_numbers[i].d; |
| 267 | float output; |
| 268 | |
| 269 | feclearexcept(FE_ALL_EXCEPT); |
| 270 | |
| 271 | print_double_number(i, input); |
| 272 | |
| 273 | #if defined(__arm__) |
| 274 | asm("vcvt.f32.f64 %0, %P1" : "=w" (output) : "x" (input)); |
| 275 | #else |
| 276 | asm("fcvt %s0, %d1" : "=w" (output) : "w" (input)); |
| 277 | #endif |
| 278 | |
| 279 | print_single_number(i, output); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | static void convert_double_to_integer(void) |
| 284 | { |
| 285 | int i; |
| 286 | |
| 287 | printf("Converting double-precision to integer\n"); |
| 288 | |
| 289 | for (i = 0; i < ARRAY_SIZE(double_numbers); ++i) { |
| 290 | double input = double_numbers[i].d; |
| 291 | int64_t output; |
| 292 | |
| 293 | feclearexcept(FE_ALL_EXCEPT); |
| 294 | |
| 295 | print_double_number(i, input); |
| 296 | #if defined(__arm__) |
| 297 | /* asm("vcvt.s32.f32 %s0, %s1" : "=t" (output) : "t" (input)); */ |
| 298 | output = input; |
| 299 | #else |
| 300 | asm("fcvtzs %0, %d1" : "=r" (output) : "w" (input)); |
| 301 | #endif |
| 302 | print_int64(i, output); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /* no handy defines for these numbers */ |
| 307 | uint16_t half_numbers[] = { |
| 308 | 0xffff, /* -NaN / AHP -Max */ |
| 309 | 0xfcff, /* -NaN / AHP */ |
| 310 | 0xfc01, /* -NaN / AHP */ |
| 311 | 0xfc00, /* -Inf */ |
| 312 | 0xfbff, /* -Max */ |
| 313 | 0xc000, /* -2 */ |
| 314 | 0xbc00, /* -1 */ |
| 315 | 0x8001, /* -MIN subnormal */ |
| 316 | 0x8000, /* -0 */ |
| 317 | 0x0000, /* +0 */ |
| 318 | 0x0001, /* MIN subnormal */ |
| 319 | 0x3c00, /* 1 */ |
| 320 | 0x7bff, /* Max */ |
| 321 | 0x7c00, /* Inf */ |
| 322 | 0x7c01, /* NaN / AHP */ |
| 323 | 0x7cff, /* NaN / AHP */ |
| 324 | 0x7fff, /* NaN / AHP +Max*/ |
| 325 | }; |
| 326 | |
| 327 | static void convert_half_to_double(void) |
| 328 | { |
| 329 | int i; |
| 330 | |
| 331 | printf("Converting half-precision to double-precision\n"); |
| 332 | |
| 333 | for (i = 0; i < ARRAY_SIZE(half_numbers); ++i) { |
| 334 | uint16_t input = half_numbers[i]; |
| 335 | double output; |
| 336 | |
| 337 | feclearexcept(FE_ALL_EXCEPT); |
| 338 | |
| 339 | print_half_number(i, input); |
| 340 | #if defined(__arm__) |
| 341 | /* asm("vcvtb.f64.f16 %P0, %1" : "=w" (output) : "t" (input)); */ |
| 342 | output = input; |
| 343 | #else |
| 344 | asm("fcvt %d0, %h1" : "=w" (output) : "w" (input)); |
| 345 | #endif |
| 346 | print_double_number(i, output); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | static void convert_half_to_single(void) |
| 351 | { |
| 352 | int i; |
| 353 | |
| 354 | printf("Converting half-precision to single-precision\n"); |
| 355 | |
| 356 | for (i = 0; i < ARRAY_SIZE(half_numbers); ++i) { |
| 357 | uint16_t input = half_numbers[i]; |
| 358 | float output; |
| 359 | |
| 360 | feclearexcept(FE_ALL_EXCEPT); |
| 361 | |
| 362 | print_half_number(i, input); |
| 363 | #if defined(__arm__) |
| 364 | /* |
| 365 | * Clang refuses to allocate an integer to a fp register. |
| 366 | * Perform the move from a general register by hand. |
| 367 | */ |
| 368 | asm("vmov %0, %1\n\t" |
| 369 | "vcvtb.f32.f16 %0, %0" : "=w" (output) : "r" (input)); |
| 370 | #else |
| 371 | asm("fcvt %s0, %h1" : "=w" (output) : "w" (input)); |
| 372 | #endif |
| 373 | print_single_number(i, output); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | static void convert_half_to_integer(void) |
| 378 | { |
| 379 | int i; |
| 380 | |
| 381 | printf("Converting half-precision to integer\n"); |
| 382 | |
| 383 | for (i = 0; i < ARRAY_SIZE(half_numbers); ++i) { |
| 384 | uint16_t input = half_numbers[i]; |
| 385 | int64_t output; |
| 386 | |
| 387 | feclearexcept(FE_ALL_EXCEPT); |
| 388 | |
| 389 | print_half_number(i, input); |
| 390 | #if defined(__arm__) |
| 391 | /* asm("vcvt.s32.f16 %0, %1" : "=t" (output) : "t" (input)); v8.2*/ |
| 392 | output = input; |
| 393 | #else |
| 394 | asm("fcvt %s0, %h1" : "=w" (output) : "w" (input)); |
| 395 | #endif |
| 396 | print_int64(i, output); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | typedef struct { |
| 401 | int flag; |
| 402 | char *desc; |
| 403 | } float_mapping; |
| 404 | |
| 405 | float_mapping round_flags[] = { |
| 406 | { FE_TONEAREST, "to nearest" }, |
| 407 | { FE_UPWARD, "upwards" }, |
| 408 | { FE_DOWNWARD, "downwards" }, |
| 409 | { FE_TOWARDZERO, "to zero" } |
| 410 | }; |
| 411 | |
| 412 | int main(int argc, char *argv[argc]) |
| 413 | { |
| 414 | int i; |
| 415 | |
| 416 | printf("#### Enabling IEEE Half Precision\n"); |
| 417 | |
| 418 | for (i = 0; i < ARRAY_SIZE(round_flags); ++i) { |
| 419 | fesetround(round_flags[i].flag); |
| 420 | printf("### Rounding %s\n", round_flags[i].desc); |
| 421 | convert_single_to_half(); |
| 422 | convert_single_to_double(); |
| 423 | convert_double_to_half(); |
| 424 | convert_double_to_single(); |
| 425 | convert_half_to_single(); |
| 426 | convert_half_to_double(); |
| 427 | } |
| 428 | |
| 429 | /* convert to integer */ |
| 430 | convert_single_to_integer(); |
| 431 | convert_double_to_integer(); |
| 432 | convert_half_to_integer(); |
| 433 | |
| 434 | |
| 435 | /* And now with ARM alternative FP16 */ |
| 436 | #if defined(__arm__) |
| 437 | asm("vmrs r1, fpscr\n\t" |
| 438 | "orr r1, r1, %[flags]\n\t" |
| 439 | "vmsr fpscr, r1" |
| 440 | : /* no output */ : [flags] "n" (1 << 26) : "r1" ); |
| 441 | #else |
| 442 | asm("mrs x1, fpcr\n\t" |
| 443 | "orr x1, x1, %[flags]\n\t" |
| 444 | "msr fpcr, x1\n\t" |
| 445 | : /* no output */ : [flags] "n" (1 << 26) : "x1" ); |
| 446 | #endif |
| 447 | |
| 448 | printf("#### Enabling ARM Alternative Half Precision\n"); |
| 449 | |
| 450 | for (i = 0; i < ARRAY_SIZE(round_flags); ++i) { |
| 451 | fesetround(round_flags[i].flag); |
| 452 | printf("### Rounding %s\n", round_flags[i].desc); |
| 453 | convert_single_to_half(); |
| 454 | convert_single_to_double(); |
| 455 | convert_double_to_half(); |
| 456 | convert_double_to_single(); |
| 457 | convert_half_to_single(); |
| 458 | convert_half_to_double(); |
| 459 | } |
| 460 | |
| 461 | /* convert to integer */ |
| 462 | convert_single_to_integer(); |
| 463 | convert_double_to_integer(); |
| 464 | convert_half_to_integer(); |
| 465 | |
| 466 | return 0; |
| 467 | } |