1 /* bitrotate.h - Rotate bits in integers
2 Copyright (C) 2008, 2009 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>
27 /* Given an unsigned 64-bit argument X, return the value corresponding
28 to rotating the bits N steps to the left. N must be between 1 and
30 static inline uint64_t
31 rotl64 (uint64_t x, int n)
33 return ((x << n) | (x >> (64 - n))) & UINT64_MAX;
36 /* Given an unsigned 64-bit argument X, return the value corresponding
37 to rotating the bits N steps to the right. N must be between 1 to
39 static inline uint64_t
40 rotr64 (uint64_t x, int n)
42 return ((x >> n) | (x << (64 - n))) & UINT64_MAX;
46 /* Given an unsigned 32-bit argument X, return the value corresponding
47 to rotating the bits N steps to the left. N must be between 1 and
49 static inline uint32_t
50 rotl32 (uint32_t x, int n)
52 return ((x << n) | (x >> (32 - n))) & UINT32_MAX;
55 /* Given an unsigned 32-bit argument X, return the value corresponding
56 to rotating the bits N steps to the right. N must be between 1 to
58 static inline uint32_t
59 rotr32 (uint32_t x, int n)
61 return ((x >> n) | (x << (32 - n))) & UINT32_MAX;
64 /* Given a size_t argument X, return the value corresponding
65 to rotating the bits N steps to the left. N must be between 1 and
66 (CHAR_BIT * sizeof (size_t) - 1) inclusive. */
68 rotl_sz (size_t x, int n)
70 return ((x << n) | (x >> ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
73 /* Given a size_t argument X, return the value corresponding
74 to rotating the bits N steps to the right. N must be between 1 to
75 (CHAR_BIT * sizeof (size_t) - 1) inclusive. */
77 rotr_sz (size_t x, int n)
79 return ((x >> n) | (x << ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
82 /* Given an unsigned 16-bit argument X, return the value corresponding
83 to rotating the bits N steps to the left. N must be between 1 to
84 15 inclusive, but on most relevant targets N can also be 0 and 16
85 because 'int' is at least 32 bits and the arguments must widen
87 static inline uint16_t
88 rotl16 (uint16_t x, int n)
90 return ((x << n) | (x >> (16 - n))) & UINT16_MAX;
93 /* Given an unsigned 16-bit argument X, return the value corresponding
94 to rotating the bits N steps to the right. N must be in 1 to 15
95 inclusive, but on most relevant targets N can also be 0 and 16
96 because 'int' is at least 32 bits and the arguments must widen
98 static inline uint16_t
99 rotr16 (uint16_t x, int n)
101 return ((x >> n) | (x << (16 - n))) & UINT16_MAX;
104 /* Given an unsigned 8-bit argument X, return the value corresponding
105 to rotating the bits N steps to the left. N must be between 1 to 7
106 inclusive, but on most relevant targets N can also be 0 and 8
107 because 'int' is at least 32 bits and the arguments must widen
109 static inline uint8_t
110 rotl8 (uint8_t x, int n)
112 return ((x << n) | (x >> (8 - n))) & UINT8_MAX;
115 /* Given an unsigned 8-bit argument X, return the value corresponding
116 to rotating the bits N steps to the right. N must be in 1 to 7
117 inclusive, but on most relevant targets N can also be 0 and 8
118 because 'int' is at least 32 bits and the arguments must widen
120 static inline uint8_t
121 rotr8 (uint8_t x, int n)
123 return ((x >> n) | (x << (8 - n))) & UINT8_MAX;
126 #endif /* _GL_BITROTATE_H */