Skip to content

Commit d6dedd0

Browse files
committed
color_fade optimizations
* removed unnecessary conditions * optimized bit-shifting logic * use uint16_t for colors, to prevent the compiler from using 64bit integer
1 parent e756216 commit d6dedd0

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

wled00/colors.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,27 @@ IRAM_ATTR_YN __attribute__((hot)) uint32_t color_fade(uint32_t c1, uint8_t amoun
8484
if (amount == 255) return c1; // WLEDMM small optimization - plus it avoids over-fading in "video" mode
8585
if (amount == 0) return 0; // WLEDMM shortcut
8686

87-
uint32_t scaledcolor; // color order is: W R G B from MSB to LSB
88-
uint32_t r = R(c1);
89-
uint32_t g = G(c1);
90-
uint32_t b = B(c1);
91-
uint32_t w = W(c1);
87+
uint32_t scaledcolor = 0; // color order is: W R G B from MSB to LSB
88+
uint16_t w = W(c1); // WLEDMM 16bit to make sure the compiler uses 32bit (not 64bit) for the math
89+
uint16_t r = R(c1);
90+
uint16_t g = G(c1);
91+
uint16_t b = B(c1);
9292
if (video) {
93-
uint32_t scale = amount; // 32bit for faster calculation
94-
scaledcolor = (((r * scale) >> 8) << 16) + ((r && scale) ? 1 : 0);
95-
scaledcolor |= (((g * scale) >> 8) << 8) + ((g && scale) ? 1 : 0);
96-
scaledcolor |= ((b * scale) >> 8) + ((b && scale) ? 1 : 0);
97-
if (w>0) scaledcolor |= (((w * scale) >> 8) << 24) + ((scale) ? 1 : 0); // WLEDMM small speedup when no white channel
93+
uint16_t scale = amount; // 32bit for faster calculation
94+
// bugfix: doing "+1" after shifting is obviously wrong
95+
// optimization: ((r && scale) ? 1 : 0) can be simplified to "if (r > 0) +1" ; if we arive here, then scale != 0 and scale < 255
96+
if (w>0) scaledcolor |= (((w * scale) >> 8) +1) << 24; // WLEDMM small speedup when no white channel
97+
if (r>0) scaledcolor |= (((r * scale) >> 8) +1) << 16;
98+
if (g>0) scaledcolor |= (((g * scale) >> 8) +1) << 8;
99+
if (b>0) scaledcolor |= ((b * scale) >> 8) +1;
98100
return scaledcolor;
99101
}
100102
else {
101-
uint32_t scale = 1 + amount;
102-
scaledcolor = ((r * scale) >> 8) << 16;
103-
scaledcolor |= ((g * scale) >> 8) << 8;
104-
scaledcolor |= (b * scale) >> 8;
103+
uint16_t scale = 1 + amount;
105104
if (w>0) scaledcolor |= ((w * scale) >> 8) << 24; // WLEDMM small speedup when no white channel
105+
scaledcolor |= ((r * scale) >> 8) << 16;
106+
scaledcolor |= (g * scale) & 0x0000FF00; // WLEDMM faster than right-left shift "" >>8 ) <<8"
107+
scaledcolor |= (b * scale) >> 8;
106108
return scaledcolor;
107109
}
108110
}

wled00/wled.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
// version code in format yymmddb (b = daily build)
11-
#define VERSION 2411140
11+
#define VERSION 2411150
1212

1313
// WLEDMM - you can check for this define in usermods, to only enabled WLEDMM specific code in the "right" fork. Its not defined in AC WLED.
1414
#define _MoonModules_WLED_

0 commit comments

Comments
 (0)