| 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 |
#ifndef _USUAL_BITS_H_ |
| 27 |
#define _USUAL_BITS_H_ |
| 28 |
|
| 29 |
#include <usual/base.h> |
| 30 |
|
| 31 |
#include <string.h> |
| 32 |
#include <limits.h> |
| 33 |
|
| 34 |
|
| 35 |
static inline bool is_power_of_2(unsigned int n) |
| 36 |
{ |
| 37 |
return (n > 0) && !(n & (n - 1)); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
static inline uint16_t rol16(uint16_t v, int s) |
| 46 |
{ |
| 47 |
return (v << s) | (v >> (16 - s)); |
| 48 |
} |
| 49 |
|
| 50 |
static inline uint32_t rol32(uint32_t v, int s) |
| 51 |
{ |
| 52 |
return (v << s) | (v >> (32 - s)); |
| 53 |
} |
| 54 |
|
| 55 |
static inline uint64_t rol64(uint64_t v, int s) |
| 56 |
{ |
| 57 |
return (v << s) | (v >> (64 - s)); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
static inline uint16_t ror16(uint16_t v, int s) { return rol16(v, 16 - s); } |
| 62 |
|
| 63 |
|
| 64 |
static inline uint32_t ror32(uint32_t v, int s) { return rol32(v, 32 - s); } |
| 65 |
|
| 66 |
|
| 67 |
static inline uint64_t ror64(uint64_t v, int s) { return rol64(v, 64 - s); } |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
#undef fls |
| 78 |
#undef flsl |
| 79 |
#undef flsll |
| 80 |
#define fls(x) usual_fls(x) |
| 81 |
#define flsl(x) usual_flsl(x) |
| 82 |
#define flsll(x) usual_flsll(x) |
| 83 |
|
| 84 |
#if _COMPILER_GNUC(4,0) || __has_builtin(__builtin_clzll) |
| 85 |
#define _USUAL_FLS_(sfx, type) \ |
| 86 |
return (x == 0) ? 0 : ((8*sizeof(type)) - __builtin_clz ## sfx(x)) |
| 87 |
#else |
| 88 |
#define _USUAL_FLS_(sfx, type) \ |
| 89 |
unsigned type u = x; \ |
| 90 |
unsigned int bit; \ |
| 91 |
if (x == 0) return 0; \ |
| 92 |
\ |
| 93 |
for (bit = 1; u > 1; bit++) u >>= 1; \ |
| 94 |
return bit |
| 95 |
#endif |
| 96 |
|
| 97 |
|
| 98 |
static inline int fls(int x) |
| 99 |
{ |
| 100 |
_USUAL_FLS_(, int); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
static inline int flsl(long x) |
| 105 |
{ |
| 106 |
_USUAL_FLS_(l, long); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
static inline int flsll(long long x) |
| 111 |
{ |
| 112 |
_USUAL_FLS_(ll, long long); |
| 113 |
} |
| 114 |
|
| 115 |
#undef _USUAL_FLS_ |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
#undef ffs |
| 126 |
#undef ffsl |
| 127 |
#undef ffsll |
| 128 |
#define ffs(x) usual_ffs(x) |
| 129 |
#define ffsl(x) usual_ffsl(x) |
| 130 |
#define ffsll(x) usual_ffsll(x) |
| 131 |
|
| 132 |
#if _COMPILER_GNUC(4,0) || __has_builtin(__builtin_ffsll) |
| 133 |
#define _USUAL_FFS_(sfx, type) \ |
| 134 |
return __builtin_ffs ## sfx((unsigned type)(x)) |
| 135 |
#else |
| 136 |
#define _USUAL_FFS_(sfx, type) \ |
| 137 |
unsigned int bit; \ |
| 138 |
unsigned type u = x; \ |
| 139 |
if (!x) return 0; \ |
| 140 |
\ |
| 141 |
for (bit = 1; !(u & 1); bit++) u >>= 1; \ |
| 142 |
return bit |
| 143 |
#endif |
| 144 |
|
| 145 |
|
| 146 |
static inline int ffs(int x) |
| 147 |
{ |
| 148 |
_USUAL_FFS_(, int); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
static inline int ffsl(long x) |
| 153 |
{ |
| 154 |
_USUAL_FFS_(l, long); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
static inline int ffsll(long long x) |
| 159 |
{ |
| 160 |
_USUAL_FFS_(ll, long long); |
| 161 |
} |
| 162 |
|
| 163 |
#undef _USUAL_FFS_ |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
#define _USUAL_MUL_SAFE_(type, max) \ |
| 170 |
type unsafe = (type)(1) << (sizeof(type) * 8/2); \ |
| 171 |
if (a < unsafe && b < unsafe) \ |
| 172 |
goto safe; \ |
| 173 |
if (!a || !b) \ |
| 174 |
goto safe; \ |
| 175 |
if ((max / a) >= b) \ |
| 176 |
goto safe; \ |
| 177 |
return false; \ |
| 178 |
safe: \ |
| 179 |
*res_p = a * b; \ |
| 180 |
return true; |
| 181 |
|
| 182 |
|
| 183 |
static inline bool safe_mul_uint(unsigned int *res_p, unsigned int a, unsigned int b) |
| 184 |
{ |
| 185 |
_USUAL_MUL_SAFE_(unsigned int, UINT_MAX); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
static inline bool safe_mul_ulong(unsigned long *res_p, unsigned long a, unsigned long b) |
| 190 |
{ |
| 191 |
_USUAL_MUL_SAFE_(unsigned long, ULONG_MAX); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
static inline bool safe_mul_uint8(uint8_t *res_p, uint8_t a, uint8_t b) |
| 196 |
{ |
| 197 |
_USUAL_MUL_SAFE_(uint8_t, UINT8_MAX); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
static inline bool safe_mul_uint16(uint16_t *res_p, uint16_t a, uint16_t b) |
| 202 |
{ |
| 203 |
_USUAL_MUL_SAFE_(uint16_t, UINT16_MAX); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
static inline bool safe_mul_uint32(uint32_t *res_p, uint32_t a, uint32_t b) |
| 208 |
{ |
| 209 |
_USUAL_MUL_SAFE_(uint32_t, UINT32_MAX); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
static inline bool safe_mul_uint64(uint64_t *res_p, uint64_t a, uint64_t b) |
| 214 |
{ |
| 215 |
_USUAL_MUL_SAFE_(uint64_t, UINT64_MAX); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
static inline bool safe_mul_size(size_t *res_p, size_t a, size_t b) |
| 220 |
{ |
| 221 |
_USUAL_MUL_SAFE_(size_t, SIZE_MAX); |
| 222 |
} |
| 223 |
|
| 224 |
#undef _USUAL_MUL_SAFE_ |
| 225 |
|
| 226 |
#endif |