Skip to content

Commit 3a6f9d1

Browse files
committed
Fix compile issues under Developer Studio 12.6
1 parent 410d8bc commit 3a6f9d1

File tree

21 files changed

+363
-76
lines changed

21 files changed

+363
-76
lines changed

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ if(CMAKE_VERSION VERSION_LESS 3.20)
6262
endif()
6363
include(CheckIncludeFile)
6464
include(CheckCCompilerFlag)
65+
include(CheckSymbolExists)
6566
include(GenerateExportHeader)
6667
include(CMakePackageConfigHelpers)
6768
include(GNUInstallDirs)
@@ -168,7 +169,8 @@ target_link_libraries(zone-bench PRIVATE zone)
168169
check_include_file(endian.h HAVE_ENDIAN_H)
169170

170171
check_include_file(unistd.h HAVE_UNISTD_H)
171-
if(NOT HAVE_UNISTD_H)
172+
check_symbol_exists(getopt unistd.h HAVE_GETOPT)
173+
if(NOT HAVE_UNISTD_H OR NOT HAVE_GETOPT)
172174
target_include_directories(
173175
zone-bench PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/compat>)
174176
target_sources(zone-bench PRIVATE compat/getopt.c)
@@ -222,7 +224,7 @@ foreach(match ${matches})
222224
set(prefix "${CMAKE_MATCH_1}")
223225
set(variable "${CMAKE_MATCH_2}")
224226
set(suffix "${CMAKE_MATCH_3}")
225-
if(NOT DEFINED ${variable} OR
227+
if(NOT DEFINED ${variable} OR NOT ${variable} OR
226228
${variable} MATCHES "^[Ff][Aa][Ll][Ss][Ee]$" OR
227229
${variable} MATCHES "^[Oo][Ff][Ff]")
228230
set(replace "${prefix}/* #undef ${variable} */${suffix}")

src/attributes.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
# define unlikely(params) (params)
2525

2626
#else // _MSC_VER
27+
#if defined __has_builtin
28+
# define has_builtin(params) __has_builtin(params)
29+
#else
30+
# define has_builtin(params) (0)
31+
#endif
32+
2733
# if (zone_has_attribute(always_inline) || zone_has_gnuc(3, 1)) && ! defined __NO_INLINE__
2834
// Compilation using GCC 4.2.1 without optimizations fails.
2935
// sorry, unimplemented: inlining failed in call to ...
@@ -52,7 +58,7 @@
5258
# define no_sanitize_undefined __attribute__((no_sanitize("undefined")))
5359
# elif zone_has_attribute(no_sanitize_undefined)
5460
// GCC 4.9.0 added the UndefinedBehaviorSanitizer (ubsan) and the
55-
// no_sanitize_undefined function attribute.
61+
// no_sanitize_undefined function attribute.
5662
# define no_sanitize_undefined
5763
# else
5864
# define no_sanitize_undefined

src/bench.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <stdio.h>
1111
#include <stdlib.h>
1212
#include <string.h>
13-
#if _WIN32
13+
#if !defined(HAVE_GETOPT)
1414
# include "getopt.h"
1515
#else
1616
# include <unistd.h>

src/fallback/bits.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,60 @@ static really_inline uint64_t leading_zeroes(uint64_t mask)
2929
else
3030
return 64;
3131
}
32+
3233
#else
34+
3335
static really_inline uint64_t trailing_zeroes(uint64_t mask)
3436
{
37+
#if has_builtin(__builtin_ctzll)
3538
return (uint64_t)__builtin_ctzll(mask);
39+
#else
40+
// Code by Kim Walish from https://www.chessprogramming.org/BitScan.
41+
// Distributed under CC BY-SA 3.0.
42+
static const uint64_t magic = 0x03f79d71b4cb0a89ull;
43+
const int magictable[64] = {
44+
0, 47, 1, 56, 48, 27, 2, 60,
45+
57, 49, 41, 37, 28, 16, 3, 61,
46+
54, 58, 35, 52, 50, 42, 21, 44,
47+
38, 32, 29, 23, 17, 11, 4, 62,
48+
46, 55, 26, 59, 40, 36, 15, 53,
49+
34, 51, 20, 43, 31, 22, 10, 45,
50+
25, 39, 14, 33, 19, 30, 9, 24,
51+
13, 18, 8, 12, 7, 6, 5, 63
52+
};
53+
54+
return magictable[((mask ^ (mask - 1)) * magic) >> 58];
55+
#endif
3656
}
3757

