]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/tinycbor/compilersupport_p.h
1 /****************************************************************************
3 ** Copyright (C) 2017 Intel Corporation
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
12 ** The above copyright notice and this permission notice shall be included in
13 ** all copies or substantial portions of the Software.
15 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 ****************************************************************************/
25 #ifndef COMPILERSUPPORT_H
26 #define COMPILERSUPPORT_H
33 #ifndef _DEFAULT_SOURCE
34 # define _DEFAULT_SOURCE
47 #if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L || __cpp_static_assert >= 200410
48 # define cbor_static_assert(x) static_assert(x, #x)
49 #elif !defined(__cplusplus) && defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406) && (__STDC_VERSION__ > 199901L)
50 # define cbor_static_assert(x) _Static_assert(x, #x)
52 # define cbor_static_assert(x) ((void)sizeof(char[2*!!(x) - 1]))
54 #if __STDC_VERSION__ >= 199901L || defined(__cplusplus)
55 /* inline is a keyword */
57 /* use the definition from cbor.h */
58 # define inline CBOR_INLINE
62 # define cbor_assert(cond) do { if (!(cond)) unreachable(); } while (0)
64 # define cbor_assert(cond) assert(cond)
68 #define STRINGIFY(x) STRINGIFY2(x)
70 #define STRINGIFY2(x) #x
72 #if !defined(UINT32_MAX) || !defined(INT64_MAX)
73 /* C89? We can define UINT32_MAX portably, but not INT64_MAX */
74 # error "Your system has stdint.h but that doesn't define UINT32_MAX or INT64_MAX"
77 #ifndef DBL_DECIMAL_DIG
78 /* DBL_DECIMAL_DIG is C11 */
79 # define DBL_DECIMAL_DIG 17
81 #define DBL_DECIMAL_DIG_STR STRINGIFY(DBL_DECIMAL_DIG)
83 #if defined(__GNUC__) && defined(__i386__) && !defined(__iamcu__)
84 # define CBOR_INTERNAL_API_CC __attribute__((regparm(3)))
85 #elif defined(_MSC_VER) && defined(_M_IX86)
86 # define CBOR_INTERNAL_API_CC __fastcall
88 # define CBOR_INTERNAL_API_CC
92 # define __has_builtin(x) 0
95 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) || \
96 (__has_builtin(__builtin_bswap64) && __has_builtin(__builtin_bswap32))
97 # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
98 # define cbor_ntohll __builtin_bswap64
99 # define cbor_htonll __builtin_bswap64
100 # define cbor_ntohl __builtin_bswap32
101 # define cbor_htonl __builtin_bswap32
102 # ifdef __INTEL_COMPILER
103 # define cbor_ntohs _bswap16
104 # define cbor_htons _bswap16
105 # elif (__GNUC__ * 100 + __GNUC_MINOR__ >= 608) || __has_builtin(__builtin_bswap16)
106 # define cbor_ntohs __builtin_bswap16
107 # define cbor_htons __builtin_bswap16
109 # define cbor_ntohs(x) (((uint16_t)(x) >> 8) | ((uint16_t)(x) << 8))
110 # define cbor_htons cbor_ntohs
121 # include <sys/byteorder.h>
122 #elif defined(_MSC_VER)
123 /* MSVC, which implies Windows, which implies little-endian and sizeof(long) == 4 */
125 # define cbor_ntohll _byteswap_uint64
126 # define cbor_htonll _byteswap_uint64
127 # define cbor_ntohl _byteswap_ulong
128 # define cbor_htonl _byteswap_ulong
129 # define cbor_ntohs _byteswap_ushort
130 # define cbor_htons _byteswap_ushort
133 # include <arpa/inet.h>
134 # define cbor_ntohs ntohs
135 # define cbor_htons htons
138 # include <arpa/inet.h>
139 # define cbor_ntohl ntohl
140 # define cbor_htonl htonl
143 # define cbor_ntohll ntohll
144 # define cbor_htonll htonll
145 /* ntohll isn't usually defined */
147 # if (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \
148 (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN) || \
149 (defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN) || \
150 (defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)) || (defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)) || \
151 defined(__ARMEB__) || defined(__MIPSEB__) || defined(__s390__) || defined(__sparc__)
154 # elif (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || \
155 (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN) || \
156 (defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && BYTE_ORDER == LITTLE_ENDIAN) || \
157 defined(_LITTLE_ENDIAN) || defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || defined(__MIPSEL__) || \
158 defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__) || defined(__amd64)
159 # define ntohll(x) ((ntohl((uint32_t)(x)) * UINT64_C(0x100000000)) + (ntohl((x) >> 32)))
160 # define htonll ntohll
162 # error "Unable to determine byte order!"
169 # define CONST_CAST(t, v) const_cast<t>(v)
171 /* C-style const_cast without triggering a warning with -Wcast-qual */
172 # define CONST_CAST(t, v) (t)(uintptr_t)(v)
177 # define likely(x) __builtin_expect(!!(x), 1)
180 # define unlikely(x) __builtin_expect(!!(x), 0)
182 # define unreachable() __builtin_unreachable()
183 #elif defined(_MSC_VER)
184 # define likely(x) (x)
185 # define unlikely(x) (x)
186 # define unreachable() __assume(0)
188 # define likely(x) (x)
189 # define unlikely(x) (x)
190 # define unreachable() do {} while (0)
193 static inline bool add_check_overflow(size_t v1
, size_t v2
, size_t *r
)
195 #if ((defined(__GNUC__) && (__GNUC__ >= 5)) && !defined(__INTEL_COMPILER)) || __has_builtin(__builtin_add_overflow)
196 return __builtin_add_overflow(v1
, v2
, r
);
198 /* unsigned additions are well-defined */
204 #endif /* COMPILERSUPPORT_H */