After some latency measurement tests, I figured out I need to optimize an pythagoras triangle calculation done on an embedded CPU with a pretty slow FPU.
The problem is if these calculations occur, they come in numbers and this messes up the timing. I cannot reduce the absolute number of calculations. But somehow they need to get faster ... by at least factor 5. :-/
I'm currently thinking of pre-processing these calculations since the input range of distinct values is somehow limited to about 300-500 permutations and interpolation between two table entry should suffice. But I was also wondering if using some conditions to the problem it might be possible to also speed up this code:
float h = 0.f, v=0.f;
/// ...
float const d = std::sqrt( (h*h) + (v*v) );
This I haven't used yet:
I don't know if some integer fixed point calculation are available fort square root or if the function can be substituted to a one with less accuracy or somehow using the aspect ratio.
Any ideas?
Thank you!
If you know the ratio is 16:9, you can do a little algebra:
h = 16*x
v = 9*x
x = h/16
sqrt((h*h) + (v*v)) = sqrt((16*16*x*x) + (9*9*x*x))
= sqrt((16*16+9*9)*x*x)
= sqrt(16*16+9*9)*x
= sqrt(16*16+9*9)*h/16
= sqrt(16*16+9*9)/16 * h
pre-compute sqrt(16*16+9*9)/16
:
static float const multiplier = std::sqrt(16*16+9*9)/16.0;
and use
float const d = multiplier*h;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With