Botan  2.11.0
Crypto and TLS for C++11
bswap.h
Go to the documentation of this file.
1 /*
2 * Byte Swapping Operations
3 * (C) 1999-2011,2018 Jack Lloyd
4 * (C) 2007 Yves Jerschow
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #ifndef BOTAN_BYTE_SWAP_H_
10 #define BOTAN_BYTE_SWAP_H_
11 
12 #include <botan/types.h>
13 
14 #if defined(BOTAN_BUILD_COMPILER_IS_MSVC)
15  #include <stdlib.h>
16 #endif
17 
18 namespace Botan {
19 
20 /**
21 * Swap a 16 bit integer
22 */
23 inline uint16_t reverse_bytes(uint16_t val)
24  {
25 #if defined(BOTAN_BUILD_COMPILER_IS_GCC) || defined(BOTAN_BUILD_COMPILER_IS_CLANG) || defined(BOTAN_BUILD_COMPILER_IS_XLC)
26  return __builtin_bswap16(val);
27 #else
28  return static_cast<uint16_t>((val << 8) | (val >> 8));
29 #endif
30  }
31 
32 /**
33 * Swap a 32 bit integer
34 */
35 inline uint32_t reverse_bytes(uint32_t val)
36  {
37 #if defined(BOTAN_BUILD_COMPILER_IS_GCC) || defined(BOTAN_BUILD_COMPILER_IS_CLANG) || defined(BOTAN_BUILD_COMPILER_IS_XLC)
38  return __builtin_bswap32(val);
39 
40 #elif defined(BOTAN_BUILD_COMPILER_IS_MSVC)
41  return _byteswap_ulong(val);
42 
43 #elif defined(BOTAN_USE_GCC_INLINE_ASM) && defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
44 
45  // GCC-style inline assembly for x86 or x86-64
46  asm("bswapl %0" : "=r" (val) : "0" (val));
47  return val;
48 
49 #else
50  // Generic implementation
51  uint16_t hi = static_cast<uint16_t>(val >> 16);
52  uint16_t lo = static_cast<uint16_t>(val);
53 
54  hi = reverse_bytes(hi);
55  lo = reverse_bytes(lo);
56 
57  return (static_cast<uint32_t>(lo) << 16) | hi;
58 #endif
59  }
60 
61 /**
62 * Swap a 64 bit integer
63 */
64 inline uint64_t reverse_bytes(uint64_t val)
65  {
66 #if defined(BOTAN_BUILD_COMPILER_IS_GCC) || defined(BOTAN_BUILD_COMPILER_IS_CLANG) || defined(BOTAN_BUILD_COMPILER_IS_XLC)
67  return __builtin_bswap64(val);
68 
69 #elif defined(BOTAN_BUILD_COMPILER_IS_MSVC)
70  return _byteswap_uint64(val);
71 
72 #elif defined(BOTAN_USE_GCC_INLINE_ASM) && defined(BOTAN_TARGET_ARCH_IS_X86_64)
73  // GCC-style inline assembly for x86-64
74  asm("bswapq %0" : "=r" (val) : "0" (val));
75  return val;
76 
77 #else
78  /* Generic implementation. Defined in terms of 32-bit bswap so any
79  * optimizations in that version can help.
80  */
81 
82  uint32_t hi = static_cast<uint32_t>(val >> 32);
83  uint32_t lo = static_cast<uint32_t>(val);
84 
85  hi = reverse_bytes(hi);
86  lo = reverse_bytes(lo);
87 
88  return (static_cast<uint64_t>(lo) << 32) | hi;
89 #endif
90  }
91 
92 /**
93 * Swap 4 Ts in an array
94 */
95 template<typename T>
96 inline void bswap_4(T x[4])
97  {
98  x[0] = reverse_bytes(x[0]);
99  x[1] = reverse_bytes(x[1]);
100  x[2] = reverse_bytes(x[2]);
101  x[3] = reverse_bytes(x[3]);
102  }
103 
104 }
105 
106 #endif
BigInt const BigInt & x
Definition: numthry.h:139
Definition: alg_id.cpp:13
void bswap_4(T x[4])
Definition: bswap.h:96
uint16_t reverse_bytes(uint16_t val)
Definition: bswap.h:23
uint32_t * val
Definition: ffi.h:800
fe T
Definition: ge.cpp:37