File: rounding-math.cpp

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,496,180 kB
  • sloc: cpp: 5,593,972; ansic: 986,872; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,538; xml: 953; cs: 573; fortran: 567
file content (78 lines) | stat: -rw-r--r-- 2,024 bytes parent folder | download | duplicates (9)
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
// RUN: %clang_cc1 -triple x86_64-linux -verify=norounding -Wno-unknown-pragmas %s
// RUN: %clang_cc1 -triple x86_64-linux -verify=rounding %s -frounding-math -Wno-unknown-pragmas
// rounding-no-diagnostics

#define fold(x) (__builtin_constant_p(x) ? (x) : (x))

constexpr double a = 1.0 / 3.0;

constexpr int f(int n) { return int(n * (1.0 / 3.0)); }

using T = int[f(3)];
using T = int[1];

enum Enum { enum_a = f(3) };

struct Bitfield {
  unsigned int n : 1;
  unsigned int m : f(3);
};

void f(Bitfield &b) {
  b.n = int(6 * (1.0 / 3.0)); // norounding-warning {{changes value from 2 to 0}}
}

const int k = 3 * (1.0 / 3.0);
static_assert(k == 1, "");

void g() {
  // FIXME: Constant-evaluating this initializer is surprising, and violates
  // the recommended practice in C++ [expr.const]p12:
  //
  //   Implementations should provide consistent results of floating-point
  //   evaluations, irrespective of whether the evaluation is performed during
  //   translation or during program execution.
  const int k = 3 * (1.0 / 3.0);
  static_assert(k == 1, "");
}

int *h() {
  return new int[int(-3 * (1.0 / 3.0))]; // norounding-error {{too large}}
}


// nextUp(1.F) == 0x1.000002p0F
static_assert(1.0F + 0x0.000001p0F == 0x1.0p0F, "");

char Arr01[1 + (1.0F + 0x0.000001p0F > 1.0F)];
static_assert(sizeof(Arr01) == 1, "");

struct S1 {
  int : (1.0F + 0x0.000001p0F > 1.0F);
  int f;
};
static_assert(sizeof(S1) == sizeof(int), "");

#pragma STDC FENV_ROUND FE_UPWARD
static_assert(1.0F + 0x0.000001p0F == 0x1.000002p0F, "");

char Arr01u[1 + (1.0F + 0x0.000001p0F > 1.0F)];
static_assert(sizeof(Arr01u) == 2, "");

struct S1u {
  int : (1.0F + 0x0.000001p0F > 1.0F);
  int f;
};
static_assert(sizeof(S1u) > sizeof(int), "");

#pragma STDC FENV_ROUND FE_DOWNWARD
static_assert(1.0F + 0x0.000001p0F == 1.0F, "");

char Arr01d[1 + (1.0F + 0x0.000001p0F > 1.0F)];
static_assert(sizeof(Arr01d) == 1, "");

struct S1d {
  int : (1.0F + 0x0.000001p0F > 1.0F);
  int f;
};
static_assert(sizeof(S1d) == sizeof(int), "");