Questions tagged [assumption]

6 questions
13
votes
2 answers

What happens when an assumption, i.e. [[assume]] contains UB?

In C++23, the [[assume(expression)]] attribute makes it so that if expression is false, the behavior is undefined. For example: int div(int x, int y) { [[assume(y == 1)]]; return x / y; } This compiles to the same code as if y was always…
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
6
votes
1 answer

Why is [[assume]] not evaluated but also potentially evaluated?

The cppreference page for [[assume]] says that: [[assume( expression )]] [...] the expression is not evaluated (but it is still potentially evaluated). This wording confuses me. Is cppreference wrong here? Why would it be potentially evaluated…
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
5
votes
3 answers

Reproducing clang's __builtin_assume for GCC

Recently, I discovered void __builtin_assume(bool) for clang, which can provide additional information about the state of the program to the compiler. This can make a huge difference, like for example: #include // compiles to about 80…
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
1
vote
2 answers

What happens if an assumption, i.e. [[assume]] fails in a constant expression?

In C++23, the [[assume(conditonal-expression)]] attribute makes it so that if conditional-expression doesn't evaluate to true, the behavior is undefined. For example: int div(int x, int y) { [[assume(y == 1)]]; return x / y; } This compiles…
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
1
vote
0 answers

Is it possible to make MSVC's __assume(0) aka std::unreachable() actually optimize?

Compiling the following code with MSVC #include #include static auto bit_width(unsigned long x) { unsigned long i; _BitScanReverse(&i, x); ++i; if (x == 0) { i = 0; } return i; } auto foo(unsigned long x)…
user541686
  • 205,094
  • 128
  • 528
  • 886