1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Arm Limited
12 * This file defines a family of APIs for bit operations
13 * without enforcing memory ordering.
17 #include <rte_debug.h>
18 #include <rte_compat.h>
21 * Get the uint64_t value for a specified bit set.
24 * The bit number in range of 0 to 63.
26 #define RTE_BIT64(nr) (UINT64_C(1) << (nr))
29 * Get the uint32_t value for a specified bit set.
32 * The bit number in range of 0 to 31.
34 #define RTE_BIT32(nr) (UINT32_C(1) << (nr))
36 /*------------------------ 32-bit relaxed operations ------------------------*/
40 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
42 * Get the target bit from a 32-bit value without memory ordering.
45 * The target bit to get.
47 * The address holding the bit.
52 static inline uint32_t
53 rte_bit_relaxed_get32(unsigned int nr, volatile uint32_t *addr)
57 uint32_t mask = UINT32_C(1) << nr;
58 return (*addr) & mask;
63 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
65 * Set the target bit in a 32-bit value to 1 without memory ordering.
68 * The target bit to set.
70 * The address holding the bit.
74 rte_bit_relaxed_set32(unsigned int nr, volatile uint32_t *addr)
78 uint32_t mask = RTE_BIT32(nr);
79 *addr = (*addr) | mask;
84 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
86 * Clear the target bit in a 32-bit value to 0 without memory ordering.
89 * The target bit to clear.
91 * The address holding the bit.
95 rte_bit_relaxed_clear32(unsigned int nr, volatile uint32_t *addr)
99 uint32_t mask = RTE_BIT32(nr);
100 *addr = (*addr) & (~mask);
105 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
107 * Return the original bit from a 32-bit value, then set it to 1 without
111 * The target bit to get and set.
113 * The address holding the bit.
118 static inline uint32_t
119 rte_bit_relaxed_test_and_set32(unsigned int nr, volatile uint32_t *addr)
123 uint32_t mask = RTE_BIT32(nr);
124 uint32_t val = *addr;
131 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
133 * Return the original bit from a 32-bit value, then clear it to 0 without
137 * The target bit to get and clear.
139 * The address holding the bit.
144 static inline uint32_t
145 rte_bit_relaxed_test_and_clear32(unsigned int nr, volatile uint32_t *addr)
149 uint32_t mask = RTE_BIT32(nr);
150 uint32_t val = *addr;
151 *addr = val & (~mask);
155 /*------------------------ 64-bit relaxed operations ------------------------*/
159 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
161 * Get the target bit from a 64-bit value without memory ordering.
164 * The target bit to get.
166 * The address holding the bit.
171 static inline uint64_t
172 rte_bit_relaxed_get64(unsigned int nr, volatile uint64_t *addr)
176 uint64_t mask = RTE_BIT64(nr);
177 return (*addr) & mask;
182 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
184 * Set the target bit in a 64-bit value to 1 without memory ordering.
187 * The target bit to set.
189 * The address holding the bit.
193 rte_bit_relaxed_set64(unsigned int nr, volatile uint64_t *addr)
197 uint64_t mask = RTE_BIT64(nr);
198 (*addr) = (*addr) | mask;
203 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
205 * Clear the target bit in a 64-bit value to 0 without memory ordering.
208 * The target bit to clear.
210 * The address holding the bit.
214 rte_bit_relaxed_clear64(unsigned int nr, volatile uint64_t *addr)
218 uint64_t mask = RTE_BIT64(nr);
219 *addr = (*addr) & (~mask);
224 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
226 * Return the original bit from a 64-bit value, then set it to 1 without
230 * The target bit to get and set.
232 * The address holding the bit.
237 static inline uint64_t
238 rte_bit_relaxed_test_and_set64(unsigned int nr, volatile uint64_t *addr)
242 uint64_t mask = RTE_BIT64(nr);
243 uint64_t val = *addr;
250 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
252 * Return the original bit from a 64-bit value, then clear it to 0 without
256 * The target bit to get and clear.
258 * The address holding the bit.
263 static inline uint64_t
264 rte_bit_relaxed_test_and_clear64(unsigned int nr, volatile uint64_t *addr)
268 uint64_t mask = RTE_BIT64(nr);
269 uint64_t val = *addr;
270 *addr = val & (~mask);
274 #endif /* _RTE_BITOPS_H_ */