1 /* bitrotate.h - Rotate bits in integers
2 Copyright (C) 2008 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
25 /* Given an unsigned 64-bit argument X, return the value corresponding
26 to rotating the bits N steps to the left. N must be between 1 and
28 static inline uint64_t
29 rotl64 (uint64_t x, int n)
31 return ((x << n) | (x >> (64 - n))) & UINT64_MAX;
34 /* Given an unsigned 64-bit argument X, return the value corresponding
35 to rotating the bits N steps to the right. N must be between 1 to
37 static inline uint64_t
38 rotr64 (uint64_t x, int n)
40 return ((x >> n) | (x << (64 - n))) & UINT64_MAX;
44 /* Given an unsigned 32-bit argument X, return the value corresponding
45 to rotating the bits N steps to the left. N must be between 1 and
47 static inline uint32_t
48 rotl32 (uint32_t x, int n)
50 return ((x << n) | (x >> (32 - n))) & UINT32_MAX;
53 /* Given an unsigned 32-bit argument X, return the value corresponding
54 to rotating the bits N steps to the right. N must be between 1 to
56 static inline uint32_t
57 rotr32 (uint32_t x, int n)
59 return ((x >> n) | (x << (32 - n))) & UINT32_MAX;
62 /* Given an unsigned 16-bit argument X, return the value corresponding
63 to rotating the bits N steps to the left. N must be between 1 to
64 15 inclusive, but on most relevant targets N can also be 0 and 16
65 because 'int' is at least 32 bits and the arguments must widen
67 static inline uint16_t
68 rotl16 (uint16_t x, int n)
70 return ((x << n) | (x >> (16 - n))) & UINT16_MAX;
73 /* Given an unsigned 16-bit argument X, return the value corresponding
74 to rotating the bits N steps to the right. N must be in 1 to 15
75 inclusive, but on most relevant targets N can also be 0 and 16
76 because 'int' is at least 32 bits and the arguments must widen
78 static inline uint16_t
79 rotr16 (uint16_t x, int n)
81 return ((x >> n) | (x << (16 - n))) & UINT16_MAX;
84 /* Given an unsigned 8-bit argument X, return the value corresponding
85 to rotating the bits N steps to the left. N must be between 1 to 7
86 inclusive, but on most relevant targets N can also be 0 and 8
87 because 'int' is at least 32 bits and the arguments must widen
90 rotl8 (uint8_t x, int n)
92 return ((x << n) | (x >> (8 - n))) & UINT8_MAX;
95 /* Given an unsigned 8-bit argument X, return the value corresponding
96 to rotating the bits N steps to the right. N must be in 1 to 7
97 inclusive, but on most relevant targets N can also be 0 and 8
98 because 'int' is at least 32 bits and the arguments must widen
100 static inline uint8_t
101 rotr8 (uint8_t x, int n)
103 return ((x >> n) | (x << (8 - n))) & UINT8_MAX;
106 #endif /* _GL_BITROTATE_H */