Skip to content

Commit

Permalink
valueCellIndex() logic optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Explorer09 committed Feb 9, 2024
1 parent a9c4868 commit 54d0102
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions Meter.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,20 +322,22 @@ static void GraphMeterMode_reallocateGraphBuffer(Meter* this, const GraphDrawCon
}

static size_t GraphMeterMode_valueCellIndex(unsigned int graphHeight, bool isPercentChart, int deltaExp, unsigned int y, unsigned int* scaleFactor, unsigned int* increment) {
assert(deltaExp >= 0);
assert(deltaExp < UINT16_WIDTH);

if (scaleFactor)
*scaleFactor = 1;
if (increment)
*increment = isPercentChart ? 1 : (2U << deltaExp);

assert(deltaExp >= 0);
assert(deltaExp < UINT16_WIDTH);
unsigned int yTop = (graphHeight - 1) >> deltaExp;
if (y > yTop) {
assert(increment == NULL);
return (size_t)-1;
}

if (isPercentChart) {
assert(deltaExp == 0);
if (increment)
*increment = 1;

if (y > yTop)
return (size_t)-1;

return y;
}
// A record may be rendered in different scales depending on the largest
Expand All @@ -355,24 +357,33 @@ static size_t GraphMeterMode_valueCellIndex(unsigned int graphHeight, bool isPer
// [5] [10] X X X | computed from cells of a
// [3] [6] (12) X X | lower scale and not stored in
// [1] [2] [4] [8] (16) | the array.
if (increment)
*increment = 2U << deltaExp;

if (y > yTop)
return (size_t)-1;

// "b" is the "base" offset or the upper bits of offset
unsigned int b = (y * 2) << deltaExp;
unsigned int offset = 1U << deltaExp;
if (y == yTop) {
assert(((2 * graphHeight - 1) & b) == b);
unsigned int offsetTop = powerOf2Floor(2 * graphHeight - 1 - b);
if (scaleFactor && offsetTop) {
*scaleFactor = offset / offsetTop;

if (!scaleFactor) {
// This function is called for writing. The "increment" argument is
// optional, but the caller should assert the index is in bounds.
if (b + offset > 2 * graphHeight - 1) {
assert(increment == NULL);
return (size_t)-1;
}
return b + offsetTop;
return b + offset;
}

// This function is called for reading.
assert(increment == NULL);

if (y < yTop)
return b + offset;

assert(((2 * graphHeight - 1) & b) == b);
unsigned int offsetTop = powerOf2Floor(2 * graphHeight - 1 - b);
if (offsetTop) {
*scaleFactor = offset / offsetTop;
}
return b + offset;
return b + offsetTop;
}

static uint8_t GraphMeterMode_findTopCellItem(const Meter* this, double scaledTotal, unsigned int topCell) {
Expand Down Expand Up @@ -915,8 +926,7 @@ static void GraphMeterMode_recordNewValue(Meter* this, const GraphDrawContext* c
// Clear cells
unsigned int y = ((unsigned int)numDots + 8 - 1) / 8; // Round up
size_t i = GraphMeterMode_valueCellIndex(graphHeight, isPercentChart, 0, y, NULL, NULL);
if (i != (size_t)-1) {
assert(i < nCellsPerValue);
if (i < nCellsPerValue) {
memset(&valueStart[i], 0, (nCellsPerValue - i) * sizeof(*valueStart));
}

Expand Down Expand Up @@ -1049,7 +1059,7 @@ static int GraphMeterMode_lookupCell(const Meter* this, const GraphDrawContext*

unsigned int scaleFactor;
size_t i = GraphMeterMode_valueCellIndex(graphHeight, isPercentChart, deltaExpArg, y, &scaleFactor, NULL);
if (i == (size_t)-1)
if (i >= nCellsPerValue)
goto cellIsEmpty;

if (deltaExp >= UINT16_WIDTH) {
Expand Down

0 comments on commit 54d0102

Please sign in to comment.