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