diff options
author | Nick Pisarro <infinityminusnine@gmail.com> | 2019-10-31 21:40:32 +0300 |
---|---|---|
committer | Nick Pisarro <npisarro@twitter.com> | 2019-11-02 03:47:55 +0300 |
commit | 1e3f3c78ac05e4413e591f2a3a9f3421788ad5e6 (patch) | |
tree | dc053dd66286a5e7afa26d399b4c458539e710f0 | |
parent | 8b31b643e42d8a53a3df9abc96bf45f00adf52df (diff) |
Convert rand multiplication constants to shifts and adds.
Since many popular low-cost chipsets do not have a native multiply operation,
this calculation can be executed much more efficiently using only add and left shift.
-rw-r--r-- | lib8tion/random8.h | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/lib8tion/random8.h b/lib8tion/random8.h index ba60cf57..ab9ac27e 100644 --- a/lib8tion/random8.h +++ b/lib8tion/random8.h @@ -12,13 +12,16 @@ #define FASTLED_RAND16_2053 ((uint16_t)(2053)) #define FASTLED_RAND16_13849 ((uint16_t)(13849)) +// equivalent to x * 2053 +#define APPLY_FASTLED_RAND16_2053(x) (x << 11) + (x << 2) + x + /// random number seed extern uint16_t rand16seed;// = RAND16_SEED; /// Generate an 8-bit random number LIB8STATIC uint8_t random8() { - rand16seed = (rand16seed * FASTLED_RAND16_2053) + FASTLED_RAND16_13849; + rand16seed = APPLY_FASTLED_RAND16_2053(rand16seed) + FASTLED_RAND16_13849; // return the sum of the high and low bytes, for better // mixing and non-sequential correlation return (uint8_t)(((uint8_t)(rand16seed & 0xFF)) + @@ -28,7 +31,7 @@ LIB8STATIC uint8_t random8() /// Generate a 16 bit random number LIB8STATIC uint16_t random16() { - rand16seed = (rand16seed * FASTLED_RAND16_2053) + FASTLED_RAND16_13849; + rand16seed = APPLY_FASTLED_RAND16_2053(rand16seed) + FASTLED_RAND16_13849; return rand16seed; } |