2 * (C) Copyright 2007-2019 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
4 * This file is released under the GPLv2. See the COPYING file for more
14 #define true ((_Bool) 1)
15 #define false ((_Bool) 0)
17 #define NULL ((void*) 0)
19 typedef unsigned char uint8_t;
20 typedef signed char int8_t;
22 typedef unsigned short uint16_t;
23 typedef signed short int16_t;
25 typedef unsigned int uint32_t;
26 typedef signed int int32_t;
29 typedef unsigned long uint64_t;
30 typedef signed long int64_t;
32 typedef uint64_t uintptr_t;
33 typedef int64_t intptr_t;
35 typedef unsigned long long uint64_t;
36 typedef signed long long int64_t;
38 typedef uint32_t uintptr_t;
39 typedef int32_t intptr_t;
40 typedef int32_t ptrdiff_t;
43 typedef uintptr_t size_t;
44 typedef intptr_t ssize_t
;
45 typedef intptr_t ptrdiff_t;
47 typedef __builtin_va_list
va_list;
65 * BIT*_SHIFT(x) calculates the amount of shift necessary for a shifted
66 * value's LSB to bit at bit position x.
68 * BIT*(x) produces a value with the bit at position x set and all other
71 #define BIT64_SHIFT(x) (63-(x))
72 #define BIT32_SHIFT(x) (31-(x))
73 #define BIT16_SHIFT(x) (15-(x))
74 #define BIT8_SHIFT(x) (7-(x))
76 #define BIT64(x) ((uint64_t) (1ull << BIT64_SHIFT(x)))
77 #define BIT32(x) (1u << BIT32_SHIFT(x))
78 #define BIT16(x) (1u << BIT16_SHIFT(x))
79 #define BIT8(x) (1u << BIT8_SHIFT(x))
82 * min/max/clamp/min_t/max_t/clamp_t borrowed from Linux
86 * min()/max()/clamp() macros that also do
87 * strict type-checking.. See the
88 * "unnecessary" pointer comparison.
90 #define min(x, y) ({ \
91 typeof(x) _min1 = (x); \
92 typeof(y) _min2 = (y); \
93 (void) (&_min1 == &_min2); \
94 _min1 < _min2 ? _min1 : _min2; })
96 #define max(x, y) ({ \
97 typeof(x) _max1 = (x); \
98 typeof(y) _max2 = (y); \
99 (void) (&_max1 == &_max2); \
100 _max1 > _max2 ? _max1 : _max2; })
103 * clamp - return a value clamped to a given range with strict typechecking
104 * @val: current value
105 * @min: minimum allowable value
106 * @max: maximum allowable value
108 * This macro does strict typechecking of min/max to make sure they are of the
109 * same type as val. See the unnecessary pointer comparisons.
111 #define clamp(val, min, max) ({ \
112 typeof(val) __val = (val); \
113 typeof(min) __min = (min); \
114 typeof(max) __max = (max); \
115 (void) (&__val == &__min); \
116 (void) (&__val == &__max); \
117 __val = __val < __min ? __min: __val; \
118 __val > __max ? __max: __val; })
121 * ..and if you can't take the strict
122 * types, you can specify one yourself.
124 * Or not use min/max/clamp at all, of course.
126 #define min_t(type, x, y) ({ \
129 __min1 < __min2 ? __min1: __min2; })
131 #define max_t(type, x, y) ({ \
134 __max1 > __max2 ? __max1: __max2; })
137 * clamp_t - return a value clamped to a given range using a given type
138 * @type: the type of variable to use
139 * @val: current value
140 * @min: minimum allowable value
141 * @max: maximum allowable value
143 * This macro does no typechecking and uses temporary variables of type
144 * 'type' to make all the comparisons.
146 #define clamp_t(type, val, min, max) ({ \
147 type __val = (val); \
148 type __min = (min); \
149 type __max = (max); \
150 __val = __val < __min ? __min: __val; \
151 __val > __max ? __max: __val; })