aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/bswap.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/bswap.h')
-rw-r--r--src/utils/bswap.h33
1 files changed, 21 insertions, 12 deletions
diff --git a/src/utils/bswap.h b/src/utils/bswap.h
index 3294111a0..1a5349fd0 100644
--- a/src/utils/bswap.h
+++ b/src/utils/bswap.h
@@ -10,7 +10,7 @@
#define BOTAN_BYTE_SWAP_H__
#include <botan/types.h>
-#include <botan/internal/rotate.h>
+#include <botan/rotate.h>
namespace Botan {
@@ -24,35 +24,44 @@ inline u16bit reverse_bytes(u16bit input)
inline u32bit reverse_bytes(u32bit input)
{
-#if BOTAN_USE_GCC_INLINE_ASM && \
- (defined(BOTAN_TARGET_ARCH_IS_IA32) || defined(BOTAN_TARGET_ARCH_IS_AMD64))
+#if BOTAN_USE_GCC_INLINE_ASM && (defined(BOTAN_TARGET_ARCH_IS_IA32) || \
+ defined(BOTAN_TARGET_ARCH_IS_AMD64))
- /* GCC-style inline assembly for x86 or x86-64 */
+ // GCC-style inline assembly for x86 or x86-64
asm("bswapl %0" : "=r" (input) : "0" (input));
return input;
#elif defined(_MSC_VER) && defined(BOTAN_TARGET_ARCH_IS_IA32)
- /* Visual C++ inline asm for 32-bit x86, by Yves Jerschow */
+ // Visual C++ inline asm for 32-bit x86, by Yves Jerschow
__asm mov eax, input;
__asm bswap eax;
#else
- /* Generic implementation */
- input = ((input & 0xFF00FF00) >> 8) | ((input & 0x00FF00FF) << 8);
- return rotate_left(input, 16);
+ // Generic implementation
+ return (rotate_right(input, 8) & 0xFF00FF00) |
+ (rotate_left (input, 8) & 0x00FF00FF);
#endif
}
inline u64bit reverse_bytes(u64bit input)
{
#if BOTAN_USE_GCC_INLINE_ASM && defined(BOTAN_TARGET_ARCH_IS_AMD64)
+ // GCC-style inline assembly for x86-64
asm("bswapq %0" : "=r" (input) : "0" (input));
return input;
+
#else
- u32bit hi = ((input >> 40) & 0x00FF00FF) | ((input >> 24) & 0xFF00FF00);
- u32bit lo = ((input & 0xFF00FF00) >> 8) | ((input & 0x00FF00FF) << 8);
- hi = (hi << 16) | (hi >> 16);
- lo = (lo << 16) | (lo >> 16);
+ /* Generic implementation. Defined in terms of 32-bit bswap so any
+ * optimizations in that version can help here (particularly
+ * useful for 32-bit x86).
+ */
+
+ u32bit hi = static_cast<u32bit>(input >> 32);
+ u32bit lo = static_cast<u32bit>(input);
+
+ hi = reverse_bytes(hi);
+ lo = reverse_bytes(lo);
+
return (static_cast<u64bit>(lo) << 32) | hi;
#endif
}