converting uint8 value normalized float pretty straight forward:
uint8 = 0xaf; float f = float(i)/float(0xff);
but feels more expensive should be...
is there way of making conversion that's more efficient? ask out of curiosity, because 3d program makes conversion significant number of times.
readability isn't important, , uint8 span entire range
0 == 0.f
, 255 == 1.f
here operations see:
at compile time:
- convert 0xff floating point constant.
at runtime:
- convert
i
floating point , store in temporary variable (or register). - divide floating point value of
i
floating point constant. - assign result of division floating point variable
f
.
bottlenecks:
the bottleneck in division. division takes long time, period (regardless of how implemented).
the next major bottleneck may conversion of integer float. processors may have single instructions perform this; otherwise software function executed (usually faster division).
to optimize:
- get rid of division. use method such shifting or table lookup.
- minimize floating point conversion. convert necessary, @ input , output. stay in integral or stay in floating point.
notes:
- hardcoded constants fast -- compiler stores in memory , execution takes memory. compiler doesn't need calculate.
- constant expressions faster, slow down compilation (probably negligible). compiler performs calculation , places result in executable.
- multiplication faster division.
- integral math faster floating point because floating point format needs separated, before calculations, recombined afterwords (even in hardware, there more work simple integral operations).
Comments
Post a Comment