2
* Word Rotation Operations
3
* (C) 1999-2008,2017 Jack Lloyd
5
* Botan is released under the Simplified BSD License (see license.txt)
8
#ifndef BOTAN_WORD_ROTATE_H_
9
#define BOTAN_WORD_ROTATE_H_
11
#include <botan/types.h>
16
* Bit rotation left by a compile-time constant amount
17
* @param input the input word
18
* @return input rotated left by ROT bits
20
template<size_t ROT, typename T>
21
inline T rotl(T input)
23
static_assert(ROT > 0 && ROT < 8*sizeof(T), "Invalid rotation constant");
24
return static_cast<T>((input << ROT) | (input >> (8*sizeof(T) - ROT)));
28
* Bit rotation right by a compile-time constant amount
29
* @param input the input word
30
* @return input rotated right by ROT bits
32
template<size_t ROT, typename T>
33
inline T rotr(T input)
35
static_assert(ROT > 0 && ROT < 8*sizeof(T), "Invalid rotation constant");
36
return static_cast<T>((input >> ROT) | (input << (8*sizeof(T) - ROT)));
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
46
inline T rotl_var(T input, size_t rot)
48
return rot ? static_cast<T>((input << rot) | (input >> (sizeof(T)*8 - rot))) : input;
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
58
inline T rotr_var(T input, size_t rot)
60
return rot ? static_cast<T>((input >> rot) | (input << (sizeof(T)*8 - rot))) : input;
63
#if BOTAN_USE_GCC_INLINE_ASM
65
#if defined(BOTAN_TARGET_ARCH_IS_X86_64) || defined(BOTAN_TARGET_ARCH_IS_X86_32)
68
inline uint32_t rotl_var(uint32_t input, size_t rot)
70
asm("roll %1,%0" : "+r" (input) : "c" (static_cast<uint8_t>(rot)));
75
inline uint32_t rotr_var(uint32_t input, size_t rot)
77
asm("rorl %1,%0" : "+r" (input) : "c" (static_cast<uint8_t>(rot)));
87
BOTAN_DEPRECATED("Use rotl<N> or rotl_var")
88
inline T rotate_left(T input, size_t rot)
90
// rotl_var does not reduce
91
return rotl_var(input, rot % (8 * sizeof(T)));
95
BOTAN_DEPRECATED("Use rotr<N> or rotr_var")
96
inline T rotate_right(T input, size_t rot)
98
// rotr_var does not reduce
99
return rotr_var(input, rot % (8 * sizeof(T)));