From 780059ad5c5312e8594c46b3f32f4268d2ea6130 Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Wed, 22 Nov 2023 09:25:37 -0700 Subject: [PATCH 1/2] handle non-vla compilers Leverages the C std version and the standard __STDC_NO_VLA__ macro to detect whether VLA is supported, and if not falls back to using alloca. --- src/arch.h | 11 +++++++++++ src/celt_lpc.c | 8 ++++---- src/pitch.c | 8 ++++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/arch.h b/src/arch.h index 52de623..cfa7df6 100644 --- a/src/arch.h +++ b/src/arch.h @@ -258,4 +258,15 @@ static OPUS_INLINE int celt_isnan(float x) #endif #endif +#if __STDC_VERSION__ < 199901L || (__STDC_VERSION__ > 201000L && __STDC_NO_VLA__ == 1) + #define NO_VLA +#endif + +#ifdef NO_VLA + #include + #define stackalloc(type, id, len) type *id = alloca(len) +#else + #define stackalloc(type, id, len) type id[len] +#endif + #endif /* ARCH_H */ diff --git a/src/celt_lpc.c b/src/celt_lpc.c index e922783..08bd713 100644 --- a/src/celt_lpc.c +++ b/src/celt_lpc.c @@ -96,7 +96,7 @@ void celt_fir( int ord) { int i,j; - opus_val16 rnum[ord]; + stackalloc(opus_val16, rnum, ord); for(i=0;i0); celt_assert(overlap>=0); if (overlap == 0) diff --git a/src/pitch.c b/src/pitch.c index f4a2651..fd29e3b 100644 --- a/src/pitch.c +++ b/src/pitch.c @@ -297,9 +297,9 @@ void tn_pitch_search(const opus_val16 *x_lp, opus_val16 *y, celt_assert(max_pitch>0); lag = len+max_pitch; - opus_val16 x_lp4[len>>2]; - opus_val16 y_lp4[lag>>2]; - opus_val32 xcorr[max_pitch>>1]; + stackalloc(opus_val16, x_lp4, len>>2); + stackalloc(opus_val16, y_lp4, lag>>2); + stackalloc(opus_val32, xcorr, max_pitch>>1); /* Downsample by 2 again */ for (j=0;j>2;j++) @@ -443,7 +443,7 @@ opus_val16 tn_remove_doubling(opus_val16 *x, int maxperiod, int minperiod, *T0_=maxperiod-1; T = T0 = *T0_; - opus_val32 yy_lookup[maxperiod+1]; + stackalloc(opus_val32, yy_lookup, maxperiod+1); dual_inner_prod(x, x, x-T0, N, &xx, &xy); yy_lookup[0] = xx; yy=xx; From b1917582a4feb8cff07f798bf182a08673e4e08b Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Wed, 22 Nov 2023 09:49:41 -0700 Subject: [PATCH 2/2] expose M_PI for MSVC compiler The MSVC compiler only exposes M_PI if _USE_MATH_DEFINES is defined... don't ask me why. --- CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index fa5be32..0e5af83 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,11 @@ set( # Build rnnoise add_definitions(-DRNNOISE_BUILD) +if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + # MSVC requires this to expose definitions like M_PI + add_definitions(-D_USE_MATH_DEFINES) +endif() + # Compile the library add_library(rnnoise ${SOURCES})