Program: 107578
Project: 107578
Buggy source:
/** 
 * Compute Normalized Line Spectral Frequencies (NLSFs) from whitening filter coefficients. If not all roots are found, the a_Q16 coefficients are bandwidth expanded until convergence.
 * @param NLSF Normalized Line Spectral Frequencies, Q15 (0 - (2^15-1)), [d]
 * @param a_Q16 Monic whitening filter coefficients in Q16 [d]
 * @param d Filter order (must be even)
 */
static void SKP_Silk_A2NLSF(int[] NLSF,int[] a_Q16,final int d){
  int i, k, m, dd, root_ix, ffrac;
  int xlo, xhi, xmid;
  int ylo, yhi, ymid;
  int nom, den;
  int[] P=new int[SigProcFIX.SKP_Silk_MAX_ORDER_LPC / 2 + 1];
  int[] Q=new int[SigProcFIX.SKP_Silk_MAX_ORDER_LPC / 2 + 1];
  int[][] PQ=new int[2][];
  int[] p;
  PQ[0]=P;
  PQ[1]=Q;
  dd=d >> 1;
  SKP_Silk_A2NLSF_init(a_Q16,P,Q,dd);
  p=P;
  xlo=LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[0];
  ylo=SKP_Silk_A2NLSF_eval_poly(p,xlo,dd);
  if (ylo < 0) {
    NLSF[0]=0;
    p=Q;
    ylo=SKP_Silk_A2NLSF_eval_poly(p,xlo,dd);
    root_ix=1;
  }
 else {
    root_ix=0;
  }
  k=1;
  i=0;
  while (true) {
    if (OVERSAMPLE_COSINE_TABLE) {
      xhi=LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[k >> 1] + ((LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[(k + 1) >> 1] - LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[k >> 1]) >> 1);
    }
 else {
      xhi=LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[k];
    }
    yhi=SKP_Silk_A2NLSF_eval_poly(p,xhi,dd);
    if ((ylo <= 0 && yhi >= 0) || (ylo >= 0 && yhi <= 0)) {
      if (OVERSAMPLE_COSINE_TABLE)       ffrac=-128;
 else       ffrac=-256;
      for (m=0; m < BIN_DIV_STEPS_A2NLSF_FIX; m++) {
        xmid=SigProcFIX.SKP_RSHIFT_ROUND(xlo + xhi,1);
        ymid=SKP_Silk_A2NLSF_eval_poly(p,xmid,dd);
        if ((ylo <= 0 && ymid >= 0) || (ylo >= 0 && ymid <= 0)) {
          xhi=xmid;
          yhi=ymid;
        }
 else {
          xlo=xmid;
          ylo=ymid;
          if (OVERSAMPLE_COSINE_TABLE)           ffrac=ffrac + (64 >> m);
 else           ffrac=ffrac + (128 >> m);
        }
      }
      if (Math.abs(ylo) < 65536) {
        den=ylo - yhi;
        nom=(ylo << (8 - BIN_DIV_STEPS_A2NLSF_FIX)) + (den >> 1);
        if (den != 0) {
          ffrac+=nom / den;
        }
      }
 else {
        ffrac+=ylo / ((ylo - yhi) >> (8 - BIN_DIV_STEPS_A2NLSF_FIX));
      }
      if (OVERSAMPLE_COSINE_TABLE)       NLSF[root_ix]=Math.min((k << 7) + ffrac,SKP_int16_MAX);
 else       NLSF[root_ix]=Math.min((k << 8) + ffrac,SKP_int16_MAX);
      assert(NLSF[root_ix] >= 0);
      assert(NLSF[root_ix] <= 32767);
      root_ix++;
      if (root_ix >= d) {
        break;
      }
      p=PQ[root_ix & 1];
      if (OVERSAMPLE_COSINE_TABLE)       xlo=LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[(k - 1) >> 1] + ((LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[k >> 1] - LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[(k - 1) >> 1]) >> 1);
 else       xlo=LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[k - 1];
      ylo=(1 - (root_ix & 2)) << 12;
    }
 else {
      k++;
      xlo=xhi;
      ylo=yhi;
      if (OVERSAMPLE_COSINE_TABLE) {
        if (k > 2 * SigProcFIX.LSF_COS_TAB_SZ_FIX) {
          i++;
          if (i > MAX_ITERATIONS_A2NLSF_FIX) {
            NLSF[0]=(1 << 15) / (d + 1);
            for (k=1; k < d; k++) {
              NLSF[k]=SKP_SMULBB(k + 1,NLSF[0]);
            }
            return;
          }
          Bwexpander32.SKP_Silk_bwexpander_32(a_Q16,d,65536 - SKP_SMULBB(66,i));
          SKP_Silk_A2NLSF_init(a_Q16,P,Q,dd);
          p=P;
          xlo=LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[0];
          ylo=SKP_Silk_A2NLSF_eval_poly(p,xlo,dd);
          if (ylo < 0) {
            NLSF[0]=0;
            p=Q;
            ylo=SKP_Silk_A2NLSF_eval_poly(p,xlo,dd);
            root_ix=1;
          }
 else {
            root_ix=0;
          }
          k=1;
        }
      }
 else {
        if (k > SigProcFIX.LSF_COS_TAB_SZ_FIX) {
          i++;
          if (i > MAX_ITERATIONS_A2NLSF_FIX) {
            NLSF[0]=(1 << 15) / (d + 1);
            for (k=1; k < d; k++) {
              NLSF[k]=SKP_SMULBB(k + 1,NLSF[0]);
            }
            return;
          }
          Bwexpander32.SKP_Silk_bwexpander_32(a_Q16,d,65536 - SKP_SMULBB(66,i));
          SKP_Silk_A2NLSF_init(a_Q16,P,Q,dd);
          p=P;
          xlo=LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[0];
          ylo=SKP_Silk_A2NLSF_eval_poly(p,xlo,dd);
          if (ylo < 0) {
            NLSF[0]=0;
            p=Q;
            ylo=SKP_Silk_A2NLSF_eval_poly(p,xlo,dd);
            root_ix=1;
          }
 else {
            root_ix=0;
          }
          k=1;
        }
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * Compute Normalized Line Spectral Frequencies (NLSFs) from whitening filter coefficients. If not all roots are found, the a_Q16 coefficients are bandwidth expanded until convergence.
 * @param NLSF Normalized Line Spectral Frequencies, Q15 (0 - (2^15-1)), [d]
 * @param a_Q16 Monic whitening filter coefficients in Q16 [d]
 * @param d Filter order (must be even)
 */
static void SKP_Silk_A2NLSF(int[] NLSF,int[] a_Q16,final int d){
  int i, k, m, dd, root_ix, ffrac;
  int xlo, xhi, xmid;
  int ylo, yhi, ymid;
  int nom, den;
  int[] P=new int[SigProcFIX.SKP_Silk_MAX_ORDER_LPC / 2 + 1];
  int[] Q=new int[SigProcFIX.SKP_Silk_MAX_ORDER_LPC / 2 + 1];
  int[][] PQ=new int[2][];
  int[] p;
  PQ[0]=P;
  PQ[1]=Q;
  dd=d >> 1;
  SKP_Silk_A2NLSF_init(a_Q16,P,Q,dd);
  p=P;
  xlo=LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[0];
  ylo=SKP_Silk_A2NLSF_eval_poly(p,xlo,dd);
  if (ylo < 0) {
    NLSF[0]=0;
    p=Q;
    ylo=SKP_Silk_A2NLSF_eval_poly(p,xlo,dd);
    root_ix=1;
  }
 else {
    root_ix=0;
  }
  k=1;
  i=0;
  while (true) {
    if (OVERSAMPLE_COSINE_TABLE) {
      xhi=LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[k >> 1] + ((LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[(k + 1) >> 1] - LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[k >> 1]) >> 1);
    }
 else {
      xhi=LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[k];
    }
    yhi=SKP_Silk_A2NLSF_eval_poly(p,xhi,dd);
    if ((ylo <= 0 && yhi >= 0) || (ylo >= 0 && yhi <= 0)) {
      if (OVERSAMPLE_COSINE_TABLE)       ffrac=-128;
 else       ffrac=-256;
      for (m=0; m < BIN_DIV_STEPS_A2NLSF_FIX; m++) {
        xmid=SigProcFIX.SKP_RSHIFT_ROUND(xlo + xhi,1);
        ymid=SKP_Silk_A2NLSF_eval_poly(p,xmid,dd);
        if ((ylo <= 0 && ymid >= 0) || (ylo >= 0 && ymid <= 0)) {
          xhi=xmid;
          yhi=ymid;
        }
 else {
          xlo=xmid;
          ylo=ymid;
          if (OVERSAMPLE_COSINE_TABLE)           ffrac=ffrac + (64 >> m);
 else           ffrac=ffrac + (128 >> m);
        }
      }
      if (Math.abs(ylo) < 65536) {
        den=ylo - yhi;
        nom=(ylo << (8 - BIN_DIV_STEPS_A2NLSF_FIX)) + (den >> 1);
        if (den != 0) {
          ffrac+=nom / den;
        }
      }
 else {
        ffrac+=ylo / ((ylo - yhi) >> (8 - BIN_DIV_STEPS_A2NLSF_FIX));
      }
      if (OVERSAMPLE_COSINE_TABLE)       NLSF[root_ix]=Math.min((k << 7) + ffrac,SKP_int16_MAX);
 else       NLSF[root_ix]=Math.min((k << 8) + ffrac,SKP_int16_MAX);
      assert(NLSF[root_ix] >= 0);
      assert(NLSF[root_ix] <= 32767);
      root_ix++;
      if (root_ix >= d) {
        break;
      }
      p=PQ[root_ix & 1];
      if (OVERSAMPLE_COSINE_TABLE)       xlo=LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[(k - 1) >> 1] + ((LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[k >> 1] - LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[(k - 1) >> 1]) >> 1);
 else       xlo=LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[k - 1];
      ylo=(1 - (root_ix & 2)) << 12;
    }
 else {
      k++;
      xlo=xhi;
      ylo=yhi;
      boolean b;
      if (OVERSAMPLE_COSINE_TABLE)       b=(k > 2 * SigProcFIX.LSF_COS_TAB_SZ_FIX);
 else       b=(k > SigProcFIX.LSF_COS_TAB_SZ_FIX);
      if (b) {
        i++;
        if (i > MAX_ITERATIONS_A2NLSF_FIX) {
          NLSF[0]=(1 << 15) / (d + 1);
          for (k=1; k < d; k++) {
            NLSF[k]=SKP_SMULBB(k + 1,NLSF[0]);
          }
          return;
        }
        Bwexpander32.SKP_Silk_bwexpander_32(a_Q16,d,65536 - SKP_SMULBB(10 + i,i));
        SKP_Silk_A2NLSF_init(a_Q16,P,Q,dd);
        p=P;
        xlo=LSFCosTable.SKP_Silk_LSFCosTab_FIX_Q12[0];
        ylo=SKP_Silk_A2NLSF_eval_poly(p,xlo,dd);
        if (ylo < 0) {
          NLSF[0]=0;
          p=Q;
          ylo=SKP_Silk_A2NLSF_eval_poly(p,xlo,dd);
          root_ix=1;
        }
 else {
          root_ix=0;
        }
        k=1;
      }
    }
  }
}