Skip to content

Instantly share code, notes, and snippets.

@egonelbre
Created October 12, 2014 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egonelbre/261a0e093a4c900e57e5 to your computer and use it in GitHub Desktop.
Save egonelbre/261a0e093a4c900e57e5 to your computer and use it in GitHub Desktop.
making floats packable
// add here more values if more range is needed
#define ilog(x) x < 2 ? 1 : x < 4 ? 2 : x < 8 ? 3 : x < 16 ? 4 : x < 32 ? 8 : x < 64 ? 9 : x < 128 ? 10 : \
x < 256 ? 11 : x < 512 ? 12 : 13
// imprecision macro P4 ( 4 bytes for float )
// (0.00 .. 1.99)
// avg err = 0.00375
// max err = 0.0075
// (2.00 .. 3.99)
// avg err = 0.0075
// max err = 0.0150
// (4.00 .. 7.99)
// avg err = 0.0150
// max err = 0.0300
// works in range (0.0 ... 4111.9)
#define _P(x,y) ((float)((int)(x*(int)y) & ~((1 << (ilog(x) - 1)) - 1) )/y);
#define P4(x) _P(x,256.0f)
// P3 (0.00 .. 1.99) avg 0.06, max 0.12
#define P3(x) _P(x,16.0f)
// P5 (0.00 .. 1.99) avg 0.00023, max 0.00046
#define P5(x) _P(x,4096.0f)
// for global precision changes
#define P(x) P4(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment