Commit 48c7ea39663a7c151a2a0b1af5d45ae3cf676299

Authored by Masahiro Yamada
Committed by Tom Rini
1 parent cba1da495d

linux/kernel.h: import more macros

These macros seem to be useful for U-Boot too (or at least
harmless).  Imported from Linux 3.18-rc2.

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

Showing 1 changed file with 92 additions and 0 deletions Side-by-side Diff

include/linux/kernel.h
... ... @@ -4,16 +4,41 @@
4 4  
5 5 #include <linux/types.h>
6 6  
  7 +#define USHRT_MAX ((u16)(~0U))
  8 +#define SHRT_MAX ((s16)(USHRT_MAX>>1))
  9 +#define SHRT_MIN ((s16)(-SHRT_MAX - 1))
7 10 #define INT_MAX ((int)(~0U>>1))
8 11 #define INT_MIN (-INT_MAX - 1)
  12 +#define UINT_MAX (~0U)
  13 +#define LONG_MAX ((long)(~0UL>>1))
  14 +#define LONG_MIN (-LONG_MAX - 1)
  15 +#define ULONG_MAX (~0UL)
9 16 #define LLONG_MAX ((long long)(~0ULL>>1))
  17 +#define LLONG_MIN (-LLONG_MAX - 1)
  18 +#define ULLONG_MAX (~0ULL)
  19 +#define SIZE_MAX (~(size_t)0)
10 20  
11 21 #define U8_MAX ((u8)~0U)
  22 +#define S8_MAX ((s8)(U8_MAX>>1))
  23 +#define S8_MIN ((s8)(-S8_MAX - 1))
  24 +#define U16_MAX ((u16)~0U)
  25 +#define S16_MAX ((s16)(U16_MAX>>1))
  26 +#define S16_MIN ((s16)(-S16_MAX - 1))
12 27 #define U32_MAX ((u32)~0U)
  28 +#define S32_MAX ((s32)(U32_MAX>>1))
  29 +#define S32_MIN ((s32)(-S32_MAX - 1))
13 30 #define U64_MAX ((u64)~0ULL)
  31 +#define S64_MAX ((s64)(U64_MAX>>1))
  32 +#define S64_MIN ((s64)(-S64_MAX - 1))
14 33  
  34 +#define STACK_MAGIC 0xdeadbeef
  35 +
  36 +#define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
  37 +
15 38 #define ALIGN(x,a) __ALIGN_MASK((x),(typeof(x))(a)-1)
16 39 #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
  40 +#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
  41 +#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
17 42  
18 43 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
19 44  
20 45  
21 46  
... ... @@ -27,10 +52,24 @@
27 52 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
28 53 #define round_down(x, y) ((x) & ~__round_mask(x, y))
29 54  
  55 +#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
30 56 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
31 57  
  58 +#if BITS_PER_LONG == 32
  59 +# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
  60 +#else
  61 +# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
  62 +#endif
  63 +
32 64 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
33 65  
  66 +#define rounddown(x, y) ( \
  67 +{ \
  68 + typeof(x) __x = (x); \
  69 + __x - (__x % (y)); \
  70 +} \
  71 +)
  72 +
34 73 /*
35 74 * Divide positive or negative dividend by positive divisor and round
36 75 * to closest integer. Result is undefined for negative divisors and
... ... @@ -127,6 +166,27 @@
127 166 _max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
128 167 (_max2 > _max3 ? _max2 : _max3); })
129 168  
  169 +/**
  170 + * min_not_zero - return the minimum that is _not_ zero, unless both are zero
  171 + * @x: value1
  172 + * @y: value2
  173 + */
  174 +#define min_not_zero(x, y) ({ \
  175 + typeof(x) __x = (x); \
  176 + typeof(y) __y = (y); \
  177 + __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
  178 +
  179 +/**
  180 + * clamp - return a value clamped to a given range with strict typechecking
  181 + * @val: current value
  182 + * @lo: lowest allowable value
  183 + * @hi: highest allowable value
  184 + *
  185 + * This macro does strict typechecking of lo/hi to make sure they are of the
  186 + * same type as val. See the unnecessary pointer comparisons.
  187 + */
  188 +#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
  189 +
130 190 /*
131 191 * ..and if you can't take the strict
132 192 * types, you can specify one yourself.
... ... @@ -142,6 +202,38 @@
142 202 type __max1 = (x); \
143 203 type __max2 = (y); \
144 204 __max1 > __max2 ? __max1: __max2; })
  205 +
  206 +/**
  207 + * clamp_t - return a value clamped to a given range using a given type
  208 + * @type: the type of variable to use
  209 + * @val: current value
  210 + * @lo: minimum allowable value
  211 + * @hi: maximum allowable value
  212 + *
  213 + * This macro does no typechecking and uses temporary variables of type
  214 + * 'type' to make all the comparisons.
  215 + */
  216 +#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
  217 +
  218 +/**
  219 + * clamp_val - return a value clamped to a given range using val's type
  220 + * @val: current value
  221 + * @lo: minimum allowable value
  222 + * @hi: maximum allowable value
  223 + *
  224 + * This macro does no typechecking and uses temporary variables of whatever
  225 + * type the input argument 'val' is. This is useful when val is an unsigned
  226 + * type and min and max are literals that will otherwise be assigned a signed
  227 + * integer type.
  228 + */
  229 +#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
  230 +
  231 +
  232 +/*
  233 + * swap - swap value of @a and @b
  234 + */
  235 +#define swap(a, b) \
  236 + do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
145 237  
146 238 /**
147 239 * container_of - cast a member of a structure out to the containing structure