The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/sys/bitops.h

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: bitops.h,v 1.2.10.1 2011/05/20 19:26:01 bouyer Exp $   */
    2 
    3 /*-
    4  * Copyright (c) 2007 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.
    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 #ifndef _SYS_BITOPS_H_
   32 #define _SYS_BITOPS_H_
   33 
   34 /*
   35  * Find First Set functions
   36  */
   37 #ifndef ffs32
   38 static __inline int __unused
   39 ffs32(uint32_t _n)
   40 {
   41         int _v;
   42 
   43         if (!_n)
   44                 return 0;
   45 
   46         _v = 1;
   47         if ((_n & 0x0000FFFFU) == 0) {
   48                 _n >>= 16;
   49                 _v += 16;
   50         }
   51         if ((_n & 0x000000FFU) == 0) {
   52                 _n >>= 8;
   53                 _v += 8;
   54         }
   55         if ((_n & 0x0000000FU) == 0) {
   56                 _n >>= 4;
   57                 _v += 4;
   58         }
   59         if ((_n & 0x00000003U) == 0) {
   60                 _n >>= 2;
   61                 _v += 2;
   62         }
   63         if ((_n & 0x00000001U) == 0) {
   64                 _n >>= 1;
   65                 _v += 1;
   66         }
   67         return _v;
   68 }
   69 #endif
   70 
   71 #ifndef ffs64
   72 static __inline int __unused
   73 ffs64(uint64_t _n)
   74 {
   75         int _v;
   76 
   77         if (!_n)
   78                 return 0;
   79 
   80         _v = 1;
   81         if ((_n & 0x00000000FFFFFFFFULL) == 0) {
   82                 _n >>= 32;
   83                 _v += 32;
   84         }
   85         if ((_n & 0x000000000000FFFFULL) == 0) {
   86                 _n >>= 16;
   87                 _v += 16;
   88         }
   89         if ((_n & 0x00000000000000FFULL) == 0) {
   90                 _n >>= 8;
   91                 _v += 8;
   92         }
   93         if ((_n & 0x000000000000000FULL) == 0) {
   94                 _n >>= 4;
   95                 _v += 4;
   96         }
   97         if ((_n & 0x0000000000000003ULL) == 0) {
   98                 _n >>= 2;
   99                 _v += 2;
  100         }
  101         if ((_n & 0x0000000000000001ULL) == 0) {
  102                 _n >>= 1;
  103                 _v += 1;
  104         }
  105         return _v;
  106 }
  107 #endif
  108 
  109 /*
  110  * Find Last Set functions
  111  */
  112 #ifndef fls32
  113 static __inline int __unused
  114 fls32(uint32_t _n)
  115 {
  116         int _v;
  117 
  118         if (!_n)
  119                 return 0;
  120 
  121         _v = 32;
  122         if ((_n & 0xFFFF0000U) == 0) {
  123                 _n <<= 16;
  124                 _v -= 16;
  125         }
  126         if ((_n & 0xFF000000U) == 0) {
  127                 _n <<= 8;
  128                 _v -= 8;
  129         }
  130         if ((_n & 0xF0000000U) == 0) {
  131                 _n <<= 4;
  132                 _v -= 4;
  133         }
  134         if ((_n & 0xC0000000U) == 0) {
  135                 _n <<= 2;
  136                 _v -= 2;
  137         }
  138         if ((_n & 0x80000000U) == 0) {
  139                 _n <<= 1;
  140                 _v -= 1;
  141         }
  142         return _v;
  143 }
  144 #endif
  145 
  146 #ifndef fls64
  147 static __inline int __unused
  148 fls64(uint64_t _n)
  149 {
  150         int _v;
  151 
  152         if (!_n)
  153                 return 0;
  154 
  155         _v = 64;
  156         if ((_n & 0xFFFFFFFF00000000ULL) == 0) {
  157                 _n <<= 32;
  158                 _v -= 32;
  159         }
  160         if ((_n & 0xFFFF000000000000ULL) == 0) {
  161                 _n <<= 16;
  162                 _v -= 16;
  163         }
  164         if ((_n & 0xFF00000000000000ULL) == 0) {
  165                 _n <<= 8;
  166                 _v -= 8;
  167         }
  168         if ((_n & 0xF000000000000000ULL) == 0) {
  169                 _n <<= 4;
  170                 _v -= 4;
  171         }
  172         if ((_n & 0xC000000000000000ULL) == 0) {
  173                 _n <<= 2;
  174                 _v -= 2;
  175         }
  176         if ((_n & 0x8000000000000000ULL) == 0) {
  177                 _n <<= 1;
  178                 _v -= 1;
  179         }
  180         return _v;
  181 }
  182 #endif
  183 
  184 /*
  185  * Integer logarithm, returns -1 on error. Inspired by the linux
  186  * version written by David Howells.
  187  */
  188 #define _ilog2_helper(_n, _x)   ((_n) & (1ULL << (_x))) ? _x :
  189 #define ilog2(_n) \
  190 ( \
  191         __builtin_constant_p(_n) ? ( \
  192         _ilog2_helper(_n, 63) \
  193         _ilog2_helper(_n, 62) \
  194         _ilog2_helper(_n, 61) \
  195         _ilog2_helper(_n, 60) \
  196         _ilog2_helper(_n, 59) \
  197         _ilog2_helper(_n, 58) \
  198         _ilog2_helper(_n, 57) \
  199         _ilog2_helper(_n, 56) \
  200         _ilog2_helper(_n, 55) \
  201         _ilog2_helper(_n, 54) \
  202         _ilog2_helper(_n, 53) \
  203         _ilog2_helper(_n, 52) \
  204         _ilog2_helper(_n, 51) \
  205         _ilog2_helper(_n, 50) \
  206         _ilog2_helper(_n, 49) \
  207         _ilog2_helper(_n, 48) \
  208         _ilog2_helper(_n, 47) \
  209         _ilog2_helper(_n, 46) \
  210         _ilog2_helper(_n, 45) \
  211         _ilog2_helper(_n, 44) \
  212         _ilog2_helper(_n, 43) \
  213         _ilog2_helper(_n, 42) \
  214         _ilog2_helper(_n, 41) \
  215         _ilog2_helper(_n, 40) \
  216         _ilog2_helper(_n, 39) \
  217         _ilog2_helper(_n, 38) \
  218         _ilog2_helper(_n, 37) \
  219         _ilog2_helper(_n, 36) \
  220         _ilog2_helper(_n, 35) \
  221         _ilog2_helper(_n, 34) \
  222         _ilog2_helper(_n, 33) \
  223         _ilog2_helper(_n, 32) \
  224         _ilog2_helper(_n, 31) \
  225         _ilog2_helper(_n, 30) \
  226         _ilog2_helper(_n, 29) \
  227         _ilog2_helper(_n, 28) \
  228         _ilog2_helper(_n, 27) \
  229         _ilog2_helper(_n, 26) \
  230         _ilog2_helper(_n, 25) \
  231         _ilog2_helper(_n, 24) \
  232         _ilog2_helper(_n, 23) \
  233         _ilog2_helper(_n, 22) \
  234         _ilog2_helper(_n, 21) \
  235         _ilog2_helper(_n, 20) \
  236         _ilog2_helper(_n, 19) \
  237         _ilog2_helper(_n, 18) \
  238         _ilog2_helper(_n, 17) \
  239         _ilog2_helper(_n, 16) \
  240         _ilog2_helper(_n, 15) \
  241         _ilog2_helper(_n, 14) \
  242         _ilog2_helper(_n, 13) \
  243         _ilog2_helper(_n, 12) \
  244         _ilog2_helper(_n, 11) \
  245         _ilog2_helper(_n, 10) \
  246         _ilog2_helper(_n,  9) \
  247         _ilog2_helper(_n,  8) \
  248         _ilog2_helper(_n,  7) \
  249         _ilog2_helper(_n,  6) \
  250         _ilog2_helper(_n,  5) \
  251         _ilog2_helper(_n,  4) \
  252         _ilog2_helper(_n,  3) \
  253         _ilog2_helper(_n,  2) \
  254         _ilog2_helper(_n,  1) \
  255         _ilog2_helper(_n,  0) \
  256         -1) : ((sizeof(_n) > 4 ? fls64(_n) : fls32(_n)) - 1) \
  257 )
  258 
  259 #endif /* _SYS_BITOPS_H_ */

Cache object: 665d74d4d76bd3c85bd2d4c1f4b627a3


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.