Skip to content

Commit

Permalink
use constant for total so that it will be converted to bit shift by c…
Browse files Browse the repository at this point in the history
…ompiler

git-svn-id: http://svn.coderepos.org/share/lang/cplusplus/range_coder@7161 d0d07461-0603-4401-acd4-de1884942a52
  • Loading branch information
kazuho committed Feb 26, 2008
1 parent ed218c6 commit e311db8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
5 changes: 2 additions & 3 deletions bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ int main(int argc, char **argv)
ch = to_ordered[ch];
#endif
assert(freq[ch] != freq[ch + 1]);
enc.encode(freq[ch] - FREQ_BASE, freq[ch + 1] - FREQ_BASE,
freq[256] - FREQ_BASE);
enc.encode(freq[ch] - FREQ_BASE, freq[ch + 1] - FREQ_BASE, MAX_FREQ);
}
enc.final();
cbuflen = cbufpt - cbuf;
Expand All @@ -64,7 +63,7 @@ int main(int argc, char **argv)
rc_decoder_t<const char*, rc_decoder_search_t<short, 256, FREQ_BASE> >
dec(cbuf, cbuf + cbuflen);
for (char *p = rbuf, *e = rbuf + buflen; p != e; p++) {
unsigned ch = dec.decode(freq[256] - FREQ_BASE, freq);
unsigned ch = dec.decode(MAX_FREQ, freq);
#ifdef USE_ORDERED_TABLE
ch = from_ordered[ch];
#endif
Expand Down
22 changes: 17 additions & 5 deletions build_table.pl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use warnings;

use Getopt::Long;
use List::Util qw/sum/;
use List::Util qw/sum max/;

my ($do_ordered);

Expand Down Expand Up @@ -33,10 +33,22 @@
my @freq;
my $acc = 0;
my $cc = sum @cnt;
for (my $i = 0; $i < 256; $i++) {
push @freq, $acc;
$acc += int(($cnt[$i] / $cc) * 0xfe00);
my ($mult, $mult_diff) = (0x8000, 0);
while (1) {
$acc = 0;
for (my $i = 0; $i < 256; $i++) {
push @freq, $acc;
$acc += $cnt[$i] != 0 ? max(int($cnt[$i] / $cc * $mult + 0.5), 1) : 0;
}
last if $acc == 0x8000;
@freq = ();
$mult_diff = $mult_diff != 0 ? $mult_diff / 2 : abs($acc - 0x8000);
if ($acc < 0x8000) {
$mult += $mult_diff;
} else {
$mult -= $mult_diff;
}
}
push @freq, $acc;

print "#define MAX_FREQ 0x8000\n";
print "static short freq[] __attribute__((aligned(16))) = {", join(',', map { $_ - 0x8000 } @freq), "};\n";

0 comments on commit e311db8

Please sign in to comment.