Files
archived-ballistic/include/bal_attributes.h
Ronald Caesar 0b0f4bab98 engine: add __restrict__ compiler attribute
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-01-13 18:28:39 -04:00

93 lines
1.6 KiB
C

/** @file bal_attributes.h
*
* @brief Compiler specific attribute macros.
*/
#ifndef BALLISTIC_ATTRIBUTES_H
#define BALLISTIC_ATTRIBUTES_H
/*!
* BAL_HOT()/BAL_COLD()
* Marks a function as hot or cold. Hot makes the compiller optimize it more
* aggressively. Cold marks the function as rarely executed.
*
* Usage:
* BAL_HOT bal_error_t emit_instruction(...);
*/
#if defined(__GNUC__) || defined(__clang__)
#define BAL_HOT __attribute__((hot))
#define BAL_COLD __attribute__((cold))
#else
#define BAL_HOT
#define BAL_COLD
#endif
/*!
* BAL_LIKELY(x)/BAL_UNLIKELY(x)
* Hints to the CPU branch predictor. Should only be used in hot functions.
*
* Usage: if (BAL_UNLIKELY(ptr == NULL)) { ... }
*/
#if defined(__GNUC__) || defined(__clang__)
#define BAL_LIKELY(x) __builtin_expect(!!(x), 1)
#define BAL_UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
#define BAL_LIKELY(x) (x)
#define BAL_UNLIKELY(x) (x)
#endif
/*!
* BAL_ALIGNED(x)
* Aligns a variable or a structure to x bytes.
*
* Usage: BAL_ALIGNED(64) struct data { ... };
*/
#if defined(__GNUC__) || defined(__clang__)
#define BAL_ALIGNED(x) __attribute__((aligned(x)))
#elif defined(_MSC_VER)
#define BAL_ALIGNED(x) __declspec(align(x))
#else
#define BAL_ALIGNED(x)
#endif
/*!
* BAL_RESTRICT:
* Tells the compiler that a pointer does not alias any other pointer in
* current scope.
*/
#if defined(__GNUC__) || defined(__clang__)
#define BAL_RESTRICT __restrict__
#elif defined(_MSC_VER)
#define BAL_RESTRICT __restrict
#else
#define BAL_RESTRICT
#endif
#endif /* BALLISTIC_ATTRIBUTES_H */
/*** end of file ***/