cutils.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * C utilities
  3. *
  4. * Copyright (c) 2017 Fabrice Bellard
  5. * Copyright (c) 2018 Charlie Gordon
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #ifndef CUTILS_H
  26. #define CUTILS_H
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <inttypes.h>
  30. #define likely(x) __builtin_expect(!!(x), 1)
  31. #define unlikely(x) __builtin_expect(!!(x), 0)
  32. #define force_inline inline __attribute__((always_inline))
  33. #define no_inline __attribute__((noinline))
  34. #define __maybe_unused __attribute__((unused))
  35. #define xglue(x, y) x ## y
  36. #define glue(x, y) xglue(x, y)
  37. #define stringify(s) tostring(s)
  38. #define tostring(s) #s
  39. #ifndef offsetof
  40. #define offsetof(type, field) ((size_t) &((type *)0)->field)
  41. #endif
  42. #ifndef countof
  43. #define countof(x) (sizeof(x) / sizeof((x)[0]))
  44. #endif
  45. #ifndef container_of
  46. /* return the pointer of type 'type *' containing 'ptr' as field 'member' */
  47. #define container_of(ptr, type, member) ((type *)((uint8_t *)(ptr) - offsetof(type, member)))
  48. #endif
  49. #if !defined(_MSC_VER) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  50. #define minimum_length(n) static n
  51. #else
  52. #define minimum_length(n) n
  53. #endif
  54. typedef int BOOL;
  55. #ifndef FALSE
  56. enum {
  57. FALSE = 0,
  58. TRUE = 1,
  59. };
  60. #endif
  61. void pstrcpy(char *buf, int buf_size, const char *str);
  62. char *pstrcat(char *buf, int buf_size, const char *s);
  63. int strstart(const char *str, const char *val, const char **ptr);
  64. int has_suffix(const char *str, const char *suffix);
  65. /* Prevent UB when n == 0 and (src == NULL or dest == NULL) */
  66. static inline void memcpy_no_ub(void *dest, const void *src, size_t n) {
  67. if (n)
  68. memcpy(dest, src, n);
  69. }
  70. static inline int max_int(int a, int b)
  71. {
  72. if (a > b)
  73. return a;
  74. else
  75. return b;
  76. }
  77. static inline int min_int(int a, int b)
  78. {
  79. if (a < b)
  80. return a;
  81. else
  82. return b;
  83. }
  84. static inline uint32_t max_uint32(uint32_t a, uint32_t b)
  85. {
  86. if (a > b)
  87. return a;
  88. else
  89. return b;
  90. }
  91. static inline uint32_t min_uint32(uint32_t a, uint32_t b)
  92. {
  93. if (a < b)
  94. return a;
  95. else
  96. return b;
  97. }
  98. static inline int64_t max_int64(int64_t a, int64_t b)
  99. {
  100. if (a > b)
  101. return a;
  102. else
  103. return b;
  104. }
  105. static inline int64_t min_int64(int64_t a, int64_t b)
  106. {
  107. if (a < b)
  108. return a;
  109. else
  110. return b;
  111. }
  112. /* WARNING: undefined if a = 0 */
  113. static inline int clz32(unsigned int a)
  114. {
  115. return __builtin_clz(a);
  116. }
  117. /* WARNING: undefined if a = 0 */
  118. static inline int clz64(uint64_t a)
  119. {
  120. return __builtin_clzll(a);
  121. }
  122. /* WARNING: undefined if a = 0 */
  123. static inline int ctz32(unsigned int a)
  124. {
  125. return __builtin_ctz(a);
  126. }
  127. /* WARNING: undefined if a = 0 */
  128. static inline int ctz64(uint64_t a)
  129. {
  130. return __builtin_ctzll(a);
  131. }
  132. struct __attribute__((packed)) packed_u64 {
  133. uint64_t v;
  134. };
  135. struct __attribute__((packed)) packed_u32 {
  136. uint32_t v;
  137. };
  138. struct __attribute__((packed)) packed_u16 {
  139. uint16_t v;
  140. };
  141. static inline uint64_t get_u64(const uint8_t *tab)
  142. {
  143. return ((const struct packed_u64 *)tab)->v;
  144. }
  145. static inline int64_t get_i64(const uint8_t *tab)
  146. {
  147. return (int64_t)((const struct packed_u64 *)tab)->v;
  148. }
  149. static inline void put_u64(uint8_t *tab, uint64_t val)
  150. {
  151. ((struct packed_u64 *)tab)->v = val;
  152. }
  153. static inline uint32_t get_u32(const uint8_t *tab)
  154. {
  155. return ((const struct packed_u32 *)tab)->v;
  156. }
  157. static inline int32_t get_i32(const uint8_t *tab)
  158. {
  159. return (int32_t)((const struct packed_u32 *)tab)->v;
  160. }
  161. static inline void put_u32(uint8_t *tab, uint32_t val)
  162. {
  163. ((struct packed_u32 *)tab)->v = val;
  164. }
  165. static inline uint32_t get_u16(const uint8_t *tab)
  166. {
  167. return ((const struct packed_u16 *)tab)->v;
  168. }
  169. static inline int32_t get_i16(const uint8_t *tab)
  170. {
  171. return (int16_t)((const struct packed_u16 *)tab)->v;
  172. }
  173. static inline void put_u16(uint8_t *tab, uint16_t val)
  174. {
  175. ((struct packed_u16 *)tab)->v = val;
  176. }
  177. static inline uint32_t get_u8(const uint8_t *tab)
  178. {
  179. return *tab;
  180. }
  181. static inline int32_t get_i8(const uint8_t *tab)
  182. {
  183. return (int8_t)*tab;
  184. }
  185. static inline void put_u8(uint8_t *tab, uint8_t val)
  186. {
  187. *tab = val;
  188. }
  189. #ifndef bswap16
  190. static inline uint16_t bswap16(uint16_t x)
  191. {
  192. return (x >> 8) | (x << 8);
  193. }
  194. #endif
  195. #ifndef bswap32
  196. static inline uint32_t bswap32(uint32_t v)
  197. {
  198. return ((v & 0xff000000) >> 24) | ((v & 0x00ff0000) >> 8) |
  199. ((v & 0x0000ff00) << 8) | ((v & 0x000000ff) << 24);
  200. }
  201. #endif
  202. #ifndef bswap64
  203. static inline uint64_t bswap64(uint64_t v)
  204. {
  205. return ((v & ((uint64_t)0xff << (7 * 8))) >> (7 * 8)) |
  206. ((v & ((uint64_t)0xff << (6 * 8))) >> (5 * 8)) |
  207. ((v & ((uint64_t)0xff << (5 * 8))) >> (3 * 8)) |
  208. ((v & ((uint64_t)0xff << (4 * 8))) >> (1 * 8)) |
  209. ((v & ((uint64_t)0xff << (3 * 8))) << (1 * 8)) |
  210. ((v & ((uint64_t)0xff << (2 * 8))) << (3 * 8)) |
  211. ((v & ((uint64_t)0xff << (1 * 8))) << (5 * 8)) |
  212. ((v & ((uint64_t)0xff << (0 * 8))) << (7 * 8));
  213. }
  214. #endif
  215. /* XXX: should take an extra argument to pass slack information to the caller */
  216. typedef void *DynBufReallocFunc(void *opaque, void *ptr, size_t size);
  217. typedef struct DynBuf {
  218. uint8_t *buf;
  219. size_t size;
  220. size_t allocated_size;
  221. BOOL error; /* true if a memory allocation error occurred */
  222. DynBufReallocFunc *realloc_func;
  223. void *opaque; /* for realloc_func */
  224. } DynBuf;
  225. void dbuf_init(DynBuf *s);
  226. void dbuf_init2(DynBuf *s, void *opaque, DynBufReallocFunc *realloc_func);
  227. int dbuf_claim(DynBuf *s, size_t len);
  228. int dbuf_put(DynBuf *s, const uint8_t *data, size_t len);
  229. int dbuf_put_self(DynBuf *s, size_t offset, size_t len);
  230. int dbuf_putstr(DynBuf *s, const char *str);
  231. int __dbuf_putc(DynBuf *s, uint8_t c);
  232. int __dbuf_put_u16(DynBuf *s, uint16_t val);
  233. int __dbuf_put_u32(DynBuf *s, uint32_t val);
  234. int __dbuf_put_u64(DynBuf *s, uint64_t val);
  235. static inline int dbuf_putc(DynBuf *s, uint8_t val)
  236. {
  237. if (unlikely((s->allocated_size - s->size) < 1)) {
  238. return __dbuf_putc(s, val);
  239. } else {
  240. s->buf[s->size++] = val;
  241. return 0;
  242. }
  243. }
  244. static inline int dbuf_put_u16(DynBuf *s, uint16_t val)
  245. {
  246. if (unlikely((s->allocated_size - s->size) < 2)) {
  247. return __dbuf_put_u16(s, val);
  248. } else {
  249. put_u16(s->buf + s->size, val);
  250. s->size += 2;
  251. return 0;
  252. }
  253. }
  254. static inline int dbuf_put_u32(DynBuf *s, uint32_t val)
  255. {
  256. if (unlikely((s->allocated_size - s->size) < 4)) {
  257. return __dbuf_put_u32(s, val);
  258. } else {
  259. put_u32(s->buf + s->size, val);
  260. s->size += 4;
  261. return 0;
  262. }
  263. }
  264. static inline int dbuf_put_u64(DynBuf *s, uint64_t val)
  265. {
  266. if (unlikely((s->allocated_size - s->size) < 8)) {
  267. return __dbuf_put_u64(s, val);
  268. } else {
  269. put_u64(s->buf + s->size, val);
  270. s->size += 8;
  271. return 0;
  272. }
  273. }
  274. int __attribute__((format(printf, 2, 3))) dbuf_printf(DynBuf *s,
  275. const char *fmt, ...);
  276. void dbuf_free(DynBuf *s);
  277. static inline BOOL dbuf_error(DynBuf *s) {
  278. return s->error;
  279. }
  280. static inline void dbuf_set_error(DynBuf *s)
  281. {
  282. s->error = TRUE;
  283. }
  284. #define UTF8_CHAR_LEN_MAX 6
  285. int unicode_to_utf8(uint8_t *buf, unsigned int c);
  286. int unicode_from_utf8(const uint8_t *p, int max_len, const uint8_t **pp);
  287. static inline BOOL is_surrogate(uint32_t c)
  288. {
  289. return (c >> 11) == (0xD800 >> 11); // 0xD800-0xDFFF
  290. }
  291. static inline BOOL is_hi_surrogate(uint32_t c)
  292. {
  293. return (c >> 10) == (0xD800 >> 10); // 0xD800-0xDBFF
  294. }
  295. static inline BOOL is_lo_surrogate(uint32_t c)
  296. {
  297. return (c >> 10) == (0xDC00 >> 10); // 0xDC00-0xDFFF
  298. }
  299. static inline uint32_t get_hi_surrogate(uint32_t c)
  300. {
  301. return (c >> 10) - (0x10000 >> 10) + 0xD800;
  302. }
  303. static inline uint32_t get_lo_surrogate(uint32_t c)
  304. {
  305. return (c & 0x3FF) | 0xDC00;
  306. }
  307. static inline uint32_t from_surrogate(uint32_t hi, uint32_t lo)
  308. {
  309. return 0x10000 + 0x400 * (hi - 0xD800) + (lo - 0xDC00);
  310. }
  311. static inline int from_hex(int c)
  312. {
  313. if (c >= '0' && c <= '9')
  314. return c - '0';
  315. else if (c >= 'A' && c <= 'F')
  316. return c - 'A' + 10;
  317. else if (c >= 'a' && c <= 'f')
  318. return c - 'a' + 10;
  319. else
  320. return -1;
  321. }
  322. void rqsort(void *base, size_t nmemb, size_t size,
  323. int (*cmp)(const void *, const void *, void *),
  324. void *arg);
  325. static inline uint64_t float64_as_uint64(double d)
  326. {
  327. union {
  328. double d;
  329. uint64_t u64;
  330. } u;
  331. u.d = d;
  332. return u.u64;
  333. }
  334. static inline double uint64_as_float64(uint64_t u64)
  335. {
  336. union {
  337. double d;
  338. uint64_t u64;
  339. } u;
  340. u.u64 = u64;
  341. return u.d;
  342. }
  343. static inline double fromfp16(uint16_t v)
  344. {
  345. double d;
  346. uint32_t v1;
  347. v1 = v & 0x7fff;
  348. if (unlikely(v1 >= 0x7c00))
  349. v1 += 0x1f8000; /* NaN or infinity */
  350. d = uint64_as_float64(((uint64_t)(v >> 15) << 63) | ((uint64_t)v1 << (52 - 10)));
  351. return d * 0x1p1008;
  352. }
  353. static inline uint16_t tofp16(double d)
  354. {
  355. uint64_t a, addend;
  356. uint32_t v, sgn;
  357. int shift;
  358. a = float64_as_uint64(d);
  359. sgn = a >> 63;
  360. a = a & 0x7fffffffffffffff;
  361. if (unlikely(a > 0x7ff0000000000000)) {
  362. /* nan */
  363. v = 0x7c01;
  364. } else if (a < 0x3f10000000000000) { /* 0x1p-14 */
  365. /* subnormal f16 number or zero */
  366. if (a <= 0x3e60000000000000) { /* 0x1p-25 */
  367. v = 0x0000; /* zero */
  368. } else {
  369. shift = 1051 - (a >> 52);
  370. a = ((uint64_t)1 << 52) | (a & (((uint64_t)1 << 52) - 1));
  371. addend = ((a >> shift) & 1) + (((uint64_t)1 << (shift - 1)) - 1);
  372. v = (a + addend) >> shift;
  373. }
  374. } else {
  375. /* normal number or infinity */
  376. a -= 0x3f00000000000000; /* adjust the exponent */
  377. /* round */
  378. addend = ((a >> (52 - 10)) & 1) + (((uint64_t)1 << (52 - 11)) - 1);
  379. v = (a + addend) >> (52 - 10);
  380. /* overflow ? */
  381. if (unlikely(v > 0x7c00))
  382. v = 0x7c00;
  383. }
  384. return v | (sgn << 15);
  385. }
  386. static inline int isfp16nan(uint16_t v)
  387. {
  388. return (v & 0x7FFF) > 0x7C00;
  389. }
  390. static inline int isfp16zero(uint16_t v)
  391. {
  392. return (v & 0x7FFF) == 0;
  393. }
  394. #endif /* CUTILS_H */