Skip to content

Commit 6b8ca65

Browse files
committed
CPUMeter: Improve spacing between sub-meter algorithm
The new algorithm will make the meter bars aligned when groups of CPU meters with 2, 4 and 8 sub-columns are placed together in one larger column. It utilizes the fact that the number of sub-columns of CPU meters are in power of 2 only, and calculates spacing with bit tricks. Signed-off-by: Kang-Che Sung <[email protected]>
1 parent 0290c58 commit 6b8ca65

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

CPUMeter.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ in the source distribution for its full text.
1212
#include <assert.h>
1313
#include <stdbool.h>
1414
#include <stddef.h>
15+
#include <stdint.h>
1516
#include <stdlib.h>
1617
#include <string.h>
1718

@@ -306,11 +307,14 @@ static void CPUMeterCommonDraw(Meter* this, int x, int y, int w, int ncol) {
306307
int start, count;
307308
AllCPUsMeter_getRange(this, &start, &count);
308309
int colwidth = w / ncol;
309-
int diff = w % ncol;
310310
int nrows = (count + ncol - 1) / ncol;
311311
for (int i = 0; i < count; i++) {
312-
int d = (i / nrows) > diff ? diff : (i / nrows); // dynamic spacer
313-
int xpos = x + ((i / nrows) * colwidth) + d;
312+
// Spacing between meters based on the remainder of (w % ncol)
313+
uint32_t v = (unsigned int)((w % ncol) * 32 / ncol);
314+
v = ((v * 0x00210842U) & 0x02082082U) * (unsigned int)(i / nrows);
315+
v = (uint32_t)(((v + 0x02108420U) & 0x7DE71840U) * 0x00210842ULL) >> 27;
316+
int spacing = (int)v;
317+
int xpos = x + ((i / nrows) * colwidth) + spacing;
314318
int ypos = y + ((i % nrows) * meters[0]->h);
315319
meters[i]->draw(meters[i], xpos, ypos, colwidth);
316320
}

0 commit comments

Comments
 (0)