17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 26*bf16b11eSMatthew Ahrens 27*bf16b11eSMatthew Ahrens /* 28*bf16b11eSMatthew Ahrens * Copyright (c) 2014 by Delphix. All rights reserved. 29*bf16b11eSMatthew Ahrens */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate /* 327c478bd9Sstevel@tonic-gate * Architecture specific definition for bitmap related routines. 337c478bd9Sstevel@tonic-gate * These may be implemented using ISA specific instructions. 347c478bd9Sstevel@tonic-gate */ 357c478bd9Sstevel@tonic-gate #include <sys/bitmap.h> 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate /* 387c478bd9Sstevel@tonic-gate * Find highest one bit set. 397c478bd9Sstevel@tonic-gate * Returns bit number + 1 of highest bit that is set, otherwise returns 0. 40*bf16b11eSMatthew Ahrens */ 41*bf16b11eSMatthew Ahrens int 42*bf16b11eSMatthew Ahrens highbit64(uint64_t i) 43*bf16b11eSMatthew Ahrens { 44*bf16b11eSMatthew Ahrens int h = 1; 45*bf16b11eSMatthew Ahrens 46*bf16b11eSMatthew Ahrens if (i == 0) 47*bf16b11eSMatthew Ahrens return (0); 48*bf16b11eSMatthew Ahrens if (i & 0xffffffff00000000ULL) { 49*bf16b11eSMatthew Ahrens h += 32; i >>= 32; 50*bf16b11eSMatthew Ahrens } 51*bf16b11eSMatthew Ahrens if (i & 0xffff0000) { 52*bf16b11eSMatthew Ahrens h += 16; i >>= 16; 53*bf16b11eSMatthew Ahrens } 54*bf16b11eSMatthew Ahrens if (i & 0xff00) { 55*bf16b11eSMatthew Ahrens h += 8; i >>= 8; 56*bf16b11eSMatthew Ahrens } 57*bf16b11eSMatthew Ahrens if (i & 0xf0) { 58*bf16b11eSMatthew Ahrens h += 4; i >>= 4; 59*bf16b11eSMatthew Ahrens } 60*bf16b11eSMatthew Ahrens if (i & 0xc) { 61*bf16b11eSMatthew Ahrens h += 2; i >>= 2; 62*bf16b11eSMatthew Ahrens } 63*bf16b11eSMatthew Ahrens if (i & 0x2) { 64*bf16b11eSMatthew Ahrens h += 1; 65*bf16b11eSMatthew Ahrens } 66*bf16b11eSMatthew Ahrens return (h); 67*bf16b11eSMatthew Ahrens } 68*bf16b11eSMatthew Ahrens 69*bf16b11eSMatthew Ahrens /* 70*bf16b11eSMatthew Ahrens * Find highest one bit set. 71*bf16b11eSMatthew Ahrens * Returns bit number + 1 of highest bit that is set, otherwise returns 0. 727c478bd9Sstevel@tonic-gate * High order bit is 31 (or 63 in _LP64 kernel). 737c478bd9Sstevel@tonic-gate */ 747c478bd9Sstevel@tonic-gate int 757c478bd9Sstevel@tonic-gate highbit(ulong_t i) 767c478bd9Sstevel@tonic-gate { 777c478bd9Sstevel@tonic-gate register int h = 1; 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate if (i == 0) 807c478bd9Sstevel@tonic-gate return (0); 817c478bd9Sstevel@tonic-gate #ifdef _LP64 827c478bd9Sstevel@tonic-gate if (i & 0xffffffff00000000ul) { 837c478bd9Sstevel@tonic-gate h += 32; i >>= 32; 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate #endif 867c478bd9Sstevel@tonic-gate if (i & 0xffff0000) { 877c478bd9Sstevel@tonic-gate h += 16; i >>= 16; 887c478bd9Sstevel@tonic-gate } 897c478bd9Sstevel@tonic-gate if (i & 0xff00) { 907c478bd9Sstevel@tonic-gate h += 8; i >>= 8; 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate if (i & 0xf0) { 937c478bd9Sstevel@tonic-gate h += 4; i >>= 4; 947c478bd9Sstevel@tonic-gate } 957c478bd9Sstevel@tonic-gate if (i & 0xc) { 967c478bd9Sstevel@tonic-gate h += 2; i >>= 2; 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate if (i & 0x2) { 997c478bd9Sstevel@tonic-gate h += 1; 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate return (h); 1027c478bd9Sstevel@tonic-gate } 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate /* 1057c478bd9Sstevel@tonic-gate * Find lowest one bit set. 1067c478bd9Sstevel@tonic-gate * Returns bit number + 1 of lowest bit that is set, otherwise returns 0. 1077c478bd9Sstevel@tonic-gate * Low order bit is 0. 1087c478bd9Sstevel@tonic-gate */ 1097c478bd9Sstevel@tonic-gate int 1107c478bd9Sstevel@tonic-gate lowbit(ulong_t i) 1117c478bd9Sstevel@tonic-gate { 1127c478bd9Sstevel@tonic-gate register int h = 1; 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate if (i == 0) 1157c478bd9Sstevel@tonic-gate return (0); 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate #ifdef _LP64 1187c478bd9Sstevel@tonic-gate if (!(i & 0xffffffff)) { 1197c478bd9Sstevel@tonic-gate h += 32; i >>= 32; 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate #endif 1227c478bd9Sstevel@tonic-gate if (!(i & 0xffff)) { 1237c478bd9Sstevel@tonic-gate h += 16; i >>= 16; 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate if (!(i & 0xff)) { 1267c478bd9Sstevel@tonic-gate h += 8; i >>= 8; 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate if (!(i & 0xf)) { 1297c478bd9Sstevel@tonic-gate h += 4; i >>= 4; 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate if (!(i & 0x3)) { 1327c478bd9Sstevel@tonic-gate h += 2; i >>= 2; 1337c478bd9Sstevel@tonic-gate } 1347c478bd9Sstevel@tonic-gate if (!(i & 0x1)) { 1357c478bd9Sstevel@tonic-gate h += 1; 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate return (h); 1387c478bd9Sstevel@tonic-gate } 139