libc/sysvipc: Fix some style issues (no functional change).
[dragonfly.git] / contrib / dhcpcd / compat / bitops.h
blob31979a20f458bdb26529327cd67fd7f00b0f4103
1 /* $NetBSD: bitops.h,v 1.11 2012/12/07 02:27:58 christos Exp $ */
3 /*-
4 * Copyright (c) 2007, 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas and Joerg Sonnenberger.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #ifndef COMPAT_BITOPS_H
33 #define COMPAT_BITOPS_H
35 #include <stdint.h>
36 #include "common.h"
39 * Find First Set functions
41 #ifndef ffs32
42 static inline int __unused
43 ffs32(uint32_t _n)
45 int _v;
47 if (!_n)
48 return 0;
50 _v = 1;
51 if ((_n & 0x0000FFFFU) == 0) {
52 _n >>= 16;
53 _v += 16;
55 if ((_n & 0x000000FFU) == 0) {
56 _n >>= 8;
57 _v += 8;
59 if ((_n & 0x0000000FU) == 0) {
60 _n >>= 4;
61 _v += 4;
63 if ((_n & 0x00000003U) == 0) {
64 _n >>= 2;
65 _v += 2;
67 if ((_n & 0x00000001U) == 0) {
68 //_n >>= 1;
69 _v += 1;
71 return _v;
73 #endif
75 #ifndef ffs64
76 static inline int __unused
77 ffs64(uint64_t _n)
79 int _v;
81 if (!_n)
82 return 0;
84 _v = 1;
85 if ((_n & 0x00000000FFFFFFFFULL) == 0) {
86 _n >>= 32;
87 _v += 32;
89 if ((_n & 0x000000000000FFFFULL) == 0) {
90 _n >>= 16;
91 _v += 16;
93 if ((_n & 0x00000000000000FFULL) == 0) {
94 _n >>= 8;
95 _v += 8;
97 if ((_n & 0x000000000000000FULL) == 0) {
98 _n >>= 4;
99 _v += 4;
101 if ((_n & 0x0000000000000003ULL) == 0) {
102 _n >>= 2;
103 _v += 2;
105 if ((_n & 0x0000000000000001ULL) == 0) {
106 //_n >>= 1;
107 _v += 1;
109 return _v;
111 #endif
114 * Find Last Set functions
116 #ifndef fls32
117 static __inline int __unused
118 fls32(uint32_t _n)
120 int _v;
122 if (!_n)
123 return 0;
125 _v = 32;
126 if ((_n & 0xFFFF0000U) == 0) {
127 _n <<= 16;
128 _v -= 16;
130 if ((_n & 0xFF000000U) == 0) {
131 _n <<= 8;
132 _v -= 8;
134 if ((_n & 0xF0000000U) == 0) {
135 _n <<= 4;
136 _v -= 4;
138 if ((_n & 0xC0000000U) == 0) {
139 _n <<= 2;
140 _v -= 2;
142 if ((_n & 0x80000000U) == 0) {
143 //_n <<= 1;
144 _v -= 1;
146 return _v;
148 #endif
150 #ifndef fls64
151 static int __unused
152 fls64(uint64_t _n)
154 int _v;
156 if (!_n)
157 return 0;
159 _v = 64;
160 if ((_n & 0xFFFFFFFF00000000ULL) == 0) {
161 _n <<= 32;
162 _v -= 32;
164 if ((_n & 0xFFFF000000000000ULL) == 0) {
165 _n <<= 16;
166 _v -= 16;
168 if ((_n & 0xFF00000000000000ULL) == 0) {
169 _n <<= 8;
170 _v -= 8;
172 if ((_n & 0xF000000000000000ULL) == 0) {
173 _n <<= 4;
174 _v -= 4;
176 if ((_n & 0xC000000000000000ULL) == 0) {
177 _n <<= 2;
178 _v -= 2;
180 if ((_n & 0x8000000000000000ULL) == 0) {
181 //_n <<= 1;
182 _v -= 1;
184 return _v;
186 #endif
188 #endif /* COMPAT_BITOPS_H_ */