Fix always_inline definition for Darwin, by Andreas Faerber.
[qemu] / host-utils.h
1 /*
2  * Utility compute operations used by translated code.
3  *
4  * Copyright (c) 2007 Thiemo Seufer
5  * Copyright (c) 2007 Jocelyn Mayer
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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
23  * THE SOFTWARE.
24  */
25
26 #include "osdep.h"
27
28 #if defined(__x86_64__)
29 #define __HAVE_FAST_MULU64__
30 static always_inline void mulu64 (uint64_t *plow, uint64_t *phigh,
31                                   uint64_t a, uint64_t b)
32 {
33     __asm__ ("mul %0\n\t"
34              : "=d" (*phigh), "=a" (*plow)
35              : "a" (a), "0" (b));
36 }
37 #define __HAVE_FAST_MULS64__
38 static always_inline void muls64 (uint64_t *plow, uint64_t *phigh,
39                                   int64_t a, int64_t b)
40 {
41     __asm__ ("imul %0\n\t"
42              : "=d" (*phigh), "=a" (*plow)
43              : "a" (a), "0" (b));
44 }
45 #else
46 void muls64(uint64_t *phigh, uint64_t *plow, int64_t a, int64_t b);
47 void mulu64(uint64_t *phigh, uint64_t *plow, uint64_t a, uint64_t b);
48 #endif
49
50 /* Note that some of those functions may end up calling libgcc functions,
51    depending on the host machine. It is up to the target emulation to
52    cope with that. */
53
54 /* Binary search for leading zeros.  */
55
56 static always_inline int clz32(uint32_t val)
57 {
58     int cnt = 0;
59
60     if (!(val & 0xFFFF0000U)) {
61         cnt += 16;
62         val <<= 16;
63     }
64     if (!(val & 0xFF000000U)) {
65         cnt += 8;
66         val <<= 8;
67     }
68     if (!(val & 0xF0000000U)) {
69         cnt += 4;
70         val <<= 4;
71     }
72     if (!(val & 0xC0000000U)) {
73         cnt += 2;
74         val <<= 2;
75     }
76     if (!(val & 0x80000000U)) {
77         cnt++;
78         val <<= 1;
79     }
80     if (!(val & 0x80000000U)) {
81         cnt++;
82     }
83     return cnt;
84 }
85
86 static always_inline int clo32(uint32_t val)
87 {
88     return clz32(~val);
89 }
90
91 static always_inline int clz64(uint64_t val)
92 {
93     int cnt = 0;
94
95     if (!(val >> 32)) {
96         cnt += 32;
97     } else {
98         val >>= 32;
99     }
100
101     return cnt + clz32(val);
102 }
103
104 static always_inline int clo64(uint64_t val)
105 {
106     return clz64(~val);
107 }
108
109 static always_inline int ctz32 (uint32_t val)
110 {
111     int cnt;
112
113     cnt = 0;
114     if (!(val & 0x0000FFFFUL)) {
115          cnt += 16;
116         val >>= 16;
117      }
118     if (!(val & 0x000000FFUL)) {
119          cnt += 8;
120         val >>= 8;
121      }
122     if (!(val & 0x0000000FUL)) {
123          cnt += 4;
124         val >>= 4;
125      }
126     if (!(val & 0x00000003UL)) {
127          cnt += 2;
128         val >>= 2;
129      }
130     if (!(val & 0x00000001UL)) {
131          cnt++;
132         val >>= 1;
133      }
134     if (!(val & 0x00000001UL)) {
135          cnt++;
136      }
137
138      return cnt;
139  }
140  
141 static always_inline int cto32 (uint32_t val)
142  {
143     return ctz32(~val);
144 }
145
146 static always_inline int ctz64 (uint64_t val)
147 {
148     int cnt;
149
150     cnt = 0;
151     if (!((uint32_t)val)) {
152         cnt += 32;
153         val >>= 32;
154     }
155
156     return cnt + ctz32(val);
157 }
158
159 static always_inline int cto64 (uint64_t val)
160 {
161     return ctz64(~val);
162 }
163
164 static always_inline int ctpop8 (uint8_t val)
165 {
166     val = (val & 0x55) + ((val >> 1) & 0x55);
167     val = (val & 0x33) + ((val >> 2) & 0x33);
168     val = (val & 0x0f) + ((val >> 4) & 0x0f);
169
170     return val;
171 }
172
173 static always_inline int ctpop16 (uint16_t val)
174 {
175     val = (val & 0x5555) + ((val >> 1) & 0x5555);
176     val = (val & 0x3333) + ((val >> 2) & 0x3333);
177     val = (val & 0x0f0f) + ((val >> 4) & 0x0f0f);
178     val = (val & 0x00ff) + ((val >> 8) & 0x00ff);
179
180     return val;
181 }
182
183 static always_inline int ctpop32 (uint32_t val)
184 {
185     val = (val & 0x55555555) + ((val >>  1) & 0x55555555);
186     val = (val & 0x33333333) + ((val >>  2) & 0x33333333);
187     val = (val & 0x0f0f0f0f) + ((val >>  4) & 0x0f0f0f0f);
188     val = (val & 0x00ff00ff) + ((val >>  8) & 0x00ff00ff);
189     val = (val & 0x0000ffff) + ((val >> 16) & 0x0000ffff);
190
191     return val;
192 }
193
194 static always_inline int ctpop64 (uint64_t val)
195 {
196     val = (val & 0x5555555555555555ULL) + ((val >>  1) & 0x5555555555555555ULL);
197     val = (val & 0x3333333333333333ULL) + ((val >>  2) & 0x3333333333333333ULL);
198     val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >>  4) & 0x0f0f0f0f0f0f0f0fULL);
199     val = (val & 0x00ff00ff00ff00ffULL) + ((val >>  8) & 0x00ff00ff00ff00ffULL);
200     val = (val & 0x0000ffff0000ffffULL) + ((val >> 16) & 0x0000ffff0000ffffULL);
201     val = (val & 0x00000000ffffffffULL) + ((val >> 32) & 0x00000000ffffffffULL);
202
203     return val;
204 }