1// RUN: %clang_builtins %s %librt -o %t && %run %t
2
3#define DOUBLE_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_scalbn(const char *mode, fp_t x, int y) {
12 fp_t crt_value = __compiler_rt_scalbn(x, y);
13 fp_t libm_value = scalbn(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_scalbn(%a [%llX], %d) = %a [%llX] "
18 "!= %a [%llX]\n",
19 mode, x, (unsigned long long)toRep(x), y,
20 crt_value, (unsigned long long)toRep(crt_value),
21 libm_value, (unsigned long long)toRep(libm_value));
22 return 1;
23 }
24 return 0;
25}
26
27fp_t cases[] = {
28 -NAN, NAN, -INFINITY, INFINITY, -0.0, 0.0, -1, 1, -2, 2,
29 DBL_TRUE_MIN, DBL_TRUE_MIN*7, DBL_MIN, DBL_MAX,
30 -1.001, 1.001, -1.002, 1.002, 1.e-6, -1.e-6,
31 0x1.0p-1021,
32 0x1.0p-1022,
33 0x1.0p-1023, // subnormal
34 0x1.0p-1024, // subnormal
35};
36
37int iterate_cases(const char *mode) {
38 const unsigned N = sizeof(cases) / sizeof(cases[0]);
39 unsigned i;
40 for (i = 0; i < N; ++i) {
41 int j;
42 for (j = -5; j <= 5; ++j) {
43 if (test__compiler_rt_scalbn(mode, cases[i], j)) return 1;
44 }
45 if (test__compiler_rt_scalbn(mode, cases[i], -10000)) return 1;
46 if (test__compiler_rt_scalbn(mode, cases[i], 10000)) return 1;
47 if (test__compiler_rt_scalbn(mode, cases[i], INT_MIN)) return 1;
48 if (test__compiler_rt_scalbn(mode, cases[i], INT_MAX)) return 1;
49 }
50 return 0;
51}
52
53int main() {
54 if (iterate_cases("default")) return 1;
55
56 // Rounding mode tests on supported architectures. __compiler_rt_scalbn
57 // should have the same rounding behavior as double-precision multiplication.
58#if (defined(__arm__) || defined(__aarch64__)) && defined(__ARM_FP) || \
59 defined(__i386__) || defined(__x86_64__)
60// Skip these tests for MSVC because its scalbn function always behaves as if
61// the default rounding mode is set (FE_TONEAREST).
62#ifndef _MSC_VER
63 fesetround(FE_UPWARD);
64 if (iterate_cases("FE_UPWARD")) return 1;
65
66 fesetround(FE_DOWNWARD);
67 if (iterate_cases("FE_DOWNWARD")) return 1;
68
69 fesetround(FE_TOWARDZERO);
70 if (iterate_cases("FE_TOWARDZERO")) return 1;
71#endif
72
73 fesetround(FE_TONEAREST);
74 if (iterate_cases("FE_TONEAREST")) return 1;
75#endif
76
77 return 0;
78}
79