File: arch-impl.h

package info (click to toggle)
ncbi-vdb 3.0.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 41,756 kB
  • sloc: ansic: 310,503; cpp: 57,986; python: 5,353; perl: 3,392; yacc: 2,158; sh: 706; lex: 570; makefile: 421; xml: 4
file content (443 lines) | stat: -rw-r--r-- 9,530 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*===========================================================================
*
*                            PUBLIC DOMAIN NOTICE
*               National Center for Biotechnology Information
*
*  This software/database is a "United States Government Work" under the
*  terms of the United States Copyright Act.  It was written as part of
*  the author's official duties as a United States Government employee and
*  thus cannot be copyrighted.  This software/database is freely available
*  to the public for use. The National Library of Medicine and the U.S.
*  Government have not placed any restriction on its use or reproduction.
*
*  Although all reasonable efforts have been taken to ensure the accuracy
*  and reliability of the software and data, the NLM and the U.S.
*  Government do not and cannot warrant the performance or results that
*  may be obtained by using this software or data. The NLM and the U.S.
*  Government disclaim all warranties, express or implied, including
*  warranties of performance, merchantability or fitness for any particular
*  purpose.
*
*  Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
*/

#ifndef _h_arch_impl_
#define _h_arch_impl_

#include <stdint.h>

#ifndef USE_GCC_BUILTIN
#define USE_GCC_BUILTIN 1
#endif

#if USE_GCC_BUILTIN
#include <strings.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif

static __inline__
int16_t uint16_lsbit ( uint16_t self )
{
    return ( int16_t ) __builtin_ffs ( self ) - 1;
}

static __inline__
int32_t uint32_lsbit ( uint32_t self )
{
    return __builtin_ffs ( self ) - 1;
}

static __inline__
int32_t uint64_lsbit ( uint64_t self )
{
    return __builtin_ffsll ( self ) - 1;
}

static __inline__
int16_t uint16_msbit ( uint16_t self )
{
    if ( self != 0 )
        return ( int16_t ) 31 - __builtin_clz ( ( uint32_t ) self );
    return -1;
}

static __inline__
int32_t uint32_msbit ( uint32_t self )
{
    if ( self != 0 )
        return 31 - __builtin_clz ( self );
    return -1;
}

static __inline__
int32_t uint64_msbit ( uint64_t self )
{
    if (self==0) return -1;
    return 63 - __builtin_clzll ( self );
}

typedef struct int128_t int128_t;
struct int128_t
{
    uint64_t lo;
    int64_t hi;
};

static __inline__
int64_t int128_hi ( const int128_t *self )
{
    return self -> hi;
}

static __inline__
uint64_t int128_lo ( const int128_t *self )
{
    return self -> lo;
}

static __inline__
void int128_sethi ( int128_t *self, int64_t i )
{
    self -> hi = i;
}

static __inline__
void int128_setlo ( int128_t *self, uint64_t i )
{
    self -> lo = i;
}

typedef struct uint128_t uint128_t;
struct uint128_t
{
    uint64_t lo;
    uint64_t hi;
};

static __inline__
uint64_t uint128_hi ( const uint128_t *self )
{
    return self -> hi;
}

static __inline__
uint64_t uint128_lo ( const uint128_t *self )
{
    return self -> lo;
}

static __inline__
void uint128_sethi ( uint128_t *self, uint64_t i )
{
    self -> hi = i;
}

static __inline__
void uint128_setlo ( uint128_t *self, uint64_t i )
{
    self -> lo = i;
}

static __inline__
void int128_add ( int128_t *self, const int128_t *i )
{
    __asm__ __volatile__
    (
        "push %%ebx;"
        "mov (%%edx), %%eax;"
        "mov 4(%%edx), %%ebx;"
        "mov 8(%%edx), %%esi;"
        "mov 12(%%edx), %%edi;"
        "add %%eax, (%%ecx);"
        "adc %%ebx, 4(%%ecx);"
        "adc %%esi, 8(%%ecx);"
        "adc %%edi, 12(%%ecx);"
        "pop %%ebx;"
        :
        : "c" ( self ), "d" ( i )
        : "%eax", "%esi", "%edi"
    );
}

