Convert fixnum to bignum

This commit is contained in:
Justin Ethier 2022-06-01 22:50:48 -04:00
parent 22c4f0277a
commit 165da29c49
3 changed files with 25 additions and 4 deletions

7
gc.c
View file

@ -1322,16 +1322,17 @@ hrt_log_delta("gc sweep fixed size", tstamp);
* @param data The mutator's thread data object
* @return Pointer to a heap object for the bignum
*/
void *gc_alloc_bignum2(gc_thread_data *data)
void *gc_alloc_bignum2(gc_thread_data *data, uint32_t num_digits)
{
int heap_grown, result;
int heap_grown;
bignum2_type *bn;
bignum2_type tmp;
tmp.num_digits = num_digits;
// No need to do this since tmp is always local
//tmp.hdr.mark = gc_color_red;
//tmp.hdr.grayed = 0;
tmp.tag = bignum2_tag;
bn = gc_alloc(((gc_thread_data *)data)->heap, sizeof(bignum2_type), (char *)(&tmp), (gc_thread_data *)data, &heap_grown);
bn = gc_alloc(((gc_thread_data *)data)->heap, sizeof(bignum2_type) + (num_digits * sizeof(uint32_t)), (char *)(&tmp), (gc_thread_data *)data, &heap_grown);
return bn;
}

View file

@ -415,7 +415,7 @@ void *gc_try_alloc_slow(gc_heap *h_passed, gc_heap *h, size_t size, char *obj, g
void *gc_alloc(gc_heap_root * h, size_t size, char *obj, gc_thread_data * thd,
int *heap_grown);
void *gc_alloc_bignum(gc_thread_data *data);
void *gc_alloc_bignum2(gc_thread_data *data);
void *gc_alloc_bignum2(gc_thread_data *data, uint32_t num_digits);
size_t gc_allocated_bytes(object obj, gc_free_list * q, gc_free_list * r);
gc_heap *gc_heap_last(gc_heap * h);
@ -1555,6 +1555,7 @@ void vpbuffer_free(void **buf);
/* Bignum utility functions */
int Cyc_bignum_cmp(bn_cmp_type type, object x, int tx, object y, int ty);
void Cyc_int2bignum(int n, mp_int *bn);
object Cyc_int2bignum2(gc_thread_data *data, int n);
/* Remaining GC prototypes that require objects to be defined */
void *gc_alloc_from_bignum(gc_thread_data *data, bignum_type *src);

View file

@ -1770,6 +1770,25 @@ void Cyc_int2bignum(int n, mp_int *bn)
}
}
object Cyc_bignum2(bignum2_type *bn, int sign, int n)
{
uint32_t *p = &(bn->sign);
*(p++) = 1;
*(p++) = sign;
*(p++) = n;
return bn;
}
object Cyc_int2bignum2(gc_thread_data *data, int n)
{
bignum2_type *bn = gc_alloc_bignum2(data, n);
if (n < 0) {
return Cyc_bignum2(bn, 1, -n);
} else {
return Cyc_bignum2(bn, 0, n);
}
}
int Cyc_bignum_cmp(bn_cmp_type type, object x, int tx, object y, int ty)
{
mp_int tmp;