|
4 | 4 |
|
5 | 5 | #include "bigint.h"
|
6 | 6 |
|
| 7 | +static u64 BigInt_as_unsigned(const BigInt* bigint) |
| 8 | +{ |
| 9 | + redassert(!bigint->is_negative); |
| 10 | + if (bigint->digit_count == 0) |
| 11 | + { |
| 12 | + return 0; |
| 13 | + } |
| 14 | + else if (bigint->digit_count == 1) |
| 15 | + { |
| 16 | + return bigint->digit; |
| 17 | + } |
| 18 | + else |
| 19 | + { |
| 20 | + RED_UNREACHABLE; |
| 21 | + return 0; |
| 22 | + } |
| 23 | +} |
| 24 | + |
7 | 25 | static void BigInt_normalize(BigInt *dst)
|
8 | 26 | {
|
9 | 27 | const u64* digits = bigint_ptr(dst);
|
@@ -31,6 +49,78 @@ static void BigInt_normalize(BigInt *dst)
|
31 | 49 | }
|
32 | 50 | }
|
33 | 51 |
|
| 52 | +static u8 digit_to_char(u8 digit, bool uppercase) |
| 53 | +{ |
| 54 | + if (digit <= 9) |
| 55 | + { |
| 56 | + return digit + '0'; |
| 57 | + } |
| 58 | + else if (digit <= 35) |
| 59 | + { |
| 60 | + return (digit - 10) + (uppercase ? 'A' : 'a'); |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + RED_UNREACHABLE; |
| 65 | + return 0; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +static bool bit_at_index(const BigInt* bi, size_t index) |
| 70 | +{ |
| 71 | + usize digit_index = index / 64; |
| 72 | + if (digit_index >= bi->digit_count) |
| 73 | + { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + usize digit_bit_index = index % 64; |
| 78 | + const u64* digits = bigint_ptr(bi); |
| 79 | + u64 digit = digits[digit_bit_index]; |
| 80 | + return ((digit >> digit_bit_index) & 0x1) == 0x1; |
| 81 | +} |
| 82 | + |
| 83 | +size_t BigInt_clz(const BigInt* big_int, size_t bit_count) |
| 84 | +{ |
| 85 | + if (big_int->is_negative || bit_count == 0) |
| 86 | + { |
| 87 | + return 0; |
| 88 | + } |
| 89 | + |
| 90 | + if (big_int->digit_count == 0) |
| 91 | + { |
| 92 | + return bit_count; |
| 93 | + } |
| 94 | + |
| 95 | + usize count = 0; |
| 96 | + |
| 97 | + for (usize i = bit_count - 1;;) |
| 98 | + { |
| 99 | + if (bit_at_index(big_int, i)) |
| 100 | + { |
| 101 | + return count; |
| 102 | + } |
| 103 | + |
| 104 | + count++; |
| 105 | + |
| 106 | + if (i == 0) |
| 107 | + { |
| 108 | + break; |
| 109 | + } |
| 110 | + i--; |
| 111 | + } |
| 112 | + |
| 113 | + return count; |
| 114 | +} |
| 115 | + |
| 116 | +size_t BigInt_bits_needed(const BigInt* op) |
| 117 | +{ |
| 118 | + usize full_bits = op->digit_count * 64; |
| 119 | + usize leading_zero_count = BigInt_clz(op, full_bits); |
| 120 | + usize bits_needed = full_bits - leading_zero_count; |
| 121 | + return bits_needed + op->is_negative; |
| 122 | +} |
| 123 | + |
34 | 124 | void BigInt_init_unsigned(BigInt* dst, u64 x)
|
35 | 125 | {
|
36 | 126 | if (x == 0)
|
@@ -196,10 +286,175 @@ void BigInt_add(BigInt *dst, const BigInt *op1, const BigInt *op2)
|
196 | 286 | BigInt_normalize(dst);
|
197 | 287 | }
|
198 | 288 |
|
| 289 | +static void mul_overflow(u64 op1, u64 op2, u64* lo, u64* hi) |
| 290 | +{ |
| 291 | + u64 u1 = (op1 & 0xffffffff); |
| 292 | + u64 v1 = (op2 & 0xffffffff); |
| 293 | + u64 t = (u1 * v1); |
| 294 | + u64 w3 = (t & 0xffffffff); |
| 295 | + u64 k = (t >> 32); |
| 296 | + |
| 297 | + op1 >>= 32; |
| 298 | + t = (op1 * v1) + k; |
| 299 | + k = (t & 0xffffffff); |
| 300 | + u64 w1 = (t >> 32); |
| 301 | + |
| 302 | + op2 >>= 32; |
| 303 | + t = (u1 * op2) + k; |
| 304 | + k = (t >> 32); |
| 305 | + |
| 306 | + *hi = (op1 * op2) + w1 + k; |
| 307 | + *lo = (t << 32) + w3; |
| 308 | +} |
| 309 | + |
| 310 | +void BigInt_shl(BigInt* dst, const BigInt* op1, const BigInt* op2) |
| 311 | +{ |
| 312 | + redassert(!op2->is_negative); |
| 313 | + |
| 314 | + if (op2->digit_count == 0) |
| 315 | + { |
| 316 | + BigInt_init_bigint(dst, op1); |
| 317 | + return; |
| 318 | + } |
| 319 | + |
| 320 | + if (op2->digit_count != 1) |
| 321 | + { |
| 322 | + RED_UNREACHABLE; |
| 323 | + } |
| 324 | + |
| 325 | + const u64* op1_digits = bigint_ptr(op1); |
| 326 | + u64 shift_amt = BigInt_as_unsigned(op2); |
| 327 | + |
| 328 | + if (op1->digit_count == 1 && shift_amt < 64) |
| 329 | + { |
| 330 | + dst->digit = op1_digits[0] << shift_amt; |
| 331 | + if (dst->digit > op1_digits[0]) |
| 332 | + { |
| 333 | + dst->digit_count = 1; |
| 334 | + dst->is_negative = op1->is_negative; |
| 335 | + return; |
| 336 | + } |
| 337 | + } |
| 338 | + |
| 339 | + u64 digit_shift_count = shift_amt / 64; |
| 340 | + u64 leftover_shift_count = shift_amt % 64; |
| 341 | + |
| 342 | + dst->digits = NEW(u64, op1->digit_count + digit_shift_count + 1); |
| 343 | + dst->digit_count = digit_shift_count; |
| 344 | + u64 carry = 0; |
| 345 | + |
| 346 | + for (usize i = 0; i < op1->digit_count; i++) |
| 347 | + { |
| 348 | + u64 digit = op1_digits[i]; |
| 349 | + dst->digits[dst->digit_count] = carry | (digit << leftover_shift_count); |
| 350 | + dst->digit_count++; |
| 351 | + |
| 352 | + if (leftover_shift_count > 0) |
| 353 | + { |
| 354 | + carry = digit >> (64 - leftover_shift_count); |
| 355 | + } |
| 356 | + else |
| 357 | + { |
| 358 | + carry = 0; |
| 359 | + } |
| 360 | + } |
| 361 | + |
| 362 | + dst->digits[dst->digit_count] = carry; |
| 363 | + dst->digit_count++; |
| 364 | + dst->is_negative = op1->is_negative; |
| 365 | + BigInt_normalize(dst); |
| 366 | +} |
| 367 | + |
| 368 | +static void mul_scalar(BigInt* dst, const BigInt* op, u64 scalar) |
| 369 | +{ |
| 370 | + BigInt_init_unsigned(dst, 0); |
| 371 | + |
| 372 | + BigInt bi64; |
| 373 | + BigInt_init_unsigned(&bi64, 64); |
| 374 | + |
| 375 | + const u64* op_digits = bigint_ptr(op); |
| 376 | + usize i = op->digit_count - 1; |
| 377 | + |
| 378 | + for (;;) |
| 379 | + { |
| 380 | + BigInt shifted; |
| 381 | + BigInt_shl(&shifted, dst, &bi64); |
| 382 | + u64 result_scalar; |
| 383 | + u64 carry_scalar; |
| 384 | + |
| 385 | + mul_overflow(scalar, op_digits[i], &result_scalar, &carry_scalar); |
| 386 | + |
| 387 | + BigInt result; |
| 388 | + BigInt_init_unsigned(&result, result_scalar); |
| 389 | + |
| 390 | + BigInt carry; |
| 391 | + BigInt_init_unsigned(&carry, carry_scalar); |
| 392 | + |
| 393 | + BigInt carry_shifted; |
| 394 | + BigInt_shl(&carry_shifted, &carry, &bi64); |
| 395 | + |
| 396 | + BigInt tmp; |
| 397 | + BigInt_add(&tmp, &shifted, &carry_shifted); |
| 398 | + |
| 399 | + BigInt_add(dst, &tmp, &result); |
| 400 | + |
| 401 | + if (i == 0) |
| 402 | + { |
| 403 | + break; |
| 404 | + } |
| 405 | + |
| 406 | + i--; |
| 407 | + } |
| 408 | +} |
199 | 409 |
|
200 | 410 | void BigInt_mul(BigInt *dst, const BigInt *op1, const BigInt *op2)
|
201 | 411 | {
|
| 412 | + if (op1->digit_count == 0 || op2->digit_count == 0) |
| 413 | + { |
| 414 | + return BigInt_init_unsigned(dst, 0); |
| 415 | + } |
| 416 | + |
| 417 | + const u64* op1_digits = bigint_ptr(op1); |
| 418 | + const u64* op2_digits = bigint_ptr(op2); |
| 419 | + |
| 420 | + u64 carry; |
| 421 | + mul_overflow(op1_digits[0], op2_digits[0], &dst->digit, &carry); |
| 422 | + if (carry == 0 && op1->digit_count == 1 && op2->digit_count == 1) |
| 423 | + { |
| 424 | + dst->is_negative = op1->is_negative != op2->is_negative; |
| 425 | + dst->digit_count = 1; |
| 426 | + BigInt_normalize(dst); |
| 427 | + return; |
| 428 | + } |
| 429 | + |
| 430 | + BigInt_init_unsigned(dst, 0); |
202 | 431 |
|
| 432 | + BigInt bi_64; |
| 433 | + |
| 434 | + BigInt_init_unsigned(&bi_64, 0); |
| 435 | + |
| 436 | + usize i = op2->digit_count - 1; |
| 437 | + |
| 438 | + for (;;) |
| 439 | + { |
| 440 | + BigInt shifted; |
| 441 | + BigInt_shl(&shifted, dst, &bi_64); |
| 442 | + |
| 443 | + BigInt scalar_result; |
| 444 | + mul_scalar(&scalar_result, op1, op2_digits[i]); |
| 445 | + |
| 446 | + BigInt_add(dst, &scalar_result, &shifted); |
| 447 | + |
| 448 | + if (i == 0) |
| 449 | + { |
| 450 | + break; |
| 451 | + } |
| 452 | + |
| 453 | + i--; |
| 454 | + } |
| 455 | + |
| 456 | + dst->is_negative = op1->is_negative != op2->is_negative; |
| 457 | + BigInt_normalize(dst); |
203 | 458 | }
|
204 | 459 |
|
205 | 460 | void BigInt_init_bigint(BigInt *dst, const BigInt *src)
|
|
0 commit comments