Upload Kmake

This commit is contained in:
Gorochu
2026-05-26 23:36:42 -07:00
parent ba051b2f74
commit 555ec72358
41615 changed files with 13344630 additions and 1 deletions

View File

@ -0,0 +1,115 @@
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <cmath>
#include "src/execution/isolate.h"
#include "src/heap/factory.h"
#include "src/numbers/conversions.h"
#include "src/objects/bigint.h"
#include "src/objects/objects-inl.h"
#include "test/unittests/test-utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace v8 {
namespace internal {
using BigIntWithIsolate = TestWithIsolate;
void Compare(DirectHandle<BigInt> x, double value, ComparisonResult expected) {
CHECK_EQ(expected, BigInt::CompareToDouble(x, value));
}
DirectHandle<BigInt> NewFromInt(Isolate* isolate, int value) {
DirectHandle<Smi> smi_value(Smi::FromInt(value), isolate);
return BigInt::FromNumber(isolate, smi_value).ToHandleChecked();
}
TEST_F(BigIntWithIsolate, CompareToDouble) {
DirectHandle<BigInt> zero = NewFromInt(isolate(), 0);
DirectHandle<BigInt> one = NewFromInt(isolate(), 1);
DirectHandle<BigInt> minus_one = NewFromInt(isolate(), -1);
// Non-finite doubles.
Compare(zero, std::nan(""), ComparisonResult::kUndefined);
Compare(one, INFINITY, ComparisonResult::kLessThan);
Compare(one, -INFINITY, ComparisonResult::kGreaterThan);
// Unequal sign.
Compare(one, -1, ComparisonResult::kGreaterThan);
Compare(minus_one, 1, ComparisonResult::kLessThan);
// Cases involving zero.
Compare(zero, 0, ComparisonResult::kEqual);
Compare(zero, -0, ComparisonResult::kEqual);
Compare(one, 0, ComparisonResult::kGreaterThan);
Compare(minus_one, 0, ComparisonResult::kLessThan);
Compare(zero, 1, ComparisonResult::kLessThan);
Compare(zero, -1, ComparisonResult::kGreaterThan);
// Small doubles.
Compare(zero, 0.25, ComparisonResult::kLessThan);
Compare(one, 0.5, ComparisonResult::kGreaterThan);
Compare(one, -0.5, ComparisonResult::kGreaterThan);
Compare(zero, -0.25, ComparisonResult::kGreaterThan);
Compare(minus_one, -0.5, ComparisonResult::kLessThan);
// Different bit lengths.
DirectHandle<BigInt> four = NewFromInt(isolate(), 4);
DirectHandle<BigInt> minus_five = NewFromInt(isolate(), -5);
Compare(four, 3.9, ComparisonResult::kGreaterThan);
Compare(four, 1.5, ComparisonResult::kGreaterThan);
Compare(four, 8, ComparisonResult::kLessThan);
Compare(four, 16, ComparisonResult::kLessThan);
Compare(minus_five, -4.9, ComparisonResult::kLessThan);
Compare(minus_five, -4, ComparisonResult::kLessThan);
Compare(minus_five, -25, ComparisonResult::kGreaterThan);
// Same bit length, difference in first digit.
double big_double = 4428155326412785451008.0;
DirectHandle<BigInt> big =
BigIntLiteral(isolate(), "0xF10D00000000000000").ToHandleChecked();
Compare(big, big_double, ComparisonResult::kGreaterThan);
big = BigIntLiteral(isolate(), "0xE00D00000000000000").ToHandleChecked();
Compare(big, big_double, ComparisonResult::kLessThan);
double other_double = -13758438578910658560.0;
DirectHandle<BigInt> other =
BigIntLiteral(isolate(), "-0xBEEFC1FE00000000").ToHandleChecked();
Compare(other, other_double, ComparisonResult::kGreaterThan);
other = BigIntLiteral(isolate(), "-0xBEEFCBFE00000000").ToHandleChecked();
Compare(other, other_double, ComparisonResult::kLessThan);
// Same bit length, difference in non-first digit.
big = BigIntLiteral(isolate(), "0xF00D00000000000001").ToHandleChecked();
Compare(big, big_double, ComparisonResult::kGreaterThan);
big = BigIntLiteral(isolate(), "0xF00A00000000000000").ToHandleChecked();
Compare(big, big_double, ComparisonResult::kLessThan);
other = BigIntLiteral(isolate(), "-0xBEEFCAFE00000001").ToHandleChecked();
Compare(other, other_double, ComparisonResult::kLessThan);
// Same bit length, difference in fractional part.
Compare(one, 1.5, ComparisonResult::kLessThan);
Compare(minus_one, -1.25, ComparisonResult::kGreaterThan);
big = NewFromInt(isolate(), 0xF00D00);
Compare(big, 15731968.125, ComparisonResult::kLessThan);
Compare(big, 15731967.875, ComparisonResult::kGreaterThan);
big = BigIntLiteral(isolate(), "0x123456789AB").ToHandleChecked();
Compare(big, 1250999896491.125, ComparisonResult::kLessThan);
// Equality!
Compare(one, 1, ComparisonResult::kEqual);
Compare(minus_one, -1, ComparisonResult::kEqual);
big = BigIntLiteral(isolate(), "0xF00D00000000000000").ToHandleChecked();
Compare(big, big_double, ComparisonResult::kEqual);
DirectHandle<BigInt> two_52 =
BigIntLiteral(isolate(), "0x10000000000000").ToHandleChecked();
Compare(two_52, 4503599627370496.0, ComparisonResult::kEqual);
}
} // namespace internal
} // namespace v8

View File

