<commit_before>/* (C) 2007-2008 <NAME>, CSIRO
*/
/*
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
   
   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
   
   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
   
   - Neither the name of the Xiph.org Foundation nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.
   
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "mathops.h"
#include "cwrs.h"
#include "vq.h"
#include "arch.h"
#include "os_support.h"

/** Takes the pitch vector and the decoded residual vector, computes the gain
    that will give ||p+g*y||=1 and mixes the residual with the pitch. */
static void mix_pitch_and_residual(int * restrict iy, celt_norm_t * restrict X, int N, int K, const celt_norm_t * restrict P)
{
   int i;
   celt_word32_t Ryp, Ryy, Rpp;
   celt_word16_t ryp, ryy, rpp;
   celt_word32_t g;
   VARDECL(celt_norm_t, y);
#ifdef FIXED_POINT
   int yshift;
#endif
   SAVE_STACK;
#ifdef FIXED_POINT
   yshift = 13-celt_ilog2(K);
#endif
   ALLOC(y, N, celt_norm_t);

   /*for (i=0;i<N;i++)
   printf ("%d ", iy[i]);*/
   Rpp = 0;
   i=0;
   do {
      Rpp = MAC16_16(Rpp,P[i],P[i]);
      y[i] = SHL16(iy[i],yshift);
   } while (++i < N);

   Ryp = 0;
   Ryy = 0;
   /* If this doesn't generate a dual MAC (on supported archs), fire the compiler guy */
   i=0;
   do {
      Ryp = MAC16_16(Ryp, y[i], P[i]);
      Ryy = MAC16_16(Ryy, y[i], y[i]);
   } while (++i < N);

   ryp = ROUND16(Ryp,14);
   ryy = ROUND16(Ryy,14);
   rpp = ROUND16(Rpp,14);
   /* g = (sqrt(Ryp^2 + Ryy - Rpp*Ryy)-Ryp)/Ryy */
   g = MULT16_32_Q15(celt_sqrt(MAC16_16(Ryy, ryp,ryp) - MULT16_16(ryy,rpp)) - ryp,
                     celt_rcp(SHR32(Ryy,9)));

   i=0;
   do 
      X[i] = ADD16(P[i], ROUND16(MULT16_16(y[i], g),11));
   while (++i < N);

   RESTORE_STACK;
}


