Commit 3e98f04d authored by yangguo's avatar yangguo Committed by Commit bot

Use inline constants instead of typed array for math constants.

R=bmeurer@chromium.org

Review URL: https://codereview.chromium.org/1425333002

Cr-Commit-Position: refs/heads/master@{#31710}
parent 2210cc84
......@@ -1811,7 +1811,6 @@ Handle<JSTypedArray> CreateTypedArray(Isolate* isolate, ExternalArrayType type,
bool Genesis::InitializeBuiltinTypedArrays() {
HandleScope scope(isolate());
Handle<JSTypedArray> rng_state;
Handle<JSTypedArray> math_constants;
Handle<JSTypedArray> rempio2result;
{
......@@ -1826,13 +1825,6 @@ bool Genesis::InitializeBuiltinTypedArrays() {
} while (state[0] == 0 || state[1] == 0);
}
{ // Initialize trigonometric lookup tables and constants.
const size_t num_elements = arraysize(fdlibm::MathConstants::constants);
double* constants = const_cast<double*>(fdlibm::MathConstants::constants);
math_constants = CreateTypedArray(isolate(), kExternalFloat64Array,
num_elements, &constants);
}
{ // Initialize a result array for rempio2 calculation
const size_t num_elements = 2;
double* data = NULL;
......@@ -1847,7 +1839,7 @@ bool Genesis::InitializeBuiltinTypedArrays() {
"InitializeBuiltinTypedArrays");
Handle<Object> fun = JSObject::GetDataProperty(utils, name_string);
Handle<Object> receiver = isolate()->factory()->undefined_value();
Handle<Object> args[] = {utils, rng_state, math_constants, rempio2result};
Handle<Object> args[] = {utils, rng_state, rempio2result};
return !Execution::Call(isolate(), fun, receiver, arraysize(args), args)
.is_null();
}
......
......@@ -272,12 +272,11 @@ function PostDebug(utils) {
}
function InitializeBuiltinTypedArrays(
utils, rng_state, math_constants, rempio2result) {
function InitializeBuiltinTypedArrays(utils, rng_state, rempio2result) {
var setup_list = typed_array_setup;
for ( ; !IS_UNDEFINED(setup_list); setup_list = setup_list.next) {
setup_list(rng_state, math_constants, rempio2result);
setup_list(rng_state, rempio2result);
}
}
......
......@@ -29,75 +29,6 @@ namespace fdlibm {
inline double scalbn(double x, int y) { return _scalb(x, y); }
#endif // _MSC_VER
const double MathConstants::constants[] = {
6.36619772367581382433e-01, // invpio2 0
1.57079632673412561417e+00, // pio2_1 1
6.07710050650619224932e-11, // pio2_1t 2
6.07710050630396597660e-11, // pio2_2 3
2.02226624879595063154e-21, // pio2_2t 4
2.02226624871116645580e-21, // pio2_3 5
8.47842766036889956997e-32, // pio2_3t 6
-1.66666666666666324348e-01, // S1 7 coefficients for sin
8.33333333332248946124e-03, // 8
-1.98412698298579493134e-04, // 9
2.75573137070700676789e-06, // 10
-2.50507602534068634195e-08, // 11
1.58969099521155010221e-10, // S6 12
4.16666666666666019037e-02, // C1 13 coefficients for cos
-1.38888888888741095749e-03, // 14
2.48015872894767294178e-05, // 15
-2.75573143513906633035e-07, // 16
2.08757232129817482790e-09, // 17
-1.13596475577881948265e-11, // C6 18
3.33333333333334091986e-01, // T0 19 coefficients for tan
1.33333333333201242699e-01, // 20
5.39682539762260521377e-02, // 21
2.18694882948595424599e-02, // 22
8.86323982359930005737e-03, // 23
3.59207910759131235356e-03, // 24
1.45620945432529025516e-03, // 25
5.88041240820264096874e-04, // 26
2.46463134818469906812e-04, // 27
7.81794442939557092300e-05, // 28
7.14072491382608190305e-05, // 29
-1.85586374855275456654e-05, // 30
2.59073051863633712884e-05, // T12 31
7.85398163397448278999e-01, // pio4 32
3.06161699786838301793e-17, // pio4lo 33
6.93147180369123816490e-01, // ln2_hi 34
1.90821492927058770002e-10, // ln2_lo 35
6.666666666666666666e-01, // 2/3 36
6.666666666666735130e-01, // LP1 37 coefficients for log1p
3.999999999940941908e-01, // 38
2.857142874366239149e-01, // 39
2.222219843214978396e-01, // 40
1.818357216161805012e-01, // 41
1.531383769920937332e-01, // 42
1.479819860511658591e-01, // LP7 43
7.09782712893383973096e+02, // 44 overflow threshold for expm1
1.44269504088896338700e+00, // 1/ln2 45
-3.33333333333331316428e-02, // Q1 46 coefficients for expm1
1.58730158725481460165e-03, // 47
-7.93650757867487942473e-05, // 48
4.00821782732936239552e-06, // 49
-2.01099218183624371326e-07, // Q5 50
710.4758600739439, // 51 overflow threshold sinh, cosh
4.34294481903251816668e-01, // ivln10 52 coefficients for log10
3.01029995663611771306e-01, // log10_2hi 53
3.69423907715893078616e-13, // log10_2lo 54
5.99999999999994648725e-01, // L1 55 coefficients for log2
4.28571428578550184252e-01, // 56
3.33333329818377432918e-01, // 57
2.72728123808534006489e-01, // 58
2.30660745775561754067e-01, // 59
2.06975017800338417784e-01, // L6 60
9.61796693925975554329e-01, // cp 61 2/(3*ln(2))
9.61796700954437255859e-01, // cp_h 62
-7.02846165095275826516e-09, // cp_l 63
5.84962487220764160156e-01, // dp_h 64
1.35003920212974897128e-08 // dp_l 65
};
// Table of constants for 2/pi, 396 Hex digits (476 decimal) of 2/pi
static const int two_over_pi[] = {
......
......@@ -21,10 +21,6 @@ namespace fdlibm {
int rempio2(double x, double* y);
// Constants to be exposed to builtins via Float64Array.
struct MathConstants {
static const double constants[66];
};
} // namespace internal
} // namespace v8
......
......@@ -16,10 +16,6 @@
// The following is a straightforward translation of fdlibm routines
// by Raymond Toy (rtoy@google.com).
// Double constants that do not have empty lower 32 bits are found in fdlibm.cc
// and exposed through kMath as typed array. We assume the compiler to convert
// from decimal to binary accurately enough to produce the intended values.
// kMath is initialized to a Float64Array during genesis and not writable.
// rempio2result is used as a container for return values of %RemPiO2. It is
// initialized to a two-element Float64Array during genesis.
......@@ -33,7 +29,6 @@
// Imports
var GlobalMath = global.Math;
var kMath;
var MathAbs;
var MathExp;
var NaN = %GetRootNaN();
......@@ -44,22 +39,21 @@ utils.Import(function(from) {
MathExp = from.MathExp;
});
utils.SetupTypedArray(function(arg1, arg2, arg3) {
kMath = arg2;
rempio2result = arg3;
utils.SetupTypedArray(function(arg1, arg2) {
rempio2result = arg2;
});
// -------------------------------------------------------------------
define INVPIO2 = kMath[0];
define PIO2_1 = kMath[1];
define PIO2_1T = kMath[2];
define PIO2_2 = kMath[3];
define PIO2_2T = kMath[4];
define PIO2_3 = kMath[5];
define PIO2_3T = kMath[6];
define PIO4 = kMath[32];
define PIO4LO = kMath[33];
define INVPIO2 = 6.36619772367581382433e-01;
define PIO2_1 = 1.57079632673412561417;
define PIO2_1T = 6.07710050650619224932e-11;
define PIO2_2 = 6.07710050630396597660e-11;
define PIO2_2T = 2.02226624879595063154e-21;
define PIO2_3 = 2.02226624871116645580e-21;
define PIO2_3T = 8.47842766036889956997e-32;
define PIO4 = 7.85398163397448278999e-01;
define PIO4LO = 3.06161699786838301793e-17;
// Compute k and r such that x - k*pi/2 = r where |r| < pi/4. For
// precision, r is returned as two values y0 and y1 such that r = y0 + y1
......@@ -271,9 +265,19 @@ endmacro
// Set returnTan to 1 for tan; -1 for cot. Anything else is illegal
// and will cause incorrect results.
//
macro KTAN(x)
kMath[19+x]
endmacro
define T00 = 3.33333333333334091986e-01;
define T01 = 1.33333333333201242699e-01;
define T02 = 5.39682539762260521377e-02;
define T03 = 2.18694882948595424599e-02;
define T04 = 8.86323982359930005737e-03;
define T05 = 3.59207910759131235356e-03;
define T06 = 1.45620945432529025516e-03;
define T07 = 5.88041240820264096874e-04;
define T08 = 2.46463134818469906812e-04;
define T09 = 7.81794442939557092300e-05;
define T10 = 7.14072491382608190305e-05;
define T11 = -1.85586374855275456654e-05;
define T12 = 2.59073051863633712884e-05;
function KernelTan(x, y, returnTan) {
var z;
......@@ -316,13 +320,13 @@ function KernelTan(x, y, returnTan) {
// Break x^5 * (T1 + x^2*T2 + ...) into
// x^5 * (T1 + x^4*T3 + ... + x^20*T11) +
// x^5 * (x^2 * (T2 + x^4*T4 + ... + x^22*T12))
var r = KTAN(1) + w * (KTAN(3) + w * (KTAN(5) +
w * (KTAN(7) + w * (KTAN(9) + w * KTAN(11)))));
var v = z * (KTAN(2) + w * (KTAN(4) + w * (KTAN(6) +
w * (KTAN(8) + w * (KTAN(10) + w * KTAN(12))))));
var r = T01 + w * (T03 + w * (T05 +
w * (T07 + w * (T09 + w * T11))));
var v = z * (T02 + w * (T04 + w * (T06 +
w * (T08 + w * (T10 + w * T12)))));
var s = z * x;
r = y + z * (s * (r + v) + y);
r = r + KTAN(0) * s;
r = r + T00 * s;
w = x + r;
if (ix >= 0x3fe59428) {
return (1 - ((hx >> 30) & 2)) *
......@@ -455,12 +459,17 @@ function MathTan(x) {
//
// See HP-15C Advanced Functions Handbook, p.193.
//
define LN2_HI = kMath[34];
define LN2_LO = kMath[35];
define TWO_THIRD = kMath[36];
macro KLOG1P(x)
(kMath[37+x])
endmacro
define LN2_HI = 6.93147180369123816490e-01;
define LN2_LO = 1.90821492927058770002e-10;
define TWO_THIRD = 6.666666666666666666e-01;
define LP1 = 6.666666666666735130e-01;
define LP2 = 3.999999999940941908e-01;
define LP3 = 2.857142874366239149e-01;
define LP4 = 2.222219843214978396e-01;
define LP5 = 1.818357216161805012e-01;
define LP6 = 1.531383769920937332e-01;
define LP7 = 1.479819860511658591e-01;
// 2^54
define TWO54 = 18014398509481984;
......@@ -542,9 +551,8 @@ function MathLog1p(x) {
var s = f / (2 + f);
var z = s * s;
var R = z * (KLOG1P(0) + z * (KLOG1P(1) + z *
(KLOG1P(2) + z * (KLOG1P(3) + z *
(KLOG1P(4) + z * (KLOG1P(5) + z * KLOG1P(6)))))));
var R = z * (LP1 + z * (LP2 + z * (LP3 + z * (LP4 +
z * (LP5 + z * (LP6 + z * LP7))))));
if (k === 0) {
return f - (hfsq - s * (hfsq + R));
} else {
......@@ -641,11 +649,13 @@ function MathLog1p(x) {
// For IEEE double
// if x > 7.09782712893383973096e+02 then expm1(x) overflow
//
define KEXPM1_OVERFLOW = kMath[44];
define INVLN2 = kMath[45];
macro KEXPM1(x)
(kMath[46+x])
endmacro
define KEXPM1_OVERFLOW = 7.09782712893383973096e+02;
define INVLN2 = 1.44269504088896338700;
define EXPM1_1 = -3.33333333333331316428e-02;
define EXPM1_2 = 1.58730158725481460165e-03;
define EXPM1_3 = -7.93650757867487942473e-05;
define EXPM1_4 = 4.00821782732936239552e-06;
define EXPM1_5 = -2.01099218183624371326e-07;
function MathExpm1(x) {
x = x * 1; // Convert to number.
......@@ -705,8 +715,8 @@ function MathExpm1(x) {
// x is now in primary range
var hfx = 0.5 * x;
var hxs = x * hfx;
var r1 = 1 + hxs * (KEXPM1(0) + hxs * (KEXPM1(1) + hxs *
(KEXPM1(2) + hxs * (KEXPM1(3) + hxs * KEXPM1(4)))));
var r1 = 1 + hxs * (EXPM1_1 + hxs * (EXPM1_2 + hxs *
(EXPM1_3 + hxs * (EXPM1_4 + hxs * EXPM1_5))));
t = 3 - r1 * hfx;
var e = hxs * ((r1 - t) / (6 - x * t));
if (k === 0) { // c is 0
......@@ -764,7 +774,7 @@ function MathExpm1(x) {
// sinh(x) is |x| if x is +Infinity, -Infinity, or NaN.
// only sinh(0)=0 is exact for finite x.
//
define KSINH_OVERFLOW = kMath[51];
define KSINH_OVERFLOW = 710.4758600739439;
define TWO_M28 = 3.725290298461914e-9; // 2^-28, empty lower half
define LOG_MAXD = 709.7822265625; // 0x40862e42 00000000, empty lower half
......@@ -816,7 +826,7 @@ function MathSinh(x) {
// cosh(x) is |x| if x is +INF, -INF, or NaN.
// only cosh(0)=1 is exact for finite x.
//
define KCOSH_OVERFLOW = kMath[51];
define KCOSH_OVERFLOW = 710.4758600739439;
function MathCosh(x) {
x = x * 1; // Convert to number.
......@@ -931,9 +941,9 @@ function MathTanh(x) {
// log10(10**N) = N for N=0,1,...,22.
//
define IVLN10 = kMath[52];
define LOG10_2HI = kMath[53];
define LOG10_2LO = kMath[54];
define IVLN10 = 4.34294481903251816668e-01;
define LOG10_2HI = 3.01029995663611771306e-01;
define LOG10_2LO = 3.69423907715893078616e-13;
function MathLog10(x) {
x = x * 1; // Convert to number.
......@@ -981,18 +991,21 @@ function MathLog10(x) {
// log2(x) = w1 + w2
// where w1 has 53-24 = 29 bits of trailing zeroes.
define DP_H = kMath[64];
define DP_L = kMath[65];
define DP_H = 5.84962487220764160156e-01;
define DP_L = 1.35003920212974897128e-08;
// Polynomial coefficients for (3/2)*(log2(x) - 2*s - 2/3*s^3)
macro KLOG2(x)
(kMath[55+x])
endmacro
define LOG2_1 = 5.99999999999994648725e-01;
define LOG2_2 = 4.28571428578550184252e-01;
define LOG2_3 = 3.33333329818377432918e-01;
define LOG2_4 = 2.72728123808534006489e-01;
define LOG2_5 = 2.30660745775561754067e-01;
define LOG2_6 = 2.06975017800338417784e-01;
// cp = 2/(3*ln(2)). Note that cp_h + cp_l is cp, but with more accuracy.
define CP = kMath[61];
define CP_H = kMath[62];
define CP_L = kMath[63];
define CP = 9.61796693925975554329e-01;
define CP_H = 9.61796700954437255859e-01;
define CP_L = -7.02846165095275826516e-09;
// 2^53
define TWO53 = 9007199254740992;
......@@ -1057,8 +1070,8 @@ function MathLog2(x) {
// Compute log2(ax)
var s2 = ss * ss;
var r = s2 * s2 * (KLOG2(0) + s2 * (KLOG2(1) + s2 * (KLOG2(2) + s2 * (
KLOG2(3) + s2 * (KLOG2(4) + s2 * KLOG2(5))))));
var r = s2 * s2 * (LOG2_1 + s2 * (LOG2_2 + s2 * (LOG2_3 + s2 * (
LOG2_4 + s2 * (LOG2_5 + s2 * LOG2_6)))));
r += s_l * (s_h + ss);
s2 = s_h * s_h;
t_h = %_ConstructDouble(%_DoubleHi(3.0 + s2 + r), 0);
......
......@@ -242,7 +242,7 @@ def ExpandInlineMacros(lines):
lines = ExpandMacroDefinition(lines, pos, name_pattern, macro, non_expander)
INLINE_CONSTANT_PATTERN = re.compile(r'define\s+([a-zA-Z0-9_]+)\s*=\s*([^;\n]+)[;\n]')
INLINE_CONSTANT_PATTERN = re.compile(r'define\s+([a-zA-Z0-9_]+)\s*=\s*([^;\n]+);\n')
def ExpandInlineConstants(lines):
pos = 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment