-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
51 lines (44 loc) · 1.17 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* File: utils.h
* Author: torsten.roemer@luniks.net
*
* Created on 07. February 2025, 00:25
*/
#ifndef UTILS_H
#define UTILS_H
/**
* Computes the length of the given array.
*/
#define array_length(array) (sizeof(array) / sizeof(array[0]))
/**
* Returns the greater of both given numbers.
* https://stackoverflow.com/questions/3437404/min-and-max-in-c
*/
#define max(a, b) \
({ \
__typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; \
})
/**
* Returns the smaller of both given numbers.
* https://stackoverflow.com/questions/3437404/min-and-max-in-c
*/
#define min(a, b) \
({ \
__typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; \
})
/**
* Divides the given numerator by the given denominator,
* rounds to the nearest int and returns it.
* http://stackoverflow.com/a/18067292/709426
*/
#define divRoundNearest(num, den) \
({ \
return ((num < 0) ^ (den < 0)) ? \
((num - den / 2) / den) : \
((num + den / 2) / den); \
})
#endif /* UTILS_H */