Skip to content

Commit d89881e

Browse files
committed
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 <[email protected]>
1 parent 07a976e commit d89881e

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

libraries/Arduino_LED_Matrix/src/Arduino_LED_Matrix.h

Lines changed: 7 additions & 3 deletions
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,14 +15,17 @@ extern "C" {
1415
#define MATRIX_WITH_ARDUINOGRAPHICS
1516
#endif
1617

17-
static uint32_t reverse(uint32_t x)
18-
{
18+
static inline uint32_t reverse(uint32_t x) {
19+
#if defined(__CORTEX_M) && (__CORTEX_M >= 3U)
20+
return __RBIT(x);
21+
#else
1922
x = ((x >> 1) & 0x55555555u) | ((x & 0x55555555u) << 1);
2023
x = ((x >> 2) & 0x33333333u) | ((x & 0x33333333u) << 2);
2124
x = ((x >> 4) & 0x0f0f0f0fu) | ((x & 0x0f0f0f0fu) << 4);
2225
x = ((x >> 8) & 0x00ff00ffu) | ((x & 0x00ff00ffu) << 8);
2326
x = ((x >> 16) & 0xffffu) | ((x & 0xffffu) << 16);
2427
return x;
28+
#endif
2529
}
2630

2731
// TODO: this is dangerous, use with care
@@ -239,4 +243,4 @@ class Arduino_LED_Matrix
239243
};
240244

241245
// For backwards compatibility, also define the class name without underscores
242-
typedef Arduino_LED_Matrix ArduinoLEDMatrix;
246+
typedef Arduino_LED_Matrix ArduinoLEDMatrix;

0 commit comments

Comments
 (0)