From 2169f64c5283098171e0cab26cc0588e964cbb71 Mon Sep 17 00:00:00 2001
From: Jean-Marc Valin <jeanmarcv@google.com>
Date: Thu, 27 Feb 2025 10:13:36 -0500
Subject: [PATCH] Increase accuracy of QEXT twiddle factors/window

---
 celt/kiss_fft.c | 4 ++--
 celt/modes.c    | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/celt/kiss_fft.c b/celt/kiss_fft.c
index d7bc7d4c8..601d04b68 100644
--- a/celt/kiss_fft.c
+++ b/celt/kiss_fft.c
@@ -417,8 +417,8 @@ static void compute_twiddles(kiss_twiddle_cpx *twiddles, int nfft)
    for (i=0;i<nfft;++i) {
       opus_val32 phase = -i;
 #ifdef ENABLE_QEXT
-      twiddles[i].r = floor(.5+2147483647*cos((2*M_PI/nfft)*phase));
-      twiddles[i].i = floor(.5+2147483647*sin((2*M_PI/nfft)*phase));
+      twiddles[i].r = (int)MIN32(2147483647, floor(.5+2147483648*cos((2*M_PI/nfft)*phase)));
+      twiddles[i].i = (int)MIN32(2147483647, floor(.5+2147483648*sin((2*M_PI/nfft)*phase)));
 #else
       kf_cexp2(twiddles+i, DIV32(SHL32(phase,17),nfft));
 #endif
diff --git a/celt/modes.c b/celt/modes.c
index d7c3efa10..9182281a2 100644
--- a/celt/modes.c
+++ b/celt/modes.c
@@ -380,7 +380,7 @@ CELTMode *opus_custom_mode_create(opus_int32 Fs, int frame_size, int *error)
 #else
 # ifdef ENABLE_QEXT
    for (i=0;i<mode->overlap;i++)
-      window[i] = 2147483647*sin(.5*M_PI* sin(.5*M_PI*(i+.5)/mode->overlap) * sin(.5*M_PI*(i+.5)/mode->overlap));
+      window[i] = MIN32(2147483647, 2147483648*sin(.5*M_PI* sin(.5*M_PI*(i+.5)/mode->overlap) * sin(.5*M_PI*(i+.5)/mode->overlap)));
 # else
    for (i=0;i<mode->overlap;i++)
       window[i] = MIN32(32767,floor(.5+32768.*sin(.5*M_PI* sin(.5*M_PI*(i+.5)/mode->overlap) * sin(.5*M_PI*(i+.5)/mode->overlap))));
-- 
GitLab