Thrill  0.1
rol.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/math/rol.hpp
3  *
4  * rol32() to rotate bits left - mainly for portability.
5  *
6  * Part of tlx - http://panthema.net/tlx
7  *
8  * Copyright (C) 2018 Timo Bingmann <[email protected]>
9  *
10  * All rights reserved. Published under the Boost Software License, Version 1.0
11  ******************************************************************************/
12 
13 #ifndef TLX_MATH_ROL_HEADER
14 #define TLX_MATH_ROL_HEADER
15 
16 #include <cstdint>
17 
18 #ifdef _MSC_VER
19 #include <cstdlib>
20 #endif
21 
22 namespace tlx {
23 
24 //! \addtogroup tlx_math
25 //! \{
26 
27 /******************************************************************************/
28 // rol32() - rotate bits left in 32-bit integers
29 
30 //! rol32 - generic implementation
31 static inline uint32_t rol32_generic(const uint32_t& x, int i) {
32  return (x << static_cast<uint32_t>(i & 31)) |
33  (x >> static_cast<uint32_t>((32 - (i & 31)) & 31));
34 }
35 
36 #if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
37 
38 //! rol32 - gcc/clang assembler
39 static inline uint32_t rol32(const uint32_t& x, int i) {
40  uint32_t x1 = x;
41  asm ("roll %%cl,%0" : "=r" (x1) : "0" (x1), "c" (i));
42  return x1;
43 }
44 
45 #elif defined(_MSC_VER)
46 
47 //! rol32 - MSVC intrinsic
48 static inline uint32_t rol32(const uint32_t& x, int i) {
49  return _rotl(x, i);
50 }
51 
52 #else
53 
54 //! rol32 - generic
55 static inline uint32_t rol32(const uint32_t& x, int i) {
56  return rol32_generic(x, i);
57 }
58 
59 #endif
60 
61 /******************************************************************************/
62 // rol64() - rotate bits left in 64-bit integers
63 
64 //! rol64 - generic implementation
65 static inline uint64_t rol64_generic(const uint64_t& x, int i) {
66  return (x << static_cast<uint64_t>(i & 63)) |
67  (x >> static_cast<uint64_t>((64 - (i & 63)) & 63));
68 }
69 
70 #if (defined(__GNUC__) || defined(__clang__)) && defined(__x86_64__)
71 
72 //! rol64 - gcc/clang assembler
73 static inline uint64_t rol64(const uint64_t& x, int i) {
74  uint64_t x1 = x;
75  asm ("rolq %%cl,%0" : "=r" (x1) : "0" (x1), "c" (i));
76  return x1;
77 }
78 
79 #elif defined(_MSC_VER)
80 
81 //! rol64 - MSVC intrinsic
82 static inline uint64_t rol64(const uint64_t& x, int i) {
83  return _rotl64(x, i);
84 }
85 
86 #else
87 
88 //! rol64 - generic
89 static inline uint64_t rol64(const uint64_t& x, int i) {
90  return rol64_generic(x, i);
91 }
92 
93 #endif
94 
95 /******************************************************************************/
96 
97 //! \}
98 
99 } // namespace tlx
100 
101 #endif // !TLX_MATH_ROL_HEADER
102 
103 /******************************************************************************/
static uint64_t rol64_generic(const uint64_t &x, int i)
rol64 - generic implementation
Definition: rol.hpp:65
static uint32_t rol32_generic(const uint32_t &x, int i)
rol32 - generic implementation
Definition: rol.hpp:31
list x
Definition: gen_data.py:39
static uint32_t rol32(const uint32_t &x, int i)
rol32 - generic
Definition: rol.hpp:55
static uint64_t rol64(const uint64_t &x, int i)
rol64 - generic
Definition: rol.hpp:89