-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.h
374 lines (293 loc) · 9.41 KB
/
calc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
calc.h
calc utilities
Copyright (c) 2024 yuesong-feng 冯岳松
Version 1.0 2024/09/05
Version 1.1 2024/09/18 add calc_abstime
*/
#ifndef CALC_H
#define CALC_H
#include "basic.h" // for unlikely
#include <stdbool.h> // for bool
#include <stdint.h> // for (u)int(n)_t
#include <sys/time.h> // for struct timeval
#include <time.h> // for calc_abstime
#include "dbg.h"
/** Calculates fast the remainder of n/m when m is a power of two.
@param n in: numerator
@param m in: denominator, must be a power of two
@return the remainder of n/m */
#define calc_2pow_remainder(n, m) ((n) & ((m)-1))
/** Calculates the biggest multiple of m that is not bigger than n
when m is a power of two. In other words, rounds n down to m * k.
@param n in: number to round down
@param m in: alignment, must be a power of two
@return n rounded down to the biggest possible integer multiple of m */
#define calc_2pow_round(n, m) ((n) & ~((m)-1))
/** Align a number down to a multiple of a power of two.
@param n in: number to round down
@param m in: alignment, must be a power of two
@return n rounded down to the biggest possible integer multiple of m */
#define calc_align_down(n, m) wl_2pow_round(n, m)
/** Calculates the smallest multiple of m that is not smaller than n
when m is a power of two. In other words, rounds n up to m * k.
@param n in: number to round up
@param m in: alignment, must be a power of two
@return n rounded up to the smallest possible integer multiple of m */
#define calc_align(n, m) (((n) + ((m)-1)) & ~((m)-1))
/** Calculates fast the 2-logarithm of a number, rounded upward to an
integer.
@return logarithm in the base 2, rounded upward */
static inline long calc_2_log(long n) /*!< in: number != 0 */
{
long res = 0;
wl_ad(n > 0);
n = n - 1;
for (;;) {
n = n / 2;
if (n == 0) {
break;
}
res++;
}
return (res + 1);
}
/** Calculates 2 to power n.
@param[in] n power of 2
@return 2 to power n */
static inline uint32_t calc_2_exp(uint32_t n) { return (1 << n); }
/** Calculates fast the number rounded up to the nearest power of 2.
@return first power of 2 which is >= n */
long calc_2_power_up(long n) /*!< in: number != 0 */
{
long res;
res = 1;
wl_ad(n > 0);
while (res < n) {
res = res * 2;
}
return (res);
}
/** Determine how many bytes (groups of 8 bits) are needed to
store the given number of bits.
@param b in: bits
@return number of bytes (octets) needed to represent b */
#define BITS_IN_BYTES(b) (((b) + 7UL) / 8UL)
/** Determines if a number is zero or a power of two.
@param[in] n number
@return nonzero if n is zero or a power of two; zero otherwise */
#define calc_is_2pow(n) likely(!((n) & ((n)-1)))
uint64_t find_prime(uint64_t n) {
uint64_t pow2;
uint64_t i;
double random1 = 1.0412321;
double random2 = 1.1131347;
double random3 = 1.0132677;
n += 100;
pow2 = 1;
while (pow2 * 2 < n) {
pow2 = 2 * pow2;
}
if ((double)n < 1.05 * (double)pow2) {
n = (uint64_t)((double)n * random1);
}
pow2 = 2 * pow2;
if ((double)n > 0.95 * (double)pow2) {
n = (uint64_t)((double)n * random2);
}
if (n > pow2 - 20) {
n += 30;
}
/* Now we have n far enough from powers of 2. To make
n more random (especially, if it was not near
a power of 2), we then multiply it by a random number. */
n = (uint64_t)((double)n * random3);
for (;; n++) {
i = 2;
while (i * i <= n) {
if (n % i == 0) {
goto next_n;
}
i++;
}
/* Found a prime */
break;
next_n:;
}
return (n);
}
/////////
#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))
#define overflow_if(cond) \
if (unlikely(cond)) \
return true;
// from PosrgreSQL
static inline bool int8_add_overflow(int8_t a, int8_t b, int8_t *result) {
int16_t res = (int16_t)a + (int16_t)b;
overflow_if(res > INT8_MAX || res < INT8_MIN);
*result = (int8_t)res;
return false;
}
static inline bool int16_add_overflow(int16_t a, int16_t b, int16_t *result) {
int32_t res = (int32_t)a + (int32_t)b;
overflow_if(res > INT16_MAX || res < INT16_MIN);
*result = (int16_t)res;
return false;
}
static inline bool int32_add_overflow(int32_t a, int32_t b, int32_t *result) {
int64_t res = (int64_t)a + (int64_t)b;
overflow_if(res > INT32_MAX || res < INT32_MIN);
*result = (int32_t)res;
return false;
}
static inline bool int64_add_overflow(int64_t a, int64_t b, int64_t *result) {
overflow_if((a > 0 && b > 0 && a > INT64_MAX - b) ||
(a < 0 && b < 0 && a < INT64_MIN - b));
*result = a + b;
return false;
}
static inline bool uint8_add_overflow(uint8_t a, uint8_t b, uint8_t *result) {
uint8_t res = a + b;
overflow_if(res < a);
*result = res;
return false;
}
static inline bool uint16_add_overflow(uint16_t a, uint16_t b, uint16_t *result) {
uint16_t res = a + b;
overflow_if(res < a);
*result = res;
return false;
}
static inline bool uint32_add_overflow(uint32_t a, uint32_t b, uint32_t *result) {
uint32_t res = a + b;
overflow_if(res < a);
*result = res;
return false;
}
static inline bool uint64_add_overflow(uint64_t a, uint64_t b, uint64_t *result) {
uint64_t res = a + b;
overflow_if(res < a);
*result = res;
return false;
}
static inline bool int8_sub_overflow(int8_t a, int8_t b, int8_t *result) {
int16_t res = (int16_t)a - (int16_t)b;
overflow_if(res > INT8_MAX || res < INT8_MIN);
*result = (int8_t)res;
return false;
}
static inline bool int16_sub_overflow(int16_t a, int16_t b, int16_t *result) {
int32_t res = (int32_t)a - (int32_t)b;
overflow_if(res > INT16_MAX || res < INT16_MIN);
*result = (int16_t)res;
return false;
}
static inline bool int32_sub_overflow(int32_t a, int32_t b, int32_t *result) {
int64_t res = (int64_t)a - (int64_t)b;
overflow_if(res > INT32_MAX || res < INT32_MIN);
*result = (int32_t)res;
return false;
}
static inline bool int64_sub_overflow(int64_t a, int64_t b, int64_t *result) {
overflow_if((a < 0 && b > 0 && a < INT64_MIN + b) ||
(a >= 0 && b < 0 && a > INT64_MAX + b));
*result = a - b;
return false;
}
static inline bool uint8_sub_overflow(uint8_t a, uint8_t b, uint8_t *result) {
overflow_if(b > a);
*result = a - b;
return false;
}
static inline bool uint16_sub_overflow(uint16_t a, uint16_t b, uint16_t *result) {
overflow_if(b > a);
*result = a - b;
return false;
}
static inline bool uint32_sub_overflow(uint32_t a, uint32_t b, uint32_t *result) {
overflow_if(b > a);
*result = a - b;
return false;
}
static inline bool uint64_sub_overflow(uint64_t a, uint64_t b, uint64_t *result) {
overflow_if(b > a);
*result = a - b;
return false;
}
static inline bool int8_mul_overflow(int8_t a, int8_t b, int8_t *result) {
int16_t res = (int16_t)a * (int16_t)b;
overflow_if(res > INT8_MAX || res < INT8_MIN);
*result = (int8_t)res;
return false;
}
static inline bool int16_mul_overflow(int16_t a, int16_t b, int16_t *result) {
int32_t res = (int32_t)a * (int32_t)b;
overflow_if(res > INT16_MAX || res < INT16_MIN);
*result = (int16_t)res;
return false;
}
static inline bool int32_mul_overflow(int32_t a, int32_t b, int32_t *result) {
int64_t res = (int64_t)a * (int64_t)b;
overflow_if(res > INT32_MAX || res < INT32_MIN);
*result = (int32_t)res;
return false;
}
static inline bool int64_mul_overflow(int64_t a, int64_t b, int64_t *result) {
overflow_if((a > INT32_MAX || a < INT32_MIN || b > INT32_MAX || b < INT32_MIN) &&
a != 0 && a != 1 && b != 0 && b != 1 &&
((a > 0 && b > 0 && a > INT64_MAX / b) ||
(a > 0 && b < 0 && b < INT64_MIN / a) ||
(a < 0 && b > 0 && a < INT64_MIN / b) ||
(a < 0 && b < 0 && a < INT64_MAX / b)));
*result = a * b;
return false;
}
static inline bool uint8_mul_overflow(uint8_t a, uint8_t b, uint8_t *result) {
uint16_t res = (uint16_t)a * (uint16_t)b;
overflow_if(res > UINT8_MAX);
*result = (uint8_t)res;
return false;
}
static inline bool uint16_mul_overflow(uint16_t a, uint16_t b, uint16_t *result) {
uint32_t res = (uint32_t)a * (uint32_t)b;
overflow_if(res > UINT16_MAX);
*result = (uint16_t)res;
return false;
}
static inline bool uint32_mul_overflow(uint32_t a, uint32_t b, uint32_t *result) {
uint64_t res = (uint64_t)a * (uint64_t)b;
overflow_if(res > UINT32_MAX);
*result = (uint32_t)res;
return false;
}
static inline bool uint64_mul_overflow(uint64_t a, uint64_t b, uint64_t *result) {
uint64_t res = a * b;
overflow_if(a != 0 && b != res / a);
*result = res;
return false;
}
// used by event_timedwait and semaphore_p_timedwait
// convert microseconds to abstime, from MySQL 5.6.51
#define INFINITE_TIME ((unsigned long)(-1)) // ULINT_UNDEFINED
static inline void calc_abstime(unsigned long time_in_usec /*timeout in microseconds, or INFINITE_TIME*/, struct timespec *abstime) {
const unsigned long MICROSECS_IN_A_SECOND = 1000000;
if (time_in_usec != INFINITE_TIME) {
struct timeval tv;
int ret;
ret = gettimeofday(&tv, NULL);
wl_ad(ret == 0);
tv.tv_usec += time_in_usec;
if ((unsigned long)tv.tv_usec >= MICROSECS_IN_A_SECOND) {
tv.tv_sec += time_in_usec / MICROSECS_IN_A_SECOND;
tv.tv_usec %= MICROSECS_IN_A_SECOND;
}
abstime->tv_sec = tv.tv_sec;
abstime->tv_nsec = tv.tv_usec * 1000;
} else {
abstime->tv_nsec = 999999999;
abstime->tv_sec = (time_t)(unsigned long)(-2); // ULINT_MAX
}
wl_ad(abstime->tv_nsec <= 999999999);
}
#endif