Botan  2.4.0
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 
13 namespace Botan {
14 
15 /**
16 * Bit rotation left by a compile-time constant amount
17 * @param input the input word
18 * @return input rotated left by ROT bits
19 */
20 template<size_t ROT, typename T>
21 inline T rotl(T input)
22  {
23  static_assert(ROT > 0 && ROT < 8*sizeof(T), "Invalid rotation constant");
24  return static_cast<T>((input << ROT) | (input >> (8*sizeof(T) - ROT)));
25  }
26 
27 /**
28 * Bit rotation right by a compile-time constant amount
29 * @param input the input word
30 * @return input rotated right by ROT bits
31 */
32 template<size_t ROT, typename T>
33 inline T rotr(T input)
34  {
35  static_assert(ROT > 0 && ROT < 8*sizeof(T), "Invalid rotation constant");
36  return static_cast<T>((input >> ROT) | (input << (8*sizeof(T) - ROT)));
37  }
38 
39 /**
40 * Bit rotation left, variable rotation amount
41 * @param input the input word
42 * @param rot the number of bits to rotate, must be between 0 and sizeof(T)*8-1
43 * @return input rotated left by rot bits
44 */
45 template<typename T>
46 inline T rotl_var(T input, size_t rot)
47  {
48  return rot ? static_cast<T>((input << rot) | (input >> (sizeof(T)*8 - rot))) : input;
49  }
50 
51 /**
52 * Bit rotation right, variable rotation amount
53 * @param input the input word
54 * @param rot the number of bits to rotate, must be between 0 and sizeof(T)*8-1
55 * @return input rotated right by rot bits
56 */
57 template<typename T>
58 inline T rotr_var(T input, size_t rot)
59  {
60  return rot ? static_cast<T>((input >> rot) | (input << (sizeof(T)*8 - rot))) : input;
61  }
62 
63 #if BOTAN_USE_GCC_INLINE_ASM
64 
65 #if defined(BOTAN_TARGET_ARCH_IS_X86_64) || defined(BOTAN_TARGET_ARCH_IS_X86_32)
66 
67 template<>
68 inline uint32_t rotl_var(uint32_t input, size_t rot)
69  {
70  asm("roll %1,%0" : "+r" (input) : "c" (static_cast<uint8_t>(rot)));
71  return input;
72  }
73 
74 template<>
75 inline uint32_t rotr_var(uint32_t input, size_t rot)
76  {
77  asm("rorl %1,%0" : "+r" (input) : "c" (static_cast<uint8_t>(rot)));
78  return input;
79  }
80 
81 #endif
82 
83 #endif
84 
85 
86 template<typename T>
87 BOTAN_DEPRECATED("Use rotl<N> or rotl_var")
88 inline T rotate_left(T input, size_t rot)
89  {
90  // rotl_var does not reduce
91  return rotl_var(input, rot % (8 * sizeof(T)));
92  }
93 
94 template<typename T>
95 BOTAN_DEPRECATED("Use rotr<N> or rotr_var")
96 inline T rotate_right(T input, size_t rot)
97  {
98  // rotr_var does not reduce
99  return rotr_var(input, rot % (8 * sizeof(T)));
100  }
101 
102 }
103 
104 #endif
T rotr_var(T input, size_t rot)
Definition: rotate.h:58
T rotr(T input)
Definition: rotate.h:33
T rotate_left(T input, size_t rot)
Definition: rotate.h:88
T rotl_var(T input, size_t rot)
Definition: rotate.h:46
T rotate_right(T input, size_t rot)
Definition: rotate.h:96
T rotl(T input)
Definition: rotate.h:21
Definition: alg_id.cpp:13
fe T
Definition: ge.cpp:37