Skip to content

Commit

Permalink
Fix compile issues under Developer Studio 12.6
Browse files Browse the repository at this point in the history
  • Loading branch information
k0ekk0ek committed Jul 1, 2024
1 parent 410d8bc commit 3a6f9d1
Show file tree
Hide file tree
Showing 21 changed files with 363 additions and 76 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ if(CMAKE_VERSION VERSION_LESS 3.20)
endif()
include(CheckIncludeFile)
include(CheckCCompilerFlag)
include(CheckSymbolExists)
include(GenerateExportHeader)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
Expand Down Expand Up @@ -168,7 +169,8 @@ target_link_libraries(zone-bench PRIVATE zone)
check_include_file(endian.h HAVE_ENDIAN_H)

check_include_file(unistd.h HAVE_UNISTD_H)
if(NOT HAVE_UNISTD_H)
check_symbol_exists(getopt unistd.h HAVE_GETOPT)
if(NOT HAVE_UNISTD_H OR NOT HAVE_GETOPT)
target_include_directories(
zone-bench PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/compat>)
target_sources(zone-bench PRIVATE compat/getopt.c)
Expand Down Expand Up @@ -222,7 +224,7 @@ foreach(match ${matches})
set(prefix "${CMAKE_MATCH_1}")
set(variable "${CMAKE_MATCH_2}")
set(suffix "${CMAKE_MATCH_3}")
if(NOT DEFINED ${variable} OR
if(NOT DEFINED ${variable} OR NOT ${variable} OR
${variable} MATCHES "^[Ff][Aa][Ll][Ss][Ee]$" OR
${variable} MATCHES "^[Oo][Ff][Ff]")
set(replace "${prefix}/* #undef ${variable} */${suffix}")
Expand Down
8 changes: 7 additions & 1 deletion src/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
# define unlikely(params) (params)

#else // _MSC_VER
#if defined __has_builtin
# define has_builtin(params) __has_builtin(params)
#else
# define has_builtin(params) (0)
#endif

# if (zone_has_attribute(always_inline) || zone_has_gnuc(3, 1)) && ! defined __NO_INLINE__
// Compilation using GCC 4.2.1 without optimizations fails.
// sorry, unimplemented: inlining failed in call to ...
Expand Down Expand Up @@ -52,7 +58,7 @@
# define no_sanitize_undefined __attribute__((no_sanitize("undefined")))
# elif zone_has_attribute(no_sanitize_undefined)
// GCC 4.9.0 added the UndefinedBehaviorSanitizer (ubsan) and the
// no_sanitize_undefined function attribute.
// no_sanitize_undefined function attribute.
# define no_sanitize_undefined
# else
# define no_sanitize_undefined
Expand Down
2 changes: 1 addition & 1 deletion src/bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if _WIN32
#if !defined(HAVE_GETOPT)
# include "getopt.h"
#else
# include <unistd.h>
Expand Down
45 changes: 45 additions & 0 deletions src/fallback/bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,60 @@ static really_inline uint64_t leading_zeroes(uint64_t mask)
else
return 64;
}

#else

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

return magictable[((mask ^ (mask - 1)) * magic) >> 58];
#endif
}

