From 175f09ffd806f2f19cd509017a67ae1384f29ae1 Mon Sep 17 00:00:00 2001
From: Jack Lloyd <jack@randombit.net>
Date: Wed, 11 Oct 2017 17:02:20 -0400
Subject: Add compile-time rotation functions

The problem with asm rol/ror is the compiler can't schedule effectively.
But we only need asm in the case when the rotation is variable, so distinguish
the two cases. If a compile time constant, then static_assert that the rotation
is in the correct range and do the straightforward expression knowing the compiler
will probably do the right thing. Otherwise do a tricky expression that both
GCC and Clang happen to have recognize. Avoid the reduction case; instead
require that the rotation be in range (this reverts 2b37c13dcf).

Remove the asm rotations (making this branch illnamed), because now both Clang
and GCC will create a roll without any extra help.

Remove the reduction/mask by the word size for the variable case. The compiler
can't optimize that it out well, but it's easy to ensure it is valid in the callers,
especially now that the variable input cases are easy to grep for.
---
 src/lib/utils/bswap.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

(limited to 'src/lib/utils/bswap.h')

diff --git a/src/lib/utils/bswap.h b/src/lib/utils/bswap.h
index 23b3113ce..c1aa8b594 100644
--- a/src/lib/utils/bswap.h
+++ b/src/lib/utils/bswap.h
@@ -19,7 +19,7 @@ namespace Botan {
 */
 inline uint16_t reverse_bytes(uint16_t val)
    {
-   return rotate_left(val, 8);
+   return rotl<8>(val);
    }
 
 /**
@@ -64,8 +64,8 @@ inline uint32_t reverse_bytes(uint32_t val)
 #else
 
    // Generic implementation
-   return (rotate_right(val, 8) & 0xFF00FF00) |
-          (rotate_left (val, 8) & 0x00FF00FF);
+   return (rotr<8>(val) & 0xFF00FF00) |
+          (rotl<8>(val) & 0x00FF00FF);
 
 #endif
    }
-- 
cgit v1.2.3