ece6237329ee55193d50d5c758fdfa5419f2b4df
[debian/tar] / gnu / bitrotate.h
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.
5
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.
10
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.
15
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/>.  */
18
19 /* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
20
21 #ifndef _GL_BITROTATE_H
22 #define _GL_BITROTATE_H
23
24 #include <limits.h>
25 #include <stdint.h>
26 #include <sys/types.h>
27
28 _GL_INLINE_HEADER_BEGIN
29 #ifndef BITROTATE_INLINE
30 # define BITROTATE_INLINE _GL_INLINE
31 #endif
32
33 #ifdef UINT64_MAX
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
36    63 inclusive. */
37 BITROTATE_INLINE uint64_t
38 rotl64 (uint64_t x, int n)
39 {
40   return ((x << n) | (x >> (64 - n))) & UINT64_MAX;
41 }
42
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
45    63 inclusive.*/
46 BITROTATE_INLINE uint64_t
47 rotr64 (uint64_t x, int n)
48 {
49   return ((x >> n) | (x << (64 - n))) & UINT64_MAX;
50 }
51 #endif
52
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
55    31 inclusive. */
56 BITROTATE_INLINE uint32_t
57 rotl32 (uint32_t x, int n)
58 {
59   return ((x << n) | (x >> (32 - n))) & UINT32_MAX;
60 }
61
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
64    31 inclusive.*/
65 BITROTATE_INLINE uint32_t
66 rotr32 (uint32_t x, int n)
67 {
68   return ((x >> n) | (x << (32 - n))) & UINT32_MAX;
69 }
70
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)
76 {
77   return ((x << n) | (x >> ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
78 }
79
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)
85 {
86   return ((x >> n) | (x << ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
87 }
88
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
93    before shifting. */
94 BITROTATE_INLINE uint16_t
95 rotl16 (uint16_t x, int n)
96 {
97   return ((x << n) | (x >> (16 - n))) & UINT16_MAX;
98 }
99
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
104    before shifting. */
105 BITROTATE_INLINE uint16_t
106 rotr16 (uint16_t x, int n)
107 {
108   return ((x >> n) | (x << (16 - n))) & UINT16_MAX;
109 }
110
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
115    before shifting. */
116 BITROTATE_INLINE uint8_t
117 rotl8 (uint8_t x, int n)
118 {
119   return ((x << n) | (x >> (8 - n))) & UINT8_MAX;
120 }
121
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
126    before shifting. */
127 BITROTATE_INLINE uint8_t
128 rotr8 (uint8_t x, int n)
129 {
130   return ((x >> n) | (x << (8 - n))) & UINT8_MAX;
131 }
132
133 _GL_INLINE_HEADER_END
134
135 #endif /* _GL_BITROTATE_H */