Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/FastLED/FastLED.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Isaacs <75862+ben-xo@users.noreply.github.com>2021-08-27 14:20:14 +0300
committerBen Isaacs <75862+ben-xo@users.noreply.github.com>2021-08-27 14:20:14 +0300
commitca59a45a2be94c5bc1f8067b538cbced3ba049d1 (patch)
tree79e62b4091abc3dfdec210632bd763024f2dd088
parented0a7edd2f89b80c84a950b9b046f40a96737034 (diff)
Refactor blend8() to apply the same optimisations on all platforms (in BLEND_C).
As noted on https://github.com/FastLED/FastLED/pull/1288, many platforms can eliminate an entire multiplication this way.
-rw-r--r--src/lib8tion/math8.h61
1 files changed, 37 insertions, 24 deletions
diff --git a/src/lib8tion/math8.h b/src/lib8tion/math8.h
index 61b12e6a..f95697bd 100644
--- a/src/lib8tion/math8.h
+++ b/src/lib8tion/math8.h
@@ -469,39 +469,52 @@ LIB8STATIC uint8_t sqrt16(uint16_t x)
#if (FASTLED_BLEND_FIXED == 1)
LIB8STATIC uint8_t blend8( uint8_t a, uint8_t b, uint8_t amountOfB)
{
-#if BLEND8_C == 1
+
+ // The BLEND_FIXED formula is
+ //
+ // result = ( A*(amountOfA) + B*(amountOfB) )/ 256
+ //
+ // …where amountOfA = 255-amountOfB.
+ //
+ // This formula will never return 255, which is why the BLEND_FIXED + SCALE8_FIXED version is
+ //
+ // result = ( A*(amountOfA) + A + B*(amountOfB) + B ) / 256
+ //
+ // We can rearrange this formula for some great optimisations.
+ //
+ // result = ( A*(amountOfA) + A + B*(amountOfB) + B ) / 256
+ // = ( A*(255-amountOfB) + A + B*(amountOfB) + B ) / 256
+ // = ( A*(256-amountOfB) + B*(amountOfB) + B ) / 256
+ // = ( A*256 + B + B*(amountOfB) - A*(amountOfB) ) / 256 // this is the version used in SCALE8_FIXED AVR below
+ // = ( A*256 + B + (B-A)*(amountOfB) ) / 256 // this is the version used in SCALE8_FIXED C below
+
uint16_t partial;
uint8_t result;
-
+
+#if BLEND8_C == 1
+
+# if (FASTLED_SCALE8_FIXED == 1)
+ partial = (a << 8) | b; // A*256 + B
+
+ // on many platforms this compiles to a single multiply of (B-A) * amountOfB
+ partial += (b * amountOfB);
+ partial -= (a * amountOfB);
+
+# else
uint8_t amountOfA = 255 - amountOfB;
-
+
+ // on the other hand, this compiles to two multiplies, and gives the "wrong" answer :]
partial = (a * amountOfA);
-#if (FASTLED_SCALE8_FIXED == 1)
- partial += a;
- //partial = add8to16( a, partial);
-#endif
-
partial += (b * amountOfB);
-#if (FASTLED_SCALE8_FIXED == 1)
- partial += b;
- //partial = add8to16( b, partial);
-#endif
+# endif
result = partial >> 8;
return result;
#elif BLEND8_AVRASM == 1
- uint16_t partial;
- uint8_t result;
-
-#if (FASTLED_SCALE8_FIXED == 1)
-
- // with SCALE8_FIXED, the algorithm above is:
- // result = A*(255-amountOfB) + A + B*(amountOfB) + B
- // however, we can rearrange that to:
- // result = 256*A + B - A*amountOfB + B*amountOfB
+# if (FASTLED_SCALE8_FIXED == 1)
// 1 or 2 cycles depending on how the compiler optimises
partial = (a << 8) | b;
@@ -522,7 +535,7 @@ LIB8STATIC uint8_t blend8( uint8_t a, uint8_t b, uint8_t amountOfB)
: "r0", "r1"
);
-#else
+# else
// non-SCALE8-fixed version
@@ -550,14 +563,14 @@ LIB8STATIC uint8_t blend8( uint8_t a, uint8_t b, uint8_t amountOfB)
: "r0", "r1"
);
-#endif
+# endif
result = partial >> 8;
return result;
#else
-#error "No implementation for blend8 available."
+# error "No implementation for blend8 available."
#endif
}