Skip to content

Commit

Permalink
Simplify definition of NaN
Browse files Browse the repository at this point in the history
Replace u64->double bitcast with 0.0/0.0.

Change definition of JS_FLOAT64_NAN in the same way because that lets us
drop <math.h> 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.
  • Loading branch information
bnoordhuis committed Nov 18, 2024
1 parent f93dd58 commit 60c6eed
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 ),
};
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 1 addition & 5 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@
#ifndef QUICKJS_H
#define QUICKJS_H

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <math.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -89,7 +87,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
Expand Down Expand Up @@ -970,7 +968,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;
Expand Down Expand Up @@ -999,7 +996,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 } } }
Expand Down

0 comments on commit 60c6eed

Please sign in to comment.