xref: /llvm-project/clang/test/SemaCXX/rounding-math.cpp (revision e620035a28d5d957623aa7b4aeda35ab5130e2c9)
1 // RUN: %clang_cc1 -triple x86_64-linux -verify=norounding -Wno-unknown-pragmas %s
2 // RUN: %clang_cc1 -triple x86_64-linux -verify=rounding %s -frounding-math -Wno-unknown-pragmas
3 // RUN: %clang_cc1 -triple x86_64-linux -verify=rounding %s -frounding-math -fexperimental-new-constant-interpreter -Wno-unknown-pragmas
4 // rounding-no-diagnostics
5 
6 #define fold(x) (__builtin_constant_p(x) ? (x) : (x))
7 
8 constexpr double a = 1.0 / 3.0;
9 
f(int n)10 constexpr int f(int n) { return int(n * (1.0 / 3.0)); }
11 
12 using T = int[f(3)];
13 using T = int[1];
14 
15 enum Enum { enum_a = f(3) };
16 
17 struct Bitfield {
18   unsigned int n : 1;
19   unsigned int m : f(3);
20 };
21 
f(Bitfield & b)22 void f(Bitfield &b) {
23   b.n = int(6 * (1.0 / 3.0)); // norounding-warning {{changes value from 2 to 0}}
24 }
25 
26 const int k = 3 * (1.0 / 3.0);
27 static_assert(k == 1, "");
28 
g()29 void g() {
30   // FIXME: Constant-evaluating this initializer is surprising, and violates
31   // the recommended practice in C++ [expr.const]p12:
32   //
33   //   Implementations should provide consistent results of floating-point
34   //   evaluations, irrespective of whether the evaluation is performed during
35   //   translation or during program execution.
36   const int k = 3 * (1.0 / 3.0);
37   static_assert(k == 1, "");
38 }
39 
h()40 int *h() {
41   return new int[int(-3 * (1.0 / 3.0))]; // norounding-error {{too large}}
42 }
43 
44 
45 // nextUp(1.F) == 0x1.000002p0F
46 static_assert(1.0F + 0x0.000001p0F == 0x1.0p0F, "");
47 
48 char Arr01[1 + (1.0F + 0x0.000001p0F > 1.0F)];
49 static_assert(sizeof(Arr01) == 1, "");
50 
51 struct S1 {
52   int : (1.0F + 0x0.000001p0F > 1.0F);
53   int f;
54 };
55 static_assert(sizeof(S1) == sizeof(int), "");
56 
57 #pragma STDC FENV_ROUND FE_UPWARD
58 static_assert(1.0F + 0x0.000001p0F == 0x1.000002p0F, "");
59 
60 char Arr01u[1 + (1.0F + 0x0.000001p0F > 1.0F)];
61 static_assert(sizeof(Arr01u) == 2, "");
62 
63 struct S1u {
64   int : (1.0F + 0x0.000001p0F > 1.0F);
65   int f;
66 };
67 static_assert(sizeof(S1u) > sizeof(int), "");
68 
69 #pragma STDC FENV_ROUND FE_DOWNWARD
70 static_assert(1.0F + 0x0.000001p0F == 1.0F, "");
71 
72 char Arr01d[1 + (1.0F + 0x0.000001p0F > 1.0F)];
73 static_assert(sizeof(Arr01d) == 1, "");
74 
75 struct S1d {
76   int : (1.0F + 0x0.000001p0F > 1.0F);
77   int f;
78 };
79 static_assert(sizeof(S1d) == sizeof(int), "");
80 
incr_down(float k)81 constexpr float incr_down(float k) {
82   float x = k;
83   ++x;
84   return x;
85 }
86 
87 // 0x1.0p23 = 8388608.0, inc(8388608.0) = 8388609.0
88 static_assert(incr_down(0x1.0p23F) == 0x1.000002p23F, "");
89 // 0x1.0p24 = 16777216.0, inc(16777216.0) = 16777217.0 -> round down -> 16777216.0
90 static_assert(incr_down(0x1.0p24F) == 0x1.0p24F, "");
91 
92 #pragma STDC FENV_ROUND FE_UPWARD
incr_up(float k)93 constexpr float incr_up(float k) {
94   float x = k;
95   ++x;
96   return x;
97 }
98 static_assert(incr_up(0x1.0p23F) == 0x1.000002p23F, "");
99 // 0x1.0p24 = 16777216.0, inc(16777216.0) = 16777217.0 -> round up -> 16777218.0
100 static_assert(incr_up(0x1.0p24F) == 0x1.000002p24F, "");
101