mesa/gallium: Move u_bit_scan{,64} from gallium to util.
[mesa.git] / src / util / bitscan.h
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #ifndef BITSCAN_H
30 #define BITSCAN_H
31
32 #include <stdint.h>
33
34 #if defined(_MSC_VER)
35 #include <intrin.h>
36 #endif
37
38 #include "c99_compat.h"
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44
45 /**
46 * Find first bit set in word. Least significant bit is 1.
47 * Return 0 if no bits set.
48 */
49 #ifdef HAVE___BUILTIN_FFS
50 #define ffs __builtin_ffs
51 #elif defined(_MSC_VER) && (_M_IX86 || _M_ARM || _M_AMD64 || _M_IA64)
52 static inline
53 int ffs(unsigned i)
54 {
55 unsigned long index;
56 if (_BitScanForward(&index, i))
57 return index + 1;
58 else
59 return 0;
60 }
61 #else
62 extern
63 int ffs(unsigned i);
64 #endif
65
66 #ifdef HAVE___BUILTIN_FFSLL
67 #define ffsll __builtin_ffsll
68 #elif defined(_MSC_VER) && (_M_AMD64 || _M_ARM || _M_IA64)
69 static inline int
70 ffsll(uint64_t i)
71 {
72 unsigned long index;
73 if (_BitScanForward64(&index, i))
74 return index + 1;
75 else
76 return 0;
77 }
78 #else
79 extern int
80 ffsll(uint64_t val);
81 #endif
82
83
84 /* Destructively loop over all of the bits in a mask as in:
85 *
86 * while (mymask) {
87 * int i = u_bit_scan(&mymask);
88 * ... process element i
89 * }
90 *
91 */
92 static inline int
93 u_bit_scan(unsigned *mask)
94 {
95 const int i = ffs(*mask) - 1;
96 *mask ^= (1u << i);
97 return i;
98 }
99
100 static inline int
101 u_bit_scan64(uint64_t *mask)
102 {
103 const int i = ffsll(*mask) - 1;
104 *mask ^= (((uint64_t)1) << i);
105 return i;
106 }
107
108 /* For looping over a bitmask when you want to loop over consecutive bits
109 * manually, for example:
110 *
111 * while (mask) {
112 * int start, count, i;
113 *
114 * u_bit_scan_consecutive_range(&mask, &start, &count);
115 *
116 * for (i = 0; i < count; i++)
117 * ... process element (start+i)
118 * }
119 */
120 static inline void
121 u_bit_scan_consecutive_range(unsigned *mask, int *start, int *count)
122 {
123 if (*mask == 0xffffffff) {
124 *start = 0;
125 *count = 32;
126 *mask = 0;
127 return;
128 }
129 *start = ffs(*mask) - 1;
130 *count = ffs(~(*mask >> *start)) - 1;
131 *mask &= ~(((1u << *count) - 1) << *start);
132 }
133
134 static inline void
135 u_bit_scan_consecutive_range64(uint64_t *mask, int *start, int *count)
136 {
137 if (*mask == ~0llu) {
138 *start = 0;
139 *count = 64;
140 *mask = 0;
141 return;
142 }
143 *start = ffsll(*mask) - 1;
144 *count = ffsll(~(*mask >> *start)) - 1;
145 *mask &= ~(((((uint64_t)1) << *count) - 1) << *start);
146 }
147
148
149 #ifdef __cplusplus
150 }
151 #endif
152
153 #endif /* BITSCAN_H */