static __inline__
void int128_sub ( int128_t *self, const int128_t *i )
{
    __asm__ __volatile__
    (
        "push %%ebx;"
        "mov (%%edx), %%eax;"
        "mov 4(%%edx), %%ebx;"
        "mov 8(%%edx), %%esi;"
        "mov 12(%%edx), %%edi;"
        "sub %%eax, (%%ecx);"
        "sbb %%ebx, 4(%%ecx);"
        "sbb %%esi, 8(%%ecx);"
        "sbb %%edi, 12(%%ecx);"
        "pop %%ebx;"
        :
        : "c" ( self ), "d" ( i )
        : "%eax", "%esi", "%edi"
    );
}

static __inline__
void int128_sar ( int128_t *self, uint32_t i )
{
    __asm__ __volatile__
    (
        "mov 4(%%edx), %%eax;"
        "shrd %%cl, %%eax, (%%edx);"
        "mov 8(%%edx), %%eax;"
        "shrd %%cl, %%eax, 4(%%edx);"
        "mov 12(%%edx), %%eax;"
        "shrd %%cl, %%eax, 8(%%edx);"
        "sar %%cl, %%eax;"
        "mov %%eax, 12(%%edx);"
        :
        : "d" ( self ), "c" ( i )
        :  "%eax"
    );
}

static __inline__
void int128_shl ( int128_t *self, uint32_t i )
{
    __asm__ __volatile__
    (
        "mov 8(%%edx), %%eax;"
        "shld %%cl, %%eax, 12(%%edx);"
        "mov 4(%%edx), %%eax;"
        "shld %%cl, %%eax, 8(%%edx);"
        "mov (%%edx), %%eax;"
        "shld %%cl, %%eax, 4(%%edx);"
        "shl %%cl, %%eax;"
        "mov %%eax, (%%edx);"
        :
        : "d" ( self ), "c" ( i )
        :  "%eax"
    );
}

static __inline__
void uint128_and ( uint128_t *self, const uint128_t *i )
{
    __asm__ __volatile__
    (
        "push %%ebx;"
        "mov (%%edx), %%eax;"
        "mov 4(%%edx), %%ebx;"
        "mov 8(%%edx), %%esi;"
        "mov 12(%%edx), %%edi;"
        "and %%eax, (%%ecx);"
        "and %%ebx, 4(%%ecx);"
        "and %%esi, 8(%%ecx);"
        "and %%edi, 12(%%ecx);"
        "pop %%ebx;"
        :
        : "c" ( self ), "d" ( i )
        : "%eax", "%esi", "%edi"
    );
}

static __inline__
void uint128_or ( uint128_t *self, const uint128_t *i )
{
    __asm__ __volatile__
    (
        "push %%ebx;"
        "mov (%%edx), %%eax;"
        "mov 4(%%edx), %%ebx;"
        "mov 8(%%edx), %%esi;"
        "mov 12(%%edx), %%edi;"
        "or %%eax, (%%ecx);"
        "or %%ebx, 4(%%ecx);"
        "or %%esi, 8(%%ecx);"
        "or %%edi, 12(%%ecx);"
        "pop %%ebx;"
        :
        : "c" ( self ), "d" ( i )
        : "%eax", "%esi", "%edi"
    );
}

static __inline__
void uint128_orlo ( uint128_t *self, uint64_t i )
{
    self -> lo |= i;
}

static __inline__
void uint128_xor ( uint128_t *self, const uint128_t *i )
{
    __asm__ __volatile__
    (
        "push %%ebx;"
        "mov (%%edx), %%eax;"
        "mov 4(%%edx), %%ebx;"
        "mov 8(%%edx), %%esi;"
        "mov 12(%%edx), %%edi;"
        "xor %%eax, (%%ecx);"
        "xor %%ebx, 4(%%ecx);"
        "xor %%esi, 8(%%ecx);"
        "xor %%edi, 12(%%ecx);"
        "pop %%ebx;"
        :
        : "c" ( self ), "d" ( i )
        : "%eax", "%esi", "%edi"
    );
}

