Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Can be used to efficiently encode floating point values from a limited range ([0..65k]) into a byte
- // edgeDecode is optimized for GPUs (native exp2)
- // edgeEncode1 is optimized for GPUs (native log2)
- // edgeEncode2 is optimized for CPUs
- float edgeDecode(int e)
- {
- return pow(2.0f, float(e) / 16) - 1;
- }
- int edgeEncode1(float f)
- {
- return log2f(f + 1) * 16 + 0.5f;
- }
- int edgeEncode2(float f)
- {
- // This is an approximation of log2(f + 1) * 16 + 0.5
- // Instead of computing log2 precisely, we compute integer part by taking the floating point exponent
- // and take the fractional part as is (rounded); we can refine it with a table lookup but the precision we get
- // after reconstruction is reasonable as it is
- union { float f; unsigned ui; } u;
- u.f = f + 1;
- unsigned v = u.ui;
- return (v - (127 << 23) + (1 << 18)) >> 19;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement