Linux-IIO Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH v2 1/1] iio: core: Fix fractional format generation
@ 2019-08-22  6:06 Alexander Stein
  2019-11-07 13:54 ` Alexander Stein
  0 siblings, 1 reply; 3+ messages in thread
From: Alexander Stein @ 2019-08-22  6:06 UTC (permalink / raw)
  To: Jonathan Cameron, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler
  Cc: Alexander Stein, linux-iio, linux-kernel

In case the result is -0.3252 tmp0 is 0 after the div_s64_rem, so tmp0 is
non-negative which results in an output of 0.3252.
Fix this by explicitly handling the negative sign ourselves.

Signed-off-by: Alexander Stein <alexander.stein@systec-electronic.com>
---
Changes in v2:
* Support vals[0] >= and vals[1] < 0 in IIO_VAL_FRACTIONAL
* Note: IIO_VAL_FRACTIONAL is untested, as I lack hardware
* Note2: Currently IIO_VAL_FRACTIONAL is only called with vals[1] from
         in-kernel drivers AFAICS

 drivers/iio/industrialio-core.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 245b5844028d..247338142c87 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -568,6 +568,7 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
 {
 	unsigned long long tmp;
 	int tmp0, tmp1;
+	char *sign;
 	bool scale_db = false;
 
 	switch (type) {
@@ -593,11 +594,17 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
 		tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
 		tmp1 = vals[1];
 		tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1);
-		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
+		if (vals[1] < 0) {
+			sign = vals[0] >= 0 ? "-" : "";
+		} else {
+			sign = vals[0] < 0 ? "-" : "";
+		}
+		return snprintf(buf, len, "%s%u.%09u", sign, abs(tmp0), abs(tmp1));
 	case IIO_VAL_FRACTIONAL_LOG2:
+		sign = vals[0] < 0 ? "-" : "";
 		tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
 		tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1);
-		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
+		return snprintf(buf, len, "%s%u.%09u", sign, abs(tmp0), abs(tmp1));
 	case IIO_VAL_INT_MULTIPLE:
 	{
 		int i;
-- 
2.23.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2 1/1] iio: core: Fix fractional format generation
  2019-08-22  6:06 [PATCH v2 1/1] iio: core: Fix fractional format generation Alexander Stein
@ 2019-11-07 13:54 ` Alexander Stein
  2019-11-09 11:55   ` Jonathan Cameron
  0 siblings, 1 reply; 3+ messages in thread
From: Alexander Stein @ 2019-11-07 13:54 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-iio, linux-kernel

Gentle ping

On Thursday, August 22, 2019, 8:06:07 AM CET Alexander Stein wrote:
> In case the result is -0.3252 tmp0 is 0 after the div_s64_rem, so tmp0 is
> non-negative which results in an output of 0.3252.
> Fix this by explicitly handling the negative sign ourselves.
> 
> Signed-off-by: Alexander Stein <alexander.stein@systec-electronic.com>
> ---
> Changes in v2:
> * Support vals[0] >= and vals[1] < 0 in IIO_VAL_FRACTIONAL
> * Note: IIO_VAL_FRACTIONAL is untested, as I lack hardware
> * Note2: Currently IIO_VAL_FRACTIONAL is only called with vals[1] from
>          in-kernel drivers AFAICS
> 
>  drivers/iio/industrialio-core.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index 245b5844028d..247338142c87 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -568,6 +568,7 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
>  {
>  	unsigned long long tmp;
>  	int tmp0, tmp1;
> +	char *sign;
>  	bool scale_db = false;
>  
>  	switch (type) {
> @@ -593,11 +594,17 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
>  		tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
>  		tmp1 = vals[1];
>  		tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1);
> -		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
> +		if (vals[1] < 0) {
> +			sign = vals[0] >= 0 ? "-" : "";
> +		} else {
> +			sign = vals[0] < 0 ? "-" : "";
> +		}
> +		return snprintf(buf, len, "%s%u.%09u", sign, abs(tmp0), abs(tmp1));
>  	case IIO_VAL_FRACTIONAL_LOG2:
> +		sign = vals[0] < 0 ? "-" : "";
>  		tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
>  		tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1);
> -		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
> +		return snprintf(buf, len, "%s%u.%09u", sign, abs(tmp0), abs(tmp1));
>  	case IIO_VAL_INT_MULTIPLE:
>  	{
>  		int i;
> 


-- 
Dipl-Inf. Alexander Stein

SYS TEC electronic AG
Am Windrad 2
08468 Heinsdorfergrund
Germany
Tel: +49-3765-38600-0, Fax: +49-3765-38600-4100
Email   : alexander.stein@systec-electronic.com
Internet: http://www.systec-electronic.com

Board of Directors: Dipl.-Ing. Ronald Sieber, Dipl.-Ing. Dirk Bause
Chairman of the Supervisory Board: Dipl.-Phys. Siegmar Schmidt
Commercial registry: Amtsgericht Chemnitz, HRB 32220; USt.-Id Nr. 
DE150534010

--------------------------------------------------------------------------

Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte 
Informationen.

Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich 
erhalten haben, informieren Sie bitte sofort den Absender und vernichten 
Sie diese Mail.

Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail sind 
nicht gestattet.

--------------------------------------------------------------------------

This e-mail may contain confidential and/or privileged information.

If you are not the intended recipient (or have received this e-mail in 
error) please notify the sender immediately and destroy this e-mail.

Any unauthorized copying, disclosure or distribution of the material in 
this e-mail is strictly forbidden.

--------------------------------------------------------------------------



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2 1/1] iio: core: Fix fractional format generation
  2019-11-07 13:54 ` Alexander Stein