static __inline__
void uint128_not ( uint128_t *self )
{
    __asm__ __volatile__
    (
        "notl (%%ecx);"
        "notl 4(%%ecx);"
        "notl 8(%%ecx);"
        "notl 12(%%ecx);"
        :
        : "c" ( self )
    );
}

static __inline__
void uint128_shr ( uint128_t *self, uint32_t i )
{
    __asm__ __volatile__
    (
        "mov 4(%%edx), %%eax;"
        "shrd %%cl, %%eax, (%%edx);"
        "mov 8(%%edx), %%eax;"
        "shrd %%cl, %%eax, 4(%%edx);"
        "mov 12(%%edx), %%eax;"
        "shrd %%cl, %%eax, 8(%%edx);"
        "shr %%cl, %%eax;"
        "mov %%eax, 12(%%edx);"
        :
        : "d" ( self ), "c" ( i )
        :  "%eax"
    );
}

static __inline__
void uint128_shl ( uint128_t *self, uint32_t i )
{
    __asm__ __volatile__
    (
        "mov 8(%%edx), %%eax;"
        "shld %%cl, %%eax, 12(%%edx);"
        "mov 4(%%edx), %%eax;"
        "shld %%cl, %%eax, 8(%%edx);"
        "mov (%%edx), %%eax;"
        "shld %%cl, %%eax, 4(%%edx);"
        "shl %%cl, %%eax;"
        "mov %%eax, (%%edx);"
        :
        : "d" ( self ), "c" ( i )
        :  "%eax"
    );
}

static __inline__
void uint128_bswap ( uint128_t *self )
{
    __asm__ __volatile__
    (
        "mov (%%ecx), %%eax;"
        "mov 12(%%ecx), %%edx;"
        "bswap %%eax;"
        "bswap %%edx;"
        "mov %%eax, 12(%%ecx);"
        "mov %%edx, (%%ecx);"
        "mov 4(%%ecx), %%eax;"
        "mov 8(%%ecx), %%edx;"
        "bswap %%eax;"
        "bswap %%edx;"
        "mov %%eax, 8(%%ecx);"
        "mov %%edx, 4(%%ecx);"
        :
        : "c" ( self )
        :  "%eax", "%edx"
    );
}

static __inline__
void uint128_bswap_copy ( uint128_t *to, const uint128_t *from )
{
    __asm__ __volatile__
    (
        "push %%ebx;"
        "mov (%%edx), %%eax;"
        "mov 12(%%edx), %%ebx;"
        "bswap %%eax;"
        "bswap %%ebx;"
        "mov %%eax, 12(%%ecx);"
        "mov %%ebx, (%%ecx);"
        "mov 4(%%edx), %%eax;"
        "mov 8(%%edx), %%ebx;"
        "bswap %%eax;"
        "bswap %%ebx;"
        "mov %%eax, 8(%%ecx);"
        "mov %%ebx, 4(%%ecx);"
        "pop %%ebx;"
        :
        : "c" ( to ), "d" ( from )
        :  "%eax"
    );
}

static __inline__
uint32_t uint32_rol ( uint32_t val, uint8_t bits )
{
    uint32_t rtn;
    __asm__ __volatile__
    (
        "rol %%cl, %%eax;"
        : "=a" ( rtn )
        : "a" ( val ), "c" ( bits )
    );
    return rtn;
}

static __inline__
uint32_t uint32_ror ( uint32_t val, uint8_t bits )
{
    uint32_t rtn;
    __asm__ __volatile__
    (
        "ror %%cl, %%eax;"
        : "=a" ( rtn )
        : "a" ( val ), "c" ( bits )
    );
    return rtn;
}

static __inline__
uint64_t uint64_rol ( uint64_t val, uint8_t bits )
{
    uint64_t rtn;
    rtn = ( val << bits ) | ( val >> ( 64 - bits ) );
    return rtn;
}

static __inline__
uint64_t uint64_ror ( uint64_t val, uint8_t bits )
{
    uint64_t rtn;
    rtn = ( val >> bits ) | ( val << ( 64 - bits ) );
    return rtn;
}


#ifdef __cplusplus
}
#endif

#endif /* _h_arch_impl_ */