xref: /llvm-project/compiler-rt/test/builtins/Unit/compiler_rt_scalbnf_test.c (revision 703de006d30b734676e48deadafd6d89903f357e)
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 
test__compiler_rt_scalbnf(const char * mode,fp_t x,int y)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 
iterate_cases(const char * mode)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 
main()56 int main() {
57   if (iterate_cases("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 on Windows because the UCRT scalbnf function always behaves
64 // as if the default rounding mode is set (FE_TONEAREST).
65 // Also skip for newlib because although its scalbnf function does respect the
66 // rounding mode, where the tests trigger an underflow or overflow using a
67 // large exponent the result is rounded in the opposite direction to that which
68 // would be expected in the (FE_UPWARD) and (FE_DOWNWARD) modes.
69 #  if !defined(_WIN32) && !defined(_NEWLIB_VERSION)
70   fesetround(FE_UPWARD);
71   if (iterate_cases("FE_UPWARD")) return 1;
72 
73   fesetround(FE_DOWNWARD);
74   if (iterate_cases("FE_DOWNWARD")) return 1;
75 
76   fesetround(FE_TOWARDZERO);
77   if (iterate_cases("FE_TOWARDZERO")) return 1;
78 #endif
79 
80   fesetround(FE_TONEAREST);
81   if (iterate_cases("FE_TONEAREST")) return 1;
82 #endif
83 
84   return 0;
85 }
86