1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* bitrotate.h - Rotate bits in integers
4 Copyright (C) 2008-2013 Free Software Foundation, Inc.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
21 #ifndef _GL_BITROTATE_H
22 #define _GL_BITROTATE_H
26 #include <sys/types.h>
28 _GL_INLINE_HEADER_BEGIN
29 #ifndef BITROTATE_INLINE
30 # define BITROTATE_INLINE _GL_INLINE
34 /* Given an unsigned 64-bit argument X, return the value corresponding
35 to rotating the bits N steps to the left. N must be between 1 and
37 BITROTATE_INLINE uint64_t
38 rotl64 (uint64_t x, int n)
40 return ((x << n) | (x >> (64 - n))) & UINT64_MAX;
43 /* Given an unsigned 64-bit argument X, return the value corresponding
44 to rotating the bits N steps to the right. N must be between 1 to
46 BITROTATE_INLINE uint64_t
47 rotr64 (uint64_t x, int n)
49 return ((x >> n) | (x << (64 - n))) & UINT64_MAX;
53 /* Given an unsigned 32-bit argument X, return the value corresponding
54 to rotating the bits N steps to the left. N must be between 1 and
56 BITROTATE_INLINE uint32_t
57 rotl32 (uint32_t x, int n)
59 return ((x << n) | (x >> (32 - n))) & UINT32_MAX;
62 /* Given an unsigned 32-bit argument X, return the value corresponding
63 to rotating the bits N steps to the right. N must be between 1 to
65 BITROTATE_INLINE uint32_t
66 rotr32 (uint32_t x, int n)
68 return ((x >> n) | (x << (32 - n))) & UINT32_MAX;
71 /* Given a size_t argument X, return the value corresponding
72 to rotating the bits N steps to the left. N must be between 1 and
73 (CHAR_BIT * sizeof (size_t) - 1) inclusive. */
74 BITROTATE_INLINE size_t
75 rotl_sz (size_t x, int n)
77 return ((x << n) | (x >> ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
80 /* Given a size_t argument X, return the value corresponding
81 to rotating the bits N steps to the right. N must be between 1 to
82 (CHAR_BIT * sizeof (size_t) - 1) inclusive. */
83 BITROTATE_INLINE size_t
84 rotr_sz (size_t x, int n)
86 return ((x >> n) | (x << ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
89 /* Given an unsigned 16-bit argument X, return the value corresponding
90 to rotating the bits N steps to the left. N must be between 1 to
91 15 inclusive, but on most relevant targets N can also be 0 and 16
92 because 'int' is at least 32 bits and the arguments must widen
94 BITROTATE_INLINE uint16_t
95 rotl16 (uint16_t x, int n)
97 return ((x << n) | (x >> (16 - n))) & UINT16_MAX;
100 /* Given an unsigned 16-bit argument X, return the value corresponding
101 to rotating the bits N steps to the right. N must be in 1 to 15
102 inclusive, but on most relevant targets N can also be 0 and 16
103 because 'int' is at least 32 bits and the arguments must widen
105 BITROTATE_INLINE uint16_t
106 rotr16 (uint16_t x, int n)
108 return ((x >> n) | (x << (16 - n))) & UINT16_MAX;
111 /* Given an unsigned 8-bit argument X, return the value corresponding
112 to rotating the bits N steps to the left. N must be between 1 to 7
113 inclusive, but on most relevant targets N can also be 0 and 8
114 because 'int' is at least 32 bits and the arguments must widen
116 BITROTATE_INLINE uint8_t
117 rotl8 (uint8_t x, int n)
119 return ((x << n) | (x >> (8 - n))) & UINT8_MAX;
122 /* Given an unsigned 8-bit argument X, return the value corresponding
123 to rotating the bits N steps to the right. N must be in 1 to 7
124 inclusive, but on most relevant targets N can also be 0 and 8
125 because 'int' is at least 32 bits and the arguments must widen
127 BITROTATE_INLINE uint8_t
128 rotr8 (uint8_t x, int n)
130 return ((x >> n) | (x << (8 - n))) & UINT8_MAX;
133 _GL_INLINE_HEADER_END
135 #endif /* _GL_BITROTATE_H */