cmake: use extra compiler warnings only on gcc6+
[project/fwtool.git] / utils.h
1 /*
2 * utils - misc libubox utility functions
3 *
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #ifndef __LIBUBOX_UTILS_H
20 #define __LIBUBOX_UTILS_H
21
22 #include <sys/types.h>
23 #include <stdint.h>
24 #include <stdbool.h>
25
26 #ifndef ARRAY_SIZE
27 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
28 #endif
29
30 #ifdef __GNUC__
31 #define _GNUC_MIN_VER(maj, min) (((__GNUC__ << 8) + __GNUC_MINOR__) >= (((maj) << 8) + (min)))
32 #else
33 #define _GNUC_MIN_VER(maj, min) 0
34 #endif
35
36 #if defined(__linux__) || defined(__CYGWIN__)
37 #include <byteswap.h>
38 #include <endian.h>
39
40 #elif defined(__APPLE__)
41 #include <machine/endian.h>
42 #include <machine/byte_order.h>
43 #define bswap_32(x) OSSwapInt32(x)
44 #define bswap_64(x) OSSwapInt64(x)
45 #elif defined(__FreeBSD__)
46 #include <sys/endian.h>
47 #define bswap_32(x) bswap32(x)
48 #define bswap_64(x) bswap64(x)
49 #else
50 #include <machine/endian.h>
51 #define bswap_32(x) swap32(x)
52 #define bswap_64(x) swap64(x)
53 #endif
54
55 #ifndef __BYTE_ORDER
56 #define __BYTE_ORDER BYTE_ORDER
57 #endif
58 #ifndef __BIG_ENDIAN
59 #define __BIG_ENDIAN BIG_ENDIAN
60 #endif
61 #ifndef __LITTLE_ENDIAN
62 #define __LITTLE_ENDIAN LITTLE_ENDIAN
63 #endif
64
65 static inline uint16_t __u_bswap16(uint16_t val)
66 {
67 return ((val >> 8) & 0xffu) | ((val & 0xffu) << 8);
68 }
69
70 #if _GNUC_MIN_VER(4, 2)
71 #define __u_bswap32(x) __builtin_bswap32(x)
72 #define __u_bswap64(x) __builtin_bswap64(x)
73 #else
74 #define __u_bswap32(x) bswap_32(x)
75 #define __u_bswap64(x) bswap_64(x)
76 #endif
77
78 #if __BYTE_ORDER == __LITTLE_ENDIAN
79
80 #define cpu_to_be64(x) __u_bswap64(x)
81 #define cpu_to_be32(x) __u_bswap32(x)
82 #define cpu_to_be16(x) __u_bswap16((uint16_t) (x))
83
84 #define be64_to_cpu(x) __u_bswap64(x)
85 #define be32_to_cpu(x) __u_bswap32(x)
86 #define be16_to_cpu(x) __u_bswap16((uint16_t) (x))
87
88 #define cpu_to_le64(x) (x)
89 #define cpu_to_le32(x) (x)
90 #define cpu_to_le16(x) (x)
91
92 #define le64_to_cpu(x) (x)
93 #define le32_to_cpu(x) (x)
94 #define le16_to_cpu(x) (x)
95
96 #else /* __BYTE_ORDER == __LITTLE_ENDIAN */
97
98 #define cpu_to_le64(x) __u_bswap64(x)
99 #define cpu_to_le32(x) __u_bswap32(x)
100 #define cpu_to_le16(x) __u_bswap16((uint16_t) (x))
101
102 #define le64_to_cpu(x) __u_bswap64(x)
103 #define le32_to_cpu(x) __u_bswap32(x)
104 #define le16_to_cpu(x) __u_bswap16((uint16_t) (x))
105
106 #define cpu_to_be64(x) (x)
107 #define cpu_to_be32(x) (x)
108 #define cpu_to_be16(x) (x)
109
110 #define be64_to_cpu(x) (x)
111 #define be32_to_cpu(x) (x)
112 #define be16_to_cpu(x) (x)
113
114 #endif
115
116 #endif