diff options
author | Drew Paroski <drew.paroski@mongodb.com> | 2021-04-29 00:01:01 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-04-29 00:44:00 +0000 |
commit | cdfe92c4bcf1a4ff9f43cab56c587bca74ca2ffc (patch) | |
tree | e2ceb29b626d4b564f6aab6c81fb17987a49c9f6 /src/mongo/platform/decimal128.cpp | |
parent | fbc4a0423c4930139b719ed5fee985af52b92e90 (diff) | |
download | mongo-cdfe92c4bcf1a4ff9f43cab56c587bca74ca2ffc.tar.gz |
SERVER-56326 Add a round() method to the Decimal128 class
Diffstat (limited to 'src/mongo/platform/decimal128.cpp')
-rw-r--r-- | src/mongo/platform/decimal128.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/mongo/platform/decimal128.cpp b/src/mongo/platform/decimal128.cpp index 65eaf7eaef4..0d12479f70a 100644 --- a/src/mongo/platform/decimal128.cpp +++ b/src/mongo/platform/decimal128.cpp @@ -420,6 +420,32 @@ Decimal128 Decimal128::cos(std::uint32_t* signalingFlags, RoundingMode roundMode return Decimal128{libraryTypeToValue(current)}; } +Decimal128 Decimal128::round(RoundingMode roundMode) const { + std::uint32_t throwAwayFlag = 0; + return round(&throwAwayFlag, roundMode); +} + +Decimal128 Decimal128::round(std::uint32_t* signalingFlags, RoundingMode roundMode) const { + BID_UINT128 current = decimal128ToLibraryType(_value); + current = [&]() { + switch (roundMode) { + case kRoundTiesToEven: + return bid128_round_integral_nearest_even(current, signalingFlags); + case kRoundTowardNegative: + return bid128_round_integral_negative(current, signalingFlags); + case kRoundTowardPositive: + return bid128_round_integral_positive(current, signalingFlags); + case kRoundTowardZero: + return bid128_round_integral_zero(current, signalingFlags); + case kRoundTiesToAway: + return bid128_round_integral_nearest_away(current, signalingFlags); + } + MONGO_UNREACHABLE; + }(); + + return Decimal128{libraryTypeToValue(current)}; +} + std::int32_t Decimal128::toInt(RoundingMode roundMode) const { std::uint32_t throwAwayFlag = 0; return toInt(&throwAwayFlag, roundMode); |