mirror of
https://github.com/janet-lang/janet
synced 2024-11-16 21:54:48 +00:00
Add with-syms and combine bignat_add and bignatr mul
into a single operation for strtod.c
This commit is contained in:
parent
0d4ab7dee0
commit
618f8d6818
@ -652,6 +652,11 @@
|
|||||||
(array/concat res (f x)))
|
(array/concat res (f x)))
|
||||||
res)
|
res)
|
||||||
|
|
||||||
|
(defmacro with-syms
|
||||||
|
"Evaluates body with each symbol in syms bound to a generated, unique symbol."
|
||||||
|
[syms & body]
|
||||||
|
~(let ,(mapcat (fn [s] @[s (tuple gensym)]) syms) ,;body))
|
||||||
|
|
||||||
(defn filter
|
(defn filter
|
||||||
"Given a predicate, take only elements from an array or tuple for
|
"Given a predicate, take only elements from an array or tuple for
|
||||||
which (pred element) is truthy. Returns a new array."
|
which (pred element) is truthy. Returns a new array."
|
||||||
|
@ -103,33 +103,11 @@ static void bignat_append(struct BigNat *mant, uint32_t dig) {
|
|||||||
bignat_extra(mant, 1)[0] = dig;
|
bignat_extra(mant, 1)[0] = dig;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add term to mant */
|
/* Multiply the mantissa mant by a factor and the add a term
|
||||||
static void bignat_add(struct BigNat *mant, uint32_t dig) {
|
* in one operation. Both factor and term will be between 2 and 36. */
|
||||||
|
static void bignat_muladd(struct BigNat *mant, uint32_t factor, uint32_t term) {
|
||||||
int32_t i;
|
int32_t i;
|
||||||
int carry = 0;
|
uint64_t carry = ((uint64_t) mant->first_digit) * factor + term;
|
||||||
uint32_t next = mant->first_digit + dig;
|
|
||||||
if (next >= BIGNAT_BASE) {
|
|
||||||
next -= BIGNAT_BASE;
|
|
||||||
carry = 1;
|
|
||||||
}
|
|
||||||
mant->first_digit = next;
|
|
||||||
for (i = 0; i < mant->n; i++) {
|
|
||||||
if (!carry) return;
|
|
||||||
uint32_t next = mant->digits[i] + 1;
|
|
||||||
if (next >= BIGNAT_BASE) {
|
|
||||||
next = 0;
|
|
||||||
} else {
|
|
||||||
carry = 0;
|
|
||||||
}
|
|
||||||
mant->digits[i] = next;
|
|
||||||
}
|
|
||||||
if (carry) bignat_append(mant, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Multiply the mantissa mant by a factor */
|
|
||||||
static void bignat_mul(struct BigNat *mant, uint32_t factor) {
|
|
||||||
int32_t i;
|
|
||||||
uint64_t carry = ((uint64_t) mant->first_digit) * factor;
|
|
||||||
mant->first_digit = carry % BIGNAT_BASE;
|
mant->first_digit = carry % BIGNAT_BASE;
|
||||||
carry /= BIGNAT_BASE;
|
carry /= BIGNAT_BASE;
|
||||||
for (i = 0; i < mant->n; i++) {
|
for (i = 0; i < mant->n; i++) {
|
||||||
@ -238,7 +216,7 @@ static double convert(
|
|||||||
|
|
||||||
/* Positive exponents are simple */
|
/* Positive exponents are simple */
|
||||||
while (exponent > 0) {
|
while (exponent > 0) {
|
||||||
bignat_mul(mant, base);
|
bignat_muladd(mant, base, 0);
|
||||||
exponent--;
|
exponent--;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -331,8 +309,7 @@ double janet_scan_number(
|
|||||||
int digit = digit_lookup[*str & 0x7F];
|
int digit = digit_lookup[*str & 0x7F];
|
||||||
if (*str > 127 || digit >= base) goto error;
|
if (*str > 127 || digit >= base) goto error;
|
||||||
if (seenpoint) ex--;
|
if (seenpoint) ex--;
|
||||||
bignat_mul(&mant, base);
|
bignat_muladd(&mant, base, digit);
|
||||||
bignat_add(&mant, digit);
|
|
||||||
seenadigit = 1;
|
seenadigit = 1;
|
||||||
}
|
}
|
||||||
str++;
|
str++;
|
||||||
|
Loading…
Reference in New Issue
Block a user