/* * Word Rotation Operations * (C) 1999-2008,2017 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ #ifndef BOTAN_WORD_ROTATE_H_ #define BOTAN_WORD_ROTATE_H_ #include BOTAN_FUTURE_INTERNAL_HEADER(rotate.h) namespace Botan { /** * Bit rotation left by a compile-time constant amount * @param input the input word * @return input rotated left by ROT bits */ template inline constexpr T rotl(T input) { static_assert(ROT > 0 && ROT < 8*sizeof(T), "Invalid rotation constant"); return static_cast((input << ROT) | (input >> (8*sizeof(T) - ROT))); } /** * Bit rotation right by a compile-time constant amount * @param input the input word * @return input rotated right by ROT bits */ template inline constexpr T rotr(T input) { static_assert(ROT > 0 && ROT < 8*sizeof(T), "Invalid rotation constant"); return static_cast((input >> ROT) | (input << (8*sizeof(T) - ROT))); } /** * Bit rotation left, variable rotation amount * @param input the input word * @param rot the number of bits to rotate, must be between 0 and sizeof(T)*8-1 * @return input rotated left by rot bits */ template inline T rotl_var(T input, size_t rot) { return rot ? static_cast((input << rot) | (input >> (sizeof(T)*8 - rot))) : input; } /** * Bit rotation right, variable rotation amount * @param input the input word * @param rot the number of bits to rotate, must be between 0 and sizeof(T)*8-1 * @return input rotated right by rot bits */ template inline T rotr_var(T input, size_t rot) { return rot ? static_cast((input >> rot) | (input << (sizeof(T)*8 - rot))) : input; } #if defined(BOTAN_USE_GCC_INLINE_ASM) #if defined(BOTAN_TARGET_ARCH_IS_X86_64) || defined(BOTAN_TARGET_ARCH_IS_X86_32) template<> inline uint32_t rotl_var(uint32_t input, size_t rot) { asm("roll %1,%0" : "+r" (input) : "c" (static_cast(rot)) : "cc"); return input; } template<> inline uint32_t rotr_var(uint32_t input, size_t rot) { asm("rorl %1,%0" : "+r" (input) : "c" (static_cast(rot)) : "cc"); return input; } #endif #endif template BOTAN_DEPRECATED("Use rotl or rotl_var") inline T rotate_left(T input, size_t rot) { // rotl_var does not reduce return rotl_var(input, rot % (8 * sizeof(T))); } template BOTAN_DEPRECATED("Use rotr or rotr_var") inline T rotate_right(T input, size_t rot) { // rotr_var does not reduce return rotr_var(input, rot % (8 * sizeof(T))); } } #endif