// // 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_UTILITY_HPP #define CT_UTILITY_HPP #include #include #include #include namespace cbn { namespace detail { template constexpr auto tight_length(big_int num) { // count the effective number of limbs // (ignoring zero-limbs at the most-significant-limb side) size_t L = N; while (L > 0 && num[L - 1] == 0) --L; return L; } template constexpr auto tight_length(std::integer_sequence) { // count the effective number of limbs // (ignoring zero-limbs at the most-significant-limb side) size_t L = sizeof...(Is); std::array num{Is...}; while (L > 0 && num[L - 1] == 0) --L; return L; } template constexpr auto bit_length(big_int num) { auto L = tight_length(num); size_t bitlen = L * std::numeric_limits::digits; T msb = num[L - 1]; while (bitlen > 0 && (msb & (static_cast(1) << (std::numeric_limits::digits - 1))) == 0) { msb <<= 1; --bitlen; } return bitlen; } template constexpr void assign(big_int& dst, big_int src) { // assignment for the scenario where N1 >= N2 static_assert(N1 >= N2, "cannot assign: destination has smaller size than source"); for (auto i = 0; i < N1; ++i) dst[i] = src[i]; for (auto i = N1; i < N2; ++i) dst[i] = 0; } } // end of detail namespace template constexpr auto to_big_int(std::integer_sequence) { return big_int{ Limbs... }; } } // end of cbn namespace #endif