109 #ifndef S2_UTIL_MATH_EXACTFLOAT_EXACTFLOAT_H_ 110 #define S2_UTIL_MATH_EXACTFLOAT_EXACTFLOAT_H_ 120 #include <openssl/bn.h> 122 #include "base/logging.h" 123 #include "base/integral_types.h" 131 static const int kMaxExp = 200*1000*1000;
135 static const int kMinExp = -kMaxExp;
140 static const int kMaxPrec = 64 << 20;
148 kRoundTiesAwayFromZero,
151 kRoundTowardPositive,
186 explicit ExactFloat(
const char* s) { Unimplemented(); }
219 int max_prec()
const {
return kMaxPrec; }
231 void set_zero(
int sign);
235 void set_inf(
int sign);
248 inline bool is_zero()
const;
251 inline bool is_inf()
const;
254 inline bool is_nan()
const;
260 inline bool is_normal()
const;
264 inline bool is_finite()
const;
267 inline bool sign_bit()
const;
272 inline int sgn()
const;
291 double ToDouble()
const;
302 string ToString()
const;
306 string ToStringWithMaxDigits(
int max_digits)
const;
312 string ToUniqueString()
const;
317 static int NumSignificantDigitsForPrec(
int prec);
320 friend std::ostream& operator<<(std::ostream& o,
ExactFloat const& f) {
321 return o << f.ToString();
330 ExactFloat RoundToMaxPrec(
int max_prec, RoundingMode mode)
const;
339 ExactFloat operator+()
const {
return *
this; }
433 friend long long llround(
const ExactFloat& a);
442 return Unimplemented();
451 return Unimplemented();
456 return remainder(a, b);
466 return Unimplemented();
485 return ldexp(a, exp);
507 #if defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER < 0x10100000L 511 BigNum() { BN_init(&bn_); }
515 ~
BigNum() { BN_free(&bn_); }
516 BIGNUM* get() {
return &bn_; }
517 const BIGNUM* get()
const {
return &bn_; }
524 BigNum() : bn_(BN_new()) {}
525 BigNum(
const BigNum&) =
delete;
526 BigNum& operator=(
const BigNum&) =
delete;
527 ~BigNum() { BN_free(bn_); }
528 BIGNUM* get() {
return bn_; }
529 const BIGNUM* get()
const {
return bn_; }
539 static const int32 kExpNaN = INT_MAX;
540 static const int32 kExpInfinity = INT_MAX - 1;
541 static const int32 kExpZero = INT_MAX - 2;
553 static const int kDoubleMantissaBits = 53;
557 double ToDoubleHelper()
const;
561 ExactFloat RoundToPowerOf2(
int bit_exp, RoundingMode mode)
const;
566 int GetDecimalDigits(
int max_digits,
string* digits)
const;
584 int ScaleAndCompare(
const ExactFloat& b)
const;
593 inline ExactFloat CopyWithSign(
int sign)
const;
599 template <
class T> T ToInteger(RoundingMode mode)
const;
608 inline ExactFloat::ExactFloat() : sign_(1), bn_exp_(kExpZero) {
611 inline bool ExactFloat::is_zero()
const {
return bn_exp_ == kExpZero; }
612 inline bool ExactFloat::is_inf()
const {
return bn_exp_ == kExpInfinity; }
613 inline bool ExactFloat::is_nan()
const {
return bn_exp_ == kExpNaN; }
614 inline bool ExactFloat::is_normal()
const {
return bn_exp_ < kExpZero; }
615 inline bool ExactFloat::is_finite()
const {
return bn_exp_ <= kExpZero; }
616 inline bool ExactFloat::sign_bit()
const {
return sign_ < 0; }
618 inline int ExactFloat::sgn()
const {
619 return (is_nan() || is_zero()) ? 0 : sign_;
628 if (a.is_nan() || b.is_nan())
return false;
640 inline ExactFloat ExactFloat::CopyWithSign(
int sign)
const {
646 #endif // S2_UTIL_MATH_EXACTFLOAT_EXACTFLOAT_H_