From 011e1928d79fb643e3f8e426b01bdfa685383495 Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Wed, 22 Nov 2023 09:25:37 -0700 Subject: [PATCH] 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 52de6233..cfa7df6f 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 521351e9..d44b26f3 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 bd101a6c..bd52d87d 100644 --- a/src/pitch.c +++ b/src/pitch.c @@ -297,9 +297,9 @@ void 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 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;