3858
static really_inline uint64_t leading_zeroes(uint64_t mask)
3959
{
60+
#if has_builtin(__builtin_clzll)
4061
return (uint64_t)__builtin_clzll(mask);
62+
#else
63+
// Code by Kim Walish from https://www.chessprogramming.org/BitScan.
64+
// Distributed under CC BY-SA 3.0.
65+
static const uint64_t magic = 0x03f79d71b4cb0a89ull;
66+
const int magictable[64] = {
67+
63, 16, 62, 7, 15, 36, 61, 3,
68+
6, 14, 22, 26, 35, 47, 60, 2,
69+
9, 5, 28, 11, 13, 21, 42, 19,
70+
25, 31, 34, 40, 46, 52, 59, 1,
71+
17, 8, 37, 4, 23, 27, 48, 10,
72+
29, 12, 43, 20, 32, 41, 53, 18,
73+
38, 24, 49, 30, 44, 33, 54, 39,
74+
50, 45, 55, 51, 56, 57, 58, 0
75+
};
76+
77+
mask |= mask >> 1;
78+
mask |= mask >> 2;
79+
mask |= mask >> 4;
80+
mask |= mask >> 8;
81+
mask |= mask >> 16;
82+
mask |= mask >> 32;
83+
84+
return magictable[(mask * magic) >> 58];
85+
#endif
4186
}
4287
#endif // _MSC_VER
4388
#endif // BITS_H