@ 2019-11-09 11:55   ` Jonathan Cameron
  0 siblings, 0 replies; 3+ messages in thread
From: Jonathan Cameron @ 2019-11-09 11:55 UTC (permalink / raw)
  To: Alexander Stein
  Cc: Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-iio, linux-kernel

On Thu, 07 Nov 2019 14:54:52 +0100
Alexander Stein <alexander.stein@systec-electronic.com> wrote:

> Gentle ping
> 
> On Thursday, August 22, 2019, 8:06:07 AM CET Alexander Stein wrote:
> > In case the result is -0.3252 tmp0 is 0 after the div_s64_rem, so tmp0 is
> > non-negative which results in an output of 0.3252.
> > Fix this by explicitly handling the negative sign ourselves.
> > 
> > Signed-off-by: Alexander Stein <alexander.stein@systec-electronic.com>

Apologies. I'd missed this v2 entirely for some reason.

It looks correct to me, but as Lars had inputs on v1...

Lars, could you take a quick look at this v2 and give an ack if
you are happy with it.

Thanks,

Jonathan

> > ---
> > Changes in v2:
> > * Support vals[0] >= and vals[1] < 0 in IIO_VAL_FRACTIONAL
> > * Note: IIO_VAL_FRACTIONAL is untested, as I lack hardware
> > * Note2: Currently IIO_VAL_FRACTIONAL is only called with vals[1] from
> >          in-kernel drivers AFAICS
> > 
> >  drivers/iio/industrialio-core.c | 11 +++++++++--
> >  1 file changed, 9 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> > index 245b5844028d..247338142c87 100644
> > --- a/drivers/iio/industrialio-core.c
> > +++ b/drivers/iio/industrialio-core.c
> > @@ -568,6 +568,7 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
> >  {
> >  	unsigned long long tmp;
> >  	int tmp0, tmp1;
> > +	char *sign;
> >  	bool scale_db = false;
> >  
> >  	switch (type) {
> > @@ -593,11 +594,17 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
> >  		tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
> >  		tmp1 = vals[1];
> >  		tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1);
> > -		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
> > +		if (vals[1] < 0) {
> > +			sign = vals[0] >= 0 ? "-" : "";
> > +		} else {
> > +			sign = vals[0] < 0 ? "-" : "";
> > +		}
> > +		return snprintf(buf, len, "%s%u.%09u", sign, abs(tmp0), abs(tmp1));
> >  	case IIO_VAL_FRACTIONAL_LOG2:
> > +		sign = vals[0] < 0 ? "-" : "";
> >  		tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
> >  		tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1);
> > -		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
> > +		return snprintf(buf, len, "%s%u.%09u", sign, abs(tmp0), abs(tmp1));
> >  	case IIO_VAL_INT_MULTIPLE:
> >  	{
> >  		int i;
> >   
> 
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-22  6:06 [PATCH v2 1/1] iio: core: Fix fractional format generation Alexander Stein
2019-11-07 13:54 ` Alexander Stein
2019-11-09 11:55   ` Jonathan Cameron

Linux-IIO Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-iio/0 linux-iio/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-iio linux-iio/ https://lore.kernel.org/linux-iio \
		linux-iio@vger.kernel.org
	public-inbox-index linux-iio

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-iio


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git