Skip to content

Commit e311db8

Browse files
author
kazuho
committed
use constant for total so that it will be converted to bit shift by compiler
git-svn-id: http://svn.coderepos.org/share/lang/cplusplus/range_coder@7161 d0d07461-0603-4401-acd4-de1884942a52
1 parent ed218c6 commit e311db8

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

bench.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ int main(int argc, char **argv)
5151
ch = to_ordered[ch];
5252
#endif
5353
assert(freq[ch] != freq[ch + 1]);
54-
enc.encode(freq[ch] - FREQ_BASE, freq[ch + 1] - FREQ_BASE,
55-
freq[256] - FREQ_BASE);
54+
enc.encode(freq[ch] - FREQ_BASE, freq[ch + 1] - FREQ_BASE, MAX_FREQ);
5655
}
5756
enc.final();
5857
cbuflen = cbufpt - cbuf;
@@ -64,7 +63,7 @@ int main(int argc, char **argv)
6463
rc_decoder_t<const char*, rc_decoder_search_t<short, 256, FREQ_BASE> >
6564
dec(cbuf, cbuf + cbuflen);
6665
for (char *p = rbuf, *e = rbuf + buflen; p != e; p++) {
67-
unsigned ch = dec.decode(freq[256] - FREQ_BASE, freq);
66+
unsigned ch = dec.decode(MAX_FREQ, freq);
6867
#ifdef USE_ORDERED_TABLE
6968
ch = from_ordered[ch];
7069
#endif

build_table.pl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use warnings;
55

66
use Getopt::Long;
7-
use List::Util qw/sum/;
7+
use List::Util qw/sum max/;
88

99
my ($do_ordered);
1010

@@ -33,10 +33,22 @@
3333
my @freq;
3434
my $acc = 0;
3535
my $cc = sum @cnt;
36-
for (my $i = 0; $i < 256; $i++) {
37-
push @freq, $acc;
38-
$acc += int(($cnt[$i] / $cc) * 0xfe00);
36+
my ($mult, $mult_diff) = (0x8000, 0);
37+
while (1) {
38+
$acc = 0;
39+
for (my $i = 0; $i < 256; $i++) {
40+
push @freq, $acc;
41+
$acc += $cnt[$i] != 0 ? max(int($cnt[$i] / $cc * $mult + 0.5), 1) : 0;
42+
}
43+
last if $acc == 0x8000;
44+
@freq = ();
45+
$mult_diff = $mult_diff != 0 ? $mult_diff / 2 : abs($acc - 0x8000);
46+
if ($acc < 0x8000) {
47+
$mult += $mult_diff;
48+
} else {
49+
$mult -= $mult_diff;
50+
}
3951
}
40-
push @freq, $acc;
4152

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

0 commit comments

Comments
 (0)