// // This file is part of // // CTBignum // // C++ Library for Compile-Time and Run-Time Multi-Precision and Modular Arithmetic // // // This file is distributed under the Apache License, Version 2.0. See the LICENSE // file for details. #ifndef CT_MULT_HPP #define CT_MULT_HPP #include #include #include #include #include #include #include namespace cbn { template CBN_ALWAYS_INLINE constexpr auto short_mul(big_int a, T b) { using TT = typename dbl_bitlen::type; big_int p{}; T k = 0; for (auto j = 0; j < N; ++j) { TT t = static_cast(a[j]) * static_cast(b) + k; p[j] = t; k = t >> std::numeric_limits::digits; } p[N] = k; return p; } template CBN_ALWAYS_INLINE constexpr auto mul(big_int u, big_int v) { using TT = typename dbl_bitlen::type; big_int w{}; for (auto j = 0; j < N; ++j) { // if (v[j] == 0) // w[j + M] = static_cast(0); // else { T k = 0; for (auto i = 0; i < M; ++i) { TT t = static_cast(u[i]) * static_cast(v[j]) + w[i + j] + k; w[i + j] = static_cast(t); k = t >> std::numeric_limits::digits; } w[j + M] = k; //} } return w; } template constexpr auto partial_mul(big_int u, big_int v) { using TT = typename dbl_bitlen::type; big_int w{}; for (auto j = 0; j < N; ++j) { // if (v[j] == 0) { // if (j + M < ResultLength) // w[j + M] = static_cast(0); //} else { T k = 0; const auto m = std::min(M, ResultLength - j); for (auto i = 0; i < m; ++i) { TT t = static_cast(u[i]) * static_cast(v[j]) + w[i + j] + k; w[i + j] = static_cast(t); k = t >> std::numeric_limits::digits; } if (j + M < ResultLength) w[j + M] = k; //} } return w; } template constexpr auto operator*(big_int a, big_int b) { return mul(a, b); } } #endif