Optimize quantizeUnorm/quantizeSnorm

Make sure clamp branches are never taken unless we need to clamp -
ideally we're expecting the compiler to generate maxss/minss
instructions here (which clang does, but other compilers like gcc don't
do), but if that doesn't work we'd rather have a 100% predictable
branch.

Also compute the sign for rounding pre-clamp in quantizeSnorm - this
makes the dependency chain shorter and makes gcc generate much better
code.
This commit is contained in:
Arseny Kapoulkine
2017-06-05 08:52:03 -07:00
parent b0e1104006
commit cd53d2c173
+7 -5
View File
@@ -92,8 +92,8 @@ inline int quantizeUnorm(float v, int bits)
{
const float scale = float((1 << bits) - 1);
v = (v > 0) ? v : 0;
v = (v < 1) ? v : 1;
v = (v >= 0) ? v : 0;
v = (v <= 1) ? v : 1;
return int(v * scale + 0.5f);
}
@@ -106,10 +106,12 @@ inline int quantizeSnorm(float v, int bits)
{
const float scale = float((1 << (bits - 1)) - 1);
v = (v > -1) ? v : -1;
v = (v < +1) ? v : +1;
float round = (v >= 0 ? 0.5f : -0.5f);
return int(v * scale + (v >= 0 ? 0.5f : -0.5f));
v = (v >= -1) ? v : -1;
v = (v <= +1) ? v : +1;
return int(v * scale + round);
}
// Quantize a float into half-precision floating point value