87 lines · c
1// RUN: %clang_builtins %s %librt -o %t && %run %t2 3#define DOUBLE_PRECISION4#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#if defined(__ve__)13 if (fpclassify(x) == FP_SUBNORMAL)14 return 0;15#endif16 fp_t crt_value = __compiler_rt_scalbn(x, y);17 fp_t libm_value = scalbn(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_scalbn(%a [%llX], %d) = %a [%llX] "22 "!= %a [%llX]\n",23 mode, x, (unsigned long long)toRep(x), y,24 crt_value, (unsigned long long)toRep(crt_value),25 libm_value, (unsigned long long)toRep(libm_value));26 return 1;27 }28 return 0;29}30 31fp_t cases[] = {32 -NAN, NAN, -INFINITY, INFINITY, -0.0, 0.0, -1, 1, -2, 2,33 DBL_TRUE_MIN, DBL_TRUE_MIN*7, DBL_MIN, DBL_MAX,34 -1.001, 1.001, -1.002, 1.002, 1.e-6, -1.e-6,35 0x1.0p-1021,36 0x1.0p-1022,37 0x1.0p-1023, // subnormal38 0x1.0p-1024, // subnormal39};40 41int iterate_cases(const char *mode) {42 const unsigned N = sizeof(cases) / sizeof(cases[0]);43 unsigned i;44 for (i = 0; i < N; ++i) {45 int j;46 for (j = -5; j <= 5; ++j) {47 if (test__compiler_rt_scalbn(mode, cases[i], j)) return 1;48 }49 if (test__compiler_rt_scalbn(mode, cases[i], -10000)) return 1;50 if (test__compiler_rt_scalbn(mode, cases[i], 10000)) return 1;51 if (test__compiler_rt_scalbn(mode, cases[i], INT_MIN)) return 1;52 if (test__compiler_rt_scalbn(mode, cases[i], INT_MAX)) return 1;53 }54 return 0;55}56 57int main() {58 if (iterate_cases("default")) return 1;59 60 // Rounding mode tests on supported architectures. __compiler_rt_scalbn61 // should have the same rounding behavior as double-precision multiplication.62#if (defined(__arm__) || defined(__aarch64__)) && defined(__ARM_FP) || \63 defined(__i386__) || defined(__x86_64__)64// Skip these tests on Windows because the UCRT scalbn function always behaves65// as if the default rounding mode is set (FE_TONEAREST).66// Also skip for newlib because although its scalbn function does respect the67// rounding mode, where the tests trigger an underflow or overflow using a68// large exponent the result is rounded in the opposite direction to that which69// would be expected in the (FE_UPWARD) and (FE_DOWNWARD) modes.70# if !defined(_WIN32) && !defined(_NEWLIB_VERSION)71 fesetround(FE_UPWARD);72 if (iterate_cases("FE_UPWARD")) return 1;73 74 fesetround(FE_DOWNWARD);75 if (iterate_cases("FE_DOWNWARD")) return 1;76 77 fesetround(FE_TOWARDZERO);78 if (iterate_cases("FE_TOWARDZERO")) return 1;79#endif80 81 fesetround(FE_TONEAREST);82 if (iterate_cases("FE_TONEAREST")) return 1;83#endif84 85 return 0;86}87