Resync with broadcom drivers 5.100.138.20 and utilities.
[tomato.git] / release / src-rt / include / bitfuncs.h
blobd926fabf10df572e57a463ed60bdbb60e3609f9f
1 /*
2 * bit manipulation utility functions
4 * Copyright (C) 2010, Broadcom Corporation. All Rights Reserved.
5 *
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.
9 *
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 $
20 #ifndef _BITFUNCS_H
21 #define _BITFUNCS_H
23 #include <typedefs.h>
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)
44 uint msbit;
45 __asm__("bsrl %1,%0"
46 :"=r" (msbit)
47 :"r" (x));
48 return msbit;
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)
65 uint32 temp_x = x;
66 uint msbit = 0;
67 if (temp_x & DB_POW_MASK16) {
68 temp_x >>= 16;
69 msbit = 16;
71 if (temp_x & DB_POW_MASK8) {
72 temp_x >>= 8;
73 msbit += 8;
75 if (temp_x & DB_POW_MASK4) {
76 temp_x >>= 4;
77 msbit += 4;
79 if (temp_x & DB_POW_MASK2) {
80 temp_x >>= 2;
81 msbit += 2;
83 if (temp_x & DB_POW_MASK1) {
84 msbit += 1;
86 return (msbit);
89 #endif /* USE_PENTIUM_BSR && __GNUC__ */
91 #endif /* _BITFUNCS_H */