16 #include <gtest/gtest.h>
18 #include <boost/math/constants/constants.hpp>
24 namespace mp = boost::multiprecision;
27 using std::numeric_limits;
31 TEST(MultiPrecisionIntTest, Conversion) {
42 x = numeric_limits<int32_t>::max();
45 x = numeric_limits<int32_t>::min();
48 x = numeric_limits<int64_t>::max();
51 x = numeric_limits<int64_t>::min();
54 x = numeric_limits<int64_t>::max();
56 y = numeric_limits<int64_t>::max();
60 x = -numeric_limits<int64_t>::max();
62 y = -numeric_limits<int64_t>::max();
68 memset(&MAX_VALUE, 255,
sizeof(MAX_VALUE));
69 uint8_t* buf =
reinterpret_cast<uint8_t*
>(&MAX_VALUE);
72 bool overflow =
false;
74 EXPECT_FALSE(overflow);
77 TEST(MultiPrecisionIntTest, HighLowBits) {
80 for (
int i = 0; i <
sizeof(x); ++i) {
81 *(
reinterpret_cast<uint8_t*
>(&x) + i) = i;
83 EXPECT_EQ(
LowBits(x), 0x0706050403020100);
84 EXPECT_EQ(
HighBits(x), 0x0f0e0d0c0b0a0908);
89 TEST(MultiPrecisionIntTest, Example) {
91 v128 +=
int128_t(numeric_limits<uint64_t>::max());
92 v128 +=
int128_t(numeric_limits<uint64_t>::max());
94 v128 -=
int128_t(numeric_limits<uint64_t>::max());
95 EXPECT_EQ(v128, numeric_limits<uint64_t>::max());
97 v128 -=
int128_t(numeric_limits<uint64_t>::max());
105 using boost::math::constants::pi;
106 return pi<T>() * r * r;
109 TEST(MultiPrecisionFloatTest, Example) {
110 const float r_f(
float(123) / 100);
113 const double r_d(
double(123) / 100);
116 const mp::cpp_dec_float_50 r_mp(mp::cpp_dec_float_50(123) / 100);
123 ss << setprecision(numeric_limits<float>::digits10)
125 EXPECT_EQ(ss.str(),
"4.75292");
128 ss << std::setprecision(std::numeric_limits<double>::digits10)
130 EXPECT_EQ(ss.str(),
"4.752915525616");
133 ss << std::setprecision(std::numeric_limits<mp::cpp_dec_float_50>::digits10)
135 EXPECT_EQ(ss.str(),
"4.7529155256159981904701331745635599135018975843146");
140 int main(
int argc,
char **argv) {
141 ::testing::InitGoogleTest(&argc, argv);
142 return RUN_ALL_TESTS();
int128_t ConvertToInt128(int256_t x, int128_t max_value, bool *overflow)
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void > > int256_t
Define 256 bit int type.
int256_t ConvertToInt256(const int128_t &x)
uint64_t LowBits(int128_t x)
int main(int argc, char **argv)
uint64_t HighBits(int128_t x)
Get the high and low bits of an int128_t.
__int128_t int128_t
We use the c++ int128_t type. This is stored using 16 bytes and very performant.