|
| 1 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// Simple Long Integer Math for C++ |
| 4 | +// version 1.3 |
| 5 | +// |
| 6 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | +// |
| 8 | +// Licensed under the MIT License <http://opensource.org/licenses/MIT>. |
| 9 | +// SPDX-License-Identifier: MIT |
| 10 | +// |
| 11 | +// Copyright (c) 2021 Yury Kalmykov <y_kalmykov@mail.ru>. |
| 12 | +// |
| 13 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | +// of this software and associated documentation files (the "Software"), to deal |
| 15 | +// in the Software without restriction, including without limitation the rights |
| 16 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 17 | +// copies of the Software, and to permit persons to whom the Software is |
| 18 | +// furnished to do so, subject to the following conditions: |
| 19 | +// |
| 20 | +// The above copyright notice and this permission notice shall be included in all |
| 21 | +// copies or substantial portions of the Software. |
| 22 | +// |
| 23 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 28 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 29 | +// SOFTWARE. |
| 30 | +// |
| 31 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 32 | +// |
| 33 | +// $Id:$ |
| 34 | +// |
| 35 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 36 | + |
| 37 | +#include "../include/slimcpplib/long_int.h" |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 42 | +// define 128-bit and 256-bit integer types in your namespace |
| 43 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 44 | + |
| 45 | +using namespace slim::literals; |
| 46 | +using uint128_t = slim::uint128_t; |
| 47 | +using int128_t = slim::int128_t; |
| 48 | +using uint256_t = slim::uint256_t; |
| 49 | +using int256_t = slim::int256_t; |
| 50 | + |
| 51 | +//using slim_uint64_t = slim::long_uint_t<uint32_t, 2>; |
| 52 | +//using uint128_t = slim::long_uint_t<slim_uint64_t, 2>; |
| 53 | +//using int128_t = slim::long_int_t<slim_uint64_t, 2>; |
| 54 | +//using uint256_t = slim::long_uint_t<slim_uint64_t, 4>; |
| 55 | +//using int256_t = slim::long_int_t<slim_uint64_t, 4>; |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 60 | +// standalone routines |
| 61 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 62 | + |
| 63 | +static void initalization_and_conversion() noexcept |
| 64 | +{ |
| 65 | + // construct long integer |
| 66 | + |
| 67 | + uint128_t u1; // can't be used as constant expressions |
| 68 | + assert(u1 == u1); |
| 69 | + |
| 70 | + |
| 71 | + // construct long integer from unsigned integer value |
| 72 | + |
| 73 | + constexpr uint256_t u2 = 1U; |
| 74 | + static_assert(u2 == 1); |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + // construct long integer from signed integer value |
| 79 | + |
| 80 | + constexpr uint128_t u3 = -1; // sign will be expanded |
| 81 | + static_assert(u3 == -1); |
| 82 | + constexpr int128_t s4 = -1; // sign will be expanded |
| 83 | + static_assert(s4 == -1); |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + // construct long integer from long integer with opposite sign |
| 88 | + |
| 89 | + constexpr int128_t s5 = uint128_t(-1); |
| 90 | + static_assert(u3 == -1); |
| 91 | + constexpr uint128_t u6 = int128_t(-1); |
| 92 | + static_assert(s4 == -1); |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + // construct long integer from shorter long integer value |
| 97 | + |
| 98 | + constexpr int256_t s7 = int128_t(-1); // sign will be expanded |
| 99 | + static_assert(s7 == -1); |
| 100 | + const int256_t s8 = int128_t(-1); // sign will be expanded |
| 101 | + assert(s8 == -1); |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + // construct long integer from longer long integer value |
| 106 | + |
| 107 | + //constexpr int s9 = u2; // error! implicit conversion not allowed |
| 108 | + constexpr int s9 = static_cast<int>(uint256_t(1)); |
| 109 | + static_assert(s9 == 1); |
| 110 | + |
| 111 | + // constexpr int128_t s10 = u2; // error! implicit conversion not allowed |
| 112 | + constexpr int128_t s10 = static_cast<uint128_t>(uint256_t(-1)); |
| 113 | + static_assert(s10 == -1); |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + // construct long integer from long integer constant |
| 118 | + |
| 119 | + constexpr auto u11 = 0xaf5705a4'89525e79'a5120c42'daebbc57'd55f0277'53a05970'9fee8a5d'41e2ae79_ui256; |
| 120 | + static_assert(u11 == 0xaf5705a4'89525e79'a5120c42'daebbc57'd55f0277'53a05970'9fee8a5d'41e2ae79_ui256); |
| 121 | + constexpr auto s12 = -0xf473e8e5'f6e812c3'fde4523b'51b6d251_si128; |
| 122 | + static_assert(s12 == -0xf473e8e5'f6e812c3'fde4523b'51b6d251_si128); |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + // construct long integer from boolean value |
| 127 | + |
| 128 | + constexpr uint128_t u13 = true; // implicitly converted to one as standard |
| 129 | + static_assert(u13 == 1); |
| 130 | + constexpr int128_t s14 = false; // implicitly converted to zero as standard |
| 131 | + static_assert(s14 == 0); |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 137 | +void comparison() noexcept |
| 138 | +{ |
| 139 | + // compare long unsigned integers |
| 140 | + |
| 141 | + constexpr auto uzero = 0_ui128; |
| 142 | + |
| 143 | + static_assert(uzero == 0); |
| 144 | + static_assert(uzero != 1); |
| 145 | + |
| 146 | + assert(uzero < -1); |
| 147 | + assert(uzero <= 0); |
| 148 | + assert(-1 > uzero); |
| 149 | + assert(-1 >= uzero); |
| 150 | + |
| 151 | + // compare long signed integers |
| 152 | + |
| 153 | + constexpr auto szero = 0_si128; |
| 154 | + |
| 155 | + static_assert(szero == 0); |
| 156 | + static_assert(szero != 1); |
| 157 | + |
| 158 | + assert(-1 < szero); |
| 159 | + assert(-1 <= szero); |
| 160 | + assert(szero > -1); |
| 161 | + assert(szero >= 0); |
| 162 | +} |
| 163 | + |
| 164 | + |
| 165 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 166 | +static void bitwise_oprations() noexcept |
| 167 | +{ |
| 168 | + constexpr uint256_t zero = 0; |
| 169 | + constexpr uint256_t ones = -1; |
| 170 | + |
| 171 | + // inverse all bits |
| 172 | + static_assert(~zero == -1); |
| 173 | + static_assert(~ones == 0); |
| 174 | + |
| 175 | + // perform AND operation |
| 176 | + static_assert((zero & ones) == 0); |
| 177 | + |
| 178 | + // perform OR operation |
| 179 | + static_assert((zero | ones) == -1); |
| 180 | + |
| 181 | + // perform exclusive OR operation |
| 182 | + static_assert((zero ^ ones) == -1); |
| 183 | +} |
| 184 | + |
| 185 | + |
| 186 | + |
| 187 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 188 | +static void bit_shift_operations() noexcept |
| 189 | +{ |
| 190 | + constexpr uint128_t bits = -1; |
| 191 | + constexpr uint128_t shift = 127; |
| 192 | + |
| 193 | + assert((bits >> static_cast<int>(shift)) == 1); // shift argument has native integer type |
| 194 | + assert((bits << 127) == 0x80000000'00000000'00000000'00000000_ui128); |
| 195 | +} |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 200 | +static void arithmetic_operations() noexcept |
| 201 | +{ |
| 202 | + constexpr auto uvalue = 0xf473e8e5'f6e812c3'fde4523b'51b6d251_ui128; |
| 203 | + constexpr auto svalue = -0xf473e8e5'f6e812c3'fde4523b'51b6d251_si128; |
| 204 | + |
| 205 | + // unary plus |
| 206 | + assert(+uvalue == 0xf473e8e5'f6e812c3'fde4523b'51b6d251_ui128); |
| 207 | + assert(+svalue == -0xf473e8e5'f6e812c3'fde4523b'51b6d251_ui128); |
| 208 | + |
| 209 | + // prefix increment |
| 210 | + assert(++uint128_t(uvalue) == 0xf473e8e5'f6e812c3'fde4523b'51b6d252_ui128); |
| 211 | + assert(++int128_t(svalue) == -0xf473e8e5'f6e812c3'fde4523b'51b6d250_ui128); |
| 212 | + |
| 213 | + // postfix increment |
| 214 | + assert(uint128_t(uvalue)++ == 0xf473e8e5'f6e812c3'fde4523b'51b6d251_ui128); |
| 215 | + assert(int128_t(svalue)++ == -0xf473e8e5'f6e812c3'fde4523b'51b6d251_ui128); |
| 216 | + |
| 217 | + // addition |
| 218 | + assert(uvalue + 1 == 0xf473e8e5'f6e812c3'fde4523b'51b6d252_ui128); |
| 219 | + assert(svalue + 1 == -0xf473e8e5'f6e812c3'fde4523b'51b6d250_ui128); |
| 220 | + |
| 221 | + |
| 222 | + |
| 223 | + // unary minus |
| 224 | + assert(-uvalue == -0xf473e8e5'f6e812c3'fde4523b'51b6d251_ui128); |
| 225 | + assert(-svalue == 0xf473e8e5'f6e812c3'fde4523b'51b6d251_ui128); |
| 226 | + |
| 227 | + // prefix decrement |
| 228 | + assert(--uint128_t(uvalue) == 0xf473e8e5'f6e812c3'fde4523b'51b6d250_ui128); |
| 229 | + assert(--int128_t(svalue) == -0xf473e8e5'f6e812c3'fde4523b'51b6d252_ui128); |
| 230 | + |
| 231 | + // postfix decrement |
| 232 | + assert(uint128_t(uvalue)-- == 0xf473e8e5'f6e812c3'fde4523b'51b6d251_ui128); |
| 233 | + assert(int128_t(svalue)-- == -0xf473e8e5'f6e812c3'fde4523b'51b6d251_ui128); |
| 234 | + |
| 235 | + // subtraction |
| 236 | + assert(uvalue - 1 == 0xf473e8e5'f6e812c3'fde4523b'51b6d250_ui128); |
| 237 | + assert(svalue - 1 == -0xf473e8e5'f6e812c3'fde4523b'51b6d252_ui128); |
| 238 | + |
| 239 | + |
| 240 | + |
| 241 | + // multiplication |
| 242 | + assert(uvalue * 1 == 0xf473e8e5'f6e812c3'fde4523b'51b6d251_ui128); |
| 243 | + assert(svalue * 1 == -0xf473e8e5'f6e812c3'fde4523b'51b6d251_ui128); |
| 244 | + |
| 245 | + |
| 246 | + |
| 247 | + // division |
| 248 | + assert(uvalue / 1 == 0xf473e8e5'f6e812c3'fde4523b'51b6d251_ui128); |
| 249 | + // uvalue / 0; // fail! will be interrupted as standard integer division by zero |
| 250 | + assert(svalue / 1 == -0xf473e8e5'f6e812c3'fde4523b'51b6d251_ui128); |
| 251 | + // svalue / 0; // fail! will be interrupted as standard integer division by zero |
| 252 | + |
| 253 | + |
| 254 | + |
| 255 | + // modulo remainder |
| 256 | + assert(uvalue % 1 == 0); |
| 257 | + // uvalue % 0; // fail! will be interrupted as standard integer division by zero |
| 258 | + assert(svalue % 1 == 0); |
| 259 | + // svalue % 0; // fail! will be interrupted as standard integer division by zero |
| 260 | +} |
| 261 | + |
| 262 | + |
| 263 | + |
| 264 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 265 | +static void muldiv_operation() noexcept |
| 266 | +{ |
| 267 | + constexpr auto uvalue = 0xf473e8e5'f6e812c3'fde4523b'51b6d251_ui128; |
| 268 | + constexpr auto svalue = -0xf473e8e5'f6e812c3'fde4523b'51b6d251_si128; |
| 269 | + |
| 270 | + // multiplication then division operation |
| 271 | + |
| 272 | + assert(muldiv(uvalue, uvalue, uvalue) == 0xf473e8e5'f6e812c3'fde4523b'51b6d251_ui128); |
| 273 | + assert(muldiv(svalue, svalue, svalue) == -0xf473e8e5'f6e812c3'fde4523b'51b6d251_ui128); |
| 274 | +} |
| 275 | + |
| 276 | + |
| 277 | + |
| 278 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 279 | +// module main entry point |
| 280 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 281 | + |
| 282 | +int main() |
| 283 | +{ |
| 284 | + initalization_and_conversion(); |
| 285 | + comparison(); |
| 286 | + bitwise_oprations(); |
| 287 | + bit_shift_operations(); |
| 288 | + arithmetic_operations(); |
| 289 | + muldiv_operation(); |
| 290 | + |
| 291 | + return 1; |
| 292 | +} |
| 293 | + |
| 294 | + |
| 295 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 296 | +// End of main.cpp |
| 297 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
0 commit comments