lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Mon, 6 Mar 2023 14:16:24 +0200
From:   Andy Shevchenko <andriy.shevchenko@...ux.intel.com>
To:     Jonathan Cameron <jic23@...nel.org>
Cc:     Mike Looijmans <mike.looijmans@...ic.nl>,
        devicetree@...r.kernel.org, linux-iio@...r.kernel.org,
        Caleb Connolly <caleb.connolly@...aro.org>,
        ChiYuan Huang <cy_huang@...htek.com>,
        ChiaEn Wu <chiaen_wu@...htek.com>,
        Cosmin Tanislav <demonsingur@...il.com>,
        Ibrahim Tilki <Ibrahim.Tilki@...log.com>,
        Lars-Peter Clausen <lars@...afoo.de>,
        Ramona Bolboaca <ramona.bolboaca@...log.com>,
        William Breathitt Gray <william.gray@...aro.org>,
        linux-kernel@...r.kernel.org
Subject: Re: [PATCH v3 2/2] iio: adc: Add TI ADS1100 and ADS1000

On Mon, Mar 06, 2023 at 02:11:48PM +0200, Andy Shevchenko wrote:
> On Sat, Mar 04, 2023 at 05:57:51PM +0000, Jonathan Cameron wrote:
> > On Tue, 28 Feb 2023 07:31:51 +0100
> > Mike Looijmans <mike.looijmans@...ic.nl> wrote:

...

> > > +	for (i = 0; i < 4; i++) {
> > > +		if (BIT(i) == gain) {
> > > +			ads1100_set_config_bits(data, ADS1100_PGA_MASK, i);
> > > +			return 0;
> > > +		}
> > > +	}
> > Andy's suggestion of something like..
> > 	if (!gain)
> > 		return -EINVAL;
> > 	i = ffs(gain);
> > 	if (i >= 4 || BIT(i) != gain)
> > 		return -EINVAL;
> > 
> > 	ads...
> > 
> > Is perhaps nicer than the loop.
> 
> Even better:
> 
> 	if (!gain || !is_power_of_2(gain))
> 		return -EINVAL;

Or if you want to combine all checks:

	if (clamp_val(gain, BIT(0), BIT(3)) != gain || !is_power_of_2(gain))
		return -EINVAL;

	ads1100_set_config_bits(data, ADS1100_PGA_MASK, ffs(gain));
	return 0;

(You can play with bloat-o-meter for the code generation and see which one is
 better from that aspect)

-- 
With Best Regards,
Andy Shevchenko


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