From 2c0063dc5b76c1d35aca4bc7ca617bd6d93fdcc2 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Fri, 17 Jan 2025 21:13:27 +0100 Subject: [PATCH] tiny optimization * use bitmask operations in getBitFromArray, setBitInArray * make currentBri "const" --- wled00/FX.h | 2 +- wled00/FX_fcn.cpp | 2 +- wled00/bus_manager.cpp | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index 27cf5f6d..ddbd0146 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -616,7 +616,7 @@ typedef struct Segment { } // WLEDMM method inlined for speed (its called at each setPixelColor) - [[gnu::hot]] inline uint8_t currentBri(uint8_t briNew, bool useCct = false) { + [[gnu::hot]] inline uint8_t currentBri(uint8_t briNew, bool useCct = false) const { uint32_t prog = progress(); if (prog < 0xFFFFU) { // progress() < 0xFFFFU implies that _t is valid (see progress() function) if (useCct) return ((briNew * prog) + _t->_cctT * (0xFFFFU - prog)) >> 16; diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index a6e127e1..b09136f5 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -464,7 +464,7 @@ uint16_t IRAM_ATTR_YN Segment::progress() const { // WLEDMM Segment::currentBri() is declared inline, see FX.h #if 0 -uint8_t IRAM_ATTR_YN Segment::currentBri(uint8_t briNew, bool useCct) { +uint8_t IRAM_ATTR_YN Segment::currentBri(uint8_t briNew, bool useCct) const { uint32_t prog = (transitional && _t) ? progress() : 0xFFFFU; if (transitional && _t && prog < 0xFFFFU) { if (useCct) return ((briNew * prog) + _t->_cctT * (0xFFFFU - prog)) >> 16; diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index 426ac919..231a2385 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -12,16 +12,16 @@ // WLEDMM functions to get/set bits in an array - based on functions created by Brandon for GOL // toDo : make this a class that's completely defined in a header file inline bool getBitFromArray(const uint8_t* byteArray, size_t position) { // get bit value - size_t byteIndex = position / 8; - unsigned bitIndex = position % 8; + size_t byteIndex = position >> 3; // same as "position/8" + unsigned bitIndex = position & 0x0007; // last 3 bits uint8_t byteValue = byteArray[byteIndex]; return (byteValue >> bitIndex) & 1; } inline void setBitInArray(uint8_t* byteArray, size_t position, bool value) { // set bit - with error handling for nullptr //if (byteArray == nullptr) return; - size_t byteIndex = position / 8; - unsigned bitIndex = position % 8; + size_t byteIndex = position >> 3; + unsigned bitIndex = position & 0x0007; // last 3 bits if (value) byteArray[byteIndex] |= (1 << bitIndex); else