commit be48e6388408766659a77d5f60ad698585c1af29
Author: jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Fri Aug 27 19:22:09 2021 -0600
Commit: jtothebell <j.to.the.bell@gmail.com>
CommitDate: Fri Aug 27 19:22:09 2021 -0600
Tweak nibble helpers (more inline, more bitwise)
---
source/nibblehelpers.cpp | 23 +++++++----------------
source/nibblehelpers.h | 3 ++-
2 files changed, 9 insertions(+), 17 deletions(-)
diff --git a/source/nibblehelpers.cpp b/source/nibblehelpers.cpp
index 3c1c80b..1adf99c 100644
--- a/source/nibblehelpers.cpp
+++ b/source/nibblehelpers.cpp
@@ -9,29 +9,20 @@
int getCombinedIdx(const int x, const int y){
//bit shifting might be faster? trying to optimize
//return y * 64 + (x / 2);
- //return (y << 6) + (x >> 1);
+ //return (y << 6) | (x >> 1);
return COMBINED_IDX(x, y);
}
//try look up table to optimize?
void setPixelNibble(const int x, const int y, uint8_t value, uint8_t* targetBuffer) {
- const int combinedIdx = COMBINED_IDX(x, y);
-
- const uint8_t currentByte = targetBuffer[combinedIdx];
- //get mask for even or odd
- const uint8_t mask = (BITMASK(0) & x) == 0 ? 0x0f : 0xf0;
- value = mask == 0xf0 ? value << 4 : value;
-
- targetBuffer[combinedIdx] = (currentByte & ~mask) | (value & mask);
-
//potentially slightly optimized by inlining - don't have measurements to back it up
- //(BITMASK(0) & x) == 0
- // ? targetBuffer[(y << 6) + (x >> 1)] = (targetBuffer[(y << 6) + (x >> 1)] & 0xf0) | (value & 0x0f)
- // : targetBuffer[(y << 6) + (x >> 1)] = (targetBuffer[(y << 6) + (x >> 1)] & 0x0f) | (value << 4 & 0xf0);
+ targetBuffer[COMBINED_IDX(x, y)] = (BITMASK(0) & x)
+ ? (targetBuffer[COMBINED_IDX(x, y)] & 0x0f) | (value << 4 & 0xf0)
+ : (targetBuffer[COMBINED_IDX(x, y)] & 0xf0) | (value & 0x0f);
}
uint8_t getPixelNibble(const int x, const int y, const uint8_t* targetBuffer) {
- return (BITMASK(0) & x) == 0
- ? targetBuffer[COMBINED_IDX(x, y)] & 0x0f //just first 4 bits
- : targetBuffer[COMBINED_IDX(x, y)] >> 4; //just last 4 bits
+ return (BITMASK(0) & x)
+ ? targetBuffer[COMBINED_IDX(x, y)] >> 4 //just last 4 bits
+ : targetBuffer[COMBINED_IDX(x, y)] & 0x0f; //just first 4 bits
}
\ No newline at end of file
diff --git a/source/nibblehelpers.h b/source/nibblehelpers.h
index 157c06f..bf37ff2 100644
--- a/source/nibblehelpers.h
+++ b/source/nibblehelpers.h
@@ -2,7 +2,8 @@
#include <string>
-#define COMBINED_IDX(x, y) ((y) << 6) + ((x) >> 1)
+//should be the equivalent of return y * 64 + (x / 2);
+#define COMBINED_IDX(x, y) ((y) << 6) | ((x) >> 1)
int getCombinedIdx(int x, int y);