2 * Utility compute operations used by translated code.
4 * Copyright (c) 2007 Thiemo Seufer
5 * Copyright (c) 2007 Jocelyn Mayer
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 /* Note that some of those functions may end up calling libgcc functions,
27 depending on the host machine. It is up to the target emulation to
30 /* Binary search for leading zeros. */
32 static always_inline int clz32(uint32_t val)
36 if (!(val & 0xFFFF0000U)) {
40 if (!(val & 0xFF000000U)) {
44 if (!(val & 0xF0000000U)) {
48 if (!(val & 0xC0000000U)) {
52 if (!(val & 0x80000000U)) {
56 if (!(val & 0x80000000U)) {
62 static always_inline int clo32(uint32_t val)
67 static always_inline int clz64(uint64_t val)
71 if (!(val & 0xFFFFFFFF00000000ULL)) {
75 if (!(val & 0xFFFF000000000000ULL)) {
79 if (!(val & 0xFF00000000000000ULL)) {
83 if (!(val & 0xF000000000000000ULL)) {
87 if (!(val & 0xC000000000000000ULL)) {
91 if (!(val & 0x8000000000000000ULL)) {
95 if (!(val & 0x8000000000000000ULL)) {
101 static always_inline int clo64(uint64_t val)
106 static always_inline int ctz32 (uint32_t val)
111 if (!(val & 0x0000FFFFUL)) {
115 if (!(val & 0x000000FFUL)) {
119 if (!(val & 0x0000000FUL)) {
123 if (!(val & 0x00000003UL)) {
127 if (!(val & 0x00000001UL)) {
131 if (!(val & 0x00000001UL)) {
138 static always_inline int cto32 (uint32_t val)
143 static always_inline int ctz64 (uint64_t val)
148 if (!((uint32_t)val)) {
153 return cnt + ctz32(val);
156 static always_inline int cto64 (uint64_t val)
161 static always_inline int ctpop8 (uint8_t val)
163 val = (val & 0x55) + ((val >> 1) & 0x55);
164 val = (val & 0x33) + ((val >> 2) & 0x33);
165 val = (val & 0x0f) + ((val >> 4) & 0x0f);
170 static always_inline int ctpop16 (uint16_t val)
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);
180 static always_inline int ctpop32 (uint32_t val)
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);
191 static always_inline int ctpop64 (uint64_t val)
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);