Browse Source

Make ES6 math funcs conditional, log constants

pull/1095/head
Sami Vaarala 8 years ago
parent
commit
67101ff313
  1. 15
      src-input/duk_bi_math.c
  2. 9
      src-input/duk_util.h

15
src-input/duk_bi_math.c

@ -90,6 +90,7 @@ DUK_LOCAL double duk__fmax_fixed(double x, double y) {
return duk_double_fmax(x, y);
}
#if defined(DUK_USE_ES6)
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
@ -118,7 +119,7 @@ DUK_LOCAL double duk__log2(double x) {
#if defined(DUK_LOG2)
return DUK_LOG2(x);
#else
return DUK_LOG(x) / DUK_LOG(2.0);
return DUK_LOG(x) * DUK_DOUBLE_LOG2E;
#endif
}
@ -126,7 +127,7 @@ DUK_LOCAL double duk__log10(double x) {
#if defined(DUK_LOG10)
return DUK_LOG10(x);
#else
return DUK_LOG(x) / DUK_LOG(10.0);
return DUK_LOG(x) * DUK_DOUBLE_LOG10E;
#endif
}
@ -134,9 +135,13 @@ DUK_LOCAL double duk__trunc(double x) {
#if defined(DUK_TRUNC)
return DUK_TRUNC(x);
#else
/* Handles -0 correctly: -0.0 matches 'x >= 0.0' but floor()
* is required to return -0 when the argument is -0.
*/
return x >= 0.0 ? DUK_FLOOR(x) : DUK_CEIL(x);
#endif
}
#endif /* DUK_USE_ES6 */
DUK_LOCAL double duk__round_fixed(double x) {
/* Numbers half-way between integers must be rounded towards +Infinity,
@ -241,11 +246,13 @@ DUK_LOCAL const duk__one_arg_func duk__one_arg_funcs[] = {
duk__sin,
duk__sqrt,
duk__tan,
#if defined(DUK_USE_ES6)
duk__cbrt,
duk__log2,
duk__log10,
duk__trunc
#else
#endif
#else /* DUK_USE_AVOID_PLATFORM_FUNCPTRS */
DUK_FABS,
DUK_ACOS,
DUK_ASIN,
@ -259,11 +266,13 @@ DUK_LOCAL const duk__one_arg_func duk__one_arg_funcs[] = {
DUK_SIN,
DUK_SQRT,
DUK_TAN,
#if defined(DUK_USE_ES6)
duk__cbrt,
duk__log2,
duk__log10,
duk__trunc
#endif
#endif /* DUK_USE_AVOID_PLATFORM_FUNCPTRS */
};
/* order must match constants in genbuiltins.py */

9
src-input/duk_util.h

@ -15,6 +15,15 @@
#define DUK_UTIL_GET_RANDOM_DOUBLE(thr) duk_util_tinyrandom_get_double(thr)
#endif
/*
* Some useful constants
*/
#define DUK_DOUBLE_2TO32 4294967296.0
#define DUK_DOUBLE_2TO31 2147483648.0
#define DUK_DOUBLE_LOG2E 1.4426950408889634
#define DUK_DOUBLE_LOG10E 0.4342944819032518
/*
* Endian conversion
*/

Loading…
Cancel
Save