From ab5c49bdcb2479884b89ce01f2a7df71fe5dc488 Mon Sep 17 00:00:00 2001 From: Ton Huisman Date: Mon, 2 Sep 2024 23:31:37 +0200 Subject: [PATCH] [P165] Add more code from #5091 --- src/src/PluginStructs/P073_data_struct.cpp | 23 +++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/src/PluginStructs/P073_data_struct.cpp b/src/src/PluginStructs/P073_data_struct.cpp index 540dfa6a31..80ecaa19c8 100644 --- a/src/src/PluginStructs/P073_data_struct.cpp +++ b/src/src/PluginStructs/P073_data_struct.cpp @@ -139,15 +139,24 @@ void P073_font_selector(const __FlashStringHelper *id, int16_t value) { * Dot/colon bit is still bit 8 */ uint8_t P073_revert7bits(uint8_t character) { - uint8_t newCharacter = character & 0x80; // Keep dot-bit if passed in + uint8_t dpBit = character & 0x80; // Keep dot-bit if passed in + uint8_t b = character << 1; // Pre-shift as only 7 bits to revert - for (int b = 0; b < 7; ++b) { - if (character & (0x01 << b)) { - newCharacter |= (0x40 >> b); - } - } - return newCharacter; + // Source: https://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious + # ifdef ESP8266 + + // 32 bit 7 operations variant, 64 bit operations is quite slow on ESP8266, but fast on most ESP32 variants + b = ((b * 0x0802LU & 0x22110LU) | (b * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16; + # endif // ifdef ESP8266 + # ifdef ESP32 + + // 64 bit 4 operations variant + b = ((b * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32; + # endif // ifdef ESP32 + + return b | dpBit; // Restore dot-bit } + // FIXME End of part to merge from PR #5091 void P073_data_struct::init(struct EventStruct *event)