Skip to content

Commit 3eaadb8

Browse files
authored
Merge pull request #18 from bugparty/codex/investigate-slow-push-back-speed
Enable optional AVX2/march support
2 parents 9fe2927 + ff0d14e commit 3eaadb8

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@ elseif(MSVC)
4242
message(WARNING "BMI1 support is not available for MSVC in this configuration.")
4343
endif()
4444

45+
# Detect AVX2 or native CPU optimizations and enable them if available
46+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
47+
include(CheckCXXCompilerFlag)
48+
check_cxx_compiler_flag("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE)
49+
if(COMPILER_SUPPORTS_MARCH_NATIVE)
50+
add_compile_options(-march=native)
51+
else()
52+
check_cxx_compiler_flag("-mavx2" COMPILER_SUPPORTS_AVX2)
53+
if(COMPILER_SUPPORTS_AVX2)
54+
add_compile_options(-mavx2)
55+
endif()
56+
endif()
57+
elseif(MSVC)
58+
include(CheckCXXCompilerFlag)
59+
check_cxx_compiler_flag("/arch:AVX2" COMPILER_SUPPORTS_AVX2)
60+
if(COMPILER_SUPPORTS_AVX2)
61+
add_compile_options(/arch:AVX2)
62+
endif()
63+
endif()
64+
4565
# Optionally disable bounds checking in the bitvector implementation
4666

4767
# Enable testing

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ This repository provides a small bit vector implementation along with tests and
44

55
## Running the benchmarks without bounds checking
66

7-
Bounds checking is enabled by default. To benchmark without checks, configure and build with (this defines `BITVECTOR_NO_BOUND_CHECK`):
7+
Bounds checking is enabled by default. To benchmark without checks, configure
8+
and build with (this defines `BITVECTOR_NO_BOUND_CHECK`). The build system now
9+
detects whether the compiler and host CPU support AVX2 or other native
10+
instructions and enables them when possible:
811

912
```bash
1013
cmake -S . -B build -DBITVECTOR_NO_BOUND_CHECK=ON -DCMAKE_BUILD_TYPE=Release

0 commit comments

Comments
 (0)