Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/compiler_hints: add assume() hint #19354

Merged
merged 2 commits into from
Apr 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion 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 @@ -89,7 +90,7 @@ extern "C" {
* Use this if the compiler cannot tell that e.g.
* an assembler instruction causes a longjmp, or a write causes a reboot.
*/
#if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ >= 5)
#if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ >= 5) || defined(__clang__)
#define UNREACHABLE() __builtin_unreachable()
#else
#define UNREACHABLE() do { /* nothing */ } while (1)
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 : UNREACHABLE())
#else
#define assume(cond) assert(cond)
#endif

#ifdef __cplusplus
}
#endif
Expand Down