| 1 |
|
| #include "bigint.h" |
| 2 |
|
| #include "compat.h" |
| 3 |
|
| |
| 4 |
|
| #include <stdlib.h> |
| 5 |
|
| #include <string.h> |
| 6 |
|
| |
| 7 |
|
| #define DIGIT_BITS ((uint64_t)32) |
| 8 |
|
| #define DIGIT_BITMASK ((uint64_t)0xFFFFFFFFUL) |
| 9 |
|
| #define HIGH_DIGIT_BITMASK (DIGIT_BITMASK << DIGIT_BITS) |
| 10 |
|
| |
| 11 |
2594 |
| _kdl_ubigint* _kdl_ubigint_new(uint32_t initial_value) |
| 12 |
|
| { |
| 13 |
2594 |
| _kdl_ubigint* i = malloc(sizeof(_kdl_ubigint) + sizeof(uint32_t)); |
| 14 |
2594 |
▶ | if (i == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i == NULL) return NULL; |
0 |
2594 |
2594 |
|
| 15 |
2594 |
| i->n_digits = 1; |
| 16 |
2594 |
| i->num[0] = initial_value; |
| 17 |
2594 |
| return i; |
| 18 |
|
| } |
| 19 |
|
| |
| 20 |
13 |
| _kdl_ubigint* _kdl_ubigint_dup(_kdl_ubigint const* value) |
| 21 |
|
| { |
| 22 |
13 |
| size_t size = sizeof(_kdl_ubigint) + value->n_digits * sizeof(uint32_t); |
| 23 |
13 |
| _kdl_ubigint* i = malloc(size); |
| 24 |
13 |
▶ | if (i == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i == NULL) return NULL; |
0 |
13 |
13 |
|
| 25 |
13 |
| memcpy(i, value, size); |
| 26 |
13 |
| return i; |
| 27 |
|
| } |
| 28 |
|
| |
| 29 |
2607 |
| void _kdl_ubigint_free(_kdl_ubigint* i) { free(i); } |
| 30 |
|
| |
| 31 |
|
| // a += b |
| 32 |
2826 |
| _kdl_ubigint* _kdl_ubigint_add_inplace(_kdl_ubigint* a, unsigned int b) |
| 33 |
|
| { |
| 34 |
2826 |
| uint64_t carry = b; |
| 35 |
5787 |
▶ | for (size_t i = 0; i < a->n_digits; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < a->n_digits; ++i) { |
2961 |
2826 |
5787 |
|
| 36 |
2961 |
| uint64_t tmp = a->num[i] + carry; |
| 37 |
2961 |
| carry = (tmp & HIGH_DIGIT_BITMASK) >> DIGIT_BITS; |
| 38 |
2961 |
| a->num[i] = tmp & DIGIT_BITMASK; |
| 39 |
|
| } |
| 40 |
2826 |
▶ | if (carry != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (carry != 0) { |
1 |
2825 |
2826 |
|
| 41 |
|
| // overflow |
| 42 |
1 |
| a = reallocf(a, sizeof(_kdl_ubigint) + (++a->n_digits) * sizeof(uint32_t)); |
| 43 |
1 |
▶ | if (a != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a != NULL) { |
1 |
0 |
1 |
|
| 44 |
1 |
| a->num[a->n_digits - 1] = (uint32_t)(carry & DIGIT_BITMASK); |
| 45 |
|
| } |
| 46 |
|
| } |
| 47 |
2826 |
| return a; |
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
|
| // a *= b |
| 51 |
2826 |
| _kdl_ubigint* _kdl_ubigint_multiply_inplace(_kdl_ubigint* a, unsigned int b) |
| 52 |
|
| { |
| 53 |
2826 |
| uint32_t carry = 0; |
| 54 |
5774 |
▶ | for (size_t i = 0; i < a->n_digits; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < a->n_digits; ++i) { |
2948 |
2826 |
5774 |
|
| 55 |
2948 |
| uint64_t tmp = a->num[i]; |
| 56 |
2948 |
| tmp *= b; |
| 57 |
2948 |
| tmp += carry; |
| 58 |
2948 |
| carry = (tmp & HIGH_DIGIT_BITMASK) >> DIGIT_BITS; |
| 59 |
2948 |
| a->num[i] = tmp & DIGIT_BITMASK; |
| 60 |
|
| } |
| 61 |
2826 |
▶ | if (carry != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (carry != 0) { |
13 |
2813 |
2826 |
|
| 62 |
|
| // overflow |
| 63 |
13 |
| a = reallocf(a, sizeof(_kdl_ubigint) + (++a->n_digits) * sizeof(uint32_t)); |
| 64 |
13 |
▶ | if (a != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a != NULL) { |
13 |
0 |
13 |
|
| 65 |
13 |
| a->num[a->n_digits - 1] = carry; |
| 66 |
|
| } |
| 67 |
|
| } |
| 68 |
2826 |
| return a; |
| 69 |
|
| } |
| 70 |
|
| |
| 71 |
239 |
| uint32_t _kdl_ubigint_divide_inplace(_kdl_ubigint* a, uint32_t b) |
| 72 |
|
| { |
| 73 |
239 |
| uint64_t rem = 0; |
| 74 |
614 |
▶ | for (int i = (int)a->n_digits - 1; i >= 0; --i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = (int)a->n_digits - 1; i >= 0; --i) { |
375 |
239 |
614 |
|
| 75 |
375 |
| uint64_t tmp = a->num[i] + (rem << 32); |
| 76 |
375 |
| rem = tmp % b; |
| 77 |
375 |
| a->num[i] = (uint32_t)(tmp / b); |
| 78 |
|
| } |
| 79 |
|
| // shrink the number if possible |
| 80 |
253 |
▶ | while (a->n_digits > 1 && a->num[a->n_digits - 1] == 0) --a->n_digits; |
Branch: 2 branches
| Branch | True | False | Total |
| while (a->n_digits > 1 && a->num[a->n_digits - 1] == 0) --a->n_digits; |
117 |
136 |
253 |
| while (a->n_digits > 1 && a->num[a->n_digits - 1] == 0) --a->n_digits; |
14 |
103 |
117 |
|
| 81 |
239 |
| return (uint32_t)rem; |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
2594 |
| bool _kdl_ubigint_as_long_long(_kdl_ubigint* i, long long* dest) |
| 85 |
|
| { |
| 86 |
|
| // does it fit? |
| 87 |
2594 |
| size_t digits_per_long_long = sizeof(long long) / sizeof(uint32_t); |
| 88 |
2594 |
▶ | if (i->n_digits > digits_per_long_long) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i->n_digits > digits_per_long_long) return false; |
4 |
2590 |
2594 |
|
| 89 |
|
| // does it fit in a *signed* long long though? |
| 90 |
2590 |
| uint32_t top_bit = (uint32_t)1 << (uint32_t)(DIGIT_BITS - 1); |
| 91 |
2590 |
▶ | if (i->num[i->n_digits - 1] & top_bit) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i->num[i->n_digits - 1] & top_bit) return false; |
9 |
2581 |
2590 |
|
| 92 |
|
| |
| 93 |
|
| // ok, we're sure it fits. |
| 94 |
2581 |
| *dest = 0; |
| 95 |
5162 |
▶ | for (int k = (int)i->n_digits - 1; k >= 0; --k) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int k = (int)i->n_digits - 1; k >= 0; --k) { |
2581 |
2581 |
5162 |
|
| 96 |
2581 |
| *dest <<= DIGIT_BITS; |
| 97 |
2581 |
| *dest += i->num[k]; |
| 98 |
|
| } |
| 99 |
2581 |
| return true; |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
0 |
| kdl_owned_string _kdl_ubigint_as_string(_kdl_ubigint* i) { return _kdl_ubigint_as_string_sgn(+1, i); } |
| 103 |
|
| |
| 104 |
13 |
| kdl_owned_string _kdl_ubigint_as_string_sgn(int sign, _kdl_ubigint* i) |
| 105 |
|
| { |
| 106 |
13 |
| i = _kdl_ubigint_dup(i); |
| 107 |
13 |
▶ | if (i == NULL) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i == NULL) goto error; |
0 |
13 |
13 |
|
| 108 |
|
| |
| 109 |
13 |
| size_t max_digits = i->n_digits * 10; // max 10 decimal digits per 32 bits |
| 110 |
13 |
| char* buf = malloc(max_digits); |
| 111 |
13 |
▶ | if (buf == NULL) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (buf == NULL) goto error; |
0 |
13 |
13 |
|
| 112 |
13 |
| char* p = buf; |
| 113 |
|
| // write the number backwards |
| 114 |
252 |
▶ | while (i->n_digits > 1 || i->num[0] != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (i->n_digits > 1 || i->num[0] != 0) { |
112 |
140 |
252 |
| while (i->n_digits > 1 || i->num[0] != 0) { |
127 |
13 |
140 |
|
| 115 |
239 |
| uint32_t digit = _kdl_ubigint_divide_inplace(i, 10); |
| 116 |
239 |
| *(p++) = (char)('0' + digit); |
| 117 |
|
| } |
| 118 |
13 |
| _kdl_ubigint_free(i); |
| 119 |
|
| // flip the number and add the sign |
| 120 |
13 |
| size_t len = p - buf; |
| 121 |
13 |
▶ | if (sign < 0) ++len; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sign < 0) ++len; |
3 |
10 |
13 |
|
| 122 |
13 |
| char* buf2 = malloc(len + 1); |
| 123 |
13 |
▶ | if (buf2 == NULL) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (buf2 == NULL) goto error; |
0 |
13 |
13 |
|
| 124 |
13 |
| char* p2 = buf2; |
| 125 |
13 |
▶ | if (sign < 0) *(p2++) = '-'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sign < 0) *(p2++) = '-'; |
3 |
10 |
13 |
|
| 126 |
252 |
▶ | while (p > buf) *(p2++) = *(--p); |
Branch: 1 branch
| Branch | True | False | Total |
| while (p > buf) *(p2++) = *(--p); |
239 |
13 |
252 |
|
| 127 |
13 |
| *p2 = '\0'; |
| 128 |
13 |
| free(buf); |
| 129 |
13 |
| return (kdl_owned_string){buf2, len}; |
| 130 |
|
| |
| 131 |
0 |
| error: |
| 132 |
0 |
| return (kdl_owned_string){NULL, 0}; |
| 133 |
|
| } |