libdragon
Loading...
Searching...
No Matches
Macros
utils.h File Reference

Misc utilities functions and macros. More...

Go to the source code of this file.

Macros

#define SWAP(a, b)   ({ typeof(a) t = a; a = b; b = t; })
 Swap two values.
 
#define MAX(a, b)   ({ typeof(a) _a = a; typeof(b) _b = b; _a > _b ? _a : _b; })
 Return the maximum of two values.
 
#define MIN(a, b)   ({ typeof(a) _a = a; typeof(b) _b = b; _a < _b ? _a : _b; })
 Return the minimum of two values.
 
#define CLAMP(x, min, max)   (MIN(MAX((x), (min)), (max)))
 Clamp a value between min and max.
 
#define ROUND_UP(n, d)
 Round n up to the next multiple of d.
 
#define ROUND_DOWN(n, d)
 Round n down to the previous multiple of d.
 
#define DIVIDE_CEIL(n, d)
 Return the ceil of n/d.
 
#define ABS(x)
 Absolute number.
 
#define F2I(f)   ({ uint32_t __i; memcpy(&__i, &(f), 4); __i; })
 Type-safe bitcast from float to integer.
 
#define I2F(i)   ({ float __f; memcpy(&__f, &(i), 4); __f; })
 Type-safe bitcast from integer to float.
 
#define LIKELY(cond)   __builtin_expect(!!(cond), 1)
 Hint for the compiler that the condition is likely to happen.
 
#define UNLIKELY(cond)   __builtin_expect(!!(cond), 0)
 Hint for the compiler that the condition is unlikely to happen.
 

Detailed Description

Misc utilities functions and macros.

Author
Giovanni Bajo giova.nosp@m.nnib.nosp@m.ajo@g.nosp@m.mail.nosp@m..com

Macro Definition Documentation

◆ ROUND_UP

#define ROUND_UP (   n,
 
)
Value:
({ \
typeof(n) _n = n; typeof(d) _d = d; \
(((_n) + (_d) - 1) / (_d) * (_d)); \
})

Round n up to the next multiple of d.

◆ ROUND_DOWN

#define ROUND_DOWN (   n,
 
)
Value:
({ \
typeof(n) _n = n; typeof(d) _d = d; \
((_n >= 0) ? ((_n) / (_d) * (_d)) : -(((-_n + (_d) - 1) / (_d)) * (_d))); \
})

Round n down to the previous multiple of d.

◆ DIVIDE_CEIL

#define DIVIDE_CEIL (   n,
 
)
Value:
({ \
typeof(n) _n = n; typeof(d) _d = d; \
((_n) + (_d) - 1) / (_d); \
})

Return the ceil of n/d.

◆ ABS

#define ABS (   x)
Value:
({ \
typeof(x) _x = x; \
(_x < 0 ? -_x : _x); \
})

Absolute number.