FastLED 3.9.15
Loading...
Searching...
No Matches

◆ div1024_32_16()

LIB8STATIC uint16_t div1024_32_16 ( uint32_t in32)

Helper routine to divide a 32-bit value by 1024, returning only the low 16 bits.

You'd think this would be just

result = (in32 >> 10) & 0xFFFF;

And on ARM, that's what you want and all is well. But on AVR that code turns into a loop that executes a four-byte shift ten times: 40 shifts in all, plus loop overhead. This routine gets exactly the same result with just six shifts (vs 40), and no loop overhead. Used to convert millis to "binary seconds" aka bseconds: one bsecond == 1024 millis.

Definition at line 1040 of file lib8tion.h.

1041{
1042 uint16_t out16;
1043#if defined(__AVR__)
1044 asm volatile (
1045 " lsr %D[in] \n\t"
1046 " ror %C[in] \n\t"
1047 " ror %B[in] \n\t"
1048 " lsr %D[in] \n\t"
1049 " ror %C[in] \n\t"
1050 " ror %B[in] \n\t"
1051 " mov %B[out],%C[in] \n\t"
1052 " mov %A[out],%B[in] \n\t"
1053 : [in] "+r" (in32),
1054 [out] "=r" (out16)
1055 );
1056#else
1057 out16 = (in32 >> 10) & 0xFFFF;
1058#endif
1059 return out16;
1060}

References LIB8STATIC.

Referenced by bseconds16().

+ Here is the caller graph for this function: