Add inline fls() function for linux
[project/netifd.git] / netifd.h
1 #ifndef __NETIFD_H
2 #define __NETIFD_H
3
4 #include <sys/socket.h>
5 #include <net/if.h>
6
7 #include <stdbool.h>
8 #include <stdio.h>
9
10 #include <libubox/list.h>
11 #include <libubox/uloop.h>
12
13 #include <libubus.h>
14 #include <uci.h>
15
16 #ifdef DEBUG
17 #define DPRINTF(format, ...) fprintf(stderr, "%s(%d): " format, __func__, __LINE__, ## __VA_ARGS__)
18 #else
19 #define DPRINTF(...) do {} while(0)
20 #endif
21
22 #define __init __attribute__((constructor))
23
24 struct device;
25 struct interface;
26
27 extern struct uci_context *uci_ctx;
28 extern bool config_init;
29
30 int avl_strcmp(const void *k1, const void *k2, void *ptr);
31 void config_init_interfaces(const char *name);
32
33 #ifdef __linux__
34 static inline int fls(int x)
35 {
36 int r = 32;
37
38 if (!x)
39 return 0;
40 if (!(x & 0xffff0000u)) {
41 x <<= 16;
42 r -= 16;
43 }
44 if (!(x & 0xff000000u)) {
45 x <<= 8;
46 r -= 8;
47 }
48 if (!(x & 0xf0000000u)) {
49 x <<= 4;
50 r -= 4;
51 }
52 if (!(x & 0xc0000000u)) {
53 x <<= 2;
54 r -= 2;
55 }
56 if (!(x & 0x80000000u)) {
57 x <<= 1;
58 r -= 1;
59 }
60 return r;
61 }
62 #endif
63
64 #endif