On Tuesday, September 14, 2010 3:38:47 am Henri Verbeet wrote:
Is that correct in the first place though? This will produce a non-uniform mapping for positive and negative values. IIRC the common way to do these kinds of mappings is "((x * (2^n - 1)) - 1) / 2", which would be "x * 127.5f - .5f" for an 8 bit signed format. The main disadvantage of that method is that integer 0 doesn't correspond exactly to floating-point 0.0f, but in practice few people seem to care. Of course it all depends on how the target format is actually defined.
It likely doesn't matter, but it already has to check for values that exceed the -1..+1 range, so one more check to see if it's positive or negative won't make much of a difference, I don't think. Though if you'd rather do
if (*v < -1.0f) d = -128; else if (*v > 1.0f) d = 127; else d = *v * 127.5f - .5f;
..as an alternative, it's fine by me as long as no one else has an objection.