While employing the classical arithmetic compression implementation [1], I found it possible to make some optimizations that significantly increase compression speed, especially on 32+-bit platforms. These optimizations do not alter the original algorithm, are absolutely compatible, and I have not found them mentioned so far. Considerations and changes proposed below are based on the original implementation [1] and hence some acquaintance with it is assumed.
Silverlight 2 SDK for Visual Studio 2008
This package is an add-on to the RTM release of Visual Studio 2008 to provide tooling for Microsoft Silverlight 2 Beta 1. It provides a Silverlight project system for developing Silverlight applications using C# or Visual Basic.
»
Article: What Does it Take to Build the Best RIA?
With the proliferation of Rich Interactive Application (RIA) platform choices out there, you no longer have to take a one-size-fits-all approach to developing your next RIA application. Knowing the strengths (and weaknesses) of each platform can help you to decide the best RIA for your next application.
»
Expression Blend 2.5 Preview
Use Expression Blend 2.5 to create and modify managed Silverlight 2-based applications. Expression Blend for Silverlight 2 includes all of the features in Expression Blend 2 but has not reached the quality level of Expression Blend 2 for WPF or Silverlight 1 development. »
The Hottest Mobile Platform Meets the Hottest RIA Platform
With the Symbian OS now supporting Microsoft Silverlight, mobile developers can bring new and exciting capabilities to handsets all over the globe. Find out why developers now need to make mobile devices a core part of their RIA development strategy.
»
Article: Leveraging Your Flash Development with Silverlight
You're not giving up Flash any time soon (and we don't blame you.) But if you could get your Flash application working in Silverlight, why wouldn't you? We show you the tools and techniques required to have your rockin' Flash application rolled for Silverlight.
»
It can be seen that, while looping inside encode_symbol() and decode_symbol(), as soon as the conditions high<Half and low>=Half are not met, they won't be met again. Moreover, a "bits plus follow" call is needed only once at the beginning; then "output bit" is enough. Here is the encode method structure changed:
do
{
if (high<Half)
{
bit plus follow 0;
}
elseif (low>=Half)
{
bit plus follow 1;
low -= Half;
high -= Half;
}
elsebreak;
for (;;)
{
low = 2*low;
high = 2*high+1;
if (high<Half)
{
output bit 0;
}
elseif (low>=Half)
{
output bit 1;
low -= Half;
high -= Half;
}
elsebreak;
}
}
while (false);
while (low>=First_qtr && high<Third_qtr)
{
bits_to_follow++;
low = 2 * (low - First_qtr); // Subtract offset to middle
high = 2 * (high - First_qtr) + 1;
}
Because only 16 bits of low and high variables are used, these variables are substituted by the combined low_high one.
low_high = low + ((~high) << 16);
Variable high binary negation makes the next optimizations possible: As soon as ~X is equal to -X - 1, ~((~X) << 1) stands for 2 * X + 1. Then,
low = 2*low;
high = 2*high+1;
can be substituted by just:
low_high <<= 1;
The cycle with checking
while (low>=First_qtr && high<Third_qtr)
can be reduced to (taking into account that conditions high<Half and low>=Half are met):
Flushing bits to follow can be optimized as well. The flush_bits_to_follow helper is introduced; it simply puts the required amount of one or zero bits to the output:
void bit_plus_follow_0()
{
output_bit_0(); /* Output the bit. */while (bits_to_follow>0) {
buffer >>= 1; buffer |= 0x80;
/* Put bit in top of buffer.*/if (--bits_to_go==0) {
/* Output buffer if it is */
flush_bits_to_follow(255);
return;
}
bits_to_follow--;
/* opposite bits. Set */
} /* bits_to_follow to zero. */
}
void output_bit_0()
{
buffer >>= 1; //if (bit) buffer |= 0x80;/* Put bit in top of buffer.*/if (--bits_to_go==0) {
/* Output buffer if it is */
PutByte_(buffer);
/* now full. */
bits_to_go = 8;
}
}
void flush_bits_to_follow(int buffer_)
{
for (;;)
{
PutByte_(buffer); /* now full. */
buffer = buffer_;
if (bits_to_follow <= 8)
break;
bits_to_follow -= 8;
}
bits_to_go = 9 - bits_to_follow;
bits_to_follow = 0;
}
However, there is a drawback concerned with code value buffer capacity limitation. It can be fixed by changing the way conditions are checked. For maximal code value buffer size (32 bits for IA32 in this example), the algorithm encoding loop body fragment,
for (;;)
{
if (high < Half)
{
bit plus follow 0;
}
elseif (low >= Half)
{
bit plus follow 1;
low -= Half;
high -= Half;
}
elsebreak;
low = 2*low;
high = 2*high+1;
}
with introduction of the special flags,
long half_flags = ~(low | not_high);
can be changed to:
int shift = num_clear_bits(half_flags);
if (shift != 0)
{
bit_plus_follow(low & Half);
// This loop can be definitely optimizedunsignedlong flag = Half;
for (int i = shift; --i > 0;)
{
flag >>= 1;
output_bit(low & flag);
}
low <<= shift;
not_high <<= shift;
}
where num_clear_bits uses an algorithm similar to one of calculating an integer's logarithm [2]:
The decoding routine can be modified in a similar way. Optimizing multiple bits input/output can further accelerate it.
The complete modified sources can be downloaded as parts of the encoder/decoder code samples. They are implemented as template classes to ease their use with various data providers and consumers.
References
[1] Witten, I. H., Neal, R. M., and Cleary, J. G. (1987) "Arithmetic coding for data compression", Communications of the ACM, vol. 30, no. 6 (June).
Add www.codeguru.com to your favorites Add www.codeguru.com to your browser search box IE 7 | Firefox 2.0 | Firefox 1.5.xReceive news via our XML/RSS feed