libdragon
Loading...
Searching...
No Matches
utils.h
1#ifndef __LIBDRAGON_UTILS_H
2#define __LIBDRAGON_UTILS_H
3
4#include <string.h> // memcpy
5
10#define SWAP(a, b) ({ typeof(a) t = a; a = b; b = t; })
11
12#define MAX(a,b) ({ typeof(a) _a = a; typeof(b) _b = b; _a > _b ? _a : _b; })
13#define MIN(a,b) ({ typeof(a) _a = a; typeof(b) _b = b; _a < _b ? _a : _b; })
14#define CLAMP(x, min, max) (MIN(MAX((x), (min)), (max)))
15
17#define ROUND_UP(n, d) ({ \
18 typeof(n) _n = n; typeof(d) _d = d; \
19 (((_n) + (_d) - 1) / (_d) * (_d)); \
20})
21
23#define ROUND_DOWN(n, d) ({ \
24 typeof(n) _n = n; typeof(d) _d = d; \
25 ((_n >= 0) ? ((_n) / (_d) * (_d)) : -(((-_n + (_d) - 1) / (_d)) * (_d))); \
26})
27
29#define DIVIDE_CEIL(n, d) ({ \
30 typeof(n) _n = n; typeof(d) _d = d; \
31 ((_n) + (_d) - 1) / (_d); \
32})
33
35#define ABS(x) ({ \
36 typeof(x) _x = x; \
37 (_x < 0 ? -_x : _x); \
38})
39
41#define F2I(f) ({ uint32_t __i; memcpy(&__i, &(f), 4); __i; })
42
44#define I2F(i) ({ float __f; memcpy(&__f, &(i), 4); __f; })
45
47#define LIKELY(cond) __builtin_expect(!!(cond), 1)
49#define UNLIKELY(cond) __builtin_expect(!!(cond), 0)
50
51#endif