Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 BOTAN_FUTURE_INTERNAL_HEADER(rotate.h)
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" : "+r" (input) : "c" (static_cast<uint8_t>(rot)));
73 return input;
74 }
75
76 template<>
77 inline uint32_t rotr_var(uint32_t input, size_t rot)
78 {
79 asm("rorl %1,%0" : "+r" (input) : "c" (static_cast<uint8_t>(rot)));
80 return input;
81 }
82
83 #endif
84
85 #endif
86
87
88 template<typename T>
89 BOTAN_DEPRECATED("Use rotl<N> or rotl_var")
90 inline T rotate_left(T input, size_t rot)
91 {
92 // rotl_var does not reduce
93 return rotl_var(input, rot % (8 * sizeof(T)));
94 }
95
96 template<typename T>
97 BOTAN_DEPRECATED("Use rotr<N> or rotr_var")
98 inline T rotate_right(T input, size_t rot)
99 {
100 // rotr_var does not reduce
101 return rotr_var(input, rot % (8 * sizeof(T)));
102 }
103
104 }
105
106 #endif
Botan::rotr_var
T rotr_var(T input, size_t rot)
Definition: rotate.h:60
Botan::rotr
constexpr T rotr(T input)
Definition: rotate.h:35
Botan::rotate_left
T rotate_left(T input, size_t rot)
Definition: rotate.h:90
Botan::rotl_var
T rotl_var(T input, size_t rot)
Definition: rotate.h:48
Botan::rotate_right
T rotate_right(T input, size_t rot)
Definition: rotate.h:98
Botan
Definition: alg_id.cpp:13
T
fe T
Definition: ge.cpp:37
BOTAN_FUTURE_INTERNAL_HEADER
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition: compiler.h:139
Botan::rotl
constexpr T rotl(T input)
Definition: rotate.h:23