/src/njs/src/njs_cutils.h
Line | Count | Source |
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 | 37.0M | #define likely(x) __builtin_expect(!!(x), 1) |
15 | | #endif |
16 | | |
17 | | #ifndef unlikely |
18 | 35.4k | #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 | 41.1M | #define FALSE 0 |
34 | | #endif |
35 | | |
36 | | #ifndef TRUE |
37 | 14.5M | #define TRUE 1 |
38 | | #endif |
39 | | |
40 | | static inline int |
41 | | max_int(int a, int b) |
42 | 346k | { |
43 | 346k | return (a > b) ? a : b; |
44 | 346k | } |
45 | | |
46 | | static inline int |
47 | | min_int(int a, int b) |
48 | 31.4M | { |
49 | 31.4M | return (a < b) ? a : b; |
50 | 31.4M | } |
51 | | |
52 | | static inline uint32_t |
53 | | max_uint32(uint32_t a, uint32_t b) |
54 | 0 | { |
55 | 0 | return (a > b) ? a : b; |
56 | 0 | } |
57 | | |
58 | | static inline uint32_t |
59 | | min_uint32(uint32_t a, uint32_t b) |
60 | 0 | { |
61 | 0 | return (a < b) ? a : b; |
62 | 0 | } |
63 | | |
64 | | static inline int64_t |
65 | | max_int64(int64_t a, int64_t b) |
66 | 0 | { |
67 | 0 | return (a > b) ? a : b; |
68 | 0 | } |
69 | | |
70 | | static inline int64_t |
71 | | min_int64(int64_t a, int64_t b) |
72 | 0 | { |
73 | 0 | return (a < b) ? a : b; |
74 | 0 | } |
75 | | |
76 | | /* WARNING: undefined if a = 0 */ |
77 | | static inline int |
78 | | clz32(unsigned int a) |
79 | 4.32M | { |
80 | 4.32M | return __builtin_clz(a); |
81 | 4.32M | } |
82 | | |
83 | | /* WARNING: undefined if a = 0 */ |
84 | | static inline int |
85 | | clz64(uint64_t a) |
86 | 1.67k | { |
87 | 1.67k | return __builtin_clzll(a); |
88 | 1.67k | } |
89 | | |
90 | | /* WARNING: undefined if a = 0 */ |
91 | | static inline int |
92 | | ctz32(unsigned int a) |
93 | 80.7M | { |
94 | 80.7M | return __builtin_ctz(a); |
95 | 80.7M | } |
96 | | |
97 | | /* WARNING: undefined if a = 0 */ |
98 | | static inline int |
99 | | ctz64(uint64_t a) |
100 | 0 | { |
101 | 0 | return __builtin_ctzll(a); |
102 | 0 | } |
103 | | |
104 | | static inline uint64_t |
105 | | float64_as_uint64(double d) |
106 | 63.4M | { |
107 | 63.4M | union { |
108 | 63.4M | double d; |
109 | 63.4M | uint64_t u64; |
110 | 63.4M | } u; |
111 | | |
112 | 63.4M | u.d = d; |
113 | 63.4M | return u.u64; |
114 | 63.4M | } |
115 | | |
116 | | static inline double |
117 | | uint64_as_float64(uint64_t u64) |
118 | 3.31M | { |
119 | 3.31M | union { |
120 | 3.31M | double d; |
121 | 3.31M | uint64_t u64; |
122 | 3.31M | } u; |
123 | | |
124 | 3.31M | u.u64 = u64; |
125 | 3.31M | return u.d; |
126 | 3.31M | } |
127 | | |
128 | | static inline int |
129 | | strstart(const char *str, const char *val, const char **ptr) |
130 | 17.0M | { |
131 | 17.0M | const char *p = str; |
132 | 17.0M | const char *q = val; |
133 | | |
134 | 17.1M | while (*q != '\0') { |
135 | 17.1M | if (*p != *q) { |
136 | 17.0M | return 0; |
137 | 17.0M | } |
138 | 34.6k | p++; |
139 | 34.6k | q++; |
140 | 34.6k | } |
141 | | |
142 | 2.60k | if (ptr != NULL) { |
143 | 2.60k | *ptr = p; |
144 | 2.60k | } |
145 | | |
146 | 2.60k | return 1; |
147 | 17.0M | } |
148 | | |
149 | | #endif /* _NJS_CUTILS_H_INCLUDED_ */ |