@ -0,0 +1,586 @@
// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/numbers/conversions.h"
#include <stdlib.h>
#include "src/base/platform/platform.h"
#include "src/base/vector.h"
#include "src/execution/isolate.h"
#include "src/heap/factory-inl.h"
#include "src/init/v8.h"
#include "src/objects/heap-number-inl.h"
#include "src/objects/objects.h"
#include "src/objects/smi.h"
#include "test/unittests/test-utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace v8 {
namespace internal {
namespace interpreter {
class ConversionsTest : public TestWithIsolate {
public:
ConversionsTest() = default;
~ConversionsTest() override = default;
SourcePosition toPos(int offset) {
return SourcePosition(offset, offset % 10 - 1);
}
void CheckNonArrayIndex(bool expected, const char* chars) {
auto isolate = i_isolate();
auto string = isolate->factory()->NewStringFromAsciiChecked(chars);
CHECK_EQ(expected, IsSpecialIndex(*string));
}
};
TEST_F(ConversionsTest, Hex) {
CHECK_EQ(0.0, StringToDouble("0x0", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(0.0, StringToDouble("0X0", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(1.0, StringToDouble("0x1", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(16.0, StringToDouble("0x10", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(255.0, StringToDouble("0xFF", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(175.0, StringToDouble("0xAF", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(0.0, HexStringToDouble(base::OneByteVector("0x0")));
CHECK_EQ(0.0, HexStringToDouble(base::OneByteVector("0X0")));
CHECK_EQ(1.0, HexStringToDouble(base::OneByteVector("0x1")));
CHECK_EQ(16.0, HexStringToDouble(base::OneByteVector("0x10")));
CHECK_EQ(255.0, HexStringToDouble(base::OneByteVector("0xFF")));
CHECK_EQ(175.0, HexStringToDouble(base::OneByteVector("0xAF")));
}
TEST_F(ConversionsTest, Octal) {
CHECK_EQ(0.0, StringToDouble("0o0", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(0.0, StringToDouble("0O0", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(1.0, StringToDouble("0o1", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(7.0, StringToDouble("0o7", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(8.0, StringToDouble("0o10", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(63.0, StringToDouble("0o77", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(0.0, OctalStringToDouble(base::OneByteVector("0o0")));
CHECK_EQ(0.0, OctalStringToDouble(base::OneByteVector("0O0")));
CHECK_EQ(1.0, OctalStringToDouble(base::OneByteVector("0o1")));
CHECK_EQ(7.0, OctalStringToDouble(base::OneByteVector("0o7")));
CHECK_EQ(8.0, OctalStringToDouble(base::OneByteVector("0o10")));
CHECK_EQ(63.0, OctalStringToDouble(base::OneByteVector("0o77")));
const double x = 010000000000; // Power of 2, no rounding errors.
CHECK_EQ(x * x * x * x * x,
OctalStringToDouble(base::OneByteVector("0o01"
"0000000000"
"0000000000"
"0000000000"
"0000000000"
"0000000000")));
}
TEST_F(ConversionsTest, ImplicitOctal) {
CHECK_EQ(0.0, ImplicitOctalStringToDouble(base::OneByteVector("0")));
CHECK_EQ(0.0, ImplicitOctalStringToDouble(base::OneByteVector("00")));
CHECK_EQ(1.0, ImplicitOctalStringToDouble(base::OneByteVector("01")));
CHECK_EQ(7.0, ImplicitOctalStringToDouble(base::OneByteVector("07")));
CHECK_EQ(8.0, ImplicitOctalStringToDouble(base::OneByteVector("010")));
CHECK_EQ(63.0, ImplicitOctalStringToDouble(base::OneByteVector("077")));
CHECK_EQ(0.0, StringToDouble("0", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(0.0, StringToDouble("00", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(1.0, StringToDouble("01", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(7.0, StringToDouble("07", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(10.0, StringToDouble("010", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(77.0, StringToDouble("077", ALLOW_NON_DECIMAL_PREFIX));
const double x = 010000000000; // Power of 2, no rounding errors.
CHECK_EQ(x * x * x * x * x,
ImplicitOctalStringToDouble(base::OneByteVector("01"
"0000000000"
"0000000000"
"0000000000"
"0000000000"
"0000000000")));
}
TEST_F(ConversionsTest, Binary) {
CHECK_EQ(0.0, StringToDouble("0b0", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(0.0, StringToDouble("0B0", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(1.0, StringToDouble("0b1", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(2.0, StringToDouble("0b10", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(3.0, StringToDouble("0b11", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(0.0, BinaryStringToDouble(base::OneByteVector("0b0")));
CHECK_EQ(0.0, BinaryStringToDouble(base::OneByteVector("0B0")));
CHECK_EQ(1.0, BinaryStringToDouble(base::OneByteVector("0b1")));
CHECK_EQ(2.0, BinaryStringToDouble(base::OneByteVector("0b10")));
CHECK_EQ(3.0, BinaryStringToDouble(base::OneByteVector("0b11")));
}
TEST_F(ConversionsTest, MalformedOctal) {
CHECK_EQ(8.0, StringToDouble("08", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(81.0, StringToDouble("081", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(78.0, StringToDouble("078", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(7.7, StringToDouble("07.7", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(7.8, StringToDouble("07.8", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(7e8, StringToDouble("07e8", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(7e7, StringToDouble("07e7", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(8.7, StringToDouble("08.7", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(8e7, StringToDouble("08e7", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(0.001, StringToDouble("0.001", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(0.713, StringToDouble("0.713", ALLOW_NON_DECIMAL_PREFIX));
}
TEST_F(ConversionsTest, TrailingJunk) {
CHECK_EQ(8.0, StringToDouble("8q", ALLOW_TRAILING_JUNK));
CHECK_EQ(10.0, StringToDouble("10e", ALLOW_TRAILING_JUNK));
CHECK_EQ(10.0, StringToDouble("10e-", ALLOW_TRAILING_JUNK));
}
TEST_F(ConversionsTest, NonStrDecimalLiteral) {
CHECK(std::isnan(StringToDouble(" ", NO_CONVERSION_FLAG,
std::numeric_limits<double>::quiet_NaN())));
CHECK(std::isnan(StringToDouble("", NO_CONVERSION_FLAG,
std::numeric_limits<double>::quiet_NaN())));
CHECK(std::isnan(StringToDouble(" ", NO_CONVERSION_FLAG,
std::numeric_limits<double>::quiet_NaN())));
CHECK_EQ(0.0, StringToDouble("", NO_CONVERSION_FLAG));
CHECK_EQ(0.0, StringToDouble(" ", NO_CONVERSION_FLAG));
}
TEST_F(ConversionsTest, IntegerStrLiteral) {
CHECK_EQ(0.0, StringToDouble("0.0", NO_CONVERSION_FLAG));
CHECK_EQ(0.0, StringToDouble("0", NO_CONVERSION_FLAG));
CHECK_EQ(0.0, StringToDouble("00", NO_CONVERSION_FLAG));
CHECK_EQ(0.0, StringToDouble("000", NO_CONVERSION_FLAG));
CHECK_EQ(1.0, StringToDouble("1", NO_CONVERSION_FLAG));
CHECK_EQ(-1.0, StringToDouble("-1", NO_CONVERSION_FLAG));
CHECK_EQ(-1.0, StringToDouble(" -1 ", NO_CONVERSION_FLAG));
CHECK_EQ(1.0, StringToDouble(" +1 ", NO_CONVERSION_FLAG));
CHECK(std::isnan(StringToDouble(" - 1 ", NO_CONVERSION_FLAG)));
CHECK(std::isnan(StringToDouble(" + 1 ", NO_CONVERSION_FLAG)));
CHECK_EQ(0.0, StringToDouble("0e0", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(0.0, StringToDouble("0e1", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(0.0, StringToDouble("0e-1", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(0.0, StringToDouble("0e-100000", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(0.0, StringToDouble("0e+100000", ALLOW_NON_DECIMAL_PREFIX));
CHECK_EQ(0.0, StringToDouble("0.", ALLOW_NON_DECIMAL_PREFIX));
}
TEST_F(ConversionsTest, LongNumberStr) {
CHECK_EQ(1e10, StringToDouble("1"
"0000000000",
NO_CONVERSION_FLAG));
CHECK_EQ(1e20, StringToDouble("1"
"0000000000"
"0000000000",
NO_CONVERSION_FLAG));
CHECK_EQ(1e60, StringToDouble("1"
"0000000000"
"0000000000"
"0000000000"
"0000000000"
"0000000000"
"0000000000",
NO_CONVERSION_FLAG));
CHECK_EQ(1e-2, StringToDouble("."
"0"
"1",
NO_CONVERSION_FLAG));
CHECK_EQ(1e-11, StringToDouble("."
"0000000000"
"1",
NO_CONVERSION_FLAG));
CHECK_EQ(1e-21, StringToDouble("."
"0000000000"
"0000000000"
"1",
NO_CONVERSION_FLAG));
CHECK_EQ(1e-61, StringToDouble("."
"0000000000"
"0000000000"
"0000000000"
"0000000000"
"0000000000"
"0000000000"
"1",
NO_CONVERSION_FLAG));
// x = 24414062505131248.0 and y = 24414062505131252.0 are representable in
// double. Check chat z = (x + y) / 2 is rounded to x...
CHECK_EQ(24414062505131248.0,
StringToDouble("24414062505131250.0", NO_CONVERSION_FLAG));
// ... and z = (x + y) / 2 + delta is rounded to y.
CHECK_EQ(24414062505131252.0,
StringToDouble("24414062505131250.000000001", NO_CONVERSION_FLAG));
}
TEST_F(ConversionsTest, MaximumSignificantDigits) {
char num[] =
"4.4501477170144020250819966727949918635852426585926051135169509"
"122872622312493126406953054127118942431783801370080830523154578"
"251545303238277269592368457430440993619708911874715081505094180"
"604803751173783204118519353387964161152051487413083163272520124"
"606023105869053620631175265621765214646643181420505164043632222"
"668006474326056011713528291579642227455489682133472873831754840"
"341397809846934151055619529382191981473003234105366170879223151"
"087335413188049110555339027884856781219017754500629806224571029"
"581637117459456877330110324211689177656713705497387108207822477"
"584250967061891687062782163335299376138075114200886249979505279"
"101870966346394401564490729731565935244123171539810221213221201"
"847003580761626016356864581135848683152156368691976240370422601"
"6998291015625000000000000000000000000000000000e-308";
CHECK_EQ(4.4501477170144017780491e-308,
StringToDouble(num, NO_CONVERSION_FLAG));
// Changes the result of strtod (at least in glibc implementation).
num[sizeof(num) - 8] = '1';
CHECK_EQ(4.4501477170144022721148e-308,
StringToDouble(num, NO_CONVERSION_FLAG));
}
TEST_F(ConversionsTest, MinimumExponent) {
// Same test but with different point-position.
char num[] =
"445014771701440202508199667279499186358524265859260511351695091"
"228726223124931264069530541271189424317838013700808305231545782"
"515453032382772695923684574304409936197089118747150815050941806"
"048037511737832041185193533879641611520514874130831632725201246"
"060231058690536206311752656217652146466431814205051640436322226"
"680064743260560117135282915796422274554896821334728738317548403"
"413978098469341510556195293821919814730032341053661708792231510"
"873354131880491105553390278848567812190177545006298062245710295"
"816371174594568773301103242116891776567137054973871082078224775"
"842509670618916870627821633352993761380751142008862499795052791"
"018709663463944015644907297315659352441231715398102212132212018"
"470035807616260163568645811358486831521563686919762403704226016"
"998291015625000000000000000000000000000000000e-1108";
CHECK_EQ(4.4501477170144017780491e-308,
StringToDouble(num, NO_CONVERSION_FLAG));
// Changes the result of strtod (at least in glibc implementation).
num[sizeof(num) - 8] = '1';
CHECK_EQ(4.4501477170144022721148e-308,
StringToDouble(num, NO_CONVERSION_FLAG));
}
TEST_F(ConversionsTest, MaximumExponent) {
char num[] = "0.16e309";
CHECK_EQ(1.59999999999999997765e+308,
StringToDouble(num, NO_CONVERSION_FLAG));
}
TEST_F(ConversionsTest, ExponentNumberStr) {
CHECK_EQ(1e1, StringToDouble("1e1", NO_CONVERSION_FLAG));
CHECK_EQ(1e1, StringToDouble("1e+1", NO_CONVERSION_FLAG));
CHECK_EQ(1e-1, StringToDouble("1e-1", NO_CONVERSION_FLAG));
CHECK_EQ(1e100, StringToDouble("1e+100", NO_CONVERSION_FLAG));
CHECK_EQ(1e-100, StringToDouble("1e-100", NO_CONVERSION_FLAG));
CHECK_EQ(1e-106, StringToDouble(".000001e-100", NO_CONVERSION_FLAG));
}
using OneBit1 = base::BitField<uint32_t, 0, 1>;
using OneBit2 = base::BitField<uint32_t, 7, 1>;
using EightBit1 = base::BitField<uint32_t, 0, 8>;
using EightBit2 = base::BitField<uint32_t, 13, 8>;
TEST_F(ConversionsTest, BitField) {
uint32_t x;
// One bit bit field can hold values 0 and 1.
CHECK(!OneBit1::is_valid(static_cast<uint32_t>(-1)));
CHECK(!OneBit2::is_valid(static_cast<uint32_t>(-1)));
for (unsigned i = 0; i < 2; i++) {
CHECK(OneBit1::is_valid(i));
x = OneBit1::encode(i);
CHECK_EQ(i, OneBit1::decode(x));
CHECK(OneBit2::is_valid(i));
x = OneBit2::encode(i);
CHECK_EQ(i, OneBit2::decode(x));
}
CHECK(!OneBit1::is_valid(2));
CHECK(!OneBit2::is_valid(2));
// Eight bit bit field can hold values from 0 tp 255.
CHECK(!EightBit1::is_valid(static_cast<uint32_t>(-1)));
CHECK(!EightBit2::is_valid(static_cast<uint32_t>(-1)));
for (unsigned i = 0; i < 256; i++) {
CHECK(EightBit1::is_valid(i));
x = EightBit1::encode(i);
CHECK_EQ(i, EightBit1::decode(x));
CHECK(EightBit2::is_valid(i));
x = EightBit2::encode(i);
CHECK_EQ(i, EightBit2::decode(x));
}
CHECK(!EightBit1::is_valid(256));
CHECK(!EightBit2::is_valid(256));
}
using UpperBits = base::BitField64<int, 61, 3>;
using MiddleBits = base::BitField64<int, 31, 2>;
TEST_F(ConversionsTest, BitField64) {
uint64_t x;
// Test most significant bits.
x = 0xE000'0000'0000'0000;
CHECK(x == UpperBits::encode(7));
CHECK_EQ(7, UpperBits::decode(x));
// Test the 32/64-bit boundary bits.
x = 0x0000'0001'8000'0000;
CHECK(x == MiddleBits::encode(3));
CHECK_EQ(3, MiddleBits::decode(x));
}
TEST_F(ConversionsTest, SpecialIndexParsing) {
HandleScope scope(i_isolate());
CheckNonArrayIndex(false, "");
CheckNonArrayIndex(false, "-");
CheckNonArrayIndex(true, "0");
CheckNonArrayIndex(true, "-0");
CheckNonArrayIndex(false, "01");
CheckNonArrayIndex(false, "-01");
CheckNonArrayIndex(true, "0.5");
CheckNonArrayIndex(true, "-0.5");
CheckNonArrayIndex(true, "1");
CheckNonArrayIndex(true, "-1");
CheckNonArrayIndex(true, "10");
CheckNonArrayIndex(true, "-10");
CheckNonArrayIndex(true, "NaN");
CheckNonArrayIndex(true, "Infinity");
CheckNonArrayIndex(true, "-Infinity");
CheckNonArrayIndex(true, "4294967295");
CheckNonArrayIndex(true, "429496.7295");
CheckNonArrayIndex(true, "1.3333333333333333");
CheckNonArrayIndex(false, "1.3333333333333339");
CheckNonArrayIndex(true, "1.333333333333331e+222");
CheckNonArrayIndex(true, "-1.3333333333333211e+222");
CheckNonArrayIndex(false, "-1.3333333333333311e+222");
CheckNonArrayIndex(true, "429496.7295");
CheckNonArrayIndex(false, "43s3");
CheckNonArrayIndex(true, "4294967296");
CheckNonArrayIndex(true, "-4294967296");
CheckNonArrayIndex(true, "999999999999999");
CheckNonArrayIndex(false, "9999999999999999");
CheckNonArrayIndex(true, "-999999999999999");
CheckNonArrayIndex(false, "-9999999999999999");
CheckNonArrayIndex(false, "42949672964294967296429496729694966");
}
TEST_F(ConversionsTest, NoHandlesForTryNumberToSize) {
size_t result = 0;
{
SealHandleScope no_handles(i_isolate());
Tagged<Smi> smi = Smi::FromInt(1);
CHECK(TryNumberToSize(smi, &result));
CHECK_EQ(result, 1u);
}
result = 0;
{
HandleScope scope(i_isolate());
DirectHandle<HeapNumber> heap_number1 =
i_isolate()->factory()->NewHeapNumber(2.0);
{
SealHandleScope no_handles(i_isolate());
CHECK(TryNumberToSize(*heap_number1, &result));
CHECK_EQ(result, 2u);
}
DirectHandle<HeapNumber> heap_number2 =
i_isolate()->factory()->NewHeapNumber(
static_cast<double>(std::numeric_limits<size_t>::max()) + 10000.0);
{
SealHandleScope no_handles(i_isolate());
CHECK(!TryNumberToSize(*heap_number2, &result));
}
}
}
TEST_F(ConversionsTest, TryNumberToSizeWithMaxSizePlusOne) {
{
HandleScope scope(i_isolate());
// 1 << 64, larger than the limit of size_t.
double value = 18446744073709551616.0;
size_t result = 0;
DirectHandle<HeapNumber> heap_number =
i_isolate()->factory()->NewHeapNumber(value);
CHECK(!TryNumberToSize(*heap_number, &result));
}
}
TEST_F(ConversionsTest, PositiveNumberToUint32) {
i::Factory* factory = i_isolate()->factory();
uint32_t max = std::numeric_limits<uint32_t>::max();
HandleScope scope(i_isolate());
// Test Smi conversions.
DirectHandle<Object> number(Smi::FromInt(0), i_isolate());
CHECK_EQ(PositiveNumberToUint32(*number), 0u);
number = direct_handle(Smi::FromInt(-1), i_isolate());
CHECK_EQ(PositiveNumberToUint32(*number), 0u);
number = direct_handle(Smi::FromInt(-1), i_isolate());
CHECK_EQ(PositiveNumberToUint32(*number), 0u);
number = direct_handle(Smi::FromInt(Smi::kMinValue), i_isolate());
CHECK_EQ(PositiveNumberToUint32(*number), 0u);
number = direct_handle(Smi::FromInt(Smi::kMaxValue), i_isolate());
CHECK_EQ(PositiveNumberToUint32(*number),
static_cast<uint32_t>(Smi::kMaxValue));
// Test Double conversions.
number = factory->NewHeapNumber(0.0);
CHECK_EQ(PositiveNumberToUint32(*number), 0u);
number = factory->NewHeapNumber(0.999);
CHECK_EQ(PositiveNumberToUint32(*number), 0u);
number = factory->NewHeapNumber(1.999);
CHECK_EQ(PositiveNumberToUint32(*number), 1u);
number = factory->NewHeapNumber(-12.0);
CHECK_EQ(PositiveNumberToUint32(*number), 0u);
number = factory->NewHeapNumber(12000.0);
CHECK_EQ(PositiveNumberToUint32(*number), 12000u);
number = factory->NewHeapNumber(static_cast<double>(Smi::kMaxValue) + 1);
CHECK_EQ(PositiveNumberToUint32(*number),
static_cast<uint32_t>(Smi::kMaxValue) + 1);
number = factory->NewHeapNumber(max);
CHECK_EQ(PositiveNumberToUint32(*number), max);
number = factory->NewHeapNumber(static_cast<double>(max) * 1000);
CHECK_EQ(PositiveNumberToUint32(*number), max);
number = factory->NewHeapNumber(std::numeric_limits<double>::max());
CHECK_EQ(PositiveNumberToUint32(*number), max);
number = factory->NewHeapNumber(std::numeric_limits<double>::infinity());
CHECK_EQ(PositiveNumberToUint32(*number), max);
number =
factory->NewHeapNumber(-1.0 * std::numeric_limits<double>::infinity());
CHECK_EQ(PositiveNumberToUint32(*number), 0u);
number = factory->NewHeapNumber(std::nan(""));
CHECK_EQ(PositiveNumberToUint32(*number), 0u);
}
// Some random offsets, mostly at 'suspicious' bit boundaries.
struct IntStringPair {
int integer;
std::string string;
};
static IntStringPair int_pairs[] = {{0, "0"},
{101, "101"},
{-1, "-1"},
{1024, "1024"},
{200000, "200000"},
{-1024, "-1024"},
{-200000, "-200000"},
{kMinInt, "-2147483648"},
{kMaxInt, "2147483647"}};
TEST_F(ConversionsTest, IntToStringView) {
std::unique_ptr<char[]> buf(new char[4096]);
for (size_t i = 0; i < arraysize(int_pairs); i++) {
ASSERT_EQ(
std::string(IntToStringView(int_pairs[i].integer, {buf.get(), 4096})),
int_pairs[i].string);
}
}
struct DoubleStringPair {
double number;
std::string string;
};
static DoubleStringPair double_pairs[] = {
{0.0, "0"},
{kMinInt, "-2147483648"},
{kMaxInt, "2147483647"},
// ES section 7.1.12.1 #sec-tostring-applied-to-the-number-type:
// -0.0 is stringified to "0".
{-0.0, "0"},
{1.1, "1.1"},
{0.1, "0.1"}};
TEST_F(ConversionsTest, DoubleToStringView) {
std::unique_ptr<char[]> buf(new char[4096]);
for (size_t i = 0; i < arraysize(double_pairs); i++) {
ASSERT_EQ(std::string(DoubleToStringView(double_pairs[i].number,
{buf.get(), 4096})),
double_pairs[i].string);
}
}
struct DoubleInt32Pair {
double number;
int integer;
};
static DoubleInt32Pair double_int32_pairs[] = {
{0.0, 0},
{-0.0, 0},
{std::numeric_limits<double>::quiet_NaN(), 0},
{std::numeric_limits<double>::infinity(), 0},
{-std::numeric_limits<double>::infinity(), 0},
{3.14, 3},
{1.99, 1},
{-1.99, -1},
{static_cast<double>(kMinInt), kMinInt},
{static_cast<double>(kMaxInt), kMaxInt},
{kMaxSafeInteger, -1},
{kMinSafeInteger, 1},
{kMaxSafeInteger + 1, 0},
{kMinSafeInteger - 1, 0},
};
TEST_F(ConversionsTest, DoubleToInt32) {
for (size_t i = 0; i < arraysize(double_int32_pairs); i++) {
ASSERT_EQ(DoubleToInt32(double_int32_pairs[i].number),
double_int32_pairs[i].integer);
}
}
struct DoubleInt64Pair {
double number;
int64_t integer;
};
static DoubleInt64Pair double_int64_pairs[] = {
{0.0, 0},
{-0.0, 0},
{std::numeric_limits<double>::quiet_NaN(), 0},
{std::numeric_limits<double>::infinity(), 0},
{-std::numeric_limits<double>::infinity(), 0},
{3.14, 3},
{1.99, 1},
{-1.99, -1},
{kMinSafeInteger, static_cast<int64_t>(kMinSafeInteger)},
{kMaxSafeInteger, static_cast<int64_t>(kMaxSafeIntegerUint64)},
{kMinSafeInteger - 1, static_cast<int64_t>(kMinSafeInteger) - 1},
{kMaxSafeInteger + 1, static_cast<int64_t>(kMaxSafeIntegerUint64) + 1},
{static_cast<double>(std::numeric_limits<int64_t>::min()),
std::numeric_limits<int64_t>::min()},
// Max int64_t is not representable as a double, the closest is -2^63.
{static_cast<double>(std::numeric_limits<int64_t>::max()),
std::numeric_limits<int64_t>::min()},
// So we test for a smaller number, representable as a double.
{static_cast<double>((1ull << 63) - 1024), (1ull << 63) - 1024}};
TEST_F(ConversionsTest, DoubleToWebIDLInt64) {
for (size_t i = 0; i < arraysize(double_int64_pairs); i++) {
ASSERT_EQ(DoubleToWebIDLInt64(double_int64_pairs[i].number),
double_int64_pairs[i].integer);
}
}
} // namespace interpreter
} // namespace internal
} // namespace v8

View File

@ -0,0 +1,96 @@
// Copyright 2006-2008 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "src/base/numbers/diy-fp.h"
#include <stdlib.h>
#include "src/base/platform/platform.h"
#include "src/init/v8.h"
#include "test/unittests/test-utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace v8 {
namespace base {
using DiyFpTest = ::testing::Test;
TEST_F(DiyFpTest, Subtract) {
DiyFp diy_fp1 = DiyFp(3, 0);
DiyFp diy_fp2 = DiyFp(1, 0);
DiyFp diff = DiyFp::Minus(diy_fp1, diy_fp2);
CHECK_EQ(2, diff.f());
CHECK_EQ(0, diff.e());
diy_fp1.Subtract(diy_fp2);
CHECK_EQ(2, diy_fp1.f());
CHECK_EQ(0, diy_fp1.e());
}
TEST_F(DiyFpTest, Multiply) {
DiyFp diy_fp1 = DiyFp(3, 0);
DiyFp diy_fp2 = DiyFp(2, 0);
DiyFp product = DiyFp::Times(diy_fp1, diy_fp2);
CHECK_EQ(0, product.f());
CHECK_EQ(64, product.e());
diy_fp1.Multiply(diy_fp2);
CHECK_EQ(0, diy_fp1.f());
CHECK_EQ(64, diy_fp1.e());
diy_fp1 = DiyFp(0x8000'0000'0000'0000, 11);
diy_fp2 = DiyFp(2, 13);
product = DiyFp::Times(diy_fp1, diy_fp2);
CHECK_EQ(1, product.f());
CHECK_EQ(11 + 13 + 64, product.e());
// Test rounding.
diy_fp1 = DiyFp(0x8000'0000'0000'0001, 11);
diy_fp2 = DiyFp(1, 13);
product = DiyFp::Times(diy_fp1, diy_fp2);
CHECK_EQ(1, product.f());
CHECK_EQ(11 + 13 + 64, product.e());
diy_fp1 = DiyFp(0x7FFF'FFFF'FFFF'FFFF, 11);
diy_fp2 = DiyFp(1, 13);
product = DiyFp::Times(diy_fp1, diy_fp2);
CHECK_EQ(0, product.f());
CHECK_EQ(11 + 13 + 64, product.e());
// Halfway cases are allowed to round either way. So don't check for it.
// Big numbers.
diy_fp1 = DiyFp(0xFFFF'FFFF'FFFF'FFFF, 11);
diy_fp2 = DiyFp(0xFFFF'FFFF'FFFF'FFFF, 13);
// 128bit result: 0xFFFFFFFFFFFFFFFE0000000000000001
product = DiyFp::Times(diy_fp1, diy_fp2);
CHECK_EQ(0xFFFF'FFFF'FFFF'FFFE, product.f());
CHECK_EQ(11 + 13 + 64, product.e());
}
} // namespace base
} // namespace v8

View File

@ -0,0 +1,477 @@
// Copyright 2006-2008 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "src/base/numbers/strtod.h"
#include <stdlib.h>
#include "src/base/numbers/bignum.h"
#include "src/base/numbers/diy-fp.h"
#include "src/base/numbers/double.h"
#include "src/base/utils/random-number-generator.h"
#include "src/init/v8.h"
#include "test/unittests/test-utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace v8 {
namespace base {
using StrtodTest = ::testing::Test;
static double StrtodChar(const char* str, int exponent) {
return Strtod(CStrVector(str), exponent);
}
TEST_F(StrtodTest, Strtod) {
Vector<const char> vector;
vector = CStrVector("0");
CHECK_EQ(0.0, Strtod(vector, 1));
CHECK_EQ(0.0, Strtod(vector, 2));
CHECK_EQ(0.0, Strtod(vector, -2));
CHECK_EQ(0.0, Strtod(vector, -999));
CHECK_EQ(0.0, Strtod(vector, +999));
vector = CStrVector("1");
CHECK_EQ(1.0, Strtod(vector, 0));
CHECK_EQ(10.0, Strtod(vector, 1));
CHECK_EQ(100.0, Strtod(vector, 2));
CHECK_EQ(1e20, Strtod(vector, 20));
CHECK_EQ(1e22, Strtod(vector, 22));
CHECK_EQ(1e23, Strtod(vector, 23));
CHECK_EQ(1e35, Strtod(vector, 35));
CHECK_EQ(1e36, Strtod(vector, 36));
CHECK_EQ(1e37, Strtod(vector, 37));
CHECK_EQ(1e-1, Strtod(vector, -1));
CHECK_EQ(1e-2, Strtod(vector, -2));
CHECK_EQ(1e-5, Strtod(vector, -5));
CHECK_EQ(1e-20, Strtod(vector, -20));
CHECK_EQ(1e-22, Strtod(vector, -22));
CHECK_EQ(1e-23, Strtod(vector, -23));
CHECK_EQ(1e-25, Strtod(vector, -25));
CHECK_EQ(1e-39, Strtod(vector, -39));
vector = CStrVector("2");
CHECK_EQ(2.0, Strtod(vector, 0));
CHECK_EQ(20.0, Strtod(vector, 1));
CHECK_EQ(200.0, Strtod(vector, 2));
CHECK_EQ(2e20, Strtod(vector, 20));
CHECK_EQ(2e22, Strtod(vector, 22));
CHECK_EQ(2e23, Strtod(vector, 23));
CHECK_EQ(2e35, Strtod(vector, 35));
CHECK_EQ(2e36, Strtod(vector, 36));
CHECK_EQ(2e37, Strtod(vector, 37));
CHECK_EQ(2e-1, Strtod(vector, -1));
CHECK_EQ(2e-2, Strtod(vector, -2));
CHECK_EQ(2e-5, Strtod(vector, -5));
CHECK_EQ(2e-20, Strtod(vector, -20));
CHECK_EQ(2e-22, Strtod(vector, -22));
CHECK_EQ(2e-23, Strtod(vector, -23));
CHECK_EQ(2e-25, Strtod(vector, -25));
CHECK_EQ(2e-39, Strtod(vector, -39));
vector = CStrVector("9");
CHECK_EQ(9.0, Strtod(vector, 0));
CHECK_EQ(90.0, Strtod(vector, 1));
CHECK_EQ(900.0, Strtod(vector, 2));
CHECK_EQ(9e20, Strtod(vector, 20));
CHECK_EQ(9e22, Strtod(vector, 22));
CHECK_EQ(9e23, Strtod(vector, 23));
CHECK_EQ(9e35, Strtod(vector, 35));
CHECK_EQ(9e36, Strtod(vector, 36));
CHECK_EQ(9e37, Strtod(vector, 37));
CHECK_EQ(9e-1, Strtod(vector, -1));
CHECK_EQ(9e-2, Strtod(vector, -2));
CHECK_EQ(9e-5, Strtod(vector, -5));
CHECK_EQ(9e-20, Strtod(vector, -20));
CHECK_EQ(9e-22, Strtod(vector, -22));
CHECK_EQ(9e-23, Strtod(vector, -23));
CHECK_EQ(9e-25, Strtod(vector, -25));
CHECK_EQ(9e-39, Strtod(vector, -39));
vector = CStrVector("12345");
CHECK_EQ(12345.0, Strtod(vector, 0));
CHECK_EQ(123450.0, Strtod(vector, 1));
CHECK_EQ(1234500.0, Strtod(vector, 2));
CHECK_EQ(12345e20, Strtod(vector, 20));
CHECK_EQ(12345e22, Strtod(vector, 22));
CHECK_EQ(12345e23, Strtod(vector, 23));
CHECK_EQ(12345e30, Strtod(vector, 30));
CHECK_EQ(12345e31, Strtod(vector, 31));
CHECK_EQ(12345e32, Strtod(vector, 32));
CHECK_EQ(12345e35, Strtod(vector, 35));
CHECK_EQ(12345e36, Strtod(vector, 36));
CHECK_EQ(12345e37, Strtod(vector, 37));
CHECK_EQ(12345e-1, Strtod(vector, -1));
CHECK_EQ(12345e-2, Strtod(vector, -2));
CHECK_EQ(12345e-5, Strtod(vector, -5));
CHECK_EQ(12345e-20, Strtod(vector, -20));
CHECK_EQ(12345e-22, Strtod(vector, -22));
CHECK_EQ(12345e-23, Strtod(vector, -23));
CHECK_EQ(12345e-25, Strtod(vector, -25));
CHECK_EQ(12345e-39, Strtod(vector, -39));
vector = CStrVector("12345678901234");
CHECK_EQ(12345678901234.0, Strtod(vector, 0));
CHECK_EQ(123456789012340.0, Strtod(vector, 1));
CHECK_EQ(1234567890123400.0, Strtod(vector, 2));
CHECK_EQ(12345678901234e20, Strtod(vector, 20));
CHECK_EQ(12345678901234e22, Strtod(vector, 22));
CHECK_EQ(12345678901234e23, Strtod(vector, 23));
CHECK_EQ(12345678901234e30, Strtod(vector, 30));
CHECK_EQ(12345678901234e31, Strtod(vector, 31));
CHECK_EQ(12345678901234e32, Strtod(vector, 32));
CHECK_EQ(12345678901234e35, Strtod(vector, 35));
CHECK_EQ(12345678901234e36, Strtod(vector, 36));
CHECK_EQ(12345678901234e37, Strtod(vector, 37));
CHECK_EQ(12345678901234e-1, Strtod(vector, -1));
CHECK_EQ(12345678901234e-2, Strtod(vector, -2));
CHECK_EQ(12345678901234e-5, Strtod(vector, -5));
CHECK_EQ(12345678901234e-20, Strtod(vector, -20));
CHECK_EQ(12345678901234e-22, Strtod(vector, -22));
CHECK_EQ(12345678901234e-23, Strtod(vector, -23));
CHECK_EQ(12345678901234e-25, Strtod(vector, -25));
CHECK_EQ(12345678901234e-39, Strtod(vector, -39));
vector = CStrVector("123456789012345");
CHECK_EQ(123456789012345.0, Strtod(vector, 0));
CHECK_EQ(1234567890123450.0, Strtod(vector, 1));
CHECK_EQ(12345678901234500.0, Strtod(vector, 2));
CHECK_EQ(123456789012345e20, Strtod(vector, 20));
CHECK_EQ(123456789012345e22, Strtod(vector, 22));
CHECK_EQ(123456789012345e23, Strtod(vector, 23));
CHECK_EQ(123456789012345e35, Strtod(vector, 35));
CHECK_EQ(123456789012345e36, Strtod(vector, 36));
CHECK_EQ(123456789012345e37, Strtod(vector, 37));
CHECK_EQ(123456789012345e39, Strtod(vector, 39));
CHECK_EQ(123456789012345e-1, Strtod(vector, -1));
CHECK_EQ(123456789012345e-2, Strtod(vector, -2));
CHECK_EQ(123456789012345e-5, Strtod(vector, -5));
CHECK_EQ(123456789012345e-20, Strtod(vector, -20));
CHECK_EQ(123456789012345e-22, Strtod(vector, -22));
CHECK_EQ(123456789012345e-23, Strtod(vector, -23));
CHECK_EQ(123456789012345e-25, Strtod(vector, -25));
CHECK_EQ(123456789012345e-39, Strtod(vector, -39));
CHECK_EQ(0.0, StrtodChar("0", 12345));
CHECK_EQ(0.0, StrtodChar("", 1324));
CHECK_EQ(0.0, StrtodChar("000000000", 123));
CHECK_EQ(0.0, StrtodChar("2", -324));
CHECK_EQ(4e-324, StrtodChar("3", -324));
// It would be more readable to put non-zero literals on the left side (i.e.
// CHECK_EQ(1e-325, StrtodChar("1", -325))), but then Gcc complains that
// they are truncated to zero.
CHECK_EQ(0.0, StrtodChar("1", -325));
CHECK_EQ(0.0, StrtodChar("1", -325));
CHECK_EQ(0.0, StrtodChar("20000", -328));
CHECK_EQ(40000e-328, StrtodChar("30000", -328));
CHECK_EQ(0.0, StrtodChar("10000", -329));
CHECK_EQ(0.0, StrtodChar("90000", -329));
CHECK_EQ(0.0, StrtodChar("000000001", -325));
CHECK_EQ(0.0, StrtodChar("000000001", -325));
CHECK_EQ(0.0, StrtodChar("0000000020000", -328));
CHECK_EQ(40000e-328, StrtodChar("00000030000", -328));
CHECK_EQ(0.0, StrtodChar("0000000010000", -329));
CHECK_EQ(0.0, StrtodChar("0000000090000", -329));
// It would be more readable to put the literals (and not V8_INFINITY) on the
// left side (i.e. CHECK_EQ(1e309, StrtodChar("1", 309))), but then Gcc
// complains that the floating constant exceeds range of 'double'.
CHECK_EQ(V8_INFINITY, StrtodChar("1", 309));
CHECK_EQ(1e308, StrtodChar("1", 308));
CHECK_EQ(1234e305, StrtodChar("1234", 305));
CHECK_EQ(1234e304, StrtodChar("1234", 304));
CHECK_EQ(V8_INFINITY, StrtodChar("18", 307));
CHECK_EQ(17e307, StrtodChar("17", 307));
CHECK_EQ(V8_INFINITY, StrtodChar("0000001", 309));
CHECK_EQ(1e308, StrtodChar("00000001", 308));
CHECK_EQ(1234e305, StrtodChar("00000001234", 305));
CHECK_EQ(1234e304, StrtodChar("000000001234", 304));
CHECK_EQ(V8_INFINITY, StrtodChar("0000000018", 307));
CHECK_EQ(17e307, StrtodChar("0000000017", 307));
CHECK_EQ(V8_INFINITY, StrtodChar("1000000", 303));
CHECK_EQ(1e308, StrtodChar("100000", 303));
CHECK_EQ(1234e305, StrtodChar("123400000", 300));
CHECK_EQ(1234e304, StrtodChar("123400000", 299));
CHECK_EQ(V8_INFINITY, StrtodChar("180000000", 300));
CHECK_EQ(17e307, StrtodChar("170000000", 300));
CHECK_EQ(V8_INFINITY, StrtodChar("00000001000000", 303));
CHECK_EQ(1e308, StrtodChar("000000000000100000", 303));
CHECK_EQ(1234e305, StrtodChar("00000000123400000", 300));
CHECK_EQ(1234e304, StrtodChar("0000000123400000", 299));
CHECK_EQ(V8_INFINITY, StrtodChar("00000000180000000", 300));
CHECK_EQ(17e307, StrtodChar("00000000170000000", 300));
CHECK_EQ(1.7976931348623157E+308, StrtodChar("17976931348623157", 292));
CHECK_EQ(1.7976931348623158E+308, StrtodChar("17976931348623158", 292));
CHECK_EQ(V8_INFINITY, StrtodChar("17976931348623159", 292));
// The following number is the result of 89255.0/1e22. Both floating-point
// numbers can be accurately represented with doubles. However on Linux,x86
// the floating-point stack is set to 80bits and the double-rounding
// introduces an error.
CHECK_EQ(89255e-22, StrtodChar("89255", -22));
// Some random values.
CHECK_EQ(358416272e-33, StrtodChar("358416272", -33));
CHECK_EQ(104110013277974872254e-225,
StrtodChar("104110013277974872254", -225));
CHECK_EQ(123456789e108, StrtodChar("123456789", 108));
CHECK_EQ(123456789e109, StrtodChar("123456789", 109));
CHECK_EQ(123456789e110, StrtodChar("123456789", 110));
CHECK_EQ(123456789e111, StrtodChar("123456789", 111));
CHECK_EQ(123456789e112, StrtodChar("123456789", 112));
CHECK_EQ(123456789e113, StrtodChar("123456789", 113));
CHECK_EQ(123456789e114, StrtodChar("123456789", 114));
CHECK_EQ(123456789e115, StrtodChar("123456789", 115));
CHECK_EQ(1234567890123456789012345e108,
StrtodChar("1234567890123456789012345", 108));
CHECK_EQ(1234567890123456789012345e109,
StrtodChar("1234567890123456789012345", 109));
CHECK_EQ(1234567890123456789012345e110,
StrtodChar("1234567890123456789012345", 110));
CHECK_EQ(1234567890123456789012345e111,
StrtodChar("1234567890123456789012345", 111));
CHECK_EQ(1234567890123456789012345e112,
StrtodChar("1234567890123456789012345", 112));
CHECK_EQ(1234567890123456789012345e113,
StrtodChar("1234567890123456789012345", 113));
CHECK_EQ(1234567890123456789012345e114,
StrtodChar("1234567890123456789012345", 114));
CHECK_EQ(1234567890123456789012345e115,
StrtodChar("1234567890123456789012345", 115));
CHECK_EQ(1234567890123456789052345e108,
StrtodChar("1234567890123456789052345", 108));
CHECK_EQ(1234567890123456789052345e109,
StrtodChar("1234567890123456789052345", 109));
CHECK_EQ(1234567890123456789052345e110,
StrtodChar("1234567890123456789052345", 110));
CHECK_EQ(1234567890123456789052345e111,
StrtodChar("1234567890123456789052345", 111));
CHECK_EQ(1234567890123456789052345e112,
StrtodChar("1234567890123456789052345", 112));
CHECK_EQ(1234567890123456789052345e113,
StrtodChar("1234567890123456789052345", 113));
CHECK_EQ(1234567890123456789052345e114,
StrtodChar("1234567890123456789052345", 114));
CHECK_EQ(1234567890123456789052345e115,
StrtodChar("1234567890123456789052345", 115));
CHECK_EQ(5.445618932859895e-255,
StrtodChar("5445618932859895362967233318697132813618813095743952975"
"4392982234069699615600475529427176366709107287468930197"
"8628345413991790019316974825934906752493984055268219809"
"5012176093045431437495773903922425632551857520884625114"
"6241265881735209066709685420744388526014389929047617597"
"0302268848374508109029268898695825171158085457567481507"
"4162979705098246243690189880319928315307816832576838178"
"2563074014542859888710209237525873301724479666744537857"
"9026553346649664045621387124193095870305991178772256504"
"4368663670643970181259143319016472430928902201239474588"
"1392338901353291306607057623202353588698746085415097902"
"6640064319118728664842287477491068264828851624402189317"
"2769161449825765517353755844373640588822904791244190695"
"2998382932630754670573838138825217065450843010498555058"
"88186560731",
-1035));
// Boundary cases. Boundaries themselves should round to even.
//
// 0x1FFFFFFFFFFFF * 2^3 = 72057594037927928
// next: 72057594037927936
// boundary: 72057594037927932 should round up.
CHECK_EQ(72057594037927928.0, StrtodChar("72057594037927928", 0));
CHECK_EQ(72057594037927936.0, StrtodChar("72057594037927936", 0));
CHECK_EQ(72057594037927936.0, StrtodChar("72057594037927932", 0));
CHECK_EQ(72057594037927928.0, StrtodChar("7205759403792793199999", -5));
CHECK_EQ(72057594037927936.0, StrtodChar("7205759403792793200001", -5));
// 0x1FFFFFFFFFFFF * 2^10 = 9223372036854774784
// next: 9223372036854775808
// boundary: 9223372036854775296 should round up.
CHECK_EQ(9223372036854774784.0, StrtodChar("9223372036854774784", 0));
CHECK_EQ(9223372036854775808.0, StrtodChar("9223372036854775808", 0));
CHECK_EQ(9223372036854775808.0, StrtodChar("9223372036854775296", 0));
CHECK_EQ(9223372036854774784.0, StrtodChar("922337203685477529599999", -5));
CHECK_EQ(9223372036854775808.0, StrtodChar("922337203685477529600001", -5));
// 0x1FFFFFFFFFFFF * 2^50 = 10141204801825834086073718800384
// next: 10141204801825835211973625643008
// boundary: 10141204801825834649023672221696 should round up.
CHECK_EQ(10141204801825834086073718800384.0,
StrtodChar("10141204801825834086073718800384", 0));
CHECK_EQ(10141204801825835211973625643008.0,
StrtodChar("10141204801825835211973625643008", 0));
CHECK_EQ(10141204801825835211973625643008.0,
StrtodChar("10141204801825834649023672221696", 0));
CHECK_EQ(10141204801825834086073718800384.0,
StrtodChar("1014120480182583464902367222169599999", -5));
CHECK_EQ(10141204801825835211973625643008.0,
StrtodChar("1014120480182583464902367222169600001", -5));
// 0x1FFFFFFFFFFFF * 2^99 = 5708990770823838890407843763683279797179383808
// next: 5708990770823839524233143877797980545530986496
// boundary: 5708990770823839207320493820740630171355185152
// The boundary should round up.
CHECK_EQ(5708990770823838890407843763683279797179383808.0,
StrtodChar("5708990770823838890407843763683279797179383808", 0));
CHECK_EQ(5708990770823839524233143877797980545530986496.0,
StrtodChar("5708990770823839524233143877797980545530986496", 0));
CHECK_EQ(5708990770823839524233143877797980545530986496.0,
StrtodChar("5708990770823839207320493820740630171355185152", 0));
CHECK_EQ(5708990770823838890407843763683279797179383808.0,
StrtodChar("5708990770823839207320493820740630171355185151999", -3));
CHECK_EQ(5708990770823839524233143877797980545530986496.0,
StrtodChar("5708990770823839207320493820740630171355185152001", -3));
// The following test-cases got some public attention in early 2011 when they
// sent Java and PHP into an infinite loop.
CHECK_EQ(2.225073858507201e-308, StrtodChar("22250738585072011", -324));
CHECK_EQ(2.22507385850720138309e-308,
StrtodChar("22250738585072011360574097967091319759348195463516456480"
"23426109724822222021076945516529523908135087914149158913"
"03962110687008643869459464552765720740782062174337998814"
"10632673292535522868813721490129811224514518898490572223"
"07285255133155755015914397476397983411801999323962548289"
"01710708185069063066665599493827577257201576306269066333"
"26475653000092458883164330377797918696120494973903778297"
"04905051080609940730262937128958950003583799967207254304"
"36028407889577179615094551674824347103070260914462157228"
"98802581825451803257070188608721131280795122334262883686"
"22321503775666622503982534335974568884423900265498198385"
"48794829220689472168983109969836584681402285424333066033"
"98508864458040010349339704275671864433837704860378616227"
"71738545623065874679014086723327636718751",
-1076));
}
static int CompareBignumToDiyFp(const Bignum& bignum_digits,
int bignum_exponent, DiyFp diy_fp) {
Bignum bignum;
bignum.AssignBignum(bignum_digits);
Bignum other;
other.AssignUInt64(diy_fp.f());
if (bignum_exponent >= 0) {
bignum.MultiplyByPowerOfTen(bignum_exponent);
} else {
other.MultiplyByPowerOfTen(-bignum_exponent);
}
if (diy_fp.e() >= 0) {
other.ShiftLeft(diy_fp.e());
} else {
bignum.ShiftLeft(-diy_fp.e());
}
return Bignum::Compare(bignum, other);
}
static bool CheckDouble(Vector<const char> buffer, int exponent,
double to_check) {
DiyFp lower_boundary;
DiyFp upper_boundary;
Bignum input_digits;
input_digits.AssignDecimalString(buffer);
if (to_check == 0.0) {
const double kMinDouble = 4e-324;
// Check that the buffer*10^exponent < (0 + kMinDouble)/2.
Double d(kMinDouble);
d.NormalizedBoundaries(&lower_boundary, &upper_boundary);
return CompareBignumToDiyFp(input_digits, exponent, lower_boundary) <= 0;
}
if (to_check == V8_INFINITY) {
const double kMaxDouble = 1.7976931348623157e308;
// Check that the buffer*10^exponent >= boundary between kMaxDouble and inf.
Double d(kMaxDouble);
d.NormalizedBoundaries(&lower_boundary, &upper_boundary);
return CompareBignumToDiyFp(input_digits, exponent, upper_boundary) >= 0;
}
Double d(to_check);
d.NormalizedBoundaries(&lower_boundary, &upper_boundary);
if ((d.Significand() & 1) == 0) {
return CompareBignumToDiyFp(input_digits, exponent, lower_boundary) >= 0 &&
CompareBignumToDiyFp(input_digits, exponent, upper_boundary) <= 0;
} else {
return CompareBignumToDiyFp(input_digits, exponent, lower_boundary) > 0 &&
CompareBignumToDiyFp(input_digits, exponent, upper_boundary) < 0;
}
}
// Copied from v8.cc and adapted to make the function deterministic.
static uint32_t DeterministicRandom() {
// Random number generator using George Marsaglia's MWC algorithm.
static uint32_t hi = 0;
static uint32_t lo = 0;
// Initialization values don't have any special meaning. (They are the result
// of two calls to rand().)
if (hi == 0) hi = 0xBFE166E7;
if (lo == 0) lo = 0x64D1C3C9;
// Mix the bits.
hi = 36969 * (hi & 0xFFFF) + (hi >> 16);
lo = 18273 * (lo & 0xFFFF) + (lo >> 16);
return (hi << 16) + (lo & 0xFFFF);
}
static const int kBufferSize = 1024;
static const int kShortStrtodRandomCount = 2;
static const int kLargeStrtodRandomCount = 2;
TEST_F(StrtodTest, RandomStrtod) {
base::RandomNumberGenerator rng;
char buffer[kBufferSize];
for (int length = 1; length < 15; length++) {
for (int i = 0; i < kShortStrtodRandomCount; ++i) {
int pos = 0;
for (int j = 0; j < length; ++j) {
buffer[pos++] = rng.NextInt(10) + '0';
}
int exponent = DeterministicRandom() % (25 * 2 + 1) - 25 - length;
buffer[pos] = '\0';
Vector<const char> vector(buffer, pos);
double strtod_result = Strtod(vector, exponent);
CHECK(CheckDouble(vector, exponent, strtod_result));
}
}
for (int length = 15; length < 800; length += 2) {
for (int i = 0; i < kLargeStrtodRandomCount; ++i) {
int pos = 0;
for (int j = 0; j < length; ++j) {
buffer[pos++] = rng.NextInt(10) + '0';
}
int exponent = DeterministicRandom() % (308 * 2 + 1) - 308 - length;
buffer[pos] = '\0';
Vector<const char> vector(buffer, pos);
double strtod_result = Strtod(vector, exponent);
CHECK(CheckDouble(vector, exponent, strtod_result));
}
}
}
} // namespace base
} // namespace v8