|
| 1 | +/* |
| 2 | + * Selected C utilities adapted from QuickJS cutils.h |
| 3 | + * |
| 4 | + * Copyright (c) 2024 Fabrice Bellard |
| 5 | + */ |
| 6 | + |
| 7 | +#ifndef _NJS_CUTILS_H_INCLUDED_ |
| 8 | +#define _NJS_CUTILS_H_INCLUDED_ |
| 9 | + |
| 10 | +#include <stddef.h> |
| 11 | +#include <stdint.h> |
| 12 | + |
| 13 | +#ifndef likely |
| 14 | +#define likely(x) __builtin_expect(!!(x), 1) |
| 15 | +#endif |
| 16 | + |
| 17 | +#ifndef unlikely |
| 18 | +#define unlikely(x) __builtin_expect(!!(x), 0) |
| 19 | +#endif |
| 20 | + |
| 21 | +#ifndef no_inline |
| 22 | +#define no_inline __attribute__((noinline)) |
| 23 | +#endif |
| 24 | + |
| 25 | +/* compatibility attribute used in dtoa implementation */ |
| 26 | +#ifndef __maybe_unused |
| 27 | +#define __maybe_unused __attribute__((unused)) |
| 28 | +#endif |
| 29 | + |
| 30 | +typedef int BOOL; |
| 31 | + |
| 32 | +#ifndef FALSE |
| 33 | +#define FALSE 0 |
| 34 | +#endif |
| 35 | + |
| 36 | +#ifndef TRUE |
| 37 | +#define TRUE 1 |
| 38 | +#endif |
| 39 | + |
| 40 | +static inline int |
| 41 | +max_int(int a, int b) |
| 42 | +{ |
| 43 | + return (a > b) ? a : b; |
| 44 | +} |
| 45 | + |
| 46 | +static inline int |
| 47 | +min_int(int a, int b) |
| 48 | +{ |
| 49 | + return (a < b) ? a : b; |
| 50 | +} |
| 51 | + |
| 52 | +static inline uint32_t |
| 53 | +max_uint32(uint32_t a, uint32_t b) |
| 54 | +{ |
| 55 | + return (a > b) ? a : b; |
| 56 | +} |
| 57 | + |
| 58 | +static inline uint32_t |
| 59 | +min_uint32(uint32_t a, uint32_t b) |
| 60 | +{ |
| 61 | + return (a < b) ? a : b; |
| 62 | +} |
| 63 | + |
| 64 | +static inline int64_t |
| 65 | +max_int64(int64_t a, int64_t b) |
| 66 | +{ |
| 67 | + return (a > b) ? a : b; |
| 68 | +} |
| 69 | + |
| 70 | +static inline int64_t |
| 71 | +min_int64(int64_t a, int64_t b) |
| 72 | +{ |
| 73 | + return (a < b) ? a : b; |
| 74 | +} |
| 75 | + |
| 76 | +/* WARNING: undefined if a = 0 */ |
| 77 | +static inline int |
| 78 | +clz32(unsigned int a) |
| 79 | +{ |
| 80 | + return __builtin_clz(a); |
| 81 | +} |
| 82 | + |
| 83 | +/* WARNING: undefined if a = 0 */ |
| 84 | +static inline int |
| 85 | +clz64(uint64_t a) |
| 86 | +{ |
| 87 | + return __builtin_clzll(a); |
| 88 | +} |
| 89 | + |
| 90 | +/* WARNING: undefined if a = 0 */ |
| 91 | +static inline int |
| 92 | +ctz32(unsigned int a) |
| 93 | +{ |
| 94 | + return __builtin_ctz(a); |
| 95 | +} |
| 96 | + |
| 97 | +/* WARNING: undefined if a = 0 */ |
| 98 | +static inline int |
| 99 | +ctz64(uint64_t a) |
| 100 | +{ |
| 101 | + return __builtin_ctzll(a); |
| 102 | +} |
| 103 | + |
| 104 | +static inline uint64_t |
| 105 | +float64_as_uint64(double d) |
| 106 | +{ |
| 107 | + union { |
| 108 | + double d; |
| 109 | + uint64_t u64; |
| 110 | + } u; |
| 111 | + |
| 112 | + u.d = d; |
| 113 | + return u.u64; |
| 114 | +} |
| 115 | + |
| 116 | +static inline double |
| 117 | +uint64_as_float64(uint64_t u64) |
| 118 | +{ |
| 119 | + union { |
| 120 | + double d; |
| 121 | + uint64_t u64; |
| 122 | + } u; |
| 123 | + |
| 124 | + u.u64 = u64; |
| 125 | + return u.d; |
| 126 | +} |
| 127 | + |
| 128 | +static inline int |
| 129 | +strstart(const char *str, const char *val, const char **ptr) |
| 130 | +{ |
| 131 | + const char *p = str; |
| 132 | + const char *q = val; |
| 133 | + |
| 134 | + while (*q != '\0') { |
| 135 | + if (*p != *q) { |
| 136 | + return 0; |
| 137 | + } |
| 138 | + p++; |
| 139 | + q++; |
| 140 | + } |
| 141 | + |
| 142 | + if (ptr != NULL) { |
| 143 | + *ptr = p; |
| 144 | + } |
| 145 | + |
| 146 | + return 1; |
| 147 | +} |
| 148 | + |
| 149 | +#endif /* _NJS_CUTILS_H_INCLUDED_ */ |
0 commit comments