From d724c5b9592393506ad607e2cc1a60206e0782ac Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sun, 30 Dec 2018 12:37:50 -0500 Subject: [PATCH] Update number representation so that wrapping numbers isn't doesn't need to check for NaNs. Change ordering of types. --- src/core/marsh.c | 4 ++-- src/core/strtod.c | 2 +- src/core/util.c | 2 +- src/core/util.h | 2 ++ src/core/wrap.c | 43 ++++++++++++++++++--------------------- src/include/janet/janet.h | 11 +--------- test/suite0.janet | 4 ++-- 7 files changed, 29 insertions(+), 39 deletions(-) diff --git a/src/core/marsh.c b/src/core/marsh.c index 4f0dcb2b..8db041b9 100644 --- a/src/core/marsh.c +++ b/src/core/marsh.c @@ -61,12 +61,12 @@ const char *mr_strings[] = { /* Lead bytes in marshaling protocol */ enum { - LB_NIL = 200, + LB_REAL = 200, + LB_NIL, LB_FALSE, LB_TRUE, LB_FIBER, LB_INTEGER, - LB_REAL, LB_STRING, LB_SYMBOL, LB_ARRAY, diff --git a/src/core/strtod.c b/src/core/strtod.c index 1cf144a8..d14f0aea 100644 --- a/src/core/strtod.c +++ b/src/core/strtod.c @@ -229,7 +229,7 @@ static double convert( /* Short circuit zero and huge numbers */ if (mant->n == 0 && mant->first_digit == 0) - return 0.0; + return negative ? -0.0 : 0.0; if (exponent > 1023) return negative ? -INFINITY : INFINITY; diff --git a/src/core/util.c b/src/core/util.c index a75e542f..f9494cc0 100644 --- a/src/core/util.c +++ b/src/core/util.c @@ -35,11 +35,11 @@ const char janet_base64[65] = /* The JANET value types in order. These types can be used as * mnemonics instead of a bit pattern for type checking */ const char *const janet_type_names[15] = { + ":number", ":nil", ":boolean", ":boolean", ":fiber", - ":number", ":string", ":symbol", ":array", diff --git a/src/core/util.h b/src/core/util.h index 1bc7cc4d..a3a6d059 100644 --- a/src/core/util.h +++ b/src/core/util.h @@ -35,6 +35,8 @@ int32_t janet_tablen(int32_t n); void janet_buffer_push_types(JanetBuffer *buffer, int types); const JanetKV *janet_dict_find(const JanetKV *buckets, int32_t cap, Janet key); Janet janet_dict_get(const JanetKV *buckets, int32_t cap, Janet key); +void janet_memempty(JanetKV *mem, int32_t count); +void *janet_memalloc_empty(int32_t count); const void *janet_strbinsearch( const void *tab, size_t tabcount, diff --git a/src/core/wrap.c b/src/core/wrap.c index 5db56b16..8cca403c 100644 --- a/src/core/wrap.c +++ b/src/core/wrap.c @@ -22,6 +22,26 @@ #include +void *janet_memalloc_empty(int32_t count) { + int32_t i; + void *mem = malloc(count * sizeof(JanetKV)); + JanetKV *mmem = (JanetKV *)mem; + for (i = 0; i < count; i++) { + JanetKV *kv = mmem + i; + kv->key = janet_wrap_nil(); + kv->value = janet_wrap_nil(); + } + return mem; +} + +void janet_memempty(JanetKV *mem, int32_t count) { + int32_t i; + for (i = 0; i < count; i++) { + mem[i].key = janet_wrap_nil(); + mem[i].value = janet_wrap_nil(); + } +} + #ifdef JANET_NANBOX_64 void *janet_nanbox_to_pointer(Janet x) { @@ -46,9 +66,6 @@ Janet janet_nanbox_from_cpointer(const void *p, uint64_t tagmask) { Janet janet_nanbox_from_double(double d) { Janet ret; ret.number = d; - /* Normalize NaNs */ - if (d != d) - ret.u64 = janet_nanbox_tag(JANET_NUMBER); return ret; } @@ -58,26 +75,6 @@ Janet janet_nanbox_from_bits(uint64_t bits) { return ret; } -void *janet_nanbox_memalloc_empty(int32_t count) { - int32_t i; - void *mem = malloc(count * sizeof(JanetKV)); - JanetKV *mmem = (JanetKV *)mem; - for (i = 0; i < count; i++) { - JanetKV *kv = mmem + i; - kv->key = janet_wrap_nil(); - kv->value = janet_wrap_nil(); - } - return mem; -} - -void janet_nanbox_memempty(JanetKV *mem, int32_t count) { - int32_t i; - for (i = 0; i < count; i++) { - mem[i].key = janet_wrap_nil(); - mem[i].value = janet_wrap_nil(); - } -} - #elif defined(JANET_NANBOX_32) Janet janet_wrap_number(double x) { diff --git a/src/include/janet/janet.h b/src/include/janet/janet.h index a29f78ad..46d25806 100644 --- a/src/include/janet/janet.h +++ b/src/include/janet/janet.h @@ -274,11 +274,11 @@ typedef int (*JanetCFunction)(JanetArgs args); /* Basic types for all Janet Values */ typedef enum JanetType { + JANET_NUMBER, JANET_NIL, JANET_FALSE, JANET_TRUE, JANET_FIBER, - JANET_NUMBER, JANET_STRING, JANET_SYMBOL, JANET_ARRAY, @@ -372,16 +372,11 @@ union Janet { : janet_nanbox_checkauxtype((x), (t))) JANET_API void *janet_nanbox_to_pointer(Janet x); -JANET_API void janet_nanbox_memempty(JanetKV *mem, int32_t count); -JANET_API void *janet_nanbox_memalloc_empty(int32_t count); JANET_API Janet janet_nanbox_from_pointer(void *p, uint64_t tagmask); JANET_API Janet janet_nanbox_from_cpointer(const void *p, uint64_t tagmask); JANET_API Janet janet_nanbox_from_double(double d); JANET_API Janet janet_nanbox_from_bits(uint64_t bits); -#define janet_memempty(mem, len) janet_nanbox_memempty((mem), (len)) -#define janet_memalloc_empty(count) janet_nanbox_memalloc_empty(count) - /* Todo - check for single mask operation */ #define janet_truthy(x) \ (!(janet_checktype((x), JANET_NIL) || janet_checktype((x), JANET_FALSE))) @@ -462,8 +457,6 @@ union Janet { #define janet_u64(x) ((x).u64) #define janet_type(x) (((x).tagged.type < JANET_DOUBLE_OFFSET) ? (x).tagged.type : JANET_NUMBER) #define janet_checktype(x, t) ((x).tagged.type == (t)) -#define janet_memempty(mem, count) memset((mem), 0, sizeof(JanetKV) * (count)) -#define janet_memalloc_empty(count) calloc((count), sizeof(JanetKV)) #define janet_truthy(x) ((x).tagged.type != JANET_NIL && (x).tagged.type != JANET_FALSE) JANET_API Janet janet_wrap_number(double x); @@ -518,8 +511,6 @@ struct Janet { }; #define janet_u64(x) ((x).as.u64) -#define janet_memempty(mem, count) memset((mem), 0, sizeof(JanetKV) * (count)) -#define janet_memalloc_empty(count) calloc((count), sizeof(JanetKV)) #define janet_type(x) ((x).type) #define janet_checktype(x, t) ((x).type == (t)) #define janet_truthy(x) \ diff --git a/test/suite0.janet b/test/suite0.janet index 3e761e26..6a50ccbb 100644 --- a/test/suite0.janet +++ b/test/suite0.janet @@ -37,9 +37,9 @@ (assert (= 7 (% 20 13)) "modulo 1") (assert (= -7 (% -20 13)) "modulo 2") -(assert (order< nil false true +(assert (order< 1.0 nil false true (fiber/new (fn [] 1)) - 1.0 "hi" + "hi" (quote hello) (array 1 2 3) (tuple 1 2 3)