VM: CELL_TO_FOO macro-magic in the same spirit as BIGNUM_TO_FOO
Now also factor_vm::to_cell doesn't piggyback on factor_vm::to_fixnum's (strict) conversion.db4
parent
c3f9c0a9a3
commit
d225bec128
74
vm/math.cpp
74
vm/math.cpp
|
@ -277,28 +277,28 @@ void factor_vm::primitive_bits_double() {
|
||||||
ctx->push(allot_float(bits_double(to_unsigned_8(ctx->pop()))));
|
ctx->push(allot_float(bits_double(to_unsigned_8(ctx->pop()))));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Cannot allocate */
|
/* Cannot allocate. */
|
||||||
fixnum factor_vm::to_fixnum(cell tagged) {
|
#define CELL_TO_FOO(name, type, converter) \
|
||||||
switch (TAG(tagged)) {
|
type factor_vm::name(cell tagged) { \
|
||||||
case FIXNUM_TYPE:
|
switch (TAG(tagged)) { \
|
||||||
return untag_fixnum(tagged);
|
case FIXNUM_TYPE: \
|
||||||
case BIGNUM_TYPE:
|
return (type)untag_fixnum(tagged); \
|
||||||
return bignum_to_fixnum_strict(untag<bignum>(tagged));
|
case BIGNUM_TYPE: \
|
||||||
default:
|
return converter(untag<bignum>(tagged)); \
|
||||||
type_error(FIXNUM_TYPE, tagged);
|
default: \
|
||||||
return 0; /* can't happen */
|
type_error(FIXNUM_TYPE, tagged); \
|
||||||
|
return 0; /* can't happen */ \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
VM_C_API type name(cell tagged, factor_vm* parent) { \
|
||||||
|
return parent->name(tagged); \
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
VM_C_API fixnum to_fixnum(cell tagged, factor_vm* parent) {
|
/* Note that to_fixnum, unlike the others, is strict. */
|
||||||
return parent->to_fixnum(tagged);
|
CELL_TO_FOO(to_fixnum, fixnum, bignum_to_fixnum_strict)
|
||||||
}
|
CELL_TO_FOO(to_cell, cell, bignum_to_cell)
|
||||||
|
CELL_TO_FOO(to_signed_8, int64_t, bignum_to_long_long)
|
||||||
cell factor_vm::to_cell(cell tagged) { return (cell)to_fixnum(tagged); }
|
CELL_TO_FOO(to_unsigned_8, uint64_t, bignum_to_ulong_long)
|
||||||
|
|
||||||
VM_C_API cell to_cell(cell tagged, factor_vm* parent) {
|
|
||||||
return parent->to_cell(tagged);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Allocates memory */
|
/* Allocates memory */
|
||||||
VM_C_API cell from_signed_cell(fixnum integer, factor_vm* parent) {
|
VM_C_API cell from_signed_cell(fixnum integer, factor_vm* parent) {
|
||||||
|
@ -322,23 +322,6 @@ VM_C_API cell from_signed_8(int64_t n, factor_vm* parent) {
|
||||||
return parent->from_signed_8(n);
|
return parent->from_signed_8(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Cannot allocate */
|
|
||||||
int64_t factor_vm::to_signed_8(cell obj) {
|
|
||||||
switch (tagged<object>(obj).type()) {
|
|
||||||
case FIXNUM_TYPE:
|
|
||||||
return untag_fixnum(obj);
|
|
||||||
case BIGNUM_TYPE:
|
|
||||||
return bignum_to_long_long(untag<bignum>(obj));
|
|
||||||
default:
|
|
||||||
type_error(BIGNUM_TYPE, obj);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
VM_C_API int64_t to_signed_8(cell obj, factor_vm* parent) {
|
|
||||||
return parent->to_signed_8(obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Allocates memory */
|
/* Allocates memory */
|
||||||
cell factor_vm::from_unsigned_8(uint64_t n) {
|
cell factor_vm::from_unsigned_8(uint64_t n) {
|
||||||
if (n > (uint64_t)fixnum_max)
|
if (n > (uint64_t)fixnum_max)
|
||||||
|
@ -351,23 +334,6 @@ VM_C_API cell from_unsigned_8(uint64_t n, factor_vm* parent) {
|
||||||
return parent->from_unsigned_8(n);
|
return parent->from_unsigned_8(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Cannot allocate */
|
|
||||||
uint64_t factor_vm::to_unsigned_8(cell obj) {
|
|
||||||
switch (tagged<object>(obj).type()) {
|
|
||||||
case FIXNUM_TYPE:
|
|
||||||
return untag_fixnum(obj);
|
|
||||||
case BIGNUM_TYPE:
|
|
||||||
return bignum_to_ulong_long(untag<bignum>(obj));
|
|
||||||
default:
|
|
||||||
type_error(BIGNUM_TYPE, obj);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
VM_C_API uint64_t to_unsigned_8(cell obj, factor_vm* parent) {
|
|
||||||
return parent->to_unsigned_8(obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Cannot allocate */
|
/* Cannot allocate */
|
||||||
float factor_vm::to_float(cell value) {
|
float factor_vm::to_float(cell value) {
|
||||||
return (float)untag_float_check(value);
|
return (float)untag_float_check(value);
|
||||||
|
|
Loading…
Reference in New Issue