1 /* $NetBSD: bitops.h,v 1.11 2012/12/07 02:27:58 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2007, 2010 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas and Joerg Sonnenberger. 9 * 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. 18 * 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. 30 */ 31 32 #ifndef COMPAT_BITOPS_H 33 #define COMPAT_BITOPS_H 34 35 #include <stdint.h> 36 #include "common.h" 37 38 /* 39 * Find First Set functions 40 */ 41 #ifndef ffs32 42 static inline int __unused 43 ffs32(uint32_t _n) 44 { 45 int _v; 46 47 if (!_n) 48 return 0; 49 50 _v = 1; 51 if ((_n & 0x0000FFFFU) == 0) { 52 _n >>= 16; 53 _v += 16; 54 } 55 if ((_n & 0x000000FFU) == 0) { 56 _n >>= 8; 57 _v += 8; 58 } 59 if ((_n & 0x0000000FU) == 0) { 60 _n >>= 4; 61 _v += 4; 62 } 63 if ((_n & 0x00000003U) == 0) { 64 _n >>= 2; 65 _v += 2; 66 } 67 if ((_n & 0x00000001U) == 0) { 68 //_n >>= 1; 69 _v += 1; 70 } 71 return _v; 72 } 73 #endif 74 75 #ifndef ffs64 76 static inline int __unused 77 ffs64(uint64_t _n) 78 { 79 int _v; 80 81 if (!_n) 82 return 0; 83 84 _v = 1; 85 if ((_n & 0x00000000FFFFFFFFULL) == 0) { 86 _n >>= 32; 87 _v += 32; 88 } 89 if ((_n & 0x000000000000FFFFULL) == 0) { 90 _n >>= 16; 91 _v += 16; 92 } 93 if ((_n & 0x00000000000000FFULL) == 0) { 94 _n >>= 8; 95 _v += 8; 96 } 97 if ((_n & 0x000000000000000FULL) == 0) { 98 _n >>= 4; 99 _v += 4; 100 } 101 if ((_n & 0x0000000000000003ULL) == 0) { 102 _n >>= 2; 103 _v += 2; 104 } 105 if ((_n & 0x0000000000000001ULL) == 0) { 106 //_n >>= 1; 107 _v += 1; 108 } 109 return _v; 110 } 111 #endif 112 113 /* 114 * Find Last Set functions 115 */ 116 #ifndef fls32 117 static __inline int __unused 118 fls32(uint32_t _n) 119 { 120 int _v; 121 122 if (!_n) 123 return 0; 124 125 _v = 32; 126 if ((_n & 0xFFFF0000U) == 0) { 127 _n <<= 16; 128 _v -= 16; 129 } 130 if ((_n & 0xFF000000U) == 0) { 131 _n <<= 8; 132 _v -= 8; 133 } 134 if ((_n & 0xF0000000U) == 0) { 135 _n <<= 4; 136 _v -= 4; 137 } 138 if ((_n & 0xC0000000U) == 0) { 139 _n <<= 2; 140 _v -= 2; 141 } 142 if ((_n & 0x80000000U) == 0) { 143 //_n <<= 1; 144 _v -= 1; 145 } 146 return _v; 147 } 148 #endif 149 150 #ifndef fls64 151 static int __unused 152 fls64(uint64_t _n) 153 { 154 int _v; 155 156 if (!_n) 157 return 0; 158 159 _v = 64; 160 if ((_n & 0xFFFFFFFF00000000ULL) == 0) { 161 _n <<= 32; 162 _v -= 32; 163 } 164 if ((_n & 0xFFFF000000000000ULL) == 0) { 165 _n <<= 16; 166 _v -= 16; 167 } 168 if ((_n & 0xFF00000000000000ULL) == 0) { 169 _n <<= 8; 170 _v -= 8; 171 } 172 if ((_n & 0xF000000000000000ULL) == 0) { 173 _n <<= 4; 174 _v -= 4; 175 } 176 if ((_n & 0xC000000000000000ULL) == 0) { 177 _n <<= 2; 178 _v -= 2; 179 } 180 if ((_n & 0x8000000000000000ULL) == 0) { 181 //_n <<= 1; 182 _v -= 1; 183 } 184 return _v; 185 } 186 #endif 187 188 #endif /* COMPAT_BITOPS_H_ */ 189