Botan 2.17.3
Crypto and TLS for C&
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
15namespace 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*/
22template<size_t ROT, typename T>
23inline 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*/
34template<size_t ROT, typename T>
35inline 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*/
47template<typename T>
48inline 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*/
59template<typename T>
60inline 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
69template<>
70inline 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
76template<>
77inline 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
88template<typename T>
89BOTAN_DEPRECATED("Use rotl<N> or rotl_var")
90inline 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
96template<typename T>
97BOTAN_DEPRECATED("Use rotr<N> or rotr_var")
98inline 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
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition: compiler.h:136
fe T
Definition: ge.cpp:37
Definition: alg_id.cpp:13
constexpr T rotl(T input)
Definition: rotate.h:23
T rotl_var(T input, size_t rot)
Definition: rotate.h:48
T rotate_right(T input, size_t rot)
Definition: rotate.h:98
T rotate_left(T input, size_t rot)
Definition: rotate.h:90
constexpr T rotr(T input)
Definition: rotate.h:35
T rotr_var(T input, size_t rot)
Definition: rotate.h:60