From 78c5fa0d03ddc68928c4182b902c9fb34845aa8b Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 18 Nov 2024 10:56:58 +0100 Subject: [PATCH] Simplify definition of NaN Replace u64->double bitcast with 0.0/0.0. Change definition of JS_FLOAT64_NAN in the same way because that lets us drop from quickjs.h. Replace INFINITY and -INFINITY with 1.0/0.0 and -1.0/0.0 respectively because we already defined globalThis.INFINITY that way and so we might as well go all the way. --- cutils.h | 2 +- quickjs.c | 20 ++++++++++---------- quickjs.h | 5 +---- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/cutils.h b/cutils.h index 1e96ed68..2ed8bfc2 100644 --- a/cutils.h +++ b/cutils.h @@ -362,7 +362,7 @@ static inline double fromfp16(uint16_t v) { double d, s; int e; if ((v & 0x7C00) == 0x7C00) { - d = (v & 0x3FF) ? NAN : INFINITY; + d = (v & 0x3FF) ? 0.0/0.0 : 1.0/0.0; // nan or +inf } else { d = (v & 0x3FF) / 1024.; e = (v & 0x7C00) >> 10; diff --git a/quickjs.c b/quickjs.c index fc49fc8c..cfc056c5 100644 --- a/quickjs.c +++ b/quickjs.c @@ -41116,9 +41116,9 @@ static const JSCFunctionListEntry js_number_funcs[] = { JS_CFUNC_DEF("isSafeInteger", 1, js_number_isSafeInteger ), JS_PROP_DOUBLE_DEF("MAX_VALUE", 1.7976931348623157e+308, 0 ), JS_PROP_DOUBLE_DEF("MIN_VALUE", 5e-324, 0 ), - JS_PROP_U2D_DEF("NaN", 0x7FF8ull<<48, 0 ), // workaround for msvc - JS_PROP_DOUBLE_DEF("NEGATIVE_INFINITY", -INFINITY, 0 ), - JS_PROP_DOUBLE_DEF("POSITIVE_INFINITY", INFINITY, 0 ), + JS_PROP_DOUBLE_DEF("NaN", 0.0/0.0, 0 ), + JS_PROP_DOUBLE_DEF("NEGATIVE_INFINITY", -1.0/0.0, 0 ), + JS_PROP_DOUBLE_DEF("POSITIVE_INFINITY", 1.0/0.0, 0 ), JS_PROP_DOUBLE_DEF("EPSILON", 2.220446049250313e-16, 0 ), /* ES6 */ JS_PROP_DOUBLE_DEF("MAX_SAFE_INTEGER", 9007199254740991.0, 0 ), /* ES6 */ JS_PROP_DOUBLE_DEF("MIN_SAFE_INTEGER", -9007199254740991.0, 0 ), /* ES6 */ @@ -50196,8 +50196,8 @@ static const JSCFunctionListEntry js_global_funcs[] = { JS_CFUNC_MAGIC_DEF("encodeURIComponent", 1, js_global_encodeURI, 1 ), JS_CFUNC_DEF("escape", 1, js_global_escape ), JS_CFUNC_DEF("unescape", 1, js_global_unescape ), - JS_PROP_DOUBLE_DEF("Infinity", 1.0 / 0.0, 0 ), - JS_PROP_U2D_DEF("NaN", 0x7FF8ull<<48, 0 ), // workaround for msvc + JS_PROP_DOUBLE_DEF("Infinity", 1.0/0.0, 0 ), + JS_PROP_DOUBLE_DEF("NaN", 0.0/0.0, 0 ), JS_PROP_UNDEFINED_DEF("undefined", 0 ), JS_PROP_STRING_DEF("[Symbol.toStringTag]", "global", JS_PROP_CONFIGURABLE ), }; @@ -50337,7 +50337,7 @@ static double time_clip(double t) { if (t >= -8.64e15 && t <= 8.64e15) return trunc(t) + 0.0; /* convert -0 to +0 */ else - return NAN; + return JS_FLOAT64_NAN; } /* The spec mandates the use of 'double' and it specifies the order @@ -50357,7 +50357,7 @@ static double set_date_fields(double fields[minimum_length(7)], int is_local) { if (mn < 0) mn += 12; if (ym < -271821 || ym > 275760) - return NAN; + return JS_FLOAT64_NAN; yi = ym; mi = mn; @@ -50389,7 +50389,7 @@ static double set_date_fields(double fields[minimum_length(7)], int is_local) { /* emulate 21.4.1.16 MakeDate ( day, time ) */ tv = (temp = day * 86400000) + time; /* prevent generation of FMA */ if (!isfinite(tv)) - return NAN; + return JS_FLOAT64_NAN; /* adjust for local time and clip */ if (is_local) { @@ -50428,7 +50428,7 @@ static JSValue set_date_field(JSContext *ctx, JSValue this_val, int res, first_field, end_field, is_local, i, n; double d, a; - d = NAN; + d = JS_FLOAT64_NAN; first_field = (magic >> 8) & 0x0F; end_field = (magic >> 4) & 0x0F; is_local = magic & 0x0F; @@ -50628,7 +50628,7 @@ static JSValue js_date_constructor(JSContext *ctx, JSValue new_target, if (i == 0 && fields[0] >= 0 && fields[0] < 100) fields[0] += 1900; } - val = (i == n) ? set_date_fields(fields, 1) : NAN; + val = (i == n) ? set_date_fields(fields, 1) : JS_FLOAT64_NAN; } has_val: rv = js_create_from_ctor(ctx, new_target, JS_CLASS_DATE); diff --git a/quickjs.h b/quickjs.h index 709da6b1..753a0604 100644 --- a/quickjs.h +++ b/quickjs.h @@ -30,7 +30,6 @@ #include #include #include -#include #ifdef __cplusplus extern "C" { @@ -89,7 +88,7 @@ enum { /* any larger tag is FLOAT64 if JS_NAN_BOXING */ }; -#define JS_FLOAT64_NAN NAN +#define JS_FLOAT64_NAN (0.0/0.0) #define JSValueConst JSValue /* For backwards compatibility. */ #if defined(JS_NAN_BOXING) && JS_NAN_BOXING @@ -970,7 +969,6 @@ typedef struct JSCFunctionListEntry { const char *str; /* pure ASCII or UTF-8 encoded */ int32_t i32; int64_t i64; - uint64_t u64; double f64; } u; } JSCFunctionListEntry; @@ -999,7 +997,6 @@ typedef struct JSCFunctionListEntry { #define JS_PROP_INT32_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT32, 0, { .i32 = val } } #define JS_PROP_INT64_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT64, 0, { .i64 = val } } #define JS_PROP_DOUBLE_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_DOUBLE, 0, { .f64 = val } } -#define JS_PROP_U2D_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_DOUBLE, 0, { .u64 = val } } #define JS_PROP_UNDEFINED_DEF(name, prop_flags) { name, prop_flags, JS_DEF_PROP_UNDEFINED, 0, { .i32 = 0 } } #define JS_OBJECT_DEF(name, tab, len, prop_flags) { name, prop_flags, JS_DEF_OBJECT, 0, { .prop_list = { tab, len } } } #define JS_ALIAS_DEF(name, from) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_ALIAS, 0, { .alias = { from, -1 } } }