1 /* bitrotate.h - Rotate bits in integers
2 Copyright (C) 2008-2013 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
19 #ifndef _GL_BITROTATE_H
20 #define _GL_BITROTATE_H
24 #include <sys/types.h>
26 _GL_INLINE_HEADER_BEGIN
27 #ifndef BITROTATE_INLINE
28 # define BITROTATE_INLINE _GL_INLINE
32 /* Given an unsigned 64-bit argument X, return the value corresponding
33 to rotating the bits N steps to the left. N must be between 1 and
35 BITROTATE_INLINE uint64_t
36 rotl64 (uint64_t x, int n)
38 return ((x << n) | (x >> (64 - n))) & UINT64_MAX;
41 /* Given an unsigned 64-bit argument X, return the value corresponding
42 to rotating the bits N steps to the right. N must be between 1 to
44 BITROTATE_INLINE uint64_t
45 rotr64 (uint64_t x, int n)
47 return ((x >> n) | (x << (64 - n))) & UINT64_MAX;
51 /* Given an unsigned 32-bit argument X, return the value corresponding
52 to rotating the bits N steps to the left. N must be between 1 and
54 BITROTATE_INLINE uint32_t
55 rotl32 (uint32_t x, int n)
57 return ((x << n) | (x >> (32 - n))) & UINT32_MAX;
60 /* Given an unsigned 32-bit argument X, return the value corresponding
61 to rotating the bits N steps to the right. N must be between 1 to
63 BITROTATE_INLINE uint32_t
64 rotr32 (uint32_t x, int n)
66 return ((x >> n) | (x << (32 - n))) & UINT32_MAX;
69 /* Given a size_t argument X, return the value corresponding
70 to rotating the bits N steps to the left. N must be between 1 and
71 (CHAR_BIT * sizeof (size_t) - 1) inclusive. */
72 BITROTATE_INLINE size_t
73 rotl_sz (size_t x, int n)
75 return ((x << n) | (x >> ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
78 /* Given a size_t argument X, return the value corresponding
79 to rotating the bits N steps to the right. N must be between 1 to
80 (CHAR_BIT * sizeof (size_t) - 1) inclusive. */
81 BITROTATE_INLINE size_t
82 rotr_sz (size_t x, int n)
84 return ((x >> n) | (x << ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
87 /* Given an unsigned 16-bit argument X, return the value corresponding
88 to rotating the bits N steps to the left. N must be between 1 to
89 15 inclusive, but on most relevant targets N can also be 0 and 16
90 because 'int' is at least 32 bits and the arguments must widen
92 BITROTATE_INLINE uint16_t
93 rotl16 (uint16_t x, int n)
95 return ((x << n) | (x >> (16 - n))) & UINT16_MAX;
98 /* Given an unsigned 16-bit argument X, return the value corresponding
99 to rotating the bits N steps to the right. N must be in 1 to 15
100 inclusive, but on most relevant targets N can also be 0 and 16
101 because 'int' is at least 32 bits and the arguments must widen
103 BITROTATE_INLINE uint16_t
104 rotr16 (uint16_t x, int n)
106 return ((x >> n) | (x << (16 - n))) & UINT16_MAX;
109 /* Given an unsigned 8-bit argument X, return the value corresponding
110 to rotating the bits N steps to the left. N must be between 1 to 7
111 inclusive, but on most relevant targets N can also be 0 and 8
112 because 'int' is at least 32 bits and the arguments must widen
114 BITROTATE_INLINE uint8_t
115 rotl8 (uint8_t x, int n)
117 return ((x << n) | (x >> (8 - n))) & UINT8_MAX;
120 /* Given an unsigned 8-bit argument X, return the value corresponding
121 to rotating the bits N steps to the right. N must be in 1 to 7
122 inclusive, but on most relevant targets N can also be 0 and 8
123 because 'int' is at least 32 bits and the arguments must widen
125 BITROTATE_INLINE uint8_t
126 rotr8 (uint8_t x, int n)
128 return ((x >> n) | (x << (8 - n))) & UINT8_MAX;
131 _GL_INLINE_HEADER_END
133 #endif /* _GL_BITROTATE_H */