// // 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_SLICING_HPP #define CT_SLICING_HPP #include #include namespace cbn { namespace detail { template constexpr auto take_first(std::integer_sequence, std::index_sequence) { constexpr big_int num = {Limbs...}; return std::integer_sequence{}; } template constexpr auto take(big_int< N1, T> t) { //static_assert(End >= Begin, "invalid range"); //static_assert(End - Begin <= N1, "invalid range"); big_int< End - Begin + Padding, T> res{}; for (auto i = Begin; i < End; ++i) { res[i-Begin] = t[i]; } return res; } template constexpr auto take(big_int< N1, T> t, const size_t Begin, const size_t End, const size_t Offset = 0) { big_int< ResultLength, T> res{}; for (auto i = Begin; i < End; ++i) { res[i-Begin+Offset] = t[i]; } return res; } template constexpr auto skip(big_int t) { // skip first N limbs // skip(x) corresponds with right-shifting x by N limbs return take(t); } template constexpr auto skip(big_int t, size_t N) { // skip first N limbs, runtime version // skip(x) corresponds with right-shifting x by N limbs return take(t, N, N1); } template constexpr auto first(big_int t) { // take first N limbs // first(x) corresponds with x modulo (2^64)^N return take<0, N>(t); } template constexpr auto pad(big_int t) { // add N extra limbs (at msb side) return take<0, N1, N>(t); } template constexpr auto to_length(big_int t) { return (N1 < N) ? pad(t) : first(t); } template constexpr auto join(big_int< N1, T> a, big_int< N2, T> b) { big_int< N1+N2, T> result {}; for (auto i = 0; i constexpr auto limbwise_shift_left(big_int< N1, T> t, const size_t k) { // shift left by k limbs (and produce output of limb-length ResultLength) return take(t, 0, N1, k); } template constexpr auto unary_encoding() { // N limbs, Kth limb set to one big_int res{}; res[K] = 1; return res; } template constexpr auto unary_encoding(size_t K) { big_int res{}; res[K] = 1; return res; } template constexpr auto place_at(uint64_t value, size_t K) { // N limbs, Kth limb set to value big_int res{}; res[K] = value; return res; } } } #endif