src/generic/base16.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,13 @@ static really_inline int base16_stream_decode(
173173
// Duff's device again:
174174
switch (st.bytes)
175175
{
176+
#if defined(__SUNPRO_C)
177+
#pragma error_messages(off, E_STATEMENT_NOT_REACHED)
178+
#endif
176179
for (;;)
180+
#if defined(__SUNPRO_C)
181+
#pragma error_messages(default, E_STATEMENT_NOT_REACHED)
182+
#endif
177183
{
178184
case 0:
179185
base16_dec_loop_generic_32(&s, &slen, &o, &olen);

src/generic/base64.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,13 @@ static really_inline int base64_stream_decode(
575575
// Duff's device again:
576576
switch (st.bytes)
577577
{
578+
#if defined(__SUNPRO_C)
579+
#pragma error_messages(off, E_STATEMENT_NOT_REACHED)
580+
#endif
578581
for (;;)
582+
#if defined(__SUNPRO_C)
583+
#pragma error_messages(default, E_STATEMENT_NOT_REACHED)
584+
#endif
579585
{
580586
case 0:
581587
dec_loop_generic_32(&s, &slen, &o, &olen);

src/generic/endian.h

Lines changed: 112 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,123 @@
8686
#include <sys/endian.h>
8787
#endif
8888

89-
#if !defined BYTE_ORDER
90-
#error "missing definition of BYTE_ORDER"
89+
#if !defined(LITTLE_ENDIAN)
90+
# if defined(__ORDER_LITTLE_ENDIAN__)
91+
# define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
92+
# else
93+
# define LITTLE_ENDIAN 1234
94+
# endif
9195
#endif
9296

93-
#if !defined LITTLE_ENDIAN
94-
#error "missing definition of LITTLE_ENDIAN"
97+
#if !defined(BIG_ENDIAN)
98+
# if defined(__ORDER_BIG_ENDIAN__)
99+
# define BIG_ENDIAN __ORDER_BIG_ENDIAN__
100+
# else
101+
# define BIG_ENDIAN 4321
102+
# endif
95103
#endif
96104

97-
#if !defined BIG_ENDIAN
98-
#error "missing definition of BIG_ENDIAN"
105+
#if !defined(BYTE_ORDER)
106+
# if defined(__BYTE_ORDER__)
107+
# define BYTE_ORDER __BYTE_ORDER__
108+
# elif defined(__BYTE_ORDER)
109+
# define BYTE_ORDER __BYTE_ORDER
110+
# elif defined(i386) || defined(__i386__) || defined(__i486__) || \
111+
defined(__i586__) || defined(__i686__) || \
112+
defined(__x86) || defined(__x86_64) || defined(__x86_64__) || \
113+
defined(__amd64) || defined(__amd64__)
114+
# define BYTE_ORDER LITTLE_ENDIAN
115+
# elif defined(sparc) || defined(__sparc) || defined(__sparc__) || \
116+
defined(POWERPC) || defined(mc68000) || defined(sel)
117+
# define BYTE_ORDER BIG_ENDIAN
118+
# else
119+
# error "missing definition of BYTE_ORDER"
120+
# endif
99121
#endif
122+
123+
static really_inline uint16_t bswap16(uint16_t x)
124+
{
125+
// Copied from src/common/lib/libc/gen/bswap16.c in NetBSD
126+
// Written by Manuel Bouyer <[email protected]>.
127+
// Public domain.
128+
return ((x << 8) & 0xff00) | ((x >> 8) & 0x00ff);
129+
}
130+
131+
static really_inline uint32_t bswap32(uint32_t x)
132+
{
133+
// Copied from src/common/lib/libc/gen/bswap32.c in NetBSD
134+
// Written by Manuel Bouyer <[email protected]>.
135+
// Public domain.
136+
return ( (x << 24) & 0xff000000 ) |
137+
( (x << 8) & 0x00ff0000 ) |
138+
( (x >> 8) & 0x0000ff00 ) |
139+
( (x >> 24) & 0x000000ff );
140+
}
141+
142+
static really_inline uint64_t bswap64(uint64_t x)
143+
{
144+
// Copied from src/common/lib/libc/gen/bswap64.c in NetBSD
145+
// Written by Manuel Bouyer <[email protected]>.
146+
// Public domain.
147+
return ( (x << 56) & 0xff00000000000000ull ) |
148+
( (x << 40) & 0x00ff000000000000ull ) |
149+
( (x << 24) & 0x0000ff0000000000ull ) |
150+
( (x << 8) & 0x000000ff00000000ull ) |
151+
( (x >> 8) & 0x00000000ff000000ull ) |
152+
( (x >> 24) & 0x0000000000ff0000ull ) |
153+
( (x >> 40) & 0x000000000000ff00ull ) |
154+
( (x >> 56) & 0x00000000000000ffull );
155+
}
156+
157+
# if BYTE_ORDER == LITTLE_ENDIAN
158+
# define htobe(bits, x) bswap ## bits((x))
159+
# define htole(bits, x) (x)
160+
# define betoh(bits, x) bswap ## bits((x))
161+
# define letoh(bits, x) (x)
162+
# else
163+
# define htobe(bits, x) (x)
164+
# define htole(bits, x) bswap ## bits((x))
165+
# define betoh(bits, x) (x)
166+
# define letoh(bits, x) bswap ## bits((x))
167+
# endif
168+
169+
# if !defined htobe16
170+
# define htobe16(x) htobe(16,(x))
171+
# endif
172+
# if !defined htobe32
173+
# define htobe32(x) htobe(32,(x))
174+
# endif
175+
# if !defined htobe64
176+
# define htobe64(x) htobe(64,(x))
177+
# endif
178+
# if !defined htole16
179+
# define htole16(x) htole(16,(x))
180+
# endif
181+
# if !defined htole32
182+
# define htole32(x) htole(32,(x))
183+
# endif
184+
# if !defined htole64
185+
# define htole64(x) htole(64,(x))
186+
# endif
187+
188+
# if !defined be16toh
189+
# define be16toh(x) betoh(16,(x))
190+
# endif
191+
# if !defined be32toh
192+
# define be32toh(x) betoh(32,(x))
193+
# endif
194+
# if !defined be64toh
195+
# define be64toh(x) betoh(64,(x))
196+
# endif
197+
# if !defined le16toh
198+
# define le16toh(x) letoh(16,(x))
199+
# endif
200+
# if !defined le32toh
201+
# define le32toh(x) letoh(32,(x))
202+
# endif
203+
# if !defined le64toh
204+
# define le64toh(x) letoh(64,(x))
205+
# endif
100206
#endif
101207

102208
#endif // ENDIAN_H

0 commit comments

Comments
 (0)