1*23a1cceaSRoger A. Faulkner /* 2*23a1cceaSRoger A. Faulkner * CDDL HEADER START 3*23a1cceaSRoger A. Faulkner * 4*23a1cceaSRoger A. Faulkner * The contents of this file are subject to the terms of the 5*23a1cceaSRoger A. Faulkner * Common Development and Distribution License (the "License"). 6*23a1cceaSRoger A. Faulkner * You may not use this file except in compliance with the License. 7*23a1cceaSRoger A. Faulkner * 8*23a1cceaSRoger A. Faulkner * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*23a1cceaSRoger A. Faulkner * or http://www.opensolaris.org/os/licensing. 10*23a1cceaSRoger A. Faulkner * See the License for the specific language governing permissions 11*23a1cceaSRoger A. Faulkner * and limitations under the License. 12*23a1cceaSRoger A. Faulkner * 13*23a1cceaSRoger A. Faulkner * When distributing Covered Code, include this CDDL HEADER in each 14*23a1cceaSRoger A. Faulkner * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*23a1cceaSRoger A. Faulkner * If applicable, add the following below this CDDL HEADER, with the 16*23a1cceaSRoger A. Faulkner * fields enclosed by brackets "[]" replaced with your own identifying 17*23a1cceaSRoger A. Faulkner * information: Portions Copyright [yyyy] [name of copyright owner] 18*23a1cceaSRoger A. Faulkner * 19*23a1cceaSRoger A. Faulkner * CDDL HEADER END 20*23a1cceaSRoger A. Faulkner */ 21*23a1cceaSRoger A. Faulkner 22*23a1cceaSRoger A. Faulkner /* 23*23a1cceaSRoger A. Faulkner * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 24*23a1cceaSRoger A. Faulkner */ 25*23a1cceaSRoger A. Faulkner 26*23a1cceaSRoger A. Faulkner #include "lint.h" 27*23a1cceaSRoger A. Faulkner #include <string.h> 28*23a1cceaSRoger A. Faulkner #include <strings.h> 29*23a1cceaSRoger A. Faulkner #include <sys/types.h> 30*23a1cceaSRoger A. Faulkner 31*23a1cceaSRoger A. Faulkner static int ffs_impl(uint64_t bits)32*23a1cceaSRoger A. Faulknerffs_impl(uint64_t bits) 33*23a1cceaSRoger A. Faulkner { 34*23a1cceaSRoger A. Faulkner int i = 1; 35*23a1cceaSRoger A. Faulkner uint32_t bits32; 36*23a1cceaSRoger A. Faulkner 37*23a1cceaSRoger A. Faulkner if (bits == 0) 38*23a1cceaSRoger A. Faulkner return (0); 39*23a1cceaSRoger A. Faulkner 40*23a1cceaSRoger A. Faulkner if ((bits32 = (uint32_t)bits) == 0) { 41*23a1cceaSRoger A. Faulkner bits32 = (uint32_t)(bits >> 32); 42*23a1cceaSRoger A. Faulkner i += 32; 43*23a1cceaSRoger A. Faulkner } 44*23a1cceaSRoger A. Faulkner 45*23a1cceaSRoger A. Faulkner if ((bits32 & 0xffff) == 0) { 46*23a1cceaSRoger A. Faulkner bits32 >>= 16; 47*23a1cceaSRoger A. Faulkner i += 16; 48*23a1cceaSRoger A. Faulkner } 49*23a1cceaSRoger A. Faulkner if ((bits32 & 0xff) == 0) { 50*23a1cceaSRoger A. Faulkner bits32 >>= 8; 51*23a1cceaSRoger A. Faulkner i += 8; 52*23a1cceaSRoger A. Faulkner } 53*23a1cceaSRoger A. Faulkner if ((bits32 & 0xf) == 0) { 54*23a1cceaSRoger A. Faulkner bits32 >>= 4; 55*23a1cceaSRoger A. Faulkner i += 4; 56*23a1cceaSRoger A. Faulkner } 57*23a1cceaSRoger A. Faulkner if ((bits32 & 0x3) == 0) { 58*23a1cceaSRoger A. Faulkner bits32 >>= 2; 59*23a1cceaSRoger A. Faulkner i += 2; 60*23a1cceaSRoger A. Faulkner } 61*23a1cceaSRoger A. Faulkner if ((bits32 & 0x1) == 0) 62*23a1cceaSRoger A. Faulkner i += 1; 63*23a1cceaSRoger A. Faulkner 64*23a1cceaSRoger A. Faulkner return (i); 65*23a1cceaSRoger A. Faulkner } 66*23a1cceaSRoger A. Faulkner 67*23a1cceaSRoger A. Faulkner int ffs(int bits)68*23a1cceaSRoger A. Faulknerffs(int bits) 69*23a1cceaSRoger A. Faulkner { 70*23a1cceaSRoger A. Faulkner return (ffs_impl((uint64_t)(uint_t)bits)); 71*23a1cceaSRoger A. Faulkner } 72*23a1cceaSRoger A. Faulkner 73*23a1cceaSRoger A. Faulkner int ffsl(long bits)74*23a1cceaSRoger A. Faulknerffsl(long bits) 75*23a1cceaSRoger A. Faulkner { 76*23a1cceaSRoger A. Faulkner return (ffs_impl((uint64_t)(ulong_t)bits)); 77*23a1cceaSRoger A. Faulkner } 78*23a1cceaSRoger A. Faulkner 79*23a1cceaSRoger A. Faulkner int ffsll(long long bits)80*23a1cceaSRoger A. Faulknerffsll(long long bits) 81*23a1cceaSRoger A. Faulkner { 82*23a1cceaSRoger A. Faulkner return (ffs_impl((uint64_t)(u_longlong_t)bits)); 83*23a1cceaSRoger A. Faulkner } 84