Botan 1.10.17
bswap.h
Go to the documentation of this file.
1/*
2* Byte Swapping Operations
3* (C) 1999-2011 Jack Lloyd
4* (C) 2007 Yves Jerschow
5*
6* Distributed under the terms of the Botan license
7*/
8
9#ifndef BOTAN_BYTE_SWAP_H__
10#define BOTAN_BYTE_SWAP_H__
11
12#include <botan/types.h>
13#include <botan/rotate.h>
14
15#if defined(BOTAN_TARGET_CPU_HAS_SSE2) && !defined(BOTAN_NO_SSE_INTRINSICS)
16 #include <emmintrin.h>
17#endif
18
19namespace Botan {
20
21/**
22* Swap a 16 bit integer
23*/
25 {
26 return rotate_left(val, 8);
27 }
28
29/**
30* Swap a 32 bit integer
31*/
33 {
34#if BOTAN_GCC_VERSION >= 430 && !defined(BOTAN_TARGET_CPU_IS_ARM_FAMILY)
35 /*
36 GCC intrinsic added in 4.3, works for a number of CPUs
37
38 However avoid under ARM, as it branches to a function in libgcc
39 instead of generating inline asm, so slower even than the generic
40 rotate version below.
41 */
42 return __builtin_bswap32(val);
43
44#elif BOTAN_USE_GCC_INLINE_ASM && defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
45
46 // GCC-style inline assembly for x86 or x86-64
47 asm("bswapl %0" : "=r" (val) : "0" (val));
48 return val;
49
50#elif BOTAN_USE_GCC_INLINE_ASM && defined(BOTAN_TARGET_CPU_IS_ARM_FAMILY)
51
52 asm ("eor r3, %1, %1, ror #16\n\t"
53 "bic r3, r3, #0x00FF0000\n\t"
54 "mov %0, %1, ror #8\n\t"
55 "eor %0, %0, r3, lsr #8"
56 : "=r" (val)
57 : "0" (val)
58 : "r3", "cc");
59
60 return val;
61
62#elif defined(_MSC_VER) && defined(BOTAN_TARGET_ARCH_IS_X86_32)
63
64 // Visual C++ inline asm for 32-bit x86, by Yves Jerschow
65 __asm mov eax, val;
66 __asm bswap eax;
67
68#else
69
70 // Generic implementation
71 return (rotate_right(val, 8) & 0xFF00FF00) |
72 (rotate_left (val, 8) & 0x00FF00FF);
73
74#endif
75 }
76
77/**
78* Swap a 64 bit integer
79*/
81 {
82#if BOTAN_GCC_VERSION >= 430
83
84 // GCC intrinsic added in 4.3, works for a number of CPUs
85 return __builtin_bswap64(val);
86
87#elif BOTAN_USE_GCC_INLINE_ASM && defined(BOTAN_TARGET_ARCH_IS_X86_64)
88 // GCC-style inline assembly for x86-64
89 asm("bswapq %0" : "=r" (val) : "0" (val));
90 return val;
91
92#else
93 /* Generic implementation. Defined in terms of 32-bit bswap so any
94 * optimizations in that version can help here (particularly
95 * useful for 32-bit x86).
96 */
97
98 u32bit hi = static_cast<u32bit>(val >> 32);
99 u32bit lo = static_cast<u32bit>(val);
100
101 hi = reverse_bytes(hi);
102 lo = reverse_bytes(lo);
103
104 return (static_cast<u64bit>(lo) << 32) | hi;
105#endif
106 }
107
108/**
109* Swap 4 Ts in an array
110*/
111template<typename T>
112inline void bswap_4(T x[4])
113 {
114 x[0] = reverse_bytes(x[0]);
115 x[1] = reverse_bytes(x[1]);
116 x[2] = reverse_bytes(x[2]);
117 x[3] = reverse_bytes(x[3]);
118 }
119
120#if defined(BOTAN_TARGET_CPU_HAS_SSE2) && !defined(BOTAN_NO_SSE_INTRINSICS)
121
122/**
123* Swap 4 u32bits in an array using SSE2 shuffle instructions
124*/
125template<>
126inline void bswap_4(u32bit x[4])
127 {
128 __m128i T = _mm_loadu_si128(reinterpret_cast<const __m128i*>(x));
129
130 T = _mm_shufflehi_epi16(T, _MM_SHUFFLE(2, 3, 0, 1));
131 T = _mm_shufflelo_epi16(T, _MM_SHUFFLE(2, 3, 0, 1));
132
133 T = _mm_or_si128(_mm_srli_epi16(T, 8), _mm_slli_epi16(T, 8));
134
135 _mm_storeu_si128(reinterpret_cast<__m128i*>(x), T);
136 }
137
138#endif
139
140}
141
142#endif
u16bit reverse_bytes(u16bit val)
Definition bswap.h:24
void bswap_4(T x[4])
Definition bswap.h:112
T rotate_right(T input, size_t rot)
Definition rotate.h:34
unsigned long long u64bit
Definition types.h:49
T rotate_left(T input, size_t rot)
Definition rotate.h:21
unsigned int u32bit
Definition types.h:32
unsigned short u16bit
Definition types.h:27