Skip to content

Commit 3659c90

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 <explorer09@gmail.com>
1 parent 09de683 commit 3659c90

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

CPUMeter.c

Lines changed: 8 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

@@ -302,9 +303,13 @@ static void OctoColCPUsMeter_updateMode(Meter* this, MeterModeId mode) {
302303

303304
static int xOffsetOfMeterColumn(int w, int nCol, int col) {
304305
int colwidth = w / nCol;
305-
int diff = w % nCol;
306-
int d = MINIMUM(diff, col); // dynamic spacer
307-
return (col * colwidth) + d;
306+
307+
// Spacing between meters based on the remainder of (w % nCol)
308+
uint32_t v = (unsigned int)((w % nCol) * 32 / nCol);
309+
v = ((v * 0x00210842U) & 0x02082082U) * (unsigned int)col;
310+
v = (uint32_t)(((v + 0x02108420U) & 0x7DE71840U) * 0x00210842ULL) >> 27;
311+
int spacing = (int)v;
312+
return col * colwidth + spacing;
308313
}
309314

310315
static void CPUMeterCommonDraw(Meter* this, int x, int y, int w, int ncol) {

0 commit comments

Comments
 (0)