Originally posted by bryant
I have not looked at the FLAC code at all so I don't know where this came from, but it doesn't necessarily mean that the code isn't portable to another floating-point implementation.
It is not portable and it do not work properly.
This would only be a problem if it was required that the operation be performed identically in the encoder and decoder.
I would NEVER use a format not requiring that.
For example, if this statement was calculating some bit count in the encoder that was then literally transmitted in the datastream, and that the decoder simply read this value and acted on it, there would be no portability problem.
Nonsense!
If you miss to store the MSB, you can't reconstruct the value right.
Intel:
1 + log(/log(2) = 4.0000000000000000001 => 4 bits are stored (1000)
log(2) as IEEE-854 is rounded down, so the result is slightly too high.
Cray:
1 + log(8)/log(2) = 3.999999999999998 => 3 bits are stored (000)
1./log(2) as IEEE-754 is rounded down, so the result is slightly too low.
You can't distinguish the value from 0 (000).
It would just mean that the encoder would generate different datastreams on different computers but that both would be playable by any decoder. Not an ideal scenario, but not a showstopper either.
Wrong. Using the brain or some tests will show this perfectly. I tested both methods.
It is not working and it is not portable. You can't decode files encoded on a Cray even on a Cray (fsuj01.rz.uni-jena.de).
And log() is extremly slow compared to all other solutions (250 clocks on P4). Best would be a table based solution:
int bits ( unsigned __int32 value )
{
static unsigned char table [256] = {
0,1,2,2,3,3,3,3,....
.....,8
};
if ( value < 0x0000100) return table[value];
if ( value < 0x0010000) return table[value >> 8] + 8;
if ( value < 0x1000000) return table[value >> 16] + 16;
return table[value >> 24] + 24;
}
Fast, portable, easy to read.
I sent this solution twice to jcoalson. He isn't interested in portable code.