libdragon
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1
6#ifndef __LIBDRAGON_UTILS_H
7#define __LIBDRAGON_UTILS_H
8
9#include <string.h> // memcpy
10
12#define SWAP(a, b) ({ typeof(a) t = a; a = b; b = t; })
13
15#define MAX(a,b) ({ typeof(a) _a = a; typeof(b) _b = b; _a > _b ? _a : _b; })
17#define MIN(a,b) ({ typeof(a) _a = a; typeof(b) _b = b; _a < _b ? _a : _b; })
19#define CLAMP(x, min, max) (MIN(MAX((x), (min)), (max)))
20
22#define ROUND_UP(n, d) ({ \
23 typeof(n) _n = n; typeof(d) _d = d; \
24 (((_n) + (_d) - 1) / (_d) * (_d)); \
25})
26
28#define ROUND_DOWN(n, d) ({ \
29 typeof(n) _n = n; typeof(d) _d = d; \
30 ((_n >= 0) ? ((_n) / (_d) * (_d)) : -(((-_n + (_d) - 1) / (_d)) * (_d))); \
31})
32
34#define DIVIDE_CEIL(n, d) ({ \
35 typeof(n) _n = n; typeof(d) _d = d; \
36 ((_n) + (_d) - 1) / (_d); \
37})
38
40#define ABS(x) ({ \
41 typeof(x) _x = x; \
42 (_x < 0 ? -_x : _x); \
43})
44
46#define F2I(f) ({ uint32_t __i; memcpy(&__i, &(f), 4); __i; })
47
49#define I2F(i) ({ float __f; memcpy(&__f, &(i), 4); __f; })
50
52#define LIKELY(cond) __builtin_expect(!!(cond), 1)
54#define UNLIKELY(cond) __builtin_expect(!!(cond), 0)
55
56#endif