Skip to contentSkip to conversation

drivers:iio:dac make expression evaluation 64-bit

PATCH In review Muhammad Usama Anjum 6 participants 7 messages Dec 21, 2021 – Jan 5, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers:iio:dac make expression evaluation 64-bit

Two 32-bit values are being evaluated using 32-bit arithmetic and then passed to s64 type. It is wrong. Expression should be evaluated using 64-bit arithmetic and then passed.

Fixes 8f2b54824b ("drivers:iio:dac: Add AD3552R driver support")
---
 drivers/iio/dac/ad3552r.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/dac/ad3552r.c +1 -1
index 97f13c0b9631..b03d3c7cd4c4 100644
@@ -770,7 +770,7 @@ static void ad3552r_calc_gain_and_offset(struct ad3552r_desc *dac, s32 ch)
 	dac->ch_data[ch].scale_dec = DIV_ROUND_CLOSEST((s64)rem * 1000000,
 							65536);
 
-	dac->ch_data[ch].offset_int = div_s64_rem(v_min * 65536, span, &rem);
+	dac->ch_data[ch].offset_int = div_s64_rem(v_min * 65536L, span, &rem);
 	tmp = (s64)rem * 1000000;
 	dac->ch_data[ch].offset_dec = div_s64(tmp, span);
 }
Signature · 1 line
2.30.2
#1 lore raw
Quoted 4 lines · Muhammad Usama Anjum

Two 32-bit values are being evaluated using 32-bit arithmetic and then passed to s64 type. It is wrong. Expression should be evaluated using 64-bit arithmetic and then passed.

...

Quoted 2 lines
        dac->ch_data[ch].scale_dec = DIV_ROUND_CLOSEST((s64)rem * 1000000,
                                                        65536);

Shouldn't the above be fixed as well? Has anybody tried to compile on 32-bit arch this?

Quoted 5 lines
drivers/iio/dac/ad3552r.c +1 -1
-       dac->ch_data[ch].offset_int = div_s64_rem(v_min * 65536, span, &rem);
+       dac->ch_data[ch].offset_int = div_s64_rem(v_min * 65536L, span, &rem);
        tmp = (s64)rem * 1000000;
        dac->ch_data[ch].offset_dec = div_s64(tmp, span);
 }
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 7 lines · Andy Shevchenko
Quoted 4 lines · Muhammad Usama Anjum

Two 32-bit values are being evaluated using 32-bit arithmetic and then passed to s64 type. It is wrong. Expression should be evaluated using 64-bit arithmetic and then passed.

Quoted 7 lines

...

Quoted 2 lines
        dac->ch_data[ch].scale_dec = DIV_ROUND_CLOSEST((s64)rem * 1000000,
                                                        65536);

Shouldn't the above be fixed as well? Has anybody tried to compile on 32-bit arch this?

No, it correct already. In this case, rem is being typecasted to s64 and then multiplied with a 32-bit number, 1000000. Thus 64-bit arithmetic is being performed here.

#3 lore raw
Quoted 8 lines · Muhammad Usama Anjum
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Muhammad Usama Anjum

Two 32-bit values are being evaluated using 32-bit arithmetic and then passed to s64 type. It is wrong. Expression should be evaluated using 64-bit arithmetic and then passed.

...

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
         dac->ch_data[ch].scale_dec = DIV_ROUND_CLOSEST((s64)rem * 1000000,
                                                         65536);

Shouldn't the above be fixed as well? Has anybody tried to compile on 32-bit arch this?

No, it correct already. In this case, rem is being typecasted to s64 and then multiplied with a 32-bit number, 1000000. Thus 64-bit arithmetic is being performed here.

What Andy means is that this needs to be DIV_S64_ROUND_CLOSEST() to work on 32-bit platforms. But it is clearly unrelated to your change and should be in its own patch.

#4 lore raw
Quoted 10 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

Two 32-bit values are being evaluated using 32-bit arithmetic and then passed to s64 type. It is wrong. Expression should be evaluated using 64-bit arithmetic and then passed.

...

Quoted 1 line
         dac->ch_data[ch].scale_dec = DIV_ROUND_CLOSEST((s64)rem *

1000000,

Quoted 3 lines
Quoted 3 lines
Quoted 1 line
                                                         65536);

Shouldn't the above be fixed as well? Has anybody tried to compile on 32-bit arch this?

Quoted 7 lines
Quoted 3 lines

