2 * (C) Copyright 2007-2010 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
4 * This file is released under the GPLv2. See the COPYING file for more
11 #define NULL ((void*) 0)
13 typedef unsigned long long u64
;
14 typedef signed long long s64
;
16 typedef unsigned int u32
;
17 typedef signed int s32
;
19 typedef unsigned short u16
;
20 typedef signed short s16
;
22 typedef unsigned char u8
;
23 typedef signed char s8
;
27 typedef __builtin_va_list
va_list;
29 typedef int ptrdiff_t; /* wha? well, vsnprintf wants it */
32 * min/max/clamp/min_t/max_t/clamp_t borrowed from Linux
36 * min()/max()/clamp() macros that also do
37 * strict type-checking.. See the
38 * "unnecessary" pointer comparison.
40 #define min(x, y) ({ \
41 typeof(x) _min1 = (x); \
42 typeof(y) _min2 = (y); \
43 (void) (&_min1 == &_min2); \
44 _min1 < _min2 ? _min1 : _min2; })
46 #define max(x, y) ({ \
47 typeof(x) _max1 = (x); \
48 typeof(y) _max2 = (y); \
49 (void) (&_max1 == &_max2); \
50 _max1 > _max2 ? _max1 : _max2; })
53 * clamp - return a value clamped to a given range with strict typechecking
55 * @min: minimum allowable value
56 * @max: maximum allowable value
58 * This macro does strict typechecking of min/max to make sure they are of the
59 * same type as val. See the unnecessary pointer comparisons.
61 #define clamp(val, min, max) ({ \
62 typeof(val) __val = (val); \
63 typeof(min) __min = (min); \
64 typeof(max) __max = (max); \
65 (void) (&__val == &__min); \
66 (void) (&__val == &__max); \
67 __val = __val < __min ? __min: __val; \
68 __val > __max ? __max: __val; })
71 * ..and if you can't take the strict
72 * types, you can specify one yourself.
74 * Or not use min/max/clamp at all, of course.
76 #define min_t(type, x, y) ({ \
79 __min1 < __min2 ? __min1: __min2; })
81 #define max_t(type, x, y) ({ \
84 __max1 > __max2 ? __max1: __max2; })
87 * clamp_t - return a value clamped to a given range using a given type
88 * @type: the type of variable to use
90 * @min: minimum allowable value
91 * @max: maximum allowable value
93 * This macro does no typechecking and uses temporary variables of type
94 * 'type' to make all the comparisons.
96 #define clamp_t(type, val, min, max) ({ \
100 __val = __val < __min ? __min: __val; \
101 __val > __max ? __max: __val; })