Skip to content
Snippets Groups Projects
Commit 1cb32aa0 authored by Timothy B. Terriberry's avatar Timothy B. Terriberry Committed by Jean-Marc Valin
Browse files

Fix rounding in bits2pulses search.

The mid = (lo+hi)>>1 line in the binary search would allow hi to drop
 down to the same value as lo, meaning the rounding after the search
 would be choosing between the same two values.
This patch changes it to (lo+hi+1)>>1.
This will allow lo to increase up to the value hi, but only in the
 case that we can't possibly allocate enough pulses to meet the
 target number of bits (in which case the rounding doesn't matter).
To pay for the extra add, this moves the +1 in the comparison to bits
 to the other side, which can then be taken outside the loop.
The compiler can't normally do this because it might cause overflow
 which would change the results.

This rarely mattered, but gives a 0.01 PEAQ improvement on 12-byte
 120 sample frames.
It also makes the search process describable with a simple
 algorithm, rather than relying on this particular optimized
 implementation.
I.e., the binary search loop can now be replaced with
  for(lo=0;lo+1<cache[0]&&cache[lo+1]<bits;lo++);
  hi=lo+1;
 and it will give equivalent results.
This was not true before.
parent 845dfa19
No related branches found
No related tags found
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment