diff --git a/contrib/10.math/math.c b/contrib/10.math/math.c index 4609d6ed..f2b9d7f5 100644 --- a/contrib/10.math/math.c +++ b/contrib/10.math/math.c @@ -1,5 +1,7 @@ #include "picrin.h" +#include + static pic_value pic_number_floor2(pic_state *pic) { diff --git a/extlib/benz/file.c b/extlib/benz/file.c index 96f25c74..3a4f6228 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -345,15 +345,14 @@ int xvfprintf(pic_state *pic, xFILE *stream, const char *fmt, va_list ap) { ival = va_arg(ap, int); cnt += print_int(pic, stream, ival, 10); break; -#if PIC_ENABLE_FLOAT -# if PIC_ENABLE_LIBC +#if PIC_ENABLE_LIBC case 'f': { char buf[100]; sprintf(buf, "%g", va_arg(ap, double)); xfputs(pic, buf, stream); break; } -# else +#else case 'f': { double dval, dint; dval = modf(va_arg(ap, double), &dint); @@ -376,7 +375,6 @@ int xvfprintf(pic_state *pic, xFILE *stream, const char *fmt, va_list ap) { } break; } -# endif #endif case 's': sval = va_arg(ap, char*); diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 5285f7ab..9ac40c27 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -439,9 +439,7 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) } case PIC_TT_NIL: case PIC_TT_BOOL: -#if PIC_ENABLE_FLOAT case PIC_TT_FLOAT: -#endif case PIC_TT_INT: case PIC_TT_CHAR: case PIC_TT_EOF: @@ -679,9 +677,7 @@ gc_finalize_object(pic_state *pic, struct pic_object *obj) } case PIC_TT_NIL: case PIC_TT_BOOL: -#if PIC_ENABLE_FLOAT case PIC_TT_FLOAT: -#endif case PIC_TT_INT: case PIC_TT_CHAR: case PIC_TT_EOF: diff --git a/extlib/benz/include/picrin/compat.h b/extlib/benz/include/picrin/compat.h index e6abc5dd..36c9981a 100644 --- a/extlib/benz/include/picrin/compat.h +++ b/extlib/benz/include/picrin/compat.h @@ -205,10 +205,6 @@ strcpy(char *dst, const char *src) #endif -#if PIC_ENABLE_FLOAT -# include -#endif - #if PIC_ENABLE_STDIO # include #endif diff --git a/extlib/benz/include/picrin/config.h b/extlib/benz/include/picrin/config.h index 16d872c2..e90684d0 100644 --- a/extlib/benz/include/picrin/config.h +++ b/extlib/benz/include/picrin/config.h @@ -11,9 +11,6 @@ /** enable word boxing */ /* #define PIC_WORD_BOXING 0 */ -/** enable floating point number support */ -/* #define PIC_ENABLE_FLOAT 1 */ - /** no dependency on libc */ /* #define PIC_ENABLE_LIBC 1 */ @@ -68,10 +65,6 @@ # error cannot enable both PIC_NAN_BOXING and PIC_WORD_BOXING simultaneously #endif -#if PIC_WORD_BOXING && PIC_ENABLE_FLOAT -# error cannot enable both PIC_WORD_BOXING and PIC_ENABLE_FLOAT simultaneously -#endif - #ifndef PIC_WORD_BOXING # define PIC_WORD_BOXING 0 #endif @@ -84,20 +77,10 @@ # endif #endif -#ifndef PIC_ENABLE_FLOAT -# if ! PIC_WORD_BOXING -# define PIC_ENABLE_FLOAT 1 -# endif -#endif - #ifndef PIC_ENABLE_LIBC # define PIC_ENABLE_LIBC 1 #endif -#if PIC_NAN_BOXING && defined(PIC_ENABLE_FLOAT) && ! PIC_ENABLE_FLOAT -# error cannot disable float support when nan boxing is on -#endif - #ifndef PIC_ENABLE_STDIO # define PIC_ENABLE_STDIO 1 #endif diff --git a/extlib/benz/include/picrin/type.h b/extlib/benz/include/picrin/type.h index e6626960..535421f5 100644 --- a/extlib/benz/include/picrin/type.h +++ b/extlib/benz/include/picrin/type.h @@ -20,9 +20,7 @@ enum pic_vtype { PIC_VTYPE_FALSE, PIC_VTYPE_UNDEF, PIC_VTYPE_INVALID, -#if PIC_ENABLE_FLOAT PIC_VTYPE_FLOAT, -#endif PIC_VTYPE_INT, PIC_VTYPE_CHAR, PIC_VTYPE_EOF, @@ -116,9 +114,7 @@ typedef struct { enum pic_vtype type; union { void *data; -#if PIC_ENABLE_FLOAT double f; -#endif int i; char c; } u; @@ -128,9 +124,7 @@ typedef struct { #define pic_vtype(v) ((v).type) #define pic_init_value(v,vtype) ((v).type = (vtype), (v).u.data = NULL) -#if PIC_ENABLE_FLOAT -# define pic_float(v) ((v).u.f) -#endif +#define pic_float(v) ((v).u.f) #define pic_int(v) ((v).u.i) #define pic_char(v) ((v).u.c) @@ -140,9 +134,7 @@ enum pic_tt { /* immediate */ PIC_TT_NIL, PIC_TT_BOOL, -#if PIC_ENABLE_FLOAT PIC_TT_FLOAT, -#endif PIC_TT_INT, PIC_TT_CHAR, PIC_TT_EOF, @@ -218,21 +210,12 @@ PIC_INLINE const char *pic_type_repr(enum pic_tt); pic_errorf(pic, "expected " #type ", but got ~s", v); \ } -#if PIC_ENABLE_FLOAT PIC_INLINE bool pic_valid_int(double v) { return INT_MIN <= v && v <= INT_MAX; } -#else -PIC_INLINE bool -pic_valid_int(int PIC_UNUSED(v)) -{ - return true; -} -#endif - PIC_INLINE pic_value pic_nil_value(); PIC_INLINE pic_value pic_true_value(); PIC_INLINE pic_value pic_false_value(); @@ -240,9 +223,7 @@ PIC_INLINE pic_value pic_bool_value(bool); PIC_INLINE pic_value pic_undef_value(); PIC_INLINE pic_value pic_invalid_value(); PIC_INLINE pic_value pic_obj_value(void *); -#if PIC_ENABLE_FLOAT PIC_INLINE pic_value pic_float_value(double); -#endif PIC_INLINE pic_value pic_int_value(int); PIC_INLINE pic_value pic_size_value(size_t); PIC_INLINE pic_value pic_char_value(char c); @@ -264,10 +245,8 @@ pic_type(pic_value v) return PIC_TT_UNDEF; case PIC_VTYPE_INVALID: return PIC_TT_INVALID; -#if PIC_ENABLE_FLOAT case PIC_VTYPE_FLOAT: return PIC_TT_FLOAT; -#endif case PIC_VTYPE_INT: return PIC_TT_INT; case PIC_VTYPE_CHAR: @@ -289,10 +268,8 @@ pic_type_repr(enum pic_tt tt) return "nil"; case PIC_TT_BOOL: return "boolean"; -#if PIC_ENABLE_FLOAT case PIC_TT_FLOAT: return "float"; -#endif case PIC_TT_INT: return "int"; case PIC_TT_SYMBOL: @@ -382,13 +359,11 @@ pic_bool_value(bool b) PIC_INLINE pic_value pic_size_value(size_t s) { -#if PIC_ENABLE_FLOAT if (sizeof(unsigned) < sizeof(size_t)) { if (s > (size_t)INT_MAX) { return pic_float_value(s); } } -#endif return pic_int_value((int)s); } @@ -472,8 +447,6 @@ pic_obj_value(void *ptr) return v; } -#if PIC_ENABLE_FLOAT - PIC_INLINE pic_value pic_float_value(double f) { @@ -484,8 +457,6 @@ pic_float_value(double f) return v; } -#endif - PIC_INLINE pic_value pic_int_value(int i) { @@ -569,10 +540,8 @@ pic_eqv_p(pic_value x, pic_value y) return true; case PIC_TT_BOOL: return pic_vtype(x) == pic_vtype(y); -#if PIC_ENABLE_FLOAT case PIC_TT_FLOAT: return pic_float(x) == pic_float(y); -#endif case PIC_TT_INT: return pic_int(x) == pic_int(y); default: @@ -582,82 +551,52 @@ pic_eqv_p(pic_value x, pic_value y) #endif -#if PIC_ENABLE_FLOAT -# define pic_define_aop(name, op, guard) \ - PIC_INLINE pic_value \ - name(pic_state *pic, pic_value a, pic_value b) \ - { \ - extern PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); \ - double f; \ - if (pic_int_p(a) && pic_int_p(b)) { \ - f = (double)pic_int(a) op (double)pic_int(b); \ - return (INT_MIN <= f && f <= INT_MAX && guard) \ - ? pic_int_value((int)f) \ - : pic_float_value(f); \ - } else if (pic_float_p(a) && pic_float_p(b)) { \ - return pic_float_value(pic_float(a) op pic_float(b)); \ - } else if (pic_int_p(a) && pic_float_p(b)) { \ - return pic_float_value(pic_int(a) op pic_float(b)); \ - } else if (pic_float_p(a) && pic_int_p(b)) { \ - return pic_float_value(pic_float(a) op pic_int(b)); \ - } else { \ - pic_errorf(pic, #name ": non-number operand given"); \ - } \ - PIC_UNREACHABLE(); \ +#define pic_define_aop(name, op, guard) \ + PIC_INLINE pic_value \ + name(pic_state *pic, pic_value a, pic_value b) \ + { \ + PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); \ + double f; \ + if (pic_int_p(a) && pic_int_p(b)) { \ + f = (double)pic_int(a) op (double)pic_int(b); \ + return (INT_MIN <= f && f <= INT_MAX && guard) \ + ? pic_int_value((int)f) \ + : pic_float_value(f); \ + } else if (pic_float_p(a) && pic_float_p(b)) { \ + return pic_float_value(pic_float(a) op pic_float(b)); \ + } else if (pic_int_p(a) && pic_float_p(b)) { \ + return pic_float_value(pic_int(a) op pic_float(b)); \ + } else if (pic_float_p(a) && pic_int_p(b)) { \ + return pic_float_value(pic_float(a) op pic_int(b)); \ + } else { \ + pic_errorf(pic, #name ": non-number operand given"); \ + } \ + PIC_UNREACHABLE(); \ } -#else -# define pic_define_aop(name, op, guard) \ - PIC_INLINE pic_value \ - name(pic_state *pic, pic_value a, pic_value b) \ - { \ - extern PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); \ - if (pic_int_p(a) && pic_int_p(b)) { \ - return pic_int_value(pic_int(a) op pic_int(b)); \ - } else { \ - pic_errorf(pic, #name ": non-number operand given"); \ - } \ - PIC_UNREACHABLE(); \ - } -#endif pic_define_aop(pic_add, +, true) pic_define_aop(pic_sub, -, true) pic_define_aop(pic_mul, *, true) -pic_define_aop(pic_div, /, f == round(f)) +pic_define_aop(pic_div, /, f == (int)f) -#if PIC_ENABLE_FLOAT -# define pic_define_cmp(name, op) \ - PIC_INLINE bool \ - name(pic_state *pic, pic_value a, pic_value b) \ - { \ - extern PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); \ - if (pic_int_p(a) && pic_int_p(b)) { \ - return pic_int(a) op pic_int(b); \ - } else if (pic_float_p(a) && pic_float_p(b)) { \ - return pic_float(a) op pic_float(b); \ - } else if (pic_int_p(a) && pic_float_p(b)) { \ - return pic_int(a) op pic_float(b); \ - } else if (pic_float_p(a) && pic_int_p(b)) { \ - return pic_float(a) op pic_int(b); \ - } else { \ - pic_errorf(pic, #name ": non-number operand given"); \ - } \ - PIC_UNREACHABLE(); \ +#define pic_define_cmp(name, op) \ + PIC_INLINE bool \ + name(pic_state *pic, pic_value a, pic_value b) \ + { \ + PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); \ + if (pic_int_p(a) && pic_int_p(b)) { \ + return pic_int(a) op pic_int(b); \ + } else if (pic_float_p(a) && pic_float_p(b)) { \ + return pic_float(a) op pic_float(b); \ + } else if (pic_int_p(a) && pic_float_p(b)) { \ + return pic_int(a) op pic_float(b); \ + } else if (pic_float_p(a) && pic_int_p(b)) { \ + return pic_float(a) op pic_int(b); \ + } else { \ + pic_errorf(pic, #name ": non-number operand given"); \ + } \ + PIC_UNREACHABLE(); \ } -#else -# define pic_define_cmp(name, op) \ - PIC_INLINE bool \ - name(pic_state *pic, pic_value a, pic_value b) \ - { \ - extern PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); \ - if (pic_int_p(a) && pic_int_p(b)) { \ - return pic_int(a) op pic_int(b); \ - } else { \ - pic_errorf(pic, #name ": non-number operand given"); \ - } \ - PIC_UNREACHABLE(); \ - } -#endif pic_define_cmp(pic_eq, ==) pic_define_cmp(pic_lt, <) diff --git a/extlib/benz/number.c b/extlib/benz/number.c index 464882ef..bd723f9f 100644 --- a/extlib/benz/number.c +++ b/extlib/benz/number.c @@ -11,11 +11,7 @@ pic_number_number_p(pic_state *pic) pic_get_args(pic, "o", &v); -#if PIC_ENABLE_FLOAT return pic_bool_value(pic_float_p(v) || pic_int_p(v)); -#else - return pic_bool_value(pic_int_p(v)); -#endif } static pic_value @@ -35,11 +31,7 @@ pic_number_inexact_p(pic_state *pic) pic_get_args(pic, "o", &v); -#if PIC_ENABLE_FLOAT return pic_bool_value(pic_float_p(v)); -#else - return pic_false_value(); -#endif } static pic_value @@ -59,7 +51,7 @@ pic_number_exact(pic_state *pic) pic_get_args(pic, "f", &f); - return pic_int_value((int)(round(f))); + return pic_int_value((int)f); } #define DEFINE_CMP(op) \ @@ -181,7 +173,6 @@ number_string(int val, int radix, int length, char *buffer) { static pic_value pic_number_number_to_string(pic_state *pic) { -#if PIC_ENABLE_FLOAT double f; bool e; int radix = 10; @@ -216,41 +207,11 @@ pic_number_number_to_string(pic_state *pic) } return pic_obj_value(str); -#else - int f; - bool e; - int radix = 10; - pic_str *str; - size_t s; - char *buf; - int ival, ilen; - - pic_get_args(pic, "i|i", &f, &e, &radix); - - if (radix < 2 || radix > 36) { - pic_errorf(pic, "number->string: invalid radix %d (between 2 and 36, inclusive)", radix); - } - - ival = f; - ilen = number_string_length(ival, radix); - s = ilen + 1; - - buf = pic_malloc(pic, s); - - number_string(ival, radix, ilen, buf); - - str = pic_make_str(pic, buf, s - 1); - - pic_free(pic, buf); - - return pic_obj_value(str); -#endif } static pic_value pic_number_string_to_number(pic_state *pic) { -#if PIC_ENABLE_FLOAT const char *str; int radix = 10; long num; @@ -272,21 +233,6 @@ pic_number_string_to_number(pic_state *pic) } pic_errorf(pic, "invalid string given: %s", str); -#else - const char *str; - int radix = 10; - long num; - char *eptr; - - pic_get_args(pic, "z|i", &str, &radix); - - num = strtol(str, &eptr, radix); - if (*eptr == '\0') { - return pic_int_value(num); - } - - pic_errorf(pic, "invalid string given: %s", str); -#endif } void diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 3c13fd03..926204b2 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -64,7 +64,6 @@ isdelim(int c) return c == EOF || strchr("();,|\" \t\n\r", c) != NULL; /* ignores "#", "'" */ } -#if PIC_ENABLE_FLOAT static bool strcaseeq(const char *s1, const char *s2) { @@ -76,7 +75,6 @@ strcaseeq(const char *s1, const char *s2) } return a == b; } -#endif static int case_fold(pic_state *pic, int c) @@ -271,8 +269,7 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c) u = read_uinteger(pic, port, c); switch (peek(pic, port)) { -#if PIC_ENABLE_FLOAT -# if PIC_ENABLE_LIBC +#if PIC_ENABLE_LIBC case '.': { char buf[256]; i = sprintf(buf, "%d", u); @@ -283,7 +280,7 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c) sprintf(buf + i, "e%d", read_suffix(pic, port)); return pic_float_value(atof(buf)); } -# else +#else case '.': { double f, g; next(pic, port); @@ -312,7 +309,6 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c) } return pic_float_value(f); } -# endif #endif default: @@ -346,15 +342,11 @@ read_number(pic_state *pic, struct pic_port *port, int c) static pic_value negate(pic_value n) { -#if PIC_ENABLE_FLOAT if (pic_int_p(n)) { return pic_int_value(-pic_int(n)); } else { return pic_float_value(-pic_float(n)); } -#else - return pic_int_value(-pic_int(n)); -#endif } static pic_value @@ -367,14 +359,12 @@ read_minus(pic_state *pic, struct pic_port *port, int c) } else { sym = read_symbol(pic, port, c); -#if PIC_ENABLE_FLOAT if (strcaseeq(pic_symbol_name(pic, pic_sym_ptr(sym)), "-inf.0")) { - return pic_float_value(-INFINITY); + return pic_float_value(-(1.0 / 0.0)); } if (strcaseeq(pic_symbol_name(pic, pic_sym_ptr(sym)), "-nan.0")) { - return pic_float_value(-NAN); + return pic_float_value(-(0.0 / 0.0)); } -#endif return sym; } } @@ -389,14 +379,12 @@ read_plus(pic_state *pic, struct pic_port *port, int c) } else { sym = read_symbol(pic, port, c); -#if PIC_ENABLE_FLOAT if (strcaseeq(pic_symbol_name(pic, pic_sym_ptr(sym)), "+inf.0")) { - return pic_float_value(INFINITY); + return pic_float_value(1.0 / 0.0); } if (strcaseeq(pic_symbol_name(pic, pic_sym_ptr(sym)), "+nan.0")) { - return pic_float_value(NAN); + return pic_float_value(0.0 / 0.0); } -#endif return sym; } } diff --git a/extlib/benz/state.c b/extlib/benz/state.c index bb587966..7fcb51bf 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -48,7 +48,7 @@ pic_init_features(pic_state *pic) { pic_add_feature(pic, "picrin"); -#if PIC_ENABLE_FLOAT +#if __STDC_IEC_559__ pic_add_feature(pic, "ieee-float"); #endif diff --git a/extlib/benz/string.c b/extlib/benz/string.c index f94dfd50..ac1b2449 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -316,11 +316,9 @@ pic_xvfformat(pic_state *pic, xFILE *file, const char *fmt, va_list ap) case 'p': xfprintf(pic, file, "%p", va_arg(ap, void *)); break; -#if PIC_ENABLE_FLOAT case 'f': xfprintf(pic, file, "%f", va_arg(ap, double)); break; -#endif } break; case '~': diff --git a/extlib/benz/vm.c b/extlib/benz/vm.c index 4ab5a644..8068f933 100644 --- a/extlib/benz/vm.c +++ b/extlib/benz/vm.c @@ -103,7 +103,6 @@ pic_get_args(pic_state *pic, const char *format, ...) *p = GET_OPERAND(pic,i); break; } -#if PIC_ENABLE_FLOAT case 'f': { double *f; pic_value v; @@ -169,7 +168,6 @@ pic_get_args(pic_state *pic, const char *format, ...) } break; } -#endif case 'i': { int *k; pic_value v; @@ -178,11 +176,9 @@ pic_get_args(pic_state *pic, const char *format, ...) v = GET_OPERAND(pic, i); switch (pic_type(v)) { -#if PIC_ENABLE_FLOAT case PIC_TT_FLOAT: *k = (int)pic_float(v); break; -#endif case PIC_TT_INT: *k = pic_int(v); break; diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 124ae7b4..88f63804 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -101,19 +101,19 @@ write_str(pic_state *pic, pic_str *str, xFILE *file, int mode) xfprintf(pic, file, "\""); } -#if PIC_ENABLE_FLOAT static void write_float(pic_state *pic, double f, xFILE *file) { - if (isnan(f)) { - xfprintf(pic, file, signbit(f) ? "-nan.0" : "+nan.0"); - } else if (isinf(f)) { - xfprintf(pic, file, signbit(f) ? "-inf.0" : "+inf.0"); + if (f != f) { + xfprintf(pic, file, "+nan.0"); + } else if (f == 1.0 / 0.0) { + xfprintf(pic, file, "+inf.0"); + } else if (f == -1.0 / 0.0) { + xfprintf(pic, file, "-inf.0"); } else { xfprintf(pic, file, "%f", f); } } -#endif static void write_core(struct writer_control *p, pic_value); @@ -291,11 +291,9 @@ write_core(struct writer_control *p, pic_value obj) case PIC_TT_INT: xfprintf(pic, file, "%d", pic_int(obj)); break; -#if PIC_ENABLE_FLOAT case PIC_TT_FLOAT: write_float(pic, pic_float(obj), file); break; -#endif case PIC_TT_SYMBOL: xfprintf(pic, file, "%s", pic_symbol_name(pic, pic_sym_ptr(obj))); break;