5#ifndef PRISM_INTERNAL_BIGNUM_H
6#define PRISM_INTERNAL_BIGNUM_H
10#include "prism/internal/assert.h"
11#include "prism/internal/bit.h"
21#define PM_BIGNUM_BINARY_BASE ((uint64_t) 1 << 32)
26#define PM_BIGNUM_DECIMAL_BASE 1000000000
46pm_bignum_mul_add(
pm_bignum_t *bignum, uint32_t multiplier, uint32_t addend) {
47 PRISM_ASSERT(multiplier != 0);
48 uint64_t carry = addend;
50 for (uint32_t index = 0; index < bignum->length; index++) {
51 uint64_t product = (uint64_t) bignum->words[index] * multiplier + carry;
52 bignum->words[index] = (uint32_t) product;
53 carry = product >> 32;
56 if (carry != 0) bignum->words[bignum->length++] = (uint32_t) carry;
67pm_bignum_div(
pm_bignum_t *bignum, uint32_t divisor) {
68 PRISM_ASSERT(divisor != 0);
69 uint64_t remainder = 0;
71 for (uint32_t index = bignum->length; index > 0; index--) {
72 uint64_t current = (remainder << 32) | bignum->words[index - 1];
73 bignum->words[index - 1] = (uint32_t) (current / divisor);
74 remainder = current % divisor;
77 if (bignum->length > 0 && bignum->words[bignum->length - 1] == 0) bignum->length--;
78 return (uint32_t) remainder;
87pm_bignum_div_exact(
pm_bignum_t *bignum, uint32_t divisor) {
88 uint32_t remainder = pm_bignum_div(bignum, divisor);
89 if (remainder == 0)
return true;
91 pm_bignum_mul_add(bignum, divisor, remainder);
100pm_bignum_shift_left(
pm_bignum_t *bignum,
size_t count) {
101 if (bignum->length == 0)
return;
103 uint32_t *words = bignum->words;
104 uint32_t length = bignum->length;
105 uint32_t word_shift = (uint32_t) (count / 32);
106 uint32_t bit_shift = (uint32_t) (count % 32);
110 uint32_t carry = bit_shift == 0 ? 0 : words[length - 1] >> (32 - bit_shift);
111 for (uint32_t index = length; index > 0; index--) {
112 uint32_t below = (bit_shift == 0 || index == 1) ? 0 : words[index - 2] >> (32 - bit_shift);
113 words[index - 1 + word_shift] = (words[index - 1] << bit_shift) | below;
116 memset(words, 0,
sizeof(uint32_t) * word_shift);
117 bignum->length = length + word_shift;
118 if (carry != 0) words[bignum->length++] = carry;
126pm_bignum_shift_right(
pm_bignum_t *bignum,
size_t count) {
127 uint32_t *words = bignum->words;
128 uint32_t word_shift = (uint32_t) (count / 32);
129 uint32_t bit_shift = (uint32_t) (count % 32);
131 if (word_shift >= bignum->length) {
138 uint32_t length = bignum->length - word_shift;
139 for (uint32_t index = 0; index < length; index++) {
140 uint32_t above = (bit_shift == 0 || index + 1 == length) ? 0 : words[index + word_shift + 1] << (32 - bit_shift);
141 words[index] = (words[index + word_shift] >> bit_shift) | above;
144 bignum->length = length;
145 if (words[length - 1] == 0) bignum->length--;
153pm_bignum_trailing_zeros(
const pm_bignum_t *bignum) {
154 PRISM_ASSERT(bignum->length > 0 && bignum->words[bignum->length - 1] != 0);
157 while (bignum->words[index] == 0) index++;
158 return (uint64_t) index * 32 + pm_ctzll(bignum->words[index]);
167 if (left->length != right->length)
return left->length < right->length ? -1 : 1;
169 for (uint32_t index = left->length; index > 0; index--) {
170 uint32_t left_word = left->words[index - 1];
171 uint32_t right_word = right->words[index - 1];
172 if (left_word != right_word)
return left_word < right_word ? -1 : 1;
185 PRISM_ASSERT(bignum->length <= 2);
186 return (bignum->length > 0 ? (uint64_t) bignum->words[0] : 0) | (bignum->length > 1 ? (uint64_t) bignum->words[1] << 32 : 0);
207 PRISM_ASSERT((
size_t) (scratch->end - scratch->cursor) >= count);
208 uint32_t *words = scratch->cursor;
209 scratch->cursor += count;
#define PRISM_INLINE
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.