Debian Codegraph @

interfaces/cc/clang/x86_64/arch-impl.h

raw
1 /*===========================================================================
2 *
3 * PUBLIC DOMAIN NOTICE
4 * National Center for Biotechnology Information
5 *
6 * This software/database is a "United States Government Work" under the
7 * terms of the United States Copyright Act. It was written as part of
8 * the author's official duties as a United States Government employee and
9 * thus cannot be copyrighted. This software/database is freely available
10 * to the public for use. The National Library of Medicine and the U.S.
11 * Government have not placed any restriction on its use or reproduction.
12 *
13 * Although all reasonable efforts have been taken to ensure the accuracy
14 * and reliability of the software and data, the NLM and the U.S.
15 * Government do not and cannot warrant the performance or results that
16 * may be obtained by using this software or data. The NLM and the U.S.
17 * Government disclaim all warranties, express or implied, including
18 * warranties of performance, merchantability or fitness for any particular
19 * purpose.
20 *
21 * Please cite the author in any work or product based on this material.
22 *
23 * ===========================================================================
24 *
25 */
26
27 #ifndef _h_arch_impl_
28 #define _h_arch_impl_
29
30 #include <stdint.h>
31
32 /* Intel versions from 11.0 through 13.0 mangle certain shift
33 * statements in inline assembly. */
34 #if defined(__INTEL_COMPILER) && __INTEL_COMPILER_BUILD_DATE >= 20090131 \
35 && __INTEL_COMPILER_BUILD_DATE < 20130607
36 # define HAVE_ICC_SHIFT_BUG 1
37 #endif
38
39 #ifndef USE_GCC_BUILTIN
40 #define USE_GCC_BUILTIN 1
41 #endif
42
43 #if USE_GCC_BUILTIN
44 #include <strings.h>
45 #endif
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 static __inline__
52 int16_t uint16_lsbit ( uint16_t self )
53 {
54 return ( int16_t ) __builtin_ffs ( self ) - 1;
55 }
56
57 static __inline__
58 int32_t uint32_lsbit ( uint32_t self )
59 {
60 return __builtin_ffs ( self ) - 1;
61 }
62
63 static __inline__
64 int32_t uint64_lsbit ( uint64_t self )
65 {
66 return __builtin_ffsll ( self ) - 1;
67 }
68
69 static __inline__
70 int16_t uint16_msbit ( uint16_t self )
71 {
72 if ( self != 0 )
73 return ( int16_t ) 31 - __builtin_clz ( ( uint32_t ) self );
74 return -1;
75 }
76
77 static __inline__
78 int32_t uint32_msbit ( uint32_t self )
79 {
80 if ( self != 0 )
81 return 31 - __builtin_clz ( self );
82 return -1;
83 }
84
85 static __inline__
86 int32_t uint64_msbit ( uint64_t self )
87 {
88 if ( self != 0 )
89 return 63 - __builtin_clzll ( self );
90 return -1;
91 }
92
93 typedef struct int128_t int128_t;
94 struct int128_t
95 {
96 uint64_t lo;
97 int64_t hi;
98 };
99
100 static __inline__
101 int64_t int128_hi ( const int128_t *self )
102 {
103 return self -> hi;
104 }
105
106 static __inline__
107 uint64_t int128_lo ( const int128_t *self )
108 {
109 return self -> lo;
110 }
111
112 static __inline__
113 void int128_sethi ( int128_t *self, int64_t i )
114 {
115 self -> hi = i;
116 }
117
118 static __inline__
119 void int128_setlo ( int128_t *self, uint64_t i )
120 {
121 self -> lo = i;
122 }
123
124 typedef struct uint128_t uint128_t;
125 struct uint128_t
126 {
127 uint64_t lo;
128 uint64_t hi;
129 };
130
131 static __inline__
132 uint64_t uint128_hi ( const uint128_t *self )
133 {
134 return self -> hi;
135 }
136
137 static __inline__
138 uint64_t uint128_lo ( const uint128_t *self )
139 {
140 return self -> lo;
141 }
142
143 static __inline__
144 void uint128_sethi ( uint128_t *self, uint64_t i )
145 {
146 self -> hi = i;
147 }
148
149 static __inline__
150 void uint128_setlo ( uint128_t *self, uint64_t i )
151 {
152 self -> lo = i;
153 }
154
155 static __inline__
156 void int128_add ( int128_t *self, const int128_t *i )
157 {
158 __asm__ __volatile__
159 (
160 "mov (%%rsi), %%rax;"
161 "mov 8(%%rsi), %%rcx;"
162 "add %%rax, (%%rdi);"
163 "adc %%rcx, 8(%%rdi);"
164 :
165 : "D" ( self ), "S" ( i )
166 : "%rax", "%rcx"
167 );
168 }
169
170 static __inline__
171 void int128_sub ( int128_t *self, const int128_t *i )
172 {
173 __asm__ __volatile__
174 (
175 "mov (%%rsi), %%rax;"
176 "mov 8(%%rsi), %%rcx;"
177 "sub %%rax, (%%rdi);"
178 "sbb %%rcx, 8(%%rdi);"
179 :
180 : "D" ( self ), "S" ( i )
181 : "%rax", "%rcx"
182 );
183 }
184
185 static __inline__
186 void int128_sar ( int128_t *self, uint32_t i )
187 {
188 __asm__ __volatile__
189 (
190 "mov %%esi, %%ecx;"
191 "mov 8(%%rdi), %%rax;"
192 "shrd %%cl, %%rax, (%%rdi);"
193 "sar %%cl, %%rax;"
194 "mov %%rax, 8(%%rdi);"
195 :
196 : "D" ( self ), "S" ( i )
197 : "%rax", "%rcx"
198 );
199 }
200
201 static __inline__
202 void int128_shl ( int128_t *self, uint32_t i )
203 {
204 __asm__ __volatile__
205 (
206 "mov %%esi, %%ecx;"
207 "mov (%%rdi), %%rax;"
208 "shld %%cl, %%rax, 8(%%rdi);"
209 "shl %%cl, %%rax;"
210 "mov %%rax, (%%rdi);"
211 :
212 : "D" ( self ), "S" ( i )
213 : "%rax", "%rcx"
214 );
215 }
216
217 static __inline__
218 void uint128_and ( uint128_t *self, const uint128_t *i )
219 {
220 __asm__ __volatile__
221 (
222 "mov (%%rsi), %%rax;"
223 "mov 8(%%rsi), %%rcx;"
224 "and %%rax, (%%rdi);"
225 "and %%rcx, 8(%%rdi);"
226 :
227 : "D" ( self ), "S" ( i )
228 :"%rax", "%rcx"
229 );
230 }
231
232 static __inline__
233 void uint128_or ( uint128_t *self, const uint128_t *i )
234 {
235 __asm__ __volatile__
236 (
237 "mov (%%rsi), %%rax;"
238 "mov 8(%%rsi), %%rcx;"
239 "or %%rax, (%%rdi);"
240 "or %%rcx, 8(%%rdi);"
241 :
242 : "D" ( self ), "S" ( i )
243 :"%rax", "%rcx"
244 );
245 }
246
247 static __inline__
248 void uint128_orlo ( uint128_t *self, uint64_t i )
249 {
250 self -> lo |= i;
251 }
252
253 static __inline__
254 void uint128_xor ( uint128_t *self, const uint128_t *i )
255 {
256 __asm__ __volatile__
257 (
258 "mov (%%rsi), %%rax;"
259 "mov 8(%%rsi), %%rcx;"
260 "xor %%rax, (%%rdi);"
261 "xor %%rcx, 8(%%rdi);"
262 :
263 : "D" ( self ), "S" ( i )
264 :"%rax", "%rcx"
265 );
266 }
267
268 static __inline__
269 void uint128_not ( uint128_t *self )
270 {
271 __asm__ __volatile__
272 (
273 "mov (%%rdi), %%rax;"
274 "mov 8(%%rdi), %%rcx;"
275 "not %%rax;"
276 "not %%rcx;"
277 "mov %%rax, (%%rdi);"
278 "mov %%rcx, 8(%%rdi);"
279 :
280 : "D" ( self )
281 : "%rax", "%rcx"
282 );
283 }
284
285 static __inline__
286 void uint128_shr ( uint128_t *self, uint32_t i )
287 {
288 #ifdef HAVE_ICC_SHIFT_BUG
289 if (i > 0) {
290 self->lo >>= i;
291 self->lo |= self->hi << (64 - i);
292 self->hi >>= i;
293 }
294 #else
295 __asm__ __volatile__
296 (
297 "mov %%esi, %%ecx;"
298 "mov 8(%%rdi), %%rax;"
299 "shrd %%cl, %%rax, (%%rdi);"
300 "shr %%cl, %%rax;"
301 "mov %%rax, 8(%%rdi);"
302 :
303 : "D" ( self ), "S" ( i )
304 : "%rax", "%rcx"
305 );
306 #endif
307 }
308
309 static __inline__
310 void uint128_shl ( uint128_t *self, uint32_t i )
311 {
312 __asm__ __volatile__
313 (
314 "mov %%esi, %%ecx;"
315 "mov (%%rdi), %%rax;"
316 "shld %%cl, %%rax, 8(%%rdi);"
317 "shl %%cl, %%rax;"
318 "mov %%rax, (%%rdi);"
319 :
320 : "D" ( self ), "S" ( i )
321 : "%rax", "%rcx"
322 );
323 }
324
325 static __inline__
326 void uint128_bswap ( uint128_t *self )
327 {
328 __asm__ __volatile__
329 (
330 "mov (%%rdi), %%rax;"
331 "mov 8(%%rdi), %%rcx;"
332 "bswap %%rax;"
333 "bswap %%rcx;"
334 "mov %%rax, 8(%%rdi);"
335 "mov %%rcx, (%%rdi);"
336 :
337 : "D" ( self )
338 : "%rax", "%rcx"
339 );
340 }
341
342 static __inline__
343 void uint128_bswap_copy ( uint128_t *to, const uint128_t *from )
344 {
345 __asm__ __volatile__
346 (
347 "mov (%%rsi), %%rax;"
348 "mov 8(%%rsi), %%rcx;"
349 "bswap %%rax;"
350 "bswap %%rcx;"
351 "mov %%rax, 8(%%rdi);"
352 "mov %%rcx, (%%rdi);"
353 :
354 : "D" ( to ), "S" ( from )
355 : "%rax", "%rcx"
356 );
357 }
358
359 static __inline__
360 uint32_t uint32_rol ( uint32_t val, uint8_t bits )
361 {
362 uint32_t rtn;
363 __asm__ __volatile__
364 (
365 "rol %%cl, %%eax;"
366 : "=a" ( rtn )
367 : "a" ( val ), "c" ( bits )
368 );
369 return rtn;
370 }
371
372 static __inline__
373 uint32_t uint32_ror ( uint32_t val, uint8_t bits )
374 {
375 uint32_t rtn;
376 __asm__ __volatile__
377 (
378 "ror %%cl, %%eax;"
379 : "=a" ( rtn )
380 : "a" ( val ), "c" ( bits )
381 );
382 return rtn;
383 }
384
385 static __inline__
386 uint64_t uint64_rol ( uint64_t val, uint8_t bits )
387 {
388 uint64_t rtn;
389 __asm__ __volatile__
390 (
391 "rol %%cl, %%rax;"
392 : "=a" ( rtn )
393 : "a" ( val ), "c" ( bits )
394 );
395 return rtn;
396 }
397
398 static __inline__
399 uint64_t uint64_ror ( uint64_t val, uint8_t bits )
400 {
401 uint64_t rtn;
402 __asm__ __volatile__
403 (
404 "ror %%cl, %%rax;"
405 : "=a" ( rtn )
406 : "a" ( val ), "c" ( bits )
407 );
408 return rtn;
409 }
410
411
412 #ifdef __cplusplus
413 }
414 #endif
415
416 #endif /* _h_arch_impl_ */