algebra/include/nil/crypto3/detail/unbounded_shift.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2021 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020-2021 Alexander Sokolov <asokolov@nil.foundation>
4 // Copyright (c) 2020-2021 Nikita Kaskov <nbering@nil.foundation>
5 //
6 // MIT License
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 //---------------------------------------------------------------------------//
26 
27 #ifndef CRYPTO3_DETAIL_UNBOUNDED_SHIFT_HPP
28 #define CRYPTO3_DETAIL_UNBOUNDED_SHIFT_HPP
29 
30 #include <boost/assert.hpp>
31 
32 namespace nil {
33  namespace crypto3 {
34  namespace detail {
35 
36  template<int Shift, typename T>
38  static T shl(T x) {
39  return unbounded_shifter<Shift - 1, T>::shl(T(x << 1));
40  }
41 
42  static T shr(T x) {
43  return unbounded_shifter<Shift - 1, T>::shr(T(x >> 1));
44  }
45  };
46 
47  template<typename T>
48  struct unbounded_shifter<0, T> {
49  static T shl(T x) {
50  return x;
51  }
52 
53  static T shr(T x) {
54  return x;
55  }
56  };
57 
58  template<int Shift, typename T>
59  T unbounded_shl(T x) {
61  }
62 
63  template<int Shift, typename T>
64  T unbounded_shr(T x) {
66  }
67 
68  template<typename T>
69  T unbounded_shl(T x, std::size_t n) {
70  return x << n;
71  }
72 
73  template<typename T>
74  T unbounded_shr(T x, std::size_t n) {
75  return x >> n;
76  }
77  // FIXME: it wouldn't work when Shift == sizeof(T) * CHAR_BIT
78  template<int Shift, typename T>
79  T low_bits(T x) {
80  T highmask = unbounded_shl<Shift, T>(~T());
81  return T(x & ~highmask);
82  }
83 
84  template<size_t Shift, std::size_t TypeBits, typename T>
85  T low_bits(T x) {
86  constexpr std::size_t real_shift = TypeBits - Shift;
87  T lowmask = ((bool)Shift) * unbounded_shr<real_shift, T>(~T());
88  return x & lowmask;
89  }
90 
91  template<size_t type_bits, typename T>
92  T low_bits(T x, std::size_t shift) {
93  T lowmask = ((bool)shift) * unbounded_shr<T>(~T(), type_bits - shift);
94  return x & lowmask;
95  }
96 
97  template<size_t type_bits, typename T>
98  T high_bits(T x, std::size_t shift) {
99  T highmask = ((bool)shift) * unbounded_shl<T>(~T(), type_bits - shift);
100  return x & highmask;
101  }
102  } // namespace detail
103  } // namespace crypto3
104 } // namespace nil
105 
106 #endif // CRYPTO3_DETAIL_UNBOUNDED_SHIFT_HPP
T unbounded_shr(T x)
Definition: algebra/include/nil/crypto3/detail/unbounded_shift.hpp:64
T high_bits(T x, std::size_t shift)
Definition: algebra/include/nil/crypto3/detail/unbounded_shift.hpp:98
T low_bits(T x)
Definition: algebra/include/nil/crypto3/detail/unbounded_shift.hpp:79
T unbounded_shl(T x)
Definition: algebra/include/nil/crypto3/detail/unbounded_shift.hpp:59
Definition: pair.hpp:31
static T shl(T x)
Definition: algebra/include/nil/crypto3/detail/unbounded_shift.hpp:49
static T shr(T x)
Definition: algebra/include/nil/crypto3/detail/unbounded_shift.hpp:53
Definition: algebra/include/nil/crypto3/detail/unbounded_shift.hpp:37
static T shr(T x)
Definition: algebra/include/nil/crypto3/detail/unbounded_shift.hpp:42
static T shl(T x)
Definition: algebra/include/nil/crypto3/detail/unbounded_shift.hpp:38