2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief intel 80x86 implementation of bitsets
25 #ifndef _BITSET_IA32_H
26 #define _BITSET_IA32_H
28 #undef _bitset_inside_clear
29 #undef _bitset_inside_set
30 #undef _bitset_inside_flip
32 #define _bitset_inside_set(unit, bit) \
33 __asm__ __volatile__( "btsl %1,%0" :"=m" (*unit) : "Ir" (bit) : "cc")
35 #define _bitset_inside_clear(unit, bit) \
36 __asm__ __volatile__( "btrl %1,%0" :"=m" (*unit) : "Ir" (bit) : "cc")
38 #define _bitset_inside_flip(unit, bit) \
39 __asm__ __volatile__( "btcl %1,%0" :"=m" (*unit) : "Ir" (bit) : "cc")
41 #undef _bitset_inside_is_set
42 #undef _bitset_inside_nlz
43 #undef _bitset_inside_ntz
44 #undef _bitset_inside_ntz_value
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)
51 static INLINE int _bitset_ia32_inside_is_set(bitset_unit_t *unit, unsigned bit)
54 __asm__("btl %2, %1\n\t"
58 : "m" (*unit), "Ir" (bit)
63 static INLINE unsigned _bitset_ia32_inside_nlz(bitset_unit_t *unit)
66 __asm__("bsrl %1, %0\n\t"
67 "mov $ffffffff, %2\n\t"
72 : "m" (*unit), "r" (tmp)
77 static INLINE unsigned _bitset_ia32_inside_ntz(bitset_unit_t *unit) {
79 __asm__("bsf l %1, %0\n\t"
83 : "m" (*unit), "r" (tmp)
88 static INLINE unsigned _bitset_ia32_inside_ntz_value(bitset_unit_t unit) {
90 __asm__("bsfl %1, %0\n\t"
94 : "r" (unit), "r" (tmp)
99 #if defined(__GNUC__) && defined(__SSE2__)
102 #include <xmmintrin.h>
105 #undef _bitset_overall_size
106 #undef _bitset_data_ptr
108 #undef _BITSET_BINOP_UNITS_INC
110 #undef _bitset_inside_binop_and
111 #undef _bitset_inside_binop_andnot
112 #undef _bitset_inside_binop_or
113 #undef _bitset_inside_binop_xor
115 #define _bitset_units(highest_bit) (round_up2(highest_bit, 128) / BS_UNIT_SIZE_BITS)
117 #define _bitset_overall_size(bitset_base_size,highest_bit) \
118 ((bitset_base_size) + 16 + _bitset_units(highest_bit) * BS_UNIT_SIZE)
120 #define _bitset_data_ptr(data,bitset_base_size,highest_bit) \
121 _bitset_sse_data_ptr(data, bitset_base_size, highest_bit)
123 static INLINE bitset_unit_t *_bitset_sse_data_ptr(void *data, size_t bitset_base_size,
124 bitset_pos_t highest_bit)
129 diff = (units - (char *) 0) + bitset_base_size;
130 diff = round_up2(diff, 16);
131 units = (char *) 0 + diff;
132 return (bitset_unit_t *) units;
135 #define _BITSET_BINOP_UNITS_INC 4
136 #define _bitset_inside_binop_and(tgt,src) _bitset_sse_inside_binop_and(tgt,src)
137 #define _bitset_inside_binop_andnot(tgt,src) _bitset_sse_inside_binop_andnot(tgt,src)
138 #define _bitset_inside_binop_or(tgt,src) _bitset_sse_inside_binop_or(tgt,src)
139 #define _bitset_inside_binop_xor(tgt,src) _bitset_sse_inside_binop_xor(tgt,src)
141 #define _BITSET_SSE_BINOP(name) \
142 static INLINE void _bitset_sse_inside_binop_ ## name(bitset_unit_t *tgt, bitset_unit_t *src) \
144 __m128i src_op = _mm_load_si128((__m128i *) src); \
145 __m128i tgt_op = _mm_load_si128((__m128i *) tgt); \
146 __m128i res = _mm_ ## name ## _si128(tgt_op, src_op); \
147 _mm_store_si128((void *) tgt, res); \
151 static INLINE void _bitset_sse_inside_binop_with_zero_and(bitset_unit_t *tgt)
159 static INLINE void _bitset_sse_inside_binop_andnot(bitset_unit_t *tgt, bitset_unit_t *src)
161 __m128i src_op = _mm_load_si128((void *) src);
162 __m128i tgt_op = _mm_load_si128((void *) tgt);
163 __m128i res = _mm_andnot_si128(src_op, tgt_op);
164 _mm_store_si128((__m128i *) tgt, res);
167 _BITSET_SSE_BINOP(and)
168 _BITSET_SSE_BINOP(or)
169 _BITSET_SSE_BINOP(xor)