ref: 29354ff6e05c9ead9454981a7404a9b9ea203d2e
parent: ce1173c77fa2ed5e731db2bbd4c7527d57a8c335
author: Jean-Marc Valin <[email protected]>
date: Tue Jan 21 05:39:33 EST 2014
Save more integer divisions on ARM when we know the operands are positive
--- a/celt/bands.c
+++ b/celt/bands.c
@@ -281,7 +281,8 @@
N0 = m->eBands[i+1]-m->eBands[i];
/* depth in 1/8 bits */
- depth = (1+pulses[i])/((m->eBands[i+1]-m->eBands[i])<<LM);
+ celt_assert(pulses[i]>=0);
+ depth = celt_udiv(1+pulses[i], (m->eBands[i+1]-m->eBands[i])<<LM);
#ifdef FIXED_POINT
thresh32 = SHR32(celt_exp2(-SHL16(depth, 10-BITRES)),1);
@@ -491,7 +492,7 @@
/* Only include four last bands (8 kHz and up) */
if (i>m->nbEBands-4)
- hf_sum += 32*(tcount[1]+tcount[0])/N;
+ hf_sum += celt_udiv(32*(tcount[1]+tcount[0]), N);
tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
sum += tmp*256;
nbBands++;
@@ -501,7 +502,7 @@
if (update_hf)
{
if (hf_sum)
- hf_sum /= C*(4-m->nbEBands+end);
+ hf_sum = celt_udiv(hf_sum, C*(4-m->nbEBands+end));
*hf_average = (*hf_average+hf_sum)>>1;
hf_sum = *hf_average;
if (*tapset_decision==2)
@@ -517,7 +518,8 @@
}
/*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
celt_assert(nbBands>0); /* end has to be non-zero */
- sum /= nbBands;
+ celt_assert(sum>=0);
+ sum = celt_udiv(sum, nbBands);
/* Recursive averaging */
sum = (sum+*average)>>1;
*average = sum;
@@ -775,7 +777,8 @@
ec_dec_update(ec, fl, fl+fs, ft);
}
}
- itheta = (opus_int32)itheta*16384/qn;
+ celt_assert(itheta>=0);
+ itheta = celt_udiv((opus_int32)itheta*16384, qn);
if (encode && stereo)
{
if (itheta==0)
@@ -1089,7 +1092,7 @@
longBlocks = B0==1;
- N_B /= B;
+ N_B = celt_udiv(N_B, B);
/* Special case for one sample */
if (N==1)
--- a/celt/entcode.h
+++ b/celt/entcode.h
@@ -122,6 +122,7 @@
/* Tested exhaustively for all n and for 1<=d<=256 */
static OPUS_INLINE opus_uint32 celt_udiv(opus_uint32 n, opus_uint32 d) {
+ celt_assert(d>0);
#ifdef USE_SMALL_DIV_TABLE
if (d>256)
return n/d;
--- a/celt/pitch.c
+++ b/celt/pitch.c
@@ -460,7 +460,7 @@
opus_val16 g1;
opus_val16 cont=0;
opus_val16 thresh;
- T1 = (2*T0+k)/(2*k);
+ T1 = celt_udiv(2*T0+k, 2*k);
if (T1 < minperiod)
break;
/* Look for another strong correlation at T1b */
@@ -472,7 +472,7 @@
T1b = T0+T1;
} else
{
- T1b = (2*second_check[k]*T0+k)/(2*k);
+ T1b = celt_udiv(2*second_check[k]*T0+k, 2*k);
}
dual_inner_prod(x, &x[-T1], &x[-T1b], N, &xy, &xy2);
xy += xy2;
--- a/celt/rate.c
+++ b/celt/rate.c
@@ -333,7 +333,7 @@
/*Figure out how many left-over bits we would be adding to this band.
This can include bits we've stolen back from higher, skipped bands.*/
left = total-psum;
- percoeff = left/(m->eBands[codedBands]-m->eBands[start]);
+ percoeff = celt_udiv(left, m->eBands[codedBands]-m->eBands[start]);
left -= (m->eBands[codedBands]-m->eBands[start])*percoeff;
rem = IMAX(left-(m->eBands[j]-m->eBands[start]),0);
band_width = m->eBands[codedBands]-m->eBands[j];
@@ -414,7 +414,7 @@
/* Allocate the remaining bits */
left = total-psum;
- percoeff = left/(m->eBands[codedBands]-m->eBands[start]);
+ percoeff = celt_udiv(left, m->eBands[codedBands]-m->eBands[start]);
left -= (m->eBands[codedBands]-m->eBands[start])*percoeff;
for (j=start;j<codedBands;j++)
bits[j] += ((int)percoeff*(m->eBands[j+1]-m->eBands[j]));
@@ -465,7 +465,8 @@
offset += NClogN>>3;
/* Divide with rounding */
- ebits[j] = IMAX(0, (bits[j] + offset + (den<<(BITRES-1))) / (den<<BITRES));
+ ebits[j] = IMAX(0, (bits[j] + offset + (den<<(BITRES-1))));
+ ebits[j] = celt_udiv(ebits[j], den<<BITRES);
/* Make sure not to bust */
if (C*ebits[j] > (bits[j]>>BITRES))
--- a/celt/vq.c
+++ b/celt/vq.c
@@ -94,7 +94,7 @@
}
/*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for
extract_collapse_mask().*/
- len /= stride;
+ len = celt_udiv(len, stride);
for (i=0;i<stride;i++)
{
if (dir < 0)
@@ -143,7 +143,7 @@
return 1;
/*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for
exp_rotation().*/
- N0 = N/B;
+ N0 = celt_udiv(N, B);
collapse_mask = 0;
i=0; do {
int j;