replacement for fastled sqrt16() (#4426)

* added bitwise operation based sqrt16

- replacement for fastled, it is about 10% slower for numbers smaller 128 but faster for larger numbers. speed difference is irrelevant to WLED but it saves some flash.

* updated to 32bit, improved for typical WLED use

- making it 32bits allows for larger numbers
- added another initial condition check for medium sized numbers
- increased the "small number" optimization to larger numbers: the function is currently only used to calculate sqrt(x^2+y^2) which even for small segments is larger than the initially used 64, so optimizing for 1024 makes more sense, although the value is arbitrarily chosen
This commit is contained in:
Damian Schneider
2025-01-20 05:51:04 +01:00
committed by Frank
parent bf4aae8e60
commit 532c9b762c
4 changed files with 32 additions and 7 deletions

View File

@@ -932,7 +932,7 @@ uint16_t Segment::calc_virtualLength() const {
break;
case M12_pArc:
{ unsigned vLen2 = vW * vW + vH * vH; // length ^2
if (vLen2 < UINT16_MAX) vLen = sqrt16(vLen2); // use faster function for 16bit values
if (vLen2 < UINT16_MAX) vLen = sqrt32_bw(vLen2); // use faster function for 16bit values
else vLen = sqrtf(vLen2); // fall-back to float if bigger
if (vW != vH) vLen++; // round up
}