Skip to contentSkip to conversation

Input: ff-memless - fix 180-degree force inversion from s16 negation overflow

PATCH New Ian Rogers 1 participant 1 message Nov 19

Subsystems (2)
lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH 1/1 Input: ff-memless - fix 180-degree force inversion from s16 negation overflow

In ml_combine_effects(), constant-force Y component calculation negates fixp_cos16(i) and passes it to fixp_mult(s16 a, s16 b):

  y = fixp_mult(-fixp_cos16(i), level) * gain / 0xffff;

At i = 180 degrees (direction ~ 0x8000), fixp_cos32(180) is -0x7fffffff, and arithmetic right shift by 16 in fixp_cos16(180) produces -0x8000 (-32768). Negating -32768 in 32-bit int yields +32768, which overflows fixp_mult()'s s16 parameter a back to -32768, inverting the Y force from full positive (+127) to full negative (-127) right at 180 degrees.

Widen fixp_mult()'s parameters and return type from s16 to int so +32768 scales cleanly to +0x100 without truncation overflow.

Found by enabling Clang's -Wimplicit-int-conversion-on-negation on an x86_64 allyesconfig build, with AI coding assistant help to audit the warning and prepare the fix. Verified with a standalone C test sweeping 0..360 degrees and Clang build; not runtime-tested on force-feedback hardware.

Fixes 559addc25b00 ("[media] fixp-arith: replace sin/cos table by a better precision one")
---
 drivers/input/ff-memless.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/input/ff-memless.c +3 -3
index d1fefd1dfc0d..ae8ff19905b6 100644
@@ -229,10 +229,10 @@ static inline s16 fixp_new16(s16 a)
 	return ((s32)a) >> (16 - FRAC_N);
 }
 
-static inline s16 fixp_mult(s16 a, s16 b)
+static inline int fixp_mult(int a, int b)
 {
-	a = ((s32)a * 0x100) / 0x7fff;
-	return ((s32)(a * b)) >> FRAC_N;
+	a = (a * 0x100) / 0x7fff;
+	return (a * b) >> FRAC_N;
 }
 
 /*
Signature · 1 line
2.56.1.708.g77570a6a1d-goog