Skip to content

Commit 1a52eae

Browse files
committed
chore: use int128_t calculation
1 parent 33f1124 commit 1a52eae

File tree

2 files changed

+9
-30
lines changed

2 files changed

+9
-30
lines changed

src/iceberg/util/decimal.cc

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,7 @@ Decimal::Decimal(std::string_view str) {
356356
}
357357

358358
Decimal& Decimal::Negate() {
359-
uint64_t result_low = ~low() + 1;
360-
int64_t result_high = ~high();
361-
if (result_low == 0) {
362-
result_high = SafeSignedAdd<int64_t>(result_high, 1);
363-
}
364-
*this = Decimal(result_high, result_low);
359+
data_ = ~data_ + 1;
365360
return *this;
366361
}
367362

@@ -373,35 +368,17 @@ Decimal Decimal::Abs(const Decimal& value) {
373368
}
374369

375370
Decimal& Decimal::operator+=(const Decimal& other) {
376-
int64_t result_high = SafeSignedAdd(high(), other.high());
377-
uint64_t result_low = low() + other.low();
378-
result_high = SafeSignedAdd<int64_t>(result_high, result_low < low());
379-
*this = Decimal(result_high, result_low);
371+
data_ += other.data_;
380372
return *this;
381373
}
382374

383375
Decimal& Decimal::operator-=(const Decimal& other) {
384-
int64_t result_high = SafeSignedSubtract(high(), other.high());
385-
uint64_t result_low = low() - other.low();
386-
result_high = SafeSignedSubtract<int64_t>(result_high, result_low > low());
387-
*this = Decimal(result_high, result_low);
376+
data_ -= other.data_;
388377
return *this;
389378
}
390379

391380
Decimal& Decimal::operator*=(const Decimal& other) {
392-
// Since the max value of Decimal is supposed to be 1e38 - 1 and the min the
393-
// negation taking the abosolute values here should aways be safe.
394-
const bool negate = Sign() != other.Sign();
395-
Decimal x = Decimal::Abs(*this);
396-
Decimal y = Decimal::Abs(other);
397-
398-
Uint128 r(x);
399-
r *= Uint128(y);
400-
401-
*this = Decimal(static_cast<int64_t>(r.high()), r.low());
402-
if (negate) {
403-
Negate();
404-
}
381+
data_ *= other.data_;
405382
return *this;
406383
}
407384

@@ -412,9 +389,7 @@ Result<std::pair<Decimal, Decimal>> Decimal::Divide(const Decimal& divisor) cons
412389
}
413390

414391
Decimal& Decimal::operator/=(const Decimal& other) {
415-
Decimal remainder;
416-
auto s = DecimalDivide(*this, other, this, &remainder);
417-
assert(s);
392+
data_ /= other.data_;
418393
return *this;
419394
}
420395

src/iceberg/util/decimal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ class ICEBERG_EXPORT Decimal {
5454
/// \brief Default constructor initializes to zero.
5555
constexpr Decimal() noexcept = default;
5656

57+
/// \brief Create a Decimal from a 128-bit integer.
58+
constexpr Decimal(int128_t value) noexcept // NOLINT
59+
: data_(value) {}
60+
5761
/// \brief Create a Decimal from any integer not wider than 64 bits.
5862
template <typename T>
5963
requires(std::is_integral_v<T> && (sizeof(T) <= sizeof(uint64_t)))

0 commit comments

Comments
 (0)