cpp_mp/main.cpp
2025-12-04 10:03:30 +01:00

184 lines
6.1 KiB
C++

#include <functional>
#include <iostream>
#include "mp/mp.hpp"
template <class T>
static void PrintInt(const char* name, const T& val)
{
std::cout << name << " = " << mp::to_hex_string(val) << std::endl;
}
template <mp::AnyMpInt T>
static void PrintDec(const char* name, const T& val){
std::cout << name << " = " << mp::to_string(val) << std::endl;
}
using MyInt = mp::Int<32>;
static void display_op(const char* desc, const MyInt& a, const MyInt& b, std::function<MyInt(const MyInt&, const MyInt&)> op)
{
std::cout << "op: " << mp::to_string(a) << ' ' << desc << ' ' << mp::to_string(b) << " = " << mp::to_string(op(a, b)) << std::endl;
}
template <char... Cs>
constexpr static auto operator""_mpi()
{
constexpr char str[] = {Cs..., '\0'};
return mp::from_string<128>(str);
}
int main()
{
1000000_mpi;
// mp::Int a{0xDEADBEEFDEADF154, 0x0123456789ABCDEF, 0x1111222233334444};
mp::Int<32> a{0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF};
mp::Int<1> b{0x55};
PrintInt("a", a);
PrintInt("b", b);
auto c = a + b;
//auto c = binary_opa, b);
PrintInt("c", c);
//a += mp::Int<1>{1};
//PrintInt("a", a);
//a -= mp::Int<1>{1};
//PrintInt("a", a);
std::cout << mp::Int<17>::LAST_ELEM_MASK << std::endl;
std::cout << sizeof(mp::Int<1>) << std::endl;
std::cout << sizeof(mp::Int<2>) << std::endl;
std::cout << sizeof(mp::Int<3>) << std::endl;
std::cout << sizeof(mp::Int<4>) << std::endl;
std::cout << sizeof(mp::Int<5>) << std::endl;
std::cout << sizeof(mp::Int<6>) << std::endl;
std::cout << sizeof(mp::Int<7>) << std::endl;
std::cout << sizeof(mp::Int<8>) << std::endl;
std::cout << sizeof(mp::Int<9>) << std::endl;
std::cout << sizeof(mp::Int<10>) << std::endl;
std::cout << sizeof(mp::Int<20>) << std::endl;
std::cout << sizeof(mp::Int<30>) << std::endl;
std::cout << sizeof(mp::Int<40>) << std::endl;
std::cout << sizeof(mp::Int<50>) << std::endl;
std::cout << sizeof(mp::Int<60>) << std::endl;
std::cout << sizeof(mp::Int<70>) << std::endl;
std::cout << sizeof(mp::Int<80>) << std::endl;
std::cout << sizeof(mp::Int<90>) << std::endl;
std::cout << sizeof(mp::Int<100>) << std::endl;
std::cout << sizeof(mp::Int<110>) << std::endl;
std::cout << sizeof(mp::Int<120>) << std::endl;
std::cout << sizeof(mp::Int<130>) << std::endl;
std::cout << sizeof(mp::Int<140>) << std::endl;
std::cout << sizeof(mp::Int<150>) << std::endl;
std::cout << sizeof(mp::Int<160>) << std::endl;
// auto x = 12345678901234567890123456789012345678901234567890123456789012345678901234567890_mpi;
// {
// mp::Int<1024> acc{1};
// try
// {
// while (true)
// {
// acc *= mp::Int<1>{10};
// PrintDec("acc", acc);
// }
// }
// catch (const mp::OverflowErrorOf<decltype(acc), mp::Int<1>>& e)
// {
// std::cout << "overflow" << std::endl;
// PrintDec("value", e.value());
// }
// catch (const std::exception& e)
// {
// std::cout << "error: " << e.what() << std::endl;
// }
// }
//{
// mp::Int<32> val{1};
// for (mp::Int<4> a{1}; a < 100U; a += 1U)
// {
// for (size_t i = 0; i < 20; ++i)
// {
// val *= a;
// //PrintInt("val", val);
// }
// for (size_t i = 0; i < 20; ++i)
// {
// val /= a;
// }
// //if (val != 0U)
// //{
// PrintInt("val", val);
// //}
// }
//}
// {
// mp::Int<32> a{0xA0000000, 0x6D7217CA, 0x431E0FAE, 0x1};
// mp::Int<16> b{0x6FC10000, 0x2386F2};
// PrintDec("a", a);
// PrintDec("b", b);
// auto c = a / b;
// PrintDec("c", c);
// }
{
mp::Int<32> a = "690000100000000000000000000000000000";
mp::Int<16> b{"10690010000000000000000"};
mp::Int<32> c;
c = a / b;
PrintDec("a", a);
PrintDec("b", b);
PrintDec("c", c);
}
{
display_op("+", 123456789012345678901234567890_mpi, 987654321098765432109876543210_mpi, std::plus<>{});
display_op("+", 987654321098765432109876543210_mpi, -123456789012345678901234567890_mpi, std::plus<>{});
display_op("+", "-123456789012345678901234567890", "-987654321098765432109876543210", std::plus<>{});
display_op("+", "123456789012345678901234567890", "-987654321098765432109876543210", std::plus<>{});
display_op("-", "987654321098765432109876543210", "123456789012345678901234567890", std::minus<>{});
display_op("-", "123456789012345678901234567890", "987654321098765432109876543210", std::minus<>{});
display_op("-", "-123456789012345678901234567890", "-987654321098765432109876543210", std::minus<>{});
display_op("-", "987654321098765432109876543210", "-123456789012345678901234567890", std::minus<>{});
display_op("*", "12345678901234567890", "98765432109876543210", std::multiplies<>{});
display_op("*", "-12345678901234567890", "98765432109876543210", std::multiplies<>{});
display_op("*", "-12345678901234567890", "-98765432109876543210", std::multiplies<>{});
display_op("/", "1219326311370217952237463801111263526900", "12345678901234567890", std::divides<>{});
display_op("/", "-1219326311370217952237463801111263526900", "12345678901234567890", std::divides<>{});
display_op("/", "-1219326311370217952237463801111263526900", "-12345678901234567890", std::divides<>{});
auto a = mp::from_string<32>("12345678901234567890");
auto b = mp::from_string<32>("98765432109876543210");
b.set_negative(true);
display_op("*",
a,
b,
std::multiplies<>{});
for (mp::UnlimitedInt i = "10000000000000000000000000000000000000000000";
i > mp::UnlimitedInt("-20000000000000000000000000000000000000000000");
i -= mp::UnlimitedInt("1000000000000000000000000000000000000000000"))
{
PrintDec("i", i);
}
}
}