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