Re: [PATCH v5 04/12] ltp/fsx.c: Add atomic writes support to fsx

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Sep 09, 2025 at 08:55:54AM +0100, John Garry wrote:
> On 09/09/2025 07:57, Ojaswin Mujoo wrote:
> > > > > > +
> > > > > > +/*
> > > > > > + * Round down n to nearest power of 2.
> > > > > > + * If n is already a power of 2, return n;
> > > > > > + */
> > > > > > +static int rounddown_pow_of_2(int n) {
> > > > > > +	int i = 0;
> > > > > > +
> > > > > > +	if (is_power_of_2(n))
> > > > > > +		return n;
> > > > > > +
> > > > > > +	for (; (1 << i) < n; i++);
> > > > > > +
> > > > > > +	return 1 << (i - 1);
> > > > > Is this the neatest way to do this?
> > > > Well it is a straigforward o(logn) way. Do you have something else in
> > > > mind?
> > > check what the kernel does is always a good place to start...
> > > 
> > > Thanks
> > So kernel pretty much does same thing:
> > 
> > unsigned long __rounddown_pow_of_two(unsigned long n)
> > {
> > 	return 1UL << (fls_long(n) - 1);
> > }
> > 
> > where fls*() variants jump into asm for efficiency. asm is obviously not
> > needed here so we use the for loop to calculate the fls (find last bit
> > set):
> > 
> > 	for (; (1 << i) < n; i++);
> > 
> > Ideally the compiler should do the right thing and optimize it.
> 
> I think that some versions of fls use builtin clz also, but I am not sure if
> that (clz) is always available for supported toolchains here.

Right, kernel also tries that whenever possible. Im unsure though if
fls() is a libc function. I think its not but I'm not able to find any
resources to confirm this. 

In case it is not, then I think the current logic should be good enough.
> 
> Thanks,
> John




[Index of Archives]     [Reiser Filesystem Development]     [Ceph FS]     [Kernel Newbies]     [Security]     [Netfilter]     [Bugtraq]     [Linux FS]     [Yosemite National Park]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Device Mapper]     [Linux Media]

  Powered by Linux