2 * bit manipulation utility functions
4 * Copyright (C) 2010, Broadcom Corporation. All Rights Reserved.
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
13 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
15 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * $Id: bitfuncs.h,v 13.4 2005-09-13 23:15:40 Exp $
25 /* local prototypes */
26 static INLINE uint32
find_msbit(uint32 x
);
30 * find_msbit: returns index of most significant set bit in x, with index
31 * range defined as 0-31. NOTE: returns zero if input is zero.
34 #if defined(USE_PENTIUM_BSR) && defined(__GNUC__)
37 * Implementation for Pentium processors and gcc. Note that this
38 * instruction is actually very slow on some processors (e.g., family 5,
39 * model 2, stepping 12, "Pentium 75 - 200"), so we use the generic
40 * implementation instead.
42 static INLINE uint32
find_msbit(uint32 x
)
51 #else /* !USE_PENTIUM_BSR || !__GNUC__ */
54 * Generic Implementation
57 #define DB_POW_MASK16 0xffff0000
58 #define DB_POW_MASK8 0x0000ff00
59 #define DB_POW_MASK4 0x000000f0
60 #define DB_POW_MASK2 0x0000000c
61 #define DB_POW_MASK1 0x00000002
63 static INLINE uint32
find_msbit(uint32 x
)
67 if (temp_x
& DB_POW_MASK16
) {
71 if (temp_x
& DB_POW_MASK8
) {
75 if (temp_x
& DB_POW_MASK4
) {
79 if (temp_x
& DB_POW_MASK2
) {
83 if (temp_x
& DB_POW_MASK1
) {
89 #endif /* USE_PENTIUM_BSR && __GNUC__ */
91 #endif /* _BITFUNCS_H */