]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/bit.c
Update copyright/license notices to match SGI legal prefered boilerplate.
[thirdparty/xfsprogs-dev.git] / libxfs / bit.c
CommitLineData
d4ae0811 1/*
da23017d
NS
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5000d01d 4 *
da23017d
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
d4ae0811 7 * published by the Free Software Foundation.
5000d01d 8 *
da23017d
NS
9 * This program is distributed in the hope that it would 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.
5000d01d 13 *
da23017d
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
d4ae0811
NS
17 */
18
19/*
20 * XFS bit manipulation routines
21 */
22
23#include <xfs.h>
24
25/*
26 * Index of low bit number in byte, -1 for none set, 0..7 otherwise.
27 */
28static const char xfs_lowbit[256] = {
29 -1, 0, 1, 0, 2, 0, 1, 0, /* 00 .. 07 */
30 3, 0, 1, 0, 2, 0, 1, 0, /* 08 .. 0f */
31 4, 0, 1, 0, 2, 0, 1, 0, /* 10 .. 17 */
32 3, 0, 1, 0, 2, 0, 1, 0, /* 18 .. 1f */
33 5, 0, 1, 0, 2, 0, 1, 0, /* 20 .. 27 */
34 3, 0, 1, 0, 2, 0, 1, 0, /* 28 .. 2f */
35 4, 0, 1, 0, 2, 0, 1, 0, /* 30 .. 37 */
36 3, 0, 1, 0, 2, 0, 1, 0, /* 38 .. 3f */
37 6, 0, 1, 0, 2, 0, 1, 0, /* 40 .. 47 */
38 3, 0, 1, 0, 2, 0, 1, 0, /* 48 .. 4f */
39 4, 0, 1, 0, 2, 0, 1, 0, /* 50 .. 57 */
40 3, 0, 1, 0, 2, 0, 1, 0, /* 58 .. 5f */
41 5, 0, 1, 0, 2, 0, 1, 0, /* 60 .. 67 */
42 3, 0, 1, 0, 2, 0, 1, 0, /* 68 .. 6f */
43 4, 0, 1, 0, 2, 0, 1, 0, /* 70 .. 77 */
44 3, 0, 1, 0, 2, 0, 1, 0, /* 78 .. 7f */
45 7, 0, 1, 0, 2, 0, 1, 0, /* 80 .. 87 */
46 3, 0, 1, 0, 2, 0, 1, 0, /* 88 .. 8f */
47 4, 0, 1, 0, 2, 0, 1, 0, /* 90 .. 97 */
48 3, 0, 1, 0, 2, 0, 1, 0, /* 98 .. 9f */
49 5, 0, 1, 0, 2, 0, 1, 0, /* a0 .. a7 */
50 3, 0, 1, 0, 2, 0, 1, 0, /* a8 .. af */
51 4, 0, 1, 0, 2, 0, 1, 0, /* b0 .. b7 */
52 3, 0, 1, 0, 2, 0, 1, 0, /* b8 .. bf */
53 6, 0, 1, 0, 2, 0, 1, 0, /* c0 .. c7 */
54 3, 0, 1, 0, 2, 0, 1, 0, /* c8 .. cf */
55 4, 0, 1, 0, 2, 0, 1, 0, /* d0 .. d7 */
56 3, 0, 1, 0, 2, 0, 1, 0, /* d8 .. df */
57 5, 0, 1, 0, 2, 0, 1, 0, /* e0 .. e7 */
58 3, 0, 1, 0, 2, 0, 1, 0, /* e8 .. ef */
59 4, 0, 1, 0, 2, 0, 1, 0, /* f0 .. f7 */
60 3, 0, 1, 0, 2, 0, 1, 0, /* f8 .. ff */
61};
62
63/*
64 * Index of high bit number in byte, -1 for none set, 0..7 otherwise.
65 */
66static const char xfs_highbit[256] = {
67 -1, 0, 1, 1, 2, 2, 2, 2, /* 00 .. 07 */
68 3, 3, 3, 3, 3, 3, 3, 3, /* 08 .. 0f */
69 4, 4, 4, 4, 4, 4, 4, 4, /* 10 .. 17 */
70 4, 4, 4, 4, 4, 4, 4, 4, /* 18 .. 1f */
71 5, 5, 5, 5, 5, 5, 5, 5, /* 20 .. 27 */
72 5, 5, 5, 5, 5, 5, 5, 5, /* 28 .. 2f */
73 5, 5, 5, 5, 5, 5, 5, 5, /* 30 .. 37 */
74 5, 5, 5, 5, 5, 5, 5, 5, /* 38 .. 3f */
75 6, 6, 6, 6, 6, 6, 6, 6, /* 40 .. 47 */
76 6, 6, 6, 6, 6, 6, 6, 6, /* 48 .. 4f */
77 6, 6, 6, 6, 6, 6, 6, 6, /* 50 .. 57 */
78 6, 6, 6, 6, 6, 6, 6, 6, /* 58 .. 5f */
79 6, 6, 6, 6, 6, 6, 6, 6, /* 60 .. 67 */
80 6, 6, 6, 6, 6, 6, 6, 6, /* 68 .. 6f */
81 6, 6, 6, 6, 6, 6, 6, 6, /* 70 .. 77 */
82 6, 6, 6, 6, 6, 6, 6, 6, /* 78 .. 7f */
83 7, 7, 7, 7, 7, 7, 7, 7, /* 80 .. 87 */
84 7, 7, 7, 7, 7, 7, 7, 7, /* 88 .. 8f */
85 7, 7, 7, 7, 7, 7, 7, 7, /* 90 .. 97 */
86 7, 7, 7, 7, 7, 7, 7, 7, /* 98 .. 9f */
87 7, 7, 7, 7, 7, 7, 7, 7, /* a0 .. a7 */
88 7, 7, 7, 7, 7, 7, 7, 7, /* a8 .. af */
89 7, 7, 7, 7, 7, 7, 7, 7, /* b0 .. b7 */
90 7, 7, 7, 7, 7, 7, 7, 7, /* b8 .. bf */
91 7, 7, 7, 7, 7, 7, 7, 7, /* c0 .. c7 */
92 7, 7, 7, 7, 7, 7, 7, 7, /* c8 .. cf */
93 7, 7, 7, 7, 7, 7, 7, 7, /* d0 .. d7 */
94 7, 7, 7, 7, 7, 7, 7, 7, /* d8 .. df */
95 7, 7, 7, 7, 7, 7, 7, 7, /* e0 .. e7 */
96 7, 7, 7, 7, 7, 7, 7, 7, /* e8 .. ef */
97 7, 7, 7, 7, 7, 7, 7, 7, /* f0 .. f7 */
98 7, 7, 7, 7, 7, 7, 7, 7, /* f8 .. ff */
99};
100
101/*
102 * Count of bits set in byte, 0..8.
103 */
104static const char xfs_countbit[256] = {
105 0, 1, 1, 2, 1, 2, 2, 3, /* 00 .. 07 */
106 1, 2, 2, 3, 2, 3, 3, 4, /* 08 .. 0f */
107 1, 2, 2, 3, 2, 3, 3, 4, /* 10 .. 17 */
108 2, 3, 3, 4, 3, 4, 4, 5, /* 18 .. 1f */
109 1, 2, 2, 3, 2, 3, 3, 4, /* 20 .. 27 */
110 2, 3, 3, 4, 3, 4, 4, 5, /* 28 .. 2f */
111 2, 3, 3, 4, 3, 4, 4, 5, /* 30 .. 37 */
112 3, 4, 4, 5, 4, 5, 5, 6, /* 38 .. 3f */
113 1, 2, 2, 3, 2, 3, 3, 4, /* 40 .. 47 */
114 2, 3, 3, 4, 3, 4, 4, 5, /* 48 .. 4f */
115 2, 3, 3, 4, 3, 4, 4, 5, /* 50 .. 57 */
116 3, 4, 4, 5, 4, 5, 5, 6, /* 58 .. 5f */
117 2, 3, 3, 4, 3, 4, 4, 5, /* 60 .. 67 */
118 3, 4, 4, 5, 4, 5, 5, 6, /* 68 .. 6f */
119 3, 4, 4, 5, 4, 5, 5, 6, /* 70 .. 77 */
120 4, 5, 5, 6, 5, 6, 6, 7, /* 78 .. 7f */
121 1, 2, 2, 3, 2, 3, 3, 4, /* 80 .. 87 */
122 2, 3, 3, 4, 3, 4, 4, 5, /* 88 .. 8f */
123 2, 3, 3, 4, 3, 4, 4, 5, /* 90 .. 97 */
124 3, 4, 4, 5, 4, 5, 5, 6, /* 98 .. 9f */
125 2, 3, 3, 4, 3, 4, 4, 5, /* a0 .. a7 */
126 3, 4, 4, 5, 4, 5, 5, 6, /* a8 .. af */
127 3, 4, 4, 5, 4, 5, 5, 6, /* b0 .. b7 */
128 4, 5, 5, 6, 5, 6, 6, 7, /* b8 .. bf */
129 2, 3, 3, 4, 3, 4, 4, 5, /* c0 .. c7 */
130 3, 4, 4, 5, 4, 5, 5, 6, /* c8 .. cf */
131 3, 4, 4, 5, 4, 5, 5, 6, /* d0 .. d7 */
132 4, 5, 5, 6, 5, 6, 6, 7, /* d8 .. df */
133 3, 4, 4, 5, 4, 5, 5, 6, /* e0 .. e7 */
134 4, 5, 5, 6, 5, 6, 6, 7, /* e8 .. ef */
135 4, 5, 5, 6, 5, 6, 6, 7, /* f0 .. f7 */
136 5, 6, 6, 7, 6, 7, 7, 8, /* f8 .. ff */
137};
138
139/*
140 * xfs_lowbit32: get low bit set out of 32-bit argument, -1 if none set.
141 */
142int
143xfs_lowbit32(
144 __uint32_t v)
145{
146 int i;
147
148 if (v & 0x0000ffff)
149 if (v & 0x000000ff)
150 i = 0;
151 else
152 i = 8;
153 else if (v & 0xffff0000)
154 if (v & 0x00ff0000)
155 i = 16;
156 else
157 i = 24;
158 else
159 return -1;
160 return i + xfs_lowbit[(v >> i) & 0xff];
161}
162
163/*
164 * xfs_highbit32: get high bit set out of 32-bit argument, -1 if none set.
165 */
166int
167xfs_highbit32(
168 __uint32_t v)
169{
170 int i;
171
172 if (v & 0xffff0000)
173 if (v & 0xff000000)
174 i = 24;
175 else
176 i = 16;
177 else if (v & 0x0000ffff)
178 if (v & 0x0000ff00)
179 i = 8;
180 else
181 i = 0;
182 else
183 return -1;
184 return i + xfs_highbit[(v >> i) & 0xff];
185}
186
187/*
188 * xfs_lowbit64: get low bit set out of 64-bit argument, -1 if none set.
189 */
190int
191xfs_lowbit64(
192 __uint64_t v)
193{
194 int i;
195#if XFS_64
196 if (v & 0x00000000ffffffff)
197 if (v & 0x000000000000ffff)
198 if (v & 0x00000000000000ff)
199 i = 0;
200 else
201 i = 8;
202 else
203 if (v & 0x0000000000ff0000)
204 i = 16;
205 else
206 i = 24;
207 else if (v & 0xffffffff00000000)
208 if (v & 0x0000ffff00000000)
209 if (v & 0x000000ff00000000)
210 i = 32;
211 else
212 i = 40;
213 else
214 if (v & 0x00ff000000000000)
215 i = 48;
216 else
217 i = 56;
218 else
219 return -1;
220 return i + xfs_lowbit[(v >> i) & 0xff];
221#else
222 __uint32_t vw;
223
224 if ((vw = v)) {
225 if (vw & 0x0000ffff)
226 if (vw & 0x000000ff)
227 i = 0;
228 else
229 i = 8;
230 else
231 if (vw & 0x00ff0000)
232 i = 16;
233 else
234 i = 24;
235 return i + xfs_lowbit[(vw >> i) & 0xff];
236 } else if ((vw = v >> 32)) {
237 if (vw & 0x0000ffff)
238 if (vw & 0x000000ff)
239 i = 32;
240 else
241 i = 40;
242 else
243 if (vw & 0x00ff0000)
244 i = 48;
245 else
246 i = 56;
247 return i + xfs_lowbit[(vw >> (i - 32)) & 0xff];
248 } else
249 return -1;
250#endif
251}
252
253/*
254 * xfs_highbit64: get high bit set out of 64-bit argument, -1 if none set.
255 */
256int
257xfs_highbit64(
258 __uint64_t v)
259{
260 int i;
261#if XFS_64
262 if (v & 0xffffffff00000000)
263 if (v & 0xffff000000000000)
264 if (v & 0xff00000000000000)
265 i = 56;
266 else
267 i = 48;
268 else
269 if (v & 0x0000ff0000000000)
270 i = 40;
271 else
272 i = 32;
273 else if (v & 0x00000000ffffffff)
274 if (v & 0x00000000ffff0000)
275 if (v & 0x00000000ff000000)
276 i = 24;
277 else
278 i = 16;
279 else
280 if (v & 0x000000000000ff00)
281 i = 8;
282 else
283 i = 0;
284 else
285 return -1;
286 return i + xfs_highbit[(v >> i) & 0xff];
287#else
288 __uint32_t vw;
289
290 if ((vw = v >> 32)) {
291 if (vw & 0xffff0000)
292 if (vw & 0xff000000)
293 i = 56;
294 else
295 i = 48;
296 else
297 if (vw & 0x0000ff00)
298 i = 40;
299 else
300 i = 32;
301 return i + xfs_highbit[(vw >> (i - 32)) & 0xff];
302 } else if ((vw = v)) {
303 if (vw & 0xffff0000)
304 if (vw & 0xff000000)
305 i = 24;
306 else
307 i = 16;
308 else
309 if (vw & 0x0000ff00)
310 i = 8;
311 else
312 i = 0;
313 return i + xfs_highbit[(vw >> i) & 0xff];
314 } else
315 return -1;
316#endif
317}