void alg_quant(celt_norm_t *X, celt_mask_t *W, int N, int K, celt_norm_t *P, ec_enc *enc)
{
   VARDECL(celt_norm_t, y);
   VARDECL(int, iy);
   VARDECL(celt_word16_t, signx);
   int j, is;
   celt_word16_t s;
   int pulsesLeft;
   celt_word32_t sum;
   celt_word32_t xy, yy, yp;
   celt_word16_t Rpp;
   int N_1; /* Inverse of N, in Q14 format (even for float) */
#ifdef FIXED_POINT
   int yshift;
#endif
   SAVE_STACK;

#ifdef FIXED_POINT
   yshift = 13-celt_ilog2(K);
#endif

   ALLOC(y, N, celt_norm_t);
   ALLOC(iy, N, int);
   ALLOC(signx, N, celt_word16_t);
   N_1 = 512/N;

   sum = 0;
   j=0; do {
      X[j] -= P[j];
      if (X[j]>0)
         signx[j]=1;
      else {
         signx[j]=-1;
         X[j]=-X[j];
         P[j]=-P[j];
      }
      iy[j] = 0;
      y[j] = 0;
      sum = MAC16_16(sum, P[j],P[j]);
   } while (++j<N);
   Rpp = ROUND16(sum, NORM_SHIFT);

   celt_assert2(Rpp<=NORM_SCALING, "Rpp should never have a norm greater than unity");

   xy = yy = yp = 0;

   pulsesLeft = K;
#if 0
   if (K > (N>>1))
   {
      float sum=0;
      j=0; do {
         sum += X[j];
      }  while (++j<N);
      sum = K/(EPSILON+sum);
      j=0; do {
         iy[j] = floor(sum*X[j]);
         y[j] = SHL16(iy[j],yshift);
         yy = MAC16_16(yy, y[j],y[j]);
         xy = MAC16_16(xy, X[j],y[j]);
         yp += P[j]*y[j];
         y[j] *= 2;;
         pulsesLeft -= iy[j];
      }  while (++j<N);
   }
#endif
   while (pulsesLeft > 1)
   {
      int pulsesAtOnce=1;
      int best_id;
      celt_word16_t magnitude;
      celt_word32_t best_num = -VERY_LARGE16;
      celt_word16_t best_den = 0;
#ifdef FIXED_POINT
      int rshift;
#endif
      /* Decide on how many pulses to find at once */
      pulsesAtOnce = (pulsesLeft*N_1)>>9; /* pulsesLeft/N */
      if (pulsesAtOnce<1)<commit_msg>New VQ search nearly fixed-point ready<commit_after>/* (C) 2007-2008 <NAME>, CSIRO
*/
/*
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
   
   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
   
   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
   
   - Neither the name of the Xiph.org Foundation nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.
   
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "mathops.h"
#include "cwrs.h"
#include "vq.h"
#include "arch.h"
#include "os_support.h"

/** Takes the pitch vector and the decoded residual vector, computes the gain
    that will give ||p+g*y||=1 and mixes the residual with the pitch. */
static void mix_pitch_and_residual(int * restrict iy, celt_norm_t * restrict X, int N, int K, const celt_norm_t * restrict P)
{
   int i;
   celt_word32_t Ryp, Ryy, Rpp;
   celt_word16_t ryp, ryy, rpp;
   celt_word32_t g;
   VARDECL(celt_norm_t, y);
#ifdef FIXED_POINT
   int yshift;
#endif
   SAVE_STACK;
#ifdef FIXED_POINT
   yshift = 13-celt_ilog2(K);
#endif
   ALLOC(y, N, celt_norm_t);

   /*for (i=0;i<N;i++)
   printf ("%d ", iy[i]);*/
   Rpp = 0;
   i=0;
   do {
      Rpp = MAC16_16(Rpp,P[i],P[i]);
      y[i] = SHL16(iy[i],yshift);
   } while (++i < N);

   Ryp = 0;
   Ryy = 0;
   /* If this doesn't generate a dual MAC (on supported archs), fire the compiler guy */
   i=0;
   do {
      Ryp = MAC16_16(Ryp, y[i], P[i]);
      Ryy = MAC16_16(Ryy, y[i], y[i]);
   } while (++i < N);

   ryp = ROUND16(Ryp,14);
   ryy = ROUND16(Ryy,14);
   rpp = ROUND16(Rpp,14);
   /* g = (sqrt(Ryp^2 + Ryy - Rpp*Ryy)-Ryp)/Ryy */
   g = MULT16_32_Q15(celt_sqrt(MAC16_16(Ryy, ryp,ryp) - MULT16_16(ryy,rpp)) - ryp,
                     celt_rcp(SHR32(Ryy,9)));

   i=0;
   do 
      X[i] = ADD16(P[i], ROUND16(MULT16_16(y[i], g),11));
   while (++i < N);

   RESTORE_STACK;
}


void alg_quant(celt_norm_t *X, celt_mask_t *W, int N, int K, celt_norm_t *P, ec_enc *enc)
{
   VARDECL(celt_norm_t, y);
   VARDECL(int, iy);
   VARDECL(celt_word16_t, signx);
   int j, is;
   celt_word16_t s;
   int pulsesLeft;
   celt_word32_t sum;
   celt_word32_t xy, yy, yp;
   celt_word16_t Rpp;
   int N_1; /* Inverse of N, in Q14 format (even for float) */
#ifdef FIXED_POINT
   int yshift;
#endif
   SAVE_STACK;

#ifdef FIXED_POINT
   yshift = 13-celt_ilog2(K);
#endif

   ALLOC(y, N, celt_norm_t);
   ALLOC(iy, N, int);
   ALLOC(signx, N, celt_word16_t);
   N_1 = 512/N;

   sum = 0;
   j=0; do {
      X[j] -= P[j];
      if (X[j]>0)
         signx[j]=1;
      else {
         signx[j]=-1;
         X[j]=-X[j];
         P[j]=-P[j];
      }
      iy[j] = 0;
      y[j] = 0;
      sum = MAC16_16(sum, P[j],P[j]);
   } while (++j<N);
   Rpp = ROUND16(sum, NORM_SHIFT);

   celt_assert2(Rpp<=NORM_SCALING, "Rpp should never have a norm greater than unity");

   xy = yy = yp = 0;

   pulsesLeft = K;
#if 0
   if (K > (N>>1))
   {
      celt_word32_t sum=0;
      j=0; do {
         sum += X[j];
      }  while (++j<N);
      sum = DIV32(SHL32(EXTEND32(K),15),EPSILON+sum);
      j=0; do {
#ifdef FIXED_POINT
         iy[j] = MULT16_32_Q15(X[j],sum);
#else
         iy[j] = floor(sum*X[j]);
#endif
         y[j] = SHL16(iy[j],yshift);
         yy = MAC16_16(yy, y[j],y[j]);
         xy = MAC16_16(xy, X[j],y[j]);
         yp += P[j]*y[j];
         y[j] *= 2;
         pulsesLeft -= iy[j];
      }  while (++j<N);
   }
#endif
   while (pulsesLeft > 1)
   {
      int pulsesAtOnce=1;
      int best_id;
      celt_word16_t magnitude;
      celt_word32_t best_num = -VERY_LARGE16;
      celt_word16_t best_den = 0;
#ifdef FIXED_POINT
      int rshift;
#endif
      /* Decide on how many pulses to find at once */
      pulsesAtOnce = (pulsesLeft*N_1)>>9; /* pulsesLeft/N */
      if (pulsesAtOnce<1)
{'fraction_non_alphanumeric': 0.12419634656597613, 'fraction_numerical': 0.03143177875293397, 'mean_word_length': 2.8491751767478397, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1114903', 'n_tokens_mistral': 4047, 'n_tokens_neox': 3745, 'n_words': 1110}