Core per-pixel transform, fully inlined. All uniform values are precomputed by the caller and passed in to avoid per-pixel branches.
| 34 | // Core per-pixel transform, fully inlined. All uniform values are |
| 35 | // precomputed by the caller and passed in to avoid per-pixel branches. |
| 36 | FL_ALWAYS_INLINE void five_bit_pixel( |
| 37 | u16 r16, u16 g16, u16 b16, u8 brightness, |
| 38 | // Precomputed: (1 + brightness), or 0 if brightness==0xff (skip scaling) |
| 39 | u16 bright_p1, bool apply_brightness, |
| 40 | CRGB *out, u8 *out_power_5bit) { |
| 41 | |
| 42 | // All-zero fast path (rare but worth checking — writes are cheap). |
| 43 | if ((r16 | g16 | b16) == 0) { |
| 44 | *out = CRGB(0, 0, 0); |
| 45 | *out_power_5bit = (brightness <= 31) ? brightness : 31; |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | // Apply brightness: branchless multiply, skip only if brightness==0xff. |
| 50 | if (apply_brightness) { |
| 51 | r16 = scale16by8_nozero(r16, bright_p1); |
| 52 | g16 = scale16by8_nozero(g16, bright_p1); |
| 53 | b16 = scale16by8_nozero(b16, bright_p1); |
| 54 | } |
| 55 | |
| 56 | // max3 via branchless ternary (compiler emits cmov on x86, csel on ARM). |
| 57 | u16 scale = r16; |
| 58 | if (g16 > scale) scale = g16; |
| 59 | if (b16 > scale) scale = b16; |
| 60 | |
| 61 | if (scale == 0) { |
| 62 | *out = CRGB(0, 0, 0); |
| 63 | *out_power_5bit = 0; |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | // 5-bit quantized scale at or above maximum value. |
| 68 | scale = (scale + (2047 - (scale >> 5))) >> 11; |
| 69 | |
| 70 | u32 scalef = FL_PGM_READ_DWORD_ALIGNED(&BRIGHT_SCALE[scale]); |
| 71 | u8 r8 = static_cast<u8>((r16 * scalef + 0x808000) >> 24); |
| 72 | u8 g8 = static_cast<u8>((g16 * scalef + 0x808000) >> 24); |
| 73 | u8 b8 = static_cast<u8>((b16 * scalef + 0x808000) >> 24); |
| 74 | |
| 75 | *out = CRGB(r8, g8, b8); |
| 76 | *out_power_5bit = static_cast<u8>(scale); |
| 77 | } |
| 78 | |
| 79 | } // namespace five_bit_impl |
| 80 | } // anonymous namespace |
no test coverage detected