Commit f5ea94da authored by floitschV8@gmail.com's avatar floitschV8@gmail.com

Simplify powers-of-ten cache.

The existing cache was build to support changes to fast-dtoa. This complicated the cache. Since the cached numbers are now in a .cc file they can be shared with other parts of v8.

Review URL: http://codereview.chromium.org/3608011

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@5606 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 4cd03a4f
......@@ -42,6 +42,7 @@ SOURCES = {
ast.cc
bootstrapper.cc
builtins.cc
cached-powers.cc
checks.cc
circular-queue.cc
code-stubs.cc
......
// Copyright 2006-2008 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <stdarg.h>
#include <limits.h>
#include "v8.h"
#include "cached-powers.h"
namespace v8 {
namespace internal {
struct CachedPower {
uint64_t significand;
int16_t binary_exponent;
int16_t decimal_exponent;
};
static const CachedPower kCachedPowers[] = {
{V8_2PART_UINT64_C(0xe61acf03, 3d1a45df), -1087, -308},
{V8_2PART_UINT64_C(0xab70fe17, c79ac6ca), -1060, -300},
{V8_2PART_UINT64_C(0xff77b1fc, bebcdc4f), -1034, -292},
{V8_2PART_UINT64_C(0xbe5691ef, 416bd60c), -1007, -284},
{V8_2PART_UINT64_C(0x8dd01fad, 907ffc3c), -980, -276},
{V8_2PART_UINT64_C(0xd3515c28, 31559a83), -954, -268},
{V8_2PART_UINT64_C(0x9d71ac8f, ada6c9b5), -927, -260},
{V8_2PART_UINT64_C(0xea9c2277, 23ee8bcb), -901, -252},
{V8_2PART_UINT64_C(0xaecc4991, 4078536d), -874, -244},
{V8_2PART_UINT64_C(0x823c1279, 5db6ce57), -847, -236},
{V8_2PART_UINT64_C(0xc2109436, 4dfb5637), -821, -228},
{V8_2PART_UINT64_C(0x9096ea6f, 3848984f), -794, -220},
{V8_2PART_UINT64_C(0xd77485cb, 25823ac7), -768, -212},
{V8_2PART_UINT64_C(0xa086cfcd, 97bf97f4), -741, -204},
{V8_2PART_UINT64_C(0xef340a98, 172aace5), -715, -196},
{V8_2PART_UINT64_C(0xb23867fb, 2a35b28e), -688, -188},
{V8_2PART_UINT64_C(0x84c8d4df, d2c63f3b), -661, -180},
{V8_2PART_UINT64_C(0xc5dd4427, 1ad3cdba), -635, -172},
{V8_2PART_UINT64_C(0x936b9fce, bb25c996), -608, -164},
{V8_2PART_UINT64_C(0xdbac6c24, 7d62a584), -582, -156},
{V8_2PART_UINT64_C(0xa3ab6658, 0d5fdaf6), -555, -148},
{V8_2PART_UINT64_C(0xf3e2f893, dec3f126), -529, -140},
{V8_2PART_UINT64_C(0xb5b5ada8, aaff80b8), -502, -132},
{V8_2PART_UINT64_C(0x87625f05, 6c7c4a8b), -475, -124},
{V8_2PART_UINT64_C(0xc9bcff60, 34c13053), -449, -116},
{V8_2PART_UINT64_C(0x964e858c, 91ba2655), -422, -108},
{V8_2PART_UINT64_C(0xdff97724, 70297ebd), -396, -100},
{V8_2PART_UINT64_C(0xa6dfbd9f, b8e5b88f), -369, -92},
{V8_2PART_UINT64_C(0xf8a95fcf, 88747d94), -343, -84},
{V8_2PART_UINT64_C(0xb9447093, 8fa89bcf), -316, -76},
{V8_2PART_UINT64_C(0x8a08f0f8, bf0f156b), -289, -68},
{V8_2PART_UINT64_C(0xcdb02555, 653131b6), -263, -60},
{V8_2PART_UINT64_C(0x993fe2c6, d07b7fac), -236, -52},
{V8_2PART_UINT64_C(0xe45c10c4, 2a2b3b06), -210, -44},
{V8_2PART_UINT64_C(0xaa242499, 697392d3), -183, -36},
{V8_2PART_UINT64_C(0xfd87b5f2, 8300ca0e), -157, -28},
{V8_2PART_UINT64_C(0xbce50864, 92111aeb), -130, -20},
{V8_2PART_UINT64_C(0x8cbccc09, 6f5088cc), -103, -12},
{V8_2PART_UINT64_C(0xd1b71758, e219652c), -77, -4},
{V8_2PART_UINT64_C(0x9c400000, 00000000), -50, 4},
{V8_2PART_UINT64_C(0xe8d4a510, 00000000), -24, 12},
{V8_2PART_UINT64_C(0xad78ebc5, ac620000), 3, 20},
{V8_2PART_UINT64_C(0x813f3978, f8940984), 30, 28},
{V8_2PART_UINT64_C(0xc097ce7b, c90715b3), 56, 36},
{V8_2PART_UINT64_C(0x8f7e32ce, 7bea5c70), 83, 44},
{V8_2PART_UINT64_C(0xd5d238a4, abe98068), 109, 52},
{V8_2PART_UINT64_C(0x9f4f2726, 179a2245), 136, 60},
{V8_2PART_UINT64_C(0xed63a231, d4c4fb27), 162, 68},
{V8_2PART_UINT64_C(0xb0de6538, 8cc8ada8), 189, 76},
{V8_2PART_UINT64_C(0x83c7088e, 1aab65db), 216, 84},
{V8_2PART_UINT64_C(0xc45d1df9, 42711d9a), 242, 92},
{V8_2PART_UINT64_C(0x924d692c, a61be758), 269, 100},
{V8_2PART_UINT64_C(0xda01ee64, 1a708dea), 295, 108},
{V8_2PART_UINT64_C(0xa26da399, 9aef774a), 322, 116},
{V8_2PART_UINT64_C(0xf209787b, b47d6b85), 348, 124},
{V8_2PART_UINT64_C(0xb454e4a1, 79dd1877), 375, 132},
{V8_2PART_UINT64_C(0x865b8692, 5b9bc5c2), 402, 140},
{V8_2PART_UINT64_C(0xc83553c5, c8965d3d), 428, 148},
{V8_2PART_UINT64_C(0x952ab45c, fa97a0b3), 455, 156},
{V8_2PART_UINT64_C(0xde469fbd, 99a05fe3), 481, 164},
{V8_2PART_UINT64_C(0xa59bc234, db398c25), 508, 172},
{V8_2PART_UINT64_C(0xf6c69a72, a3989f5c), 534, 180},
{V8_2PART_UINT64_C(0xb7dcbf53, 54e9bece), 561, 188},
{V8_2PART_UINT64_C(0x88fcf317, f22241e2), 588, 196},
{V8_2PART_UINT64_C(0xcc20ce9b, d35c78a5), 614, 204},
{V8_2PART_UINT64_C(0x98165af3, 7b2153df), 641, 212},
{V8_2PART_UINT64_C(0xe2a0b5dc, 971f303a), 667, 220},
{V8_2PART_UINT64_C(0xa8d9d153, 5ce3b396), 694, 228},
{V8_2PART_UINT64_C(0xfb9b7cd9, a4a7443c), 720, 236},
{V8_2PART_UINT64_C(0xbb764c4c, a7a44410), 747, 244},
{V8_2PART_UINT64_C(0x8bab8eef, b6409c1a), 774, 252},
{V8_2PART_UINT64_C(0xd01fef10, a657842c), 800, 260},
{V8_2PART_UINT64_C(0x9b10a4e5, e9913129), 827, 268},
{V8_2PART_UINT64_C(0xe7109bfb, a19c0c9d), 853, 276},
{V8_2PART_UINT64_C(0xac2820d9, 623bf429), 880, 284},
{V8_2PART_UINT64_C(0x80444b5e, 7aa7cf85), 907, 292},
{V8_2PART_UINT64_C(0xbf21e440, 03acdd2d), 933, 300},
{V8_2PART_UINT64_C(0x8e679c2f, 5e44ff8f), 960, 308},
{V8_2PART_UINT64_C(0xd433179d, 9c8cb841), 986, 316},
{V8_2PART_UINT64_C(0x9e19db92, b4e31ba9), 1013, 324},
{V8_2PART_UINT64_C(0xeb96bf6e, badf77d9), 1039, 332},
{V8_2PART_UINT64_C(0xaf87023b, 9bf0ee6b), 1066, 340},
};
static const int kCachedPowersLength = ARRAY_SIZE(kCachedPowers);
static const int kCachedPowersOffset = -kCachedPowers[0].decimal_exponent;
static const double kD_1_LOG2_10 = 0.30102999566398114; // 1 / lg(10)
static const int kCachedPowersDecimalDistance =
kCachedPowers[1].decimal_exponent - kCachedPowers[0].decimal_exponent;
void GetCachedPowerForBinaryExponentRange(int min_exponent,
int max_exponent,
DiyFp* power,
int* decimal_exponent) {
int kQ = DiyFp::kSignificandSize;
double k = ceiling((min_exponent + kQ - 1) * kD_1_LOG2_10);
int foo = kCachedPowersOffset;
int index =
(foo + static_cast<int>(k) - 1) / kCachedPowersDecimalDistance + 1;
ASSERT(0 <= index && index < kCachedPowersLength);
CachedPower cached_power = kCachedPowers[index];
ASSERT(min_exponent <= cached_power.binary_exponent);
ASSERT(cached_power.binary_exponent <= max_exponent);
*decimal_exponent = cached_power.decimal_exponent;
*power = DiyFp(cached_power.significand, cached_power.binary_exponent);
}
} } // namespace v8::internal
......@@ -33,86 +33,10 @@
namespace v8 {
namespace internal {
struct CachedPower {
uint64_t significand;
int16_t binary_exponent;
int16_t decimal_exponent;
};
// The following defines implement the interface between this file and the
// generated 'powers_ten.h'.
// GRISU_CACHE_NAME(1) contains all possible cached powers.
// GRISU_CACHE_NAME(i) contains GRISU_CACHE_NAME(1) where only every 'i'th
// element is kept. More formally GRISU_CACHE_NAME(i) contains the elements j*i
// with 0 <= j < k with k such that j*k < the size of GRISU_CACHE_NAME(1).
// The higher 'i' is the fewer elements we use.
// Given that there are less elements, the exponent-distance between two
// elements in the cache grows. The variable GRISU_CACHE_MAX_DISTANCE(i) stores
// the maximum distance between two elements.
#define GRISU_CACHE_STRUCT CachedPower
#define GRISU_CACHE_NAME(i) kCachedPowers##i
#define GRISU_CACHE_MAX_DISTANCE(i) kCachedPowersMaxDistance##i
#define GRISU_CACHE_OFFSET kCachedPowerOffset
#define GRISU_UINT64_C V8_2PART_UINT64_C
// The following include imports the precompiled cached powers.
#include "powers-ten.h" // NOLINT
static const double kD_1_LOG2_10 = 0.30102999566398114; // 1 / lg(10)
// We can't use a function since we reference variables depending on the 'i'.
// This way the compiler is able to see at compile time that only one
// cache-array variable is used and thus can remove all the others.
#define COMPUTE_FOR_CACHE(i) \
if (!found && (gamma - alpha + 1 >= GRISU_CACHE_MAX_DISTANCE(i))) { \
int kQ = DiyFp::kSignificandSize; \
double k = ceiling((alpha - e + kQ - 1) * kD_1_LOG2_10); \
int index = (GRISU_CACHE_OFFSET + static_cast<int>(k) - 1) / i + 1; \
cached_power = GRISU_CACHE_NAME(i)[index]; \
found = true; \
} \
static void GetCachedPower(int e, int alpha, int gamma, int* mk, DiyFp* c_mk) {
// The following if statement should be optimized by the compiler so that only
// one array is referenced and the others are not included in the object file.
bool found = false;
CachedPower cached_power;
COMPUTE_FOR_CACHE(20);
COMPUTE_FOR_CACHE(19);
COMPUTE_FOR_CACHE(18);
COMPUTE_FOR_CACHE(17);
COMPUTE_FOR_CACHE(16);
COMPUTE_FOR_CACHE(15);
COMPUTE_FOR_CACHE(14);
COMPUTE_FOR_CACHE(13);
COMPUTE_FOR_CACHE(12);
COMPUTE_FOR_CACHE(11);
COMPUTE_FOR_CACHE(10);
COMPUTE_FOR_CACHE(9);
COMPUTE_FOR_CACHE(8);
COMPUTE_FOR_CACHE(7);
COMPUTE_FOR_CACHE(6);
COMPUTE_FOR_CACHE(5);
COMPUTE_FOR_CACHE(4);
COMPUTE_FOR_CACHE(3);
COMPUTE_FOR_CACHE(2);
COMPUTE_FOR_CACHE(1);
if (!found) {
UNIMPLEMENTED();
// Silence compiler warnings.
cached_power.significand = 0;
cached_power.binary_exponent = 0;
cached_power.decimal_exponent = 0;
}
*c_mk = DiyFp(cached_power.significand, cached_power.binary_exponent);
*mk = cached_power.decimal_exponent;
ASSERT((alpha <= c_mk->e() + e) && (c_mk->e() + e <= gamma));
}
#undef GRISU_REDUCTION
#undef GRISU_CACHE_STRUCT
#undef GRISU_CACHE_NAME
#undef GRISU_CACHE_MAX_DISTANCE
#undef GRISU_CACHE_OFFSET
#undef GRISU_UINT64_C
void GetCachedPowerForBinaryExponentRange(int min_exponent,
int max_exponent,
DiyFp* power,
int* decimal_exponent);
} } // namespace v8::internal
......
......@@ -609,8 +609,13 @@ static bool Grisu3(double v,
ASSERT(boundary_plus.e() == w.e());
DiyFp ten_mk; // Cached power of ten: 10^-k
int mk; // -k
GetCachedPower(w.e() + DiyFp::kSignificandSize, kMinimalTargetExponent,
kMaximalTargetExponent, &mk, &ten_mk);
int ten_mk_minimal_binary_exponent =
kMinimalTargetExponent - (w.e() + DiyFp::kSignificandSize);
int ten_mk_maximal_binary_exponent =
kMaximalTargetExponent - (w.e() + DiyFp::kSignificandSize);
GetCachedPowerForBinaryExponentRange(ten_mk_minimal_binary_exponent,
ten_mk_maximal_binary_exponent,
&ten_mk, &mk);
ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
DiyFp::kSignificandSize) &&
(kMaximalTargetExponent >= w.e() + ten_mk.e() +
......@@ -662,8 +667,13 @@ static bool Grisu3Counted(double v,
DiyFp w = Double(v).AsNormalizedDiyFp();
DiyFp ten_mk; // Cached power of ten: 10^-k
int mk; // -k
GetCachedPower(w.e() + DiyFp::kSignificandSize, kMinimalTargetExponent,
kMaximalTargetExponent, &mk, &ten_mk);
int ten_mk_minimal_binary_exponent =
kMinimalTargetExponent - (w.e() + DiyFp::kSignificandSize);
int ten_mk_maximal_binary_exponent =
kMaximalTargetExponent - (w.e() + DiyFp::kSignificandSize);
GetCachedPowerForBinaryExponentRange(ten_mk_minimal_binary_exponent,
ten_mk_maximal_binary_exponent,
&ten_mk, &mk);
ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
DiyFp::kSignificandSize) &&
(kMaximalTargetExponent >= w.e() + ten_mk.e() +
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -285,6 +285,7 @@
'../../src/builtins.cc',
'../../src/builtins.h',
'../../src/bytecodes-irregexp.h',
'../../src/cached-powers.cc',
'../../src/cached-powers.h',
'../../src/char-predicates-inl.h',
'../../src/char-predicates.h',
......@@ -402,7 +403,6 @@
'../../src/parser.cc',
'../../src/parser.h',
'../../src/platform.h',
'../../src/powers-ten.h',
'../../src/prettyprinter.cc',
'../../src/prettyprinter.h',
'../../src/property.cc',
......
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