block/include/nil/crypto3/detail/unbounded_shift.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020 Alexander Sokolov <asokolov@nil.foundation>
4 // Copyright (c) 2020 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 #include <nil/crypto3/detail/stream_endian.hpp>
33 
34 namespace nil {
35  namespace crypto3 {
36  namespace detail {
37 
38  template<int Shift, typename T>
39  struct unbounded_shifter {
40  static T shl(T x) {
41  return unbounded_shifter<Shift - 1, T>::shl(T(x << 1));
42  }
43 
44  static T shr(T x) {
45  return unbounded_shifter<Shift - 1, T>::shr(T(x >> 1));
46  }
47  };
48 
49  template<typename T>
50  struct unbounded_shifter<0, T> {
51  static T shl(T x) {
52  return x;
53  }
54 
55  static T shr(T x) {
56  return x;
57  }
58  };
59 
60  template<int Shift, typename T>
61  T unbounded_shl(T x) {
63  }
64 
65  template<int Shift, typename T>
66  T unbounded_shr(T x) {
68  }
69 
70  template<typename T>
71  T unbounded_shl(T x, std::size_t n) {
72  return x << n;
73  }
74 
75  template<typename T>
76  T unbounded_shr(T x, std::size_t n) {
77  return x >> n;
78  }
79  // FIXME: it wouldn't work when Shift == sizeof(T) * CHAR_BIT
80  template<int Shift, typename T>
81  T low_bits(T x) {
82  T highmask = unbounded_shl<Shift, T>(~T());
83  return T(x & ~highmask);
84  }
85 
86  template<size_t Shift, size_t TypeBits, typename T>
87  T low_bits(T x) {
88  constexpr size_t real_shift = TypeBits - Shift;
89  T lowmask = ((bool)Shift) * unbounded_shr<real_shift, T>(~T());
90  return x & lowmask;
91  }
92 
93  template<size_t type_bits, typename T>
94  T low_bits(T x, size_t shift) {
95  T lowmask = ((bool)shift) * unbounded_shr<T>(~T(), type_bits - shift);
96  return x & lowmask;
97  }
98 
99  template<size_t type_bits, typename T>
100  T high_bits(T x, size_t shift) {
101  T highmask = ((bool)shift) * unbounded_shl<T>(~T(), type_bits - shift);
102  return x & highmask;
103  }
104  } // namespace detail
105  } // namespace crypto3
106 } // namespace nil
107 
108 #endif // CRYPTO3_HASH_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: block/include/nil/crypto3/detail/unbounded_shift.hpp:51
static T shr(T x)
Definition: block/include/nil/crypto3/detail/unbounded_shift.hpp:55
static T shr(T x)
Definition: block/include/nil/crypto3/detail/unbounded_shift.hpp:44
static T shl(T x)
Definition: block/include/nil/crypto3/detail/unbounded_shift.hpp:40