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