Skip to content

Commit

Permalink
fix(double-conversion): Upgrade bundled double-conversion #3313
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-f committed Jun 16, 2021
1 parent 034ed33 commit 558324f
Show file tree
Hide file tree
Showing 11 changed files with 522 additions and 178 deletions.
2 changes: 1 addition & 1 deletion Foundation/src/bignum-dtoa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
// (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 <cmath>

#include "bignum-dtoa.h"

Expand Down
11 changes: 6 additions & 5 deletions Foundation/src/bignum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
namespace double_conversion {

Bignum::Bignum()
: bigits_(bigits_buffer_, kBigitCapacity), used_digits_(0), exponent_(0) {
: bigits_buffer_(), bigits_(bigits_buffer_, kBigitCapacity), used_digits_(0), exponent_(0) {
for (int i = 0; i < kBigitCapacity; ++i) {
bigits_[i] = 0;
}
Expand Down Expand Up @@ -104,7 +104,7 @@ void Bignum::AssignDecimalString(Vector<const char> value) {
const int kMaxUint64DecimalDigits = 19;
Zero();
int length = value.length();
int pos = 0;
unsigned int pos = 0;
// Let's just say that each digit needs 4 bits.
while (length >= kMaxUint64DecimalDigits) {
uint64_t digits = ReadUInt64(value, pos, kMaxUint64DecimalDigits);
Expand Down Expand Up @@ -445,26 +445,27 @@ void Bignum::AssignPowerUInt16(uint16_t base, int power_exponent) {
mask >>= 2;
uint64_t this_value = base;

bool delayed_multipliciation = false;
bool delayed_multiplication = 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) {
ASSERT(bit_size > 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;
delayed_multiplication = true;
}
}
mask >>= 1;
}
AssignUInt64(this_value);
if (delayed_multipliciation) {
if (delayed_multiplication) {
MultiplyByUInt32(base);
}

Expand Down
3 changes: 1 addition & 2 deletions Foundation/src/bignum.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class Bignum {

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.
Expand Down Expand Up @@ -137,7 +136,7 @@ class Bignum {
// The Bignum's value equals value(bigits_) * 2^(exponent_ * kBigitSize).
int exponent_;

DISALLOW_COPY_AND_ASSIGN(Bignum);
DC_DISALLOW_COPY_AND_ASSIGN(Bignum);
};

} // namespace double_conversion
Expand Down
9 changes: 4 additions & 5 deletions Foundation/src/cached-powers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <stdarg.h>
#include <limits.h>
#include <math.h>
#include <climits>
#include <cmath>
#include <cstdarg>

#include "utils.h"

Expand Down Expand Up @@ -131,7 +131,6 @@ static const CachedPower kCachedPowers[] = {
{UINT64_2PART_C(0xaf87023b, 9bf0ee6b), 1066, 340},
};

static const int kCachedPowersLength = ARRAY_SIZE(kCachedPowers);
static const int kCachedPowersOffset = 348; // -1 * the first decimal_exponent.
static const double kD_1_LOG2_10 = 0.30102999566398114; // 1 / lg(10)
// Difference between the decimal exponents in the table above.
Expand All @@ -149,7 +148,7 @@ void PowersOfTenCache::GetCachedPowerForBinaryExponentRange(
int foo = kCachedPowersOffset;
int index =
(foo + static_cast<int>(k) - 1) / kDecimalExponentDistance + 1;
ASSERT(0 <= index && index < kCachedPowersLength);
ASSERT(0 <= index && index < static_cast<int>(ARRAY_SIZE(kCachedPowers)));
CachedPower cached_power = kCachedPowers[index];
ASSERT(min_exponent <= cached_power.binary_exponent);
(void) max_exponent; // Mark variable as used.
Expand Down
22 changes: 11 additions & 11 deletions Foundation/src/diy-fp.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class DiyFp {
static const int kSignificandSize = 64;

DiyFp() : f_(0), e_(0) {}
DiyFp(uint64_t f, int e) : f_(f), e_(e) {}
DiyFp(uint64_t significand, int exponent) : f_(significand), e_(exponent) {}

// this = this - other.
// The exponents of both numbers must be the same and the significand of this
Expand Down Expand Up @@ -76,22 +76,22 @@ class DiyFp {

void Normalize() {
ASSERT(f_ != 0);
uint64_t f = f_;
int e = e_;
uint64_t significand = f_;
int exponent = e_;

// This method is mainly called for normalizing boundaries. In general
// boundaries need to be shifted by 10 bits. We thus optimize for this case.
const uint64_t k10MSBits = UINT64_2PART_C(0xFFC00000, 00000000);
while ((f & k10MSBits) == 0) {
f <<= 10;
e -= 10;
while ((significand & k10MSBits) == 0) {
significand <<= 10;
exponent -= 10;
}
while ((f & kUint64MSB) == 0) {
f <<= 1;
e--;
while ((significand & kUint64MSB) == 0) {
significand <<= 1;
exponent--;
}
f_ = f;
e_ = e;
f_ = significand;
e_ = exponent;
}

static DiyFp Normalize(const DiyFp& a) {
Expand Down
Loading

0 comments on commit 558324f

Please sign in to comment.