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