Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ x64/
.DS_Store
.clang-format
tmp
build
41 changes: 30 additions & 11 deletions cmake/CompilerSetup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,20 @@ if(IS_MSVC)
message(STATUS "AVX-512 is disabled. If you would like to activate make sure you set ENABLE_AVX512 to ON while running cmake.")
endif()
elseif(IS_CLANG OR IS_GCC)
if(ENABLE_AVX512)
set(CPU_INSTRUCTION_FLAGS "-mavx -mavx2 -mavx512f -mavx512cd -mavx512vl -mavx512bw -mavx512dq" CACHE INTERNAL "CPU instruction set flags" FORCE)
message(STATUS "GCC/Clang: Enabling AVX-512 and AVX/AVX2")
# Only enable AVX flags on x86_64 targets. On non-x86 (e.g., arm64)
# these flags are invalid and will cause the compiler to error.
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64")
if(ENABLE_AVX512)
set(CPU_INSTRUCTION_FLAGS "-mavx -mavx2 -mavx512f -mavx512cd -mavx512vl -mavx512bw -mavx512dq" CACHE INTERNAL "CPU instruction set flags" FORCE)
message(STATUS "GCC/Clang: Enabling AVX-512 and AVX/AVX2")
else()
set(CPU_INSTRUCTION_FLAGS "-mavx -mavx2" CACHE INTERNAL "CPU instruction set flags" FORCE)
message(STATUS "GCC/Clang: Enabling AVX/AVX2")
message(STATUS "AVX-512 is disabled. If you would like to activate make sure you set ENABLE_AVX512 to ON while running cmake.")
endif()
else()
set(CPU_INSTRUCTION_FLAGS "-mavx -mavx2" CACHE INTERNAL "CPU instruction set flags" FORCE)
message(STATUS "GCC/Clang: Enabling AVX/AVX2")
message(STATUS "AVX-512 is disabled. If you would like to activate make sure you set ENABLE_AVX512 to ON while running cmake.")
set(CPU_INSTRUCTION_FLAGS "" CACHE INTERNAL "CPU instruction set flags" FORCE)
message(STATUS "Skipping AVX/AVX2/AVX-512 flags for non-x86_64 architecture: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
endif()

Expand All @@ -205,12 +212,24 @@ set(TEST_SPECIFIC_FLAGS "" CACHE INTERNAL "Test-specific compiler flags")
if(IS_MSVC)
set(TEST_SPECIFIC_FLAGS "/WX /EHsc" CACHE INTERNAL "Test-specific compiler flags" FORCE)
elseif(IS_CLANG OR IS_GCC)
if(USE_SANITIZER)
set(TEST_SPECIFIC_FLAGS "-Wpedantic -Werror -mrdrnd -Wcast-align -fsanitize=alignment -fno-sanitize-recover=alignment" CACHE INTERNAL "Test-specific compiler flags" FORCE)
set(TEST_SPECIFIC_LINK_FLAGS "-fsanitize=alignment" CACHE INTERNAL "Test-specific linker flags" FORCE)
# Only enable rdrand test flag on x86 targets; rdrand is an x86
# instruction and -mrdrnd is invalid on arm64.
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64")
if(USE_SANITIZER)
set(TEST_SPECIFIC_FLAGS "-Wpedantic -Werror -mrdrnd -Wcast-align -fsanitize=alignment -fno-sanitize-recover=alignment" CACHE INTERNAL "Test-specific compiler flags" FORCE)
set(TEST_SPECIFIC_LINK_FLAGS "-fsanitize=alignment" CACHE INTERNAL "Test-specific linker flags" FORCE)
else()
set(TEST_SPECIFIC_FLAGS "-Wpedantic -Werror -mrdrnd -Wcast-align " CACHE INTERNAL "Test-specific compiler flags" FORCE)
set(TEST_SPECIFIC_LINK_FLAGS "" CACHE INTERNAL "Test-specific linker flags" FORCE)
endif()
else()
set(TEST_SPECIFIC_FLAGS "-Wpedantic -Werror -mrdrnd -Wcast-align " CACHE INTERNAL "Test-specific compiler flags" FORCE)
set(TEST_SPECIFIC_LINK_FLAGS "" CACHE INTERNAL "Test-specific linker flags" FORCE)
if(USE_SANITIZER)
set(TEST_SPECIFIC_FLAGS "-Wpedantic -Werror -Wcast-align -fsanitize=alignment -fno-sanitize-recover=alignment" CACHE INTERNAL "Test-specific compiler flags" FORCE)
set(TEST_SPECIFIC_LINK_FLAGS "-fsanitize=alignment" CACHE INTERNAL "Test-specific linker flags" FORCE)
else()
set(TEST_SPECIFIC_FLAGS "-Wpedantic -Werror -Wcast-align " CACHE INTERNAL "Test-specific compiler flags" FORCE)
set(TEST_SPECIFIC_LINK_FLAGS "" CACHE INTERNAL "Test-specific linker flags" FORCE)
endif()
endif()
endif()

Expand Down
15 changes: 13 additions & 2 deletions lib/platform_common/qintrin.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
#pragma once
#pragma once

// Header file for the inclusion of plartform specific x86/x64 intrinsics header files.
// Header file for the inclusion of platform specific x86/x64 intrinsics header files.
//
// Historically the project included x86 intrinsics headers unconditionally.
// That causes hard build failures on non-x86 systems (for example Apple
// Silicon) because <immintrin.h> and related headers declare x86-only
// intrinsics and inline assembly. To remain backward compatible for x86
// builds while allowing native builds on other architectures, include
// x86 intrinsic headers only when the compiler target is an x86 family.

#if defined(_MSC_VER) && !defined(__clang__)
#include <intrin.h>
#else
#if defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86)
#include <immintrin.h>
#else
// Non-x86 targets: do not include x86 intrinsic headers.
#endif
#endif
35 changes: 26 additions & 9 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,34 @@ set_source_files_properties(platform/custom_stack.asm PROPERTIES


# Add additional compiler-specific flags for the main application
# NOTE: The x86-specific flags below (for example `-mrdrnd` and
# `-target x86_64-unknown-windows`) must only be applied when the build
# target is x86_64. On non-x86 platforms (Apple Silicon, ARM), those flags
# and intrinsics are invalid and will break the build. The conditional here
# preserves the original x86 behavior while keeping native non-x86 builds
# functional.
if(IS_CLANG)
# Clang-specific flags
target_compile_options(Qubic PRIVATE
-D_CONSOLE
$<$<CONFIG:Release>:-DNDEBUG>
-mrdrnd
-target x86_64-unknown-windows
$<$<BOOL:${CLANG_SYSROOT}>:--sysroot="${CLANG_SYSROOT}">
-Werror
-Wno-unused-parameter
)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64")
# x86_64-only flags (rdrand instruction and cross-target)
target_compile_options(Qubic PRIVATE
-D_CONSOLE
$<$<CONFIG:Release>:-DNDEBUG>
-mrdrnd
-target x86_64-unknown-windows
$<$<BOOL:${CLANG_SYSROOT}>:--sysroot="${CLANG_SYSROOT}">
-Werror
-Wno-unused-parameter
)
else()
# Generic clang flags for non-x86 platforms (e.g., arm64 mac)
target_compile_options(Qubic PRIVATE
-D_CONSOLE
$<$<CONFIG:Release>:-DNDEBUG>
-Werror
-Wno-unused-parameter
)
endif()
elseif(IS_MSVC)
# MSVC-specific flags
target_compile_options(Qubic PRIVATE
Expand Down
25 changes: 20 additions & 5 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,16 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src)
include_directories(${PROJECT_ROOT_DIR})


add_executable(
qubic_core_tests
# Test sources common to all architectures
set(TEST_SOURCES
# assets.cpp
# common_def.cpp
# contract_core.cpp
# contract_qearn.cpp
# contract_qvault.cpp
# contract_qx.cpp
# kangaroo_twelve.cpp
m256.cpp
math_lib.cpp
network_messages.cpp
# platform.cpp
# qpi_collection.cpp
# qpi.cpp
Expand All @@ -50,12 +48,29 @@ add_executable(
# vote_counter.cpp
)

# Certain test sources exercise x86-specific intrinsics and codepaths
# (for example, the AVX/m256 implementation). To keep the test target
# buildable on non-x86 platforms (like arm64), only append those sources
# when the build is targeting x86_64. This preserves the original x86
# test coverage when building on x86, while allowing native non-x86
# development without intrusive #ifdefs inside the test files.
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64")
list(APPEND TEST_SOURCES
m256.cpp
network_messages.cpp
)
endif()

add_executable(qubic_core_tests ${TEST_SOURCES})

# Apply test-specific compiler flags from the centralized detection module
apply_test_compiler_flags(qubic_core_tests)

# Add additional test-specific flags if needed
if(IS_CLANG OR IS_GCC)
target_compile_options(qubic_core_tests PRIVATE -mrdrnd)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64")
target_compile_options(qubic_core_tests PRIVATE -mrdrnd)
endif()
endif()


Expand Down