36#include "iceberg/iceberg_export.h"
37#include "iceberg/result.h"
48 static constexpr int32_t kBitWidth = 128;
49 static constexpr int32_t kByteWidth = kBitWidth / 8;
50 static constexpr int32_t kMaxPrecision = 38;
51 static constexpr int32_t kMaxScale = 38;
57 constexpr
Decimal(int128_t value) noexcept
62 requires(std::is_integral_v<T> && (
sizeof(T) <=
sizeof(uint64_t)))
64 : data_(
static_cast<int128_t
>(value)) {}
69 explicit Decimal(std::string_view str);
72 constexpr Decimal(int64_t high, uint64_t low)
noexcept {
73 data_ = (
static_cast<int128_t
>(high) << 64) | low;
104 Result<std::pair<Decimal, Decimal>> Divide(
const Decimal& divisor)
const;
116 Decimal& operator<<=(uint32_t shift);
126 Decimal& operator>>=(uint32_t shift);
136 constexpr int128_t
value()
const {
return data_; }
139 constexpr int64_t
high()
const {
return static_cast<int64_t
>(data_ >> 64); }
142 constexpr uint64_t
low()
const {
return static_cast<uint64_t
>(data_); }
147 Result<std::string> ToString(int32_t scale)
const;
150 std::string ToIntegerString()
const;
153 std::string
ToString()
const override {
return ToIntegerString(); }
161 static Result<Decimal> FromString(std::string_view str, int32_t* precision =
nullptr,
162 int32_t* scale =
nullptr);
167 static Result<Decimal>
FromBigEndian(
const uint8_t* data, int32_t length);
175 Result<Decimal> Rescale(int32_t orig_scale, int32_t new_scale)
const;
180 bool FitsInPrecision(int32_t precision)
const;
184 if (high() != other.high()) {
185 return high() <=> other.high();
187 return low() <=> other.low();
191 static std::partial_ordering Compare(
const Decimal& lhs,
const Decimal& rhs,
192 int32_t lhs_scale, int32_t rhs_scale);
194 const uint8_t* native_endian_bytes()
const {
195 return reinterpret_cast<const uint8_t*
>(&data_);
199 std::array<uint8_t, kByteWidth> ToBytes()
const;
202 int64_t
Sign()
const {
return 1 | (high() >> 63); }
207 explicit operator bool()
const {
return data_ != 0; }
209 friend bool operator==(
const Decimal& lhs,
const Decimal& rhs) {
210 return lhs.data_ == rhs.data_;
213 friend bool operator!=(
const Decimal& lhs,
const Decimal& rhs) {
214 return lhs.data_ != rhs.data_;
217 ICEBERG_EXPORT
friend Decimal operator-(
const Decimal& operand);
218 ICEBERG_EXPORT
friend Decimal operator~(
const Decimal& operand);
220 ICEBERG_EXPORT
friend Decimal operator+(
const Decimal& lhs,
const Decimal& rhs);
221 ICEBERG_EXPORT
friend Decimal operator-(
const Decimal& lhs,
const Decimal& rhs);
222 ICEBERG_EXPORT
friend Decimal operator*(
const Decimal& lhs,
const Decimal& rhs);
223 ICEBERG_EXPORT
friend Decimal operator/(
const Decimal& lhs,
const Decimal& rhs);
224 ICEBERG_EXPORT
friend Decimal operator%(
const Decimal& lhs,
const Decimal& rhs);
230ICEBERG_EXPORT std::ostream& operator<<(std::ostream& os,
const Decimal&
decimal);
Represents 128-bit fixed-point decimal numbers. The max decimal precision that can be safely represen...
Definition decimal.h:46
Decimal operator>>(uint32_t shift) const
Shift right by the given number of bits.
Definition decimal.h:129
std::strong_ordering operator<=>(const Decimal &other) const
Spaceship operator for three-way comparison.
Definition decimal.h:183
constexpr uint64_t low() const
Get the low bits of the two's complement representation of the number.
Definition decimal.h:142
bool IsNegative() const
Check if the Decimal value is negative.
Definition decimal.h:205
constexpr Decimal(T value) noexcept
Create a Decimal from any integer not wider than 64 bits.
Definition decimal.h:63
constexpr int128_t value() const
Get the underlying 128-bit integer representation of the number.
Definition decimal.h:136
constexpr int64_t high() const
Get the high bits of the two's complement representation of the number.
Definition decimal.h:139
std::string ToString() const override
Returns an integer string representation of the decimal value.
Definition decimal.h:153
constexpr Decimal(int64_t high, uint64_t low) noexcept
Create a Decimal from two 64-bit integers.
Definition decimal.h:72
Decimal operator<<(uint32_t shift) const
Shift left by the given number of bits.
Definition decimal.h:119
constexpr Decimal() noexcept=default
Default constructor initializes to zero.
int64_t Sign() const
Returns 1 if positive or zero, -1 if strictly negative.
Definition decimal.h:202
constexpr T ToBigEndian(T value)
Convert a value to big-endian format.
Definition endian.h:80
constexpr T FromBigEndian(T value)
Convert a value from big-endian format.
Definition endian.h:90
std::shared_ptr< DecimalType > decimal(int32_t precision, int32_t scale)
Create a DecimalType with the given precision and scale.
Definition type.cc:380