DUK_LOCAL duk_ret_t duk__math_minmax(duk_context *ctx, duk_double_t initial, duk__two_arg_func min_max) { duk_idx_t n = duk_get_top(ctx); duk_idx_t i; duk_double_t res = initial; duk_double_t t; /* * Note: fmax() does not match the E5 semantics. E5 requires * that if -any- input to Math.max() is a NaN, the result is a * NaN. fmax() will return a NaN only if -both- inputs are NaN. * Same applies to fmin(). * * Note: every input value must be coerced with ToNumber(), even * if we know the result will be a NaN anyway: ToNumber() may have * side effects for which even order of evaluation matters. */ for (i = 0; i < n; i++) { t = duk_to_number(ctx, i); if (DUK_FPCLASSIFY(t) == DUK_FP_NAN || DUK_FPCLASSIFY(res) == DUK_FP_NAN) { /* Note: not normalized, but duk_push_number() will normalize */ res = (duk_double_t) DUK_DOUBLE_NAN; } else { res = (duk_double_t) min_max(res, (double) t); } } duk_push_number(ctx, res); return 1; }
duk_int32_t duk_js_toint32(duk_hthread *thr, duk_tval *tv) { duk_double_t d = duk_js_tonumber(thr, tv); /* invalidates tv */ d = duk__toint32_touint32_helper(d, 1); DUK_ASSERT(DUK_FPCLASSIFY(d) == DUK_FP_ZERO || DUK_FPCLASSIFY(d) == DUK_FP_NORMAL); DUK_ASSERT(d >= -2147483648.0 && d <= 2147483647.0); /* [-0x80000000,0x7fffffff] */ DUK_ASSERT(d == ((duk_double_t) ((duk_int32_t) d))); /* whole, won't clip */ return (duk_int32_t) d; }
duk_uint32_t duk_js_touint32(duk_hthread *thr, duk_tval *tv) { duk_double_t d = duk_js_tonumber(thr, tv); /* invalidates tv */ d = duk__toint32_touint32_helper(d, 0); DUK_ASSERT(DUK_FPCLASSIFY(d) == DUK_FP_ZERO || DUK_FPCLASSIFY(d) == DUK_FP_NORMAL); DUK_ASSERT(d >= 0.0 && d <= 4294967295.0); /* [0x00000000, 0xffffffff] */ DUK_ASSERT(d == ((duk_double_t) ((duk_uint32_t) d))); /* whole, won't clip */ return (duk_uint32_t) d; }
duk_ret_t duk_bi_number_prototype_to_fixed(duk_context *ctx) { duk_small_int_t frac_digits; duk_double_t d; duk_small_int_t c; duk_small_uint_t n2s_flags; frac_digits = (duk_small_int_t) duk_to_int_check_range(ctx, 0, 0, 20); d = duk__push_this_number_plain(ctx); c = (duk_small_int_t) DUK_FPCLASSIFY(d); if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) { goto use_to_string; } if (d >= 1.0e21 || d <= -1.0e21) { goto use_to_string; } n2s_flags = DUK_N2S_FLAG_FIXED_FORMAT | DUK_N2S_FLAG_FRACTION_DIGITS; duk_numconv_stringify(ctx, 10 /*radix*/, frac_digits /*digits*/, n2s_flags /*flags*/); return 1; use_to_string: DUK_ASSERT_TOP(ctx, 2); duk_to_string(ctx, -1); return 1; }
duk_ret_t duk_bi_number_prototype_to_exponential(duk_context *ctx) { duk_bool_t frac_undefined; duk_small_int_t frac_digits; duk_double_t d; duk_small_int_t c; duk_small_uint_t n2s_flags; d = duk__push_this_number_plain(ctx); frac_undefined = duk_is_undefined(ctx, 0); duk_to_int(ctx, 0); /* for side effects */ c = (duk_small_int_t) DUK_FPCLASSIFY(d); if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) { goto use_to_string; } frac_digits = (duk_small_int_t) duk_to_int_check_range(ctx, 0, 0, 20); n2s_flags = DUK_N2S_FLAG_FORCE_EXP | (frac_undefined ? 0 : DUK_N2S_FLAG_FIXED_FORMAT); duk_numconv_stringify(ctx, 10 /*radix*/, frac_digits + 1 /*leading digit + fractions*/, n2s_flags /*flags*/); return 1; use_to_string: DUK_ASSERT_TOP(ctx, 2); duk_to_string(ctx, -1); return 1; }
DUK_INTERNAL duk_int32_t duk_js_toint32(duk_hthread *thr, duk_tval *tv) { duk_double_t d; #if defined(DUK_USE_FASTINT) if (DUK_TVAL_IS_FASTINT(tv)) { return DUK_TVAL_GET_FASTINT_I32(tv); } #endif d = duk_js_tonumber(thr, tv); /* invalidates tv */ d = duk__toint32_touint32_helper(d, 1); DUK_ASSERT(DUK_FPCLASSIFY(d) == DUK_FP_ZERO || DUK_FPCLASSIFY(d) == DUK_FP_NORMAL); DUK_ASSERT(d >= -2147483648.0 && d <= 2147483647.0); /* [-0x80000000,0x7fffffff] */ DUK_ASSERT(d == ((duk_double_t) ((duk_int32_t) d))); /* whole, won't clip */ return (duk_int32_t) d; }
int duk_repl_isfinite(double x) { int c = DUK_FPCLASSIFY(x); if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) { return 0; } else { return 1; } }
DUK_INTERNAL duk_bool_t duk_js_toboolean(duk_tval *tv) { switch (DUK_TVAL_GET_TAG(tv)) { case DUK_TAG_UNDEFINED: case DUK_TAG_NULL: return 0; case DUK_TAG_BOOLEAN: return DUK_TVAL_GET_BOOLEAN(tv); case DUK_TAG_STRING: { duk_hstring *h = DUK_TVAL_GET_STRING(tv); DUK_ASSERT(h != NULL); return (DUK_HSTRING_GET_BYTELEN(h) > 0 ? 1 : 0); } case DUK_TAG_OBJECT: { return 1; } case DUK_TAG_BUFFER: { /* mimic semantics for strings */ duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); DUK_ASSERT(h != NULL); return (DUK_HBUFFER_GET_SIZE(h) > 0 ? 1 : 0); } case DUK_TAG_POINTER: { void *p = DUK_TVAL_GET_POINTER(tv); return (p != NULL ? 1 : 0); } case DUK_TAG_LIGHTFUNC: { return 1; } #if defined(DUK_USE_FASTINT) case DUK_TAG_FASTINT: if (DUK_TVAL_GET_FASTINT(tv) != 0) { return 1; } else { return 0; } #endif default: { /* number */ duk_double_t d; int c; DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv)); DUK_ASSERT(DUK_TVAL_IS_DOUBLE(tv)); d = DUK_TVAL_GET_DOUBLE(tv); c = DUK_FPCLASSIFY((double) d); if (c == DUK_FP_ZERO || c == DUK_FP_NAN) { return 0; } else { return 1; } } } DUK_UNREACHABLE(); }
duk_ret_t duk_bi_number_prototype_to_precision(duk_context *ctx) { /* The specification has quite awkward order of coercion and * checks for toPrecision(). The operations below are a bit * reordered, within constraints of observable side effects. */ duk_double_t d; duk_small_int_t prec; duk_small_int_t c; duk_small_uint_t n2s_flags; DUK_ASSERT_TOP(ctx, 1); d = duk__push_this_number_plain(ctx); if (duk_is_undefined(ctx, 0)) { goto use_to_string; } DUK_ASSERT_TOP(ctx, 2); duk_to_int(ctx, 0); /* for side effects */ c = (duk_small_int_t) DUK_FPCLASSIFY(d); if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) { goto use_to_string; } prec = (duk_small_int_t) duk_to_int_check_range(ctx, 0, 1, 21); n2s_flags = DUK_N2S_FLAG_FIXED_FORMAT | DUK_N2S_FLAG_NO_ZERO_PAD; duk_numconv_stringify(ctx, 10 /*radix*/, prec /*digits*/, n2s_flags /*flags*/); return 1; use_to_string: /* Used when precision is undefined; also used for NaN (-> "NaN"), * and +/- infinity (-> "Infinity", "-Infinity"). */ DUK_ASSERT_TOP(ctx, 2); duk_to_string(ctx, -1); return 1; }
/* exposed, used by e.g. duk_bi_date.c */ DUK_INTERNAL duk_double_t duk_js_tointeger_number(duk_double_t x) { duk_small_int_t c = (duk_small_int_t) DUK_FPCLASSIFY(x); if (c == DUK_FP_NAN) { return 0.0; } else if (c == DUK_FP_ZERO || c == DUK_FP_INFINITE) { /* XXX: FP_ZERO check can be removed, the else clause handles it * correctly (preserving sign). */ return x; } else { duk_small_int_t s = (duk_small_int_t) DUK_SIGNBIT(x); x = DUK_FLOOR(DUK_FABS(x)); /* truncate towards zero */ if (s) { x = -x; } return x; } }
DUK_LOCAL double duk__round_fixed(double x) { /* Numbers half-way between integers must be rounded towards +Infinity, * e.g. -3.5 must be rounded to -3 (not -4). When rounded to zero, zero * sign must be set appropriately. E5.1 Section 15.8.2.15. * * Note that ANSI C round() is "round to nearest integer, away from zero", * which is incorrect for negative values. Here we make do with floor(). */ duk_small_int_t c = (duk_small_int_t) DUK_FPCLASSIFY(x); if (c == DUK_FP_NAN || c == DUK_FP_INFINITE || c == DUK_FP_ZERO) { return x; } /* * x is finite and non-zero * * -1.6 -> floor(-1.1) -> -2 * -1.5 -> floor(-1.0) -> -1 (towards +Inf) * -1.4 -> floor(-0.9) -> -1 * -0.5 -> -0.0 (special case) * -0.1 -> -0.0 (special case) * +0.1 -> +0.0 (special case) * +0.5 -> floor(+1.0) -> 1 (towards +Inf) * +1.4 -> floor(+1.9) -> 1 * +1.5 -> floor(+2.0) -> 2 (towards +Inf) * +1.6 -> floor(+2.1) -> 2 */ if (x >= -0.5 && x < 0.5) { /* +0.5 is handled by floor, this is on purpose */ if (x < 0.0) { return -0.0; } else { return +0.0; } } return DUK_FLOOR(x + 0.5); }
duk_bool_t duk_js_toboolean(duk_tval *tv) { switch (DUK_TVAL_GET_TAG(tv)) { case DUK_TAG_UNDEFINED: case DUK_TAG_NULL: return 0; case DUK_TAG_BOOLEAN: return DUK_TVAL_GET_BOOLEAN(tv); case DUK_TAG_STRING: { duk_hstring *h = DUK_TVAL_GET_STRING(tv); DUK_ASSERT(h != NULL); return (h->blen > 0 ? 1 : 0); } case DUK_TAG_OBJECT: { return 1; } case DUK_TAG_BUFFER: { /* mimic semantics for strings */ duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); DUK_ASSERT(h != NULL); return (DUK_HBUFFER_GET_SIZE(h) > 0 ? 1 : 0); } case DUK_TAG_POINTER: { void *p = DUK_TVAL_GET_POINTER(tv); return (p != NULL ? 1 : 0); } default: { /* number */ int c; DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); c = DUK_FPCLASSIFY(DUK_TVAL_GET_NUMBER(tv)); if (c == DUK_FP_ZERO || c == DUK_FP_NAN) { return 0; } else { return 1; } } } DUK_UNREACHABLE(); }
static double pow_fixed(double x, double y) { /* The ANSI C pow() semantics differ from Ecmascript. * * E.g. when x==1 and y is +/- infinite, the Ecmascript required * result is NaN, while at least Linux pow() returns 1. */ int cy; cy = DUK_FPCLASSIFY(y); if (cy == DUK_FP_NAN) { goto ret_nan; } if (fabs(x) == 1.0 && cy == DUK_FP_INFINITE) { goto ret_nan; } return pow(x, y); ret_nan: return DUK_DOUBLE_NAN; }
DUK_LOCAL double duk__cbrt(double x) { /* cbrt() is C99. To avoid hassling embedders with the need to provide a * cube root function, we can get by with pow(). The result is not * identical, but that's OK: ES2015 says it's implementation-dependent. */ #if defined(DUK_CBRT) /* cbrt() matches ES2015 requirements. */ return DUK_CBRT(x); #else duk_small_int_t c = (duk_small_int_t) DUK_FPCLASSIFY(x); /* pow() does not, however. */ if (c == DUK_FP_NAN || c == DUK_FP_INFINITE || c == DUK_FP_ZERO) { return x; } if (DUK_SIGNBIT(x)) { return -DUK_POW(-x, 1.0 / 3.0); } else { return DUK_POW(x, 1.0 / 3.0); } #endif }
/* combined algorithm matching E5 Sections 9.5 and 9.6 */ DUK_LOCAL duk_double_t duk__toint32_touint32_helper(duk_double_t x, duk_bool_t is_toint32) { duk_small_int_t c = (duk_small_int_t) DUK_FPCLASSIFY(x); duk_small_int_t s; if (c == DUK_FP_NAN || c == DUK_FP_ZERO || c == DUK_FP_INFINITE) { return 0.0; } /* x = sign(x) * floor(abs(x)), i.e. truncate towards zero, keep sign */ s = (duk_small_int_t) DUK_SIGNBIT(x); x = DUK_FLOOR(DUK_FABS(x)); if (s) { x = -x; } /* NOTE: fmod(x) result sign is same as sign of x, which * differs from what Javascript wants (see Section 9.6). */ x = DUK_FMOD(x, DUK_DOUBLE_2TO32); /* -> x in ]-2**32, 2**32[ */ if (x < 0.0) { x += DUK_DOUBLE_2TO32; } /* -> x in [0, 2**32[ */ if (is_toint32) { if (x >= DUK_DOUBLE_2TO31) { /* x in [2**31, 2**32[ */ x -= DUK_DOUBLE_2TO32; /* -> x in [-2**31,2**31[ */ } } return x; }
DUK_LOCAL duk_bool_t duk__js_equals_number(duk_double_t x, duk_double_t y) { #if defined(DUK_USE_PARANOID_MATH) /* Straightforward algorithm, makes fewer compiler assumptions. */ duk_small_int_t cx = (duk_small_int_t) DUK_FPCLASSIFY(x); duk_small_int_t cy = (duk_small_int_t) DUK_FPCLASSIFY(y); if (cx == DUK_FP_NAN || cy == DUK_FP_NAN) { return 0; } if (cx == DUK_FP_ZERO && cy == DUK_FP_ZERO) { return 1; } if (x == y) { return 1; } return 0; #else /* DUK_USE_PARANOID_MATH */ /* Better equivalent algorithm. If the compiler is compliant, C and * Ecmascript semantics are identical for this particular comparison. * In particular, NaNs must never compare equal and zeroes must compare * equal regardless of sign. Could also use a macro, but this inlines * already nicely (no difference on gcc, for instance). */ if (x == y) { /* IEEE requires that NaNs compare false */ DUK_ASSERT(DUK_FPCLASSIFY(x) != DUK_FP_NAN); DUK_ASSERT(DUK_FPCLASSIFY(y) != DUK_FP_NAN); return 1; } else { /* IEEE requires that zeros compare the same regardless * of their signed, so if both x and y are zeroes, they * are caught above. */ DUK_ASSERT(!(DUK_FPCLASSIFY(x) == DUK_FP_ZERO && DUK_FPCLASSIFY(y) == DUK_FP_ZERO)); return 0; } #endif /* DUK_USE_PARANOID_MATH */ }
DUK_LOCAL duk_bool_t duk__js_samevalue_number(duk_double_t x, duk_double_t y) { #if defined(DUK_USE_PARANOID_MATH) duk_small_int_t cx = (duk_small_int_t) DUK_FPCLASSIFY(x); duk_small_int_t cy = (duk_small_int_t) DUK_FPCLASSIFY(y); if (cx == DUK_FP_NAN && cy == DUK_FP_NAN) { /* SameValue(NaN, NaN) = true, regardless of NaN sign or extra bits */ return 1; } if (cx == DUK_FP_ZERO && cy == DUK_FP_ZERO) { /* Note: cannot assume that a non-zero return value of signbit() would * always be the same -- hence cannot (portably) use something like: * * signbit(x) == signbit(y) */ duk_small_int_t sx = (DUK_SIGNBIT(x) ? 1 : 0); duk_small_int_t sy = (DUK_SIGNBIT(y) ? 1 : 0); return (sx == sy); } /* normal comparison; known: * - both x and y are not NaNs (but one of them can be) * - both x and y are not zero (but one of them can be) * - x and y may be denormal or infinite */ return (x == y); #else /* DUK_USE_PARANOID_MATH */ duk_small_int_t cx = (duk_small_int_t) DUK_FPCLASSIFY(x); duk_small_int_t cy = (duk_small_int_t) DUK_FPCLASSIFY(y); if (x == y) { /* IEEE requires that NaNs compare false */ DUK_ASSERT(DUK_FPCLASSIFY(x) != DUK_FP_NAN); DUK_ASSERT(DUK_FPCLASSIFY(y) != DUK_FP_NAN); /* Using classification has smaller footprint than direct comparison. */ if (DUK_UNLIKELY(cx == DUK_FP_ZERO && cy == DUK_FP_ZERO)) { /* Note: cannot assume that a non-zero return value of signbit() would * always be the same -- hence cannot (portably) use something like: * * signbit(x) == signbit(y) */ duk_small_int_t sx = (DUK_SIGNBIT(x) ? 1 : 0); duk_small_int_t sy = (DUK_SIGNBIT(y) ? 1 : 0); return (sx == sy); } return 1; } else { /* IEEE requires that zeros compare the same regardless * of their signed, so if both x and y are zeroes, they * are caught above. */ DUK_ASSERT(!(DUK_FPCLASSIFY(x) == DUK_FP_ZERO && DUK_FPCLASSIFY(y) == DUK_FP_ZERO)); /* Difference to non-strict/strict comparison is that NaNs compare * equal and signed zero signs matter. */ if (DUK_UNLIKELY(cx == DUK_FP_NAN && cy == DUK_FP_NAN)) { /* SameValue(NaN, NaN) = true, regardless of NaN sign or extra bits */ return 1; } return 0; } #endif /* DUK_USE_PARANOID_MATH */ }
DUK_INTERNAL duk_ret_t duk_bi_math_object_hypot(duk_context *ctx) { /* * E6 Section 20.2.2.18: Math.hypot * * - If no arguments are passed, the result is +0. * - If any argument is +inf, the result is +inf. * - If any argument is -inf, the result is +inf. * - If no argument is +inf or -inf, and any argument is NaN, the result is * NaN. * - If all arguments are either +0 or -0, the result is +0. */ duk_idx_t nargs; duk_idx_t i; duk_bool_t found_nan; duk_double_t max; duk_double_t sum, summand; duk_double_t comp, prelim; duk_double_t t; nargs = duk_get_top(ctx); /* Find the highest value. Also ToNumber() coerces. */ max = 0.0; found_nan = 0; for (i = 0; i < nargs; i++) { t = DUK_FABS(duk_to_number(ctx, i)); if (DUK_FPCLASSIFY(t) == DUK_FP_NAN) { found_nan = 1; } else { max = duk_double_fmax(max, t); } } /* Early return cases. */ if (max == DUK_DOUBLE_INFINITY) { duk_push_number(ctx, DUK_DOUBLE_INFINITY); return 1; } else if (found_nan) { duk_push_number(ctx, DUK_DOUBLE_NAN); return 1; } else if (max == 0.0) { duk_push_number(ctx, 0.0); /* Otherwise we'd divide by zero. */ return 1; } /* Use Kahan summation and normalize to the highest value to minimize * floating point rounding error and avoid overflow. * * https://en.wikipedia.org/wiki/Kahan_summation_algorithm */ sum = 0.0; comp = 0.0; for (i = 0; i < nargs; i++) { t = DUK_FABS(duk_get_number(ctx, i)) / max; summand = (t * t) - comp; prelim = sum + summand; comp = (prelim - sum) - summand; sum = prelim; } duk_push_number(ctx, (duk_double_t) DUK_SQRT(sum) * max); return 1; }
duk_bool_t duk_js_compare_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_int_t flags) { duk_context *ctx = (duk_context *) thr; duk_double_t d1, d2; duk_small_int_t c1, c2; duk_small_int_t s1, s2; duk_small_int_t rc; duk_bool_t retval; duk_push_tval(ctx, tv_x); duk_push_tval(ctx, tv_y); if (flags & DUK_COMPARE_FLAG_EVAL_LEFT_FIRST) { duk_to_primitive(ctx, -2, DUK_HINT_NUMBER); duk_to_primitive(ctx, -1, DUK_HINT_NUMBER); } else { duk_to_primitive(ctx, -1, DUK_HINT_NUMBER); duk_to_primitive(ctx, -2, DUK_HINT_NUMBER); } /* Note: reuse variables */ tv_x = duk_get_tval(ctx, -2); tv_y = duk_get_tval(ctx, -1); if (DUK_TVAL_IS_STRING(tv_x) && DUK_TVAL_IS_STRING(tv_y)) { duk_hstring *h1 = DUK_TVAL_GET_STRING(tv_x); duk_hstring *h2 = DUK_TVAL_GET_STRING(tv_y); DUK_ASSERT(h1 != NULL); DUK_ASSERT(h2 != NULL); rc = duk_js_string_compare(h1, h2); if (rc < 0) { goto lt_true; } else { goto lt_false; } } else { /* Ordering should not matter (E5 Section 11.8.5, step 3.a) but * preserve it just in case. */ if (flags & DUK_COMPARE_FLAG_EVAL_LEFT_FIRST) { d1 = duk_to_number(ctx, -2); d2 = duk_to_number(ctx, -1); } else { d2 = duk_to_number(ctx, -1); d1 = duk_to_number(ctx, -2); } c1 = (duk_small_int_t) DUK_FPCLASSIFY(d1); s1 = (duk_small_int_t) DUK_SIGNBIT(d1); c2 = (duk_small_int_t) DUK_FPCLASSIFY(d2); s2 = (duk_small_int_t) DUK_SIGNBIT(d2); if (c1 == DUK_FP_NAN || c2 == DUK_FP_NAN) { goto lt_undefined; } if (c1 == DUK_FP_ZERO && c2 == DUK_FP_ZERO) { /* For all combinations: +0 < +0, +0 < -0, -0 < +0, -0 < -0, * steps e, f, and g. */ goto lt_false; } if (d1 == d2) { goto lt_false; } if (c1 == DUK_FP_INFINITE && s1 == 0) { /* x == +Infinity */ goto lt_false; } if (c2 == DUK_FP_INFINITE && s2 == 0) { /* y == +Infinity */ goto lt_true; } if (c2 == DUK_FP_INFINITE && s2 != 0) { /* y == -Infinity */ goto lt_false; } if (c1 == DUK_FP_INFINITE && s1 != 0) { /* x == -Infinity */ goto lt_true; } if (d1 < d2) { goto lt_true; } goto lt_false; } lt_undefined: /* Note: undefined from Section 11.8.5 always results in false * return (see e.g. Section 11.8.3) - hence special treatment here. */ retval = 0; goto cleanup; lt_true: if (flags & DUK_COMPARE_FLAG_NEGATE) { retval = 0; goto cleanup; } else { retval = 1; goto cleanup; } /* never here */ lt_false: if (flags & DUK_COMPARE_FLAG_NEGATE) { retval = 1; goto cleanup; } else { retval = 0; goto cleanup; } /* never here */ cleanup: duk_pop_2(ctx); return retval; }
static double duk__pow_fixed(double x, double y) { /* The ANSI C pow() semantics differ from Ecmascript. * * E.g. when x==1 and y is +/- infinite, the Ecmascript required * result is NaN, while at least Linux pow() returns 1. */ int cx, cy, sx; DUK_UNREF(cx); DUK_UNREF(sx); cy = DUK_FPCLASSIFY(y); if (cy == DUK_FP_NAN) { goto ret_nan; } if (DUK_FABS(x) == 1.0 && cy == DUK_FP_INFINITE) { goto ret_nan; } #if defined(DUK_USE_POW_NETBSD_WORKAROUND) /* See test-bug-netbsd-math-pow.js: NetBSD 6.0 on x86 (at least) does not * correctly handle some cases where x=+/-0. Specific fixes to these * here. */ cx = DUK_FPCLASSIFY(x); if (cx == DUK_FP_ZERO && y < 0.0) { sx = DUK_SIGNBIT(x); if (sx == 0) { /* Math.pow(+0,y) should be Infinity when y<0. NetBSD pow() * returns -Infinity instead when y is <0 and finite. The * if-clause also catches y == -Infinity (which works even * without the fix). */ return DUK_DOUBLE_INFINITY; } else { /* Math.pow(-0,y) where y<0 should be: * - -Infinity if y<0 and an odd integer * - Infinity otherwise * NetBSD pow() returns -Infinity for all finite y<0. The * if-clause also catches y == -Infinity (which works even * without the fix). */ /* fmod() return value has same sign as input (negative) so * the result here will be in the range ]-2,0], 1 indicates * odd. If x is -Infinity, NaN is returned and the odd check * always concludes "not odd" which results in desired outcome. */ double tmp = DUK_FMOD(y, 2); if (tmp == -1.0) { return -DUK_DOUBLE_INFINITY; } else { /* Not odd, or y == -Infinity */ return DUK_DOUBLE_INFINITY; } } } #endif return DUK_POW(x, y); ret_nan: return DUK_DOUBLE_NAN; }
DUK_INTERNAL duk_bool_t duk_js_compare_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_int_t flags) { duk_context *ctx = (duk_context *) thr; duk_double_t d1, d2; duk_small_int_t c1, c2; duk_small_int_t s1, s2; duk_small_int_t rc; duk_bool_t retval; /* Fast path for fastints */ #if defined(DUK_USE_FASTINT) if (DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y)) { duk_int64_t v1 = DUK_TVAL_GET_FASTINT(tv_x); duk_int64_t v2 = DUK_TVAL_GET_FASTINT(tv_y); if (v1 < v2) { /* 'lt is true' */ retval = 1; } else { retval = 0; } if (flags & DUK_COMPARE_FLAG_NEGATE) { retval ^= 1; } return retval; } #endif /* DUK_USE_FASTINT */ /* Fast path for numbers (one of which may be a fastint) */ #if 1 /* XXX: make fast paths optional for size minimization? */ if (DUK_TVAL_IS_NUMBER(tv_x) && DUK_TVAL_IS_NUMBER(tv_y)) { d1 = DUK_TVAL_GET_NUMBER(tv_x); d2 = DUK_TVAL_GET_NUMBER(tv_y); c1 = DUK_FPCLASSIFY(d1); c2 = DUK_FPCLASSIFY(d2); if (c1 == DUK_FP_NORMAL && c2 == DUK_FP_NORMAL) { /* XXX: this is a very narrow check, and doesn't cover * zeroes, subnormals, infinities, which compare normally. */ if (d1 < d2) { /* 'lt is true' */ retval = 1; } else { retval = 0; } if (flags & DUK_COMPARE_FLAG_NEGATE) { retval ^= 1; } return retval; } } #endif /* Slow path */ duk_push_tval(ctx, tv_x); duk_push_tval(ctx, tv_y); if (flags & DUK_COMPARE_FLAG_EVAL_LEFT_FIRST) { duk_to_primitive(ctx, -2, DUK_HINT_NUMBER); duk_to_primitive(ctx, -1, DUK_HINT_NUMBER); } else { duk_to_primitive(ctx, -1, DUK_HINT_NUMBER); duk_to_primitive(ctx, -2, DUK_HINT_NUMBER); } /* Note: reuse variables */ tv_x = duk_get_tval(ctx, -2); tv_y = duk_get_tval(ctx, -1); if (DUK_TVAL_IS_STRING(tv_x) && DUK_TVAL_IS_STRING(tv_y)) { duk_hstring *h1 = DUK_TVAL_GET_STRING(tv_x); duk_hstring *h2 = DUK_TVAL_GET_STRING(tv_y); DUK_ASSERT(h1 != NULL); DUK_ASSERT(h2 != NULL); rc = duk_js_string_compare(h1, h2); if (rc < 0) { goto lt_true; } else { goto lt_false; } } else { /* Ordering should not matter (E5 Section 11.8.5, step 3.a) but * preserve it just in case. */ if (flags & DUK_COMPARE_FLAG_EVAL_LEFT_FIRST) { d1 = duk_to_number(ctx, -2); d2 = duk_to_number(ctx, -1); } else { d2 = duk_to_number(ctx, -1); d1 = duk_to_number(ctx, -2); } c1 = (duk_small_int_t) DUK_FPCLASSIFY(d1); s1 = (duk_small_int_t) DUK_SIGNBIT(d1); c2 = (duk_small_int_t) DUK_FPCLASSIFY(d2); s2 = (duk_small_int_t) DUK_SIGNBIT(d2); if (c1 == DUK_FP_NAN || c2 == DUK_FP_NAN) { goto lt_undefined; } if (c1 == DUK_FP_ZERO && c2 == DUK_FP_ZERO) { /* For all combinations: +0 < +0, +0 < -0, -0 < +0, -0 < -0, * steps e, f, and g. */ goto lt_false; } if (d1 == d2) { goto lt_false; } if (c1 == DUK_FP_INFINITE && s1 == 0) { /* x == +Infinity */ goto lt_false; } if (c2 == DUK_FP_INFINITE && s2 == 0) { /* y == +Infinity */ goto lt_true; } if (c2 == DUK_FP_INFINITE && s2 != 0) { /* y == -Infinity */ goto lt_false; } if (c1 == DUK_FP_INFINITE && s1 != 0) { /* x == -Infinity */ goto lt_true; } if (d1 < d2) { goto lt_true; } goto lt_false; } lt_undefined: /* Note: undefined from Section 11.8.5 always results in false * return (see e.g. Section 11.8.3) - hence special treatment here. */ retval = 0; goto cleanup; lt_true: if (flags & DUK_COMPARE_FLAG_NEGATE) { retval = 0; goto cleanup; } else { retval = 1; goto cleanup; } /* never here */ lt_false: if (flags & DUK_COMPARE_FLAG_NEGATE) { retval = 1; goto cleanup; } else { retval = 0; goto cleanup; } /* never here */ cleanup: duk_pop_2(ctx); return retval; }
int duk_repl_isinf(double x) { int c = DUK_FPCLASSIFY(x); return (c == DUK_FP_INFINITE); }
int duk_repl_isnan(double x) { int c = DUK_FPCLASSIFY(x); return (c == DUK_FP_NAN); }