20 #include <ripple/basics/Log.h>
21 #include <ripple/basics/contract.h>
22 #include <ripple/basics/safe_cast.h>
23 #include <ripple/beast/core/LexicalCast.h>
24 #include <ripple/protocol/STAmount.h>
25 #include <ripple/protocol/SystemParameters.h>
26 #include <ripple/protocol/UintTypes.h>
27 #include <ripple/protocol/jss.h>
28 #include <boost/algorithm/string.hpp>
29 #include <boost/multiprecision/cpp_int.hpp>
30 #include <boost/regex.hpp>
41 getStaticSTAmountCanonicalizeSwitchover()
43 static LocalValue<bool> r{
true};
51 return *getStaticSTAmountCanonicalizeSwitchover();
57 *getStaticSTAmountCanonicalizeSwitchover() = v;
69 Throw<std::runtime_error>(
"amount is not native!");
107 Throw<std::runtime_error>(
"negative zero is not canonical");
120 Throw<std::runtime_error>(
"invalid native currency");
125 Throw<std::runtime_error>(
"invalid native account");
128 int offset =
static_cast<int>(
value >> (64 - 10));
130 value &= ~(1023ull << (64 - 10));
134 bool isNegative = (offset & 256) == 0;
135 offset = (offset & 255) - 97;
140 Throw<std::runtime_error>(
"invalid currency value");
152 Throw<std::runtime_error>(
"invalid currency value");
174 , mIsNegative(negative)
189 , mIsNegative(negative)
205 , mIsNegative(negative)
211 :
STBase(name), mOffset(0), mIsNative(true)
221 , mIsNegative(negative)
236 , mIsNegative(negative)
248 , mIsNegative(mantissa != 0 && negative)
258 : mIssue(issue), mValue(mantissa), mOffset(exponent), mIsNegative(negative)
264 : mIssue(issue), mOffset(exponent)
287 , mOffset(amount.exponent())
289 , mIsNegative(amount <
beast::zero)
300 : mOffset(0), mIsNative(true), mIsNegative(amount <
beast::zero)
303 mValue = unsafe_cast<std::uint64_t>(-amount.
drops());
305 mValue = unsafe_cast<std::uint64_t>(amount.
drops());
313 return std::make_unique<STAmount>(sit, name);
325 return emplace(n, buf, std::move(*
this));
337 Throw<std::logic_error>(
338 "Cannot return non-native STAmount as XRPAmount");
352 Throw<std::logic_error>(
"Cannot return native STAmount as IOUAmount");
400 Throw<std::runtime_error>(
"Can't add amounts that are't comparable!");
402 if (v2 == beast::zero)
405 if (v1 == beast::zero)
453 if ((fv >= -10) && (fv <= 10))
497 if (offerOut == beast::zero)
502 if (r == beast::zero)
506 return (ret << (64 - 8)) | r.
mantissa();
561 if (*
this == beast::zero)
570 bool const scientific(
588 size_t const pad_prefix = 27;
589 size_t const pad_suffix = 23;
593 val.
append(pad_prefix,
'0');
595 val.
append(pad_suffix,
'0');
597 size_t const offset(
mOffset + 43);
599 auto pre_from(val.
begin());
600 auto const pre_to(val.
begin() + offset);
602 auto const post_from(val.
begin() + offset);
603 auto post_to(val.
end());
608 pre_from += pad_prefix;
610 assert(post_to >= post_from);
612 pre_from =
std::find_if(pre_from, pre_to, [](
char c) {
return c !=
'0'; });
617 post_to -= pad_suffix;
619 assert(post_to >= post_from);
624 [](
char c) {
return c !=
'0'; })
628 if (pre_from == pre_to)
631 ret.
append(pre_from, pre_to);
633 if (post_to != post_from)
636 ret.
append(post_from, post_to);
663 if (*
this == beast::zero)
684 return v && (*v == *
this);
732 Throw<std::runtime_error>(
733 "Native currency amount out of range");
760 Throw<std::runtime_error>(
761 "Native currency amount out of range");
769 Throw<std::runtime_error>(
"Native currency amount out of range");
798 Throw<std::runtime_error>(
"value overflow");
813 Throw<std::runtime_error>(
"value overflow");
845 int exponent =
static_cast<int>(rate >> (64 - 8)) - 100;
853 static boost::regex
const reNumber(
858 "([eE]([+-]?)([0-9]+))?"
860 boost::regex_constants::optimize);
864 if (!boost::regex_match(amount, match, reNumber))
865 Throw<std::runtime_error>(
"Number '" + amount +
"' is not valid");
878 if ((match[2].length() + match[4].length()) > 32)
879 Throw<std::runtime_error>(
"Number '" + amount +
"' is overlong");
881 bool negative = (match[1].matched && (match[1] ==
"-"));
884 if (
isXRP(issue) && match[3].matched)
885 Throw<std::runtime_error>(
"XRP must be specified in integral drops.");
890 if (!match[4].matched)
893 beast::lexicalCastThrow<std::uint64_t>(
std::string(match[2]));
899 mantissa = beast::lexicalCastThrow<std::uint64_t>(match[2] + match[4]);
900 exponent = -(match[4].length());
903 if (match[5].matched)
906 if (match[6].matched && (match[6] ==
"-"))
907 exponent -= beast::lexicalCastThrow<int>(
std::string(match[7]));
909 exponent += beast::lexicalCastThrow<int>(
std::string(match[7]));
912 return {issue, mantissa, exponent, negative};
920 bool negative =
false;
929 Throw<std::runtime_error>(
930 "XRP may not be specified with a null Json value");
934 value = v[jss::value];
935 currency = v[jss::currency];
936 issuer = v[jss::issuer];
948 boost::split(elements, val, boost::is_any_of(
"\t\n\r ,/"));
950 if (elements.
size() > 3)
951 Throw<std::runtime_error>(
"invalid amount string");
955 if (elements.
size() > 1)
956 currency = elements[1];
958 if (elements.
size() > 2)
959 issuer = elements[2];
972 Throw<std::runtime_error>(
"XRP may not be specified as an object");
979 Throw<std::runtime_error>(
"invalid currency");
982 Throw<std::runtime_error>(
"invalid issuer");
985 Throw<std::runtime_error>(
"invalid issuer");
990 if (value.
asInt() >= 0)
992 mantissa = value.
asInt();
996 mantissa = -value.
asInt();
1008 mantissa = ret.mantissa();
1009 exponent = ret.exponent();
1010 negative = ret.negative();
1014 Throw<std::runtime_error>(
"invalid amount type");
1017 return {name, issue, mantissa, exponent, native, negative};
1031 <<
"amountFromJsonNoThrow: caught: " << e.
what();
1053 Throw<std::runtime_error>(
1054 "Can't compare amounts that are't comparable!");
1113 boost::multiprecision::uint128_t ret;
1115 boost::multiprecision::multiply(ret, multiplier, multiplicand);
1120 Throw<std::overflow_error>(
1125 return static_cast<uint64_t
>(ret);
1135 boost::multiprecision::uint128_t ret;
1137 boost::multiprecision::multiply(ret, multiplier, multiplicand);
1143 Throw<std::overflow_error>(
1149 return static_cast<uint64_t
>(ret);
1155 if (den == beast::zero)
1156 Throw<std::runtime_error>(
"division by zero");
1158 if (num == beast::zero)
1193 numOffset - denOffset - 17,
1200 if (v1 == beast::zero || v2 == beast::zero)
1210 if (minV > 3000000000ull)
1211 Throw<std::runtime_error>(
"Native value overflow");
1213 if (((maxV >> 32) * minV) > 2095475792ull)
1214 Throw<std::runtime_error>(
"Native value overflow");
1252 offset1 + offset2 + 14,
1292 value += (loops >= 2) ? 9 : 10;
1326 bool hadRemainder =
false;
1334 hadRemainder |= (value != (newValue * 10));
1339 (hadRemainder && roundUp) ? 10 : 9;
1363 class NumberRoundModeGuard
1365 saveNumberRoundMode saved_;
1373 NumberRoundModeGuard(NumberRoundModeGuard
const&) =
delete;
1375 NumberRoundModeGuard&
1376 operator=(NumberRoundModeGuard
const&) =
delete;
1381 class DontAffectNumberRoundMode
1388 DontAffectNumberRoundMode(DontAffectNumberRoundMode
const&) =
delete;
1390 DontAffectNumberRoundMode&
1391 operator=(DontAffectNumberRoundMode
const&) =
delete;
1402 typename MightSaveRound>
1410 if (v1 == beast::zero || v2 == beast::zero)
1413 bool const xrp =
isXRP(issue);
1422 if (minV > 3000000000ull)
1423 Throw<std::runtime_error>(
"Native value overflow");
1425 if (((maxV >> 32) * minV) > 2095475792ull)
1426 Throw<std::runtime_error>(
"Native value overflow");
1465 int offset = offset1 + offset2 + 14;
1466 if (resultNegative != roundUp)
1468 CanonicalizeFunc(xrp, amount, offset, roundUp);
1474 return STAmount(issue, amount, offset, resultNegative);
1477 if (roundUp && !resultNegative && !result)
1491 return STAmount(issue, amount, offset, resultNegative);
1503 return mulRoundImpl<canonicalizeRound, DontAffectNumberRoundMode>(
1504 v1, v2, issue, roundUp);
1514 return mulRoundImpl<canonicalizeRoundStrict, NumberRoundModeGuard>(
1515 v1, v2, issue, roundUp);
1520 template <
typename MightSaveRound>
1528 if (den == beast::zero)
1529 Throw<std::runtime_error>(
"division by zero");
1531 if (num == beast::zero)
1566 numVal,
tenTo17, denVal, (resultNegative != roundUp) ? denVal - 1 : 0);
1568 int offset = numOffset - denOffset - 17;
1570 if (resultNegative != roundUp)
1578 MightSaveRound
const savedRound(
1579 roundUp ^ resultNegative ? upward : downward);
1580 return STAmount(issue, amount, offset, resultNegative);
1583 if (roundUp && !resultNegative && !result)
1597 return STAmount(issue, amount, offset, resultNegative);
1609 return divRoundImpl<DontAffectNumberRoundMode>(num, den, issue, roundUp);
1619 return divRoundImpl<NumberRoundModeGuard>(num, den, issue, roundUp);