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>
25 * Get the uint64_t value for a specified bit set.
28 * The bit number in range of 0 to 63.
30 #define RTE_BIT64(nr) (UINT64_C(1) << (nr))
33 * Get the uint32_t value for a specified bit set.
36 * The bit number in range of 0 to 31.
38 #define RTE_BIT32(nr) (UINT32_C(1) << (nr))
40 /*------------------------ 32-bit relaxed operations ------------------------*/
44 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
46 * Get the target bit from a 32-bit value without memory ordering.
49 * The target bit to get.
51 * The address holding the bit.
56 static inline uint32_t
57 rte_bit_relaxed_get32(unsigned int nr, volatile uint32_t *addr)
61 uint32_t mask = UINT32_C(1) << nr;
62 return (*addr) & mask;
67 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
69 * Set the target bit in a 32-bit value to 1 without memory ordering.
72 * The target bit to set.
74 * The address holding the bit.
78 rte_bit_relaxed_set32(unsigned int nr, volatile uint32_t *addr)
82 uint32_t mask = RTE_BIT32(nr);
83 *addr = (*addr) | mask;
88 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
90 * Clear the target bit in a 32-bit value to 0 without memory ordering.
93 * The target bit to clear.
95 * The address holding the bit.
99 rte_bit_relaxed_clear32(unsigned int nr, volatile uint32_t *addr)
103 uint32_t mask = RTE_BIT32(nr);
104 *addr = (*addr) & (~mask);
109 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
111 * Return the original bit from a 32-bit value, then set it to 1 without
115 * The target bit to get and set.
117 * The address holding the bit.
122 static inline uint32_t
123 rte_bit_relaxed_test_and_set32(unsigned int nr, volatile uint32_t *addr)
127 uint32_t mask = RTE_BIT32(nr);
128 uint32_t val = *addr;
135 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
137 * Return the original bit from a 32-bit value, then clear it to 0 without
141 * The target bit to get and clear.
143 * The address holding the bit.
148 static inline uint32_t
149 rte_bit_relaxed_test_and_clear32(unsigned int nr, volatile uint32_t *addr)
153 uint32_t mask = RTE_BIT32(nr);
154 uint32_t val = *addr;
155 *addr = val & (~mask);
159 /*------------------------ 64-bit relaxed operations ------------------------*/
163 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
165 * Get the target bit from a 64-bit value without memory ordering.
168 * The target bit to get.
170 * The address holding the bit.
175 static inline uint64_t
176 rte_bit_relaxed_get64(unsigned int nr, volatile uint64_t *addr)
180 uint64_t mask = RTE_BIT64(nr);
181 return (*addr) & mask;
186 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
188 * Set the target bit in a 64-bit value to 1 without memory ordering.
191 * The target bit to set.
193 * The address holding the bit.
197 rte_bit_relaxed_set64(unsigned int nr, volatile uint64_t *addr)
201 uint64_t mask = RTE_BIT64(nr);
202 (*addr) = (*addr) | mask;
207 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
209 * Clear the target bit in a 64-bit value to 0 without memory ordering.
212 * The target bit to clear.
214 * The address holding the bit.
218 rte_bit_relaxed_clear64(unsigned int nr, volatile uint64_t *addr)
222 uint64_t mask = RTE_BIT64(nr);
223 *addr = (*addr) & (~mask);
228 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
230 * Return the original bit from a 64-bit value, then set it to 1 without
234 * The target bit to get and set.
236 * The address holding the bit.
241 static inline uint64_t
242 rte_bit_relaxed_test_and_set64(unsigned int nr, volatile uint64_t *addr)
246 uint64_t mask = RTE_BIT64(nr);
247 uint64_t val = *addr;
254 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
256 * Return the original bit from a 64-bit value, then clear it to 0 without
260 * The target bit to get and clear.
262 * The address holding the bit.
267 static inline uint64_t
268 rte_bit_relaxed_test_and_clear64(unsigned int nr, volatile uint64_t *addr)
272 uint64_t mask = RTE_BIT64(nr);
273 uint64_t val = *addr;
274 *addr = val & (~mask);
282 #endif /* _RTE_BITOPS_H_ */