C-libs 0.1.0
Some C utils libraries
 
Loading...
Searching...
No Matches
util_macros.h File Reference

Utility macros for low-level systems and general C development. More...

Macros

#define SOURCE_PATH_SIZE   0
 Macro to truncate the root file path.
 
#define __FILENAME__   ((__FILE__) + (SOURCE_PATH_SIZE))
 Macro to adjust the file path based on SOURCE_PATH_SIZE.
 
#define LITERAL_I64(i)
 
#define LITERAL_U64(i)
 
#define I8_MAX   ((i8)(((u8)(~0)) >> 1))
 
#define I16_MAX   ((i16)(((u16)(~0)) >> 1))
 
#define I32_MAX   ((i32)(((u32)(~0)) >> 1))
 
#define I64_MAX   ((i64)(((u64)(~0)) >> 1))
 
#define I8_MIN   ((i8)(1 << (7)))
 
#define I16_MIN   ((i16)(1 << (15)))
 
#define I32_MIN   ((i32)(1 << (31)))
 
#define I64_MIN   ((i64)(1 << (63)))
 
#define U8_MAX   ((u8)(~0))
 
#define U16_MAX   ((u16)(~0))
 
#define U32_MAX   ((u32)(~0))
 
#define U64_MAX   ((u64)(~0))
 
#define PRI_I8   "hhi"
 
#define PRI_U8   "hhu"
 
#define PRI_I16   "hi"
 
#define PRI_U16   "hu"
 
#define PRI_I32   "i"
 
#define PRI_U32   "u"
 
#define PRI_I64   "lli"
 
#define PRI_U64   "llu"
 
#define MAX(a, b)
 
#define MIN(a, b)
 
#define CLAMP(val, min, max)
 
#define ABS(x)
 
#define DIFF(a, b)
 
#define SWAP(a, b)
 
#define PI   3.14159265358979323846
 
#define DEG2RAD(deg)
 
#define RAD2DEG(rad)
 
#define IS_NAN(x)
 
#define FLOAT_EPSILON   (1e-6f)
 
#define DOUBLE_EPSILON   (1e-12)
 
#define ROUND(x)
 
#define FLOOR(x)
 
#define CEIL(x)
 
#define FLOAT_ABS(x)
 
#define FLOAT_ALMOST_EQUALS(x, y)
 
#define DOUBLE_ALMOST_EQUALS(x, y)
 
#define FLOAT_RELATIVE_EQUAL(x, y)
 
#define DOUBLE_RELATIVE_EQUAL(x, y)
 
#define BIT(x)
 
#define SETBIT(x, p)
 
#define CLEARBIT(x, p)
 
#define GETBIT(x, p)
 
#define TOGGLEBIT(x, p)
 
#define __STR(s)
 
#define STR(s)
 
#define GLUE(a, b)
 
#define UNIQUE_TOKEN(name)
 
#define FOREVER   for (;;)
 
#define RANGE(begin, end)
 
#define RANGE_STEP(begin, end, step)
 
#define TRY_GOTO(pred, label)
 
#define TRY_THROW(pred)
 
#define TRY_LOG(pred, msg)
 
#define TRY_EXEC(pred, block)
 
#define DEFER(head, tail)
 Run tail after head in a single lexical scope (cleanup emulation).
 
#define STMT(stuff)
 Multi-statement macro wrapper.
 
#define SCOPE(block)
 Same as STMT but uses {} explicitly.
 
#define ONCE(stmts)
 Execute stmts once, ever.
 

Typedefs

typedef signed char i8
 8-bit signed integer
 
typedef unsigned char u8
 8-bit unsigned integer
 
typedef signed short int i16
 16-bit signed integer
 
typedef unsigned short int u16
 16-bit unsigned integer
 
typedef signed int i32
 32-bit signed integer
 
typedef unsigned int u32
 32-bit unsigned integer
 
typedef signed long long int i64
 64-bit signed integer
 
typedef unsigned long long int u64
 64-bit unsigned integer
 
typedef float f32
 32-bit float
 
typedef double f64
 64-bit float
 
typedef long double f80
 Extended precision float (usually 80-bit)
 
typedef __float128 f128
 128-bit float (GCC 4.3+ extension)
 

Detailed Description

Utility macros for low-level systems and general C development.

This header provides a collection of utility macros for:

  • Type aliases for fixed-width integers and floating point numbers
  • Bitwise operations
  • Mathematical helpers (min, max, clamp, etc.)
  • Endianness and architecture detection
  • Error handling and logging
  • Debug-friendly logging and scoped cleanups
  • Token manipulation and safe macros

Macro Definition Documentation

◆ __FILENAME__

#define __FILENAME__   ((__FILE__) + (SOURCE_PATH_SIZE))

Macro to adjust the file path based on SOURCE_PATH_SIZE.

This macro modifies the __FILE__ macro to remove a specified number of characters from the beginning, making the logged file paths more readable.

◆ DEFER

#define DEFER ( head,
tail )
Value:
for (int UNIQUE_TOKEN(defer_var) = (head, 0); !UNIQUE_TOKEN(defer_var); \
tail, UNIQUE_TOKEN(defer_var)++)
#define UNIQUE_TOKEN(name)
Definition util_macros.h:241

Run tail after head in a single lexical scope (cleanup emulation).

Example usage:

DEFER(open_file(), close_file()) {
// do work
}
#define DEFER(head, tail)
Run tail after head in a single lexical scope (cleanup emulation).
Definition util_macros.h:305
Note
Use continue instead of break when the scope need to be exited before (in error checking context)
See also
Please see https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2895.htm for a more in depth guide explaining non-standard cleanup functions

◆ ONCE

#define ONCE ( stmts)
Value:
do { \
static int UNIQUE_TOKEN(once_var) = 1; \
if (UNIQUE_TOKEN(once_var)) { \
STMT(stmts); \
UNIQUE_TOKEN(once_var) = 0; \
} \
} while (0)

Execute stmts once, ever.

Example usage:

printf("This happens once.\n");
);
#define ONCE(stmts)
Execute stmts once, ever.
Definition util_macros.h:334

◆ SCOPE

#define SCOPE ( block)
Value:
do \
block while (0)

Same as STMT but uses {} explicitly.

◆ SOURCE_PATH_SIZE

#define SOURCE_PATH_SIZE   0

Macro to truncate the root file path.

This macro is useful when the project is compiled with CMake, allowing for a cleaner file path in logs by removing a fixed number of characters from the start of the __FILE__ macro. The default value if the macro wasn't defined earlier is 0.

◆ STMT

#define STMT ( stuff)
Value:
do { \
stuff \
} while (0)

Multi-statement macro wrapper.