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