aboutsummaryrefslogtreecommitdiff
path: root/src/num.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/num.c')
-rw-r--r--src/num.c1188
1 files changed, 759 insertions, 429 deletions
diff --git a/src/num.c b/src/num.c
index dc3f63ab076e..4839a4b87353 100644
--- a/src/num.c
+++ b/src/num.c
@@ -48,7 +48,8 @@
// Before you try to understand this code, see the development manual
// (manuals/development.md#numbers).
-static void bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
+static void
+bc_num_m(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale);
/**
* Multiply two numbers and throw a math error if they overflow.
@@ -56,7 +57,9 @@ static void bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
* @param b The second operand.
* @return The product of the two operands.
*/
-static inline size_t bc_num_mulOverflow(size_t a, size_t b) {
+static inline size_t
+bc_num_mulOverflow(size_t a, size_t b)
+{
size_t res = a * b;
if (BC_ERR(BC_VM_MUL_OVERFLOW(a, b, res))) bc_err(BC_ERR_MATH_OVERFLOW);
return res;
@@ -68,7 +71,9 @@ static inline size_t bc_num_mulOverflow(size_t a, size_t b) {
* @param n The value to turn into a signed value and negate.
* @param neg The condition to negate or not.
*/
-static inline ssize_t bc_num_neg(size_t n, bool neg) {
+static inline ssize_t
+bc_num_neg(size_t n, bool neg)
+{
return (((ssize_t) n) ^ -((ssize_t) neg)) + neg;
}
@@ -77,7 +82,9 @@ static inline ssize_t bc_num_neg(size_t n, bool neg) {
* @param n The number to compare.
* @return -1 if the number is less than 0, 1 if greater, and 0 if equal.
*/
-ssize_t bc_num_cmpZero(const BcNum *n) {
+ssize_t
+bc_num_cmpZero(const BcNum* n)
+{
return bc_num_neg((n)->len != 0, BC_NUM_NEG(n));
}
@@ -86,7 +93,9 @@ ssize_t bc_num_cmpZero(const BcNum *n) {
* @param n The number to return the amount of integer limbs for.
* @return The amount of integer limbs in @a n.
*/
-static inline size_t bc_num_int(const BcNum *n) {
+static inline size_t
+bc_num_int(const BcNum* n)
+{
return n->len ? n->len - BC_NUM_RDX_VAL(n) : 0;
}
@@ -95,14 +104,15 @@ static inline size_t bc_num_int(const BcNum *n) {
* @param n The number to expand.
* @param req The number limbs to expand the allocation capacity to.
*/
-static void bc_num_expand(BcNum *restrict n, size_t req) {
-
+static void
+bc_num_expand(BcNum* restrict n, size_t req)
+{
assert(n != NULL);
req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
- if (req > n->cap) {
-
+ if (req > n->cap)
+ {
BC_SIG_LOCK;
n->num = bc_vm_realloc(n->num, BC_NUM_SIZE(req));
@@ -117,17 +127,23 @@ static void bc_num_expand(BcNum *restrict n, size_t req) {
* @param n The number to set to zero.
* @param scale The scale to set the number to.
*/
-static void bc_num_setToZero(BcNum *restrict n, size_t scale) {
+static void
+bc_num_setToZero(BcNum* restrict n, size_t scale)
+{
assert(n != NULL);
n->scale = scale;
n->len = n->rdx = 0;
}
-void bc_num_zero(BcNum *restrict n) {
+void
+bc_num_zero(BcNum* restrict n)
+{
bc_num_setToZero(n, 0);
}
-void bc_num_one(BcNum *restrict n) {
+void
+bc_num_one(BcNum* restrict n)
+{
bc_num_zero(n);
n->len = 1;
n->num[0] = 1;
@@ -138,15 +154,19 @@ void bc_num_one(BcNum *restrict n) {
* limbs are zero.
* @param n The number to clean.
*/
-static void bc_num_clean(BcNum *restrict n) {
-
+static void
+bc_num_clean(BcNum* restrict n)
+{
// Reduce the length.
- while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) n->len -= 1;
+ while (BC_NUM_NONZERO(n) && !n->num[n->len - 1])
+ {
+ n->len -= 1;
+ }
// Special cases.
if (BC_NUM_ZERO(n)) n->rdx = 0;
- else {
-
+ else
+ {
// len must be at least as much as rdx.
size_t rdx = BC_NUM_RDX_VAL(n);
if (n->len < rdx) n->len = rdx;
@@ -161,10 +181,18 @@ static void bc_num_clean(BcNum *restrict n) {
* @param i The number to return the log base 10 of.
* @return The log base 10 of @a i.
*/
-static size_t bc_num_log10(size_t i) {
+static size_t
+bc_num_log10(size_t i)
+{
size_t len;
- for (len = 1; i; i /= BC_BASE, ++len);
+
+ for (len = 1; i; i /= BC_BASE, ++len)
+ {
+ continue;
+ }
+
assert(len - 1 <= BC_BASE_DIGS + 1);
+
return len - 1;
}
@@ -175,7 +203,9 @@ static size_t bc_num_log10(size_t i) {
* @return The number of decimal digits that are 0 starting at the most
* significant digits.
*/
-static inline size_t bc_num_zeroDigits(const BcDig *n) {
+static inline size_t
+bc_num_zeroDigits(const BcDig* n)
+{
assert(*n >= 0);
assert(((size_t) *n) < BC_BASE_POW);
return BC_BASE_DIGS - bc_num_log10((size_t) *n);
@@ -187,7 +217,9 @@ static inline size_t bc_num_zeroDigits(const BcDig *n) {
* @param n The number.
* @return The number of integer digits in @a n.
*/
-static size_t bc_num_intDigits(const BcNum *n) {
+static size_t
+bc_num_intDigits(const BcNum* n)
+{
size_t digits = bc_num_int(n) * BC_BASE_DIGS;
if (digits > 0) digits -= bc_num_zeroDigits(n->num + n->len - 1);
return digits;
@@ -202,11 +234,20 @@ static size_t bc_num_intDigits(const BcNum *n) {
* @param n The number.
* @return The number of non-zero limbs after the decimal point.
*/
-static size_t bc_num_nonZeroLen(const BcNum *restrict n) {
+static size_t
+bc_num_nonZeroLen(const BcNum* restrict n)
+{
size_t i, len = n->len;
+
assert(len == BC_NUM_RDX_VAL(n));
- for (i = len - 1; i < len && !n->num[i]; --i);
+
+ for (i = len - 1; i < len && !n->num[i]; --i)
+ {
+ continue;
+ }
+
assert(i + 1 > 0);
+
return i + 1;
}
@@ -218,8 +259,9 @@ static size_t bc_num_nonZeroLen(const BcNum *restrict n) {
* carry out from this add.
* @return The resulting limb sum.
*/
-static BcDig bc_num_addDigits(BcDig a, BcDig b, bool *carry) {
-
+static BcDig
+bc_num_addDigits(BcDig a, BcDig b, bool* carry)
+{
assert(((BcBigDig) BC_BASE_POW) * 2 == ((BcDig) BC_BASE_POW) * 2);
assert(a < BC_BASE_POW);
assert(b < BC_BASE_POW);
@@ -242,8 +284,9 @@ static BcDig bc_num_addDigits(BcDig a, BcDig b, bool *carry) {
* and the carry out from this subtract.
* @return The resulting limb difference.
*/
-static BcDig bc_num_subDigits(BcDig a, BcDig b, bool *carry) {
-
+static BcDig
+bc_num_subDigits(BcDig a, BcDig b, bool* carry)
+{
assert(a < BC_BASE_POW);
assert(b < BC_BASE_POW);
@@ -263,16 +306,22 @@ static BcDig bc_num_subDigits(BcDig a, BcDig b, bool *carry) {
* @param b The second operand.
* @param len The length of @a b.
*/
-static void bc_num_addArrays(BcDig *restrict a, const BcDig *restrict b,
- size_t len)
+static void
+bc_num_addArrays(BcDig* restrict a, const BcDig* restrict b, size_t len)
{
size_t i;
bool carry = false;
- for (i = 0; i < len; ++i) a[i] = bc_num_addDigits(a[i], b[i], &carry);
+ for (i = 0; i < len; ++i)
+ {
+ a[i] = bc_num_addDigits(a[i], b[i], &carry);
+ }
// Take care of the extra limbs in the bigger array.
- for (; carry; ++i) a[i] = bc_num_addDigits(a[i], 0, &carry);
+ for (; carry; ++i)
+ {
+ a[i] = bc_num_addDigits(a[i], 0, &carry);
+ }
}
/**
@@ -281,16 +330,22 @@ static void bc_num_addArrays(BcDig *restrict a, const BcDig *restrict b,
* @param b The second operand.
* @param len The length of @a b.
*/
-static void bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b,
- size_t len)
+static void
+bc_num_subArrays(BcDig* restrict a, const BcDig* restrict b, size_t len)
{
size_t i;
bool carry = false;
- for (i = 0; i < len; ++i) a[i] = bc_num_subDigits(a[i], b[i], &carry);
+ for (i = 0; i < len; ++i)
+ {
+ a[i] = bc_num_subDigits(a[i], b[i], &carry);
+ }
// Take care of the extra limbs in the bigger array.
- for (; carry; ++i) a[i] = bc_num_subDigits(a[i], 0, &carry);
+ for (; carry; ++i)
+ {
+ a[i] = bc_num_subDigits(a[i], 0, &carry);
+ }
}
/**
@@ -300,8 +355,8 @@ static void bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b,
* @param b The one limb of the one-limb number.
* @param c The return parameter.
*/
-static void bc_num_mulArray(const BcNum *restrict a, BcBigDig b,
- BcNum *restrict c)
+static void
+bc_num_mulArray(const BcNum* restrict a, BcBigDig b, BcNum* restrict c)
{
size_t i;
BcBigDig carry = 0;
@@ -312,10 +367,12 @@ static void bc_num_mulArray(const BcNum *restrict a, BcBigDig b,
if (a->len + 1 > c->cap) bc_num_expand(c, a->len + 1);
// We want the entire return parameter to be zero for cleaning later.
+ // NOLINTNEXTLINE
memset(c->num, 0, BC_NUM_SIZE(c->cap));
// Actual multiplication loop.
- for (i = 0; i < a->len; ++i) {
+ for (i = 0; i < a->len; ++i)
+ {
BcBigDig in = ((BcBigDig) a->num[i]) * b + carry;
c->num[i] = in % BC_BASE_POW;
carry = in / BC_BASE_POW;
@@ -344,8 +401,9 @@ static void bc_num_mulArray(const BcNum *restrict a, BcBigDig b,
* @param c The return parameter for the quotient.
* @param rem The return parameter for the remainder.
*/
-static void bc_num_divArray(const BcNum *restrict a, BcBigDig b,
- BcNum *restrict c, BcBigDig *rem)
+static void
+bc_num_divArray(const BcNum* restrict a, BcBigDig b, BcNum* restrict c,
+ BcBigDig* rem)
{
size_t i;
BcBigDig carry = 0;
@@ -353,7 +411,8 @@ static void bc_num_divArray(const BcNum *restrict a, BcBigDig b,
assert(c->cap >= a->len);
// Actual division loop.
- for (i = a->len - 1; i < a->len; --i) {
+ for (i = a->len - 1; i < a->len; --i)
+ {
BcBigDig in = ((BcBigDig) a->num[i]) + carry * BC_BASE_POW;
assert(in / b < BC_BASE_POW);
c->num[i] = (BcDig) (in / b);
@@ -378,19 +437,24 @@ static void bc_num_divArray(const BcNum *restrict a, BcBigDig b,
* @param b The second array.
* @param len The minimum length of the arrays.
*/
-static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b,
- size_t len)
+static ssize_t
+bc_num_compare(const BcDig* restrict a, const BcDig* restrict b, size_t len)
{
size_t i;
BcDig c = 0;
- for (i = len - 1; i < len && !(c = a[i] - b[i]); --i);
+ for (i = len - 1; i < len && !(c = a[i] - b[i]); --i)
+ {
+ continue;
+ }
return bc_num_neg(i + 1, c < 0);
}
-ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
-
+ssize_t
+bc_num_cmp(const BcNum* a, const BcNum* b)
+{
size_t i, min, a_int, b_int, diff, ardx, brdx;
- BcDig *max_num, *min_num;
+ BcDig* max_num;
+ BcDig* min_num;
bool a_max, neg = false;
ssize_t cmp;
@@ -402,7 +466,8 @@ ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
// Easy cases.
if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !BC_NUM_NEG(b));
if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a);
- if (BC_NUM_NEG(a)) {
+ if (BC_NUM_NEG(a))
+ {
if (BC_NUM_NEG(b)) neg = true;
else return -1;
}
@@ -422,13 +487,15 @@ ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
a_max = (ardx > brdx);
// Set variables based on the above.
- if (a_max) {
+ if (a_max)
+ {
min = brdx;
diff = ardx - brdx;
max_num = a->num + diff;
min_num = b->num;
}
- else {
+ else
+ {
min = ardx;
diff = brdx - ardx;
max_num = b->num + diff;
@@ -443,15 +510,17 @@ ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
// If there was no difference, then the final step is to check which number
// has greater or lesser limbs beyond the other's.
- for (max_num -= diff, i = diff - 1; i < diff; --i) {
+ for (max_num -= diff, i = diff - 1; i < diff; --i)
+ {
if (max_num[i]) return bc_num_neg(1, !a_max == !neg);
}
return 0;
}
-void bc_num_truncate(BcNum *restrict n, size_t places) {
-
+void
+bc_num_truncate(BcNum* restrict n, size_t places)
+{
size_t nrdx, places_rdx;
if (!places) return;
@@ -468,8 +537,8 @@ void bc_num_truncate(BcNum *restrict n, size_t places) {
BC_NUM_RDX_SET(n, nrdx - places_rdx);
// Only when the number is nonzero do we need to do the hard stuff.
- if (BC_NUM_NONZERO(n)) {
-
+ if (BC_NUM_NONZERO(n))
+ {
size_t pow;
// This calculates how many decimal digits are in the least significant
@@ -482,6 +551,7 @@ void bc_num_truncate(BcNum *restrict n, size_t places) {
// We have to move limbs to maintain invariants. The limbs must begin at
// the beginning of the BcNum array.
+ // NOLINTNEXTLINE
memmove(n->num, n->num + places_rdx, BC_NUM_SIZE(n->len));
// Clear the lower part of the last digit.
@@ -491,14 +561,16 @@ void bc_num_truncate(BcNum *restrict n, size_t places) {
}
}
-void bc_num_extend(BcNum *restrict n, size_t places) {
-
+void
+bc_num_extend(BcNum* restrict n, size_t places)
+{
size_t nrdx, places_rdx;
if (!places) return;
// Easy case with zero; set the scale.
- if (BC_NUM_ZERO(n)) {
+ if (BC_NUM_ZERO(n))
+ {
n->scale += places;
return;
}
@@ -510,9 +582,12 @@ void bc_num_extend(BcNum *restrict n, size_t places) {
// This is the hard case. We need to expand the number, move the limbs, and
// set the limbs that were just cleared.
- if (places_rdx) {
+ if (places_rdx)
+ {
bc_num_expand(n, bc_vm_growSize(n->len, places_rdx));
+ // NOLINTNEXTLINE
memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len));
+ // NOLINTNEXTLINE
memset(n->num, 0, BC_NUM_SIZE(places_rdx));
}
@@ -527,8 +602,8 @@ void bc_num_extend(BcNum *restrict n, size_t places) {
/**
* Retires (finishes) a multiplication or division operation.
*/
-static void bc_num_retireMul(BcNum *restrict n, size_t scale,
- bool neg1, bool neg2)
+static void
+bc_num_retireMul(BcNum* restrict n, size_t scale, bool neg1, bool neg2)
{
// Make sure scale is correct.
if (n->scale < scale) bc_num_extend(n, scale - n->scale);
@@ -547,16 +622,17 @@ static void bc_num_retireMul(BcNum *restrict n, size_t scale,
* @param a An out parameter; the low part of @a n.
* @param b An out parameter; the high part of @a n.
*/
-static void bc_num_split(const BcNum *restrict n, size_t idx,
- BcNum *restrict a, BcNum *restrict b)
+static void
+bc_num_split(const BcNum* restrict n, size_t idx, BcNum* restrict a,
+ BcNum* restrict b)
{
// We want a and b to be clear.
assert(BC_NUM_ZERO(a));
assert(BC_NUM_ZERO(b));
// The usual case.
- if (idx < n->len) {
-
+ if (idx < n->len)
+ {
// Set the fields first.
b->len = n->len - idx;
a->len = idx;
@@ -569,7 +645,9 @@ static void bc_num_split(const BcNum *restrict n, size_t idx,
// Copy the arrays. This is not necessary for safety, but it is faster,
// for some reason.
+ // NOLINTNEXTLINE
memcpy(b->num, n->num + idx, BC_NUM_SIZE(b->len));
+ // NOLINTNEXTLINE
memcpy(a->num, n->num, BC_NUM_SIZE(idx));
bc_num_clean(b);
@@ -586,8 +664,9 @@ static void bc_num_split(const BcNum *restrict n, size_t idx,
* @param n The number to copy.
* @param r The result number with a shifted rdx, len, and num.
*/
-static void bc_num_shiftRdx(const BcNum *restrict n, BcNum *restrict r) {
-
+static void
+bc_num_shiftRdx(const BcNum* restrict n, BcNum* restrict r)
+{
size_t rdx = BC_NUM_RDX_VAL(n);
r->len = n->len - rdx;
@@ -603,15 +682,19 @@ static void bc_num_shiftRdx(const BcNum *restrict n, BcNum *restrict r) {
* skipped. This must be undone by bc_num_unshiftZero().
* @param n The number to shift.
*/
-static size_t bc_num_shiftZero(BcNum *restrict n) {
-
+static size_t
+bc_num_shiftZero(BcNum* restrict n)
+{
size_t i;
// If we don't have an integer, that is a problem, but it's also a bug
// because the caller should have set everything up right.
assert(!BC_NUM_RDX_VAL(n) || BC_NUM_ZERO(n));
- for (i = 0; i < n->len && !n->num[i]; ++i);
+ for (i = 0; i < n->len && !n->num[i]; ++i)
+ {
+ continue;
+ }
n->len -= i;
n->num += i;
@@ -625,7 +708,9 @@ static size_t bc_num_shiftZero(BcNum *restrict n) {
* @param n The number to unshift.
* @param places_rdx The amount the number was originally shift.
*/
-static void bc_num_unshiftZero(BcNum *restrict n, size_t places_rdx) {
+static void
+bc_num_unshiftZero(BcNum* restrict n, size_t places_rdx)
+{
n->len += places_rdx;
n->num -= places_rdx;
}
@@ -638,11 +723,12 @@ static void bc_num_unshiftZero(BcNum *restrict n, size_t places_rdx) {
* @param n The number to shift digits in.
* @param dig The number of places to shift right.
*/
-static void bc_num_shift(BcNum *restrict n, BcBigDig dig) {
-
+static void
+bc_num_shift(BcNum* restrict n, BcBigDig dig)
+{
size_t i, len = n->len;
BcBigDig carry = 0, pow;
- BcDig *ptr = n->num;
+ BcDig* ptr = n->num;
assert(dig < BC_BASE_DIGS);
@@ -652,7 +738,8 @@ static void bc_num_shift(BcNum *restrict n, BcBigDig dig) {
// Run a series of divisions and mods with carries across the entire number
// array. This effectively shifts everything over.
- for (i = len - 1; i < len; --i) {
+ for (i = len - 1; i < len; --i)
+ {
BcBigDig in, temp;
in = ((BcBigDig) ptr[i]);
temp = carry * dig;
@@ -669,8 +756,9 @@ static void bc_num_shift(BcNum *restrict n, BcBigDig dig) {
* @param n The number to shift left.
* @param places The amount of places to shift @a n left by.
*/
-static void bc_num_shiftLeft(BcNum *restrict n, size_t places) {
-
+static void
+bc_num_shiftLeft(BcNum* restrict n, size_t places)
+{
BcBigDig dig;
size_t places_rdx;
bool shift;
@@ -678,13 +766,15 @@ static void bc_num_shiftLeft(BcNum *restrict n, size_t places) {
if (!places) return;
// Make sure to grow the number if necessary.
- if (places > n->scale) {
+ if (places > n->scale)
+ {
size_t size = bc_vm_growSize(BC_NUM_RDX(places - n->scale), n->len);
if (size > SIZE_MAX - 1) bc_err(BC_ERR_MATH_OVERFLOW);
}
// If zero, we can just set the scale and bail.
- if (BC_NUM_ZERO(n)) {
+ if (BC_NUM_ZERO(n))
+ {
if (n->scale >= places) n->scale -= places;
else n->scale = 0;
return;
@@ -705,13 +795,13 @@ static void bc_num_shiftLeft(BcNum *restrict n, size_t places) {
// integer doesn't is because left shift would only extend the integer,
// whereas a non-integer might have its fractional part eliminated or only
// partially eliminated.
- if (n->scale) {
-
+ if (n->scale)
+ {
size_t nrdx = BC_NUM_RDX_VAL(n);
// If the number's rdx is bigger, that's the hard case.
- if (nrdx >= places_rdx) {
-
+ if (nrdx >= places_rdx)
+ {
size_t mod = n->scale % BC_BASE_DIGS, revdig;
// We want mod to be in the range [1, BC_BASE_DIGS], not
@@ -729,19 +819,24 @@ static void bc_num_shiftLeft(BcNum *restrict n, size_t places) {
}
// If this is non-zero, we need an extra place, so expand, move, and set.
- if (places_rdx) {
+ if (places_rdx)
+ {
bc_num_expand(n, bc_vm_growSize(n->len, places_rdx));
+ // NOLINTNEXTLINE
memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len));
+ // NOLINTNEXTLINE
memset(n->num, 0, BC_NUM_SIZE(places_rdx));
n->len += places_rdx;
}
// Set the scale appropriately.
- if (places > n->scale) {
+ if (places > n->scale)
+ {
n->scale = 0;
BC_NUM_RDX_SET(n, 0);
}
- else {
+ else
+ {
n->scale -= places;
BC_NUM_RDX_SET(n, BC_NUM_RDX(n->scale));
}
@@ -752,8 +847,9 @@ static void bc_num_shiftLeft(BcNum *restrict n, size_t places) {
bc_num_clean(n);
}
-void bc_num_shiftRight(BcNum *restrict n, size_t places) {
-
+void
+bc_num_shiftRight(BcNum* restrict n, size_t places)
+{
BcBigDig dig;
size_t places_rdx, scale, scale_mod, int_len, expand;
bool shift;
@@ -761,7 +857,8 @@ void bc_num_shiftRight(BcNum *restrict n, size_t places) {
if (!places) return;
// If zero, we can just set the scale and bail.
- if (BC_NUM_ZERO(n)) {
+ if (BC_NUM_ZERO(n))
+ {
n->scale += places;
bc_num_expand(n, BC_NUM_RDX(n->scale));
return;
@@ -782,11 +879,13 @@ void bc_num_shiftRight(BcNum *restrict n, size_t places) {
places_rdx = BC_NUM_RDX(places);
// If we are going to shift past a limb boundary or not, set accordingly.
- if (scale_mod + dig > BC_BASE_DIGS) {
+ if (scale_mod + dig > BC_BASE_DIGS)
+ {
expand = places_rdx - 1;
places_rdx = 1;
}
- else {
+ else
+ {
expand = places_rdx;
places_rdx = 0;
}
@@ -798,6 +897,7 @@ void bc_num_shiftRight(BcNum *restrict n, size_t places) {
// Extend, expand, and zero.
bc_num_extend(n, places_rdx * BC_BASE_DIGS);
bc_num_expand(n, bc_vm_growSize(expand, n->len));
+ // NOLINTNEXTLINE
memset(n->num + n->len, 0, BC_NUM_SIZE(expand));
// Set the fields.
@@ -823,7 +923,9 @@ void bc_num_shiftRight(BcNum *restrict n, size_t places) {
* @param b The return parameter. This must be preallocated.
* @param scale The current scale.
*/
-static inline void bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
+static inline void
+bc_num_inv(BcNum* a, BcNum* b, size_t scale)
+{
assert(BC_NUM_NONZERO(a));
bc_num_div(&vm.one, a, b, scale);
}
@@ -837,19 +939,25 @@ static inline void bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
* *not* be allocated.
* @return True if the number is a non-integer, false otherwise.
*/
-static bool bc_num_nonInt(const BcNum *restrict n, BcNum *restrict r) {
-
+static bool
+bc_num_nonInt(const BcNum* restrict n, BcNum* restrict r)
+{
bool zero;
size_t i, rdx = BC_NUM_RDX_VAL(n);
- if (!rdx) {
+ if (!rdx)
+ {
+ // NOLINTNEXTLINE
memcpy(r, n, sizeof(BcNum));
return false;
}
zero = true;
- for (i = 0; zero && i < rdx; ++i) zero = (n->num[i] == 0);
+ for (i = 0; zero && i < rdx; ++i)
+ {
+ zero = (n->num[i] == 0);
+ }
if (BC_ERR(!zero)) return true;
@@ -868,7 +976,8 @@ static bool bc_num_nonInt(const BcNum *restrict n, BcNum *restrict r) {
* @param c The result operand.
* @return The second operand as a hardware integer.
*/
-static BcBigDig bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c)
+static BcBigDig
+bc_num_intop(const BcNum* a, const BcNum* b, BcNum* restrict c)
{
BcNum temp;
@@ -890,19 +999,24 @@ static BcBigDig bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c)
* @param c The return parameter.
* @param sub Non-zero for a subtract, zero for an add.
*/
-static void bc_num_as(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
-
- BcDig *ptr_c, *ptr_l, *ptr_r;
+static void
+bc_num_as(BcNum* a, BcNum* b, BcNum* restrict c, size_t sub)
+{
+ BcDig* ptr_c;
+ BcDig* ptr_l;
+ BcDig* ptr_r;
size_t i, min_rdx, max_rdx, diff, a_int, b_int, min_len, max_len, max_int;
size_t len_l, len_r, ardx, brdx;
bool b_neg, do_sub, do_rev_sub, carry, c_neg;
- if (BC_NUM_ZERO(b)) {
+ if (BC_NUM_ZERO(b))
+ {
bc_num_copy(c, a);
return;
}
- if (BC_NUM_ZERO(a)) {
+ if (BC_NUM_ZERO(a))
+ {
bc_num_copy(c, b);
c->rdx = BC_NUM_NEG_VAL(c, BC_NUM_NEG(b) != sub);
return;
@@ -930,17 +1044,18 @@ static void bc_num_as(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
max_len = max_int + max_rdx;
- if (do_sub) {
-
+ if (do_sub)
+ {
// Check whether b has to be subtracted from a or a from b.
if (a_int != b_int) do_rev_sub = (a_int < b_int);
else if (ardx > brdx)
+ {
do_rev_sub = (bc_num_compare(a->num + diff, b->num, b->len) < 0);
- else
- do_rev_sub = (bc_num_compare(a->num, b->num + diff, a->len) <= 0);
+ }
+ else do_rev_sub = (bc_num_compare(a->num, b->num + diff, a->len) <= 0);
}
- else {
-
+ else
+ {
// The result array of the addition might come out one element
// longer than the bigger of the operand arrays.
max_len += 1;
@@ -950,13 +1065,15 @@ static void bc_num_as(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
assert(max_len <= c->cap);
// Cache values for simple code later.
- if (do_rev_sub) {
+ if (do_rev_sub)
+ {
ptr_l = b->num;
ptr_r = a->num;
len_l = b->len;
len_r = a->len;
}
- else {
+ else
+ {
ptr_l = a->num;
ptr_r = b->num;
len_l = a->len;
@@ -968,34 +1085,38 @@ static void bc_num_as(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
// This is true if the numbers have a different number of limbs after the
// decimal point.
- if (diff) {
-
+ if (diff)
+ {
// If the rdx values of the operands do not match, the result will
// have low end elements that are the positive or negative trailing
// elements of the operand with higher rdx value.
- if ((ardx > brdx) != do_rev_sub) {
-
+ if ((ardx > brdx) != do_rev_sub)
+ {
// !do_rev_sub && ardx > brdx || do_rev_sub && brdx > ardx
// The left operand has BcDig values that need to be copied,
// either from a or from b (in case of a reversed subtraction).
+ // NOLINTNEXTLINE
memcpy(ptr_c, ptr_l, BC_NUM_SIZE(diff));
ptr_l += diff;
len_l -= diff;
}
- else {
-
+ else
+ {
// The right operand has BcDig values that need to be copied
// or subtracted from zero (in case of a subtraction).
- if (do_sub) {
-
+ if (do_sub)
+ {
// do_sub (do_rev_sub && ardx > brdx ||
// !do_rev_sub && brdx > ardx)
for (i = 0; i < diff; i++)
+ {
ptr_c[i] = bc_num_subDigits(0, ptr_r[i], &carry);
+ }
}
- else {
-
+ else
+ {
// !do_sub && brdx > ardx
+ // NOLINTNEXTLINE
memcpy(ptr_c, ptr_r, BC_NUM_SIZE(diff));
}
@@ -1018,23 +1139,33 @@ static void bc_num_as(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
// Inlining takes care of eliminating constant zero arguments to
// addDigit/subDigit (checked in disassembly of resulting bc binary
// compiled with gcc and clang).
- if (do_sub) {
-
+ if (do_sub)
+ {
// Actual subtraction.
for (i = 0; i < min_len; ++i)
+ {
ptr_c[i] = bc_num_subDigits(ptr_l[i], ptr_r[i], &carry);
+ }
// Finishing the limbs beyond the direct subtraction.
- for (; i < len_l; ++i) ptr_c[i] = bc_num_subDigits(ptr_l[i], 0, &carry);
+ for (; i < len_l; ++i)
+ {
+ ptr_c[i] = bc_num_subDigits(ptr_l[i], 0, &carry);
+ }
}
- else {
-
+ else
+ {
// Actual addition.
for (i = 0; i < min_len; ++i)
+ {
ptr_c[i] = bc_num_addDigits(ptr_l[i], ptr_r[i], &carry);
+ }
// Finishing the limbs beyond the direct addition.
- for (; i < len_l; ++i) ptr_c[i] = bc_num_addDigits(ptr_l[i], 0, &carry);
+ for (; i < len_l; ++i)
+ {
+ ptr_c[i] = bc_num_addDigits(ptr_l[i], 0, &carry);
+ }
// Addition can create an extra limb. We take care of that here.
ptr_c[i] = bc_num_addDigits(0, 0, &carry);
@@ -1060,10 +1191,13 @@ static void bc_num_as(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
* @param b The second operand.
* @param c The return parameter.
*/
-static void bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c) {
-
+static void
+bc_num_m_simp(const BcNum* a, const BcNum* b, BcNum* restrict c)
+{
size_t i, alen = a->len, blen = b->len, clen;
- BcDig *ptr_a = a->num, *ptr_b = b->num, *ptr_c;
+ BcDig* ptr_a = a->num;
+ BcDig* ptr_b = b->num;
+ BcDig* ptr_c;
BcBigDig sum = 0, carry = 0;
assert(sizeof(sum) >= sizeof(BcDig) * 2);
@@ -1075,14 +1209,15 @@ static void bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c) {
// If we don't memset, then we might have uninitialized data use later.
ptr_c = c->num;
+ // NOLINTNEXTLINE
memset(ptr_c, 0, BC_NUM_SIZE(c->cap));
// This is the actual multiplication loop. It uses the lattice form of long
// multiplication (see the explanation on the web page at
// https://knilt.arcc.albany.edu/What_is_Lattice_Multiplication or the
// explanation at Wikipedia).
- for (i = 0; i < clen; ++i) {
-
+ for (i = 0; i < clen; ++i)
+ {
ssize_t sidx = (ssize_t) (i - blen + 1);
size_t j, k;
@@ -1092,18 +1227,20 @@ static void bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c) {
// On every iteration of this loop, a multiplication happens, then the
// sum is automatically calculated.
- for (; j < alen && k < blen; ++j, --k) {
-
+ for (; j < alen && k < blen; ++j, --k)
+ {
sum += ((BcBigDig) ptr_a[j]) * ((BcBigDig) ptr_b[k]);
- if (sum >= ((BcBigDig) BC_BASE_POW) * BC_BASE_POW) {
+ if (sum >= ((BcBigDig) BC_BASE_POW) * BC_BASE_POW)
+ {
carry += sum / BC_BASE_POW;
sum %= BC_BASE_POW;
}
}
// Calculate the carry.
- if (sum >= BC_BASE_POW) {
+ if (sum >= BC_BASE_POW)
+ {
carry += sum / BC_BASE_POW;
sum %= BC_BASE_POW;
}
@@ -1131,8 +1268,9 @@ static void bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c) {
* @param op The function to call, either bc_num_addArrays() or
* bc_num_subArrays().
*/
-static void bc_num_shiftAddSub(BcNum *restrict n, const BcNum *restrict a,
- size_t shift, BcNumShiftAddOp op)
+static void
+bc_num_shiftAddSub(BcNum* restrict n, const BcNum* restrict a, size_t shift,
+ BcNumShiftAddOp op)
{
assert(n->len >= shift + a->len);
assert(!BC_NUM_RDX_VAL(n) && !BC_NUM_RDX_VAL(a));
@@ -1142,11 +1280,13 @@ static void bc_num_shiftAddSub(BcNum *restrict n, const BcNum *restrict a,
/**
* Implements the Karatsuba algorithm.
*/
-static void bc_num_k(const BcNum *a, const BcNum *b, BcNum *restrict c) {
-
+static void
+bc_num_k(const BcNum* a, const BcNum* b, BcNum* restrict c)
+{
size_t max, max2, total;
BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
- BcDig *digs, *dig_ptr;
+ BcDig* digs;
+ BcDig* dig_ptr;
BcNumShiftAddOp op;
bool aone = BC_NUM_ONE(a);
@@ -1154,14 +1294,16 @@ static void bc_num_k(const BcNum *a, const BcNum *b, BcNum *restrict c) {
if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) return;
- if (aone || BC_NUM_ONE(b)) {
+ if (aone || BC_NUM_ONE(b))
+ {
bc_num_copy(c, aone ? b : a);
if ((aone && BC_NUM_NEG(a)) || BC_NUM_NEG(b)) BC_NUM_NEG_TGL(c);
return;
}
// Shell out to the simple algorithm with certain conditions.
- if (a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN) {
+ if (a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
+ {
bc_num_m_simp(a, b, c);
return;
}
@@ -1210,6 +1352,7 @@ static void bc_num_k(const BcNum *a, const BcNum *b, BcNum *restrict c) {
// First, set up c.
bc_num_expand(c, max);
c->len = max;
+ // NOLINTNEXTLINE
memset(c->num, 0, BC_NUM_SIZE(c->len));
// Split the parameters.
@@ -1225,8 +1368,8 @@ static void bc_num_k(const BcNum *a, const BcNum *b, BcNum *restrict c) {
// the ollocations and splits are done, the algorithm is pretty
// straightforward.
- if (BC_NUM_NONZERO(&h1) && BC_NUM_NONZERO(&h2)) {
-
+ if (BC_NUM_NONZERO(&h1) && BC_NUM_NONZERO(&h2))
+ {
assert(BC_NUM_RDX_VALID_NP(h1));
assert(BC_NUM_RDX_VALID_NP(h2));
@@ -1237,8 +1380,8 @@ static void bc_num_k(const BcNum *a, const BcNum *b, BcNum *restrict c) {
bc_num_shiftAddSub(c, &z2, max2, bc_num_addArrays);
}
- if (BC_NUM_NONZERO(&l1) && BC_NUM_NONZERO(&l2)) {
-
+ if (BC_NUM_NONZERO(&l1) && BC_NUM_NONZERO(&l2))
+ {
assert(BC_NUM_RDX_VALID_NP(l1));
assert(BC_NUM_RDX_VALID_NP(l2));
@@ -1249,8 +1392,8 @@ static void bc_num_k(const BcNum *a, const BcNum *b, BcNum *restrict c) {
bc_num_shiftAddSub(c, &z0, 0, bc_num_addArrays);
}
- if (BC_NUM_NONZERO(&m1) && BC_NUM_NONZERO(&m2)) {
-
+ if (BC_NUM_NONZERO(&m1) && BC_NUM_NONZERO(&m2))
+ {
assert(BC_NUM_RDX_VALID_NP(m1));
assert(BC_NUM_RDX_VALID_NP(m1));
@@ -1258,7 +1401,8 @@ static void bc_num_k(const BcNum *a, const BcNum *b, BcNum *restrict c) {
bc_num_clean(&z1);
op = (BC_NUM_NEG_NP(m1) != BC_NUM_NEG_NP(m2)) ?
- bc_num_subArrays : bc_num_addArrays;
+ bc_num_subArrays :
+ bc_num_addArrays;
bc_num_shiftAddSub(c, &z1, max2, op);
}
@@ -1281,8 +1425,9 @@ err:
* @param c The return parameter.
* @param scale The current scale.
*/
-static void bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
-
+static void
+bc_num_m(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale)
+{
BcNum cpa, cpb;
size_t ascale, bscale, ardx, brdx, azero = 0, bzero = 0, zero, len, rscale;
@@ -1302,17 +1447,19 @@ static void bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
// If this condition is true, we can use bc_num_mulArray(), which would be
// much faster.
- if ((a->len == 1 || b->len == 1) && !a->rdx && !b->rdx) {
-
- BcNum *operand;
+ if ((a->len == 1 || b->len == 1) && !a->rdx && !b->rdx)
+ {
+ BcNum* operand;
BcBigDig dig;
// Set the correct operands.
- if (a->len == 1) {
+ if (a->len == 1)
+ {
dig = (BcBigDig) a->num[0];
operand = b;
}
- else {
+ else
+ {
dig = (BcBigDig) b->num[0];
operand = a;
}
@@ -1321,7 +1468,9 @@ static void bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
// Need to make sure the sign is correct.
if (BC_NUM_NONZERO(c))
+ {
c->rdx = BC_NUM_NEG_VAL(c, BC_NUM_NEG(a) != BC_NUM_NEG(b));
+ }
return;
}
@@ -1408,10 +1557,18 @@ err:
* @param len The length of the array.
* @return True if @a has any non-zero limbs, false otherwise.
*/
-static bool bc_num_nonZeroDig(BcDig *restrict a, size_t len) {
+static bool
+bc_num_nonZeroDig(BcDig* restrict a, size_t len)
+{
size_t i;
+
bool nonzero = false;
- for (i = len - 1; !nonzero && i < len; --i) nonzero = (a[i] != 0);
+
+ for (i = len - 1; !nonzero && i < len; --i)
+ {
+ nonzero = (a[i] != 0);
+ }
+
return nonzero;
}
@@ -1424,12 +1581,14 @@ static bool bc_num_nonZeroDig(BcDig *restrict a, size_t len) {
* @param len The length to assume the arrays are. This is always less than the
* actual length because of how this is implemented.
*/
-static ssize_t bc_num_divCmp(const BcDig *a, const BcNum *b, size_t len) {
-
+static ssize_t
+bc_num_divCmp(const BcDig* a, const BcNum* b, size_t len)
+{
ssize_t cmp;
if (b->len > len && a[len]) cmp = bc_num_compare(a, b->num, len + 1);
- else if (b->len <= len) {
+ else if (b->len <= len)
+ {
if (a[len]) cmp = 1;
else cmp = bc_num_compare(a, b->num, len);
}
@@ -1446,8 +1605,8 @@ static ssize_t bc_num_divCmp(const BcDig *a, const BcNum *b, size_t len) {
* @param b The second operand.
* @param divisor The divisor estimate.
*/
-static void bc_num_divExtend(BcNum *restrict a, BcNum *restrict b,
- BcBigDig divisor)
+static void
+bc_num_divExtend(BcNum* restrict a, BcNum* restrict b, BcBigDig divisor)
{
size_t pow;
@@ -1467,8 +1626,9 @@ static void bc_num_divExtend(BcNum *restrict a, BcNum *restrict b,
* @param c The return parameter.
* @param scale The current scale.
*/
-static void bc_num_d_long(BcNum *restrict a, BcNum *restrict b,
- BcNum *restrict c, size_t scale)
+static void
+bc_num_d_long(BcNum* restrict a, BcNum* restrict b, BcNum* restrict c,
+ size_t scale)
{
BcBigDig divisor;
size_t len, end, i, rdx;
@@ -1485,6 +1645,7 @@ static void bc_num_d_long(BcNum *restrict a, BcNum *restrict b,
// This is a final time to make sure c is big enough and that its array is
// properly zeroed.
bc_num_expand(c, a->len);
+ // NOLINTNEXTLINE
memset(c->num, 0, c->cap * sizeof(BcDig));
// Setup.
@@ -1498,8 +1659,8 @@ static void bc_num_d_long(BcNum *restrict a, BcNum *restrict b,
// The entire bit of code in this if statement is to tighten the estimate of
// the divisor. The condition asks if b has any other non-zero limbs.
- if (len > 1 && bc_num_nonZeroDig(b->num, len - 1)) {
-
+ if (len > 1 && bc_num_nonZeroDig(b->num, len - 1))
+ {
// This takes a little bit of understanding. The "10*BC_BASE_DIGS/6+1"
// results in either 16 for 64-bit 9-digit limbs or 7 for 32-bit 4-digit
// limbs. Then it shifts a 1 by that many, which in both cases, puts the
@@ -1509,8 +1670,8 @@ static void bc_num_d_long(BcNum *restrict a, BcNum *restrict b,
nonzero = (divisor > 1 << ((10 * BC_BASE_DIGS) / 6 + 1));
// If the divisor is *not* greater than half the limb...
- if (!nonzero) {
-
+ if (!nonzero)
+ {
// Extend the parameters by the number of missing digits in the
// divisor.
bc_num_divExtend(a, b, divisor);
@@ -1539,6 +1700,7 @@ static void bc_num_d_long(BcNum *restrict a, BcNum *restrict b,
// Make sure c can fit the new length.
bc_num_expand(c, a->len);
+ // NOLINTNEXTLINE
memset(c->num, 0, BC_NUM_SIZE(c->cap));
assert(c->scale >= scale);
@@ -1553,10 +1715,10 @@ static void bc_num_d_long(BcNum *restrict a, BcNum *restrict b,
BC_SIG_UNLOCK;
// This is the actual division loop.
- for (i = end - 1; i < end && i >= rdx && BC_NUM_NONZERO(a); --i) {
-
+ for (i = end - 1; i < end && i >= rdx && BC_NUM_NONZERO(a); --i)
+ {
ssize_t cmp;
- BcDig *n;
+ BcDig* n;
BcBigDig result;
n = a->num + i;
@@ -1568,8 +1730,8 @@ static void bc_num_d_long(BcNum *restrict a, BcNum *restrict b,
// This is true if n is greater than b, which means that division can
// proceed, so this inner loop is the part that implements one instance
// of the division.
- while (cmp >= 0) {
-
+ while (cmp >= 0)
+ {
BcBigDig n1, dividend, quotient;
// These should be named obviously enough. Just imagine that it's a
@@ -1581,12 +1743,13 @@ static void bc_num_d_long(BcNum *restrict a, BcNum *restrict b,
// If this is true, then we can just subtract. Remember: setting
// quotient to 1 is not bad because we already know that n is
// greater than b.
- if (quotient <= 1) {
+ if (quotient <= 1)
+ {
quotient = 1;
bc_num_subArrays(n, b->num, len);
}
- else {
-
+ else
+ {
assert(quotient <= BC_BASE_POW);
// We need to multiply and subtract for a quotient above 1.
@@ -1625,26 +1788,30 @@ err:
* @param c The return parameter.
* @param scale The current scale.
*/
-static void bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
-
+static void
+bc_num_d(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale)
+{
size_t len, cpardx;
BcNum cpa, cpb;
if (BC_NUM_ZERO(b)) bc_err(BC_ERR_MATH_DIVIDE_BY_ZERO);
- if (BC_NUM_ZERO(a)) {
+ if (BC_NUM_ZERO(a))
+ {
bc_num_setToZero(c, scale);
return;
}
- if (BC_NUM_ONE(b)) {
+ if (BC_NUM_ONE(b))
+ {
bc_num_copy(c, a);
bc_num_retireMul(c, scale, BC_NUM_NEG(a), BC_NUM_NEG(b));
return;
}
// If this is true, we can use bc_num_divArray(), which would be faster.
- if (!BC_NUM_RDX_VAL(a) && !BC_NUM_RDX_VAL(b) && b->len == 1 && !scale) {
+ if (!BC_NUM_RDX_VAL(a) && !BC_NUM_RDX_VAL(b) && b->len == 1 && !scale)
+ {
BcBigDig rem;
bc_num_divArray(a, (BcBigDig) b->num[0], c, &rem);
bc_num_retireMul(c, scale, BC_NUM_NEG(a), BC_NUM_NEG(b));
@@ -1670,7 +1837,8 @@ static void bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
// Like the above comment, we want the copy of the first parameter to be
// larger than the second parameter.
- if (len > cpa.len) {
+ if (len > cpa.len)
+ {
bc_num_expand(&cpa, bc_vm_growSize(len, 2));
bc_num_extend(&cpa, (len - cpa.len) * BC_BASE_DIGS);
}
@@ -1685,7 +1853,8 @@ static void bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
cpa.scale = cpardx * BC_BASE_DIGS;
// Once again, just setting things up, this time to match scale.
- if (scale > cpa.scale) {
+ if (scale > cpa.scale)
+ {
bc_num_extend(&cpa, scale);
cpardx = BC_NUM_RDX_VAL_NP(cpa);
cpa.scale = cpardx * BC_BASE_DIGS;
@@ -1730,15 +1899,17 @@ err:
* @param ts The scale that the operation should be done to. Yes, it's not
* necessarily the same as scale, per the bc spec.
*/
-static void bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
- BcNum *restrict d, size_t scale, size_t ts)
+static void
+bc_num_r(BcNum* a, BcNum* b, BcNum* restrict c, BcNum* restrict d, size_t scale,
+ size_t ts)
{
BcNum temp;
bool neg;
if (BC_NUM_ZERO(b)) bc_err(BC_ERR_MATH_DIVIDE_BY_ZERO);
- if (BC_NUM_ZERO(a)) {
+ if (BC_NUM_ZERO(a))
+ {
bc_num_setToZero(c, ts);
bc_num_setToZero(d, ts);
return;
@@ -1786,8 +1957,9 @@ err:
* @param c The return parameter.
* @param scale The current scale.
*/
-static void bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
-
+static void
+bc_num_rem(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale)
+{
BcNum c1;
size_t ts;
@@ -1818,8 +1990,9 @@ err:
* @param c The return parameter.
* @param scale The current scale.
*/
-static void bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
-
+static void
+bc_num_p(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale)
+{
BcNum copy, btemp;
BcBigDig exp;
size_t powrdx, resrdx;
@@ -1827,18 +2000,21 @@ static void bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
if (BC_ERR(bc_num_nonInt(b, &btemp))) bc_err(BC_ERR_MATH_NON_INTEGER);
- if (BC_NUM_ZERO(&btemp)) {
+ if (BC_NUM_ZERO(&btemp))
+ {
bc_num_one(c);
return;
}
- if (BC_NUM_ZERO(a)) {
+ if (BC_NUM_ZERO(a))
+ {
if (BC_NUM_NEG_NP(btemp)) bc_err(BC_ERR_MATH_DIVIDE_BY_ZERO);
bc_num_setToZero(c, scale);
return;
}
- if (BC_NUM_ONE(&btemp)) {
+ if (BC_NUM_ONE(&btemp))
+ {
if (!BC_NUM_NEG_NP(btemp)) bc_num_copy(c, a);
else bc_num_inv(a, c, scale);
return;
@@ -1859,7 +2035,8 @@ static void bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
// If this is true, then we do not have to do a division, and we need to
// set scale accordingly.
- if (!neg) {
+ if (!neg)
+ {
size_t max = BC_MAX(scale, a->scale), scalepow;
scalepow = bc_num_mulOverflow(a->scale, exp);
scale = BC_MIN(scalepow, max);
@@ -1867,7 +2044,8 @@ static void bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
// This is only implementing the first exponentiation by squaring, until it
// reaches the first time where the square is actually used.
- for (powrdx = a->scale; !(exp & 1); exp >>= 1) {
+ for (powrdx = a->scale; !(exp & 1); exp >>= 1)
+ {
powrdx <<= 1;
assert(BC_NUM_RDX_VALID_NP(copy));
bc_num_mul(&copy, &copy, &copy, powrdx);
@@ -1880,15 +2058,16 @@ static void bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
// Now finish the exponentiation by squaring, this time saving the squares
// as necessary.
- while (exp >>= 1) {
-
+ while (exp >>= 1)
+ {
powrdx <<= 1;
assert(BC_NUM_RDX_VALID_NP(copy));
bc_num_mul(&copy, &copy, &copy, powrdx);
// If this is true, we want to save that particular square. This does
// that by multiplying c with copy.
- if (exp & 1) {
+ if (exp & 1)
+ {
resrdx += powrdx;
assert(BC_NUM_RDX_VALID(c));
assert(BC_NUM_RDX_VALID_NP(copy));
@@ -1918,8 +2097,9 @@ err:
* @param c The return parameter.
* @param scale The current scale.
*/
-static void bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
-
+static void
+bc_num_place(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale)
+{
BcBigDig val;
BC_UNUSED(scale);
@@ -1934,8 +2114,9 @@ static void bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
/**
* Implements the left shift operator. This is a BcNumBinOp function.
*/
-static void bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
-
+static void
+bc_num_left(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale)
+{
BcBigDig val;
BC_UNUSED(scale);
@@ -1948,8 +2129,9 @@ static void bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
/**
* Implements the right shift operator. This is a BcNumBinOp function.
*/
-static void bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
-
+static void
+bc_num_right(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale)
+{
BcBigDig val;
BC_UNUSED(scale);
@@ -1980,10 +2162,13 @@ static void bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
* @param scale The current scale.
* @param req The number of limbs needed to fit the result.
*/
-static void bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
- BcNumBinOp op, size_t req)
+static void
+bc_num_binary(BcNum* a, BcNum* b, BcNum* c, size_t scale, BcNumBinOp op,
+ size_t req)
{
- BcNum *ptr_a, *ptr_b, num2;
+ BcNum* ptr_a;
+ BcNum* ptr_b;
+ BcNum num2;
bool init = false;
assert(a != NULL && b != NULL && c != NULL && op != NULL);
@@ -1994,35 +2179,40 @@ static void bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
BC_SIG_LOCK;
// Reallocate if c == a.
- if (c == a) {
-
+ if (c == a)
+ {
ptr_a = &num2;
+ // NOLINTNEXTLINE
memcpy(ptr_a, c, sizeof(BcNum));
init = true;
}
- else {
+ else
+ {
ptr_a = a;
}
// Also reallocate if c == b.
- if (c == b) {
-
+ if (c == b)
+ {
ptr_b = &num2;
- if (c != a) {
+ if (c != a)
+ {
+ // NOLINTNEXTLINE
memcpy(ptr_b, c, sizeof(BcNum));
init = true;
}
}
- else {
+ else
+ {
ptr_b = b;
}
// Actually reallocate. If we don't reallocate, we want to expand at the
// very least.
- if (init) {
-
+ if (init)
+ {
bc_num_init(c, req);
// Must prepare for cleanup. We want this here so that locals that got
@@ -2030,7 +2220,8 @@ static void bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
BC_SETJMP_LOCKED(err);
BC_SIG_UNLOCK;
}
- else {
+ else
+ {
BC_SIG_UNLOCK;
bc_num_expand(c, req);
}
@@ -2049,15 +2240,14 @@ static void bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
err:
// Cleanup only needed if we initialized c to a new number.
- if (init) {
+ if (init)
+ {
BC_SIG_MAYLOCK;
bc_num_free(&num2);
BC_LONGJMP_CONT;
}
}
-#if !defined(NDEBUG) || BC_ENABLE_LIBRARY
-
/**
* Tests a number string for validity. This function has a history; I originally
* wrote it because I did not trust my parser. Over time, however, I came to
@@ -2067,8 +2257,9 @@ err:
* @param val The string to check to see if it's a valid number string.
* @return True if the string is a valid number string, false otherwise.
*/
-bool bc_num_strValid(const char *restrict val) {
-
+bool
+bc_num_strValid(const char* restrict val)
+{
bool radix = false;
size_t i, len = strlen(val);
@@ -2080,13 +2271,13 @@ bool bc_num_strValid(const char *restrict val) {
if (!len) return true;
// Loop through the characters.
- for (i = 0; i < len; ++i) {
-
+ for (i = 0; i < len; ++i)
+ {
BcDig c = val[i];
// If we have found a radix point...
- if (c == '.') {
-
+ if (c == '.')
+ {
// We don't allow two radices.
if (radix) return false;
@@ -2100,7 +2291,6 @@ bool bc_num_strValid(const char *restrict val) {
return true;
}
-#endif // !defined(NDEBUG) || BC_ENABLE_LIBRARY
/**
* Parses one character and returns the digit that corresponds to that
@@ -2109,13 +2299,14 @@ bool bc_num_strValid(const char *restrict val) {
* @param base The base.
* @return The character as a digit.
*/
-static BcBigDig bc_num_parseChar(char c, size_t base) {
-
+static BcBigDig
+bc_num_parseChar(char c, size_t base)
+{
assert(isupper(c) || isdigit(c));
// If a letter...
- if (isupper(c)) {
-
+ if (isupper(c))
+ {
// This returns the digit that directly corresponds with the letter.
c = BC_NUM_NUM_LETTER(c);
@@ -2134,14 +2325,18 @@ static BcBigDig bc_num_parseChar(char c, size_t base) {
* @param n The number to parse into and return. Must be preallocated.
* @param val The string to parse.
*/
-static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
-
+static void
+bc_num_parseDecimal(BcNum* restrict n, const char* restrict val)
+{
size_t len, i, temp, mod;
- const char *ptr;
+ const char* ptr;
bool zero = true, rdx;
// Eat leading zeroes.
- for (i = 0; val[i] == '0'; ++i);
+ for (i = 0; val[i] == '0'; ++i)
+ {
+ continue;
+ }
val += i;
assert(!val[0] || isalnum(val[0]) || val[0] == '.');
@@ -2161,13 +2356,16 @@ static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
// We eat leading zeroes again. These leading zeroes are different because
// they will come after the decimal point if they exist, and since that's
// the case, they must be preserved.
- for (i = 0; i < len && (zero = (val[i] == '0' || val[i] == '.')); ++i);
+ for (i = 0; i < len && (zero = (val[i] == '0' || val[i] == '.')); ++i)
+ {
+ continue;
+ }
// Set the scale of the number based on the location of the decimal point.
// The casts to uintptr_t is to ensure that bc does not hit undefined
// behavior when doing math on the values.
- n->scale = (size_t) (rdx * (((uintptr_t) (val + len)) -
- (((uintptr_t) ptr) + 1)));
+ n->scale = (size_t) (rdx *
+ (((uintptr_t) (val + len)) - (((uintptr_t) ptr) + 1)));
// Set rdx.
BC_NUM_RDX_SET(n, BC_NUM_RDX(n->scale));
@@ -2182,15 +2380,17 @@ static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
// Expand and zero.
bc_num_expand(n, n->len);
+ // NOLINTNEXTLINE
memset(n->num, 0, BC_NUM_SIZE(n->len));
- if (zero) {
+ if (zero)
+ {
// I think I can set rdx directly to zero here because n should be a
// new number with sign set to false.
n->len = n->rdx = 0;
}
- else {
-
+ else
+ {
// There is actually stuff to parse if we make it here. Yay...
BcBigDig exp, pow;
@@ -2202,14 +2402,14 @@ static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
// Parse loop. We parse backwards because numbers are stored little
// endian.
- for (i = len - 1; i < len; --i, ++exp) {
-
+ for (i = len - 1; i < len; --i, ++exp)
+ {
char c = val[i];
// Skip the decimal point.
if (c == '.') exp -= 1;
- else {
-
+ else
+ {
// The index of the limb.
size_t idx = exp / BC_BASE_DIGS;
@@ -2233,17 +2433,23 @@ static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
* @param val The string to parse.
* @param base The base to parse as.
*/
-static void bc_num_parseBase(BcNum *restrict n, const char *restrict val,
- BcBigDig base)
+static void
+bc_num_parseBase(BcNum* restrict n, const char* restrict val, BcBigDig base)
{
- BcNum temp, mult1, mult2, result1, result2, *m1, *m2, *ptr;
+ BcNum temp, mult1, mult2, result1, result2;
+ BcNum* m1;
+ BcNum* m2;
+ BcNum* ptr;
char c = 0;
bool zero = true;
BcBigDig v;
size_t i, digs, len = strlen(val);
// If zero, just return because the number should be virgin (already 0).
- for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
+ for (i = 0; zero && i < len; ++i)
+ {
+ zero = (val[i] == '.' || val[i] == '0');
+ }
if (zero) return;
BC_SIG_LOCK;
@@ -2260,8 +2466,8 @@ static void bc_num_parseBase(BcNum *restrict n, const char *restrict val,
// Parse the integer part. This is the easy part because we just multiply
// the number by the base, then add the digit.
- for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
-
+ for (i = 0; i < len && (c = val[i]) && c != '.'; ++i)
+ {
// Convert the character to a digit.
v = bc_num_parseChar(c, base);
@@ -2299,8 +2505,8 @@ static void bc_num_parseBase(BcNum *restrict n, const char *restrict val,
m2 = &mult2;
// Parse the fractional part. This is the hard part.
- for (i += 1, digs = 0; i < len && (c = val[i]); ++i, ++digs) {
-
+ for (i += 1, digs = 0; i < len && (c = val[i]); ++i, ++digs)
+ {
size_t rdx;
// Convert the character to a digit.
@@ -2343,7 +2549,8 @@ static void bc_num_parseBase(BcNum *restrict n, const char *restrict val,
bc_num_add(n, &result2, n, digs);
// Basic cleanup.
- if (BC_NUM_NONZERO(n)) {
+ if (BC_NUM_NONZERO(n))
+ {
if (n->scale < digs) bc_num_extend(n, digs - n->scale);
}
else bc_num_zero(n);
@@ -2364,9 +2571,12 @@ int_err:
* Prints a backslash+newline combo if the number of characters needs it. This
* is really a convenience function.
*/
-static inline void bc_num_printNewline(void) {
+static inline void
+bc_num_printNewline(void)
+{
#if !BC_ENABLE_LIBRARY
- if (vm.nchars >= vm.line_len - 1 && vm.line_len) {
+ if (vm.nchars >= vm.line_len - 1 && vm.line_len)
+ {
bc_vm_putchar('\\', bc_flush_none);
bc_vm_putchar('\n', bc_flush_err);
}
@@ -2378,7 +2588,9 @@ static inline void bc_num_printNewline(void) {
* @param c The character to print.
* @param bslash Whether to print a backslash+newline.
*/
-static void bc_num_putchar(int c, bool bslash) {
+static void
+bc_num_putchar(int c, bool bslash)
+{
if (c != '\n' && bslash) bc_num_printNewline();
bc_vm_putchar(c, bc_flush_save);
}
@@ -2396,7 +2608,9 @@ static void bc_num_putchar(int c, bool bslash) {
* @param bslash True if a backslash+newline should be printed if the character
* limit for the line is reached, false otherwise.
*/
-static void bc_num_printChar(size_t n, size_t len, bool rdx, bool bslash) {
+static void
+bc_num_printChar(size_t n, size_t len, bool rdx, bool bslash)
+{
BC_UNUSED(rdx);
BC_UNUSED(len);
BC_UNUSED(bslash);
@@ -2416,19 +2630,23 @@ static void bc_num_printChar(size_t n, size_t len, bool rdx, bool bslash) {
* @param bslash True if a backslash+newline should be printed if the character
* limit for the line is reached, false otherwise.
*/
-static void bc_num_printDigits(size_t n, size_t len, bool rdx, bool bslash) {
-
+static void
+bc_num_printDigits(size_t n, size_t len, bool rdx, bool bslash)
+{
size_t exp, pow;
// If needed, print the radix; otherwise, print a space to separate digits.
bc_num_putchar(rdx ? '.' : ' ', true);
// Calculate the exponent and power.
- for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= BC_BASE);
+ for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= BC_BASE)
+ {
+ continue;
+ }
// Print each character individually.
- for (exp = 0; exp < len; pow /= BC_BASE, ++exp) {
-
+ for (exp = 0; exp < len; pow /= BC_BASE, ++exp)
+ {
// The individual subdigit.
size_t dig = n / pow;
@@ -2451,8 +2669,9 @@ static void bc_num_printDigits(size_t n, size_t len, bool rdx, bool bslash) {
* @param bslash True if a backslash+newline should be printed if the character
* limit for the line is reached, false otherwise.
*/
-static void bc_num_printHex(size_t n, size_t len, bool rdx, bool bslash) {
-
+static void
+bc_num_printHex(size_t n, size_t len, bool rdx, bool bslash)
+{
BC_UNUSED(len);
BC_UNUSED(bslash);
@@ -2469,15 +2688,16 @@ static void bc_num_printHex(size_t n, size_t len, bool rdx, bool bslash) {
* @param n The number to print.
* @param newline Whether to print backslash+newlines on long enough lines.
*/
-static void bc_num_printDecimal(const BcNum *restrict n, bool newline) {
-
+static void
+bc_num_printDecimal(const BcNum* restrict n, bool newline)
+{
size_t i, j, rdx = BC_NUM_RDX_VAL(n);
bool zero = true;
size_t buffer[BC_BASE_DIGS];
// Print loop.
- for (i = n->len - 1; i < n->len; --i) {
-
+ for (i = n->len - 1; i < n->len; --i)
+ {
BcDig n9 = n->num[i];
size_t temp;
bool irdx = (i == rdx - 1);
@@ -2487,25 +2707,27 @@ static void bc_num_printDecimal(const BcNum *restrict n, bool newline) {
temp = n->scale % BC_BASE_DIGS;
temp = i || !temp ? 0 : BC_BASE_DIGS - temp;
+ // NOLINTNEXTLINE
memset(buffer, 0, BC_BASE_DIGS * sizeof(size_t));
// Fill the buffer with individual digits.
- for (j = 0; n9 && j < BC_BASE_DIGS; ++j) {
+ for (j = 0; n9 && j < BC_BASE_DIGS; ++j)
+ {
buffer[j] = ((size_t) n9) % BC_BASE;
n9 /= BC_BASE;
}
// Print the digits in the buffer.
- for (j = BC_BASE_DIGS - 1; j < BC_BASE_DIGS && j >= temp; --j) {
-
+ for (j = BC_BASE_DIGS - 1; j < BC_BASE_DIGS && j >= temp; --j)
+ {
// Figure out whether to print the decimal point.
bool print_rdx = (irdx & (j == BC_BASE_DIGS - 1));
// The zero variable helps us skip leading zero digits in the limb.
zero = (zero && buffer[j] == 0);
- if (!zero) {
-
+ if (!zero)
+ {
// While the first three arguments should be self-explanatory,
// the last needs explaining. I don't want to print a newline
// when the last digit to be printed could take the place of the
@@ -2527,8 +2749,8 @@ static void bc_num_printDecimal(const BcNum *restrict n, bool newline) {
* @param eng True if we are in engineering mode.
* @param newline Whether to print backslash+newlines on long enough lines.
*/
-static void bc_num_printExponent(const BcNum *restrict n,
- bool eng, bool newline)
+static void
+bc_num_printExponent(const BcNum* restrict n, bool eng, bool newline)
{
size_t places, mod, nrdx = BC_NUM_RDX_VAL(n);
bool neg = (n->len <= nrdx);
@@ -2545,15 +2767,16 @@ static void bc_num_printExponent(const BcNum *restrict n,
// We need to calculate the exponents, and they change based on whether the
// number is all fractional or not, obviously.
- if (neg) {
-
+ if (neg)
+ {
// Figure out how many limbs after the decimal point is zero.
size_t i, idx = bc_num_nonZeroLen(n) - 1;
places = 1;
// Figure out how much in the last limb is zero.
- for (i = BC_BASE_DIGS - 1; i < BC_BASE_DIGS; --i) {
+ for (i = BC_BASE_DIGS - 1; i < BC_BASE_DIGS; --i)
+ {
if (bc_num_pow10[i] > (BcBigDig) n->num[idx]) places += 1;
else break;
}
@@ -2569,8 +2792,8 @@ static void bc_num_printExponent(const BcNum *restrict n,
// Shift the temp to the right place.
bc_num_shiftLeft(&temp, places);
}
- else {
-
+ else
+ {
// This is the number of digits that we are supposed to put behind the
// decimal point.
places = bc_num_intDigits(n) - 1;
@@ -2591,7 +2814,8 @@ static void bc_num_printExponent(const BcNum *restrict n,
bc_num_putchar('e', !newline);
// Need to explicitly print a zero exponent.
- if (!places) {
+ if (!places)
+ {
bc_num_printHex(0, 1, false, !newline);
goto exit;
}
@@ -2623,12 +2847,12 @@ exit:
* conversion is O(n^2); we have to sweep through starting at the
* least significant limb
*/
-static void bc_num_printFixup(BcNum *restrict n, BcBigDig rem,
- BcBigDig pow, size_t idx)
+static void
+bc_num_printFixup(BcNum* restrict n, BcBigDig rem, BcBigDig pow, size_t idx)
{
size_t i, len = n->len - idx;
BcBigDig acc;
- BcDig *a = n->num + idx;
+ BcDig* a = n->num + idx;
// Ignore if there's just one limb left. This is the part that requires the
// extra loop after the one calling this function in bc_num_printPrepare().
@@ -2636,8 +2860,8 @@ static void bc_num_printFixup(BcNum *restrict n, BcBigDig rem,
// Loop through the remaining limbs and convert. We start at the second limb
// because we pull the value from the previous one as well.
- for (i = len - 1; i > 0; --i) {
-
+ for (i = len - 1; i > 0; --i)
+ {
// Get the limb and add it to the previous, along with multiplying by
// the remainder because that's the proper overflow. "acc" means
// "accumulator," by the way.
@@ -2651,11 +2875,11 @@ static void bc_num_printFixup(BcNum *restrict n, BcBigDig rem,
acc += (BcBigDig) a[i];
// If the accumulator is greater than the base...
- if (acc >= BC_BASE_POW) {
-
+ if (acc >= BC_BASE_POW)
+ {
// Do we need to grow?
- if (i == len - 1) {
-
+ if (i == len - 1)
+ {
// Grow.
len = bc_vm_growSize(len, 1);
bc_num_expand(n, bc_vm_growSize(len, idx));
@@ -2690,27 +2914,31 @@ static void bc_num_printFixup(BcNum *restrict n, BcBigDig rem,
* @param rem The remainder of BC_BASE_POW when divided by a power of the base.
* @param pow The power of the base.
*/
-static void bc_num_printPrepare(BcNum *restrict n, BcBigDig rem, BcBigDig pow) {
-
+static void
+bc_num_printPrepare(BcNum* restrict n, BcBigDig rem, BcBigDig pow)
+{
size_t i;
// Loop from the least significant limb to the most significant limb and
// convert limbs in each pass.
- for (i = 0; i < n->len; ++i) bc_num_printFixup(n, rem, pow, i);
+ for (i = 0; i < n->len; ++i)
+ {
+ bc_num_printFixup(n, rem, pow, i);
+ }
// bc_num_printFixup() does not do everything it is supposed to, so we do
// the last bit of cleanup here. That cleanup is to ensure that each limb
// is less than pow and to expand the number to fit new limbs as necessary.
- for (i = 0; i < n->len; ++i) {
-
+ for (i = 0; i < n->len; ++i)
+ {
assert(pow == ((BcBigDig) ((BcDig) pow)));
// If the limb needs fixing...
- if (n->num[i] >= (BcDig) pow) {
-
+ if (n->num[i] >= (BcDig) pow)
+ {
// Do we need to grow?
- if (i + 1 == n->len) {
-
+ if (i + 1 == n->len)
+ {
// Grow the number.
n->len = bc_vm_growSize(n->len, 1);
bc_num_expand(n, n->len);
@@ -2728,12 +2956,17 @@ static void bc_num_printPrepare(BcNum *restrict n, BcBigDig rem, BcBigDig pow) {
}
}
-static void bc_num_printNum(BcNum *restrict n, BcBigDig base, size_t len,
- BcNumDigitOp print, bool newline)
+static void
+bc_num_printNum(BcNum* restrict n, BcBigDig base, size_t len,
+ BcNumDigitOp print, bool newline)
{
BcVec stack;
- BcNum intp, fracp1, fracp2, digit, flen1, flen2, *n1, *n2, *temp;
- BcBigDig dig = 0, *ptr, acc, exp;
+ BcNum intp, fracp1, fracp2, digit, flen1, flen2;
+ BcNum* n1;
+ BcNum* n2;
+ BcNum* temp;
+ BcBigDig dig = 0, acc, exp;
+ BcBigDig* ptr;
size_t i, j, nrdx, idigits;
bool radix;
BcDig digit_digs[BC_NUM_BIGDIG_LOG10 + 1];
@@ -2741,7 +2974,8 @@ static void bc_num_printNum(BcNum *restrict n, BcBigDig base, size_t len,
assert(base > 1);
// Easy case. Even with scale, we just print this.
- if (BC_NUM_ZERO(n)) {
+ if (BC_NUM_ZERO(n))
+ {
print(0, len, false, !newline);
return;
}
@@ -2810,13 +3044,14 @@ static void bc_num_printNum(BcNum *restrict n, BcBigDig base, size_t len,
// exponent and power. That is to prevent us from calculating them every
// time because printing will probably happen multiple times on the same
// base.
- if (base != vm.last_base) {
-
+ if (base != vm.last_base)
+ {
vm.last_pow = 1;
vm.last_exp = 0;
// Calculate the exponent and power.
- while (vm.last_pow * base <= BC_BASE_POW) {
+ while (vm.last_pow * base <= BC_BASE_POW)
+ {
vm.last_pow *= base;
vm.last_exp += 1;
}
@@ -2838,8 +3073,8 @@ static void bc_num_printNum(BcNum *restrict n, BcBigDig base, size_t len,
// this is basically naive code that I wrote, adjusted for the larger bases.
// Fill the stack of digits for the integer part.
- for (i = 0; i < intp.len; ++i) {
-
+ for (i = 0; i < intp.len; ++i)
+ {
// Get the limb.
acc = (BcBigDig) intp.num[i];
@@ -2847,11 +3082,13 @@ static void bc_num_printNum(BcNum *restrict n, BcBigDig base, size_t len,
for (j = 0; j < exp && (i < intp.len - 1 || acc != 0); ++j)
{
// This condition is true if we are not at the last digit.
- if (j != exp - 1) {
+ if (j != exp - 1)
+ {
dig = acc % base;
acc /= base;
}
- else {
+ else
+ {
dig = acc;
acc = 0;
}
@@ -2866,8 +3103,8 @@ static void bc_num_printNum(BcNum *restrict n, BcBigDig base, size_t len,
}
// Go through the stack backwards and print each digit.
- for (i = 0; i < stack.len; ++i) {
-
+ for (i = 0; i < stack.len; ++i)
+ {
ptr = bc_vec_item_rev(&stack, i);
assert(ptr != NULL);
@@ -2877,8 +3114,8 @@ static void bc_num_printNum(BcNum *restrict n, BcBigDig base, size_t len,
// to be printed could take the place of the backslash rather than being
// pushed, as a single character, to the next line. That's what that
// last argument does for bc.
- print(*ptr, len, false, !newline ||
- (n->scale != 0 || i == stack.len - 1));
+ print(*ptr, len, false,
+ !newline || (n->scale != 0 || i == stack.len - 1));
}
// We are done if there is no fractional part.
@@ -2910,8 +3147,8 @@ static void bc_num_printNum(BcNum *restrict n, BcBigDig base, size_t len,
BC_NUM_RDX_SET_NP(fracp2, BC_NUM_RDX(fracp2.scale));
// As long as we have not reached the scale of the number, keep printing.
- while ((idigits = bc_num_intDigits(n1)) <= n->scale) {
-
+ while ((idigits = bc_num_intDigits(n1)) <= n->scale)
+ {
// These numbers will keep growing.
bc_num_expand(&fracp2, fracp1.len + 1);
bc_num_mulArray(&fracp1, base, &fracp2);
@@ -2966,8 +3203,9 @@ err:
* @param base The base to print in.
* @param newline Whether to print backslash+newlines on long enough lines.
*/
-static void bc_num_printBase(BcNum *restrict n, BcBigDig base, bool newline) {
-
+static void
+bc_num_printBase(BcNum* restrict n, BcBigDig base, bool newline)
+{
size_t width;
BcNumDigitOp print;
bool neg = BC_NUM_NEG(n);
@@ -2978,11 +3216,13 @@ static void bc_num_printBase(BcNum *restrict n, BcBigDig base, bool newline) {
// Bases at hexadecimal and below are printed as one character, larger bases
// are printed as a series of digits separated by spaces.
- if (base <= BC_NUM_MAX_POSIX_IBASE) {
+ if (base <= BC_NUM_MAX_POSIX_IBASE)
+ {
width = 1;
print = bc_num_printHex;
}
- else {
+ else
+ {
assert(base <= BC_BASE_POW);
width = bc_num_log10(base - 1);
print = bc_num_printDigits;
@@ -2997,22 +3237,27 @@ static void bc_num_printBase(BcNum *restrict n, BcBigDig base, bool newline) {
#if !BC_ENABLE_LIBRARY
-void bc_num_stream(BcNum *restrict n) {
+void
+bc_num_stream(BcNum* restrict n)
+{
bc_num_printNum(n, BC_NUM_STREAM_BASE, 1, bc_num_printChar, false);
}
#endif // !BC_ENABLE_LIBRARY
-void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
+void
+bc_num_setup(BcNum* restrict n, BcDig* restrict num, size_t cap)
+{
assert(n != NULL);
n->num = num;
n->cap = cap;
bc_num_zero(n);
}
-void bc_num_init(BcNum *restrict n, size_t req) {
-
- BcDig *num;
+void
+bc_num_init(BcNum* restrict n, size_t req)
+{
+ BcDig* num;
BC_SIG_ASSERT_LOCKED;
@@ -3024,19 +3269,24 @@ void bc_num_init(BcNum *restrict n, size_t req) {
// If we can't use a temp, allocate.
if (req != BC_NUM_DEF_SIZE || (num = bc_vm_takeTemp()) == NULL)
+ {
num = bc_vm_malloc(BC_NUM_SIZE(req));
+ }
bc_num_setup(n, num, req);
}
-void bc_num_clear(BcNum *restrict n) {
+void
+bc_num_clear(BcNum* restrict n)
+{
n->num = NULL;
n->cap = 0;
}
-void bc_num_free(void *num) {
-
- BcNum *n = (BcNum*) num;
+void
+bc_num_free(void* num)
+{
+ BcNum* n = (BcNum*) num;
BC_SIG_ASSERT_LOCKED;
@@ -3046,8 +3296,9 @@ void bc_num_free(void *num) {
else free(n->num);
}
-void bc_num_copy(BcNum *d, const BcNum *s) {
-
+void
+bc_num_copy(BcNum* d, const BcNum* s)
+{
assert(d != NULL && s != NULL);
if (d == s) return;
@@ -3059,35 +3310,43 @@ void bc_num_copy(BcNum *d, const BcNum *s) {
// properly preserved.
d->rdx = s->rdx;
d->scale = s->scale;
+ // NOLINTNEXTLINE
memcpy(d->num, s->num, BC_NUM_SIZE(d->len));
}
-void bc_num_createCopy(BcNum *d, const BcNum *s) {
+void
+bc_num_createCopy(BcNum* d, const BcNum* s)
+{
BC_SIG_ASSERT_LOCKED;
bc_num_init(d, s->len);
bc_num_copy(d, s);
}
-void bc_num_createFromBigdig(BcNum *restrict n, BcBigDig val) {
+void
+bc_num_createFromBigdig(BcNum* restrict n, BcBigDig val)
+{
BC_SIG_ASSERT_LOCKED;
bc_num_init(n, BC_NUM_BIGDIG_LOG10);
bc_num_bigdig2num(n, val);
}
-size_t bc_num_scale(const BcNum *restrict n) {
+size_t
+bc_num_scale(const BcNum* restrict n)
+{
return n->scale;
}
-size_t bc_num_len(const BcNum *restrict n) {
-
+size_t
+bc_num_len(const BcNum* restrict n)
+{
size_t len = n->len;
// Always return at least 1.
if (BC_NUM_ZERO(n)) return n->scale ? n->scale : 1;
// If this is true, there is no integer portion of the number.
- if (BC_NUM_RDX_VAL(n) == len) {
-
+ if (BC_NUM_RDX_VAL(n) == len)
+ {
// We have to take into account the fact that some of the digits right
// after the decimal could be zero. If that is the case, we need to
// ignore them until we hit the first non-zero digit.
@@ -3113,15 +3372,17 @@ size_t bc_num_len(const BcNum *restrict n) {
return len;
}
-void bc_num_parse(BcNum *restrict n, const char *restrict val, BcBigDig base) {
-
+void
+bc_num_parse(BcNum* restrict n, const char* restrict val, BcBigDig base)
+{
assert(n != NULL && val != NULL && base);
assert(base >= BC_NUM_MIN_BASE && base <= vm.maxes[BC_PROG_GLOBALS_IBASE]);
assert(bc_num_strValid(val));
// A one character number is *always* parsed as though the base was the
// maximum allowed ibase, per the bc spec.
- if (!val[1]) {
+ if (!val[1])
+ {
BcBigDig dig = bc_num_parseChar(val[0], BC_NUM_MAX_LBASE);
bc_num_bigdig2num(n, dig);
}
@@ -3131,22 +3392,25 @@ void bc_num_parse(BcNum *restrict n, const char *restrict val, BcBigDig base) {
assert(BC_NUM_RDX_VALID(n));
}
-void bc_num_print(BcNum *restrict n, BcBigDig base, bool newline) {
-
+void
+bc_num_print(BcNum* restrict n, BcBigDig base, bool newline)
+{
assert(n != NULL);
assert(BC_ENABLE_EXTRA_MATH || base >= BC_NUM_MIN_BASE);
// We may need a newline, just to start.
bc_num_printNewline();
- if (BC_NUM_NONZERO(n)) {
-
+ if (BC_NUM_NONZERO(n))
+ {
// Print the sign.
if (BC_NUM_NEG(n)) bc_num_putchar('-', true);
// Print the leading zero if necessary.
if (BC_Z && BC_NUM_RDX_VAL(n) == n->len)
+ {
bc_num_printHex(0, 1, false, !newline);
+ }
}
// Short-circuit 0.
@@ -3154,15 +3418,18 @@ void bc_num_print(BcNum *restrict n, BcBigDig base, bool newline) {
else if (base == BC_BASE) bc_num_printDecimal(n, newline);
#if BC_ENABLE_EXTRA_MATH
else if (base == 0 || base == 1)
+ {
bc_num_printExponent(n, base != 0, newline);
+ }
#endif // BC_ENABLE_EXTRA_MATH
else bc_num_printBase(n, base, newline);
if (newline) bc_num_putchar('\n', false);
}
-BcBigDig bc_num_bigdig2(const BcNum *restrict n) {
-
+BcBigDig
+bc_num_bigdig2(const BcNum* restrict n)
+{
// This function returns no errors because it's guaranteed to succeed if
// its preconditions are met. Those preconditions include both n needs to
// be non-NULL, n being non-negative, and n being less than vm.max. If all
@@ -3179,21 +3446,23 @@ BcBigDig bc_num_bigdig2(const BcNum *restrict n) {
// There is a small speed win from unrolling the loop here, and since it
// only adds 53 bytes, I decided that it was worth it.
- switch (n->len - nrdx) {
-
+ switch (n->len - nrdx)
+ {
case 3:
{
r = (BcBigDig) n->num[nrdx + 2];
+
+ // Fallthrough.
+ BC_FALLTHROUGH
}
- // Fallthrough.
- BC_FALLTHROUGH
case 2:
{
r = r * BC_BASE_POW + (BcBigDig) n->num[nrdx + 1];
+
+ // Fallthrough.
+ BC_FALLTHROUGH
}
- // Fallthrough.
- BC_FALLTHROUGH
case 1:
{
@@ -3204,8 +3473,9 @@ BcBigDig bc_num_bigdig2(const BcNum *restrict n) {
return r;
}
-BcBigDig bc_num_bigdig(const BcNum *restrict n) {
-
+BcBigDig
+bc_num_bigdig(const BcNum* restrict n)
+{
assert(n != NULL);
// This error checking is extremely important, and if you do not have a
@@ -3219,9 +3489,10 @@ BcBigDig bc_num_bigdig(const BcNum *restrict n) {
return bc_num_bigdig2(n);
}
-void bc_num_bigdig2num(BcNum *restrict n, BcBigDig val) {
-
- BcDig *ptr;
+void
+bc_num_bigdig2num(BcNum* restrict n, BcBigDig val)
+{
+ BcDig* ptr;
size_t i;
assert(n != NULL);
@@ -3238,15 +3509,18 @@ void bc_num_bigdig2num(BcNum *restrict n, BcBigDig val) {
// The conversion is easy because numbers are laid out in little-endian
// order.
for (ptr = n->num, i = 0; val; ++i, val /= BC_BASE_POW)
+ {
ptr[i] = val % BC_BASE_POW;
+ }
n->len = i;
}
#if BC_ENABLE_EXTRA_MATH
-void bc_num_rng(const BcNum *restrict n, BcRNG *rng) {
-
+void
+bc_num_rng(const BcNum* restrict n, BcRNG* rng)
+{
BcNum temp, temp2, intn, frac;
BcRand state1, state2, inc1, inc2;
size_t nrdx = BC_NUM_RDX_VAL(n);
@@ -3269,6 +3543,7 @@ void bc_num_rng(const BcNum *restrict n, BcRNG *rng) {
assert(BC_NUM_RDX_VALID_NP(vm.max));
+ // NOLINTNEXTLINE
memcpy(frac.num, n->num, BC_NUM_SIZE(nrdx));
frac.len = nrdx;
BC_NUM_RDX_SET_NP(frac, nrdx);
@@ -3284,6 +3559,7 @@ void bc_num_rng(const BcNum *restrict n, BcRNG *rng) {
bc_num_copy(&frac, &temp);
// Get the integer.
+ // NOLINTNEXTLINE
memcpy(intn.num, n->num + nrdx, BC_NUM_SIZE(bc_num_int(n)));
intn.len = bc_num_int(n);
@@ -3292,8 +3568,8 @@ void bc_num_rng(const BcNum *restrict n, BcRNG *rng) {
assert(BC_NUM_NONZERO(&vm.max));
// If there *was* a fractional part...
- if (BC_NUM_NONZERO(&frac)) {
-
+ if (BC_NUM_NONZERO(&frac))
+ {
// This divmod splits frac into the two state parts.
bc_num_divmod(&frac, &vm.max, &temp, &temp2, 0);
@@ -3308,8 +3584,8 @@ void bc_num_rng(const BcNum *restrict n, BcRNG *rng) {
else state1 = state2 = 0;
// If there *was* an integer part...
- if (BC_NUM_NONZERO(&intn)) {
-
+ if (BC_NUM_NONZERO(&intn))
+ {
// This divmod splits intn into the two inc parts.
bc_num_divmod(&intn, &vm.max, &temp, &temp2, 0);
@@ -3318,7 +3594,8 @@ void bc_num_rng(const BcNum *restrict n, BcRNG *rng) {
inc1 = (BcRand) bc_num_bigdig2(&temp2);
// Clamp the second inc part.
- if (bc_num_cmp(&temp, &vm.max) >= 0) {
+ if (bc_num_cmp(&temp, &vm.max) >= 0)
+ {
bc_num_copy(&temp2, &temp);
bc_num_mod(&temp2, &vm.max, &temp, 0);
}
@@ -3340,8 +3617,9 @@ err:
BC_LONGJMP_CONT;
}
-void bc_num_createFromRNG(BcNum *restrict n, BcRNG *rng) {
-
+void
+bc_num_createFromRNG(BcNum* restrict n, BcRNG* rng)
+{
BcRand s1, s2, i1, i2;
BcNum conv, temp1, temp2, temp3;
BcDig temp1_num[BC_RAND_NUM_SIZE], temp2_num[BC_RAND_NUM_SIZE];
@@ -3408,8 +3686,9 @@ err:
BC_LONGJMP_CONT;
}
-void bc_num_irand(BcNum *restrict a, BcNum *restrict b, BcRNG *restrict rng) {
-
+void
+bc_num_irand(BcNum* restrict a, BcNum* restrict b, BcRNG* restrict rng)
+{
BcNum atemp;
size_t i, len;
@@ -3428,12 +3707,15 @@ void bc_num_irand(BcNum *restrict a, BcNum *restrict b, BcRNG *restrict rng) {
// Just generate a random number for each limb.
for (i = 0; i < len; ++i)
+ {
b->num[i] = (BcDig) bc_rand_bounded(rng, BC_BASE_POW);
+ }
// Do the last digit explicitly because the bound must be right. But only
// do it if the limb does not equal 1. If it does, we have already hit the
// limit.
- if (atemp.num[i] != 1) {
+ if (atemp.num[i] != 1)
+ {
b->num[i] = (BcDig) bc_rand_bounded(rng, (BcRand) atemp.num[i]);
b->len = atemp.len;
}
@@ -3446,8 +3728,9 @@ void bc_num_irand(BcNum *restrict a, BcNum *restrict b, BcRNG *restrict rng) {
}
#endif // BC_ENABLE_EXTRA_MATH
-size_t bc_num_addReq(const BcNum *a, const BcNum *b, size_t scale) {
-
+size_t
+bc_num_addReq(const BcNum* a, const BcNum* b, size_t scale)
+{
size_t aint, bint, ardx, brdx;
// Addition and subtraction require the max of the length of the two numbers
@@ -3469,8 +3752,9 @@ size_t bc_num_addReq(const BcNum *a, const BcNum *b, size_t scale) {
return bc_vm_growSize(bc_vm_growSize(ardx, aint), 1);
}
-size_t bc_num_mulReq(const BcNum *a, const BcNum *b, size_t scale) {
-
+size_t
+bc_num_mulReq(const BcNum* a, const BcNum* b, size_t scale)
+{
size_t max, rdx;
// Multiplication requires the sum of the lengths of the numbers.
@@ -3485,8 +3769,9 @@ size_t bc_num_mulReq(const BcNum *a, const BcNum *b, size_t scale) {
return rdx;
}
-size_t bc_num_divReq(const BcNum *a, const BcNum *b, size_t scale) {
-
+size_t
+bc_num_divReq(const BcNum* a, const BcNum* b, size_t scale)
+{
size_t max, rdx;
// Division requires the length of the dividend plus the scale.
@@ -3501,77 +3786,103 @@ size_t bc_num_divReq(const BcNum *a, const BcNum *b, size_t scale) {
return rdx;
}
-size_t bc_num_powReq(const BcNum *a, const BcNum *b, size_t scale) {
+size_t
+bc_num_powReq(const BcNum* a, const BcNum* b, size_t scale)
+{
BC_UNUSED(scale);
return bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1);
}
#if BC_ENABLE_EXTRA_MATH
-size_t bc_num_placesReq(const BcNum *a, const BcNum *b, size_t scale) {
+size_t
+bc_num_placesReq(const BcNum* a, const BcNum* b, size_t scale)
+{
BC_UNUSED(scale);
return a->len + b->len - BC_NUM_RDX_VAL(a) - BC_NUM_RDX_VAL(b);
}
#endif // BC_ENABLE_EXTRA_MATH
-void bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
+void
+bc_num_add(BcNum* a, BcNum* b, BcNum* c, size_t scale)
+{
assert(BC_NUM_RDX_VALID(a));
assert(BC_NUM_RDX_VALID(b));
bc_num_binary(a, b, c, false, bc_num_as, bc_num_addReq(a, b, scale));
}
-void bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
+void
+bc_num_sub(BcNum* a, BcNum* b, BcNum* c, size_t scale)
+{
assert(BC_NUM_RDX_VALID(a));
assert(BC_NUM_RDX_VALID(b));
bc_num_binary(a, b, c, true, bc_num_as, bc_num_addReq(a, b, scale));
}
-void bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
+void
+bc_num_mul(BcNum* a, BcNum* b, BcNum* c, size_t scale)
+{
assert(BC_NUM_RDX_VALID(a));
assert(BC_NUM_RDX_VALID(b));
bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale));
}
-void bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
+void
+bc_num_div(BcNum* a, BcNum* b, BcNum* c, size_t scale)
+{
assert(BC_NUM_RDX_VALID(a));
assert(BC_NUM_RDX_VALID(b));
bc_num_binary(a, b, c, scale, bc_num_d, bc_num_divReq(a, b, scale));
}
-void bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
+void
+bc_num_mod(BcNum* a, BcNum* b, BcNum* c, size_t scale)
+{
assert(BC_NUM_RDX_VALID(a));
assert(BC_NUM_RDX_VALID(b));
bc_num_binary(a, b, c, scale, bc_num_rem, bc_num_divReq(a, b, scale));
}
-void bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
+void
+bc_num_pow(BcNum* a, BcNum* b, BcNum* c, size_t scale)
+{
assert(BC_NUM_RDX_VALID(a));
assert(BC_NUM_RDX_VALID(b));
bc_num_binary(a, b, c, scale, bc_num_p, bc_num_powReq(a, b, scale));
}
#if BC_ENABLE_EXTRA_MATH
-void bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
+void
+bc_num_places(BcNum* a, BcNum* b, BcNum* c, size_t scale)
+{
assert(BC_NUM_RDX_VALID(a));
assert(BC_NUM_RDX_VALID(b));
bc_num_binary(a, b, c, scale, bc_num_place, bc_num_placesReq(a, b, scale));
}
-void bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
+void
+bc_num_lshift(BcNum* a, BcNum* b, BcNum* c, size_t scale)
+{
assert(BC_NUM_RDX_VALID(a));
assert(BC_NUM_RDX_VALID(b));
bc_num_binary(a, b, c, scale, bc_num_left, bc_num_placesReq(a, b, scale));
}
-void bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
+void
+bc_num_rshift(BcNum* a, BcNum* b, BcNum* c, size_t scale)
+{
assert(BC_NUM_RDX_VALID(a));
assert(BC_NUM_RDX_VALID(b));
bc_num_binary(a, b, c, scale, bc_num_right, bc_num_placesReq(a, b, scale));
}
#endif // BC_ENABLE_EXTRA_MATH
-void bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
-
- BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
+void
+bc_num_sqrt(BcNum* restrict a, BcNum* restrict b, size_t scale)
+{
+ BcNum num1, num2, half, f, fprime;
+ BcNum* x0;
+ BcNum* x1;
+ BcNum* temp;
size_t pow, len, rdx, req, resscale;
BcDig half_digs[1];
@@ -3603,13 +3914,15 @@ void bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
assert(a->num != NULL && b->num != NULL);
// Easy case.
- if (BC_NUM_ZERO(a)) {
+ if (BC_NUM_ZERO(a))
+ {
bc_num_setToZero(b, scale);
return;
}
// Another easy case.
- if (BC_NUM_ONE(a)) {
+ if (BC_NUM_ONE(a))
+ {
bc_num_one(b);
bc_num_extend(b, scale);
return;
@@ -3654,8 +3967,8 @@ void bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
// The code in this if statement calculates the initial estimate. First, if
// a is less than 0, then 0 is a good estimate. Otherwise, we want something
// in the same ballpark. That ballpark is pow.
- if (pow) {
-
+ if (pow)
+ {
// An odd number is served by starting with 2^((pow-1)/2), and an even
// number is served by starting with 6^((pow-2)/2). Why? Because math.
if (pow & 1) x0->num[0] = 2;
@@ -3671,8 +3984,8 @@ void bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
// This is the calculation loop. This compare goes to 0 eventually as the
// difference between the two numbers gets smaller than resscale.
- while (bc_num_cmp(x1, x0)) {
-
+ while (bc_num_cmp(x1, x0))
+ {
assert(BC_NUM_NONZERO(x0));
// This loop directly corresponds to the iteration in Newton's method.
@@ -3710,8 +4023,9 @@ err:
BC_LONGJMP_CONT;
}
-void bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
-
+void
+bc_num_divmod(BcNum* a, BcNum* b, BcNum* c, BcNum* d, size_t scale)
+{
size_t ts, len;
BcNum *ptr_a, num2;
bool init = false;
@@ -3727,8 +4041,9 @@ void bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
assert(c != d && a != d && b != d && b != c);
// Initialize or expand as necessary.
- if (c == a) {
-
+ if (c == a)
+ {
+ // NOLINTNEXTLINE
memcpy(&num2, c, sizeof(BcNum));
ptr_a = &num2;
@@ -3742,14 +4057,15 @@ void bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
BC_SIG_UNLOCK;
}
- else {
+ else
+ {
ptr_a = a;
bc_num_expand(c, len);
}
// Do the quick version if possible.
- if (BC_NUM_NONZERO(a) && !BC_NUM_RDX_VAL(a) &&
- !BC_NUM_RDX_VAL(b) && b->len == 1 && !scale)
+ if (BC_NUM_NONZERO(a) && !BC_NUM_RDX_VAL(a) && !BC_NUM_RDX_VAL(b) &&
+ b->len == 1 && !scale)
{
BcBigDig rem;
@@ -3774,15 +4090,17 @@ void bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
err:
// Only cleanup if we initialized.
- if (init) {
+ if (init)
+ {
BC_SIG_MAYLOCK;
bc_num_free(&num2);
BC_LONGJMP_CONT;
}
}
-void bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
-
+void
+bc_num_modexp(BcNum* a, BcNum* b, BcNum* c, BcNum* restrict d)
+{
BcNum base, exp, two, temp, atemp, btemp, ctemp;
BcDig two_digs[2];
@@ -3828,13 +4146,13 @@ void bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
// If you know the algorithm I used, the memory-efficient method, then this
// loop should be self-explanatory because it is the calculation loop.
- while (BC_NUM_NONZERO(&exp)) {
-
+ while (BC_NUM_NONZERO(&exp))
+ {
// Num two cannot be 0, so no errors.
bc_num_divmod(&exp, &two, &exp, &temp, 0);
- if (BC_NUM_ONE(&temp) && !BC_NUM_NEG_NP(temp)) {
-
+ if (BC_NUM_ONE(&temp) && !BC_NUM_NEG_NP(temp))
+ {
assert(BC_NUM_RDX_VALID(d));
assert(BC_NUM_RDX_VALID_NP(base));
@@ -3864,7 +4182,9 @@ err:
}
#if BC_DEBUG_CODE
-void bc_num_printDebug(const BcNum *n, const char *name, bool emptyline) {
+void
+bc_num_printDebug(const BcNum* n, const char* name, bool emptyline)
+{
bc_file_puts(&vm.fout, bc_flush_none, name);
bc_file_puts(&vm.fout, bc_flush_none, ": ");
bc_num_printDecimal(n, true);
@@ -3873,46 +4193,57 @@ void bc_num_printDebug(const BcNum *n, const char *name, bool emptyline) {
vm.nchars = 0;
}
-void bc_num_printDigs(const BcDig *n, size_t len, bool emptyline) {
-
+void
+bc_num_printDigs(const BcDig* n, size_t len, bool emptyline)
+{
size_t i;
for (i = len - 1; i < len; --i)
+ {
bc_file_printf(&vm.fout, " %lu", (unsigned long) n[i]);
+ }
bc_file_putchar(&vm.fout, bc_flush_err, '\n');
if (emptyline) bc_file_putchar(&vm.fout, bc_flush_err, '\n');
vm.nchars = 0;
}
-void bc_num_printWithDigs(const BcNum *n, const char *name, bool emptyline) {
+void
+bc_num_printWithDigs(const BcNum* n, const char* name, bool emptyline)
+{
bc_file_puts(&vm.fout, bc_flush_none, name);
- bc_file_printf(&vm.fout, " len: %zu, rdx: %zu, scale: %zu\n",
- name, n->len, BC_NUM_RDX_VAL(n), n->scale);
+ bc_file_printf(&vm.fout, " len: %zu, rdx: %zu, scale: %zu\n", name, n->len,
+ BC_NUM_RDX_VAL(n), n->scale);
bc_num_printDigs(n->num, n->len, emptyline);
}
-void bc_num_dump(const char *varname, const BcNum *n) {
-
+void
+bc_num_dump(const char* varname, const BcNum* n)
+{
ulong i, scale = n->scale;
bc_file_printf(&vm.ferr, "\n%s = %s", varname,
n->len ? (BC_NUM_NEG(n) ? "-" : "+") : "0 ");
- for (i = n->len - 1; i < n->len; --i) {
-
+ for (i = n->len - 1; i < n->len; --i)
+ {
if (i + 1 == BC_NUM_RDX_VAL(n))
+ {
bc_file_puts(&vm.ferr, bc_flush_none, ". ");
+ }
if (scale / BC_BASE_DIGS != BC_NUM_RDX_VAL(n) - i - 1)
+ {
bc_file_printf(&vm.ferr, "%lu ", (unsigned long) n->num[i]);
- else {
-
+ }
+ else
+ {
int mod = scale % BC_BASE_DIGS;
int d = BC_BASE_DIGS - mod;
BcDig div;
- if (mod != 0) {
+ if (mod != 0)
+ {
div = n->num[i] / ((BcDig) bc_num_pow10[(ulong) d]);
bc_file_printf(&vm.ferr, "%lu", (unsigned long) div);
}
@@ -3922,9 +4253,8 @@ void bc_num_dump(const char *varname, const BcNum *n) {
}
}
- bc_file_printf(&vm.ferr, "(%zu | %zu.%zu / %zu) %lu\n",
- n->scale, n->len, BC_NUM_RDX_VAL(n), n->cap,
- (unsigned long) (void*) n->num);
+ bc_file_printf(&vm.ferr, "(%zu | %zu.%zu / %zu) %lu\n", n->scale, n->len,
+ BC_NUM_RDX_VAL(n), n->cap, (unsigned long) (void*) n->num);
bc_file_flush(&vm.ferr, bc_flush_err);
}