Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / builtins / Unit / compiler_rt_scalbnf_test.c
blob3ffdde6aed3f2002d09d3e3ab1e3987ae2919fc9
1 // RUN: %clang_builtins %s %librt -o %t && %run %t
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"
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;
27 return 0;
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
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;
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;
53 return 0;
56 int main() {
57 if (iterate_cases("default")) return 1;
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;
73 fesetround(FE_DOWNWARD);
74 if (iterate_cases("FE_DOWNWARD")) return 1;
76 fesetround(FE_TOWARDZERO);
77 if (iterate_cases("FE_TOWARDZERO")) return 1;
78 #endif
80 fesetround(FE_TONEAREST);
81 if (iterate_cases("FE_TONEAREST")) return 1;
82 #endif
84 return 0;