cpp_mp/main.cpp
2025-11-26 21:59:39 +01:00

232 lines
6.8 KiB
C++

#include <algorithm>
#include <functional>
#include <iostream>
//#include "mp.hpp"
#include "mp/int.hpp"
#include "mp/math.hpp"
#include "mp/lib.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;
}
int main()
{
// 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;
// {
// 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;
mp::Int<16> b;
mp::Int<32> c;
mp::parse_string("690000100000000000000000000000000000", a);
mp::parse_string("10690010000000000000000", b);
c = a / b;
PrintDec("a", a);
PrintDec("b", b);
PrintDec("c", c);
}
{
display_op("+",
mp::from_string<32>("123456789012345678901234567890"),
mp::from_string<32>("987654321098765432109876543210"),
std::plus<>{});
display_op("+",
mp::from_string<32>("987654321098765432109876543210"),
mp::from_string<32>("-123456789012345678901234567890"),
std::plus<>{});
display_op("+",
mp::from_string<32>("-123456789012345678901234567890"),
mp::from_string<32>("-987654321098765432109876543210"),
std::plus<>{});
display_op("+",
mp::from_string<32>("123456789012345678901234567890"),
mp::from_string<32>("-987654321098765432109876543210"),
std::plus<>{});
display_op("-",
mp::from_string<32>("987654321098765432109876543210"),
mp::from_string<32>("123456789012345678901234567890"),
std::minus<>{});
display_op("-",
mp::from_string<32>("123456789012345678901234567890"),
mp::from_string<32>("987654321098765432109876543210"),
std::minus<>{});
display_op("-",
mp::from_string<32>("-123456789012345678901234567890"),
mp::from_string<32>("-987654321098765432109876543210"),
std::minus<>{});
display_op("-",
mp::from_string<32>("987654321098765432109876543210"),
mp::from_string<32>("-123456789012345678901234567890"),
std::minus<>{});
display_op("*",
mp::from_string<32>("12345678901234567890"),
mp::from_string<32>("98765432109876543210"),
std::multiplies<>{});
display_op("*",
mp::from_string<32>("-12345678901234567890"),
mp::from_string<32>("98765432109876543210"),
std::multiplies<>{});
display_op("*",
mp::from_string<32>("-12345678901234567890"),
mp::from_string<32>("-98765432109876543210"),
std::multiplies<>{});
display_op("/",
mp::from_string<32>("1219326311370217952237463801111263506900"),
mp::from_string<32>("12345678901234567890"),
std::divides<>{});
display_op("/",
mp::from_string<32>("-1219326311370217952237463801111263506900"),
mp::from_string<32>("12345678901234567890"),
std::divides<>{});
display_op("/",
mp::from_string<32>("-1219326311370217952237463801111263506900"),
mp::from_string<32>("-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<>{});
}
}