Input: ff-memless - fix 180-degree force inversion from s16 negation overflow
Subsystems (2)
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.
--- drivers/input/ff-memless.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/input/ff-memless.c +3 -3
@@ -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