factor/native/float.c

244 lines
4.5 KiB
C
Raw Normal View History

2004-08-05 16:49:55 -04:00
#include "factor.h"
2004-09-19 00:33:40 -04:00
double to_float(CELL tagged)
2004-08-05 16:49:55 -04:00
{
F_RATIO* r;
2004-09-19 00:33:40 -04:00
double x;
double y;
2004-08-27 02:09:24 -04:00
2005-01-17 15:33:12 -05:00
switch(TAG(tagged))
2004-08-05 16:49:55 -04:00
{
case FIXNUM_TYPE:
2004-09-19 00:33:40 -04:00
return (double)untag_fixnum_fast(tagged);
2004-08-05 16:49:55 -04:00
case BIGNUM_TYPE:
return s48_bignum_to_double((F_ARRAY*)UNTAG(tagged));
2004-08-05 16:49:55 -04:00
case RATIO_TYPE:
r = (F_RATIO*)UNTAG(tagged);
2004-09-19 00:33:40 -04:00
x = to_float(r->numerator);
y = to_float(r->denominator);
return x / y;
2004-08-05 16:49:55 -04:00
case FLOAT_TYPE:
return ((F_FLOAT*)UNTAG(tagged))->n;
2004-08-05 16:49:55 -04:00
default:
type_error(FLOAT_TYPE,tagged);
2004-09-19 00:33:40 -04:00
return 0.0; /* can't happen */
2004-08-05 16:49:55 -04:00
}
}
void primitive_to_float(void)
{
2005-06-16 18:50:49 -04:00
maybe_gc(sizeof(F_FLOAT));
drepl(tag_float(to_float(dpeek())));
2004-08-05 16:49:55 -04:00
}
2004-08-05 17:33:02 -04:00
void primitive_str_to_float(void)
{
F_STRING* str;
char *c_str, *end;
double f;
2005-06-16 18:50:49 -04:00
maybe_gc(sizeof(F_FLOAT));
str = untag_string(dpeek());
c_str = to_c_string(str,true);
end = c_str;
f = strtod(c_str,&end);
if(end != c_str + string_capacity(str))
2006-02-07 19:09:46 -05:00
general_error(ERROR_FLOAT_FORMAT,tag_object(str),true);
drepl(tag_float(f));
2004-08-05 17:33:02 -04:00
}
void primitive_float_to_str(void)
{
char tmp[33];
2005-06-16 18:50:49 -04:00
maybe_gc(sizeof(F_FLOAT));
2004-09-19 17:39:28 -04:00
snprintf(tmp,32,"%.16g",to_float(dpop()));
2004-08-05 17:33:02 -04:00
tmp[32] = '\0';
2004-09-19 17:39:28 -04:00
box_c_string(tmp);
2004-08-05 17:33:02 -04:00
}
#define GC_AND_POP_FLOATS(x,y) \
double x, y; \
2005-06-16 18:50:49 -04:00
maybe_gc(sizeof(F_FLOAT)); \
2004-12-18 20:24:46 -05:00
y = untag_float_fast(dpop()); \
x = untag_float_fast(dpop());
2004-09-19 00:33:40 -04:00
void primitive_float_add(void)
2004-08-05 16:49:55 -04:00
{
GC_AND_POP_FLOATS(x,y);
dpush(tag_float(x + y));
2004-08-05 16:49:55 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_float_subtract(void)
2004-08-05 16:49:55 -04:00
{
GC_AND_POP_FLOATS(x,y);
dpush(tag_float(x - y));
2004-08-05 16:49:55 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_float_multiply(void)
2004-08-05 16:49:55 -04:00
{
GC_AND_POP_FLOATS(x,y);
dpush(tag_float(x * y));
2004-08-05 16:49:55 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_float_divfloat(void)
2004-08-05 16:49:55 -04:00
{
GC_AND_POP_FLOATS(x,y);
dpush(tag_float(x / y));
2004-08-05 16:49:55 -04:00
}
void primitive_float_mod(void)
{
GC_AND_POP_FLOATS(x,y);
dpush(tag_float(fmod(x,y)));
}
2004-09-19 00:33:40 -04:00
void primitive_float_less(void)
2004-08-05 16:49:55 -04:00
{
2004-11-08 22:36:51 -05:00
GC_AND_POP_FLOATS(x,y);
box_boolean(x < y);
2004-08-05 16:49:55 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_float_lesseq(void)
2004-08-05 16:49:55 -04:00
{
2004-11-08 22:36:51 -05:00
GC_AND_POP_FLOATS(x,y);
box_boolean(x <= y);
2004-08-05 16:49:55 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_float_greater(void)
2004-08-05 16:49:55 -04:00
{
2004-11-08 22:36:51 -05:00
GC_AND_POP_FLOATS(x,y);
box_boolean(x > y);
2004-08-05 16:49:55 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_float_greatereq(void)
2004-08-05 16:49:55 -04:00
{
2004-11-08 22:36:51 -05:00
GC_AND_POP_FLOATS(x,y);
box_boolean(x >= y);
2004-08-05 16:49:55 -04:00
}
void primitive_facos(void)
{
2005-06-16 18:50:49 -04:00
maybe_gc(sizeof(F_FLOAT));
drepl(tag_float(acos(to_float(dpeek()))));
}
void primitive_fasin(void)
{
2005-06-16 18:50:49 -04:00
maybe_gc(sizeof(F_FLOAT));
drepl(tag_float(asin(to_float(dpeek()))));
}
void primitive_fatan(void)
{
2005-06-16 18:50:49 -04:00
maybe_gc(sizeof(F_FLOAT));
drepl(tag_float(atan(to_float(dpeek()))));
}
void primitive_fatan2(void)
{
2004-12-18 20:24:46 -05:00
double x, y;
2005-06-16 18:50:49 -04:00
maybe_gc(sizeof(F_FLOAT));
2004-12-18 20:24:46 -05:00
y = to_float(dpop());
x = to_float(dpop());
dpush(tag_float(atan2(x,y)));
}
void primitive_fcos(void)
{
2005-06-16 18:50:49 -04:00
maybe_gc(sizeof(F_FLOAT));
drepl(tag_float(cos(to_float(dpeek()))));
}
void primitive_fexp(void)
{
2005-06-16 18:50:49 -04:00
maybe_gc(sizeof(F_FLOAT));
drepl(tag_float(exp(to_float(dpeek()))));
}
void primitive_fcosh(void)
{
2005-06-16 18:50:49 -04:00
maybe_gc(sizeof(F_FLOAT));
drepl(tag_float(cosh(to_float(dpeek()))));
}
void primitive_flog(void)
{
2005-06-16 18:50:49 -04:00
maybe_gc(sizeof(F_FLOAT));
drepl(tag_float(log(to_float(dpeek()))));
}
void primitive_fpow(void)
{
2004-12-18 20:24:46 -05:00
double x, y;
2005-06-16 18:50:49 -04:00
maybe_gc(sizeof(F_FLOAT));
2004-12-18 20:24:46 -05:00
y = to_float(dpop());
x = to_float(dpop());
dpush(tag_float(pow(x,y)));
}
void primitive_fsin(void)
{
2005-06-16 18:50:49 -04:00
maybe_gc(sizeof(F_FLOAT));
drepl(tag_float(sin(to_float(dpeek()))));
}
void primitive_fsinh(void)
{
2005-06-16 18:50:49 -04:00
maybe_gc(sizeof(F_FLOAT));
drepl(tag_float(sinh(to_float(dpeek()))));
}
void primitive_fsqrt(void)
{
2005-06-16 18:50:49 -04:00
maybe_gc(sizeof(F_FLOAT));
drepl(tag_float(sqrt(to_float(dpeek()))));
}
void primitive_float_bits(void)
{
FLOAT_BITS b;
b.x = (float)to_float(dpeek());
drepl(tag_cell(b.y));
}
2005-06-12 20:55:30 -04:00
void primitive_bits_float(void)
{
FLOAT_BITS b;
b.y = unbox_unsigned_4();
dpush(tag_float(b.x));
2005-06-12 20:55:30 -04:00
}
void primitive_double_bits(void)
{
DOUBLE_BITS b;
b.x = to_float(dpop());
box_unsigned_8(b.y);
2005-06-12 20:55:30 -04:00
}
void primitive_bits_double(void)
{
DOUBLE_BITS b;
b.y = unbox_unsigned_8();
dpush(tag_float(b.x));
}
#define DEFBOX(name,type) \
void name (type flo) \
{ \
dpush(tag_float(flo)); \
}
#define DEFUNBOX(name,type) \
type name(void) \
{ \
return to_float(dpop()); \
}
DEFBOX(box_float,float)
DEFUNBOX(unbox_float,float)
DEFBOX(box_double,double)
DEFUNBOX(unbox_double,double)