Working version of str2bignum

This commit is contained in:
Justin Ethier 2022-06-14 22:32:05 -04:00
parent e2d903e8dc
commit 96ca2263af
3 changed files with 20 additions and 13 deletions

View file

@ -1608,7 +1608,7 @@ void Cyc_int2bignum(int n, mp_int *bn);
object Cyc_int2bignum2(gc_thread_data *data, int n);
// TODO: debug only, remove this function from here!
string_type *bignum2string(void *data, bignum2_type *bn, int base);
void _str_to_bignum(void *data, char *str, *char *str_end, int radix);
object _str_to_bignum(void *data, char *str, char *str_end, int radix);
object bignum2_plus_unsigned(void *data, bignum2_type *x, bignum2_type *y, int negp);
/* Remaining GC prototypes that require objects to be defined */

View file

@ -2948,16 +2948,6 @@ inline static int hex_char_to_digit(int ch)
else return ch - (int)'0'; /* decimal (OR INVALID; handled elsewhere) */
}
void _str_to_bignum(void *data, char *str, *char *str_end, int radix)
{
TODO:
need to figure out sign, size (using nlz), what else??
bignum2_type *bn = gc_alloc_bignum2(data, 2);
bn->num_digits = 2;
bn->sign = 0;
str_to_bignum(data, bn, str, str_end, radix);
}
/* Write from digit character stream to bignum. Bignum does not need
* to be initialised. Returns the bignum, or a fixnum. Assumes the
* string contains only digits that fit within radix (checked by
@ -3022,6 +3012,21 @@ static object str_to_bignum(void *data, object bignum, char *str, char *str_end,
return C_bignum_simplify(bignum);
}
object _str_to_bignum(void *data, char *str, char *str_end, int radix)
{
int negp = 0;
size_t nbits;
uint32_t size;
if (*str == '+' || *str == '-') {
negp = ((*str++) == '-') ? 1 : 0;
}
nbits = (str_end - str) * nlz(radix - 1);
size = C_BIGNUM_BITS_TO_DIGITS(nbits);
bignum2_type *bn = gc_alloc_bignum2(data, size);
bn->sign = negp;
return str_to_bignum(data, bn, str, str_end, radix);
}
// TODO: static
//object bignum_plus_unsigned(C_word **ptr, C_word x, C_word y, C_word negp)
object bignum2_plus_unsigned(void *data, bignum2_type *x, bignum2_type *y, int negp)

View file

@ -17,8 +17,7 @@
"(void *data, int argc, closure _, object k, object str, object radix)"
" int len = string_len(str);
char *s = string_str(str);
bignum2_type *bn = gc_alloc_bignum2(data, len);
bn = str_to_bignum(data, bn, s, s + len, obj_obj2int(radix));
object bn = _str_to_bignum(data, s, s + len, obj_obj2int(radix));
return_closcall1(data, k, bn); ")
(define-c test-plus
@ -55,6 +54,9 @@ if(is_value_type(result)) {
(write (test-str2bn "123454354534523454243999" 10))
(newline)
(write (test-str2bn "-123454354534523454243999" 10))
(newline)
(write (test-str2bn "123454354534523454243999" 16))
(newline)