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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
|
/*
* Copyright (c) 2016 Andrew Kelley
*
* This file is part of zig, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
#include "bignum.hpp"
#include "buffer.hpp"
#include "os.hpp"
#include <assert.h>
#include <math.h>
#include <inttypes.h>
static void bignum_normalize(BigNum *bn) {
assert(bn->kind == BigNumKindInt);
if (bn->data.x_uint == 0) {
bn->is_negative = false;
}
}
void bignum_init_float(BigNum *dest, double x) {
dest->kind = BigNumKindFloat;
dest->is_negative = false;
dest->data.x_float = x;
}
void bignum_init_unsigned(BigNum *dest, uint64_t x) {
dest->kind = BigNumKindInt;
dest->is_negative = false;
dest->data.x_uint = x;
}
void bignum_init_signed(BigNum *dest, int64_t x) {
dest->kind = BigNumKindInt;
if (x < 0) {
dest->is_negative = true;
dest->data.x_uint = ((uint64_t)(-(x + 1))) + 1;
} else {
dest->is_negative = false;
dest->data.x_uint = x;
}
}
void bignum_init_bignum(BigNum *dest, BigNum *src) {
safe_memcpy(dest, src, 1);
}
static int u64_log2(uint64_t x) {
int result = 0;
for (; x != 0; x >>= 1) {
result += 1;
}
return result;
}
bool bignum_fits_in_bits(BigNum *bn, int bit_count, bool is_signed) {
assert(bn->kind == BigNumKindInt);
if (is_signed) {
uint64_t max_neg;
uint64_t max_pos;
if (bit_count < 64) {
max_neg = (1ULL << (bit_count - 1));
max_pos = max_neg - 1;
} else {
max_pos = ((uint64_t)INT64_MAX);
max_neg = max_pos + 1;
}
uint64_t max_val = bn->is_negative ? max_neg : max_pos;
return bn->data.x_uint <= max_val;
} else {
if (bn->is_negative) {
return bn->data.x_uint == 0;
} else {
int required_bit_count = u64_log2(bn->data.x_uint);
return bit_count >= required_bit_count;
}
}
}
void bignum_truncate(BigNum *bn, int bit_count) {
assert(bn->kind == BigNumKindInt);
// TODO handle case when negative = true
if (bit_count < 64) {
bn->data.x_uint &= (1LL << bit_count) - 1;
}
}
uint64_t bignum_to_twos_complement(BigNum *bn) {
assert(bn->kind == BigNumKindInt);
if (bn->is_negative) {
int64_t x = bn->data.x_uint;
return -x;
} else {
return bn->data.x_uint;
}
}
// returns true if overflow happened
bool bignum_add(BigNum *dest, BigNum *op1, BigNum *op2) {
assert(op1->kind == op2->kind);
dest->kind = op1->kind;
if (dest->kind == BigNumKindFloat) {
dest->data.x_float = op1->data.x_float + op2->data.x_float;
return false;
}
if (op1->is_negative == op2->is_negative) {
dest->is_negative = op1->is_negative;
return __builtin_uaddll_overflow(op1->data.x_uint, op2->data.x_uint, &dest->data.x_uint);
} else if (!op1->is_negative && op2->is_negative) {
if (__builtin_usubll_overflow(op1->data.x_uint, op2->data.x_uint, &dest->data.x_uint)) {
dest->data.x_uint = (UINT64_MAX - dest->data.x_uint) + 1;
dest->is_negative = true;
bignum_normalize(dest);
return false;
} else {
bignum_normalize(dest);
return false;
}
} else {
return bignum_add(dest, op2, op1);
}
}
void bignum_negate(BigNum *dest, BigNum *op) {
dest->kind = op->kind;
if (dest->kind == BigNumKindFloat) {
dest->data.x_float = -op->data.x_float;
} else {
dest->data.x_uint = op->data.x_uint;
dest->is_negative = !op->is_negative;
bignum_normalize(dest);
}
}
void bignum_not(BigNum *dest, BigNum *op, int bit_count, bool is_signed) {
assert(op->kind == BigNumKindInt);
uint64_t bits = ~bignum_to_twos_complement(op);
if (bit_count < 64) {
bits &= (1LL << bit_count) - 1;
}
if (is_signed)
bignum_init_signed(dest, bits);
else
bignum_init_unsigned(dest, bits);
}
void bignum_cast_to_float(BigNum *dest, BigNum *op) {
assert(op->kind == BigNumKindInt);
dest->kind = BigNumKindFloat;
dest->data.x_float = (double)op->data.x_uint;
if (op->is_negative) {
dest->data.x_float = -dest->data.x_float;
}
}
void bignum_cast_to_int(BigNum *dest, BigNum *op) {
assert(op->kind == BigNumKindFloat);
dest->kind = BigNumKindInt;
if (op->data.x_float >= 0) {
dest->data.x_uint = (unsigned long long)op->data.x_float;
dest->is_negative = false;
} else {
dest->data.x_uint = (unsigned long long)-op->data.x_float;
dest->is_negative = true;
}
}
bool bignum_sub(BigNum *dest, BigNum *op1, BigNum *op2) {
BigNum op2_negated;
bignum_negate(&op2_negated, op2);
return bignum_add(dest, op1, &op2_negated);
}
bool bignum_mul(BigNum *dest, BigNum *op1, BigNum *op2) {
assert(op1->kind == op2->kind);
dest->kind = op1->kind;
if (dest->kind == BigNumKindFloat) {
dest->data.x_float = op1->data.x_float * op2->data.x_float;
return false;
}
if (__builtin_umulll_overflow(op1->data.x_uint, op2->data.x_uint, &dest->data.x_uint)) {
return true;
}
dest->is_negative = op1->is_negative != op2->is_negative;
bignum_normalize(dest);
return false;
}
bool bignum_div(BigNum *dest, BigNum *op1, BigNum *op2) {
assert(op1->kind == op2->kind);
dest->kind = op1->kind;
if (dest->kind == BigNumKindFloat) {
dest->data.x_float = op1->data.x_float / op2->data.x_float;
} else {
return bignum_div_trunc(dest, op1, op2);
}
return false;
}
bool bignum_div_trunc(BigNum *dest, BigNum *op1, BigNum *op2) {
assert(op1->kind == op2->kind);
dest->kind = op1->kind;
if (dest->kind == BigNumKindFloat) {
double result = op1->data.x_float / op2->data.x_float;
if (result >= 0) {
dest->data.x_float = floor(result);
} else {
dest->data.x_float = ceil(result);
}
} else {
dest->data.x_uint = op1->data.x_uint / op2->data.x_uint;
dest->is_negative = op1->is_negative != op2->is_negative;
bignum_normalize(dest);
}
return false;
}
bool bignum_div_floor(BigNum *dest, BigNum *op1, BigNum *op2) {
assert(op1->kind == op2->kind);
dest->kind = op1->kind;
if (dest->kind == BigNumKindFloat) {
dest->data.x_float = floor(op1->data.x_float / op2->data.x_float);
} else {
if (op1->is_negative != op2->is_negative) {
uint64_t result = op1->data.x_uint / op2->data.x_uint;
if (result * op2->data.x_uint == op1->data.x_uint) {
dest->data.x_uint = result;
} else {
dest->data.x_uint = result + 1;
}
dest->is_negative = true;
} else {
dest->data.x_uint = op1->data.x_uint / op2->data.x_uint;
dest->is_negative = false;
}
}
return false;
}
bool bignum_rem(BigNum *dest, BigNum *op1, BigNum *op2) {
assert(op1->kind == op2->kind);
dest->kind = op1->kind;
if (dest->kind == BigNumKindFloat) {
dest->data.x_float = fmod(op1->data.x_float, op2->data.x_float);
} else {
dest->data.x_uint = op1->data.x_uint % op2->data.x_uint;
dest->is_negative = op1->is_negative;
bignum_normalize(dest);
}
return false;
}
bool bignum_mod(BigNum *dest, BigNum *op1, BigNum *op2) {
assert(op1->kind == op2->kind);
dest->kind = op1->kind;
if (dest->kind == BigNumKindFloat) {
dest->data.x_float = fmod(fmod(op1->data.x_float, op2->data.x_float) + op2->data.x_float, op2->data.x_float);
} else {
if (op1->is_negative) {
dest->data.x_uint = (op2->data.x_uint - op1->data.x_uint % op2->data.x_uint) % op2->data.x_uint;
} else {
dest->data.x_uint = op1->data.x_uint % op2->data.x_uint;
}
dest->is_negative = false;
bignum_normalize(dest);
}
return false;
}
bool bignum_or(BigNum *dest, BigNum *op1, BigNum *op2) {
assert(op1->kind == BigNumKindInt);
assert(op2->kind == BigNumKindInt);
assert(!op1->is_negative);
assert(!op2->is_negative);
dest->kind = BigNumKindInt;
dest->data.x_uint = op1->data.x_uint | op2->data.x_uint;
return false;
}
bool bignum_and(BigNum *dest, BigNum *op1, BigNum *op2) {
assert(op1->kind == BigNumKindInt);
assert(op2->kind == BigNumKindInt);
assert(!op1->is_negative);
assert(!op2->is_negative);
dest->kind = BigNumKindInt;
dest->data.x_uint = op1->data.x_uint & op2->data.x_uint;
return false;
}
bool bignum_xor(BigNum *dest, BigNum *op1, BigNum *op2) {
assert(op1->kind == BigNumKindInt);
assert(op2->kind == BigNumKindInt);
assert(!op1->is_negative);
assert(!op2->is_negative);
dest->kind = BigNumKindInt;
dest->data.x_uint = op1->data.x_uint ^ op2->data.x_uint;
return false;
}
bool bignum_shl(BigNum *dest, BigNum *op1, BigNum *op2) {
assert(op1->kind == BigNumKindInt);
assert(op2->kind == BigNumKindInt);
assert(!op1->is_negative);
assert(!op2->is_negative);
dest->kind = BigNumKindInt;
dest->data.x_uint = op1->data.x_uint << op2->data.x_uint;
return false;
}
bool bignum_shr(BigNum *dest, BigNum *op1, BigNum *op2) {
assert(op1->kind == BigNumKindInt);
assert(op2->kind == BigNumKindInt);
assert(!op1->is_negative);
assert(!op2->is_negative);
dest->kind = BigNumKindInt;
dest->data.x_uint = op1->data.x_uint >> op2->data.x_uint;
return false;
}
Buf *bignum_to_buf(BigNum *bn) {
if (bn->kind == BigNumKindFloat) {
return buf_sprintf("%f", bn->data.x_float);
} else {
const char *neg = bn->is_negative ? "-" : "";
return buf_sprintf("%s%" ZIG_PRI_llu "", neg, bn->data.x_uint);
}
}
bool bignum_cmp_eq(BigNum *op1, BigNum *op2) {
assert(op1->kind == op2->kind);
if (op1->kind == BigNumKindFloat) {
return op1->data.x_float == op2->data.x_float;
} else {
return op1->data.x_uint == op2->data.x_uint &&
(op1->is_negative == op2->is_negative || op1->data.x_uint == 0);
}
}
bool bignum_cmp_neq(BigNum *op1, BigNum *op2) {
return !bignum_cmp_eq(op1, op2);
}
bool bignum_cmp_lt(BigNum *op1, BigNum *op2) {
return !bignum_cmp_gte(op1, op2);
}
bool bignum_cmp_gt(BigNum *op1, BigNum *op2) {
return !bignum_cmp_lte(op1, op2);
}
bool bignum_cmp_lte(BigNum *op1, BigNum *op2) {
assert(op1->kind == op2->kind);
if (op1->kind == BigNumKindFloat) {
return (op1->data.x_float <= op2->data.x_float);
}
// assume normalized is_negative
if (!op1->is_negative && !op2->is_negative) {
return op1->data.x_uint <= op2->data.x_uint;
} else if (op1->is_negative && op2->is_negative) {
return op1->data.x_uint >= op2->data.x_uint;
} else if (op1->is_negative && !op2->is_negative) {
return true;
} else {
return false;
}
}
bool bignum_cmp_gte(BigNum *op1, BigNum *op2) {
assert(op1->kind == op2->kind);
if (op1->kind == BigNumKindFloat) {
return (op1->data.x_float >= op2->data.x_float);
}
// assume normalized is_negative
if (!op1->is_negative && !op2->is_negative) {
return op1->data.x_uint >= op2->data.x_uint;
} else if (op1->is_negative && op2->is_negative) {
return op1->data.x_uint <= op2->data.x_uint;
} else if (op1->is_negative && !op2->is_negative) {
return false;
} else {
return true;
}
}
bool bignum_increment_by_scalar(BigNum *bignum, uint64_t scalar) {
assert(bignum->kind == BigNumKindInt);
assert(!bignum->is_negative);
return __builtin_uaddll_overflow(bignum->data.x_uint, scalar, &bignum->data.x_uint);
}
bool bignum_multiply_by_scalar(BigNum *bignum, uint64_t scalar) {
assert(bignum->kind == BigNumKindInt);
assert(!bignum->is_negative);
return __builtin_umulll_overflow(bignum->data.x_uint, scalar, &bignum->data.x_uint);
}
uint32_t bignum_ctz(BigNum *bignum, uint32_t bit_count) {
assert(bignum->kind == BigNumKindInt);
uint64_t x = bignum_to_twos_complement(bignum);
uint32_t result = 0;
for (uint32_t i = 0; i < bit_count; i += 1) {
if ((x & 0x1) != 0)
break;
result += 1;
x = x >> 1;
}
return result;
}
uint32_t bignum_clz(BigNum *bignum, uint32_t bit_count) {
assert(bignum->kind == BigNumKindInt);
if (bit_count == 0)
return 0;
uint64_t x = bignum_to_twos_complement(bignum);
uint64_t mask = ((uint64_t)1) << ((uint64_t)bit_count - 1);
uint32_t result = 0;
for (uint32_t i = 0; i < bit_count; i += 1) {
if ((x & mask) != 0)
break;
result += 1;
x = x << 1;
}
return result;
}
void bignum_write_twos_complement(BigNum *bn, uint8_t *buf, int bit_count, bool is_big_endian) {
assert(bn->kind == BigNumKindInt);
uint64_t x = bignum_to_twos_complement(bn);
int byte_count = (bit_count + 7) / 8;
for (int i = 0; i < byte_count; i += 1) {
uint8_t le_byte = (x >> (i * 8)) & 0xff;
if (is_big_endian) {
buf[byte_count - i - 1] = le_byte;
} else {
buf[i] = le_byte;
}
}
}
void bignum_read_twos_complement(BigNum *bn, uint8_t *buf, int bit_count, bool is_big_endian, bool is_signed) {
int byte_count = (bit_count + 7) / 8;
uint64_t twos_comp = 0;
for (int i = 0; i < byte_count; i += 1) {
uint8_t be_byte;
if (is_big_endian) {
be_byte = buf[i];
} else {
be_byte = buf[byte_count - i - 1];
}
twos_comp <<= 8;
twos_comp |= be_byte;
}
uint8_t be_byte = buf[is_big_endian ? 0 : byte_count - 1];
if (is_signed && ((be_byte >> 7) & 0x1) != 0) {
bn->is_negative = true;
uint64_t mask = 0;
for (int i = 0; i < bit_count; i += 1) {
mask <<= 1;
mask |= 1;
}
bn->data.x_uint = ((~twos_comp) & mask) + 1;
} else {
bn->data.x_uint = twos_comp;
}
bn->kind = BigNumKindInt;
}
void bignum_write_ieee597(BigNum *bn, uint8_t *buf, int bit_count, bool is_big_endian) {
assert(bn->kind == BigNumKindFloat);
if (bit_count == 32) {
float f32 = bn->data.x_float;
memcpy(buf, &f32, 4);
} else if (bit_count == 64) {
double f64 = bn->data.x_float;
memcpy(buf, &f64, 8);
} else {
zig_unreachable();
}
}
void bignum_read_ieee597(BigNum *bn, uint8_t *buf, int bit_count, bool is_big_endian) {
bn->kind = BigNumKindFloat;
if (bit_count == 32) {
float f32;
memcpy(&f32, buf, 4);
bn->data.x_float = f32;
} else if (bit_count == 64) {
double f64;
memcpy(&f64, buf, 8);
bn->data.x_float = f64;
} else {
zig_unreachable();
}
}
|