Skip to content

Commit

Permalink
disable rtti and exceptions for msvc (#5167)
Browse files Browse the repository at this point in the history
* disable rtti and exceptions for msvc

* warnings--

* erff

* arch sse2 for 32bit build

* enable rtti for cross compiling
  • Loading branch information
nihui authored Nov 22, 2023
1 parent 5cc1307 commit deae9e6
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/windows-x64-cpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
- name: build-sse2
run: |
mkdir build-sse2; cd build-sse2
cmake -T ${{ matrix.toolset-version }},host=x64 -A x64 -Dprotobuf_DIR="$env:GITHUB_WORKSPACE\protobuf-install\cmake" -DNCNN_RUNTIME_CPU=OFF -DNCNN_AVX2=OFF -DNCNN_AVX=OFF -DNCNN_AVX512=OFF -DNCNN_XOP=OFF -DNCNN_BUILD_TESTS=ON ..
cmake -T ${{ matrix.toolset-version }},host=x64 -A x64 -Dprotobuf_DIR="$env:GITHUB_WORKSPACE\protobuf-install\cmake" -DNCNN_RUNTIME_CPU=OFF -DNCNN_AVX2=OFF -DNCNN_AVX=OFF -DNCNN_AVX512=OFF -DNCNN_XOP=OFF -DNCNN_BUILD_TESTS=ON -DNCNN_DISABLE_RTTI=ON -DNCNN_DISABLE_EXCEPTION=ON ..
cmake --build . --config Release -j 2
- name: test-sse2
run: cd build-sse2; ctest -C Release --output-on-failure -j 2
Expand Down
16 changes: 8 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,20 @@ option(NCNN_INT8 "int8 inference" ON)
option(NCNN_BF16 "bf16 inference" ON)
option(NCNN_FORCE_INLINE "force inline some function" ON)

if(ANDROID OR IOS OR NCNN_SIMPLESTL OR CMAKE_CROSSCOMPILING)
if(ANDROID OR IOS OR NCNN_SIMPLESTL)
option(NCNN_DISABLE_RTTI "disable rtti" ON)
option(NCNN_BUILD_TOOLS "build tools" OFF)
option(NCNN_BUILD_EXAMPLES "build examples" OFF)
option(NCNN_DISABLE_EXCEPTION "disable exception" ON)
else()
option(NCNN_DISABLE_RTTI "disable rtti" OFF)
option(NCNN_BUILD_TOOLS "build tools" ON)
option(NCNN_BUILD_EXAMPLES "build examples" ON)
option(NCNN_DISABLE_EXCEPTION "disable exception" OFF)
endif()

if(ANDROID OR IOS OR NCNN_SIMPLESTL)
option(NCNN_DISABLE_EXCEPTION "disable exception" ON)
if(ANDROID OR IOS OR NCNN_SIMPLESTL OR CMAKE_CROSSCOMPILING)
option(NCNN_BUILD_TOOLS "build tools" OFF)
option(NCNN_BUILD_EXAMPLES "build examples" OFF)
else()
option(NCNN_DISABLE_EXCEPTION "disable exception" OFF)
option(NCNN_BUILD_TOOLS "build tools" ON)
option(NCNN_BUILD_EXAMPLES "build examples" ON)
endif()

if(NCNN_SHARED_LIB)
Expand Down
17 changes: 14 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -373,17 +373,28 @@ else()
endif()

if(NCNN_DISABLE_RTTI)
target_compile_options(ncnn PUBLIC -fno-rtti)
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC" OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_SIMULATE_ID MATCHES "MSVC" AND CMAKE_CXX_COMPILER_FRONTEND_VARIANT MATCHES "MSVC"))
target_compile_options(ncnn PUBLIC /GR-)
else()
target_compile_options(ncnn PUBLIC -fno-rtti)
endif()
endif()

if(NCNN_DISABLE_EXCEPTION)
target_compile_options(ncnn PUBLIC -fno-exceptions)
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC" OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_SIMULATE_ID MATCHES "MSVC" AND CMAKE_CXX_COMPILER_FRONTEND_VARIANT MATCHES "MSVC"))
target_compile_options(ncnn PUBLIC /EHsc /D_HAS_EXCEPTIONS=0)
else()
target_compile_options(ncnn PUBLIC -fno-exceptions)
endif()
endif()

if(NCNN_TARGET_ARCH STREQUAL "x86")
if(NCNN_SSE2)
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC" OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_SIMULATE_ID MATCHES "MSVC" AND CMAKE_CXX_COMPILER_FRONTEND_VARIANT MATCHES "MSVC"))
target_compile_options(ncnn PRIVATE /arch:SSE2 /D__SSE2__)
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
target_compile_options(ncnn PRIVATE /arch:SSE2)
endif()
target_compile_options(ncnn PRIVATE /D__SSE2__)
else()
target_compile_options(ncnn PRIVATE -msse2 -msse)
if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
Expand Down
2 changes: 1 addition & 1 deletion src/layer/arm/arm_usability.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

static inline signed char float2int8(float v)
{
int int32 = round(v);
int int32 = (int)roundf(v);
if (int32 > 127) return 127;
if (int32 < -127) return -127;
return (signed char)int32;
Expand Down
2 changes: 1 addition & 1 deletion src/layer/erf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int Erf::forward_inplace(Mat& bottom_top_blob, const Option& opt) const

for (int i = 0; i < size; i++)
{
ptr[i] = erf(ptr[i]);
ptr[i] = erff(ptr[i]);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/simplemath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,11 @@ float erf(float a)
return r;
}

float erff(float x)
{
return erf(x);
}

float erfcf(float x)
{
return 1.0 - erf(x);
Expand Down
3 changes: 2 additions & 1 deletion src/simplemath.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ NCNN_EXPORT float log10f(float);
* ====================================================
*/
NCNN_EXPORT float erf(float);
NCNN_EXPORT float erff(float);
NCNN_EXPORT float erfcf(float);

/*
Expand All @@ -99,4 +100,4 @@ NCNN_EXPORT float nearbyintf(float);

#endif // NCNN_SIMPLEMATH

#endif // NCNN_SIMPLEMATH_H
#endif // NCNN_SIMPLEMATH_H

0 comments on commit deae9e6

Please sign in to comment.