]> git.cworth.org Git - tar/blob - gnu/bitrotate.h
upstream: Fix extraction of device nodes.
[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, 2009, 2010 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 #ifdef UINT64_MAX
29 /* Given an unsigned 64-bit argument X, return the value corresponding
30    to rotating the bits N steps to the left.  N must be between 1 and
31    63 inclusive. */
32 static inline uint64_t
33 rotl64 (uint64_t x, int n)
34 {
35   return ((x << n) | (x >> (64 - n))) & UINT64_MAX;
36 }
37
38 /* Given an unsigned 64-bit argument X, return the value corresponding
39    to rotating the bits N steps to the right.  N must be between 1 to
40    63 inclusive.*/
41 static inline uint64_t
42 rotr64 (uint64_t x, int n)
43 {
44   return ((x >> n) | (x << (64 - n))) & UINT64_MAX;
45 }
46 #endif
47
48 /* Given an unsigned 32-bit argument X, return the value corresponding
49    to rotating the bits N steps to the left.  N must be between 1 and
50    31 inclusive. */
51 static inline uint32_t
52 rotl32 (uint32_t x, int n)
53 {
54   return ((x << n) | (x >> (32 - n))) & UINT32_MAX;
55 }
56
57 /* Given an unsigned 32-bit argument X, return the value corresponding
58    to rotating the bits N steps to the right.  N must be between 1 to
59    31 inclusive.*/
60 static inline uint32_t
61 rotr32 (uint32_t x, int n)
62 {
63   return ((x >> n) | (x << (32 - n))) & UINT32_MAX;
64 }
65
66 /* Given a size_t argument X, return the value corresponding
67    to rotating the bits N steps to the left.  N must be between 1 and
68    (CHAR_BIT * sizeof (size_t) - 1) inclusive.  */
69 static inline size_t
70 rotl_sz (size_t x, int n)
71 {
72   return ((x << n) | (x >> ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
73 }
74
75 /* Given a size_t argument X, return the value corresponding
76    to rotating the bits N steps to the right.  N must be between 1 to
77    (CHAR_BIT * sizeof (size_t) - 1) inclusive.  */
78 static inline size_t
79 rotr_sz (size_t x, int n)
80 {
81   return ((x >> n) | (x << ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
82 }
83
84 /* Given an unsigned 16-bit argument X, return the value corresponding
85    to rotating the bits N steps to the left.  N must be between 1 to
86    15 inclusive, but on most relevant targets N can also be 0 and 16
87    because 'int' is at least 32 bits and the arguments must widen
88    before shifting. */
89 static inline uint16_t
90 rotl16 (uint16_t x, int n)
91 {
92   return ((x << n) | (x >> (16 - n))) & UINT16_MAX;
93 }
94
95 /* Given an unsigned 16-bit argument X, return the value corresponding
96    to rotating the bits N steps to the right.  N must be in 1 to 15
97    inclusive, but on most relevant targets N can also be 0 and 16
98    because 'int' is at least 32 bits and the arguments must widen
99    before shifting. */
100 static inline uint16_t
101 rotr16 (uint16_t x, int n)
102 {
103   return ((x >> n) | (x << (16 - n))) & UINT16_MAX;
104 }
105
106 /* Given an unsigned 8-bit argument X, return the value corresponding
107    to rotating the bits N steps to the left.  N must be between 1 to 7
108    inclusive, but on most relevant targets N can also be 0 and 8
109    because 'int' is at least 32 bits and the arguments must widen
110    before shifting. */
111 static inline uint8_t
112 rotl8 (uint8_t x, int n)
113 {
114   return ((x << n) | (x >> (8 - n))) & UINT8_MAX;
115 }
116
117 /* Given an unsigned 8-bit argument X, return the value corresponding
118    to rotating the bits N steps to the right.  N must be in 1 to 7
119    inclusive, but on most relevant targets N can also be 0 and 8
120    because 'int' is at least 32 bits and the arguments must widen
121    before shifting. */
122 static inline uint8_t
123 rotr8 (uint8_t x, int n)
124 {
125   return ((x >> n) | (x << (8 - n))) & UINT8_MAX;
126 }
127
128 #endif /* _GL_BITROTATE_H */