Line | Count | Source (jump to first uncovered line) |
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 | | |
32 | 618k | #define likely(x) __builtin_expect(!!(x), 1) |
33 | 7.49M | #define unlikely(x) __builtin_expect(!!(x), 0) |
34 | | #define force_inline inline __attribute__((always_inline)) |
35 | | #define no_inline __attribute__((noinline)) |
36 | | #define __maybe_unused __attribute__((unused)) |
37 | | |
38 | | #define xglue(x, y) x ## y |
39 | | #define glue(x, y) xglue(x, y) |
40 | | #define stringify(s) tostring(s) |
41 | | #define tostring(s) #s |
42 | | |
43 | | #ifndef offsetof |
44 | | #define offsetof(type, field) ((size_t) &((type *)0)->field) |
45 | | #endif |
46 | | #ifndef countof |
47 | 2.27M | #define countof(x) (sizeof(x) / sizeof((x)[0])) |
48 | | #endif |
49 | | #ifndef container_of |
50 | | /* return the pointer of type 'type *' containing 'ptr' as field 'member' */ |
51 | | #define container_of(ptr, type, member) ((type *)((uint8_t *)(ptr) - offsetof(type, member))) |
52 | | #endif |
53 | | |
54 | | #if !defined(_MSC_VER) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L |
55 | | #define minimum_length(n) static n |
56 | | #else |
57 | | #define minimum_length(n) n |
58 | | #endif |
59 | | |
60 | | typedef int BOOL; |
61 | | |
62 | | #ifndef FALSE |
63 | | enum { |
64 | | FALSE = 0, |
65 | | TRUE = 1, |
66 | | }; |
67 | | #endif |
68 | | |
69 | | void pstrcpy(char *buf, int buf_size, const char *str); |
70 | | char *pstrcat(char *buf, int buf_size, const char *s); |
71 | | int strstart(const char *str, const char *val, const char **ptr); |
72 | | int has_suffix(const char *str, const char *suffix); |
73 | | |
74 | | /* Prevent UB when n == 0 and (src == NULL or dest == NULL) */ |
75 | 2.03M | static inline void memcpy_no_ub(void *dest, const void *src, size_t n) { |
76 | 2.03M | if (n) |
77 | 2.03M | memcpy(dest, src, n); |
78 | 2.03M | } Unexecuted instantiation: libregexp.c:memcpy_no_ub Line | Count | Source | 75 | 2.03M | static inline void memcpy_no_ub(void *dest, const void *src, size_t n) { | 76 | 2.03M | if (n) | 77 | 2.03M | memcpy(dest, src, n); | 78 | 2.03M | } |
Unexecuted instantiation: libunicode.c:memcpy_no_ub |
79 | | |
80 | | static inline int max_int(int a, int b) |
81 | 860k | { |
82 | 860k | if (a > b) |
83 | 201k | return a; |
84 | 659k | else |
85 | 659k | return b; |
86 | 860k | } Unexecuted instantiation: libregexp.c:max_int Unexecuted instantiation: cutils.c:max_int Line | Count | Source | 81 | 860k | { | 82 | 860k | if (a > b) | 83 | 201k | return a; | 84 | 659k | else | 85 | 659k | return b; | 86 | 860k | } |
|
87 | | |
88 | | static inline int min_int(int a, int b) |
89 | 0 | { |
90 | 0 | if (a < b) |
91 | 0 | return a; |
92 | 0 | else |
93 | 0 | return b; |
94 | 0 | } Unexecuted instantiation: libregexp.c:min_int Unexecuted instantiation: cutils.c:min_int Unexecuted instantiation: libunicode.c:min_int |
95 | | |
96 | | static inline uint32_t max_uint32(uint32_t a, uint32_t b) |
97 | 0 | { |
98 | 0 | if (a > b) |
99 | 0 | return a; |
100 | 0 | else |
101 | 0 | return b; |
102 | 0 | } Unexecuted instantiation: libregexp.c:max_uint32 Unexecuted instantiation: cutils.c:max_uint32 Unexecuted instantiation: libunicode.c:max_uint32 |
103 | | |
104 | | static inline uint32_t min_uint32(uint32_t a, uint32_t b) |
105 | 0 | { |
106 | 0 | if (a < b) |
107 | 0 | return a; |
108 | 0 | else |
109 | 0 | return b; |
110 | 0 | } Unexecuted instantiation: libregexp.c:min_uint32 Unexecuted instantiation: cutils.c:min_uint32 Unexecuted instantiation: libunicode.c:min_uint32 |
111 | | |
112 | | static inline int64_t max_int64(int64_t a, int64_t b) |
113 | 0 | { |
114 | 0 | if (a > b) |
115 | 0 | return a; |
116 | 0 | else |
117 | 0 | return b; |
118 | 0 | } Unexecuted instantiation: libregexp.c:max_int64 Unexecuted instantiation: cutils.c:max_int64 Unexecuted instantiation: libunicode.c:max_int64 |
119 | | |
120 | | static inline int64_t min_int64(int64_t a, int64_t b) |
121 | 0 | { |
122 | 0 | if (a < b) |
123 | 0 | return a; |
124 | 0 | else |
125 | 0 | return b; |
126 | 0 | } Unexecuted instantiation: libregexp.c:min_int64 Unexecuted instantiation: cutils.c:min_int64 Unexecuted instantiation: libunicode.c:min_int64 |
127 | | |
128 | | /* WARNING: undefined if a = 0 */ |
129 | | static inline int clz32(unsigned int a) |
130 | 0 | { |
131 | 0 | return __builtin_clz(a); |
132 | 0 | } Unexecuted instantiation: libregexp.c:clz32 Unexecuted instantiation: cutils.c:clz32 Unexecuted instantiation: libunicode.c:clz32 |
133 | | |
134 | | /* WARNING: undefined if a = 0 */ |
135 | | static inline int clz64(uint64_t a) |
136 | 0 | { |
137 | 0 | return __builtin_clzll(a); |
138 | 0 | } Unexecuted instantiation: libregexp.c:clz64 Unexecuted instantiation: cutils.c:clz64 Unexecuted instantiation: libunicode.c:clz64 |
139 | | |
140 | | /* WARNING: undefined if a = 0 */ |
141 | | static inline int ctz32(unsigned int a) |
142 | 0 | { |
143 | 0 | return __builtin_ctz(a); |
144 | 0 | } Unexecuted instantiation: libregexp.c:ctz32 Unexecuted instantiation: cutils.c:ctz32 Unexecuted instantiation: libunicode.c:ctz32 |
145 | | |
146 | | /* WARNING: undefined if a = 0 */ |
147 | | static inline int ctz64(uint64_t a) |
148 | 0 | { |
149 | 0 | return __builtin_ctzll(a); |
150 | 0 | } Unexecuted instantiation: libregexp.c:ctz64 Unexecuted instantiation: cutils.c:ctz64 Unexecuted instantiation: libunicode.c:ctz64 |
151 | | |
152 | | struct __attribute__((packed)) packed_u64 { |
153 | | uint64_t v; |
154 | | }; |
155 | | |
156 | | struct __attribute__((packed)) packed_u32 { |
157 | | uint32_t v; |
158 | | }; |
159 | | |
160 | | struct __attribute__((packed)) packed_u16 { |
161 | | uint16_t v; |
162 | | }; |
163 | | |
164 | | static inline uint64_t get_u64(const uint8_t *tab) |
165 | 0 | { |
166 | 0 | return ((const struct packed_u64 *)tab)->v; |
167 | 0 | } Unexecuted instantiation: libregexp.c:get_u64 Unexecuted instantiation: cutils.c:get_u64 Unexecuted instantiation: libunicode.c:get_u64 |
168 | | |
169 | | static inline int64_t get_i64(const uint8_t *tab) |
170 | 0 | { |
171 | 0 | return (int64_t)((const struct packed_u64 *)tab)->v; |
172 | 0 | } Unexecuted instantiation: libregexp.c:get_i64 Unexecuted instantiation: cutils.c:get_i64 Unexecuted instantiation: libunicode.c:get_i64 |
173 | | |
174 | | static inline void put_u64(uint8_t *tab, uint64_t val) |
175 | 0 | { |
176 | 0 | ((struct packed_u64 *)tab)->v = val; |
177 | 0 | } Unexecuted instantiation: libregexp.c:put_u64 Unexecuted instantiation: cutils.c:put_u64 Unexecuted instantiation: libunicode.c:put_u64 |
178 | | |
179 | | static inline uint32_t get_u32(const uint8_t *tab) |
180 | 3.96M | { |
181 | 3.96M | return ((const struct packed_u32 *)tab)->v; |
182 | 3.96M | } Line | Count | Source | 180 | 3.96M | { | 181 | 3.96M | return ((const struct packed_u32 *)tab)->v; | 182 | 3.96M | } |
Unexecuted instantiation: cutils.c:get_u32 Unexecuted instantiation: libunicode.c:get_u32 |
183 | | |
184 | | static inline int32_t get_i32(const uint8_t *tab) |
185 | 0 | { |
186 | 0 | return (int32_t)((const struct packed_u32 *)tab)->v; |
187 | 0 | } Unexecuted instantiation: libregexp.c:get_i32 Unexecuted instantiation: cutils.c:get_i32 Unexecuted instantiation: libunicode.c:get_i32 |
188 | | |
189 | | static inline void put_u32(uint8_t *tab, uint32_t val) |
190 | 27.8k | { |
191 | 27.8k | ((struct packed_u32 *)tab)->v = val; |
192 | 27.8k | } Line | Count | Source | 190 | 27.8k | { | 191 | 27.8k | ((struct packed_u32 *)tab)->v = val; | 192 | 27.8k | } |
Unexecuted instantiation: cutils.c:put_u32 Unexecuted instantiation: libunicode.c:put_u32 |
193 | | |
194 | | static inline uint32_t get_u16(const uint8_t *tab) |
195 | 79.2M | { |
196 | 79.2M | return ((const struct packed_u16 *)tab)->v; |
197 | 79.2M | } Line | Count | Source | 195 | 79.2M | { | 196 | 79.2M | return ((const struct packed_u16 *)tab)->v; | 197 | 79.2M | } |
Unexecuted instantiation: cutils.c:get_u16 Unexecuted instantiation: libunicode.c:get_u16 |
198 | | |
199 | | static inline int32_t get_i16(const uint8_t *tab) |
200 | 0 | { |
201 | 0 | return (int16_t)((const struct packed_u16 *)tab)->v; |
202 | 0 | } Unexecuted instantiation: libregexp.c:get_i16 Unexecuted instantiation: cutils.c:get_i16 Unexecuted instantiation: libunicode.c:get_i16 |
203 | | |
204 | | static inline void put_u16(uint8_t *tab, uint16_t val) |
205 | 16 | { |
206 | 16 | ((struct packed_u16 *)tab)->v = val; |
207 | 16 | } Line | Count | Source | 205 | 16 | { | 206 | 16 | ((struct packed_u16 *)tab)->v = val; | 207 | 16 | } |
Unexecuted instantiation: cutils.c:put_u16 Unexecuted instantiation: libunicode.c:put_u16 |
208 | | |
209 | | static inline uint32_t get_u8(const uint8_t *tab) |
210 | 0 | { |
211 | 0 | return *tab; |
212 | 0 | } Unexecuted instantiation: libregexp.c:get_u8 Unexecuted instantiation: cutils.c:get_u8 Unexecuted instantiation: libunicode.c:get_u8 |
213 | | |
214 | | static inline int32_t get_i8(const uint8_t *tab) |
215 | 0 | { |
216 | 0 | return (int8_t)*tab; |
217 | 0 | } Unexecuted instantiation: libregexp.c:get_i8 Unexecuted instantiation: cutils.c:get_i8 Unexecuted instantiation: libunicode.c:get_i8 |
218 | | |
219 | | static inline void put_u8(uint8_t *tab, uint8_t val) |
220 | 0 | { |
221 | 0 | *tab = val; |
222 | 0 | } Unexecuted instantiation: libregexp.c:put_u8 Unexecuted instantiation: cutils.c:put_u8 Unexecuted instantiation: libunicode.c:put_u8 |
223 | | |
224 | | #ifndef bswap16 |
225 | | static inline uint16_t bswap16(uint16_t x) |
226 | 0 | { |
227 | 0 | return (x >> 8) | (x << 8); |
228 | 0 | } Unexecuted instantiation: libregexp.c:bswap16 Unexecuted instantiation: cutils.c:bswap16 Unexecuted instantiation: libunicode.c:bswap16 |
229 | | #endif |
230 | | |
231 | | #ifndef bswap32 |
232 | | static inline uint32_t bswap32(uint32_t v) |
233 | 0 | { |
234 | 0 | return ((v & 0xff000000) >> 24) | ((v & 0x00ff0000) >> 8) | |
235 | 0 | ((v & 0x0000ff00) << 8) | ((v & 0x000000ff) << 24); |
236 | 0 | } Unexecuted instantiation: libregexp.c:bswap32 Unexecuted instantiation: cutils.c:bswap32 Unexecuted instantiation: libunicode.c:bswap32 |
237 | | #endif |
238 | | |
239 | | #ifndef bswap64 |
240 | | static inline uint64_t bswap64(uint64_t v) |
241 | 0 | { |
242 | 0 | return ((v & ((uint64_t)0xff << (7 * 8))) >> (7 * 8)) | |
243 | 0 | ((v & ((uint64_t)0xff << (6 * 8))) >> (5 * 8)) | |
244 | 0 | ((v & ((uint64_t)0xff << (5 * 8))) >> (3 * 8)) | |
245 | 0 | ((v & ((uint64_t)0xff << (4 * 8))) >> (1 * 8)) | |
246 | 0 | ((v & ((uint64_t)0xff << (3 * 8))) << (1 * 8)) | |
247 | 0 | ((v & ((uint64_t)0xff << (2 * 8))) << (3 * 8)) | |
248 | 0 | ((v & ((uint64_t)0xff << (1 * 8))) << (5 * 8)) | |
249 | 0 | ((v & ((uint64_t)0xff << (0 * 8))) << (7 * 8)); |
250 | 0 | } Unexecuted instantiation: libregexp.c:bswap64 Unexecuted instantiation: cutils.c:bswap64 Unexecuted instantiation: libunicode.c:bswap64 |
251 | | #endif |
252 | | |
253 | | /* XXX: should take an extra argument to pass slack information to the caller */ |
254 | | typedef void *DynBufReallocFunc(void *opaque, void *ptr, size_t size); |
255 | | |
256 | | typedef struct DynBuf { |
257 | | uint8_t *buf; |
258 | | size_t size; |
259 | | size_t allocated_size; |
260 | | BOOL error; /* true if a memory allocation error occurred */ |
261 | | DynBufReallocFunc *realloc_func; |
262 | | void *opaque; /* for realloc_func */ |
263 | | } DynBuf; |
264 | | |
265 | | void dbuf_init(DynBuf *s); |
266 | | void dbuf_init2(DynBuf *s, void *opaque, DynBufReallocFunc *realloc_func); |
267 | | int dbuf_realloc(DynBuf *s, size_t new_size); |
268 | | int dbuf_write(DynBuf *s, size_t offset, const uint8_t *data, size_t len); |
269 | | int dbuf_put(DynBuf *s, const uint8_t *data, size_t len); |
270 | | int dbuf_put_self(DynBuf *s, size_t offset, size_t len); |
271 | | int dbuf_putc(DynBuf *s, uint8_t c); |
272 | | int dbuf_putstr(DynBuf *s, const char *str); |
273 | | static inline int dbuf_put_u16(DynBuf *s, uint16_t val) |
274 | 1.42M | { |
275 | 1.42M | return dbuf_put(s, (uint8_t *)&val, 2); |
276 | 1.42M | } Line | Count | Source | 274 | 1.42M | { | 275 | 1.42M | return dbuf_put(s, (uint8_t *)&val, 2); | 276 | 1.42M | } |
Unexecuted instantiation: cutils.c:dbuf_put_u16 Unexecuted instantiation: libunicode.c:dbuf_put_u16 |
277 | | static inline int dbuf_put_u32(DynBuf *s, uint32_t val) |
278 | 51.4k | { |
279 | 51.4k | return dbuf_put(s, (uint8_t *)&val, 4); |
280 | 51.4k | } Line | Count | Source | 278 | 51.4k | { | 279 | 51.4k | return dbuf_put(s, (uint8_t *)&val, 4); | 280 | 51.4k | } |
Unexecuted instantiation: cutils.c:dbuf_put_u32 Unexecuted instantiation: libunicode.c:dbuf_put_u32 |
281 | | static inline int dbuf_put_u64(DynBuf *s, uint64_t val) |
282 | 0 | { |
283 | 0 | return dbuf_put(s, (uint8_t *)&val, 8); |
284 | 0 | } Unexecuted instantiation: libregexp.c:dbuf_put_u64 Unexecuted instantiation: cutils.c:dbuf_put_u64 Unexecuted instantiation: libunicode.c:dbuf_put_u64 |
285 | | int __attribute__((format(printf, 2, 3))) dbuf_printf(DynBuf *s, |
286 | | const char *fmt, ...); |
287 | | void dbuf_free(DynBuf *s); |
288 | 25.3k | static inline BOOL dbuf_error(DynBuf *s) { |
289 | 25.3k | return s->error; |
290 | 25.3k | } Line | Count | Source | 288 | 25.3k | static inline BOOL dbuf_error(DynBuf *s) { | 289 | 25.3k | return s->error; | 290 | 25.3k | } |
Unexecuted instantiation: cutils.c:dbuf_error Unexecuted instantiation: libunicode.c:dbuf_error |
291 | | static inline void dbuf_set_error(DynBuf *s) |
292 | 0 | { |
293 | 0 | s->error = TRUE; |
294 | 0 | } Unexecuted instantiation: libregexp.c:dbuf_set_error Unexecuted instantiation: cutils.c:dbuf_set_error Unexecuted instantiation: libunicode.c:dbuf_set_error |
295 | | |
296 | 55.2k | #define UTF8_CHAR_LEN_MAX 6 |
297 | | |
298 | | int unicode_to_utf8(uint8_t *buf, unsigned int c); |
299 | | int unicode_from_utf8(const uint8_t *p, int max_len, const uint8_t **pp); |
300 | | |
301 | | static inline BOOL is_surrogate(uint32_t c) |
302 | 0 | { |
303 | 0 | return (c >> 11) == (0xD800 >> 11); // 0xD800-0xDFFF |
304 | 0 | } Unexecuted instantiation: libregexp.c:is_surrogate Unexecuted instantiation: cutils.c:is_surrogate Unexecuted instantiation: libunicode.c:is_surrogate |
305 | | |
306 | | static inline BOOL is_hi_surrogate(uint32_t c) |
307 | 12.0k | { |
308 | 12.0k | return (c >> 10) == (0xD800 >> 10); // 0xD800-0xDBFF |
309 | 12.0k | } libregexp.c:is_hi_surrogate Line | Count | Source | 307 | 12.0k | { | 308 | 12.0k | return (c >> 10) == (0xD800 >> 10); // 0xD800-0xDBFF | 309 | 12.0k | } |
Unexecuted instantiation: cutils.c:is_hi_surrogate Unexecuted instantiation: libunicode.c:is_hi_surrogate |
310 | | |
311 | | static inline BOOL is_lo_surrogate(uint32_t c) |
312 | 1.41k | { |
313 | 1.41k | return (c >> 10) == (0xDC00 >> 10); // 0xDC00-0xDFFF |
314 | 1.41k | } libregexp.c:is_lo_surrogate Line | Count | Source | 312 | 1.41k | { | 313 | 1.41k | return (c >> 10) == (0xDC00 >> 10); // 0xDC00-0xDFFF | 314 | 1.41k | } |
Unexecuted instantiation: cutils.c:is_lo_surrogate Unexecuted instantiation: libunicode.c:is_lo_surrogate |
315 | | |
316 | | static inline uint32_t get_hi_surrogate(uint32_t c) |
317 | 0 | { |
318 | 0 | return (c >> 10) - (0x10000 >> 10) + 0xD800; |
319 | 0 | } Unexecuted instantiation: libregexp.c:get_hi_surrogate Unexecuted instantiation: cutils.c:get_hi_surrogate Unexecuted instantiation: libunicode.c:get_hi_surrogate |
320 | | |
321 | | static inline uint32_t get_lo_surrogate(uint32_t c) |
322 | 0 | { |
323 | 0 | return (c & 0x3FF) | 0xDC00; |
324 | 0 | } Unexecuted instantiation: libregexp.c:get_lo_surrogate Unexecuted instantiation: cutils.c:get_lo_surrogate Unexecuted instantiation: libunicode.c:get_lo_surrogate |
325 | | |
326 | | static inline uint32_t from_surrogate(uint32_t hi, uint32_t lo) |
327 | 640 | { |
328 | 640 | return 0x10000 + 0x400 * (hi - 0xD800) + (lo - 0xDC00); |
329 | 640 | } libregexp.c:from_surrogate Line | Count | Source | 327 | 640 | { | 328 | 640 | return 0x10000 + 0x400 * (hi - 0xD800) + (lo - 0xDC00); | 329 | 640 | } |
Unexecuted instantiation: cutils.c:from_surrogate Unexecuted instantiation: libunicode.c:from_surrogate |
330 | | |
331 | | static inline int from_hex(int c) |
332 | 62.2k | { |
333 | 62.2k | if (c >= '0' && c <= '9') |
334 | 20.4k | return c - '0'; |
335 | 41.8k | else if (c >= 'A' && c <= 'F') |
336 | 15.9k | return c - 'A' + 10; |
337 | 25.8k | else if (c >= 'a' && c <= 'f') |
338 | 15.6k | return c - 'a' + 10; |
339 | 10.1k | else |
340 | 10.1k | return -1; |
341 | 62.2k | } Line | Count | Source | 332 | 62.2k | { | 333 | 62.2k | if (c >= '0' && c <= '9') | 334 | 20.4k | return c - '0'; | 335 | 41.8k | else if (c >= 'A' && c <= 'F') | 336 | 15.9k | return c - 'A' + 10; | 337 | 25.8k | else if (c >= 'a' && c <= 'f') | 338 | 15.6k | return c - 'a' + 10; | 339 | 10.1k | else | 340 | 10.1k | return -1; | 341 | 62.2k | } |
Unexecuted instantiation: cutils.c:from_hex Unexecuted instantiation: libunicode.c:from_hex |
342 | | |
343 | | void rqsort(void *base, size_t nmemb, size_t size, |
344 | | int (*cmp)(const void *, const void *, void *), |
345 | | void *arg); |
346 | | |
347 | | static inline uint64_t float64_as_uint64(double d) |
348 | 0 | { |
349 | 0 | union { |
350 | 0 | double d; |
351 | 0 | uint64_t u64; |
352 | 0 | } u; |
353 | 0 | u.d = d; |
354 | 0 | return u.u64; |
355 | 0 | } Unexecuted instantiation: libregexp.c:float64_as_uint64 Unexecuted instantiation: cutils.c:float64_as_uint64 Unexecuted instantiation: libunicode.c:float64_as_uint64 |
356 | | |
357 | | static inline double uint64_as_float64(uint64_t u64) |
358 | 0 | { |
359 | 0 | union { |
360 | 0 | double d; |
361 | 0 | uint64_t u64; |
362 | 0 | } u; |
363 | 0 | u.u64 = u64; |
364 | 0 | return u.d; |
365 | 0 | } Unexecuted instantiation: libregexp.c:uint64_as_float64 Unexecuted instantiation: cutils.c:uint64_as_float64 Unexecuted instantiation: libunicode.c:uint64_as_float64 |
366 | | |
367 | | static inline double fromfp16(uint16_t v) |
368 | 0 | { |
369 | 0 | double d; |
370 | 0 | uint32_t v1; |
371 | 0 | v1 = v & 0x7fff; |
372 | 0 | if (unlikely(v1 >= 0x7c00)) |
373 | 0 | v1 += 0x1f8000; /* NaN or infinity */ |
374 | 0 | d = uint64_as_float64(((uint64_t)(v >> 15) << 63) | ((uint64_t)v1 << (52 - 10))); |
375 | 0 | return d * 0x1p1008; |
376 | 0 | } Unexecuted instantiation: libregexp.c:fromfp16 Unexecuted instantiation: cutils.c:fromfp16 Unexecuted instantiation: libunicode.c:fromfp16 |
377 | | |
378 | | static inline uint16_t tofp16(double d) |
379 | 0 | { |
380 | 0 | uint64_t a, addend; |
381 | 0 | uint32_t v, sgn; |
382 | 0 | int shift; |
383 | 0 | |
384 | 0 | a = float64_as_uint64(d); |
385 | 0 | sgn = a >> 63; |
386 | 0 | a = a & 0x7fffffffffffffff; |
387 | 0 | if (unlikely(a > 0x7ff0000000000000)) { |
388 | 0 | /* nan */ |
389 | 0 | v = 0x7c01; |
390 | 0 | } else if (a < 0x3f10000000000000) { /* 0x1p-14 */ |
391 | 0 | /* subnormal f16 number or zero */ |
392 | 0 | if (a <= 0x3e60000000000000) { /* 0x1p-25 */ |
393 | 0 | v = 0x0000; /* zero */ |
394 | 0 | } else { |
395 | 0 | shift = 1051 - (a >> 52); |
396 | 0 | a = ((uint64_t)1 << 52) | (a & (((uint64_t)1 << 52) - 1)); |
397 | 0 | addend = ((a >> shift) & 1) + (((uint64_t)1 << (shift - 1)) - 1); |
398 | 0 | v = (a + addend) >> shift; |
399 | 0 | } |
400 | 0 | } else { |
401 | 0 | /* normal number or infinity */ |
402 | 0 | a -= 0x3f00000000000000; /* adjust the exponent */ |
403 | 0 | /* round */ |
404 | 0 | addend = ((a >> (52 - 10)) & 1) + (((uint64_t)1 << (52 - 11)) - 1); |
405 | 0 | v = (a + addend) >> (52 - 10); |
406 | 0 | /* overflow ? */ |
407 | 0 | if (unlikely(v > 0x7c00)) |
408 | 0 | v = 0x7c00; |
409 | 0 | } |
410 | 0 | return v | (sgn << 15); |
411 | 0 | } Unexecuted instantiation: libregexp.c:tofp16 Unexecuted instantiation: cutils.c:tofp16 Unexecuted instantiation: libunicode.c:tofp16 |
412 | | |
413 | | static inline int isfp16nan(uint16_t v) |
414 | 0 | { |
415 | 0 | return (v & 0x7FFF) > 0x7C00; |
416 | 0 | } Unexecuted instantiation: libregexp.c:isfp16nan Unexecuted instantiation: cutils.c:isfp16nan Unexecuted instantiation: libunicode.c:isfp16nan |
417 | | |
418 | | static inline int isfp16zero(uint16_t v) |
419 | 0 | { |
420 | 0 | return (v & 0x7FFF) == 0; |
421 | 0 | } Unexecuted instantiation: libregexp.c:isfp16zero Unexecuted instantiation: cutils.c:isfp16zero Unexecuted instantiation: libunicode.c:isfp16zero |
422 | | |
423 | | #endif /* CUTILS_H */ |