NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
doca_bitfield.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES, ALL RIGHTS RESERVED.
3  *
4  * This software product is a proprietary product of NVIDIA CORPORATION &
5  * AFFILIATES (the "Company") and all right, title, and interest in and to the
6  * software product, including all associated intellectual property rights, are
7  * and shall remain exclusively with the Company.
8  *
9  * This software product is governed by the End User License Agreement
10  * provided with the software product.
11  *
12  */
13 
24 #ifndef DOCA_BITFIELD_H_
25 #define DOCA_BITFIELD_H_
26 
27 #ifdef __linux__
28 #include <arpa/inet.h>
29 #else
30 #include <stdlib.h>
31 #include <intrin.h>
32 #endif
33 
34 #include <doca_types.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 #ifdef __linux__
46 #define DOCA_SHIFT(_x) (__builtin_ffsll(_x) - 1)
47 
53 #define DOCA_HTOBE16(_x) htons(_x)
54 
60 #define DOCA_HTOBE32(_x) htonl(_x)
61 
67 #define DOCA_BETOH16(_x) ntohs(_x)
68 
74 #define DOCA_BETOH32(_x) ntohl(_x)
75 
76 #else /* __linux__ */
77 
84 static int __doca_builtin_ffsll(long long x)
85 {
86  unsigned long _f;
87 
88  if (_BitScanForward64(&_f, (unsigned long long)x)) {
89  return (int)(_f + 1);
90  }
91  return 0;
92 }
93 
100 #define DOCA_SHIFT(_x) (__doca_builtin_ffsll(_x) - 1)
101 
107 #define DOCA_HTOBE16(_x) _byteswap_ushort(_x)
108 
114 #define DOCA_HTOBE32(_x) _byteswap_ulong(_x)
115 
121 #define DOCA_BETOH16(_x) _byteswap_ushort(_x)
122 
128 #define DOCA_BETOH32(_x) _byteswap_ulong(_x)
129 
130 #endif
131 
139 #define DOCA_U8_GENMASK(_h, _l) ((UINT8_MAX - (UINT8_C(1) << (_l)) + 1) & (UINT8_MAX >> (8 - 1 - (_h))))
140 
149 #define DOCA_U8P_GENMASK(_h, _l, _p) \
150  do { \
151  *(uint8_t *)_p = DOCA_U8_GENMASK(_h, _l); \
152  } while (0)
153 
161 #define DOCA_BE16_GENMASK(_h, _l) \
162  (DOCA_HTOBE16((UINT16_MAX - (UINT16_C(1) << (_l)) + 1) & (UINT16_MAX >> (16 - 1 - (_h)))))
163 
172 #define DOCA_BE16P_GENMASK(_h, _l, _p) \
173  do { \
174  *(doca_be16_t *)_p = DOCA_BE16_GENMASK(_h, _l); \
175  } while (0)
176 
184 #define DOCA_BE32_GENMASK(_h, _l) \
185  (DOCA_HTOBE32((UINT32_MAX - (UINT32_C(1) << (_l)) + 1) & (UINT32_MAX >> (32 - 1 - (_h)))))
186 
195 #define DOCA_BE32P_GENMASK(_h, _l, _p) \
196  do { \
197  *(doca_be32_t *)_p = DOCA_BE32_GENMASK(_h, _l); \
198  } while (0)
199 
209 #define DOCA_U8_SET(_m, _v) ((_v << DOCA_SHIFT(_m)) & _m)
210 
220 #define DOCA_U8P_SET(_m, _v, _p) \
221  do { \
222  uint8_t _tmp = *(uint8_t *)_p; \
223 \
224  _tmp |= DOCA_U8_SET(_m, _v); \
225  *_p = _tmp; \
226  } while (0);
227 
237 #define DOCA_BE16_SET(_m, _v) ((DOCA_HTOBE16(_v << DOCA_SHIFT(DOCA_BETOH16(_m)))) & _m)
238 
248 #define DOCA_BE16P_SET(_m, _v, _p) \
249  do { \
250  doca_be16_t _tmp = *(doca_be16_t *)_p; \
251 \
252  _tmp |= DOCA_BE16_SET(_m, _v); \
253  *_p = _tmp; \
254  } while (0);
255 
265 #define DOCA_BE32_SET(_m, _v) ((DOCA_HTOBE32(_v << DOCA_SHIFT(DOCA_BETOH32(_m)))) & _m)
266 
276 #define DOCA_BE32P_SET(_m, _v, _p) \
277  do { \
278  doca_be32_t _tmp = *(doca_be32_t *)_p; \
279 \
280  _tmp |= DOCA_BE32_SET(_m, _v); \
281  *_p = _tmp; \
282  } while (0);
283 
293 #define DOCA_U8_GET(_m, _f) (((_m) & (_f)) >> DOCA_SHIFT((_m)))
294 
304 #define DOCA_BE16_GET(_m, _f) ((DOCA_BETOH16((_m) & (_f)) >> DOCA_SHIFT(DOCA_BETOH16(_m))))
305 
315 #define DOCA_BE32_GET(_m, _f) ((DOCA_BETOH32((_m) & (_f)) >> DOCA_SHIFT(DOCA_BETOH32(_m))))
316 
317 #ifdef __cplusplus
318 }
319 #endif
320 
322 #endif /* DOCA_BITFIELD_H_ */
static int __doca_builtin_ffsll(long long x)
Definition: doca_bitfield.h:84