static really_inline uint64_t leading_zeroes(uint64_t mask)
{
#if has_builtin(__builtin_clzll)
return (uint64_t)__builtin_clzll(mask);
#else
// Code by Kim Walish from https://www.chessprogramming.org/BitScan.
// Distributed under CC BY-SA 3.0.
static const uint64_t magic = 0x03f79d71b4cb0a89ull;
const int magictable[64] = {
63, 16, 62, 7, 15, 36, 61, 3,
6, 14, 22, 26, 35, 47, 60, 2,
9, 5, 28, 11, 13, 21, 42, 19,
25, 31, 34, 40, 46, 52, 59, 1,
17, 8, 37, 4, 23, 27, 48, 10,
29, 12, 43, 20, 32, 41, 53, 18,
38, 24, 49, 30, 44, 33, 54, 39,
50, 45, 55, 51, 56, 57, 58, 0
};

mask |= mask >> 1;
mask |= mask >> 2;
mask |= mask >> 4;
mask |= mask >> 8;
mask |= mask >> 16;
mask |= mask >> 32;

return magictable[(mask * magic) >> 58];
#endif
}
#endif // _MSC_VER
#endif // BITS_H
6 changes: 6 additions & 0 deletions src/generic/base16.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,13 @@ static really_inline int base16_stream_decode(
// Duff's device again:
switch (st.bytes)
{
#if defined(__SUNPRO_C)
#pragma error_messages(off, E_STATEMENT_NOT_REACHED)
#endif
for (;;)
#if defined(__SUNPRO_C)
#pragma error_messages(default, E_STATEMENT_NOT_REACHED)
#endif
{
case 0:
base16_dec_loop_generic_32(&s, &slen, &o, &olen);
Expand Down
6 changes: 6 additions & 0 deletions src/generic/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,13 @@ static really_inline int base64_stream_decode(
// Duff's device again:
switch (st.bytes)
{
#if defined(__SUNPRO_C)
#pragma error_messages(off, E_STATEMENT_NOT_REACHED)
#endif
for (;;)
#if defined(__SUNPRO_C)
#pragma error_messages(default, E_STATEMENT_NOT_REACHED)
#endif
{
case 0:
dec_loop_generic_32(&s, &slen, &o, &olen);
Expand Down
118 changes: 112 additions & 6 deletions src/generic/endian.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,123 @@
#include <sys/endian.h>
#endif

#if !defined BYTE_ORDER
#error "missing definition of BYTE_ORDER"
#if !defined(LITTLE_ENDIAN)
# if defined(__ORDER_LITTLE_ENDIAN__)
# define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
# else
# define LITTLE_ENDIAN 1234
# endif
#endif

#if !defined LITTLE_ENDIAN
#error "missing definition of LITTLE_ENDIAN"
#if !defined(BIG_ENDIAN)
# if defined(__ORDER_BIG_ENDIAN__)
# define BIG_ENDIAN __ORDER_BIG_ENDIAN__
# else
# define BIG_ENDIAN 4321
# endif
#endif

#if !defined BIG_ENDIAN
#error "missing definition of BIG_ENDIAN"
#if !defined(BYTE_ORDER)
# if defined(__BYTE_ORDER__)
# define BYTE_ORDER __BYTE_ORDER__
# elif defined(__BYTE_ORDER)
# define BYTE_ORDER __BYTE_ORDER
# elif defined(i386) || defined(__i386__) || defined(__i486__) || \
defined(__i586__) || defined(__i686__) || \
defined(__x86) || defined(__x86_64) || defined(__x86_64__) || \
defined(__amd64) || defined(__amd64__)
# define BYTE_ORDER LITTLE_ENDIAN
# elif defined(sparc) || defined(__sparc) || defined(__sparc__) || \
defined(POWERPC) || defined(mc68000) || defined(sel)
# define BYTE_ORDER BIG_ENDIAN
# else
# error "missing definition of BYTE_ORDER"
# endif
#endif

static really_inline uint16_t bswap16(uint16_t x)
{
// Copied from src/common/lib/libc/gen/bswap16.c in NetBSD
// Written by Manuel Bouyer <[email protected]>.
// Public domain.
return ((x << 8) & 0xff00) | ((x >> 8) & 0x00ff);
}

static really_inline uint32_t bswap32(uint32_t x)
{
// Copied from src/common/lib/libc/gen/bswap32.c in NetBSD
// Written by Manuel Bouyer <[email protected]>.
// Public domain.
return ( (x << 24) & 0xff000000 ) |
( (x << 8) & 0x00ff0000 ) |
( (x >> 8) & 0x0000ff00 ) |
( (x >> 24) & 0x000000ff );
}

static really_inline uint64_t bswap64(uint64_t x)
{
// Copied from src/common/lib/libc/gen/bswap64.c in NetBSD
// Written by Manuel Bouyer <[email protected]>.
// Public domain.
return ( (x << 56) & 0xff00000000000000ull ) |
( (x << 40) & 0x00ff000000000000ull ) |
( (x << 24) & 0x0000ff0000000000ull ) |
( (x << 8) & 0x000000ff00000000ull ) |
( (x >> 8) & 0x00000000ff000000ull ) |
( (x >> 24) & 0x0000000000ff0000ull ) |
( (x >> 40) & 0x000000000000ff00ull ) |
( (x >> 56) & 0x00000000000000ffull );
}

# if BYTE_ORDER == LITTLE_ENDIAN
# define htobe(bits, x) bswap ## bits((x))
# define htole(bits, x) (x)
# define betoh(bits, x) bswap ## bits((x))
# define letoh(bits, x) (x)
# else
# define htobe(bits, x) (x)
# define htole(bits, x) bswap ## bits((x))
# define betoh(bits, x) (x)
# define letoh(bits, x) bswap ## bits((x))
# endif

# if !defined htobe16
# define htobe16(x) htobe(16,(x))
# endif
# if !defined htobe32
# define htobe32(x) htobe(32,(x))
# endif
# if !defined htobe64
# define htobe64(x) htobe(64,(x))
# endif
# if !defined htole16
# define htole16(x) htole(16,(x))
# endif
# if !defined htole32
# define htole32(x) htole(32,(x))
# endif
# if !defined htole64
# define htole64(x) htole(64,(x))
# endif

# if !defined be16toh
# define be16toh(x) betoh(16,(x))
# endif
# if !defined be32toh
# define be32toh(x) betoh(32,(x))
# endif
# if !defined be64toh
# define be64toh(x) betoh(64,(x))
# endif
# if !defined le16toh
# define le16toh(x) letoh(16,(x))
# endif
# if !defined le32toh
# define le32toh(x) letoh(32,(x))
# endif
# if !defined le64toh
# define le64toh(x) letoh(64,(x))
# endif
#endif

#endif // ENDIAN_H
Loading

0 comments on commit 3a6f9d1

Please sign in to comment.