1 | // RUN: %clang_builtins %s %librt -o %t && %run %t |
2 | |
3 | #define SINGLE_PRECISION |
4 | #include <fenv.h> |
5 | #include <float.h> |
6 | #include <limits.h> |
7 | #include <math.h> |
8 | #include <stdio.h> |
9 | #include "fp_lib.h" |
10 | |
11 | int test__compiler_rt_scalbnf(const char *mode, fp_t x, int y) { |
12 | #if defined(__ve__) |
13 | if (fpclassify(x) == FP_SUBNORMAL) |
14 | return 0; |
15 | #endif |
16 | fp_t crt_value = __compiler_rt_scalbnf(x, y); |
17 | fp_t libm_value = scalbnf(x, y); |
18 | // Consider +/-0 unequal, but disregard the sign/payload of NaN. |
19 | if (toRep(crt_value) != toRep(libm_value) && |
20 | !(crt_isnan(crt_value) && crt_isnan(libm_value))) { |
21 | printf("error: [%s] in __compiler_rt_scalbnf(%a [%X], %d) = %a [%X] " |
22 | "!= %a [%X]\n" , |
23 | mode, x, toRep(x), y, crt_value, toRep(crt_value), |
24 | libm_value, toRep(libm_value)); |
25 | return 1; |
26 | } |
27 | return 0; |
28 | } |
29 | |
30 | fp_t cases[] = { |
31 | -NAN, NAN, -INFINITY, INFINITY, -0.0, 0.0, -1, 1, -2, 2, |
32 | FLT_TRUE_MIN, FLT_TRUE_MIN*7, FLT_MIN, FLT_MAX, |
33 | -1.001, 1.001, -1.002, 1.002, 1.e-6, -1.e-6, |
34 | 0x1.0p-125, |
35 | 0x1.0p-126, |
36 | 0x1.0p-127, // subnormal |
37 | 0x1.0p-128, // subnormal |
38 | }; |
39 | |
40 | int iterate_cases(const char *mode) { |
41 | const unsigned N = sizeof(cases) / sizeof(cases[0]); |
42 | unsigned i; |
43 | for (i = 0; i < N; ++i) { |
44 | int j; |
45 | for (j = -5; j <= 5; ++j) { |
46 | if (test__compiler_rt_scalbnf(mode, cases[i], j)) return 1; |
47 | } |
48 | if (test__compiler_rt_scalbnf(mode, cases[i], -1000)) return 1; |
49 | if (test__compiler_rt_scalbnf(mode, cases[i], 1000)) return 1; |
50 | if (test__compiler_rt_scalbnf(mode, cases[i], INT_MIN)) return 1; |
51 | if (test__compiler_rt_scalbnf(mode, cases[i], INT_MAX)) return 1; |
52 | } |
53 | return 0; |
54 | } |
55 | |
56 | int main() { |
57 | if (iterate_cases(mode: "default" )) return 1; |
58 | |
59 | // Rounding mode tests on supported architectures. __compiler_rt_scalbnf |
60 | // should have the same rounding behavior as single-precision multiplication. |
61 | #if (defined(__arm__) || defined(__aarch64__)) && defined(__ARM_FP) || \ |
62 | defined(__i386__) || defined(__x86_64__) |
63 | // Skip these tests for MSVC because its scalbnf function always behaves as if |
64 | // the default rounding mode is set (FE_TONEAREST). |
65 | #ifndef _MSC_VER |
66 | fesetround(FE_UPWARD); |
67 | if (iterate_cases(mode: "FE_UPWARD" )) return 1; |
68 | |
69 | fesetround(FE_DOWNWARD); |
70 | if (iterate_cases(mode: "FE_DOWNWARD" )) return 1; |
71 | |
72 | fesetround(FE_TOWARDZERO); |
73 | if (iterate_cases(mode: "FE_TOWARDZERO" )) return 1; |
74 | #endif |
75 | |
76 | fesetround(FE_TONEAREST); |
77 | if (iterate_cases(mode: "FE_TONEAREST" )) return 1; |
78 | #endif |
79 | |
80 | return 0; |
81 | } |
82 | |