1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 #pragma ident   "%Z%%M% %I%     %E% SMI"
  27 
  28 /*
  29  * Architecture specific definition for bitmap related routines.
  30  * These may be implemented using ISA specific instructions.
  31  */
  32 #include <sys/bitmap.h>
  33 
  34 /*
  35  * Find highest one bit set.
  36  *      Returns bit number + 1 of highest bit that is set, otherwise returns 0.
  37  * High order bit is 31 (or 63 in _LP64 kernel).
  38  */
  39 int
  40 highbit(ulong_t i)
  41 {
  42         register int h = 1;
  43 
  44         if (i == 0)
  45                 return (0);
  46 #ifdef _LP64
  47         if (i & 0xffffffff00000000ul) {
  48                 h += 32; i >>= 32;
  49         }
  50 #endif
  51         if (i & 0xffff0000) {
  52                 h += 16; i >>= 16;
  53         }
  54         if (i & 0xff00) {
  55                 h += 8; i >>= 8;
  56         }
  57         if (i & 0xf0) {
  58                 h += 4; i >>= 4;
  59         }
  60         if (i & 0xc) {
  61                 h += 2; i >>= 2;
  62         }
  63         if (i & 0x2) {
  64                 h += 1;
  65         }
  66         return (h);
  67 }
  68 
  69 /*
  70  * Find lowest one bit set.
  71  *      Returns bit number + 1 of lowest bit that is set, otherwise returns 0.
  72  * Low order bit is 0.
  73  */
  74 int
  75 lowbit(ulong_t i)
  76 {
  77         register int h = 1;
  78 
  79         if (i == 0)
  80                 return (0);
  81 
  82 #ifdef _LP64
  83         if (!(i & 0xffffffff)) {
  84                 h += 32; i >>= 32;
  85         }
  86 #endif
  87         if (!(i & 0xffff)) {
  88                 h += 16; i >>= 16;
  89         }
  90         if (!(i & 0xff)) {
  91                 h += 8; i >>= 8;
  92         }
  93         if (!(i & 0xf)) {
  94                 h += 4; i >>= 4;
  95         }
  96         if (!(i & 0x3)) {
  97                 h += 2; i >>= 2;
  98         }
  99         if (!(i & 0x1)) {
 100                 h += 1;
 101         }
 102         return (h);
 103 }