Botan  2.19.1
Crypto and TLS for C++11
rotate.h
Go to the documentation of this file.
1 /*
2 * Word Rotation Operations
3 * (C) 1999-2008,2017 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_WORD_ROTATE_H_
9 #define BOTAN_WORD_ROTATE_H_
10 
11 #include <botan/types.h>
12 
14 
15 namespace Botan {
16 
17 /**
18 * Bit rotation left by a compile-time constant amount
19 * @param input the input word
20 * @return input rotated left by ROT bits
21 */
22 template<size_t ROT, typename T>
23 inline constexpr T rotl(T input)
24  {
25  static_assert(ROT > 0 && ROT < 8*sizeof(T), "Invalid rotation constant");
26  return static_cast<T>((input << ROT) | (input >> (8*sizeof(T) - ROT)));
27  }
28 
29 /**
30 * Bit rotation right by a compile-time constant amount
31 * @param input the input word
32 * @return input rotated right by ROT bits
33 */
34 template<size_t ROT, typename T>
35 inline constexpr T rotr(T input)
36  {
37  static_assert(ROT > 0 && ROT < 8*sizeof(T), "Invalid rotation constant");
38  return static_cast<T>((input >> ROT) | (input << (8*sizeof(T) - ROT)));
39  }
40 
41 /**
42 * Bit rotation left, variable rotation amount
43 * @param input the input word
44 * @param rot the number of bits to rotate, must be between 0 and sizeof(T)*8-1
45 * @return input rotated left by rot bits
46 */
47 template<typename T>
48 inline T rotl_var(T input, size_t rot)
49  {
50  return rot ? static_cast<T>((input << rot) | (input >> (sizeof(T)*8 - rot))) : input;
51  }
52 
53 /**
54 * Bit rotation right, variable rotation amount
55 * @param input the input word
56 * @param rot the number of bits to rotate, must be between 0 and sizeof(T)*8-1
57 * @return input rotated right by rot bits
58 */
59 template<typename T>
60 inline T rotr_var(T input, size_t rot)
61  {
62  return rot ? static_cast<T>((input >> rot) | (input << (sizeof(T)*8 - rot))) : input;
63  }
64 
65 #if defined(BOTAN_USE_GCC_INLINE_ASM)
66 
67 #if defined(BOTAN_TARGET_ARCH_IS_X86_64) || defined(BOTAN_TARGET_ARCH_IS_X86_32)
68 
69 template<>
70 inline uint32_t rotl_var(uint32_t input, size_t rot)
71  {
72  asm("roll %1,%0"
73  : "+r" (input)
74  : "c" (static_cast<uint8_t>(rot))
75  : "cc");
76  return input;
77  }
78 
79 template<>
80 inline uint32_t rotr_var(uint32_t input, size_t rot)
81  {
82  asm("rorl %1,%0"
83  : "+r" (input)
84  : "c" (static_cast<uint8_t>(rot))
85  : "cc");
86  return input;
87  }
88 
89 #endif
90 
91 #endif
92 
93 
94 template<typename T>
95 BOTAN_DEPRECATED("Use rotl<N> or rotl_var")
96 inline T rotate_left(T input, size_t rot)
97  {
98  // rotl_var does not reduce
99  return rotl_var(input, rot % (8 * sizeof(T)));
100  }
101 
102 template<typename T>
103 BOTAN_DEPRECATED("Use rotr<N> or rotr_var")
104 inline T rotate_right(T input, size_t rot)
105  {
106  // rotr_var does not reduce
107  return rotr_var(input, rot % (8 * sizeof(T)));
108  }
109 
110 }
111 
112 #endif
T rotr_var(T input, size_t rot)
Definition: rotate.h:60
constexpr T rotr(T input)
Definition: rotate.h:35
T rotate_left(T input, size_t rot)
Definition: rotate.h:96
T rotl_var(T input, size_t rot)
Definition: rotate.h:48
T rotate_right(T input, size_t rot)
Definition: rotate.h:104
Definition: alg_id.cpp:13
fe T
Definition: ge.cpp:37
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition: compiler.h:136
constexpr T rotl(T input)
Definition: rotate.h:23