[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <aQwHpFEfHGpoaVW9@agluck-desk3>
Date: Wed, 5 Nov 2025 18:27:48 -0800
From: "Luck, Tony" <tony.luck@...el.com>
To: Dave Martin <Dave.Martin@....com>
CC: Fenghua Yu <fenghuay@...dia.com>, Reinette Chatre
<reinette.chatre@...el.com>, Maciej Wieczor-Retman
<maciej.wieczor-retman@...el.com>, Peter Newman <peternewman@...gle.com>,
James Morse <james.morse@....com>, Babu Moger <babu.moger@....com>, "Drew
Fustini" <dfustini@...libre.com>, Chen Yu <yu.c.chen@...el.com>,
<x86@...nel.org>, <linux-kernel@...r.kernel.org>, <patches@...ts.linux.dev>
Subject: Re: [PATCH v13 12/32] x86,fs/resctrl: Support binary fixed point
event counters
On Wed, Nov 05, 2025 at 03:31:07PM -0800, Luck, Tony wrote:
> > > +
> > > + if (!binary_bits) {
> > > + seq_printf(m, "%llu.0\n", val);
> > > + return;
> > > + }
I can't completely escape a test for !binary_bits. Most of the
flow works ok (doing nothing, working towards frac == 0 when
it comes time for the snprintf()).
But the round-up code:
frac += 1ull << (binary_bits - 1);
goes badly wrong if binary_bits == 0.
I could write it like this:
static void print_event_value(struct seq_file *m, unsigned int binary_bits, u64 val)
{
char buf[decplaces[MAX_BINARY_BITS] + 1];
unsigned long long frac = 0;
if (binary_bits) {
/* Mask off the integer part of the fixed-point value. */
frac = val & GENMASK_ULL(binary_bits - 1, 0);
/*
* Multiply by 10^{desired decimal places}. The integer part of
* the fixed point value is now almost what is needed.
*/
frac *= int_pow(10ull, decplaces[binary_bits]);
/*
* Round to nearest by adding a value that would be a "1" in the
* binary_bits + 1 place. Integer part of fixed point value is
* now the needed value.
*/
frac += 1ull << (binary_bits - 1);
/*
* Extract the integer part of the value. This is the decimal
* representation of the original fixed-point fractional value.
*/
frac >>= binary_bits;
}
/*
* "frac" is now in the range [0 .. 10^decplaces). I.e. string
* representation will fit into chosen number of decimal places.
*/
snprintf(buf, sizeof(buf), "%0*llu", decplaces[binary_bits], frac);
seq_printf(m, "%llu.%s\n", val >> binary_bits, buf);
}
-Tony
Powered by blists - more mailing lists