Slimified the bitset implementation a little bit
[libfirm] / include / libfirm / adt / bitset_ia32.h
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    intel 80x86 implementation of bitsets
23  * @version  $Id$
24  */
25 #ifndef _BITSET_IA32_H
26 #define _BITSET_IA32_H
27
28 #undef _bitset_inside_clear
29 #undef _bitset_inside_set
30 #undef _bitset_inside_flip
31
32 #define _bitset_inside_set(unit, bit) \
33         __asm__ __volatile__( "btsl %1,%0" :"=m" (*unit) : "Ir" (bit) : "cc")
34
35 #define _bitset_inside_clear(unit, bit) \
36         __asm__ __volatile__( "btrl %1,%0" :"=m" (*unit) : "Ir" (bit) : "cc")
37
38 #define _bitset_inside_flip(unit, bit) \
39         __asm__ __volatile__( "btcl %1,%0" :"=m" (*unit) : "Ir" (bit) : "cc")
40
41 #undef _bitset_inside_is_set
42 #undef _bitset_inside_nlz
43 #undef _bitset_inside_ntz
44 #undef _bitset_inside_ntz_value
45
46 #define _bitset_inside_is_set(unit, bit) _bitset_ia32_inside_is_set(unit, bit)
47 #define _bitset_inside_nlz(unit)         _bitset_ia32_inside_nlz(unit)
48 #define _bitset_inside_ntz(unit)         _bitset_ia32_inside_ntz(unit)
49 #define _bitset_inside_ntz_value(unit)   _bitset_ia32_inside_ntz_value(unit)
50
51 static INLINE int _bitset_ia32_inside_is_set(bitset_unit_t *unit, unsigned bit)
52 {
53         int res;
54         __asm__("btl   %2, %1\n\t"
55                         "mov   $0, %0\n\t"
56                         "adc   $0, %0"
57                         : "=r" (res)
58                         : "m" (*unit), "Ir" (bit)
59                         : "cc");
60         return res;
61 }
62
63 static INLINE unsigned _bitset_ia32_inside_nlz(bitset_unit_t *unit)
64 {
65         unsigned res, tmp;
66         __asm__("bsrl   %1, %0\n\t"
67                         "mov    $ffffffff, %2\n\t"
68                         "cmovz  %2, %0\n\t"
69                         "neg    %0\n\t"
70                         "add   $31, %0"
71                         : "=&r" (res)
72                         : "m" (*unit), "r" (tmp)
73                         : "cc");
74         return res;
75 }
76
77 static INLINE unsigned _bitset_ia32_inside_ntz(bitset_unit_t *unit) {
78         unsigned res, tmp;
79         __asm__("bsf l  %1, %0\n\t"
80                         "mov   $32, %2\n\t"
81                         "cmovz  %2, %0\n\t"
82                         : "=&r" (res)
83                         : "m" (*unit), "r" (tmp)
84                         : "cc");
85         return res;
86 }
87
88 static INLINE unsigned _bitset_ia32_inside_ntz_value(bitset_unit_t unit) {
89         unsigned res, tmp;
90         __asm__("bsfl   %1, %0\n\t"
91                         "mov   $32, %2\n\t"
92                         "cmovz  %2, %0\n\t"
93                         : "=&r" (res)
94                         : "r" (unit), "r" (tmp)
95                         : "cc");
96         return res;
97 }
98
99 #endif