docs: bpfc: document number input format
[netsniff-ng.git] / src / built_in.h
blob8c12c5e52c655ae7a046029c22d0c773296fd3f9
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009-2012 Daniel Borkmann.
5 * Subject to the GPL, version 2.
6 */
8 #ifndef BUILT_IN_H
9 #define BUILT_IN_H
11 #include <linux/if_packet.h>
12 #include <assert.h>
13 #include <endian.h>
14 #include <byteswap.h>
15 #include <stdint.h>
17 typedef uint64_t u64;
18 typedef uint32_t u32;
19 typedef uint16_t u16;
20 typedef uint8_t u8;
22 /* /sys/devices/system/cpu/cpuX/cache/indexX/coherency_line_size */
24 #if defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || \
25 defined(_M_X64) || defined(__amd64)
26 # define CO_IN_CACHE_SHIFT 7
27 #elif defined(__i386__) || defined(__x86__) || defined(__X86__) || \
28 defined(_M_IX86) || defined(__i386)
29 # define CO_IN_CACHE_SHIFT 7
30 #elif defined(__ia64__) || defined(__IA64__) || defined(__M_IA64)
31 # define CO_IN_CACHE_SHIFT 6
32 #elif defined(__SPU__)
33 # define CO_IN_CACHE_SHIFT 7
34 #elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
35 defined(_ARCH_PPC64)
36 # define CO_IN_CACHE_SHIFT 8
37 #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || \
38 defined(_ARCH_PPC)
39 # define CO_IN_CACHE_SHIFT 7
40 #elif defined(__sparcv9__) || defined(__sparcv9)
41 # define CO_IN_CACHE_SHIFT 6
42 #elif defined(__sparc_v8__)
43 # define CO_IN_CACHE_SHIFT 5
44 #elif defined(__sparc__) || defined(__sparc)
45 # define CO_IN_CACHE_SHIFT 5
46 #elif defined(__ARM_EABI__)
47 # define CO_IN_CACHE_SHIFT 5
48 #elif defined(__arm__)
49 # define CO_IN_CACHE_SHIFT 5
50 #elif defined(__mips__) || defined(__mips) || defined(__MIPS__)
51 # if defined(_ABIO32)
52 # define CO_IN_CACHE_SHIFT 5
53 # elif defined(_ABIN32)
54 # define CO_IN_CACHE_SHIFT 5
55 # else
56 # define CO_IN_CACHE_SHIFT 6
57 # endif
58 #else
59 # define CO_IN_CACHE_SHIFT 5
60 #endif
62 #ifndef CO_CACHE_LINE_SIZE
63 # define CO_CACHE_LINE_SIZE (1 << CO_IN_CACHE_SHIFT)
64 #endif
66 #ifndef __aligned_16
67 # define __aligned_16 __attribute__((aligned(16)))
68 #endif
70 #ifndef __cacheline_aligned
71 # define __cacheline_aligned __attribute__((aligned(CO_CACHE_LINE_SIZE)))
72 #endif
74 #ifndef __aligned_tpacket
75 # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
76 #endif
78 #ifndef __packed
79 # define __packed __attribute__((packed))
80 #endif
82 #ifndef round_up
83 # define round_up(x, alignment) (((x) + (alignment) - 1) & ~((alignment) - 1))
84 #endif
86 #ifndef round_up_cacheline
87 # define round_up_cacheline(x) round_up((x), CO_CACHE_LINE_SIZE)
88 #endif
90 #ifndef likely
91 # define likely(x) __builtin_expect(!!(x), 1)
92 #endif
94 #ifndef unlikely
95 # define unlikely(x) __builtin_expect(!!(x), 0)
96 #endif
98 #ifndef constant
99 # define constant(x) __builtin_constant_p(x)
100 #endif
102 #ifndef prefetch_rd_hi
103 # define prefetch_rd_hi(addr) __builtin_prefetch(addr, 0, 3)
104 #endif
106 #ifndef prefetch_rd_lo
107 # define prefetch_rd_lo(addr) __builtin_prefetch(addr, 0, 0)
108 #endif
110 #ifndef prefetch_wr_hi
111 # define prefetch_wr_hi(addr) __builtin_prefetch(addr, 1, 3)
112 #endif
114 #ifndef prefetch_wr_lo
115 # define prefetch_wr_lo(addr) __builtin_prefetch(addr, 1, 0)
116 #endif
118 #ifndef fmemset
119 # define fmemset __builtin_memset
120 #endif
122 #ifndef fmemcpy
123 # define fmemcpy __builtin_memcpy
124 #endif
126 #ifndef atomic_cmp_swp
127 # define atomic_cmp_swp __sync_val_compare_and_swap
128 #endif
130 #ifndef __deprecated
131 # define __deprecated /* unimplemented */
132 #endif
134 #ifndef EXPORT_SYMBOL
135 # define EXPORT_SYMBOL(x) /* empty, just for readability */
136 #endif
138 #ifndef unreachable
139 # define unreachable() do { } while (1)
140 #endif
142 #ifndef __read_mostly
143 # define __read_mostly __attribute__((__section__(".data.read_mostly")))
144 #endif
146 #ifndef __unused
147 # define __unused __attribute__ ((__unused__))
148 #endif
150 #ifndef noinline
151 # define noinline __attribute__((noinline))
152 #endif
154 #ifndef __always_inline
155 # define __always_inline inline
156 #endif
158 #ifndef __hidden
159 # define __hidden __attribute__((visibility("hidden")))
160 #endif
162 #ifndef __pure
163 # define __pure __attribute__ ((pure))
164 #endif
166 #ifndef force_cast
167 # define force_cast(type, arg) ((type) (arg))
168 #endif
170 #ifndef access_once
171 # define access_once(x) (*(volatile typeof(x) *) &(x))
172 #endif
174 #ifndef max
175 # define max(a, b) \
176 ({ \
177 typeof (a) _a = (a); \
178 typeof (b) _b = (b); \
179 _a > _b ? _a : _b; \
181 #endif /* max */
183 #ifndef min
184 # define min(a, b) \
185 ({ \
186 typeof (a) _a = (a); \
187 typeof (b) _b = (b); \
188 _a < _b ? _a : _b; \
190 #endif /* min */
192 #ifndef ispow2
193 # define ispow2(x) ({ !!((x) && !((x) & ((x) - 1))); })
194 #endif
196 #ifndef offsetof
197 # define offsetof(type, member) ((size_t) &((type *) 0)->member)
198 #endif
200 #ifndef container_of
201 # define container_of(ptr, type, member) \
202 ({ \
203 const typeof(((type *) 0)->member) * __mptr = (ptr); \
204 (type *) ((char *) __mptr - offsetof(type, member)); \
206 #endif
208 #ifndef array_size
209 # define array_size(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
210 #endif
212 #ifndef __must_be_array
213 # define __must_be_array(x) \
214 build_bug_on_zero(__builtin_types_compatible_p(typeof(x), \
215 typeof(&x[0])))
216 #endif
218 #ifndef build_bug_on_zero
219 # define build_bug_on_zero(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
220 #endif
222 #ifndef bug_on
223 # define bug_on(cond) assert(!(cond))
224 #endif
226 #ifndef bug
227 # define bug assert(0)
228 #endif
230 #define PAGE_SIZE (getpagesize())
231 #define PAGE_MASK (~(PAGE_SIZE - 1))
232 #define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)
234 #if __BYTE_ORDER == __LITTLE_ENDIAN
235 static inline uint64_t htonll(uint64_t x)
237 return bswap_64(x);
240 static inline uint64_t ntohll(uint64_t x)
242 return bswap_64(x);
244 #elif __BYTE_ORDER == __BIG_ENDIAN
245 static inline uint64_t htonll(uint64_t x)
247 return x;
250 static inline uint64_t ntohll(uint64_t x)
252 return x;
254 #else
255 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
256 #endif
258 #if __BYTE_ORDER == __LITTLE_ENDIAN
259 static inline u16 cpu_to_be16(u16 val)
261 return bswap_16(val);
264 static inline u32 cpu_to_be32(u32 val)
266 return bswap_32(val);
269 static inline u64 cpu_to_be64(u64 val)
271 return bswap_64(val);
274 static inline u16 cpu_to_le16(u16 val)
276 return val;
279 static inline u32 cpu_to_le32(u32 val)
281 return val;
284 static inline u64 cpu_to_le64(u64 val)
286 return val;
288 #elif __BYTE_ORDER == __BIG_ENDIAN
289 static inline u16 cpu_to_be16(u16 val)
291 return val;
294 static inline u32 cpu_to_be32(u32 val)
296 return val;
299 static inline u64 cpu_to_be64(u64 val)
301 return val;
304 static inline u16 cpu_to_le16(u16 val)
306 return bswap_16(val);
309 static inline u32 cpu_to_le32(u32 val)
311 return bswap_32(val);
314 static inline u64 cpu_to_le64(u64 val)
316 return bswap_64(val);
318 #else
319 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
320 #endif
322 #define le64_to_cpu cpu_to_le64
323 #define le32_to_cpu cpu_to_le32
324 #define le16_to_cpu cpu_to_le16
325 #define be64_to_cpu cpu_to_be64
326 #define be32_to_cpu cpu_to_be32
327 #define be16_to_cpu cpu_to_be16
329 #undef memset
330 #undef memcpy
332 #define memset fmemset
333 #define memcpy fmemcpy
335 #endif /* BUILT_IN_H */