From 3811bbf6e026b93e18aaeb06df7a073acb716354 Mon Sep 17 00:00:00 2001 From: Dusk_NM02 Date: Sun, 23 Feb 2025 16:03:59 +0800 Subject: [PATCH] Fix popcnt cause crashes on unsupported devices --- CMakeLists.txt | 8 ++++++++ lib/nghttp3_ringbuf.c | 5 +++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b78ab52..dcc85c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -179,6 +179,12 @@ foreach(name configure_file("${name}.in" "${name}" @ONLY) endforeach() +option(ENABLE_POPCNT "Enable popcnt instruction" ON) + +if(NOT ENABLE_POPCNT) + add_definitions(-DDISABLE_POPCNT) +endif() + if(ENABLE_SHARED_LIB AND ENABLE_STATIC_LIB AND MSVC AND NOT STATIC_LIB_SUFFIX) set(STATIC_LIB_SUFFIX "_static") endif() @@ -213,6 +219,8 @@ message(STATUS "summary of build options: CXXFLAGS: ${CMAKE_CXX_FLAGS_${_build_type}} ${CMAKE_CXX_FLAGS} WARNCFLAGS: ${WARNCFLAGS} WARNCXXFLAGS: ${WARNCXXFLAGS} + SIMD instruction: + Enable popcnt: ${ENABLE_POPCNT} Library: Shared: ${ENABLE_SHARED_LIB} Static: ${ENABLE_STATIC_LIB} diff --git a/lib/nghttp3_ringbuf.c b/lib/nghttp3_ringbuf.c index 85c2e03..9bea0f5 100644 --- a/lib/nghttp3_ringbuf.c +++ b/lib/nghttp3_ringbuf.c @@ -35,8 +35,9 @@ #ifndef NDEBUG static int ispow2(size_t n) { -# if defined(_MSC_VER) && !defined(__clang__) && \ - (defined(_M_ARM) || (defined(_M_ARM64) && _MSC_VER < 1941)) +# if defined(DISABLE_POPCNT) || \ + (defined(_MSC_VER) && !defined(__clang__) && \ + (defined(_M_ARM) || (defined(_M_ARM64) && _MSC_VER < 1941))) return n && !(n & (n - 1)); # elif defined(WIN32) return 1 == __popcnt((unsigned int)n);