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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
// Integer type infos and methods
#include <gc.h>
#include <gc/cord.h>
#include <gmp.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "array.h"
#include "datatypes.h"
#include "integers.h"
#include "text.h"
#include "types.h"
#include "SipHash/halfsiphash.h"
static gmp_randstate_t Int_rng = {};
public void Int$init_random(long seed)
{
gmp_randinit_default(Int_rng);
gmp_randseed_ui(Int_rng, (unsigned long)seed);
}
public Int_t Int$from_i64(int64_t i)
{
if (i == (int32_t)i) return (Int_t){.small=(i*4)+1};
mpz_t result;
mpz_init_set_si(result, i);
return Int$from_mpz(result);
}
public Int_t Int$from_num(double n)
{
mpz_t result;
mpz_init_set_d(result, n);
return Int$from_mpz(result);
}
public CORD Int$as_text(const Int_t *i, bool colorize, const TypeInfo *type) {
(void)type;
if (!i) return "Int";
if (__builtin_expect(i->small & 1, 1)) {
return CORD_asprintf(colorize ? "\x1b[35m%ld\x1b[33;2m\x1b[m" : "%ld", (i->small)>>2);
} else {
char *str = mpz_get_str(NULL, 10, *i->big);
return CORD_asprintf(colorize ? "\x1b[35m%s\x1b[33;2m\x1b[m" : "%s", str);
}
}
public int32_t Int$compare(const Int_t *x, const Int_t *y, const TypeInfo *type) {
(void)type;
if (__builtin_expect(((x->small & y->small) & 1) == 0, 0))
return mpz_cmp(*x->big, *y->big);
return (x->small > y->small) - (x->small < y->small);
}
public int32_t Int$compare_value(const Int_t x, const Int_t y) {
if (__builtin_expect(((x.small & y.small) & 1) == 0, 0))
return mpz_cmp(*x.big, *y.big);
return (x.small > y.small) - (x.small < y.small);
}
public bool Int$equal(const Int_t *x, const Int_t *y, const TypeInfo *type) {
(void)type;
return x->small == y->small || (__builtin_expect(((x->small & y->small) & 1) == 0, 0) && mpz_cmp(*x->big, *y->big) == 0);
}
public bool Int$equal_value(const Int_t x, const Int_t y) {
return x.small == y.small || (__builtin_expect(((x.small & y.small) & 1) == 0, 0) && mpz_cmp(*x.big, *y.big) == 0);
}
public uint32_t Int$hash(const Int_t *x, const TypeInfo *type) {
(void)type;
uint32_t hash;
if (__builtin_expect(x->small & 1, 1)) {
halfsiphash(&x->small, sizeof(x->small), TOMO_HASH_KEY, (uint8_t*)&hash, sizeof(hash));
} else {
char *str = mpz_get_str(NULL, 16, *x->big);
halfsiphash(str, strlen(str), TOMO_HASH_KEY, (uint8_t*)&hash, sizeof(hash));
}
return hash;
}
public CORD Int$format(Int_t i, Int_t digits_int)
{
int64_t digits = Int$as_i64(digits_int);
if (__builtin_expect(i.small & 1, 1)) {
return CORD_asprintf("%0.*ld", digits, (i.small)>>2);
} else {
CORD str = mpz_get_str(NULL, 10, *i.big);
bool negative = (str[0] == '-');
if (digits > (int64_t)CORD_len(str)) {
if (negative)
str = CORD_all("-", CORD_chars('0', digits - CORD_len(str)), CORD_substr(str, 1, ~0));
else
str = CORD_all(CORD_chars('0', digits - CORD_len(str)), str);
}
return str;
}
}
public CORD Int$hex(Int_t i, Int_t digits_int, bool uppercase, bool prefix) {
int64_t digits = Int$as_i64(digits_int);
const char *hex_fmt = uppercase ? (prefix ? "0x%0.*lX" : "%0.*lX") : (prefix ? "0x%0.*lx" : "%0.*lx");
if (__builtin_expect(i.small & 1, 1)) {
return CORD_asprintf(hex_fmt, digits, (i.small)>>2);
} else {
CORD str = mpz_get_str(NULL, 16, *i.big);
if (uppercase) str = Text$upper(str);
if (digits > (int64_t)CORD_len(str))
str = CORD_cat(CORD_chars('0', digits - CORD_len(str)), str);
if (prefix) str = CORD_cat("0x", str);
return str;
}
}
public CORD Int$octal(Int_t i, Int_t digits_int, bool prefix) {
int64_t digits = Int$as_i64(digits_int);
const char *octal_fmt = prefix ? "0o%0.*lo" : "%0.*lo";
if (__builtin_expect(i.small & 1, 1)) {
return CORD_asprintf(octal_fmt, (int)digits, (uint64_t)(i.small >> 2));
} else {
CORD str = mpz_get_str(NULL, 8, *i.big);
if (digits > (int64_t)CORD_len(str))
str = CORD_cat(CORD_chars('0', digits - CORD_len(str)), str);
if (prefix) str = CORD_cat("0o", str);
return str;
}
}
public Int_t Int$plus(Int_t x, Int_t y) {
const int64_t z = (int64_t)((uint64_t)x.small + (uint64_t)y.small);
if (__builtin_expect(((z|2) == (int32_t)z), 1))
return (Int_t){.small=(z-1)};
mpz_t result;
mpz_init_set_int(result, x);
if (y.small & 1) {
mpz_t y_mpz;
mpz_init_set_int(y_mpz, y);
mpz_add(result, result, y_mpz);
} else {
mpz_add(result, result, *y.big);
}
return Int$from_mpz(result);
}
public Int_t Int$minus(Int_t x, Int_t y) {
const int64_t z = (int64_t)(((uint64_t)x.small ^ 3) - (uint64_t)y.small);
if (__builtin_expect(((z & ~2) == (int32_t)z), 1))
return (Int_t){.small=z};
mpz_t result;
mpz_init_set_int(result, x);
if (y.small & 1) {
mpz_t y_mpz;
mpz_init_set_int(y_mpz, y);
mpz_sub(result, result, y_mpz);
} else {
mpz_sub(result, result, *y.big);
}
return Int$from_mpz(result);
}
public Int_t Int$times(Int_t x, Int_t y) {
if (__builtin_expect(((x.small & y.small) & 1) != 0, 1)) {
const int64_t z = (x.small>>1) * (y.small>>1);
if (__builtin_expect(z == (int32_t)z, 1))
return (Int_t){.small=z+1};
}
mpz_t result;
mpz_init_set_int(result, x);
if (y.small & 1) {
mpz_t y_mpz;
mpz_init_set_int(y_mpz, y);
mpz_mul(result, result, y_mpz);
} else {
mpz_mul(result, result, *y.big);
}
return Int$from_mpz(result);
}
public Int_t Int$divided_by(Int_t x, Int_t y) {
if (__builtin_expect(((x.small & y.small) & 1) != 0, 1)) {
const int64_t z = 4*(x.small>>1) / (y.small>>1);
if (__builtin_expect(z == (int32_t)z, 1))
return (Int_t){.small=z+1};
}
mpz_t result;
mpz_init_set_int(result, x);
if (y.small & 1) {
mpz_t y_mpz;
mpz_init_set_int(y_mpz, y);
mpz_cdiv_q(result, result, y_mpz);
} else {
mpz_cdiv_q(result, result, *y.big);
}
return Int$from_mpz(result);
}
public Int_t Int$modulo(Int_t x, Int_t modulus)
{
mpz_t result;
mpz_init_set_int(result, x);
mpz_t divisor;
mpz_init_set_int(divisor, modulus);
mpz_mod(result, result, divisor);
return Int$from_mpz(result);
}
public Int_t Int$modulo1(Int_t x, Int_t modulus)
{
mpz_t result;
mpz_init_set_int(result, x);
mpz_sub_ui(result, result, 1);
mpz_t divisor;
mpz_init_set_int(divisor, modulus);
mpz_mod(result, result, divisor);
mpz_add_ui(result, result, 1);
return Int$from_mpz(result);
}
public Int_t Int$left_shifted(Int_t x, Int_t y)
{
if (__builtin_expect(((x.small & y.small) & 1) != 0, 1)) {
const int64_t z = 4*((x.small>>2) << (y.small>>2));
if (__builtin_expect(z == (int32_t)z, 1))
return (Int_t){.small=z+1};
}
mp_bitcnt_t bits = (mp_bitcnt_t)Int$as_i64(y);
mpz_t result;
mpz_init_set_int(result, x);
mpz_mul_2exp(result, result, bits);
return Int$from_mpz(result);
}
public Int_t Int$right_shifted(Int_t x, Int_t y)
{
if (__builtin_expect(((x.small & y.small) & 1) != 0, 1)) {
const int64_t z = 4*((x.small>>2) >> (y.small>>2));
if (__builtin_expect(z == (int32_t)z, 1))
return (Int_t){.small=z+1};
}
mp_bitcnt_t bits = (mp_bitcnt_t)Int$as_i64(y);
mpz_t result;
mpz_init_set_int(result, x);
mpz_tdiv_q_2exp(result, result, bits);
return Int$from_mpz(result);
}
public Int_t Int$bit_and(Int_t x, Int_t y)
{
const int64_t z = x.small & y.small;
if (__builtin_expect((z & 1) == 1, 1))
return (Int_t){.small=z};
mpz_t result;
mpz_init_set_int(result, x);
mpz_t y_mpz;
mpz_init_set_int(y_mpz, y);
mpz_and(result, result, y_mpz);
return Int$from_mpz(result);
}
public Int_t Int$bit_or(Int_t x, Int_t y)
{
if (__builtin_expect(((x.small & y.small) & 1) == 1, 1))
return (Int_t){.small=(x.small | y.small)};
mpz_t result;
mpz_init_set_int(result, x);
mpz_t y_mpz;
mpz_init_set_int(y_mpz, y);
mpz_ior(result, result, y_mpz);
return Int$from_mpz(result);
}
public Int_t Int$bit_xor(Int_t x, Int_t y)
{
if (__builtin_expect(((x.small & y.small) & 1) == 1, 1))
return (Int_t){.small=(x.small ^ y.small) | 1};
mpz_t result;
mpz_init_set_int(result, x);
mpz_t y_mpz;
mpz_init_set_int(y_mpz, y);
mpz_xor(result, result, y_mpz);
return Int$from_mpz(result);
}
public Int_t Int$negated(Int_t x)
{
if (__builtin_expect((x.small & 1), 1))
return (Int_t){.small=(~x.small) ^ 3};
mpz_t result;
mpz_init_set_int(result, x);
mpz_neg(result, result);
mpz_sub_ui(result, result, 1);
return Int$from_mpz(result);
}
public Int_t Int$negative(Int_t x)
{
if (__builtin_expect((x.small & 1), 1))
return (Int_t){.small=4*-((x.small)>>2) + 1};
mpz_t result;
mpz_init_set_int(result, x);
mpz_neg(result, result);
return Int$from_mpz(result);
}
public Int_t Int$abs(Int_t x)
{
if (__builtin_expect((x.small & 1), 1))
return (Int_t){.small=4*labs((x.small)>>2) + 1};
mpz_t result;
mpz_init_set_int(result, x);
mpz_abs(result, result);
return Int$from_mpz(result);
}
public Int_t Int$random(Int_t min, Int_t max) {
int32_t cmp = Int$compare(&min, &max, &$Int);
if (cmp > 0)
fail("Random minimum value (%r) is larger than the maximum value (%r)",
Int$as_text(&min, false, &$Int), Int$as_text(&max, false, &$Int));
if (cmp == 0) return min;
mpz_t range_size;
mpz_init_set_int(range_size, max);
if (min.small & 1) {
mpz_t min_mpz;
mpz_init_set_si(min_mpz, min.small >> 2);
mpz_sub(range_size, range_size, min_mpz);
} else {
mpz_sub(range_size, range_size, *min.big);
}
mpz_t r;
mpz_init(r);
mpz_urandomm(r, Int_rng, range_size);
return Int$plus(min, Int$from_mpz(r));
}
public Range_t Int$to(Int_t from, Int_t to) {
return (Range_t){from, to, Int$compare(&to, &from, &$Int) >= 0 ? (Int_t){.small=(1<<2)|1} : (Int_t){.small=(-1/4)|1}};
}
public Int_t Int$from_text(CORD text) {
const char *str = CORD_to_const_char_star(text);
mpz_t i;
if (strncmp(str, "0x", 2) == 0) {
mpz_init_set_str(i, str + 2, 16);
} else if (strncmp(str, "0o", 2) == 0) {
mpz_init_set_str(i, str + 2, 8);
} else if (strncmp(str, "0b", 2) == 0) {
mpz_init_set_str(i, str + 2, 2);
} else {
mpz_init_set_str(i, str, 10);
}
return Int$from_mpz(i);
}
public const TypeInfo $Int = {
.size=sizeof(Int_t),
.align=__alignof__(Int_t),
.tag=CustomInfo,
.CustomInfo={
.compare=(void*)Int$compare,
.equal=(void*)Int$equal,
.hash=(void*)Int$equal,
.as_text=(void*)Int$as_text,
},
};
#define DEFINE_INT_TYPE(c_type, KindOfInt, fmt, min_val, max_val)\
public CORD KindOfInt ## $as_text(const c_type *i, bool colorize, const TypeInfo *type) { \
(void)type; \
if (!i) return #KindOfInt; \
CORD c; \
if (colorize) CORD_sprintf(&c, "\x1b[35m%"fmt"\x1b[33;2m\x1b[m", *i); \
else CORD_sprintf(&c, "%"fmt, *i); \
return c; \
} \
public int32_t KindOfInt ## $compare(const c_type *x, const c_type *y, const TypeInfo *type) { \
(void)type; \
return (*x > *y) - (*x < *y); \
} \
public bool KindOfInt ## $equal(const c_type *x, const c_type *y, const TypeInfo *type) { \
(void)type; \
return *x == *y; \
} \
public CORD KindOfInt ## $format(c_type i, Int_t digits_int) { \
int64_t digits = Int$as_i64(digits_int); \
return CORD_asprintf("%0*" fmt, (int)digits, i); \
} \
public CORD KindOfInt ## $hex(c_type i, Int_t digits_int, bool uppercase, bool prefix) { \
int64_t digits = Int$as_i64(digits_int); \
const char *hex_fmt = uppercase ? (prefix ? "0x%0.*lX" : "%0.*lX") : (prefix ? "0x%0.*lx" : "%0.*lx"); \
return CORD_asprintf(hex_fmt, (int)digits, (uint64_t)i); \
} \
public CORD KindOfInt ## $octal(c_type i, Int_t digits_int, bool prefix) { \
int64_t digits = Int$as_i64(digits_int); \
const char *octal_fmt = prefix ? "0o%0.*lo" : "%0.*lo"; \
return CORD_asprintf(octal_fmt, (int)digits, (uint64_t)i); \
} \
public array_t KindOfInt ## $bits(c_type x) { \
array_t bit_array = (array_t){.data=GC_MALLOC_ATOMIC(sizeof(bool[8*sizeof(c_type)])), .atomic=1, .stride=sizeof(bool), .length=8*sizeof(c_type)}; \
bool *bits = bit_array.data + sizeof(c_type)*8; \
for (size_t i = 0; i < 8*sizeof(c_type); i++) { \
*(bits--) = x & 1; \
x >>= 1; \
} \
return bit_array; \
} \
public c_type KindOfInt ## $random(c_type min, c_type max) { \
if (min > max) fail("Random minimum value (%ld) is larger than the maximum value (%ld)", min, max); \
if (min == max) return min; \
if (min == min_val && max == max_val) { \
c_type r; \
arc4random_buf(&r, sizeof(r)); \
return r; \
} \
uint64_t range = (uint64_t)max - (uint64_t)min + 1; \
uint64_t min_r = -range % range; \
uint64_t r; \
for (;;) { \
arc4random_buf(&r, sizeof(r)); \
if (r >= min_r) break; \
} \
return (c_type)((uint64_t)min + (r % range)); \
} \
public Range_t KindOfInt ## $to(c_type from, c_type to) { \
return (Range_t){Int$from_i64(from), Int$from_i64(to), to >= from ? (Int_t){.small=(1<<2)&1} : (Int_t){.small=(1<<2)&1}}; \
} \
public c_type KindOfInt ## $from_text(CORD text, CORD *the_rest) { \
const char *str = CORD_to_const_char_star(text); \
long i; \
char *end_ptr = NULL; \
if (strncmp(str, "0x", 2) == 0) { \
i = strtol(str, &end_ptr, 16); \
} else if (strncmp(str, "0o", 2) == 0) { \
i = strtol(str, &end_ptr, 8); \
} else if (strncmp(str, "0b", 2) == 0) { \
i = strtol(str, &end_ptr, 2); \
} else { \
i = strtol(str, &end_ptr, 10); \
} \
if (the_rest) *the_rest = CORD_from_char_star(end_ptr); \
if (i < min_val) i = min_val; \
else if (i > max_val) i = min_val; \
return (c_type)i; \
} \
public const c_type KindOfInt##$min = min_val; \
public const c_type KindOfInt##$max = max_val; \
public const TypeInfo $ ## KindOfInt = { \
.size=sizeof(c_type), \
.align=__alignof__(c_type), \
.tag=CustomInfo, \
.CustomInfo={.compare=(void*)KindOfInt##$compare, .as_text=(void*)KindOfInt##$as_text}, \
};
DEFINE_INT_TYPE(int64_t, Int64, "ld", INT64_MIN, INT64_MAX);
DEFINE_INT_TYPE(int32_t, Int32, "d_i32", INT32_MIN, INT32_MAX);
DEFINE_INT_TYPE(int16_t, Int16, "d_i16", INT16_MIN, INT16_MAX);
DEFINE_INT_TYPE(int8_t, Int8, "d_i8", INT8_MIN, INT8_MAX);
#undef DEFINE_INT_TYPE
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
|