Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. // Can be used to efficiently encode floating point values from a limited range ([0..65k]) into a byte
  2. // edgeDecode is optimized for GPUs (native exp2)
  3. // edgeEncode1 is optimized for GPUs (native log2)
  4. // edgeEncode2 is optimized for CPUs
  5. float edgeDecode(int e)
  6. {
  7. return pow(2.0f, float(e) / 16) - 1;
  8. }
  9.  
  10. int edgeEncode1(float f)
  11. {
  12. return log2f(f + 1) * 16 + 0.5f;
  13. }
  14.  
  15. int edgeEncode2(float f)
  16. {
  17. // This is an approximation of log2(f + 1) * 16 + 0.5
  18. // Instead of computing log2 precisely, we compute integer part by taking the floating point exponent
  19. // and take the fractional part as is (rounded); we can refine it with a table lookup but the precision we get
  20. // after reconstruction is reasonable as it is
  21. union { float f; unsigned ui; } u;
  22. u.f = f + 1;
  23. unsigned v = u.ui;
  24.  
  25. return (v - (127 << 23) + (1 << 18)) >> 19;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement