Skip to content

Commit

Permalink
cmake,common/Compiler: add USE_COMPILER_INTRINSICS and USE_COMPILER_C…
Browse files Browse the repository at this point in the history
…USTOMIZATION CMake options

This introduces USE_COMPILER_INTRINSICS and
USE_COMPILER_CUSTOMIZATION CMake options.
They are enabled by default.

Disabling USE_COMPILER_INTRINSICS is meant
to test generic fallback implementations.

Disabling USE_COMPILER_CUSTOMIZATION is meant
to test the unknown compiler fallback definitions.
  • Loading branch information
illwieckz committed May 27, 2024
1 parent 0887ca5 commit e8186c6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
20 changes: 20 additions & 0 deletions cmake/DaemonFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ include(CheckCXXCompilerFlag)

add_definitions(-DDAEMON_BUILD_${CMAKE_BUILD_TYPE})

option(USE_COMPILER_INTRINSICS "Enable usage of compiler intrinsics" ON)
mark_as_advanced(USE_COMPILER_INTRINSICS)

if (USE_COMPILER_INTRINSICS)
add_definitions(-DDAEMON_USE_COMPILER_INTRINSICS=1)
message(STATUS "Enabling compiler intrinsics")
else()
message(STATUS "Disabling compiler intrinsics")
endif()

option(USE_COMPILER_CUSTOMIZATION "Enable usage of compiler custom attributes and operators" ON)
mark_as_advanced(USE_COMPILER_CUSTOMIZATION)

if (USE_COMPILER_CUSTOMIZATION)
add_definitions(-DDAEMON_USE_COMPILER_CUSTOMIZATION=1)
message(STATUS "Enabling compiler custom attributes and operators")
else()
message(STATUS "Disabling compiler custom attributes and operators")
endif()

# Set flag without checking, optional argument specifies build type
macro(set_c_flag FLAG)
if (${ARGC} GREATER 1)
Expand Down
9 changes: 5 additions & 4 deletions src/common/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ int CountTrailingZeroes(unsigned int x);
int CountTrailingZeroes(unsigned long x);
int CountTrailingZeroes(unsigned long long x);

#if defined(DAEMON_USE_ARCH_INTRINSICS) && defined(__GNUC__)
#if defined(DAEMON_USE_COMPILER_INTRINSICS) && defined(__GNUC__)
inline int CountTrailingZeroes(unsigned int x)
{ return __builtin_ctz(x); }
inline int CountTrailingZeroes(unsigned long x)
{ return __builtin_ctzl(x); }
inline int CountTrailingZeroes(unsigned long long x)
{ return __builtin_ctzll(x); }
#elif defined(DAEMON_USE_ARCH_INTRINSICS) && defined(_MSC_VER)
#elif defined(DAEMON_USE_COMPILER_INTRINSICS) && defined(_MSC_VER)
inline int CountTrailingZeroes(unsigned int x)
{ unsigned long ans; _BitScanForward(&ans, x); return ans; }
inline int CountTrailingZeroes(unsigned long x)
Expand Down Expand Up @@ -117,7 +117,7 @@ int CountTrailingZeroes(unsigned long long x);
#endif

// GCC and Clang attribute and operator customization.
#if defined(__GNUC__)
#if defined(DAEMON_USE_COMPILER_CUSTOMIZATION) && defined(__GNUC__)

// Emit a nice warning when a function is used
#define DEPRECATED __attribute__((__deprecated__))
Expand Down Expand Up @@ -199,7 +199,7 @@ See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0627r0.pdf */
#endif

// Microsoft Visual C++ attribute and operator customization.
#elif defined(_MSC_VER)
#elif defined(DAEMON_USE_COMPILER_CUSTOMIZATION) && defined(_MSC_VER)

// See descriptions above
#define DEPRECATED __declspec(deprecated)
Expand Down Expand Up @@ -230,6 +230,7 @@ See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0627r0.pdf */
#define WARN_UNUSED_RESULT
#define COLD
#define NORETURN
#define NORETURN_PTR
#define PRINTF_LIKE(n)
#define VPRINTF_LIKE(n)
#define PRINTF_TRANSLATE_ARG(a)
Expand Down

0 comments on commit e8186c6

Please sign in to comment.