No, it correct already. In this case, rem is being typecasted to s64 and then multiplied with a 32-bit number, 1000000. Thus 64-bit arithmetic is being performed here.

What Andy means is that this needs to be DIV_S64_ROUND_CLOSEST() to work on 32-bit platforms. But it is clearly unrelated to your change and should be in its own patch.

Indeed, I didn't test it on 32 bits.
But both changes make sense to me.

Regards,
Mihail

#5 lore raw
In reply to Muhammad Usama Anjum
Quoted 15 lines · Muhammad Usama Anjum

Two 32-bit values are being evaluated using 32-bit arithmetic and then passed to s64 type. It is wrong. Expression should be evaluated using 64-bit arithmetic and then passed.

Fixes 8f2b54824b ("drivers:iio:dac: Add AD3552R driver support")
---
 drivers/iio/dac/ad3552r.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/dac/ad3552r.c +0 -0
index 97f13c0b9631..b03d3c7cd4c4 100644

drivers/iio/dac/ad3552r.c static void ad3552r_calc_gain_and_offset(struct ad3552r_desc *dac, s32 ch) · line 773

Quoted 5 lines
drivers/iio/dac/ad3552r.c +1 -1
@@ -770,7 +770,7 @@ static void ad3552r_calc_gain_and_offset(struct ad3552r_desc *dac, s32 ch)
 	dac->ch_data[ch].scale_dec = DIV_ROUND_CLOSEST((s64)rem * 1000000,
 							65536);
 
-	dac->ch_data[ch].offset_int = div_s64_rem(v_min * 65536, span, &rem);
+	dac->ch_data[ch].offset_int = div_s64_rem(v_min * 65536L, span, &rem);

"v_min" is relatively close to zero on a number line so this can't overflow. There is no way that this change affects anything at runtime (except making the code a tiny tiny bit slower).

And it should be 65536LL for 32 bit systems?

But I just don't see the point of this change. Presumably it is to make a static analyzer happy?

regards,
dan carpenter

maintainer
#6 lore raw
Quoted 39 lines

From: Dan Carpenter <[email protected]>
Sent: Wednesday, January 5, 2022 2:39 PM
To: Muhammad Usama Anjum <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Jonathan Cameron <[email protected]>; Chindris, Mihail <[email protected]>; open list:IIO SUBSYSTEM AND DRIVERS <[email protected]>; open list <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] drivers:iio:dac make expression evaluation 64-bit

[External]

Quoted 1 line · Muhammad Usama Anjum

Two 32-bit values are being evaluated using 32-bit arithmetic and

then

Quoted 5 lines

passed to s64 type. It is wrong. Expression should be evaluated using 64-bit arithmetic and then passed.

Fixes 8f2b54824b ("drivers:iio:dac: Add AD3552R driver support")
Signed-off-by Muhammad Usama Anjum

<[email protected]>

drivers/iio/dac/ad3552r.c static void

Quoted 9 lines
---
 drivers/iio/dac/ad3552r.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/dac/ad3552r.c +0 -0
index 97f13c0b9631..b03d3c7cd4c4 100644
@@ -770,7 +770,7 @@ static void

ad3552r_calc_gain_and_offset(struct ad3552r_desc *dac, s32 ch)

drivers/iio/dac/ad3552r.c static void · line 770

Quoted 1 line
drivers/iio/dac/ad3552r.c +0 -0
 	dac->ch_data[ch].scale_dec = DIV_ROUND_CLOSEST((s64)rem

* 1000000,

drivers/iio/dac/ad3552r.c static void · line 773

Quoted 3 lines
drivers/iio/dac/ad3552r.c +0 -1
 							65536);
 
-	dac->ch_data[ch].offset_int = div_s64_rem(v_min * 65536,

span, &rem);

Quoted 9 lines

drivers/iio/dac/ad3552r.c static void · line 773

Quoted 1 line
drivers/iio/dac/ad3552r.c +1 -0
+	dac->ch_data[ch].offset_int = div_s64_rem(v_min * 65536L,

span, &rem);

"v_min" is relatively close to zero on a number line so this can't overflow. There is no way that this change affects anything at runtime (except making the code a tiny tiny bit slower).

And it should be 65536LL for 32 bit systems?

If I'm not missing nothing obvious, 65536LL is the right thing to do... I did not really checked, but if v_min * 65536 can never overflow, then yeah, this is not really "fixing" nothing.

- Nuno Sá