/src/gpac/src/quickjs/cutils.h
Line | Count | Source |
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 | | |
28 | | #include <stdlib.h> |
29 | | #include <string.h> |
30 | | #include <inttypes.h> |
31 | | #if defined(_MSC_VER) |
32 | | #include <intrin.h> |
33 | | #pragma intrinsic(_BitScanReverse) |
34 | | #endif |
35 | | |
36 | | |
37 | | #if defined(_MSC_VER) |
38 | | #define likely(x) (x) |
39 | | #define unlikely(x) (x) |
40 | | #define force_inline inline |
41 | | #define no_inline |
42 | | #define __maybe_unused |
43 | | #else |
44 | 0 | #define likely(x) __builtin_expect(!!(x), 1) |
45 | 0 | #define unlikely(x) __builtin_expect(!!(x), 0) |
46 | | #define force_inline inline __attribute__((always_inline)) |
47 | | #define no_inline __attribute__((noinline)) |
48 | | #define __maybe_unused __attribute__((unused)) |
49 | | #endif |
50 | | |
51 | | #define xglue(x, y) x ## y |
52 | | #define glue(x, y) xglue(x, y) |
53 | | #define stringify(s) tostring(s) |
54 | | #define tostring(s) #s |
55 | | |
56 | | #ifndef offsetof |
57 | | #define offsetof(type, field) ((size_t) &((type *)0)->field) |
58 | | #endif |
59 | | #ifndef countof |
60 | 0 | #define countof(x) (sizeof(x) / sizeof((x)[0])) |
61 | | #endif |
62 | | #ifndef container_of |
63 | | /* return the pointer of type 'type *' containing 'ptr' as field 'member' */ |
64 | 0 | #define container_of(ptr, type, member) ((type *)((uint8_t *)(ptr) - offsetof(type, member))) |
65 | | #endif |
66 | | |
67 | | #if !defined(_MSC_VER) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L |
68 | | #define minimum_length(n) static n |
69 | | #else |
70 | | #define minimum_length(n) n |
71 | | #endif |
72 | | |
73 | | typedef int BOOL; |
74 | | |
75 | | #ifndef FALSE |
76 | | enum { |
77 | | FALSE = 0, |
78 | | TRUE = 1, |
79 | | }; |
80 | | #endif |
81 | | |
82 | | void pstrcpy(char *buf, int buf_size, const char *str); |
83 | | char *pstrcat(char *buf, int buf_size, const char *s); |
84 | | int strstart(const char *str, const char *val, const char **ptr); |
85 | | int has_suffix(const char *str, const char *suffix); |
86 | | |
87 | | /* Prevent UB when n == 0 and (src == NULL or dest == NULL) */ |
88 | 0 | static inline void memcpy_no_ub(void *dest, const void *src, size_t n) { |
89 | 0 | if (n) |
90 | 0 | memcpy(dest, src, n); |
91 | 0 | } Unexecuted instantiation: quickjs.c:memcpy_no_ub Unexecuted instantiation: quickjs-libc.c:memcpy_no_ub Unexecuted instantiation: cutils.c:memcpy_no_ub Unexecuted instantiation: dtoa.c:memcpy_no_ub Unexecuted instantiation: libregexp.c:memcpy_no_ub Unexecuted instantiation: libunicode.c:memcpy_no_ub |
92 | | |
93 | | static inline int max_int(int a, int b) |
94 | 0 | { |
95 | 0 | if (a > b) |
96 | 0 | return a; |
97 | 0 | else |
98 | 0 | return b; |
99 | 0 | } Unexecuted instantiation: quickjs.c:max_int Unexecuted instantiation: quickjs-libc.c:max_int Unexecuted instantiation: cutils.c:max_int Unexecuted instantiation: dtoa.c:max_int Unexecuted instantiation: libregexp.c:max_int Unexecuted instantiation: libunicode.c:max_int |
100 | | |
101 | | static inline int min_int(int a, int b) |
102 | 0 | { |
103 | 0 | if (a < b) |
104 | 0 | return a; |
105 | 0 | else |
106 | 0 | return b; |
107 | 0 | } Unexecuted instantiation: quickjs.c:min_int Unexecuted instantiation: quickjs-libc.c:min_int Unexecuted instantiation: cutils.c:min_int Unexecuted instantiation: dtoa.c:min_int Unexecuted instantiation: libregexp.c:min_int Unexecuted instantiation: libunicode.c:min_int |
108 | | |
109 | | static inline uint32_t max_uint32(uint32_t a, uint32_t b) |
110 | 0 | { |
111 | 0 | if (a > b) |
112 | 0 | return a; |
113 | 0 | else |
114 | 0 | return b; |
115 | 0 | } Unexecuted instantiation: quickjs.c:max_uint32 Unexecuted instantiation: quickjs-libc.c:max_uint32 Unexecuted instantiation: cutils.c:max_uint32 Unexecuted instantiation: dtoa.c:max_uint32 Unexecuted instantiation: libregexp.c:max_uint32 Unexecuted instantiation: libunicode.c:max_uint32 |
116 | | |
117 | | static inline uint32_t min_uint32(uint32_t a, uint32_t b) |
118 | 0 | { |
119 | 0 | if (a < b) |
120 | 0 | return a; |
121 | 0 | else |
122 | 0 | return b; |
123 | 0 | } Unexecuted instantiation: quickjs.c:min_uint32 Unexecuted instantiation: quickjs-libc.c:min_uint32 Unexecuted instantiation: cutils.c:min_uint32 Unexecuted instantiation: dtoa.c:min_uint32 Unexecuted instantiation: libregexp.c:min_uint32 Unexecuted instantiation: libunicode.c:min_uint32 |
124 | | |
125 | | static inline int64_t max_int64(int64_t a, int64_t b) |
126 | 0 | { |
127 | 0 | if (a > b) |
128 | 0 | return a; |
129 | 0 | else |
130 | 0 | return b; |
131 | 0 | } Unexecuted instantiation: quickjs.c:max_int64 Unexecuted instantiation: quickjs-libc.c:max_int64 Unexecuted instantiation: cutils.c:max_int64 Unexecuted instantiation: dtoa.c:max_int64 Unexecuted instantiation: libregexp.c:max_int64 Unexecuted instantiation: libunicode.c:max_int64 |
132 | | |
133 | | static inline int64_t min_int64(int64_t a, int64_t b) |
134 | 0 | { |
135 | 0 | if (a < b) |
136 | 0 | return a; |
137 | 0 | else |
138 | 0 | return b; |
139 | 0 | } Unexecuted instantiation: quickjs.c:min_int64 Unexecuted instantiation: quickjs-libc.c:min_int64 Unexecuted instantiation: cutils.c:min_int64 Unexecuted instantiation: dtoa.c:min_int64 Unexecuted instantiation: libregexp.c:min_int64 Unexecuted instantiation: libunicode.c:min_int64 |
140 | | |
141 | | /* WARNING: undefined if a = 0 */ |
142 | | static inline int clz32(unsigned int a) |
143 | 0 | { |
144 | | #if defined(_MSC_VER) |
145 | | unsigned long ret = 0; |
146 | | _BitScanReverse(&ret, a); |
147 | | return 31 - ret; |
148 | | #else |
149 | 0 | return __builtin_clz(a); |
150 | 0 | #endif |
151 | 0 | } Unexecuted instantiation: quickjs.c:clz32 Unexecuted instantiation: quickjs-libc.c:clz32 Unexecuted instantiation: cutils.c:clz32 Unexecuted instantiation: dtoa.c:clz32 Unexecuted instantiation: libregexp.c:clz32 Unexecuted instantiation: libunicode.c:clz32 |
152 | | |
153 | | /* WARNING: undefined if a = 0 */ |
154 | | static inline int clz64(uint64_t a) |
155 | 0 | { |
156 | | #if defined(_MSC_VER) |
157 | | unsigned long ret = 0; |
158 | | # ifdef _WIN64 |
159 | | _BitScanReverse64(&ret, a); |
160 | | # else |
161 | | if (_BitScanReverse(&ret, (uint32_t) (a >> 32))) |
162 | | return 63 - (ret + 32); |
163 | | _BitScanReverse(&ret, (uint32_t) (a)); |
164 | | # endif |
165 | | return (int)ret; |
166 | | #else |
167 | 0 | return __builtin_clzll(a); |
168 | 0 | #endif |
169 | 0 | } Unexecuted instantiation: quickjs.c:clz64 Unexecuted instantiation: quickjs-libc.c:clz64 Unexecuted instantiation: cutils.c:clz64 Unexecuted instantiation: dtoa.c:clz64 Unexecuted instantiation: libregexp.c:clz64 Unexecuted instantiation: libunicode.c:clz64 |
170 | | |
171 | | /* WARNING: undefined if a = 0 */ |
172 | | static inline int ctz32(unsigned int a) |
173 | 0 | { |
174 | | #if defined(_MSC_VER) |
175 | | unsigned long ret = 0; |
176 | | _BitScanForward(&ret, a); |
177 | | return (int)ret; |
178 | | #else |
179 | 0 | return __builtin_ctz(a); |
180 | 0 | #endif |
181 | 0 | } Unexecuted instantiation: quickjs.c:ctz32 Unexecuted instantiation: quickjs-libc.c:ctz32 Unexecuted instantiation: cutils.c:ctz32 Unexecuted instantiation: dtoa.c:ctz32 Unexecuted instantiation: libregexp.c:ctz32 Unexecuted instantiation: libunicode.c:ctz32 |
182 | | |
183 | | /* WARNING: undefined if a = 0 */ |
184 | | static inline int ctz64(uint64_t a) |
185 | 0 | { |
186 | 0 | #if defined(_MSC_VER) |
187 | 0 | unsigned long ret = 0; |
188 | 0 | # ifdef _WIN64 |
189 | 0 | _BitScanReverse64(&ret, a); |
190 | 0 | # else |
191 | 0 | if (_BitScanReverse(&ret, (uint32_t)(a >> 32))) |
192 | 0 | return 63 - (ret + 32); |
193 | 0 | _BitScanReverse(&ret, (uint32_t)(a)); |
194 | 0 | # endif |
195 | 0 | return (int)ret; |
196 | 0 | #else |
197 | 0 | return __builtin_ctzll(a); |
198 | 0 | #endif |
199 | 0 | } Unexecuted instantiation: quickjs.c:ctz64 Unexecuted instantiation: quickjs-libc.c:ctz64 Unexecuted instantiation: cutils.c:ctz64 Unexecuted instantiation: dtoa.c:ctz64 Unexecuted instantiation: libregexp.c:ctz64 Unexecuted instantiation: libunicode.c:ctz64 |
200 | | |
201 | | #if defined(_MSC_VER) |
202 | | #pragma pack(push) |
203 | | #pragma pack(1) |
204 | | #define __attribute__(x) /* */ |
205 | | #endif |
206 | | |
207 | | struct __attribute__((packed)) packed_u64 { |
208 | | uint64_t v; |
209 | | }; |
210 | | |
211 | | struct __attribute__((packed)) packed_u32 { |
212 | | uint32_t v; |
213 | | }; |
214 | | |
215 | | struct __attribute__((packed)) packed_u16 { |
216 | | uint16_t v; |
217 | | }; |
218 | | |
219 | | #if defined(_MSC_VER) |
220 | | #pragma pack(pop) |
221 | | #undef __attribute__ |
222 | | #endif |
223 | | |
224 | | static inline uint64_t get_u64(const uint8_t *tab) |
225 | 0 | { |
226 | 0 | return ((const struct packed_u64 *)tab)->v; |
227 | 0 | } Unexecuted instantiation: quickjs.c:get_u64 Unexecuted instantiation: quickjs-libc.c:get_u64 Unexecuted instantiation: cutils.c:get_u64 Unexecuted instantiation: dtoa.c:get_u64 Unexecuted instantiation: libregexp.c:get_u64 Unexecuted instantiation: libunicode.c:get_u64 |
228 | | |
229 | | static inline int64_t get_i64(const uint8_t *tab) |
230 | 0 | { |
231 | 0 | return (int64_t)((const struct packed_u64 *)tab)->v; |
232 | 0 | } Unexecuted instantiation: quickjs.c:get_i64 Unexecuted instantiation: quickjs-libc.c:get_i64 Unexecuted instantiation: cutils.c:get_i64 Unexecuted instantiation: dtoa.c:get_i64 Unexecuted instantiation: libregexp.c:get_i64 Unexecuted instantiation: libunicode.c:get_i64 |
233 | | |
234 | | static inline void put_u64(uint8_t *tab, uint64_t val) |
235 | 0 | { |
236 | 0 | ((struct packed_u64 *)tab)->v = val; |
237 | 0 | } Unexecuted instantiation: quickjs.c:put_u64 Unexecuted instantiation: quickjs-libc.c:put_u64 Unexecuted instantiation: cutils.c:put_u64 Unexecuted instantiation: dtoa.c:put_u64 Unexecuted instantiation: libregexp.c:put_u64 Unexecuted instantiation: libunicode.c:put_u64 |
238 | | |
239 | | static inline uint32_t get_u32(const uint8_t *tab) |
240 | 0 | { |
241 | 0 | return ((const struct packed_u32 *)tab)->v; |
242 | 0 | } Unexecuted instantiation: quickjs.c:get_u32 Unexecuted instantiation: quickjs-libc.c:get_u32 Unexecuted instantiation: cutils.c:get_u32 Unexecuted instantiation: dtoa.c:get_u32 Unexecuted instantiation: libregexp.c:get_u32 Unexecuted instantiation: libunicode.c:get_u32 |
243 | | |
244 | | static inline int32_t get_i32(const uint8_t *tab) |
245 | 0 | { |
246 | 0 | return (int32_t)((const struct packed_u32 *)tab)->v; |
247 | 0 | } Unexecuted instantiation: quickjs.c:get_i32 Unexecuted instantiation: quickjs-libc.c:get_i32 Unexecuted instantiation: cutils.c:get_i32 Unexecuted instantiation: dtoa.c:get_i32 Unexecuted instantiation: libregexp.c:get_i32 Unexecuted instantiation: libunicode.c:get_i32 |
248 | | |
249 | | static inline void put_u32(uint8_t *tab, uint32_t val) |
250 | 0 | { |
251 | 0 | ((struct packed_u32 *)tab)->v = val; |
252 | 0 | } Unexecuted instantiation: quickjs.c:put_u32 Unexecuted instantiation: quickjs-libc.c:put_u32 Unexecuted instantiation: cutils.c:put_u32 Unexecuted instantiation: dtoa.c:put_u32 Unexecuted instantiation: libregexp.c:put_u32 Unexecuted instantiation: libunicode.c:put_u32 |
253 | | |
254 | | static inline uint32_t get_u16(const uint8_t *tab) |
255 | 0 | { |
256 | 0 | return ((const struct packed_u16 *)tab)->v; |
257 | 0 | } Unexecuted instantiation: quickjs.c:get_u16 Unexecuted instantiation: quickjs-libc.c:get_u16 Unexecuted instantiation: cutils.c:get_u16 Unexecuted instantiation: dtoa.c:get_u16 Unexecuted instantiation: libregexp.c:get_u16 Unexecuted instantiation: libunicode.c:get_u16 |
258 | | |
259 | | static inline int32_t get_i16(const uint8_t *tab) |
260 | 0 | { |
261 | 0 | return (int16_t)((const struct packed_u16 *)tab)->v; |
262 | 0 | } Unexecuted instantiation: quickjs.c:get_i16 Unexecuted instantiation: quickjs-libc.c:get_i16 Unexecuted instantiation: cutils.c:get_i16 Unexecuted instantiation: dtoa.c:get_i16 Unexecuted instantiation: libregexp.c:get_i16 Unexecuted instantiation: libunicode.c:get_i16 |
263 | | |
264 | | static inline void put_u16(uint8_t *tab, uint16_t val) |
265 | 0 | { |
266 | 0 | ((struct packed_u16 *)tab)->v = val; |
267 | 0 | } Unexecuted instantiation: quickjs.c:put_u16 Unexecuted instantiation: quickjs-libc.c:put_u16 Unexecuted instantiation: cutils.c:put_u16 Unexecuted instantiation: dtoa.c:put_u16 Unexecuted instantiation: libregexp.c:put_u16 Unexecuted instantiation: libunicode.c:put_u16 |
268 | | |
269 | | static inline uint32_t get_u8(const uint8_t *tab) |
270 | 0 | { |
271 | 0 | return *tab; |
272 | 0 | } Unexecuted instantiation: quickjs.c:get_u8 Unexecuted instantiation: quickjs-libc.c:get_u8 Unexecuted instantiation: cutils.c:get_u8 Unexecuted instantiation: dtoa.c:get_u8 Unexecuted instantiation: libregexp.c:get_u8 Unexecuted instantiation: libunicode.c:get_u8 |
273 | | |
274 | | static inline int32_t get_i8(const uint8_t *tab) |
275 | 0 | { |
276 | 0 | return (int8_t)*tab; |
277 | 0 | } Unexecuted instantiation: quickjs.c:get_i8 Unexecuted instantiation: quickjs-libc.c:get_i8 Unexecuted instantiation: cutils.c:get_i8 Unexecuted instantiation: dtoa.c:get_i8 Unexecuted instantiation: libregexp.c:get_i8 Unexecuted instantiation: libunicode.c:get_i8 |
278 | | |
279 | | static inline void put_u8(uint8_t *tab, uint8_t val) |
280 | 0 | { |
281 | 0 | *tab = val; |
282 | 0 | } Unexecuted instantiation: quickjs.c:put_u8 Unexecuted instantiation: quickjs-libc.c:put_u8 Unexecuted instantiation: cutils.c:put_u8 Unexecuted instantiation: dtoa.c:put_u8 Unexecuted instantiation: libregexp.c:put_u8 Unexecuted instantiation: libunicode.c:put_u8 |
283 | | |
284 | | #ifndef bswap16 |
285 | | static inline uint16_t bswap16(uint16_t x) |
286 | 0 | { |
287 | 0 | return (x >> 8) | (x << 8); |
288 | 0 | } Unexecuted instantiation: quickjs.c:bswap16 Unexecuted instantiation: quickjs-libc.c:bswap16 Unexecuted instantiation: cutils.c:bswap16 Unexecuted instantiation: dtoa.c:bswap16 Unexecuted instantiation: libregexp.c:bswap16 Unexecuted instantiation: libunicode.c:bswap16 |
289 | | #endif |
290 | | |
291 | | #ifndef bswap32 |
292 | | static inline uint32_t bswap32(uint32_t v) |
293 | 0 | { |
294 | 0 | return ((v & 0xff000000) >> 24) | ((v & 0x00ff0000) >> 8) | |
295 | 0 | ((v & 0x0000ff00) << 8) | ((v & 0x000000ff) << 24); |
296 | 0 | } Unexecuted instantiation: quickjs.c:bswap32 Unexecuted instantiation: quickjs-libc.c:bswap32 Unexecuted instantiation: cutils.c:bswap32 Unexecuted instantiation: dtoa.c:bswap32 Unexecuted instantiation: libregexp.c:bswap32 Unexecuted instantiation: libunicode.c:bswap32 |
297 | | #endif |
298 | | |
299 | | #ifndef bswap64 |
300 | | static inline uint64_t bswap64(uint64_t v) |
301 | 0 | { |
302 | 0 | return ((v & ((uint64_t)0xff << (7 * 8))) >> (7 * 8)) | |
303 | 0 | ((v & ((uint64_t)0xff << (6 * 8))) >> (5 * 8)) | |
304 | 0 | ((v & ((uint64_t)0xff << (5 * 8))) >> (3 * 8)) | |
305 | 0 | ((v & ((uint64_t)0xff << (4 * 8))) >> (1 * 8)) | |
306 | 0 | ((v & ((uint64_t)0xff << (3 * 8))) << (1 * 8)) | |
307 | 0 | ((v & ((uint64_t)0xff << (2 * 8))) << (3 * 8)) | |
308 | 0 | ((v & ((uint64_t)0xff << (1 * 8))) << (5 * 8)) | |
309 | 0 | ((v & ((uint64_t)0xff << (0 * 8))) << (7 * 8)); |
310 | 0 | } Unexecuted instantiation: quickjs.c:bswap64 Unexecuted instantiation: quickjs-libc.c:bswap64 Unexecuted instantiation: cutils.c:bswap64 Unexecuted instantiation: dtoa.c:bswap64 Unexecuted instantiation: libregexp.c:bswap64 Unexecuted instantiation: libunicode.c:bswap64 |
311 | | #endif |
312 | | |
313 | | /* XXX: should take an extra argument to pass slack information to the caller */ |
314 | | typedef void *DynBufReallocFunc(void *opaque, void *ptr, size_t size); |
315 | | |
316 | | typedef struct DynBuf { |
317 | | uint8_t *buf; |
318 | | size_t size; |
319 | | size_t allocated_size; |
320 | | BOOL error; /* true if a memory allocation error occurred */ |
321 | | DynBufReallocFunc *realloc_func; |
322 | | void *opaque; /* for realloc_func */ |
323 | | } DynBuf; |
324 | | |
325 | | void dbuf_init(DynBuf *s); |
326 | | void dbuf_init2(DynBuf *s, void *opaque, DynBufReallocFunc *realloc_func); |
327 | | int dbuf_claim(DynBuf *s, size_t len); |
328 | | int dbuf_put(DynBuf *s, const uint8_t *data, size_t len); |
329 | | int dbuf_put_self(DynBuf *s, size_t offset, size_t len); |
330 | | int dbuf_putstr(DynBuf *s, const char *str); |
331 | | int __dbuf_putc(DynBuf *s, uint8_t c); |
332 | | int __dbuf_put_u16(DynBuf *s, uint16_t val); |
333 | | int __dbuf_put_u32(DynBuf *s, uint32_t val); |
334 | | int __dbuf_put_u64(DynBuf *s, uint64_t val); |
335 | | |
336 | | static inline int dbuf_putc(DynBuf *s, uint8_t val) |
337 | 0 | { |
338 | 0 | if (unlikely((s->allocated_size - s->size) < 1)) { |
339 | 0 | return __dbuf_putc(s, val); |
340 | 0 | } else { |
341 | 0 | s->buf[s->size++] = val; |
342 | 0 | return 0; |
343 | 0 | } |
344 | 0 | } Unexecuted instantiation: quickjs.c:dbuf_putc Unexecuted instantiation: quickjs-libc.c:dbuf_putc Unexecuted instantiation: cutils.c:dbuf_putc Unexecuted instantiation: dtoa.c:dbuf_putc Unexecuted instantiation: libregexp.c:dbuf_putc Unexecuted instantiation: libunicode.c:dbuf_putc |
345 | | |
346 | | static inline int dbuf_put_u16(DynBuf *s, uint16_t val) |
347 | 0 | { |
348 | 0 | if (unlikely((s->allocated_size - s->size) < 2)) { |
349 | 0 | return __dbuf_put_u16(s, val); |
350 | 0 | } else { |
351 | 0 | put_u16(s->buf + s->size, val); |
352 | 0 | s->size += 2; |
353 | 0 | return 0; |
354 | 0 | } |
355 | 0 | } Unexecuted instantiation: quickjs.c:dbuf_put_u16 Unexecuted instantiation: quickjs-libc.c:dbuf_put_u16 Unexecuted instantiation: cutils.c:dbuf_put_u16 Unexecuted instantiation: dtoa.c:dbuf_put_u16 Unexecuted instantiation: libregexp.c:dbuf_put_u16 Unexecuted instantiation: libunicode.c:dbuf_put_u16 |
356 | | |
357 | | static inline int dbuf_put_u32(DynBuf *s, uint32_t val) |
358 | 0 | { |
359 | 0 | if (unlikely((s->allocated_size - s->size) < 4)) { |
360 | 0 | return __dbuf_put_u32(s, val); |
361 | 0 | } else { |
362 | 0 | put_u32(s->buf + s->size, val); |
363 | 0 | s->size += 4; |
364 | 0 | return 0; |
365 | 0 | } |
366 | 0 | } Unexecuted instantiation: quickjs.c:dbuf_put_u32 Unexecuted instantiation: quickjs-libc.c:dbuf_put_u32 Unexecuted instantiation: cutils.c:dbuf_put_u32 Unexecuted instantiation: dtoa.c:dbuf_put_u32 Unexecuted instantiation: libregexp.c:dbuf_put_u32 Unexecuted instantiation: libunicode.c:dbuf_put_u32 |
367 | | |
368 | | static inline int dbuf_put_u64(DynBuf *s, uint64_t val) |
369 | 0 | { |
370 | 0 | if (unlikely((s->allocated_size - s->size) < 8)) { |
371 | 0 | return __dbuf_put_u64(s, val); |
372 | 0 | } else { |
373 | 0 | put_u64(s->buf + s->size, val); |
374 | 0 | s->size += 8; |
375 | 0 | return 0; |
376 | 0 | } |
377 | 0 | } Unexecuted instantiation: quickjs.c:dbuf_put_u64 Unexecuted instantiation: quickjs-libc.c:dbuf_put_u64 Unexecuted instantiation: cutils.c:dbuf_put_u64 Unexecuted instantiation: dtoa.c:dbuf_put_u64 Unexecuted instantiation: libregexp.c:dbuf_put_u64 Unexecuted instantiation: libunicode.c:dbuf_put_u64 |
378 | | |
379 | | #if defined(_MSC_VER) |
380 | | int dbuf_printf(DynBuf *s, const char *fmt, ...); |
381 | | #else |
382 | | int __attribute__((format(printf, 2, 3))) dbuf_printf(DynBuf *s, |
383 | | const char *fmt, ...); |
384 | | #endif |
385 | | void dbuf_free(DynBuf *s); |
386 | 0 | static inline BOOL dbuf_error(DynBuf *s) { |
387 | 0 | return s->error; |
388 | 0 | } Unexecuted instantiation: quickjs.c:dbuf_error Unexecuted instantiation: quickjs-libc.c:dbuf_error Unexecuted instantiation: cutils.c:dbuf_error Unexecuted instantiation: dtoa.c:dbuf_error Unexecuted instantiation: libregexp.c:dbuf_error Unexecuted instantiation: libunicode.c:dbuf_error |
389 | | static inline void dbuf_set_error(DynBuf *s) |
390 | 0 | { |
391 | 0 | s->error = TRUE; |
392 | 0 | } Unexecuted instantiation: quickjs.c:dbuf_set_error Unexecuted instantiation: quickjs-libc.c:dbuf_set_error Unexecuted instantiation: cutils.c:dbuf_set_error Unexecuted instantiation: dtoa.c:dbuf_set_error Unexecuted instantiation: libregexp.c:dbuf_set_error Unexecuted instantiation: libunicode.c:dbuf_set_error |
393 | | |
394 | 0 | #define UTF8_CHAR_LEN_MAX 6 |
395 | | |
396 | | int unicode_to_utf8(uint8_t *buf, unsigned int c); |
397 | | int unicode_from_utf8(const uint8_t *p, int max_len, const uint8_t **pp); |
398 | | |
399 | | static inline BOOL is_surrogate(uint32_t c) |
400 | 0 | { |
401 | 0 | return (c >> 11) == (0xD800 >> 11); // 0xD800-0xDFFF |
402 | 0 | } Unexecuted instantiation: quickjs.c:is_surrogate Unexecuted instantiation: quickjs-libc.c:is_surrogate Unexecuted instantiation: cutils.c:is_surrogate Unexecuted instantiation: dtoa.c:is_surrogate Unexecuted instantiation: libregexp.c:is_surrogate Unexecuted instantiation: libunicode.c:is_surrogate |
403 | | |
404 | | static inline BOOL is_hi_surrogate(uint32_t c) |
405 | 0 | { |
406 | 0 | return (c >> 10) == (0xD800 >> 10); // 0xD800-0xDBFF |
407 | 0 | } Unexecuted instantiation: quickjs.c:is_hi_surrogate Unexecuted instantiation: quickjs-libc.c:is_hi_surrogate Unexecuted instantiation: cutils.c:is_hi_surrogate Unexecuted instantiation: dtoa.c:is_hi_surrogate Unexecuted instantiation: libregexp.c:is_hi_surrogate Unexecuted instantiation: libunicode.c:is_hi_surrogate |
408 | | |
409 | | static inline BOOL is_lo_surrogate(uint32_t c) |
410 | 0 | { |
411 | 0 | return (c >> 10) == (0xDC00 >> 10); // 0xDC00-0xDFFF |
412 | 0 | } Unexecuted instantiation: quickjs.c:is_lo_surrogate Unexecuted instantiation: quickjs-libc.c:is_lo_surrogate Unexecuted instantiation: cutils.c:is_lo_surrogate Unexecuted instantiation: dtoa.c:is_lo_surrogate Unexecuted instantiation: libregexp.c:is_lo_surrogate Unexecuted instantiation: libunicode.c:is_lo_surrogate |
413 | | |
414 | | static inline uint32_t get_hi_surrogate(uint32_t c) |
415 | 0 | { |
416 | 0 | return (c >> 10) - (0x10000 >> 10) + 0xD800; |
417 | 0 | } Unexecuted instantiation: quickjs.c:get_hi_surrogate Unexecuted instantiation: quickjs-libc.c:get_hi_surrogate Unexecuted instantiation: cutils.c:get_hi_surrogate Unexecuted instantiation: dtoa.c:get_hi_surrogate Unexecuted instantiation: libregexp.c:get_hi_surrogate Unexecuted instantiation: libunicode.c:get_hi_surrogate |
418 | | |
419 | | static inline uint32_t get_lo_surrogate(uint32_t c) |
420 | 0 | { |
421 | 0 | return (c & 0x3FF) | 0xDC00; |
422 | 0 | } Unexecuted instantiation: quickjs.c:get_lo_surrogate Unexecuted instantiation: quickjs-libc.c:get_lo_surrogate Unexecuted instantiation: cutils.c:get_lo_surrogate Unexecuted instantiation: dtoa.c:get_lo_surrogate Unexecuted instantiation: libregexp.c:get_lo_surrogate Unexecuted instantiation: libunicode.c:get_lo_surrogate |
423 | | |
424 | | static inline uint32_t from_surrogate(uint32_t hi, uint32_t lo) |
425 | 0 | { |
426 | 0 | return 0x10000 + 0x400 * (hi - 0xD800) + (lo - 0xDC00); |
427 | 0 | } Unexecuted instantiation: quickjs.c:from_surrogate Unexecuted instantiation: quickjs-libc.c:from_surrogate Unexecuted instantiation: cutils.c:from_surrogate Unexecuted instantiation: dtoa.c:from_surrogate Unexecuted instantiation: libregexp.c:from_surrogate Unexecuted instantiation: libunicode.c:from_surrogate |
428 | | |
429 | | static inline int from_hex(int c) |
430 | 0 | { |
431 | 0 | if (c >= '0' && c <= '9') |
432 | 0 | return c - '0'; |
433 | 0 | else if (c >= 'A' && c <= 'F') |
434 | 0 | return c - 'A' + 10; |
435 | 0 | else if (c >= 'a' && c <= 'f') |
436 | 0 | return c - 'a' + 10; |
437 | 0 | else |
438 | 0 | return -1; |
439 | 0 | } Unexecuted instantiation: quickjs.c:from_hex Unexecuted instantiation: quickjs-libc.c:from_hex Unexecuted instantiation: cutils.c:from_hex Unexecuted instantiation: dtoa.c:from_hex Unexecuted instantiation: libregexp.c:from_hex Unexecuted instantiation: libunicode.c:from_hex |
440 | | |
441 | | void rqsort(void *base, size_t nmemb, size_t size, |
442 | | int (*cmp)(const void *, const void *, void *), |
443 | | void *arg); |
444 | | |
445 | | static inline uint64_t float64_as_uint64(double d) |
446 | 0 | { |
447 | 0 | union { |
448 | 0 | double d; |
449 | 0 | uint64_t u64; |
450 | 0 | } u; |
451 | 0 | u.d = d; |
452 | 0 | return u.u64; |
453 | 0 | } Unexecuted instantiation: quickjs.c:float64_as_uint64 Unexecuted instantiation: quickjs-libc.c:float64_as_uint64 Unexecuted instantiation: cutils.c:float64_as_uint64 Unexecuted instantiation: dtoa.c:float64_as_uint64 Unexecuted instantiation: libregexp.c:float64_as_uint64 Unexecuted instantiation: libunicode.c:float64_as_uint64 |
454 | | |
455 | | static inline double uint64_as_float64(uint64_t u64) |
456 | 0 | { |
457 | 0 | union { |
458 | 0 | double d; |
459 | 0 | uint64_t u64; |
460 | 0 | } u; |
461 | 0 | u.u64 = u64; |
462 | 0 | return u.d; |
463 | 0 | } Unexecuted instantiation: quickjs.c:uint64_as_float64 Unexecuted instantiation: quickjs-libc.c:uint64_as_float64 Unexecuted instantiation: cutils.c:uint64_as_float64 Unexecuted instantiation: dtoa.c:uint64_as_float64 Unexecuted instantiation: libregexp.c:uint64_as_float64 Unexecuted instantiation: libunicode.c:uint64_as_float64 |
464 | | |
465 | | |
466 | | //taken from QuickJS-NG, original function is not protable due to 0x1p1008 |
467 | | #if defined(_MSC_VER) && !defined(__clang__) |
468 | | #include <math.h> |
469 | | #define INF INFINITY |
470 | | #define NEG_INF -INFINITY |
471 | | |
472 | | static inline double fromfp16(uint16_t v) { |
473 | | double d, s; |
474 | | int e; |
475 | | if ((v & 0x7C00) == 0x7C00) { |
476 | | d = (v & 0x3FF) ? NAN : INFINITY; |
477 | | } |
478 | | else { |
479 | | d = (v & 0x3FF) / 1024.; |
480 | | e = (v & 0x7C00) >> 10; |
481 | | if (e == 0) { |
482 | | e = -14; |
483 | | } |
484 | | else { |
485 | | d += 1; |
486 | | e -= 15; |
487 | | } |
488 | | d = scalbn(d, e); |
489 | | } |
490 | | s = (v & 0x8000) ? -1.0 : 1.0; |
491 | | return d * s; |
492 | | } |
493 | | #else |
494 | | |
495 | | static inline double fromfp16(uint16_t v) |
496 | 0 | { |
497 | 0 | double d; |
498 | 0 | uint32_t v1; |
499 | 0 | v1 = v & 0x7fff; |
500 | 0 | if (unlikely(v1 >= 0x7c00)) |
501 | 0 | v1 += 0x1f8000; /* NaN or infinity */ |
502 | 0 | d = uint64_as_float64(((uint64_t)(v >> 15) << 63) | ((uint64_t)v1 << (52 - 10))); |
503 | 0 | return d * 0x1p1008; |
504 | 0 | } Unexecuted instantiation: quickjs.c:fromfp16 Unexecuted instantiation: quickjs-libc.c:fromfp16 Unexecuted instantiation: cutils.c:fromfp16 Unexecuted instantiation: dtoa.c:fromfp16 Unexecuted instantiation: libregexp.c:fromfp16 Unexecuted instantiation: libunicode.c:fromfp16 |
505 | | |
506 | | #endif |
507 | | |
508 | | |
509 | | static inline uint16_t tofp16(double d) |
510 | 0 | { |
511 | 0 | uint64_t a, addend; |
512 | 0 | uint32_t v, sgn; |
513 | 0 | int shift; |
514 | | |
515 | 0 | a = float64_as_uint64(d); |
516 | 0 | sgn = a >> 63; |
517 | 0 | a = a & 0x7fffffffffffffff; |
518 | 0 | if (unlikely(a > 0x7ff0000000000000)) { |
519 | | /* nan */ |
520 | 0 | v = 0x7c01; |
521 | 0 | } else if (a < 0x3f10000000000000) { /* 0x1p-14 */ |
522 | | /* subnormal f16 number or zero */ |
523 | 0 | if (a <= 0x3e60000000000000) { /* 0x1p-25 */ |
524 | 0 | v = 0x0000; /* zero */ |
525 | 0 | } else { |
526 | 0 | shift = 1051 - (a >> 52); |
527 | 0 | a = ((uint64_t)1 << 52) | (a & (((uint64_t)1 << 52) - 1)); |
528 | 0 | addend = ((a >> shift) & 1) + (((uint64_t)1 << (shift - 1)) - 1); |
529 | 0 | v = (a + addend) >> shift; |
530 | 0 | } |
531 | 0 | } else { |
532 | | /* normal number or infinity */ |
533 | 0 | a -= 0x3f00000000000000; /* adjust the exponent */ |
534 | | /* round */ |
535 | 0 | addend = ((a >> (52 - 10)) & 1) + (((uint64_t)1 << (52 - 11)) - 1); |
536 | 0 | v = (a + addend) >> (52 - 10); |
537 | | /* overflow ? */ |
538 | 0 | if (unlikely(v > 0x7c00)) |
539 | 0 | v = 0x7c00; |
540 | 0 | } |
541 | 0 | return v | (sgn << 15); |
542 | 0 | } Unexecuted instantiation: quickjs.c:tofp16 Unexecuted instantiation: quickjs-libc.c:tofp16 Unexecuted instantiation: cutils.c:tofp16 Unexecuted instantiation: dtoa.c:tofp16 Unexecuted instantiation: libregexp.c:tofp16 Unexecuted instantiation: libunicode.c:tofp16 |
543 | | |
544 | | static inline int isfp16nan(uint16_t v) |
545 | 0 | { |
546 | 0 | return (v & 0x7FFF) > 0x7C00; |
547 | 0 | } Unexecuted instantiation: quickjs.c:isfp16nan Unexecuted instantiation: quickjs-libc.c:isfp16nan Unexecuted instantiation: cutils.c:isfp16nan Unexecuted instantiation: dtoa.c:isfp16nan Unexecuted instantiation: libregexp.c:isfp16nan Unexecuted instantiation: libunicode.c:isfp16nan |
548 | | |
549 | | static inline int isfp16zero(uint16_t v) |
550 | 0 | { |
551 | 0 | return (v & 0x7FFF) == 0; |
552 | 0 | } Unexecuted instantiation: quickjs.c:isfp16zero Unexecuted instantiation: quickjs-libc.c:isfp16zero Unexecuted instantiation: cutils.c:isfp16zero Unexecuted instantiation: dtoa.c:isfp16zero Unexecuted instantiation: libregexp.c:isfp16zero Unexecuted instantiation: libunicode.c:isfp16zero |
553 | | |
554 | | #endif /* CUTILS_H */ |