File: compiler_rt_scalbnf_test.c

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (81 lines) | stat: -rw-r--r-- 2,435 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// RUN: %clang_builtins %s %librt -o %t && %run %t

#define SINGLE_PRECISION
#include <fenv.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include "fp_lib.h"

int test__compiler_rt_scalbnf(const char *mode, fp_t x, int y) {
#if defined(__ve__)
  if (fpclassify(x) == FP_SUBNORMAL)
    return 0;
#endif
  fp_t crt_value = __compiler_rt_scalbnf(x, y);
  fp_t libm_value = scalbnf(x, y);
  // Consider +/-0 unequal, but disregard the sign/payload of NaN.
  if (toRep(crt_value) != toRep(libm_value) &&
      !(crt_isnan(crt_value) && crt_isnan(libm_value))) {
    printf("error: [%s] in __compiler_rt_scalbnf(%a [%X], %d) = %a [%X] "
           "!= %a [%X]\n",
           mode, x, toRep(x), y, crt_value, toRep(crt_value),
           libm_value, toRep(libm_value));
    return 1;
  }
  return 0;
}

fp_t cases[] = {
  -NAN, NAN, -INFINITY, INFINITY, -0.0, 0.0, -1, 1, -2, 2,
  FLT_TRUE_MIN, FLT_TRUE_MIN*7, FLT_MIN, FLT_MAX,
  -1.001, 1.001, -1.002, 1.002, 1.e-6, -1.e-6,
  0x1.0p-125,
  0x1.0p-126,
  0x1.0p-127, // subnormal
  0x1.0p-128, // subnormal
};

int iterate_cases(const char *mode) {
  const unsigned N = sizeof(cases) / sizeof(cases[0]);
  unsigned i;
  for (i = 0; i < N; ++i) {
    int j;
    for (j = -5; j <= 5; ++j) {
      if (test__compiler_rt_scalbnf(mode, cases[i], j)) return 1;
    }
    if (test__compiler_rt_scalbnf(mode, cases[i], -1000)) return 1;
    if (test__compiler_rt_scalbnf(mode, cases[i], 1000)) return 1;
    if (test__compiler_rt_scalbnf(mode, cases[i], INT_MIN)) return 1;
    if (test__compiler_rt_scalbnf(mode, cases[i], INT_MAX)) return 1;
  }
  return 0;
}

int main() {
  if (iterate_cases("default")) return 1;

  // Rounding mode tests on supported architectures. __compiler_rt_scalbnf
  // should have the same rounding behavior as single-precision multiplication.
#if (defined(__arm__) || defined(__aarch64__)) && defined(__ARM_FP) || \
    defined(__i386__) || defined(__x86_64__)
// Skip these tests for MSVC because its scalbnf function always behaves as if
// the default rounding mode is set (FE_TONEAREST).
#ifndef _MSC_VER
  fesetround(FE_UPWARD);
  if (iterate_cases("FE_UPWARD")) return 1;

  fesetround(FE_DOWNWARD);
  if (iterate_cases("FE_DOWNWARD")) return 1;

  fesetround(FE_TOWARDZERO);
  if (iterate_cases("FE_TOWARDZERO")) return 1;
#endif

  fesetround(FE_TONEAREST);
  if (iterate_cases("FE_TONEAREST")) return 1;
#endif

  return 0;
}