Skip to content

Commit 33b1a6f

Browse files
iabdalkaderpillo79
authored andcommitted
libraries/Arduino_LED_Matrix: Optimize reverse function.
Use RBIT intrinsic (if available) instead of the slow software reverse. Note that RBIT is at least ~2x faster: Reverse Function Benchmark ========================== Current implementation: 18 ms Optimized implementation: 8 ms Speedup: 2.25x Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent 02eb185 commit 33b1a6f

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

libraries/Arduino_LED_Matrix/src/Arduino_LED_Matrix.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <Arduino.h>
2+
#include <cmsis_core.h>
23

34
extern "C" {
45
void matrixBegin(void);
@@ -14,13 +15,17 @@ void matrixWrite(uint8_t *buf);
1415
#define MATRIX_WITH_ARDUINOGRAPHICS
1516
#endif
1617

17-
static uint32_t reverse(uint32_t x) {
18+
static inline uint32_t reverse(uint32_t x) {
19+
#if defined(__CORTEX_M) && (__CORTEX_M >= 3U)
20+
return __RBIT(x);
21+
#else
1822
x = ((x >> 1) & 0x55555555u) | ((x & 0x55555555u) << 1);
1923
x = ((x >> 2) & 0x33333333u) | ((x & 0x33333333u) << 2);
2024
x = ((x >> 4) & 0x0f0f0f0fu) | ((x & 0x0f0f0f0fu) << 4);
2125
x = ((x >> 8) & 0x00ff00ffu) | ((x & 0x00ff00ffu) << 8);
2226
x = ((x >> 16) & 0xffffu) | ((x & 0xffffu) << 16);
2327
return x;
28+
#endif
2429
}
2530

2631
// TODO: this is dangerous, use with care

0 commit comments

Comments
 (0)