Skip to content

Commit

Permalink
core/compiler_hints: add assume() hint
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Mar 6, 2023
1 parent bc517b5 commit ee6ffd9
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions core/lib/include/compiler_hints.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#ifndef COMPILER_HINTS_H
#define COMPILER_HINTS_H

#include <assert.h>
#include <stdint.h>

#ifdef __cplusplus
Expand Down Expand Up @@ -164,6 +165,22 @@ extern "C" {
*/
#define unlikely(x) __builtin_expect((uintptr_t)(x), 0)

/**
* @brief Behaves like an `assert()`, but tells the compiler that @p cond can
* never be false.
* This allows the compiler to optimize the code accordingly even when
* `NDEBUG` is set / with `DEVELHELP=0`.
*
* @p cond being false will result in undefined behavior.
*
* @param[in] cond Condition that is guaranteed to be true
*/
#ifdef NDEBUG
#define assume(cond) ((cond) ? (void)0 : __builtin_unreachable())
#else
#define assume(cond) assert(cond)
#endif

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit ee6ffd9

Please sign in to comment.