529057de85a63d095faf1293c8cfc621e8e267a7
[libfirm] / include / libfirm / adt / bitset_ia32.h
1 /*
2  * Copyright (C) 1995-2008 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__("bt    %2, %1\n\t"
55                         "rcl   $1, %0\n\t"
56                         "and   $1, %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;
66         __asm__("bsr    %1, %0\n\t"
67                         "cmovz  %2, %0\n\t"
68                         "neg    %0\n\t"
69                         "add   $31, %0"
70                         : "=&r" (res)
71                         : "m" (*unit), "r" (-1)
72                         : "cc");
73         return res;
74 }
75
76 static INLINE unsigned _bitset_ia32_inside_ntz(bitset_unit_t *unit) {
77         unsigned res;
78         __asm__("bsfl   %1, %0\n\t"
79                         "cmovz  %2, %0\n\t"
80                         : "=&r" (res)
81                         : "m" (*unit), "r" (32)
82                         : "cc");
83         return res;
84 }
85
86 static INLINE unsigned _bitset_ia32_inside_ntz_value(bitset_unit_t unit) {
87         unsigned res;
88         __asm__("bsfl   %1, %0\n\t"
89                         "cmovz  %2, %0\n\t"
90                         : "=&r" (res)
91                         : "r" (unit), "r" (32)
92                         : "cc");
93         return res;
94 }
95
96 #endif