mirror of https://github.com/lukechilds/node.git
Ryan Dahl
14 years ago
147 changed files with 11695 additions and 4126 deletions
@ -0,0 +1,53 @@ |
|||||
|
// Copyright 2010 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.
|
||||
|
|
||||
|
// Load definitions of standard types.
|
||||
|
|
||||
|
#ifndef V8STDINT_H_ |
||||
|
#define V8STDINT_H_ |
||||
|
|
||||
|
#include <stdio.h> |
||||
|
|
||||
|
#if defined(_WIN32) && !defined(__MINGW32__) |
||||
|
|
||||
|
typedef signed char int8_t; |
||||
|
typedef unsigned char uint8_t; |
||||
|
typedef short int16_t; // NOLINT
|
||||
|
typedef unsigned short uint16_t; // NOLINT
|
||||
|
typedef int int32_t; |
||||
|
typedef unsigned int uint32_t; |
||||
|
typedef __int64 int64_t; |
||||
|
typedef unsigned __int64 uint64_t; |
||||
|
// intptr_t and friends are defined in crtdefs.h through stdio.h.
|
||||
|
|
||||
|
#else |
||||
|
|
||||
|
#include <stdint.h> |
||||
|
|
||||
|
#endif |
||||
|
|
||||
|
#endif // V8STDINT_H_
|
@ -0,0 +1,655 @@ |
|||||
|
// Copyright 2010 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 <math.h> |
||||
|
|
||||
|
#include "v8.h" |
||||
|
#include "bignum-dtoa.h" |
||||
|
|
||||
|
#include "bignum.h" |
||||
|
#include "double.h" |
||||
|
|
||||
|
namespace v8 { |
||||
|
namespace internal { |
||||
|
|
||||
|
static int NormalizedExponent(uint64_t significand, int exponent) { |
||||
|
ASSERT(significand != 0); |
||||
|
while ((significand & Double::kHiddenBit) == 0) { |
||||
|
significand = significand << 1; |
||||
|
exponent = exponent - 1; |
||||
|
} |
||||
|
return exponent; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
// Forward declarations:
|
||||
|
// Returns an estimation of k such that 10^(k-1) <= v < 10^k.
|
||||
|
static int EstimatePower(int exponent); |
||||
|
// Computes v / 10^estimated_power exactly, as a ratio of two bignums, numerator
|
||||
|
// and denominator.
|
||||
|
static void InitialScaledStartValues(double v, |
||||
|
int estimated_power, |
||||
|
bool need_boundary_deltas, |
||||
|
Bignum* numerator, |
||||
|
Bignum* denominator, |
||||
|
Bignum* delta_minus, |
||||
|
Bignum* delta_plus); |
||||
|
// Multiplies numerator/denominator so that its values lies in the range 1-10.
|
||||
|
// Returns decimal_point s.t.
|
||||
|
// v = numerator'/denominator' * 10^(decimal_point-1)
|
||||
|
// where numerator' and denominator' are the values of numerator and
|
||||
|
// denominator after the call to this function.
|
||||
|
static void FixupMultiply10(int estimated_power, bool is_even, |
||||
|
int* decimal_point, |
||||
|
Bignum* numerator, Bignum* denominator, |
||||
|
Bignum* delta_minus, Bignum* delta_plus); |
||||
|
// Generates digits from the left to the right and stops when the generated
|
||||
|
// digits yield the shortest decimal representation of v.
|
||||
|
static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator, |
||||
|
Bignum* delta_minus, Bignum* delta_plus, |
||||
|
bool is_even, |
||||
|
Vector<char> buffer, int* length); |
||||
|
// Generates 'requested_digits' after the decimal point.
|
||||
|
static void BignumToFixed(int requested_digits, int* decimal_point, |
||||
|
Bignum* numerator, Bignum* denominator, |
||||
|
Vector<char>(buffer), int* length); |
||||
|
// Generates 'count' digits of numerator/denominator.
|
||||
|
// Once 'count' digits have been produced rounds the result depending on the
|
||||
|
// remainder (remainders of exactly .5 round upwards). Might update the
|
||||
|
// decimal_point when rounding up (for example for 0.9999).
|
||||
|
static void GenerateCountedDigits(int count, int* decimal_point, |
||||
|
Bignum* numerator, Bignum* denominator, |
||||
|
Vector<char>(buffer), int* length); |
||||
|
|
||||
|
|
||||
|
void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits, |
||||
|
Vector<char> buffer, int* length, int* decimal_point) { |
||||
|
ASSERT(v > 0); |
||||
|
ASSERT(!Double(v).IsSpecial()); |
||||
|
uint64_t significand = Double(v).Significand(); |
||||
|
bool is_even = (significand & 1) == 0; |
||||
|
int exponent = Double(v).Exponent(); |
||||
|
int normalized_exponent = NormalizedExponent(significand, exponent); |
||||
|
// estimated_power might be too low by 1.
|
||||
|
int estimated_power = EstimatePower(normalized_exponent); |
||||
|
|
||||
|
// Shortcut for Fixed.
|
||||
|
// The requested digits correspond to the digits after the point. If the
|
||||
|
// number is much too small, then there is no need in trying to get any
|
||||
|
// digits.
|
||||
|
if (mode == BIGNUM_DTOA_FIXED && -estimated_power - 1 > requested_digits) { |
||||
|
buffer[0] = '\0'; |
||||
|
*length = 0; |
||||
|
// Set decimal-point to -requested_digits. This is what Gay does.
|
||||
|
// Note that it should not have any effect anyways since the string is
|
||||
|
// empty.
|
||||
|
*decimal_point = -requested_digits; |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
Bignum numerator; |
||||
|
Bignum denominator; |
||||
|
Bignum delta_minus; |
||||
|
Bignum delta_plus; |
||||
|
// Make sure the bignum can grow large enough. The smallest double equals
|
||||
|
// 4e-324. In this case the denominator needs fewer than 324*4 binary digits.
|
||||
|
// The maximum double is 1.7976931348623157e308 which needs fewer than
|
||||
|
// 308*4 binary digits.
|
||||
|
ASSERT(Bignum::kMaxSignificantBits >= 324*4); |
||||
|
bool need_boundary_deltas = (mode == BIGNUM_DTOA_SHORTEST); |
||||
|
InitialScaledStartValues(v, estimated_power, need_boundary_deltas, |
||||
|
&numerator, &denominator, |
||||
|
&delta_minus, &delta_plus); |
||||
|
// We now have v = (numerator / denominator) * 10^estimated_power.
|
||||
|
FixupMultiply10(estimated_power, is_even, decimal_point, |
||||
|
&numerator, &denominator, |
||||
|
&delta_minus, &delta_plus); |
||||
|
// We now have v = (numerator / denominator) * 10^(decimal_point-1), and
|
||||
|
// 1 <= (numerator + delta_plus) / denominator < 10
|
||||
|
switch (mode) { |
||||
|
case BIGNUM_DTOA_SHORTEST: |
||||
|
GenerateShortestDigits(&numerator, &denominator, |
||||
|
&delta_minus, &delta_plus, |
||||
|
is_even, buffer, length); |
||||
|
break; |
||||
|
case BIGNUM_DTOA_FIXED: |
||||
|
BignumToFixed(requested_digits, decimal_point, |
||||
|
&numerator, &denominator, |
||||
|
buffer, length); |
||||
|
break; |
||||
|
case BIGNUM_DTOA_PRECISION: |
||||
|
GenerateCountedDigits(requested_digits, decimal_point, |
||||
|
&numerator, &denominator, |
||||
|
buffer, length); |
||||
|
break; |
||||
|
default: |
||||
|
UNREACHABLE(); |
||||
|
} |
||||
|
buffer[*length] = '\0'; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
// The procedure starts generating digits from the left to the right and stops
|
||||
|
// when the generated digits yield the shortest decimal representation of v. A
|
||||
|
// decimal representation of v is a number lying closer to v than to any other
|
||||
|
// double, so it converts to v when read.
|
||||
|
//
|
||||
|
// This is true if d, the decimal representation, is between m- and m+, the
|
||||
|
// upper and lower boundaries. d must be strictly between them if !is_even.
|
||||
|
// m- := (numerator - delta_minus) / denominator
|
||||
|
// m+ := (numerator + delta_plus) / denominator
|
||||
|
//
|
||||
|
// Precondition: 0 <= (numerator+delta_plus) / denominator < 10.
|
||||
|
// If 1 <= (numerator+delta_plus) / denominator < 10 then no leading 0 digit
|
||||
|
// will be produced. This should be the standard precondition.
|
||||
|
static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator, |
||||
|
Bignum* delta_minus, Bignum* delta_plus, |
||||
|
bool is_even, |
||||
|
Vector<char> buffer, int* length) { |
||||
|
// Small optimization: if delta_minus and delta_plus are the same just reuse
|
||||
|
// one of the two bignums.
|
||||
|
if (Bignum::Equal(*delta_minus, *delta_plus)) { |
||||
|
delta_plus = delta_minus; |
||||
|
} |
||||
|
*length = 0; |
||||
|
while (true) { |
||||
|
uint16_t digit; |
||||
|
digit = numerator->DivideModuloIntBignum(*denominator); |
||||
|
ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive.
|
||||
|
// digit = numerator / denominator (integer division).
|
||||
|
// numerator = numerator % denominator.
|
||||
|
buffer[(*length)++] = digit + '0'; |
||||
|
|
||||
|
// Can we stop already?
|
||||
|
// If the remainder of the division is less than the distance to the lower
|
||||
|
// boundary we can stop. In this case we simply round down (discarding the
|
||||
|
// remainder).
|
||||
|
// Similarly we test if we can round up (using the upper boundary).
|
||||
|
bool in_delta_room_minus; |
||||
|
bool in_delta_room_plus; |
||||
|
if (is_even) { |
||||
|
in_delta_room_minus = Bignum::LessEqual(*numerator, *delta_minus); |
||||
|
} else { |
||||
|
in_delta_room_minus = Bignum::Less(*numerator, *delta_minus); |
||||
|
} |
||||
|
if (is_even) { |
||||
|
in_delta_room_plus = |
||||
|
Bignum::PlusCompare(*numerator, *delta_plus, *denominator) >= 0; |
||||
|
} else { |
||||
|
in_delta_room_plus = |
||||
|
Bignum::PlusCompare(*numerator, *delta_plus, *denominator) > 0; |
||||
|
} |
||||
|
if (!in_delta_room_minus && !in_delta_room_plus) { |
||||
|
// Prepare for next iteration.
|
||||
|
numerator->Times10(); |
||||
|
delta_minus->Times10(); |
||||
|
// We optimized delta_plus to be equal to delta_minus (if they share the
|
||||
|
// same value). So don't multiply delta_plus if they point to the same
|
||||
|
// object.
|
||||
|
if (delta_minus != delta_plus) { |
||||
|
delta_plus->Times10(); |
||||
|
} |
||||
|
} else if (in_delta_room_minus && in_delta_room_plus) { |
||||
|
// Let's see if 2*numerator < denominator.
|
||||
|
// If yes, then the next digit would be < 5 and we can round down.
|
||||
|
int compare = Bignum::PlusCompare(*numerator, *numerator, *denominator); |
||||
|
if (compare < 0) { |
||||
|
// Remaining digits are less than .5. -> Round down (== do nothing).
|
||||
|
} else if (compare > 0) { |
||||
|
// Remaining digits are more than .5 of denominator. -> Round up.
|
||||
|
// Note that the last digit could not be a '9' as otherwise the whole
|
||||
|
// loop would have stopped earlier.
|
||||
|
// We still have an assert here in case the preconditions were not
|
||||
|
// satisfied.
|
||||
|
ASSERT(buffer[(*length) - 1] != '9'); |
||||
|
buffer[(*length) - 1]++; |
||||
|
} else { |
||||
|
// Halfway case.
|
||||
|
// TODO(floitsch): need a way to solve half-way cases.
|
||||
|
// For now let's round towards even (since this is what Gay seems to
|
||||
|
// do).
|
||||
|
|
||||
|
if ((buffer[(*length) - 1] - '0') % 2 == 0) { |
||||
|
// Round down => Do nothing.
|
||||
|
} else { |
||||
|
ASSERT(buffer[(*length) - 1] != '9'); |
||||
|
buffer[(*length) - 1]++; |
||||
|
} |
||||
|
} |
||||
|
return; |
||||
|
} else if (in_delta_room_minus) { |
||||
|
// Round down (== do nothing).
|
||||
|
return; |
||||
|
} else { // in_delta_room_plus
|
||||
|
// Round up.
|
||||
|
// Note again that the last digit could not be '9' since this would have
|
||||
|
// stopped the loop earlier.
|
||||
|
// We still have an ASSERT here, in case the preconditions were not
|
||||
|
// satisfied.
|
||||
|
ASSERT(buffer[(*length) -1] != '9'); |
||||
|
buffer[(*length) - 1]++; |
||||
|
return; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
// Let v = numerator / denominator < 10.
|
||||
|
// Then we generate 'count' digits of d = x.xxxxx... (without the decimal point)
|
||||
|
// from left to right. Once 'count' digits have been produced we decide wether
|
||||
|
// to round up or down. Remainders of exactly .5 round upwards. Numbers such
|
||||
|
// as 9.999999 propagate a carry all the way, and change the
|
||||
|
// exponent (decimal_point), when rounding upwards.
|
||||
|
static void GenerateCountedDigits(int count, int* decimal_point, |
||||
|
Bignum* numerator, Bignum* denominator, |
||||
|
Vector<char>(buffer), int* length) { |
||||
|
ASSERT(count >= 0); |
||||
|
for (int i = 0; i < count - 1; ++i) { |
||||
|
uint16_t digit; |
||||
|
digit = numerator->DivideModuloIntBignum(*denominator); |
||||
|
ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive.
|
||||
|
// digit = numerator / denominator (integer division).
|
||||
|
// numerator = numerator % denominator.
|
||||
|
buffer[i] = digit + '0'; |
||||
|
// Prepare for next iteration.
|
||||
|
numerator->Times10(); |
||||
|
} |
||||
|
// Generate the last digit.
|
||||
|
uint16_t digit; |
||||
|
digit = numerator->DivideModuloIntBignum(*denominator); |
||||
|
if (Bignum::PlusCompare(*numerator, *numerator, *denominator) >= 0) { |
||||
|
digit++; |
||||
|
} |
||||
|
buffer[count - 1] = digit + '0'; |
||||
|
// Correct bad digits (in case we had a sequence of '9's). Propagate the
|
||||
|
// carry until we hat a non-'9' or til we reach the first digit.
|
||||
|
for (int i = count - 1; i > 0; --i) { |
||||
|
if (buffer[i] != '0' + 10) break; |
||||
|
buffer[i] = '0'; |
||||
|
buffer[i - 1]++; |
||||
|
} |
||||
|
if (buffer[0] == '0' + 10) { |
||||
|
// Propagate a carry past the top place.
|
||||
|
buffer[0] = '1'; |
||||
|
(*decimal_point)++; |
||||
|
} |
||||
|
*length = count; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
// Generates 'requested_digits' after the decimal point. It might omit
|
||||
|
// trailing '0's. If the input number is too small then no digits at all are
|
||||
|
// generated (ex.: 2 fixed digits for 0.00001).
|
||||
|
//
|
||||
|
// Input verifies: 1 <= (numerator + delta) / denominator < 10.
|
||||
|
static void BignumToFixed(int requested_digits, int* decimal_point, |
||||
|
Bignum* numerator, Bignum* denominator, |
||||
|
Vector<char>(buffer), int* length) { |
||||
|
// Note that we have to look at more than just the requested_digits, since
|
||||
|
// a number could be rounded up. Example: v=0.5 with requested_digits=0.
|
||||
|
// Even though the power of v equals 0 we can't just stop here.
|
||||
|
if (-(*decimal_point) > requested_digits) { |
||||
|
// The number is definitively too small.
|
||||
|
// Ex: 0.001 with requested_digits == 1.
|
||||
|
// Set decimal-point to -requested_digits. This is what Gay does.
|
||||
|
// Note that it should not have any effect anyways since the string is
|
||||
|
// empty.
|
||||
|
*decimal_point = -requested_digits; |
||||
|
*length = 0; |
||||
|
return; |
||||
|
} else if (-(*decimal_point) == requested_digits) { |
||||
|
// We only need to verify if the number rounds down or up.
|
||||
|
// Ex: 0.04 and 0.06 with requested_digits == 1.
|
||||
|
ASSERT(*decimal_point == -requested_digits); |
||||
|
// Initially the fraction lies in range (1, 10]. Multiply the denominator
|
||||
|
// by 10 so that we can compare more easily.
|
||||
|
denominator->Times10(); |
||||
|
if (Bignum::PlusCompare(*numerator, *numerator, *denominator) >= 0) { |
||||
|
// If the fraction is >= 0.5 then we have to include the rounded
|
||||
|
// digit.
|
||||
|
buffer[0] = '1'; |
||||
|
*length = 1; |
||||
|
(*decimal_point)++; |
||||
|
} else { |
||||
|
// Note that we caught most of similar cases earlier.
|
||||
|
*length = 0; |
||||
|
} |
||||
|
return; |
||||
|
} else { |
||||
|
// The requested digits correspond to the digits after the point.
|
||||
|
// The variable 'needed_digits' includes the digits before the point.
|
||||
|
int needed_digits = (*decimal_point) + requested_digits; |
||||
|
GenerateCountedDigits(needed_digits, decimal_point, |
||||
|
numerator, denominator, |
||||
|
buffer, length); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
// Returns an estimation of k such that 10^(k-1) <= v < 10^k where
|
||||
|
// v = f * 2^exponent and 2^52 <= f < 2^53.
|
||||
|
// v is hence a normalized double with the given exponent. The output is an
|
||||
|
// approximation for the exponent of the decimal approimation .digits * 10^k.
|
||||
|
//
|
||||
|
// The result might undershoot by 1 in which case 10^k <= v < 10^k+1.
|
||||
|
// Note: this property holds for v's upper boundary m+ too.
|
||||
|
// 10^k <= m+ < 10^k+1.
|
||||
|
// (see explanation below).
|
||||
|
//
|
||||
|
// Examples:
|
||||
|
// EstimatePower(0) => 16
|
||||
|
// EstimatePower(-52) => 0
|
||||
|
//
|
||||
|
// Note: e >= 0 => EstimatedPower(e) > 0. No similar claim can be made for e<0.
|
||||
|
static int EstimatePower(int exponent) { |
||||
|
// This function estimates log10 of v where v = f*2^e (with e == exponent).
|
||||
|
// Note that 10^floor(log10(v)) <= v, but v <= 10^ceil(log10(v)).
|
||||
|
// Note that f is bounded by its container size. Let p = 53 (the double's
|
||||
|
// significand size). Then 2^(p-1) <= f < 2^p.
|
||||
|
//
|
||||
|
// Given that log10(v) == log2(v)/log2(10) and e+(len(f)-1) is quite close
|
||||
|
// to log2(v) the function is simplified to (e+(len(f)-1)/log2(10)).
|
||||
|
// The computed number undershoots by less than 0.631 (when we compute log3
|
||||
|
// and not log10).
|
||||
|
//
|
||||
|
// Optimization: since we only need an approximated result this computation
|
||||
|
// can be performed on 64 bit integers. On x86/x64 architecture the speedup is
|
||||
|
// not really measurable, though.
|
||||
|
//
|
||||
|
// Since we want to avoid overshooting we decrement by 1e10 so that
|
||||
|
// floating-point imprecisions don't affect us.
|
||||
|
//
|
||||
|
// Explanation for v's boundary m+: the computation takes advantage of
|
||||
|
// the fact that 2^(p-1) <= f < 2^p. Boundaries still satisfy this requirement
|
||||
|
// (even for denormals where the delta can be much more important).
|
||||
|
|
||||
|
const double k1Log10 = 0.30102999566398114; // 1/lg(10)
|
||||
|
|
||||
|
// For doubles len(f) == 53 (don't forget the hidden bit).
|
||||
|
const int kSignificandSize = 53; |
||||
|
double estimate = ceil((exponent + kSignificandSize - 1) * k1Log10 - 1e-10); |
||||
|
return static_cast<int>(estimate); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
// See comments for InitialScaledStartValues.
|
||||
|
static void InitialScaledStartValuesPositiveExponent( |
||||
|
double v, int estimated_power, bool need_boundary_deltas, |
||||
|
Bignum* numerator, Bignum* denominator, |
||||
|
Bignum* delta_minus, Bignum* delta_plus) { |
||||
|
// A positive exponent implies a positive power.
|
||||
|
ASSERT(estimated_power >= 0); |
||||
|
// Since the estimated_power is positive we simply multiply the denominator
|
||||
|
// by 10^estimated_power.
|
||||
|
|
||||
|
// numerator = v.
|
||||
|
numerator->AssignUInt64(Double(v).Significand()); |
||||
|
numerator->ShiftLeft(Double(v).Exponent()); |
||||
|
// denominator = 10^estimated_power.
|
||||
|
denominator->AssignPowerUInt16(10, estimated_power); |
||||
|
|
||||
|
if (need_boundary_deltas) { |
||||
|
// Introduce a common denominator so that the deltas to the boundaries are
|
||||
|
// integers.
|
||||
|
denominator->ShiftLeft(1); |
||||
|
numerator->ShiftLeft(1); |
||||
|
// Let v = f * 2^e, then m+ - v = 1/2 * 2^e; With the common
|
||||
|
// denominator (of 2) delta_plus equals 2^e.
|
||||
|
delta_plus->AssignUInt16(1); |
||||
|
delta_plus->ShiftLeft(Double(v).Exponent()); |
||||
|
// Same for delta_minus (with adjustments below if f == 2^p-1).
|
||||
|
delta_minus->AssignUInt16(1); |
||||
|
delta_minus->ShiftLeft(Double(v).Exponent()); |
||||
|
|
||||
|
// If the significand (without the hidden bit) is 0, then the lower
|
||||
|
// boundary is closer than just half a ulp (unit in the last place).
|
||||
|
// There is only one exception: if the next lower number is a denormal then
|
||||
|
// the distance is 1 ulp. This cannot be the case for exponent >= 0 (but we
|
||||
|
// have to test it in the other function where exponent < 0).
|
||||
|
uint64_t v_bits = Double(v).AsUint64(); |
||||
|
if ((v_bits & Double::kSignificandMask) == 0) { |
||||
|
// The lower boundary is closer at half the distance of "normal" numbers.
|
||||
|
// Increase the common denominator and adapt all but the delta_minus.
|
||||
|
denominator->ShiftLeft(1); // *2
|
||||
|
numerator->ShiftLeft(1); // *2
|
||||
|
delta_plus->ShiftLeft(1); // *2
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
// See comments for InitialScaledStartValues
|
||||
|
static void InitialScaledStartValuesNegativeExponentPositivePower( |
||||
|
double v, int estimated_power, bool need_boundary_deltas, |
||||
|
Bignum* numerator, Bignum* denominator, |
||||
|
Bignum* delta_minus, Bignum* delta_plus) { |
||||
|
uint64_t significand = Double(v).Significand(); |
||||
|
int exponent = Double(v).Exponent(); |
||||
|
// v = f * 2^e with e < 0, and with estimated_power >= 0.
|
||||
|
// This means that e is close to 0 (have a look at how estimated_power is
|
||||
|
// computed).
|
||||
|
|
||||
|
// numerator = significand
|
||||
|
// since v = significand * 2^exponent this is equivalent to
|
||||
|
// numerator = v * / 2^-exponent
|
||||
|
numerator->AssignUInt64(significand); |
||||
|
// denominator = 10^estimated_power * 2^-exponent (with exponent < 0)
|
||||
|
denominator->AssignPowerUInt16(10, estimated_power); |
||||
|
denominator->ShiftLeft(-exponent); |
||||
|
|
||||
|
if (need_boundary_deltas) { |
||||
|
// Introduce a common denominator so that the deltas to the boundaries are
|
||||
|
// integers.
|
||||
|
denominator->ShiftLeft(1); |
||||
|
numerator->ShiftLeft(1); |
||||
|
// Let v = f * 2^e, then m+ - v = 1/2 * 2^e; With the common
|
||||
|
// denominator (of 2) delta_plus equals 2^e.
|
||||
|
// Given that the denominator already includes v's exponent the distance
|
||||
|
// to the boundaries is simply 1.
|
||||
|
delta_plus->AssignUInt16(1); |
||||
|
// Same for delta_minus (with adjustments below if f == 2^p-1).
|
||||
|
delta_minus->AssignUInt16(1); |
||||
|
|
||||
|
// If the significand (without the hidden bit) is 0, then the lower
|
||||
|
// boundary is closer than just one ulp (unit in the last place).
|
||||
|
// There is only one exception: if the next lower number is a denormal
|
||||
|
// then the distance is 1 ulp. Since the exponent is close to zero
|
||||
|
// (otherwise estimated_power would have been negative) this cannot happen
|
||||
|
// here either.
|
||||
|
uint64_t v_bits = Double(v).AsUint64(); |
||||
|
if ((v_bits & Double::kSignificandMask) == 0) { |
||||
|
// The lower boundary is closer at half the distance of "normal" numbers.
|
||||
|
// Increase the denominator and adapt all but the delta_minus.
|
||||
|
denominator->ShiftLeft(1); // *2
|
||||
|
numerator->ShiftLeft(1); // *2
|
||||
|
delta_plus->ShiftLeft(1); // *2
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
// See comments for InitialScaledStartValues
|
||||
|
static void InitialScaledStartValuesNegativeExponentNegativePower( |
||||
|
double v, int estimated_power, bool need_boundary_deltas, |
||||
|
Bignum* numerator, Bignum* denominator, |
||||
|
Bignum* delta_minus, Bignum* delta_plus) { |
||||
|
const uint64_t kMinimalNormalizedExponent = |
||||
|
V8_2PART_UINT64_C(0x00100000, 00000000); |
||||
|
uint64_t significand = Double(v).Significand(); |
||||
|
int exponent = Double(v).Exponent(); |
||||
|
// Instead of multiplying the denominator with 10^estimated_power we
|
||||
|
// multiply all values (numerator and deltas) by 10^-estimated_power.
|
||||
|
|
||||
|
// Use numerator as temporary container for power_ten.
|
||||
|
Bignum* power_ten = numerator; |
||||
|
power_ten->AssignPowerUInt16(10, -estimated_power); |
||||
|
|
||||
|
if (need_boundary_deltas) { |
||||
|
// Since power_ten == numerator we must make a copy of 10^estimated_power
|
||||
|
// before we complete the computation of the numerator.
|
||||
|
// delta_plus = delta_minus = 10^estimated_power
|
||||
|
delta_plus->AssignBignum(*power_ten); |
||||
|
delta_minus->AssignBignum(*power_ten); |
||||
|
} |
||||
|
|
||||
|
// numerator = significand * 2 * 10^-estimated_power
|
||||
|
// since v = significand * 2^exponent this is equivalent to
|
||||
|
// numerator = v * 10^-estimated_power * 2 * 2^-exponent.
|
||||
|
// Remember: numerator has been abused as power_ten. So no need to assign it
|
||||
|
// to itself.
|
||||
|
ASSERT(numerator == power_ten); |
||||
|
numerator->MultiplyByUInt64(significand); |
||||
|
|
||||
|
// denominator = 2 * 2^-exponent with exponent < 0.
|
||||
|
denominator->AssignUInt16(1); |
||||
|
denominator->ShiftLeft(-exponent); |
||||
|
|
||||
|
if (need_boundary_deltas) { |
||||
|
// Introduce a common denominator so that the deltas to the boundaries are
|
||||
|
// integers.
|
||||
|
numerator->ShiftLeft(1); |
||||
|
denominator->ShiftLeft(1); |
||||
|
// With this shift the boundaries have their correct value, since
|
||||
|
// delta_plus = 10^-estimated_power, and
|
||||
|
// delta_minus = 10^-estimated_power.
|
||||
|
// These assignments have been done earlier.
|
||||
|
|
||||
|
// The special case where the lower boundary is twice as close.
|
||||
|
// This time we have to look out for the exception too.
|
||||
|
uint64_t v_bits = Double(v).AsUint64(); |
||||
|
if ((v_bits & Double::kSignificandMask) == 0 && |
||||
|
// The only exception where a significand == 0 has its boundaries at
|
||||
|
// "normal" distances:
|
||||
|
(v_bits & Double::kExponentMask) != kMinimalNormalizedExponent) { |
||||
|
numerator->ShiftLeft(1); // *2
|
||||
|
denominator->ShiftLeft(1); // *2
|
||||
|
delta_plus->ShiftLeft(1); // *2
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
// Let v = significand * 2^exponent.
|
||||
|
// Computes v / 10^estimated_power exactly, as a ratio of two bignums, numerator
|
||||
|
// and denominator. The functions GenerateShortestDigits and
|
||||
|
// GenerateCountedDigits will then convert this ratio to its decimal
|
||||
|
// representation d, with the required accuracy.
|
||||
|
// Then d * 10^estimated_power is the representation of v.
|
||||
|
// (Note: the fraction and the estimated_power might get adjusted before
|
||||
|
// generating the decimal representation.)
|
||||
|
//
|
||||
|
// The initial start values consist of:
|
||||
|
// - a scaled numerator: s.t. numerator/denominator == v / 10^estimated_power.
|
||||
|
// - a scaled (common) denominator.
|
||||
|
// optionally (used by GenerateShortestDigits to decide if it has the shortest
|
||||
|
// decimal converting back to v):
|
||||
|
// - v - m-: the distance to the lower boundary.
|
||||
|
// - m+ - v: the distance to the upper boundary.
|
||||
|
//
|
||||
|
// v, m+, m-, and therefore v - m- and m+ - v all share the same denominator.
|
||||
|
//
|
||||
|
// Let ep == estimated_power, then the returned values will satisfy:
|
||||
|
// v / 10^ep = numerator / denominator.
|
||||
|
// v's boundarys m- and m+:
|
||||
|
// m- / 10^ep == v / 10^ep - delta_minus / denominator
|
||||
|
// m+ / 10^ep == v / 10^ep + delta_plus / denominator
|
||||
|
// Or in other words:
|
||||
|
// m- == v - delta_minus * 10^ep / denominator;
|
||||
|
// m+ == v + delta_plus * 10^ep / denominator;
|
||||
|
//
|
||||
|
// Since 10^(k-1) <= v < 10^k (with k == estimated_power)
|
||||
|
// or 10^k <= v < 10^(k+1)
|
||||
|
// we then have 0.1 <= numerator/denominator < 1
|
||||
|
// or 1 <= numerator/denominator < 10
|
||||
|
//
|
||||
|
// It is then easy to kickstart the digit-generation routine.
|
||||
|
//
|
||||
|
// The boundary-deltas are only filled if need_boundary_deltas is set.
|
||||
|
static void InitialScaledStartValues(double v, |
||||
|
int estimated_power, |
||||
|
bool need_boundary_deltas, |
||||
|
Bignum* numerator, |
||||
|
Bignum* denominator, |
||||
|
Bignum* delta_minus, |
||||
|
Bignum* delta_plus) { |
||||
|
if (Double(v).Exponent() >= 0) { |
||||
|
InitialScaledStartValuesPositiveExponent( |
||||
|
v, estimated_power, need_boundary_deltas, |
||||
|
numerator, denominator, delta_minus, delta_plus); |
||||
|
} else if (estimated_power >= 0) { |
||||
|
InitialScaledStartValuesNegativeExponentPositivePower( |
||||
|
v, estimated_power, need_boundary_deltas, |
||||
|
numerator, denominator, delta_minus, delta_plus); |
||||
|
} else { |
||||
|
InitialScaledStartValuesNegativeExponentNegativePower( |
||||
|
v, estimated_power, need_boundary_deltas, |
||||
|
numerator, denominator, delta_minus, delta_plus); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
// This routine multiplies numerator/denominator so that its values lies in the
|
||||
|
// range 1-10. That is after a call to this function we have:
|
||||
|
// 1 <= (numerator + delta_plus) /denominator < 10.
|
||||
|
// Let numerator the input before modification and numerator' the argument
|
||||
|
// after modification, then the output-parameter decimal_point is such that
|
||||
|
// numerator / denominator * 10^estimated_power ==
|
||||
|
// numerator' / denominator' * 10^(decimal_point - 1)
|
||||
|
// In some cases estimated_power was too low, and this is already the case. We
|
||||
|
// then simply adjust the power so that 10^(k-1) <= v < 10^k (with k ==
|
||||
|
// estimated_power) but do not touch the numerator or denominator.
|
||||
|
// Otherwise the routine multiplies the numerator and the deltas by 10.
|
||||
|
static void FixupMultiply10(int estimated_power, bool is_even, |
||||
|
int* decimal_point, |
||||
|
Bignum* numerator, Bignum* denominator, |
||||
|
Bignum* delta_minus, Bignum* delta_plus) { |
||||
|
bool in_range; |
||||
|
if (is_even) { |
||||
|
// For IEEE doubles half-way cases (in decimal system numbers ending with 5)
|
||||
|
// are rounded to the closest floating-point number with even significand.
|
||||
|
in_range = Bignum::PlusCompare(*numerator, *delta_plus, *denominator) >= 0; |
||||
|
} else { |
||||
|
in_range = Bignum::PlusCompare(*numerator, *delta_plus, *denominator) > 0; |
||||
|
} |
||||
|
if (in_range) { |
||||
|
// Since numerator + delta_plus >= denominator we already have
|
||||
|
// 1 <= numerator/denominator < 10. Simply update the estimated_power.
|
||||
|
*decimal_point = estimated_power + 1; |
||||
|
} else { |
||||
|
*decimal_point = estimated_power; |
||||
|
numerator->Times10(); |
||||
|
if (Bignum::Equal(*delta_minus, *delta_plus)) { |
||||
|
delta_minus->Times10(); |
||||
|
delta_plus->AssignBignum(*delta_minus); |
||||
|
} else { |
||||
|
delta_minus->Times10(); |
||||
|
delta_plus->Times10(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} } // namespace v8::internal
|
@ -0,0 +1,81 @@ |
|||||
|
// Copyright 2010 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.
|
||||
|
|
||||
|
#ifndef V8_BIGNUM_DTOA_H_ |
||||
|
#define V8_BIGNUM_DTOA_H_ |
||||
|
|
||||
|
namespace v8 { |
||||
|
namespace internal { |
||||
|
|
||||
|
enum BignumDtoaMode { |
||||
|
// Return the shortest correct representation.
|
||||
|
// For example the output of 0.299999999999999988897 is (the less accurate but
|
||||
|
// correct) 0.3.
|
||||
|
BIGNUM_DTOA_SHORTEST, |
||||
|
// Return a fixed number of digits after the decimal point.
|
||||
|
// For instance fixed(0.1, 4) becomes 0.1000
|
||||
|
// If the input number is big, the output will be big.
|
||||
|
BIGNUM_DTOA_FIXED, |
||||
|
// Return a fixed number of digits, no matter what the exponent is.
|
||||
|
BIGNUM_DTOA_PRECISION |
||||
|
}; |
||||
|
|
||||
|
// Converts the given double 'v' to ascii.
|
||||
|
// The result should be interpreted as buffer * 10^(point-length).
|
||||
|
// The buffer will be null-terminated.
|
||||
|
//
|
||||
|
// The input v must be > 0 and different from NaN, and Infinity.
|
||||
|
//
|
||||
|
// The output depends on the given mode:
|
||||
|
// - SHORTEST: produce the least amount of digits for which the internal
|
||||
|
// identity requirement is still satisfied. If the digits are printed
|
||||
|
// (together with the correct exponent) then reading this number will give
|
||||
|
// 'v' again. The buffer will choose the representation that is closest to
|
||||
|
// 'v'. If there are two at the same distance, than the number is round up.
|
||||
|
// In this mode the 'requested_digits' parameter is ignored.
|
||||
|
// - FIXED: produces digits necessary to print a given number with
|
||||
|
// 'requested_digits' digits after the decimal point. The produced digits
|
||||
|
// might be too short in which case the caller has to fill the gaps with '0's.
|
||||
|
// Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2.
|
||||
|
// Halfway cases are rounded up. The call toFixed(0.15, 2) thus returns
|
||||
|
// buffer="2", point=0.
|
||||
|
// Note: the length of the returned buffer has no meaning wrt the significance
|
||||
|
// of its digits. That is, just because it contains '0's does not mean that
|
||||
|
// any other digit would not satisfy the internal identity requirement.
|
||||
|
// - PRECISION: produces 'requested_digits' where the first digit is not '0'.
|
||||
|
// Even though the length of produced digits usually equals
|
||||
|
// 'requested_digits', the function is allowed to return fewer digits, in
|
||||
|
// which case the caller has to fill the missing digits with '0's.
|
||||
|
// Halfway cases are again rounded up.
|
||||
|
// 'BignumDtoa' expects the given buffer to be big enough to hold all digits
|
||||
|
// and a terminating null-character.
|
||||
|
void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits, |
||||
|
Vector<char> buffer, int* length, int* point); |
||||
|
|
||||
|
} } // namespace v8::internal
|
||||
|
|
||||
|
#endif // V8_BIGNUM_DTOA_H_
|
@ -0,0 +1,767 @@ |
|||||
|
// Copyright 2010 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 "v8.h" |
||||
|
|
||||
|
#include "bignum.h" |
||||
|
#include "utils.h" |
||||
|
|
||||
|
namespace v8 { |
||||
|
namespace internal { |
||||
|
|
||||
|
Bignum::Bignum() |
||||
|
: bigits_(bigits_buffer_, kBigitCapacity), used_digits_(0), exponent_(0) { |
||||
|
for (int i = 0; i < kBigitCapacity; ++i) { |
||||
|
bigits_[i] = 0; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
template<typename S> |
||||
|
static int BitSize(S value) { |
||||
|
return 8 * sizeof(value); |
||||
|
} |
||||
|
|
||||
|
// Guaranteed to lie in one Bigit.
|
||||
|
void Bignum::AssignUInt16(uint16_t value) { |
||||
|
ASSERT(kBigitSize >= BitSize(value)); |
||||
|
Zero(); |
||||
|
if (value == 0) return; |
||||
|
|
||||
|
EnsureCapacity(1); |
||||
|
bigits_[0] = value; |
||||
|
used_digits_ = 1; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Bignum::AssignUInt64(uint64_t value) { |
||||
|
const int kUInt64Size = 64; |
||||
|
|
||||
|
Zero(); |
||||
|
if (value == 0) return; |
||||
|
|
||||
|
int needed_bigits = kUInt64Size / kBigitSize + 1; |
||||
|
EnsureCapacity(needed_bigits); |
||||
|
for (int i = 0; i < needed_bigits; ++i) { |
||||
|
bigits_[i] = value & kBigitMask; |
||||
|
value = value >> kBigitSize; |
||||
|
} |
||||
|
used_digits_ = needed_bigits; |
||||
|
Clamp(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Bignum::AssignBignum(const Bignum& other) { |
||||
|
exponent_ = other.exponent_; |
||||
|
for (int i = 0; i < other.used_digits_; ++i) { |
||||
|
bigits_[i] = other.bigits_[i]; |
||||
|
} |
||||
|
// Clear the excess digits (if there were any).
|
||||
|
for (int i = other.used_digits_; i < used_digits_; ++i) { |
||||
|
bigits_[i] = 0; |
||||
|
} |
||||
|
used_digits_ = other.used_digits_; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static uint64_t ReadUInt64(Vector<const char> buffer, |
||||
|
int from, |
||||
|
int digits_to_read) { |
||||
|
uint64_t result = 0; |
||||
|
for (int i = from; i < from + digits_to_read; ++i) { |
||||
|
int digit = buffer[i] - '0'; |
||||
|
ASSERT(0 <= digit && digit <= 9); |
||||
|
result = result * 10 + digit; |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Bignum::AssignDecimalString(Vector<const char> value) { |
||||
|
// 2^64 = 18446744073709551616 > 10^19
|
||||
|
const int kMaxUint64DecimalDigits = 19; |
||||
|
Zero(); |
||||
|
int length = value.length(); |
||||
|
int pos = 0; |
||||
|
// Let's just say that each digit needs 4 bits.
|
||||
|
while (length >= kMaxUint64DecimalDigits) { |
||||
|
uint64_t digits = ReadUInt64(value, pos, kMaxUint64DecimalDigits); |
||||
|
pos += kMaxUint64DecimalDigits; |
||||
|
length -= kMaxUint64DecimalDigits; |
||||
|
MultiplyByPowerOfTen(kMaxUint64DecimalDigits); |
||||
|
AddUInt64(digits); |
||||
|
} |
||||
|
uint64_t digits = ReadUInt64(value, pos, length); |
||||
|
MultiplyByPowerOfTen(length); |
||||
|
AddUInt64(digits); |
||||
|
Clamp(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static int HexCharValue(char c) { |
||||
|
if ('0' <= c && c <= '9') return c - '0'; |
||||
|
if ('a' <= c && c <= 'f') return 10 + c - 'a'; |
||||
|
if ('A' <= c && c <= 'F') return 10 + c - 'A'; |
||||
|
UNREACHABLE(); |
||||
|
return 0; // To make compiler happy.
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Bignum::AssignHexString(Vector<const char> value) { |
||||
|
Zero(); |
||||
|
int length = value.length(); |
||||
|
|
||||
|
int needed_bigits = length * 4 / kBigitSize + 1; |
||||
|
EnsureCapacity(needed_bigits); |
||||
|
int string_index = length - 1; |
||||
|
for (int i = 0; i < needed_bigits - 1; ++i) { |
||||
|
// These bigits are guaranteed to be "full".
|
||||
|
Chunk current_bigit = 0; |
||||
|
for (int j = 0; j < kBigitSize / 4; j++) { |
||||
|
current_bigit += HexCharValue(value[string_index--]) << (j * 4); |
||||
|
} |
||||
|
bigits_[i] = current_bigit; |
||||
|
} |
||||
|
used_digits_ = needed_bigits - 1; |
||||
|
|
||||
|
Chunk most_significant_bigit = 0; // Could be = 0;
|
||||
|
for (int j = 0; j <= string_index; ++j) { |
||||
|
most_significant_bigit <<= 4; |
||||
|
most_significant_bigit += HexCharValue(value[j]); |
||||
|
} |
||||
|
if (most_significant_bigit != 0) { |
||||
|
bigits_[used_digits_] = most_significant_bigit; |
||||
|
used_digits_++; |
||||
|
} |
||||
|
Clamp(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Bignum::AddUInt64(uint64_t operand) { |
||||
|
if (operand == 0) return; |
||||
|
Bignum other; |
||||
|
other.AssignUInt64(operand); |
||||
|
AddBignum(other); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Bignum::AddBignum(const Bignum& other) { |
||||
|
ASSERT(IsClamped()); |
||||
|
ASSERT(other.IsClamped()); |
||||
|
|
||||
|
// If this has a greater exponent than other append zero-bigits to this.
|
||||
|
// After this call exponent_ <= other.exponent_.
|
||||
|
Align(other); |
||||
|
|
||||
|
// There are two possibilities:
|
||||
|
// aaaaaaaaaaa 0000 (where the 0s represent a's exponent)
|
||||
|
// bbbbb 00000000
|
||||
|
// ----------------
|
||||
|
// ccccccccccc 0000
|
||||
|
// or
|
||||
|
// aaaaaaaaaa 0000
|
||||
|
// bbbbbbbbb 0000000
|
||||
|
// -----------------
|
||||
|
// cccccccccccc 0000
|
||||
|
// In both cases we might need a carry bigit.
|
||||
|
|
||||
|
EnsureCapacity(1 + Max(BigitLength(), other.BigitLength()) - exponent_); |
||||
|
Chunk carry = 0; |
||||
|
int bigit_pos = other.exponent_ - exponent_; |
||||
|
ASSERT(bigit_pos >= 0); |
||||
|
for (int i = 0; i < other.used_digits_; ++i) { |
||||
|
Chunk sum = bigits_[bigit_pos] + other.bigits_[i] + carry; |
||||
|
bigits_[bigit_pos] = sum & kBigitMask; |
||||
|
carry = sum >> kBigitSize; |
||||
|
bigit_pos++; |
||||
|
} |
||||
|
|
||||
|
while (carry != 0) { |
||||
|
Chunk sum = bigits_[bigit_pos] + carry; |
||||
|
bigits_[bigit_pos] = sum & kBigitMask; |
||||
|
carry = sum >> kBigitSize; |
||||
|
bigit_pos++; |
||||
|
} |
||||
|
used_digits_ = Max(bigit_pos, used_digits_); |
||||
|
ASSERT(IsClamped()); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Bignum::SubtractBignum(const Bignum& other) { |
||||
|
ASSERT(IsClamped()); |
||||
|
ASSERT(other.IsClamped()); |
||||
|
// We require this to be bigger than other.
|
||||
|
ASSERT(LessEqual(other, *this)); |
||||
|
|
||||
|
Align(other); |
||||
|
|
||||
|
int offset = other.exponent_ - exponent_; |
||||
|
Chunk borrow = 0; |
||||
|
int i; |
||||
|
for (i = 0; i < other.used_digits_; ++i) { |
||||
|
ASSERT((borrow == 0) || (borrow == 1)); |
||||
|
Chunk difference = bigits_[i + offset] - other.bigits_[i] - borrow; |
||||
|
bigits_[i + offset] = difference & kBigitMask; |
||||
|
borrow = difference >> (kChunkSize - 1); |
||||
|
} |
||||
|
while (borrow != 0) { |
||||
|
Chunk difference = bigits_[i + offset] - borrow; |
||||
|
bigits_[i + offset] = difference & kBigitMask; |
||||
|
borrow = difference >> (kChunkSize - 1); |
||||
|
++i; |
||||
|
} |
||||
|
Clamp(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Bignum::ShiftLeft(int shift_amount) { |
||||
|
if (used_digits_ == 0) return; |
||||
|
exponent_ += shift_amount / kBigitSize; |
||||
|
int local_shift = shift_amount % kBigitSize; |
||||
|
EnsureCapacity(used_digits_ + 1); |
||||
|
BigitsShiftLeft(local_shift); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Bignum::MultiplyByUInt32(uint32_t factor) { |
||||
|
if (factor == 1) return; |
||||
|
if (factor == 0) { |
||||
|
Zero(); |
||||
|
return; |
||||
|
} |
||||
|
if (used_digits_ == 0) return; |
||||
|
|
||||
|
// The product of a bigit with the factor is of size kBigitSize + 32.
|
||||
|
// Assert that this number + 1 (for the carry) fits into double chunk.
|
||||
|
ASSERT(kDoubleChunkSize >= kBigitSize + 32 + 1); |
||||
|
DoubleChunk carry = 0; |
||||
|
for (int i = 0; i < used_digits_; ++i) { |
||||
|
DoubleChunk product = static_cast<DoubleChunk>(factor) * bigits_[i] + carry; |
||||
|
bigits_[i] = static_cast<Chunk>(product & kBigitMask); |
||||
|
carry = (product >> kBigitSize); |
||||
|
} |
||||
|
while (carry != 0) { |
||||
|
EnsureCapacity(used_digits_ + 1); |
||||
|
bigits_[used_digits_] = carry & kBigitMask; |
||||
|
used_digits_++; |
||||
|
carry >>= kBigitSize; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Bignum::MultiplyByUInt64(uint64_t factor) { |
||||
|
if (factor == 1) return; |
||||
|
if (factor == 0) { |
||||
|
Zero(); |
||||
|
return; |
||||
|
} |
||||
|
ASSERT(kBigitSize < 32); |
||||
|
uint64_t carry = 0; |
||||
|
uint64_t low = factor & 0xFFFFFFFF; |
||||
|
uint64_t high = factor >> 32; |
||||
|
for (int i = 0; i < used_digits_; ++i) { |
||||
|
uint64_t product_low = low * bigits_[i]; |
||||
|
uint64_t product_high = high * bigits_[i]; |
||||
|
uint64_t tmp = (carry & kBigitMask) + product_low; |
||||
|
bigits_[i] = tmp & kBigitMask; |
||||
|
carry = (carry >> kBigitSize) + (tmp >> kBigitSize) + |
||||
|
(product_high << (32 - kBigitSize)); |
||||
|
} |
||||
|
while (carry != 0) { |
||||
|
EnsureCapacity(used_digits_ + 1); |
||||
|
bigits_[used_digits_] = carry & kBigitMask; |
||||
|
used_digits_++; |
||||
|
carry >>= kBigitSize; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Bignum::MultiplyByPowerOfTen(int exponent) { |
||||
|
const uint64_t kFive27 = V8_2PART_UINT64_C(0x6765c793, fa10079d); |
||||
|
const uint16_t kFive1 = 5; |
||||
|
const uint16_t kFive2 = kFive1 * 5; |
||||
|
const uint16_t kFive3 = kFive2 * 5; |
||||
|
const uint16_t kFive4 = kFive3 * 5; |
||||
|
const uint16_t kFive5 = kFive4 * 5; |
||||
|
const uint16_t kFive6 = kFive5 * 5; |
||||
|
const uint32_t kFive7 = kFive6 * 5; |
||||
|
const uint32_t kFive8 = kFive7 * 5; |
||||
|
const uint32_t kFive9 = kFive8 * 5; |
||||
|
const uint32_t kFive10 = kFive9 * 5; |
||||
|
const uint32_t kFive11 = kFive10 * 5; |
||||
|
const uint32_t kFive12 = kFive11 * 5; |
||||
|
const uint32_t kFive13 = kFive12 * 5; |
||||
|
const uint32_t kFive1_to_12[] = |
||||
|
{ kFive1, kFive2, kFive3, kFive4, kFive5, kFive6, |
||||
|
kFive7, kFive8, kFive9, kFive10, kFive11, kFive12 }; |
||||
|
|
||||
|
ASSERT(exponent >= 0); |
||||
|
if (exponent == 0) return; |
||||
|
if (used_digits_ == 0) return; |
||||
|
|
||||
|
// We shift by exponent at the end just before returning.
|
||||
|
int remaining_exponent = exponent; |
||||
|
while (remaining_exponent >= 27) { |
||||
|
MultiplyByUInt64(kFive27); |
||||
|
remaining_exponent -= 27; |
||||
|
} |
||||
|
while (remaining_exponent >= 13) { |
||||
|
MultiplyByUInt32(kFive13); |
||||
|
remaining_exponent -= 13; |
||||
|
} |
||||
|
if (remaining_exponent > 0) { |
||||
|
MultiplyByUInt32(kFive1_to_12[remaining_exponent - 1]); |
||||
|
} |
||||
|
ShiftLeft(exponent); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Bignum::Square() { |
||||
|
ASSERT(IsClamped()); |
||||
|
int product_length = 2 * used_digits_; |
||||
|
EnsureCapacity(product_length); |
||||
|
|
||||
|
// Comba multiplication: compute each column separately.
|
||||
|
// Example: r = a2a1a0 * b2b1b0.
|
||||
|
// r = 1 * a0b0 +
|
||||
|
// 10 * (a1b0 + a0b1) +
|
||||
|
// 100 * (a2b0 + a1b1 + a0b2) +
|
||||
|
// 1000 * (a2b1 + a1b2) +
|
||||
|
// 10000 * a2b2
|
||||
|
//
|
||||
|
// In the worst case we have to accumulate nb-digits products of digit*digit.
|
||||
|
//
|
||||
|
// Assert that the additional number of bits in a DoubleChunk are enough to
|
||||
|
// sum up used_digits of Bigit*Bigit.
|
||||
|
if ((1 << (2 * (kChunkSize - kBigitSize))) <= used_digits_) { |
||||
|
UNIMPLEMENTED(); |
||||
|
} |
||||
|
DoubleChunk accumulator = 0; |
||||
|
// First shift the digits so we don't overwrite them.
|
||||
|
int copy_offset = used_digits_; |
||||
|
for (int i = 0; i < used_digits_; ++i) { |
||||
|
bigits_[copy_offset + i] = bigits_[i]; |
||||
|
} |
||||
|
// We have two loops to avoid some 'if's in the loop.
|
||||
|
for (int i = 0; i < used_digits_; ++i) { |
||||
|
// Process temporary digit i with power i.
|
||||
|
// The sum of the two indices must be equal to i.
|
||||
|
int bigit_index1 = i; |
||||
|
int bigit_index2 = 0; |
||||
|
// Sum all of the sub-products.
|
||||
|
while (bigit_index1 >= 0) { |
||||
|
Chunk chunk1 = bigits_[copy_offset + bigit_index1]; |
||||
|
Chunk chunk2 = bigits_[copy_offset + bigit_index2]; |
||||
|
accumulator += static_cast<DoubleChunk>(chunk1) * chunk2; |
||||
|
bigit_index1--; |
||||
|
bigit_index2++; |
||||
|
} |
||||
|
bigits_[i] = static_cast<Chunk>(accumulator) & kBigitMask; |
||||
|
accumulator >>= kBigitSize; |
||||
|
} |
||||
|
for (int i = used_digits_; i < product_length; ++i) { |
||||
|
int bigit_index1 = used_digits_ - 1; |
||||
|
int bigit_index2 = i - bigit_index1; |
||||
|
// Invariant: sum of both indices is again equal to i.
|
||||
|
// Inner loop runs 0 times on last iteration, emptying accumulator.
|
||||
|
while (bigit_index2 < used_digits_) { |
||||
|
Chunk chunk1 = bigits_[copy_offset + bigit_index1]; |
||||
|
Chunk chunk2 = bigits_[copy_offset + bigit_index2]; |
||||
|
accumulator += static_cast<DoubleChunk>(chunk1) * chunk2; |
||||
|
bigit_index1--; |
||||
|
bigit_index2++; |
||||
|
} |
||||
|
// The overwritten bigits_[i] will never be read in further loop iterations,
|
||||
|
// because bigit_index1 and bigit_index2 are always greater
|
||||
|
// than i - used_digits_.
|
||||
|
bigits_[i] = static_cast<Chunk>(accumulator) & kBigitMask; |
||||
|
accumulator >>= kBigitSize; |
||||
|
} |
||||
|
// Since the result was guaranteed to lie inside the number the
|
||||
|
// accumulator must be 0 now.
|
||||
|
ASSERT(accumulator == 0); |
||||
|
|
||||
|
// Don't forget to update the used_digits and the exponent.
|
||||
|
used_digits_ = product_length; |
||||
|
exponent_ *= 2; |
||||
|
Clamp(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Bignum::AssignPowerUInt16(uint16_t base, int power_exponent) { |
||||
|
ASSERT(base != 0); |
||||
|
ASSERT(power_exponent >= 0); |
||||
|
if (power_exponent == 0) { |
||||
|
AssignUInt16(1); |
||||
|
return; |
||||
|
} |
||||
|
Zero(); |
||||
|
int shifts = 0; |
||||
|
// We expect base to be in range 2-32, and most often to be 10.
|
||||
|
// It does not make much sense to implement different algorithms for counting
|
||||
|
// the bits.
|
||||
|
while ((base & 1) == 0) { |
||||
|
base >>= 1; |
||||
|
shifts++; |
||||
|
} |
||||
|
int bit_size = 0; |
||||
|
int tmp_base = base; |
||||
|
while (tmp_base != 0) { |
||||
|
tmp_base >>= 1; |
||||
|
bit_size++; |
||||
|
} |
||||
|
int final_size = bit_size * power_exponent; |
||||
|
// 1 extra bigit for the shifting, and one for rounded final_size.
|
||||
|
EnsureCapacity(final_size / kBigitSize + 2); |
||||
|
|
||||
|
// Left to Right exponentiation.
|
||||
|
int mask = 1; |
||||
|
while (power_exponent >= mask) mask <<= 1; |
||||
|
|
||||
|
// The mask is now pointing to the bit above the most significant 1-bit of
|
||||
|
// power_exponent.
|
||||
|
// Get rid of first 1-bit;
|
||||
|
mask >>= 2; |
||||
|
uint64_t this_value = base; |
||||
|
|
||||
|
bool delayed_multipliciation = false; |
||||
|
const uint64_t max_32bits = 0xFFFFFFFF; |
||||
|
while (mask != 0 && this_value <= max_32bits) { |
||||
|
this_value = this_value * this_value; |
||||
|
// Verify that there is enough space in this_value to perform the
|
||||
|
// multiplication. The first bit_size bits must be 0.
|
||||
|
if ((power_exponent & mask) != 0) { |
||||
|
uint64_t base_bits_mask = |
||||
|
~((static_cast<uint64_t>(1) << (64 - bit_size)) - 1); |
||||
|
bool high_bits_zero = (this_value & base_bits_mask) == 0; |
||||
|
if (high_bits_zero) { |
||||
|
this_value *= base; |
||||
|
} else { |
||||
|
delayed_multipliciation = true; |
||||
|
} |
||||
|
} |
||||
|
mask >>= 1; |
||||
|
} |
||||
|
AssignUInt64(this_value); |
||||
|
if (delayed_multipliciation) { |
||||
|
MultiplyByUInt32(base); |
||||
|
} |
||||
|
|
||||
|
// Now do the same thing as a bignum.
|
||||
|
while (mask != 0) { |
||||
|
Square(); |
||||
|
if ((power_exponent & mask) != 0) { |
||||
|
MultiplyByUInt32(base); |
||||
|
} |
||||
|
mask >>= 1; |
||||
|
} |
||||
|
|
||||
|
// And finally add the saved shifts.
|
||||
|
ShiftLeft(shifts * power_exponent); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
// Precondition: this/other < 16bit.
|
||||
|
uint16_t Bignum::DivideModuloIntBignum(const Bignum& other) { |
||||
|
ASSERT(IsClamped()); |
||||
|
ASSERT(other.IsClamped()); |
||||
|
ASSERT(other.used_digits_ > 0); |
||||
|
|
||||
|
// Easy case: if we have less digits than the divisor than the result is 0.
|
||||
|
// Note: this handles the case where this == 0, too.
|
||||
|
if (BigitLength() < other.BigitLength()) { |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
Align(other); |
||||
|
|
||||
|
uint16_t result = 0; |
||||
|
|
||||
|
// Start by removing multiples of 'other' until both numbers have the same
|
||||
|
// number of digits.
|
||||
|
while (BigitLength() > other.BigitLength()) { |
||||
|
// This naive approach is extremely inefficient if the this divided other
|
||||
|
// might be big. This function is implemented for doubleToString where
|
||||
|
// the result should be small (less than 10).
|
||||
|
ASSERT(other.bigits_[other.used_digits_ - 1] >= ((1 << kBigitSize) / 16)); |
||||
|
// Remove the multiples of the first digit.
|
||||
|
// Example this = 23 and other equals 9. -> Remove 2 multiples.
|
||||
|
result += bigits_[used_digits_ - 1]; |
||||
|
SubtractTimes(other, bigits_[used_digits_ - 1]); |
||||
|
} |
||||
|
|
||||
|
ASSERT(BigitLength() == other.BigitLength()); |
||||
|
|
||||
|
// Both bignums are at the same length now.
|
||||
|
// Since other has more than 0 digits we know that the access to
|
||||
|
// bigits_[used_digits_ - 1] is safe.
|
||||
|
Chunk this_bigit = bigits_[used_digits_ - 1]; |
||||
|
Chunk other_bigit = other.bigits_[other.used_digits_ - 1]; |
||||
|
|
||||
|
if (other.used_digits_ == 1) { |
||||
|
// Shortcut for easy (and common) case.
|
||||
|
int quotient = this_bigit / other_bigit; |
||||
|
bigits_[used_digits_ - 1] = this_bigit - other_bigit * quotient; |
||||
|
result += quotient; |
||||
|
Clamp(); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
int division_estimate = this_bigit / (other_bigit + 1); |
||||
|
result += division_estimate; |
||||
|
SubtractTimes(other, division_estimate); |
||||
|
|
||||
|
if (other_bigit * (division_estimate + 1) > this_bigit) { |
||||
|
// No need to even try to subtract. Even if other's remaining digits were 0
|
||||
|
// another subtraction would be too much.
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
while (LessEqual(other, *this)) { |
||||
|
SubtractBignum(other); |
||||
|
result++; |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
template<typename S> |
||||
|
static int SizeInHexChars(S number) { |
||||
|
ASSERT(number > 0); |
||||
|
int result = 0; |
||||
|
while (number != 0) { |
||||
|
number >>= 4; |
||||
|
result++; |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static char HexCharOfValue(int value) { |
||||
|
ASSERT(0 <= value && value <= 16); |
||||
|
if (value < 10) return value + '0'; |
||||
|
return value - 10 + 'A'; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
bool Bignum::ToHexString(char* buffer, int buffer_size) const { |
||||
|
ASSERT(IsClamped()); |
||||
|
// Each bigit must be printable as separate hex-character.
|
||||
|
ASSERT(kBigitSize % 4 == 0); |
||||
|
const int kHexCharsPerBigit = kBigitSize / 4; |
||||
|
|
||||
|
if (used_digits_ == 0) { |
||||
|
if (buffer_size < 2) return false; |
||||
|
buffer[0] = '0'; |
||||
|
buffer[1] = '\0'; |
||||
|
return true; |
||||
|
} |
||||
|
// We add 1 for the terminating '\0' character.
|
||||
|
int needed_chars = (BigitLength() - 1) * kHexCharsPerBigit + |
||||
|
SizeInHexChars(bigits_[used_digits_ - 1]) + 1; |
||||
|
if (needed_chars > buffer_size) return false; |
||||
|
int string_index = needed_chars - 1; |
||||
|
buffer[string_index--] = '\0'; |
||||
|
for (int i = 0; i < exponent_; ++i) { |
||||
|
for (int j = 0; j < kHexCharsPerBigit; ++j) { |
||||
|
buffer[string_index--] = '0'; |
||||
|
} |
||||
|
} |
||||
|
for (int i = 0; i < used_digits_ - 1; ++i) { |
||||
|
Chunk current_bigit = bigits_[i]; |
||||
|
for (int j = 0; j < kHexCharsPerBigit; ++j) { |
||||
|
buffer[string_index--] = HexCharOfValue(current_bigit & 0xF); |
||||
|
current_bigit >>= 4; |
||||
|
} |
||||
|
} |
||||
|
// And finally the last bigit.
|
||||
|
Chunk most_significant_bigit = bigits_[used_digits_ - 1]; |
||||
|
while (most_significant_bigit != 0) { |
||||
|
buffer[string_index--] = HexCharOfValue(most_significant_bigit & 0xF); |
||||
|
most_significant_bigit >>= 4; |
||||
|
} |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
Bignum::Chunk Bignum::BigitAt(int index) const { |
||||
|
if (index >= BigitLength()) return 0; |
||||
|
if (index < exponent_) return 0; |
||||
|
return bigits_[index - exponent_]; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
int Bignum::Compare(const Bignum& a, const Bignum& b) { |
||||
|
ASSERT(a.IsClamped()); |
||||
|
ASSERT(b.IsClamped()); |
||||
|
int bigit_length_a = a.BigitLength(); |
||||
|
int bigit_length_b = b.BigitLength(); |
||||
|
if (bigit_length_a < bigit_length_b) return -1; |
||||
|
if (bigit_length_a > bigit_length_b) return +1; |
||||
|
for (int i = bigit_length_a - 1; i >= Min(a.exponent_, b.exponent_); --i) { |
||||
|
Chunk bigit_a = a.BigitAt(i); |
||||
|
Chunk bigit_b = b.BigitAt(i); |
||||
|
if (bigit_a < bigit_b) return -1; |
||||
|
if (bigit_a > bigit_b) return +1; |
||||
|
// Otherwise they are equal up to this digit. Try the next digit.
|
||||
|
} |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
int Bignum::PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c) { |
||||
|
ASSERT(a.IsClamped()); |
||||
|
ASSERT(b.IsClamped()); |
||||
|
ASSERT(c.IsClamped()); |
||||
|
if (a.BigitLength() < b.BigitLength()) { |
||||
|
return PlusCompare(b, a, c); |
||||
|
} |
||||
|
if (a.BigitLength() + 1 < c.BigitLength()) return -1; |
||||
|
if (a.BigitLength() > c.BigitLength()) return +1; |
||||
|
// The exponent encodes 0-bigits. So if there are more 0-digits in 'a' than
|
||||
|
// 'b' has digits, then the bigit-length of 'a'+'b' must be equal to the one
|
||||
|
// of 'a'.
|
||||
|
if (a.exponent_ >= b.BigitLength() && a.BigitLength() < c.BigitLength()) { |
||||
|
return -1; |
||||
|
} |
||||
|
|
||||
|
Chunk borrow = 0; |
||||
|
// Starting at min_exponent all digits are == 0. So no need to compare them.
|
||||
|
int min_exponent = Min(Min(a.exponent_, b.exponent_), c.exponent_); |
||||
|
for (int i = c.BigitLength() - 1; i >= min_exponent; --i) { |
||||
|
Chunk chunk_a = a.BigitAt(i); |
||||
|
Chunk chunk_b = b.BigitAt(i); |
||||
|
Chunk chunk_c = c.BigitAt(i); |
||||
|
Chunk sum = chunk_a + chunk_b; |
||||
|
if (sum > chunk_c + borrow) { |
||||
|
return +1; |
||||
|
} else { |
||||
|
borrow = chunk_c + borrow - sum; |
||||
|
if (borrow > 1) return -1; |
||||
|
borrow <<= kBigitSize; |
||||
|
} |
||||
|
} |
||||
|
if (borrow == 0) return 0; |
||||
|
return -1; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Bignum::Clamp() { |
||||
|
while (used_digits_ > 0 && bigits_[used_digits_ - 1] == 0) { |
||||
|
used_digits_--; |
||||
|
} |
||||
|
if (used_digits_ == 0) { |
||||
|
// Zero.
|
||||
|
exponent_ = 0; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
bool Bignum::IsClamped() const { |
||||
|
return used_digits_ == 0 || bigits_[used_digits_ - 1] != 0; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Bignum::Zero() { |
||||
|
for (int i = 0; i < used_digits_; ++i) { |
||||
|
bigits_[i] = 0; |
||||
|
} |
||||
|
used_digits_ = 0; |
||||
|
exponent_ = 0; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Bignum::Align(const Bignum& other) { |
||||
|
if (exponent_ > other.exponent_) { |
||||
|
// If "X" represents a "hidden" digit (by the exponent) then we are in the
|
||||
|
// following case (a == this, b == other):
|
||||
|
// a: aaaaaaXXXX or a: aaaaaXXX
|
||||
|
// b: bbbbbbX b: bbbbbbbbXX
|
||||
|
// We replace some of the hidden digits (X) of a with 0 digits.
|
||||
|
// a: aaaaaa000X or a: aaaaa0XX
|
||||
|
int zero_digits = exponent_ - other.exponent_; |
||||
|
EnsureCapacity(used_digits_ + zero_digits); |
||||
|
for (int i = used_digits_ - 1; i >= 0; --i) { |
||||
|
bigits_[i + zero_digits] = bigits_[i]; |
||||
|
} |
||||
|
for (int i = 0; i < zero_digits; ++i) { |
||||
|
bigits_[i] = 0; |
||||
|
} |
||||
|
used_digits_ += zero_digits; |
||||
|
exponent_ -= zero_digits; |
||||
|
ASSERT(used_digits_ >= 0); |
||||
|
ASSERT(exponent_ >= 0); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Bignum::BigitsShiftLeft(int shift_amount) { |
||||
|
ASSERT(shift_amount < kBigitSize); |
||||
|
ASSERT(shift_amount >= 0); |
||||
|
Chunk carry = 0; |
||||
|
for (int i = 0; i < used_digits_; ++i) { |
||||
|
Chunk new_carry = bigits_[i] >> (kBigitSize - shift_amount); |
||||
|
bigits_[i] = ((bigits_[i] << shift_amount) + carry) & kBigitMask; |
||||
|
carry = new_carry; |
||||
|
} |
||||
|
if (carry != 0) { |
||||
|
bigits_[used_digits_] = carry; |
||||
|
used_digits_++; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Bignum::SubtractTimes(const Bignum& other, int factor) { |
||||
|
ASSERT(exponent_ <= other.exponent_); |
||||
|
if (factor < 3) { |
||||
|
for (int i = 0; i < factor; ++i) { |
||||
|
SubtractBignum(other); |
||||
|
} |
||||
|
return; |
||||
|
} |
||||
|
Chunk borrow = 0; |
||||
|
int exponent_diff = other.exponent_ - exponent_; |
||||
|
for (int i = 0; i < other.used_digits_; ++i) { |
||||
|
DoubleChunk product = static_cast<DoubleChunk>(factor) * other.bigits_[i]; |
||||
|
DoubleChunk remove = borrow + product; |
||||
|
Chunk difference = bigits_[i + exponent_diff] - (remove & kBigitMask); |
||||
|
bigits_[i + exponent_diff] = difference & kBigitMask; |
||||
|
borrow = static_cast<Chunk>((difference >> (kChunkSize - 1)) + |
||||
|
(remove >> kBigitSize)); |
||||
|
} |
||||
|
for (int i = other.used_digits_ + exponent_diff; i < used_digits_; ++i) { |
||||
|
if (borrow == 0) return; |
||||
|
Chunk difference = bigits_[i] - borrow; |
||||
|
bigits_[i] = difference & kBigitMask; |
||||
|
borrow = difference >> (kChunkSize - 1); |
||||
|
++i; |
||||
|
} |
||||
|
Clamp(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} } // namespace v8::internal
|
@ -0,0 +1,140 @@ |
|||||
|
// Copyright 2010 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.
|
||||
|
|
||||
|
#ifndef V8_BIGNUM_H_ |
||||
|
#define V8_BIGNUM_H_ |
||||
|
|
||||
|
namespace v8 { |
||||
|
namespace internal { |
||||
|
|
||||
|
class Bignum { |
||||
|
public: |
||||
|
// 3584 = 128 * 28. We can represent 2^3584 > 10^1000 accurately.
|
||||
|
// This bignum can encode much bigger numbers, since it contains an
|
||||
|
// exponent.
|
||||
|
static const int kMaxSignificantBits = 3584; |
||||
|
|
||||
|
Bignum(); |
||||
|
void AssignUInt16(uint16_t value); |
||||
|
void AssignUInt64(uint64_t value); |
||||
|
void AssignBignum(const Bignum& other); |
||||
|
|
||||
|
void AssignDecimalString(Vector<const char> value); |
||||
|
void AssignHexString(Vector<const char> value); |
||||
|
|
||||
|
void AssignPowerUInt16(uint16_t base, int exponent); |
||||
|
|
||||
|
void AddUInt16(uint16_t operand); |
||||
|
void AddUInt64(uint64_t operand); |
||||
|
void AddBignum(const Bignum& other); |
||||
|
// Precondition: this >= other.
|
||||
|
void SubtractBignum(const Bignum& other); |
||||
|
|
||||
|
void Square(); |
||||
|
void ShiftLeft(int shift_amount); |
||||
|
void MultiplyByUInt32(uint32_t factor); |
||||
|
void MultiplyByUInt64(uint64_t factor); |
||||
|
void MultiplyByPowerOfTen(int exponent); |
||||
|
void Times10() { return MultiplyByUInt32(10); } |
||||
|
// Pseudocode:
|
||||
|
// int result = this / other;
|
||||
|
// this = this % other;
|
||||
|
// In the worst case this function is in O(this/other).
|
||||
|
uint16_t DivideModuloIntBignum(const Bignum& other); |
||||
|
|
||||
|
bool ToHexString(char* buffer, int buffer_size) const; |
||||
|
|
||||
|
static int Compare(const Bignum& a, const Bignum& b); |
||||
|
static bool Equal(const Bignum& a, const Bignum& b) { |
||||
|
return Compare(a, b) == 0; |
||||
|
} |
||||
|
static bool LessEqual(const Bignum& a, const Bignum& b) { |
||||
|
return Compare(a, b) <= 0; |
||||
|
} |
||||
|
static bool Less(const Bignum& a, const Bignum& b) { |
||||
|
return Compare(a, b) < 0; |
||||
|
} |
||||
|
// Returns Compare(a + b, c);
|
||||
|
static int PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c); |
||||
|
// Returns a + b == c
|
||||
|
static bool PlusEqual(const Bignum& a, const Bignum& b, const Bignum& c) { |
||||
|
return PlusCompare(a, b, c) == 0; |
||||
|
} |
||||
|
// Returns a + b <= c
|
||||
|
static bool PlusLessEqual(const Bignum& a, const Bignum& b, const Bignum& c) { |
||||
|
return PlusCompare(a, b, c) <= 0; |
||||
|
} |
||||
|
// Returns a + b < c
|
||||
|
static bool PlusLess(const Bignum& a, const Bignum& b, const Bignum& c) { |
||||
|
return PlusCompare(a, b, c) < 0; |
||||
|
} |
||||
|
private: |
||||
|
typedef uint32_t Chunk; |
||||
|
typedef uint64_t DoubleChunk; |
||||
|
|
||||
|
static const int kChunkSize = sizeof(Chunk) * 8; |
||||
|
static const int kDoubleChunkSize = sizeof(DoubleChunk) * 8; |
||||
|
// With bigit size of 28 we loose some bits, but a double still fits easily
|
||||
|
// into two chunks, and more importantly we can use the Comba multiplication.
|
||||
|
static const int kBigitSize = 28; |
||||
|
static const Chunk kBigitMask = (1 << kBigitSize) - 1; |
||||
|
// Every instance allocates kBigitLength chunks on the stack. Bignums cannot
|
||||
|
// grow. There are no checks if the stack-allocated space is sufficient.
|
||||
|
static const int kBigitCapacity = kMaxSignificantBits / kBigitSize; |
||||
|
|
||||
|
void EnsureCapacity(int size) { |
||||
|
if (size > kBigitCapacity) { |
||||
|
UNREACHABLE(); |
||||
|
} |
||||
|
} |
||||
|
void Align(const Bignum& other); |
||||
|
void Clamp(); |
||||
|
bool IsClamped() const; |
||||
|
void Zero(); |
||||
|
// Requires this to have enough capacity (no tests done).
|
||||
|
// Updates used_digits_ if necessary.
|
||||
|
// by must be < kBigitSize.
|
||||
|
void BigitsShiftLeft(int shift_amount); |
||||
|
// BigitLength includes the "hidden" digits encoded in the exponent.
|
||||
|
int BigitLength() const { return used_digits_ + exponent_; } |
||||
|
Chunk BigitAt(int index) const; |
||||
|
void SubtractTimes(const Bignum& other, int factor); |
||||
|
|
||||
|
Chunk bigits_buffer_[kBigitCapacity]; |
||||
|
// A vector backed by bigits_buffer_. This way accesses to the array are
|
||||
|
// checked for out-of-bounds errors.
|
||||
|
Vector<Chunk> bigits_; |
||||
|
int used_digits_; |
||||
|
// The Bignum's value equals value(bigits_) * 2^(exponent_ * kBigitSize).
|
||||
|
int exponent_; |
||||
|
|
||||
|
DISALLOW_COPY_AND_ASSIGN(Bignum); |
||||
|
}; |
||||
|
|
||||
|
} } // namespace v8::internal
|
||||
|
|
||||
|
#endif // V8_BIGNUM_H_
|
@ -0,0 +1,141 @@ |
|||||
|
// Copyright 2010 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 "externalize-string-extension.h" |
||||
|
|
||||
|
namespace v8 { |
||||
|
namespace internal { |
||||
|
|
||||
|
template <typename Char, typename Base> |
||||
|
class SimpleStringResource : public Base { |
||||
|
public: |
||||
|
// Takes ownership of |data|.
|
||||
|
SimpleStringResource(Char* data, size_t length) |
||||
|
: data_(data), |
||||
|
length_(length) {} |
||||
|
|
||||
|
virtual ~SimpleStringResource() { delete[] data_; } |
||||
|
|
||||
|
virtual const Char* data() const { return data_; } |
||||
|
|
||||
|
virtual size_t length() const { return length_; } |
||||
|
|
||||
|
private: |
||||
|
Char* const data_; |
||||
|
const size_t length_; |
||||
|
}; |
||||
|
|
||||
|
|
||||
|
typedef SimpleStringResource<char, v8::String::ExternalAsciiStringResource> |
||||
|
SimpleAsciiStringResource; |
||||
|
typedef SimpleStringResource<uc16, v8::String::ExternalStringResource> |
||||
|
SimpleTwoByteStringResource; |
||||
|
|
||||
|
|
||||
|
const char* const ExternalizeStringExtension::kSource = |
||||
|
"native function externalizeString();" |
||||
|
"native function isAsciiString();"; |
||||
|
|
||||
|
|
||||
|
v8::Handle<v8::FunctionTemplate> ExternalizeStringExtension::GetNativeFunction( |
||||
|
v8::Handle<v8::String> str) { |
||||
|
if (strcmp(*v8::String::AsciiValue(str), "externalizeString") == 0) { |
||||
|
return v8::FunctionTemplate::New(ExternalizeStringExtension::Externalize); |
||||
|
} else { |
||||
|
ASSERT(strcmp(*v8::String::AsciiValue(str), "isAsciiString") == 0); |
||||
|
return v8::FunctionTemplate::New(ExternalizeStringExtension::IsAscii); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
v8::Handle<v8::Value> ExternalizeStringExtension::Externalize( |
||||
|
const v8::Arguments& args) { |
||||
|
if (args.Length() < 1 || !args[0]->IsString()) { |
||||
|
return v8::ThrowException(v8::String::New( |
||||
|
"First parameter to externalizeString() must be a string.")); |
||||
|
} |
||||
|
bool force_two_byte = false; |
||||
|
if (args.Length() >= 2) { |
||||
|
if (args[1]->IsBoolean()) { |
||||
|
force_two_byte = args[1]->BooleanValue(); |
||||
|
} else { |
||||
|
return v8::ThrowException(v8::String::New( |
||||
|
"Second parameter to externalizeString() must be a boolean.")); |
||||
|
} |
||||
|
} |
||||
|
bool result = false; |
||||
|
Handle<String> string = Utils::OpenHandle(*args[0].As<v8::String>()); |
||||
|
if (string->IsExternalString()) { |
||||
|
return v8::ThrowException(v8::String::New( |
||||
|
"externalizeString() can't externalize twice.")); |
||||
|
} |
||||
|
if (string->IsAsciiRepresentation() && !force_two_byte) { |
||||
|
char* data = new char[string->length()]; |
||||
|
String::WriteToFlat(*string, data, 0, string->length()); |
||||
|
SimpleAsciiStringResource* resource = new SimpleAsciiStringResource( |
||||
|
data, string->length()); |
||||
|
result = string->MakeExternal(resource); |
||||
|
if (result && !string->IsSymbol()) { |
||||
|
i::ExternalStringTable::AddString(*string); |
||||
|
} |
||||
|
if (!result) delete resource; |
||||
|
} else { |
||||
|
uc16* data = new uc16[string->length()]; |
||||
|
String::WriteToFlat(*string, data, 0, string->length()); |
||||
|
SimpleTwoByteStringResource* resource = new SimpleTwoByteStringResource( |
||||
|
data, string->length()); |
||||
|
result = string->MakeExternal(resource); |
||||
|
if (result && !string->IsSymbol()) { |
||||
|
i::ExternalStringTable::AddString(*string); |
||||
|
} |
||||
|
if (!result) delete resource; |
||||
|
} |
||||
|
if (!result) { |
||||
|
return v8::ThrowException(v8::String::New("externalizeString() failed.")); |
||||
|
} |
||||
|
return v8::Undefined(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
v8::Handle<v8::Value> ExternalizeStringExtension::IsAscii( |
||||
|
const v8::Arguments& args) { |
||||
|
if (args.Length() != 1 || !args[0]->IsString()) { |
||||
|
return v8::ThrowException(v8::String::New( |
||||
|
"isAsciiString() requires a single string argument.")); |
||||
|
} |
||||
|
return Utils::OpenHandle(*args[0].As<v8::String>())->IsAsciiRepresentation() ? |
||||
|
v8::True() : v8::False(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void ExternalizeStringExtension::Register() { |
||||
|
static ExternalizeStringExtension externalize_extension; |
||||
|
static v8::DeclareExtension externalize_extension_declaration( |
||||
|
&externalize_extension); |
||||
|
} |
||||
|
|
||||
|
} } // namespace v8::internal
|
@ -0,0 +1,50 @@ |
|||||
|
// Copyright 2010 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.
|
||||
|
|
||||
|
#ifndef V8_EXTENSIONS_EXTERNALIZE_STRING_EXTENSION_H_ |
||||
|
#define V8_EXTENSIONS_EXTERNALIZE_STRING_EXTENSION_H_ |
||||
|
|
||||
|
#include "v8.h" |
||||
|
|
||||
|
namespace v8 { |
||||
|
namespace internal { |
||||
|
|
||||
|
class ExternalizeStringExtension : public v8::Extension { |
||||
|
public: |
||||
|
ExternalizeStringExtension() : v8::Extension("v8/externalize", kSource) {} |
||||
|
virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
||||
|
v8::Handle<v8::String> name); |
||||
|
static v8::Handle<v8::Value> Externalize(const v8::Arguments& args); |
||||
|
static v8::Handle<v8::Value> IsAscii(const v8::Arguments& args); |
||||
|
static void Register(); |
||||
|
private: |
||||
|
static const char* const kSource; |
||||
|
}; |
||||
|
|
||||
|
} } // namespace v8::internal
|
||||
|
|
||||
|
#endif // V8_EXTENSIONS_EXTERNALIZE_STRING_EXTENSION_H_
|
@ -0,0 +1,54 @@ |
|||||
|
// Copyright 2010 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 "gc-extension.h" |
||||
|
|
||||
|
namespace v8 { |
||||
|
namespace internal { |
||||
|
|
||||
|
const char* const GCExtension::kSource = "native function gc();"; |
||||
|
|
||||
|
|
||||
|
v8::Handle<v8::FunctionTemplate> GCExtension::GetNativeFunction( |
||||
|
v8::Handle<v8::String> str) { |
||||
|
return v8::FunctionTemplate::New(GCExtension::GC); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
v8::Handle<v8::Value> GCExtension::GC(const v8::Arguments& args) { |
||||
|
// All allocation spaces other than NEW_SPACE have the same effect.
|
||||
|
Heap::CollectAllGarbage(false); |
||||
|
return v8::Undefined(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void GCExtension::Register() { |
||||
|
static GCExtension gc_extension; |
||||
|
static v8::DeclareExtension gc_extension_declaration(&gc_extension); |
||||
|
} |
||||
|
|
||||
|
} } // namespace v8::internal
|
@ -0,0 +1,49 @@ |
|||||
|
// Copyright 2010 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.
|
||||
|
|
||||
|
#ifndef V8_EXTENSIONS_GC_EXTENSION_H_ |
||||
|
#define V8_EXTENSIONS_GC_EXTENSION_H_ |
||||
|
|
||||
|
#include "v8.h" |
||||
|
|
||||
|
namespace v8 { |
||||
|
namespace internal { |
||||
|
|
||||
|
class GCExtension : public v8::Extension { |
||||
|
public: |
||||
|
GCExtension() : v8::Extension("v8/gc", kSource) {} |
||||
|
virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
||||
|
v8::Handle<v8::String> name); |
||||
|
static v8::Handle<v8::Value> GC(const v8::Arguments& args); |
||||
|
static void Register(); |
||||
|
private: |
||||
|
static const char* const kSource; |
||||
|
}; |
||||
|
|
||||
|
} } // namespace v8::internal
|
||||
|
|
||||
|
#endif // V8_EXTENSIONS_GC_EXTENSION_H_
|
File diff suppressed because it is too large
File diff suppressed because it is too large
File diff suppressed because it is too large
@ -0,0 +1,195 @@ |
|||||
|
// Copyright 2010 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.
|
||||
|
|
||||
|
// Features shared by parsing and pre-parsing scanners.
|
||||
|
|
||||
|
#include "../include/v8stdint.h" |
||||
|
#include "scanner-base.h" |
||||
|
|
||||
|
namespace v8 { |
||||
|
namespace internal { |
||||
|
|
||||
|
// ----------------------------------------------------------------------------
|
||||
|
// Character predicates
|
||||
|
|
||||
|
unibrow::Predicate<IdentifierStart, 128> ScannerConstants::kIsIdentifierStart; |
||||
|
unibrow::Predicate<IdentifierPart, 128> ScannerConstants::kIsIdentifierPart; |
||||
|
unibrow::Predicate<unibrow::WhiteSpace, 128> ScannerConstants::kIsWhiteSpace; |
||||
|
unibrow::Predicate<unibrow::LineTerminator, 128> |
||||
|
ScannerConstants::kIsLineTerminator; |
||||
|
|
||||
|
StaticResource<ScannerConstants::Utf8Decoder> ScannerConstants::utf8_decoder_; |
||||
|
|
||||
|
// Compound predicates.
|
||||
|
|
||||
|
bool ScannerConstants::IsIdentifier(unibrow::CharacterStream* buffer) { |
||||
|
// Checks whether the buffer contains an identifier (no escape).
|
||||
|
if (!buffer->has_more()) return false; |
||||
|
if (!kIsIdentifierStart.get(buffer->GetNext())) { |
||||
|
return false; |
||||
|
} |
||||
|
while (buffer->has_more()) { |
||||
|
if (!kIsIdentifierPart.get(buffer->GetNext())) { |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
// ----------------------------------------------------------------------------
|
||||
|
// Keyword Matcher
|
||||
|
|
||||
|
KeywordMatcher::FirstState KeywordMatcher::first_states_[] = { |
||||
|
{ "break", KEYWORD_PREFIX, Token::BREAK }, |
||||
|
{ NULL, C, Token::ILLEGAL }, |
||||
|
{ NULL, D, Token::ILLEGAL }, |
||||
|
{ "else", KEYWORD_PREFIX, Token::ELSE }, |
||||
|
{ NULL, F, Token::ILLEGAL }, |
||||
|
{ NULL, UNMATCHABLE, Token::ILLEGAL }, |
||||
|
{ NULL, UNMATCHABLE, Token::ILLEGAL }, |
||||
|
{ NULL, I, Token::ILLEGAL }, |
||||
|
{ NULL, UNMATCHABLE, Token::ILLEGAL }, |
||||
|
{ NULL, UNMATCHABLE, Token::ILLEGAL }, |
||||
|
{ NULL, UNMATCHABLE, Token::ILLEGAL }, |
||||
|
{ NULL, UNMATCHABLE, Token::ILLEGAL }, |
||||
|
{ NULL, N, Token::ILLEGAL }, |
||||
|
{ NULL, UNMATCHABLE, Token::ILLEGAL }, |
||||
|
{ NULL, UNMATCHABLE, Token::ILLEGAL }, |
||||
|
{ NULL, UNMATCHABLE, Token::ILLEGAL }, |
||||
|
{ "return", KEYWORD_PREFIX, Token::RETURN }, |
||||
|
{ "switch", KEYWORD_PREFIX, Token::SWITCH }, |
||||
|
{ NULL, T, Token::ILLEGAL }, |
||||
|
{ NULL, UNMATCHABLE, Token::ILLEGAL }, |
||||
|
{ NULL, V, Token::ILLEGAL }, |
||||
|
{ NULL, W, Token::ILLEGAL } |
||||
|
}; |
||||
|
|
||||
|
|
||||
|
void KeywordMatcher::Step(unibrow::uchar input) { |
||||
|
switch (state_) { |
||||
|
case INITIAL: { |
||||
|
// matching the first character is the only state with significant fanout.
|
||||
|
// Match only lower-case letters in range 'b'..'w'.
|
||||
|
unsigned int offset = input - kFirstCharRangeMin; |
||||
|
if (offset < kFirstCharRangeLength) { |
||||
|
state_ = first_states_[offset].state; |
||||
|
if (state_ == KEYWORD_PREFIX) { |
||||
|
keyword_ = first_states_[offset].keyword; |
||||
|
counter_ = 1; |
||||
|
keyword_token_ = first_states_[offset].token; |
||||
|
} |
||||
|
return; |
||||
|
} |
||||
|
break; |
||||
|
} |
||||
|
case KEYWORD_PREFIX: |
||||
|
if (static_cast<unibrow::uchar>(keyword_[counter_]) == input) { |
||||
|
counter_++; |
||||
|
if (keyword_[counter_] == '\0') { |
||||
|
state_ = KEYWORD_MATCHED; |
||||
|
token_ = keyword_token_; |
||||
|
} |
||||
|
return; |
||||
|
} |
||||
|
break; |
||||
|
case KEYWORD_MATCHED: |
||||
|
token_ = Token::IDENTIFIER; |
||||
|
break; |
||||
|
case C: |
||||
|
if (MatchState(input, 'a', CA)) return; |
||||
|
if (MatchState(input, 'o', CO)) return; |
||||
|
break; |
||||
|
case CA: |
||||
|
if (MatchKeywordStart(input, "case", 2, Token::CASE)) return; |
||||
|
if (MatchKeywordStart(input, "catch", 2, Token::CATCH)) return; |
||||
|
break; |
||||
|
case CO: |
||||
|
if (MatchState(input, 'n', CON)) return; |
||||
|
break; |
||||
|
case CON: |
||||
|
if (MatchKeywordStart(input, "const", 3, Token::CONST)) return; |
||||
|
if (MatchKeywordStart(input, "continue", 3, Token::CONTINUE)) return; |
||||
|
break; |
||||
|
case D: |
||||
|
if (MatchState(input, 'e', DE)) return; |
||||
|
if (MatchKeyword(input, 'o', KEYWORD_MATCHED, Token::DO)) return; |
||||
|
break; |
||||
|
case DE: |
||||
|
if (MatchKeywordStart(input, "debugger", 2, Token::DEBUGGER)) return; |
||||
|
if (MatchKeywordStart(input, "default", 2, Token::DEFAULT)) return; |
||||
|
if (MatchKeywordStart(input, "delete", 2, Token::DELETE)) return; |
||||
|
break; |
||||
|
case F: |
||||
|
if (MatchKeywordStart(input, "false", 1, Token::FALSE_LITERAL)) return; |
||||
|
if (MatchKeywordStart(input, "finally", 1, Token::FINALLY)) return; |
||||
|
if (MatchKeywordStart(input, "for", 1, Token::FOR)) return; |
||||
|
if (MatchKeywordStart(input, "function", 1, Token::FUNCTION)) return; |
||||
|
break; |
||||
|
case I: |
||||
|
if (MatchKeyword(input, 'f', KEYWORD_MATCHED, Token::IF)) return; |
||||
|
if (MatchKeyword(input, 'n', IN, Token::IN)) return; |
||||
|
break; |
||||
|
case IN: |
||||
|
token_ = Token::IDENTIFIER; |
||||
|
if (MatchKeywordStart(input, "instanceof", 2, Token::INSTANCEOF)) { |
||||
|
return; |
||||
|
} |
||||
|
break; |
||||
|
case N: |
||||
|
if (MatchKeywordStart(input, "native", 1, Token::NATIVE)) return; |
||||
|
if (MatchKeywordStart(input, "new", 1, Token::NEW)) return; |
||||
|
if (MatchKeywordStart(input, "null", 1, Token::NULL_LITERAL)) return; |
||||
|
break; |
||||
|
case T: |
||||
|
if (MatchState(input, 'h', TH)) return; |
||||
|
if (MatchState(input, 'r', TR)) return; |
||||
|
if (MatchKeywordStart(input, "typeof", 1, Token::TYPEOF)) return; |
||||
|
break; |
||||
|
case TH: |
||||
|
if (MatchKeywordStart(input, "this", 2, Token::THIS)) return; |
||||
|
if (MatchKeywordStart(input, "throw", 2, Token::THROW)) return; |
||||
|
break; |
||||
|
case TR: |
||||
|
if (MatchKeywordStart(input, "true", 2, Token::TRUE_LITERAL)) return; |
||||
|
if (MatchKeyword(input, 'y', KEYWORD_MATCHED, Token::TRY)) return; |
||||
|
break; |
||||
|
case V: |
||||
|
if (MatchKeywordStart(input, "var", 1, Token::VAR)) return; |
||||
|
if (MatchKeywordStart(input, "void", 1, Token::VOID)) return; |
||||
|
break; |
||||
|
case W: |
||||
|
if (MatchKeywordStart(input, "while", 1, Token::WHILE)) return; |
||||
|
if (MatchKeywordStart(input, "with", 1, Token::WITH)) return; |
||||
|
break; |
||||
|
case UNMATCHABLE: |
||||
|
break; |
||||
|
} |
||||
|
// On fallthrough, it's a failure.
|
||||
|
state_ = UNMATCHABLE; |
||||
|
} |
||||
|
|
||||
|
} } // namespace v8::internal
|
@ -0,0 +1,206 @@ |
|||||
|
// Copyright 2010 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.
|
||||
|
|
||||
|
// Features shared by parsing and pre-parsing scanners.
|
||||
|
|
||||
|
#ifndef V8_SCANNER_BASE_H_ |
||||
|
#define V8_SCANNER_BASE_H_ |
||||
|
|
||||
|
#include "globals.h" |
||||
|
#include "checks.h" |
||||
|
#include "allocation.h" |
||||
|
#include "token.h" |
||||
|
#include "unicode-inl.h" |
||||
|
#include "char-predicates.h" |
||||
|
#include "utils.h" |
||||
|
|
||||
|
namespace v8 { |
||||
|
namespace internal { |
||||
|
|
||||
|
// Interface through which the scanner reads characters from the input source.
|
||||
|
class UTF16Buffer { |
||||
|
public: |
||||
|
UTF16Buffer(); |
||||
|
virtual ~UTF16Buffer() {} |
||||
|
|
||||
|
virtual void PushBack(uc32 ch) = 0; |
||||
|
// Returns a value < 0 when the buffer end is reached.
|
||||
|
virtual uc32 Advance() = 0; |
||||
|
virtual void SeekForward(int pos) = 0; |
||||
|
|
||||
|
int pos() const { return pos_; } |
||||
|
|
||||
|
protected: |
||||
|
int pos_; // Current position in the buffer.
|
||||
|
int end_; // Position where scanning should stop (EOF).
|
||||
|
}; |
||||
|
|
||||
|
|
||||
|
class ScannerConstants : AllStatic { |
||||
|
public: |
||||
|
typedef unibrow::Utf8InputBuffer<1024> Utf8Decoder; |
||||
|
|
||||
|
static StaticResource<Utf8Decoder>* utf8_decoder() { |
||||
|
return &utf8_decoder_; |
||||
|
} |
||||
|
|
||||
|
static unibrow::Predicate<IdentifierStart, 128> kIsIdentifierStart; |
||||
|
static unibrow::Predicate<IdentifierPart, 128> kIsIdentifierPart; |
||||
|
static unibrow::Predicate<unibrow::LineTerminator, 128> kIsLineTerminator; |
||||
|
static unibrow::Predicate<unibrow::WhiteSpace, 128> kIsWhiteSpace; |
||||
|
|
||||
|
static bool IsIdentifier(unibrow::CharacterStream* buffer); |
||||
|
|
||||
|
private: |
||||
|
static StaticResource<Utf8Decoder> utf8_decoder_; |
||||
|
}; |
||||
|
|
||||
|
|
||||
|
class KeywordMatcher { |
||||
|
// Incrementally recognize keywords.
|
||||
|
//
|
||||
|
// Recognized keywords:
|
||||
|
// break case catch const* continue debugger* default delete do else
|
||||
|
// finally false for function if in instanceof native* new null
|
||||
|
// return switch this throw true try typeof var void while with
|
||||
|
//
|
||||
|
// *: Actually "future reserved keywords". These are the only ones we
|
||||
|
// recognize, the remaining are allowed as identifiers.
|
||||
|
// In ES5 strict mode, we should disallow all reserved keywords.
|
||||
|
public: |
||||
|
KeywordMatcher() |
||||
|
: state_(INITIAL), |
||||
|
token_(Token::IDENTIFIER), |
||||
|
keyword_(NULL), |
||||
|
counter_(0), |
||||
|
keyword_token_(Token::ILLEGAL) {} |
||||
|
|
||||
|
Token::Value token() { return token_; } |
||||
|
|
||||
|
inline void AddChar(unibrow::uchar input) { |
||||
|
if (state_ != UNMATCHABLE) { |
||||
|
Step(input); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void Fail() { |
||||
|
token_ = Token::IDENTIFIER; |
||||
|
state_ = UNMATCHABLE; |
||||
|
} |
||||
|
|
||||
|
private: |
||||
|
enum State { |
||||
|
UNMATCHABLE, |
||||
|
INITIAL, |
||||
|
KEYWORD_PREFIX, |
||||
|
KEYWORD_MATCHED, |
||||
|
C, |
||||
|
CA, |
||||
|
CO, |
||||
|
CON, |
||||
|
D, |
||||
|
DE, |
||||
|
F, |
||||
|
I, |
||||
|
IN, |
||||
|
N, |
||||
|
T, |
||||
|
TH, |
||||
|
TR, |
||||
|
V, |
||||
|
W |
||||
|
}; |
||||
|
|
||||
|
struct FirstState { |
||||
|
const char* keyword; |
||||
|
State state; |
||||
|
Token::Value token; |
||||
|
}; |
||||
|
|
||||
|
// Range of possible first characters of a keyword.
|
||||
|
static const unsigned int kFirstCharRangeMin = 'b'; |
||||
|
static const unsigned int kFirstCharRangeMax = 'w'; |
||||
|
static const unsigned int kFirstCharRangeLength = |
||||
|
kFirstCharRangeMax - kFirstCharRangeMin + 1; |
||||
|
// State map for first keyword character range.
|
||||
|
static FirstState first_states_[kFirstCharRangeLength]; |
||||
|
|
||||
|
// If input equals keyword's character at position, continue matching keyword
|
||||
|
// from that position.
|
||||
|
inline bool MatchKeywordStart(unibrow::uchar input, |
||||
|
const char* keyword, |
||||
|
int position, |
||||
|
Token::Value token_if_match) { |
||||
|
if (input == static_cast<unibrow::uchar>(keyword[position])) { |
||||
|
state_ = KEYWORD_PREFIX; |
||||
|
this->keyword_ = keyword; |
||||
|
this->counter_ = position + 1; |
||||
|
this->keyword_token_ = token_if_match; |
||||
|
return true; |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
// If input equals match character, transition to new state and return true.
|
||||
|
inline bool MatchState(unibrow::uchar input, char match, State new_state) { |
||||
|
if (input == static_cast<unibrow::uchar>(match)) { |
||||
|
state_ = new_state; |
||||
|
return true; |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
inline bool MatchKeyword(unibrow::uchar input, |
||||
|
char match, |
||||
|
State new_state, |
||||
|
Token::Value keyword_token) { |
||||
|
if (input != static_cast<unibrow::uchar>(match)) { |
||||
|
return false; |
||||
|
} |
||||
|
state_ = new_state; |
||||
|
token_ = keyword_token; |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
void Step(unibrow::uchar input); |
||||
|
|
||||
|
// Current state.
|
||||
|
State state_; |
||||
|
// Token for currently added characters.
|
||||
|
Token::Value token_; |
||||
|
|
||||
|
// Matching a specific keyword string (there is only one possible valid
|
||||
|
// keyword with the current prefix).
|
||||
|
const char* keyword_; |
||||
|
int counter_; |
||||
|
Token::Value keyword_token_; |
||||
|
}; |
||||
|
|
||||
|
|
||||
|
} } // namespace v8::internal
|
||||
|
|
||||
|
#endif // V8_SCANNER_BASE_H_
|
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue