L4Re Operating System Framework
Interface and Usage Documentation
•All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
bitops_arch.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2000-2009 Technische Universität Dresden (Germany)
3 * Copyright (C) 2016, 2022, 2024 Kernkonzept GmbH. All rights reserved.
4 * Author(s): Lars Reuther <reuther@os.inf.tu-dresden.de>
5 * Frank Mehnert <frank.mehnert@kernkonzept.com>
6 *
7 * License: see LICENSE.spdx (in this directory or the directories above)
8 */
9
16#pragma once
17
19
20/* set bit */
21#define __L4UTIL_BITOPS_HAVE_ARCH_SET_BIT
22L4_INLINE void
23l4util_set_bit(int b, volatile l4_umword_t * dest)
24{
25 __asm__ __volatile__
26 (
27 "lock; btsl %1,%0 \n\t"
28 :
29 :
30 "m" (*dest), /* 0 mem, destination operand */
31 "Ir" (b) /* 1, bit number */
32 :
33 "memory", "cc"
34 );
35}
36
37/* clear bit */
38#define __L4UTIL_BITOPS_HAVE_ARCH_CLEAR_BIT
39L4_INLINE void
40l4util_clear_bit(int b, volatile l4_umword_t * dest)
41{
42 __asm__ __volatile__
43 (
44 "lock; btrl %1,%0 \n\t"
45 :
46 :
47 "m" (*dest), /* 0 mem, destination operand */
48 "Ir" (b) /* 1, bit number */
49 :
50 "memory", "cc"
51 );
52}
53
54/* change bit */
55#define __L4UTIL_BITOPS_HAVE_ARCH_COMPLEMENT_BIT
56L4_INLINE void
57l4util_complement_bit(int b, volatile l4_umword_t * dest)
58{
59 __asm__ __volatile__
60 (
61 "lock; btcl %1,%0 \n\t"
62 :
63 :
64 "m" (*dest), /* 0 mem, destination operand */
65 "Ir" (b) /* 1, bit number */
66 :
67 "memory", "cc"
68 );
69}
70
71/* test bit */
72#define __L4UTIL_BITOPS_HAVE_ARCH_TEST_BIT
73L4_INLINE int
74l4util_test_bit(int b, const volatile l4_umword_t * dest)
75{
76 l4_int8_t bit;
77
78 __asm__ __volatile__
79 (
80 "btl %2,%1 \n\t"
81 :
82 "=@ccc" (bit) /* 0, old bit value */
83 :
84 "m" (*dest), /* 1 mem, destination operand */
85 "Ir" (b) /* 2, bit number */
86 :
87 "memory"
88 );
89
90 return bit;
91}
92
93/* bit test and set */
94#define __L4UTIL_BITOPS_HAVE_ARCH_BIT_TEST_AND_SET
95L4_INLINE int
96l4util_bts(int b, volatile l4_umword_t * dest)
97{
98 l4_int8_t bit;
99
100 __asm__ __volatile__
101 (
102 "lock; btsl %2,%1 \n\t"
103 :
104 "=@ccc" (bit) /* 0, old bit value */
105 :
106 "m" (*dest), /* 1 mem, destination operand */
107 "Ir" (b) /* 2, bit number */
108 :
109 "memory"
110 );
111
112 return bit;
113}
114
115/* bit test and reset */
116#define __L4UTIL_BITOPS_HAVE_ARCH_BIT_TEST_AND_RESET
117L4_INLINE int
118l4util_btr(int b, volatile l4_umword_t * dest)
119{
120 l4_int8_t bit;
121
122 __asm__ __volatile__
123 (
124 "lock; btrl %2,%1 \n\t"
125 :
126 "=@ccc" (bit) /* 0, old bit value */
127 :
128 "m" (*dest), /* 1 mem, destination operand */
129 "Ir" (b) /* 2, bit number */
130 :
131 "memory"
132 );
133
134 return bit;
135}
136
137/* bit test and complement */
138#define __L4UTIL_BITOPS_HAVE_ARCH_BIT_TEST_AND_COMPLEMENT
139L4_INLINE int
140l4util_btc(int b, volatile l4_umword_t * dest)
141{
142 l4_int8_t bit;
143
144 __asm__ __volatile__
145 (
146 "lock; btcl %2,%1 \n\t"
147 :
148 "=@ccc" (bit) /* 0, old bit value */
149 :
150 "m" (*dest), /* 1 mem, destination operand */
151 "Ir" (b) /* 2, bit number */
152 :
153 "memory"
154 );
155
156 return bit;
157}
158
159/* bit scan reverse */
160#define __L4UTIL_BITOPS_HAVE_ARCH_BIT_SCAN_REVERSE
161L4_INLINE int
162l4util_bsr(l4_umword_t word)
163{
164 int tmp;
165
166 if (L4_UNLIKELY(word == 0))
167 return -1;
168
169 __asm__ __volatile__
170 (
171 "bsrl %1,%0 \n\t"
172 :
173 "=r" (tmp) /* 0, index of most significant set bit */
174 :
175 "r" (word) /* 1, argument */
176 );
177
178 return tmp;
179}
180
181/* bit scan forward */
182#define __L4UTIL_BITOPS_HAVE_ARCH_BIT_SCAN_FORWARD
183L4_INLINE int
184l4util_bsf(l4_umword_t word)
185{
186 int tmp;
187
188 if (L4_UNLIKELY(word == 0))
189 return -1;
190
191 __asm__ __volatile__
192 (
193 "bsfl %1,%0 \n\t"
194 :
195 "=r" (tmp) /* 0, index of least significant set bit */
196 :
197 "r" (word) /* 1, argument */
198 );
199
200 return tmp;
201}
202
203#define __L4UTIL_BITOPS_HAVE_ARCH_FIND_FIRST_SET_BIT
204L4_INLINE int
205l4util_find_first_set_bit(const void * dest, l4_size_t size)
206{
207 l4_mword_t dummy0, dummy1, res;
208
209 __asm__ __volatile__
210 (
211 "repe; scasl \n\t"
212 "jz 1f \n\t"
213 "lea -4(%%edi),%%edi \n\t"
214 "bsf (%%edi),%%eax \n"
215 "1: \n\t"
216 "sub %%esi,%%edi \n\t"
217 "shl $3,%%edi \n\t"
218 "add %%edi,%%eax \n\t"
219 :
220 "=a" (res), "=c" (dummy0), "=D" (dummy1)
221 :
222 "a" (0), "c" ((size + 31) >> 5), "D" (dest), "S" (dest)
223 :
224 "cc", "memory");
225
226 return res;
227}
228
229#define __L4UTIL_BITOPS_HAVE_ARCH_FIND_FIRST_ZERO_BIT
230L4_INLINE int
231l4util_find_first_zero_bit(const void * dest, l4_size_t size)
232{
233 l4_mword_t dummy0, dummy1, dummy2, res;
234
235 if (!size)
236 return 0;
237
238 __asm__ __volatile__
239 (
240 "repe; scasl \n\t"
241 "je 1f \n\t"
242 "xor -4(%%edi),%%eax \n\t"
243 "sub $4,%%edi \n\t"
244 "bsf %%eax,%%edx \n"
245 "1: \n\t"
246 "sub %%esi,%%edi \n\t"
247 "shl $3,%%edi \n\t"
248 "add %%edi,%%edx \n\t"
249 :
250 "=a" (dummy0), "=c" (dummy1), "=d" (res), "=D" (dummy2)
251 :
252 "a" (~0UL), "c" ((size + 31) >> 5), "d" (0), "D" (dest), "S" (dest)
253 :
254 "cc", "memory");
255
256 return res;
257}
258
unsigned int l4_size_t
Unsigned size type.
Definition l4int.h:24
unsigned long l4_umword_t
Unsigned machine word.
Definition l4int.h:40
signed long l4_mword_t
Signed machine word.
Definition l4int.h:37
signed char l4_int8_t
Signed 8bit value.
Definition l4int.h:24
#define __END_DECLS
End section with C types and functions.
Definition compiler.h:167
#define L4_UNLIKELY(x)
Expression is unlikely to execute.
Definition compiler.h:275
#define L4_INLINE
L4 Inline function attribute.
Definition compiler.h:51
#define __BEGIN_DECLS
Start section with C types and functions.
Definition compiler.h:164