18 #include "util/math/exactfloat/exactfloat.h" 27 #include <openssl/bn.h> 28 #include <openssl/crypto.h> 30 #include "base/logging.h" 31 #include "base/integral_types.h" 37 const int ExactFloat::kMinExp;
38 const int ExactFloat::kMaxExp;
39 const int ExactFloat::kMaxPrec;
40 const int32 ExactFloat::kExpNaN;
41 const int32 ExactFloat::kExpInfinity;
42 const int32 ExactFloat::kExpZero;
43 const int ExactFloat::kDoubleMantissaBits;
50 ExactFloat::kMaxExp <= INT_MAX / 2 &&
51 ExactFloat::kMinExp - ExactFloat::kMaxPrec >= INT_MIN / 2,
52 "exactfloat exponent might overflow");
59 #ifdef OPENSSL_IS_BORINGSSL 61 inline static void BN_ext_set_uint64(BIGNUM* bn, uint64 v) {
62 CHECK(BN_set_u64(bn, v));
67 inline static uint64 BN_ext_get_uint64(
const BIGNUM* bn) {
69 if (!BN_get_u64(bn, &u64)) {
70 DCHECK(
false) <<
"BN has " << BN_num_bits(bn) <<
" bits";
76 static int BN_ext_count_low_zero_bits(
const BIGNUM* bn) {
77 return BN_count_low_zero_bits(bn);
80 #else // !defined(OPENSSL_IS_BORINGSSL) 83 inline static void BN_ext_set_uint64(BIGNUM* bn, uint64 v) {
85 CHECK(BN_set_word(bn, v));
87 static_assert(BN_BITS2 == 32,
"at least 32 bit openssl build needed");
88 CHECK(BN_set_word(bn, static_cast<uint32>(v >> 32)));
89 CHECK(BN_lshift(bn, bn, 32));
90 CHECK(BN_add_word(bn, static_cast<uint32>(v)));
96 inline static uint64 BN_ext_get_uint64(
const BIGNUM* bn) {
97 DCHECK_LE(BN_num_bytes(bn),
sizeof(uint64));
99 return BN_get_word(bn);
101 static_assert(BN_BITS2 == 32,
"at least 32 bit openssl build needed");
102 if (bn->top == 0)
return 0;
103 if (bn->top == 1)
return BN_get_word(bn);
104 DCHECK_EQ(bn->top, 2);
105 return (static_cast<uint64>(bn->d[1]) << 32) + bn->d[0];
109 #if OPENSSL_VERSION_NUMBER < 0x10100000L 113 static int BN_ext_count_low_zero_bits(
const BIGNUM* bn) {
115 for (
int i = 0; i < bn->top; ++i) {
116 BN_ULONG w = bn->d[i];
118 count += 8 *
sizeof(BN_ULONG);
120 for (; (w & 1) == 0; w >>= 1) {
129 #else // OPENSSL_VERSION_NUMBER >= 0x10100000L 131 static int BN_ext_count_low_zero_bits(
const BIGNUM* bn) {
135 std::vector<unsigned char> bytes(BN_num_bytes(bn));
137 size_t ret = BN_bn2lebinpad(bn, bytes.data(), bytes.size());
138 DCHECK_EQ(ret, bytes.size());
141 for (
unsigned char c : bytes) {
145 for (; (c & 1) == 0; c >>= 1) {
154 #endif // OPENSSL_VERSION_NUMBER >= 0x10100000L 156 #endif // !defined(OPENSSL_IS_BORINGSSL) 158 ExactFloat::ExactFloat(
double v) {
159 sign_ = std::signbit(v) ? -1 : 1;
162 }
else if (std::isinf(v)) {
172 double f = frexp(fabs(v), &exp);
173 uint64 m = static_cast<uint64>(ldexp(f, kDoubleMantissaBits));
174 BN_ext_set_uint64(bn_.get(), m);
175 bn_exp_ = exp - kDoubleMantissaBits;
180 ExactFloat::ExactFloat(
int v) {
181 sign_ = (v >= 0) ? 1 : -1;
184 CHECK(BN_set_word(bn_.get(), abs(v)));
192 BN_copy(bn_.get(), b.bn_.get());
213 int ExactFloat::prec()
const {
214 return BN_num_bits(bn_.get());
217 int ExactFloat::exp()
const {
219 return bn_exp_ + BN_num_bits(bn_.get());
222 void ExactFloat::set_zero(
int sign) {
225 if (!BN_is_zero(bn_.get())) BN_zero(bn_.get());
228 void ExactFloat::set_inf(
int sign) {
230 bn_exp_ = kExpInfinity;
231 if (!BN_is_zero(bn_.get())) BN_zero(bn_.get());
234 void ExactFloat::set_nan() {
237 if (!BN_is_zero(bn_.get())) BN_zero(bn_.get());
240 double ExactFloat::ToDouble()
const {
242 if (prec() <= kDoubleMantissaBits) {
243 return ToDoubleHelper();
245 ExactFloat r = RoundToMaxPrec(kDoubleMantissaBits, kRoundTiesToEven);
246 return r.ToDoubleHelper();
250 double ExactFloat::ToDoubleHelper()
const {
251 size_t ret = BN_num_bits(bn_.get());
252 DCHECK_LE(ret, kDoubleMantissaBits);
254 if (is_zero())
return copysign(0, sign_);
255 if (is_inf())
return copysign(INFINITY, sign_);
256 return copysign(NAN, sign_);
258 uint64 d_mantissa = BN_ext_get_uint64(bn_.get());
261 return sign_ * ldexp(static_cast<double>(d_mantissa), bn_exp_);
264 ExactFloat ExactFloat::RoundToMaxPrec(
int max_prec, RoundingMode mode)
const {
267 DCHECK_GE(max_prec, 2);
268 DCHECK_LE(max_prec, kMaxPrec);
271 int shift = prec() - max_prec;
272 if (shift <= 0)
return *
this;
278 return RoundToPowerOf2(bn_exp_ + shift, mode);
281 ExactFloat ExactFloat::RoundToPowerOf2(
int bit_exp, RoundingMode mode)
const {
282 DCHECK_GE(bit_exp, kMinExp - kMaxPrec);
283 DCHECK_LE(bit_exp, kMaxExp);
287 int shift = bit_exp - bn_exp_;
288 if (shift <= 0)
return *
this;
293 if (mode == kRoundTowardPositive) {
294 mode = (sign_ > 0) ? kRoundAwayFromZero : kRoundTowardZero;
295 }
else if (mode == kRoundTowardNegative) {
296 mode = (sign_ > 0) ? kRoundTowardZero : kRoundAwayFromZero;
304 bool increment =
false;
305 if (mode == kRoundTowardZero) {
307 }
else if (mode == kRoundTiesAwayFromZero) {
309 if (BN_is_bit_set(bn_.get(), shift - 1))
311 }
else if (mode == kRoundAwayFromZero) {
313 if (BN_ext_count_low_zero_bits(bn_.get()) < shift)
316 DCHECK_EQ(mode, kRoundTiesToEven);
323 if (BN_is_bit_set(bn_.get(), shift - 1) &&
324 ((BN_is_bit_set(bn_.get(), shift) ||
325 BN_ext_count_low_zero_bits(bn_.get()) < shift - 1))) {
329 r.bn_exp_ = bn_exp_ + shift;
330 CHECK(BN_rshift(r.bn_.get(), bn_.get(), shift));
332 CHECK(BN_add_word(r.bn_.get(), 1));
339 int ExactFloat::NumSignificantDigitsForPrec(
int prec) {
351 return static_cast<int>(1 + ceil(prec * (M_LN2 / M_LN10)));
359 static const int kMinSignificantDigits = 10;
361 string ExactFloat::ToString()
const {
362 int max_digits = max(kMinSignificantDigits,
363 NumSignificantDigitsForPrec(prec()));
364 return ToStringWithMaxDigits(max_digits);
367 string ExactFloat::ToStringWithMaxDigits(
int max_digits)
const {
368 DCHECK_GT(max_digits, 0);
370 if (is_nan())
return "nan";
371 if (is_zero())
return (sign_ < 0) ?
"-0" :
"0";
372 return (sign_ < 0) ?
"-inf" :
"inf";
375 int exp10 = GetDecimalDigits(max_digits, &digits);
377 if (sign_ < 0) str.push_back(
'-');
386 if (exp10 <= -4 || exp10 > max_digits) {
388 str.push_back(digits[0]);
389 if (digits.size() > 1) {
391 str.append(digits.begin() + 1, digits.end());
394 sprintf(exp_buf,
"e%+02d", exp10 - 1);
400 if (
unsigned(exp10) >= digits.size()) {
402 for (
int i = exp10 - digits.size(); i > 0; --i) {
406 str.append(digits.begin(), digits.begin() + exp10);
408 str.append(digits.begin() + exp10, digits.end());
412 for (
int i = exp10; i < 0; ++i) {
422 static void IncrementDecimalDigits(
string* digits) {
423 string::iterator pos = digits->end();
424 while (--pos >= digits->begin()) {
425 if (*pos <
'9') { ++*pos;
return; }
428 digits->insert(0,
"1");
431 int ExactFloat::GetDecimalDigits(
int max_digits,
string* digits)
const {
435 BIGNUM* bn = BN_new();
439 CHECK(BN_lshift(bn, bn_.get(), bn_exp_));
444 BIGNUM* power = BN_new();
445 CHECK(BN_set_word(power, -bn_exp_));
446 CHECK(BN_set_word(bn, 5));
447 BN_CTX* ctx = BN_CTX_new();
448 CHECK(BN_exp(bn, bn, power, ctx));
449 CHECK(BN_mul(bn, bn, bn_.get(), ctx));
455 char* all_digits = BN_bn2dec(bn);
456 DCHECK(all_digits !=
nullptr);
459 int num_digits = strlen(all_digits);
460 if (num_digits <= max_digits) {
461 *digits = all_digits;
463 digits->assign(all_digits, max_digits);
468 if (all_digits[max_digits] >=
'5' &&
469 ((all_digits[max_digits-1] & 1) == 1 ||
470 strpbrk(all_digits + max_digits + 1,
"123456789") !=
nullptr)) {
473 IncrementDecimalDigits(digits);
476 bn_exp10 += num_digits - max_digits;
478 OPENSSL_free(all_digits);
481 DCHECK_NE((*digits)[0],
'0');
482 string::iterator pos = digits->end();
483 while (pos[-1] ==
'0') --pos;
484 if (pos < digits->end()) {
485 bn_exp10 += digits->end() - pos;
486 digits->erase(pos, digits->end());
488 DCHECK_LE(digits->size(), max_digits);
492 return bn_exp10 + digits->size();
495 string ExactFloat::ToUniqueString()
const {
497 sprintf(prec_buf,
"<%d>", prec());
498 return ToString() + prec_buf;
505 BN_copy(bn_.get(), b.bn_.get());
511 return CopyWithSign(-sign_);
515 return ExactFloat::SignedSum(a.sign_, &a, b.sign_, &b);
519 return ExactFloat::SignedSum(a.sign_, &a, -b.sign_, &b);
524 if (!a->is_normal() || !b->is_normal()) {
526 if (a->is_nan())
return *a;
527 if (b->is_nan())
return *b;
530 if (b->is_inf() && a_sign != b_sign)
return NaN();
531 return Infinity(a_sign);
533 if (b->is_inf())
return Infinity(b_sign);
535 if (!b->is_zero())
return b->CopyWithSign(b_sign);
537 if (a_sign == b_sign)
return SignedZero(a_sign);
539 return SignedZero(+1);
541 DCHECK(b->is_zero());
542 return a->CopyWithSign(a_sign);
545 if (a->bn_exp_ < b->bn_exp_) {
547 swap(a_sign, b_sign);
552 if (a->bn_exp_ > b->bn_exp_) {
553 CHECK(BN_lshift(r.bn_.get(), a->bn_.get(), a->bn_exp_ - b->bn_exp_));
556 r.bn_exp_ = b->bn_exp_;
557 if (a_sign == b_sign) {
558 CHECK(BN_add(r.bn_.get(), a->bn_.get(), b->bn_.get()));
564 CHECK(BN_sub(r.bn_.get(), a->bn_.get(), b->bn_.get()));
565 if (BN_is_zero(r.bn_.get())) {
567 }
else if (BN_is_negative(r.bn_.get())) {
570 BN_set_negative(r.bn_.get(),
false);
580 void ExactFloat::Canonicalize() {
581 if (!is_normal())
return;
586 if (my_exp < kMinExp || BN_is_zero(bn_.get())) {
588 }
else if (my_exp > kMaxExp) {
590 }
else if (!BN_is_odd(bn_.get())) {
592 DCHECK(!BN_is_zero(bn_.get()));
593 int shift = BN_ext_count_low_zero_bits(bn_.get());
595 CHECK(BN_rshift(bn_.get(), bn_.get(), shift));
601 if (prec() > kMaxPrec) {
607 int result_sign = a.sign_ * b.sign_;
608 if (!a.is_normal() || !b.is_normal()) {
610 if (a.is_nan())
return a;
611 if (b.is_nan())
return b;
614 if (b.is_zero())
return ExactFloat::NaN();
615 return ExactFloat::Infinity(result_sign);
618 if (a.is_zero())
return ExactFloat::NaN();
619 return ExactFloat::Infinity(result_sign);
621 DCHECK(a.is_zero() || b.is_zero());
622 return ExactFloat::SignedZero(result_sign);
625 r.sign_ = result_sign;
626 r.bn_exp_ = a.bn_exp_ + b.bn_exp_;
627 BN_CTX* ctx = BN_CTX_new();
628 CHECK(BN_mul(r.bn_.get(), a.bn_.get(), b.bn_.get(), ctx));
636 if (a.is_nan() || b.is_nan())
return false;
640 if (a.bn_exp_ != b.bn_exp_)
return false;
643 if (a.is_zero() && b.is_zero())
return true;
647 return a.sign_ == b.sign_ && BN_ucmp(a.bn_.get(), b.bn_.get()) == 0;
650 int ExactFloat::ScaleAndCompare(
const ExactFloat& b)
const {
651 DCHECK(is_normal() && b.is_normal() && bn_exp_ >= b.bn_exp_);
653 CHECK(BN_lshift(tmp.bn_.get(), tmp.bn_.get(), bn_exp_ - b.bn_exp_));
654 return BN_ucmp(tmp.bn_.get(), b.bn_.get());
657 bool ExactFloat::UnsignedLess(
const ExactFloat& b)
const {
659 if (is_inf() || b.is_zero())
return false;
660 if (is_zero() || b.is_inf())
return true;
662 int cmp = exp() - b.exp();
663 if (cmp != 0)
return cmp < 0;
666 return (bn_exp_ >= b.bn_exp_ ?
667 ScaleAndCompare(b) < 0 : b.ScaleAndCompare(*
this) > 0);
672 if (a.is_nan() || b.is_nan())
return false;
674 if (a.is_zero() && b.is_zero())
return false;
676 if (a.sign_ != b.sign_)
return a.sign_ < b.sign_;
678 return (a.sign_ > 0) ? a.UnsignedLess(b) : b.UnsignedLess(a);
686 return a.CopyWithSign(+1);
691 if (a.is_nan())
return b;
692 if (b.is_nan())
return a;
694 if (a.sign_ != b.sign_) {
695 return (a.sign_ < b.sign_) ? b : a;
697 return (a < b) ? b : a;
702 if (a.is_nan())
return b;
703 if (b.is_nan())
return a;
705 if (a.sign_ != b.sign_) {
706 return (a.sign_ < b.sign_) ? a : b;
708 return (a < b) ? a : b;
713 return (a <= b) ? 0 : (a - b);
717 return a.RoundToPowerOf2(0, ExactFloat::kRoundTowardPositive);
721 return a.RoundToPowerOf2(0, ExactFloat::kRoundTowardNegative);
725 return a.RoundToPowerOf2(0, ExactFloat::kRoundTowardZero);
729 return a.RoundToPowerOf2(0, ExactFloat::kRoundTiesAwayFromZero);
733 return a.RoundToPowerOf2(0, ExactFloat::kRoundTiesToEven);
737 T ExactFloat::ToInteger(RoundingMode mode)
const {
738 using std::numeric_limits;
739 static_assert(
sizeof(T) <=
sizeof(uint64),
"max 64 bits supported");
740 static_assert(numeric_limits<T>::is_signed,
"only signed types supported");
741 const int64 kMinValue = numeric_limits<T>::min();
742 const int64 kMaxValue = numeric_limits<T>::max();
745 if (r.is_nan())
return kMaxValue;
746 if (r.is_zero())
return 0;
750 int64 value = BN_ext_get_uint64(r.bn_.get()) << r.bn_exp_;
751 if (r.sign_ < 0) value = -value;
752 return max(kMinValue, min(kMaxValue, value));
755 return (r.sign_ < 0) ? kMinValue : kMaxValue;
759 return a.ToInteger<
long>(ExactFloat::kRoundTiesToEven);
763 return a.ToInteger<
long long>(ExactFloat::kRoundTiesToEven);
767 return a.ToInteger<
long>(ExactFloat::kRoundTiesAwayFromZero);
771 return a.ToInteger<
long long>(ExactFloat::kRoundTiesAwayFromZero);
775 return a.CopyWithSign(b.sign_);
779 if (!a.is_normal()) {
786 return ldexp(a, -a.exp());
790 if (!a.is_normal())
return a;
795 exp = min(ExactFloat::kMaxExp + 1 - a_exp,
796 max(ExactFloat::kMinExp - 1 + a_exp, exp));
807 exp = max(static_cast<long>(INT_MIN), min(static_cast<long>(INT_MAX), exp));
808 return ldexp(a, exp);
812 if (a.is_zero())
return FP_ILOGB0;
813 if (a.is_inf())
return INT_MAX;
814 if (a.is_nan())
return FP_ILOGBNAN;
820 if (a.is_zero())
return ExactFloat::Infinity(-1);
821 if (a.is_inf())
return ExactFloat::Infinity(+1);
822 if (a.is_nan())
return a;
828 LOG(FATAL) <<
"Unimplemented ExactFloat method called";