|
1 | | -# slimcpp |
2 | | -Simple Long Integer Math for C++ |
| 1 | +# SLIMCPP |
| 2 | +# Simple long integer math libtary for C++ |
| 3 | +This library implements long integers that exceed maximum size of native type supported by a specific compiler by 2-4 times. In some cases, it is necessary to temporarily perform calculations with precision exceeding the maximum supported size of integers, and then return the result to its native size again. |
| 4 | +The main features: |
| 5 | +* easy to use: the library is header-only |
| 6 | +* small: consists of few header files, there is no dependencies |
| 7 | +* speed: use intrinsics if supported |
| 8 | +* cross-platform: supports MSVC, GCC and CLANG C++17 compilers |
| 9 | + |
| 10 | +Library implements two main classes: |
| 11 | +```c++ |
| 12 | +template<typename native_t = uintmax_t, uint_t size = 2> |
| 13 | +class long_uint_t; // unsigned integer |
| 14 | +template<typename native_t = uintmax_t, uint_t size = 2> |
| 15 | +class long_int_t; // signed integer |
| 16 | +``` |
| 17 | +where native_t may be one of base unsigned type and size must by power of two. |
| 18 | +## Integration |
| 19 | +Library implements four predefined types: uint128_t, uint256_t, int128_t, int256_t. You can use them in you project by include code below: |
| 20 | +```c++ |
| 21 | +#include <slimcpplib/long_int.h> |
| 22 | + |
| 23 | +namespace your_namespace |
| 24 | +{ |
| 25 | + using uint128_t = slim::uint128_t; |
| 26 | + using uint256_t = slim::uint256_t; |
| 27 | + using int128_t = slim::int128_t; |
| 28 | + using int256_t = slim::int256_t; |
| 29 | + using namespace slim::literals; |
| 30 | + |
| 31 | +} // namespace your_namespace |
| 32 | +``` |
| 33 | +## Constant declaration: |
| 34 | +```c++ |
| 35 | +constexpr auto uo = 03766713523035452062041773345651416625031020_ui128; // octal unsigned integer |
| 36 | +constexpr auto ud = 338770000845734292534325025077361652240_ui128; // decimal unsigned integer |
| 37 | +constexpr auto uh = 0xfedcba9876543210fedcba9876543210_ui128; // hexadecimal unsigned integer |
| 38 | + |
| 39 | +constexpr auto io = -03766713523035452062041773345651416625031020_si128; // octal signed integer |
| 40 | +constexpr auto id = -338770000845734292534325025077361652240_si128; // decimal signed integer |
| 41 | +constexpr auto ih = -0xfedcba9876543210fedcba9876543210_si128; // hexadecimal signed integer |
| 42 | +``` |
| 43 | +also supported (') separator for integer literals: |
| 44 | +```c++ |
| 45 | +constexpr auto u = 0xfedcba98'76543210'fedcba98'76543210_ui128; // hexadecimal unsigned integer |
| 46 | +``` |
| 47 | +## Construction: |
| 48 | +```c++ |
| 49 | +const uint128_t u; // construct uninitialized unsigned integer |
| 50 | +const uint128_t u = 1U; // construction from unsigned integer |
| 51 | +const int128_t s = -1; // construction from signed integer |
| 52 | +const uint128_t u = 10000_ui128; // construction from long unsigned integer |
| 53 | +const int128_t u = -10000_i128; // construction from long signed integer |
| 54 | +const uint128_t u = true; // construction from boolean value |
| 55 | +``` |
| 56 | +## Operators |
| 57 | +long_uint_t type supports following operators: |
| 58 | +==, !=, <, <=, >, >=, <<=, <<, >>=, >>, +=, +, ++, -=, -, --, *=, *, /=, /, %=, %, ~, &=, &, |=, |, ^=, ^ |
| 59 | +long_int_t type supports following operators: |
| 60 | +==, !=, <, <=, >, >=, <<=, <<, >>=, >>, +=, +, ++, -=, -, --, *=, *, /=, /, %=, % |
0 commit comments