Commit b9ef45ffa8df1b63b8d126910f62541fa355155e
1 parent
941db528
Add shared ctz32, cto32, ctz64, cto64,
ctpop8, ctpop16, ctpop32 and ctpop64 helpers. git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@3465 c046a42c-6fe2-441c-8c8c-71466251a162
Showing
1 changed file
with
97 additions
and
0 deletions
host-utils.h
@@ -102,3 +102,100 @@ static always_inline int clo64(uint64_t val) | @@ -102,3 +102,100 @@ static always_inline int clo64(uint64_t val) | ||
102 | { | 102 | { |
103 | return clz64(~val); | 103 | return clz64(~val); |
104 | } | 104 | } |
105 | + | ||
106 | +static always_inline int ctz32 (uint32_t val) | ||
107 | +{ | ||
108 | + int cnt; | ||
109 | + | ||
110 | + cnt = 0; | ||
111 | + if (!(val & 0x0000FFFFUL)) { | ||
112 | + cnt += 16; | ||
113 | + val >>= 16; | ||
114 | + } | ||
115 | + if (!(val & 0x000000FFUL)) { | ||
116 | + cnt += 8; | ||
117 | + val >>= 8; | ||
118 | + } | ||
119 | + if (!(val & 0x0000000FUL)) { | ||
120 | + cnt += 4; | ||
121 | + val >>= 4; | ||
122 | + } | ||
123 | + if (!(val & 0x00000003UL)) { | ||
124 | + cnt += 2; | ||
125 | + val >>= 2; | ||
126 | + } | ||
127 | + if (!(val & 0x00000001UL)) { | ||
128 | + cnt++; | ||
129 | + val >>= 1; | ||
130 | + } | ||
131 | + if (!(val & 0x00000001UL)) { | ||
132 | + cnt++; | ||
133 | + } | ||
134 | + | ||
135 | + return cnt; | ||
136 | + } | ||
137 | + | ||
138 | +static always_inline int cto32 (uint32_t val) | ||
139 | + { | ||
140 | + return ctz32(~val); | ||
141 | +} | ||
142 | + | ||
143 | +static always_inline int ctz64 (uint64_t val) | ||
144 | +{ | ||
145 | + int cnt; | ||
146 | + | ||
147 | + cnt = 0; | ||
148 | + if (!((uint32_t)val)) { | ||
149 | + cnt += 32; | ||
150 | + val >>= 32; | ||
151 | + } | ||
152 | + | ||
153 | + return cnt + ctz32(val); | ||
154 | +} | ||
155 | + | ||
156 | +static always_inline int cto64 (uint64_t val) | ||
157 | +{ | ||
158 | + return ctz64(~val); | ||
159 | +} | ||
160 | + | ||
161 | +static always_inline int ctpop8 (uint8_t val) | ||
162 | +{ | ||
163 | + val = (val & 0x55) + ((val >> 1) & 0x55); | ||
164 | + val = (val & 0x33) + ((val >> 2) & 0x33); | ||
165 | + val = (val & 0x0f) + ((val >> 4) & 0x0f); | ||
166 | + | ||
167 | + return val; | ||
168 | +} | ||
169 | + | ||
170 | +static always_inline int ctpop16 (uint16_t val) | ||
171 | +{ | ||
172 | + val = (val & 0x5555) + ((val >> 1) & 0x5555); | ||
173 | + val = (val & 0x3333) + ((val >> 2) & 0x3333); | ||
174 | + val = (val & 0x0f0f) + ((val >> 4) & 0x0f0f); | ||
175 | + val = (val & 0x00ff) + ((val >> 8) & 0x00ff); | ||
176 | + | ||
177 | + return val; | ||
178 | +} | ||
179 | + | ||
180 | +static always_inline int ctpop32 (uint32_t val) | ||
181 | +{ | ||
182 | + val = (val & 0x55555555) + ((val >> 1) & 0x55555555); | ||
183 | + val = (val & 0x33333333) + ((val >> 2) & 0x33333333); | ||
184 | + val = (val & 0x0f0f0f0f) + ((val >> 4) & 0x0f0f0f0f); | ||
185 | + val = (val & 0x00ff00ff) + ((val >> 8) & 0x00ff00ff); | ||
186 | + val = (val & 0x0000ffff) + ((val >> 16) & 0x0000ffff); | ||
187 | + | ||
188 | + return val; | ||
189 | +} | ||
190 | + | ||
191 | +static always_inline int ctpop64 (uint64_t val) | ||
192 | +{ | ||
193 | + val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL); | ||
194 | + val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL); | ||
195 | + val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >> 4) & 0x0f0f0f0f0f0f0f0fULL); | ||
196 | + val = (val & 0x00ff00ff00ff00ffULL) + ((val >> 8) & 0x00ff00ff00ff00ffULL); | ||
197 | + val = (val & 0x0000ffff0000ffffULL) + ((val >> 16) & 0x0000ffff0000ffffULL); | ||
198 | + val = (val & 0x00000000ffffffffULL) + ((val >> 32) & 0x00000000ffffffffULL); | ||
199 | + | ||
200 | + return val; | ||
201 | + } |