Bug 208113

Summary: Wrong return range in fixp_sin16/fixp_cos16
Product: Drivers Reporter: Bernat Arlandis (bernat.arlandis)
Component: OtherAssignee: drivers_other
Status: NEW ---    
Severity: normal    
Priority: P1    
Hardware: All   
OS: Linux   
Kernel Version: 5.7 Subsystem:
Regression: No Bisected commit-id:

Description Bernat Arlandis 2020-06-09 15:39:10 UTC
Dear developers,

I'm writing a device driver using the fixp_sin16 macro. The comments in the header file say the range of the return value is [-0x7fff, 0x7fff] but instead I get [-0x8000, 0x7fff]. I've noticed it because I get overflows when doing calculations with the returned value.

The macro in linux/fixp-arith.h is rounding by shifting the sin32 value but this rounds the negative as well as the positive values down, changing -0x7fffffff to -0x8000.

I've wrote my own macro to fix the issue like this:

#define fixp_sin16(v) (((v % 360) > 180)? -(fixp_sin32((v % 360) - 180) >> 16) : fixp_sin32(v) >> 16)

The rounding should always be done on positive values changing the sign afterwards.

The function __fixp_sin32 could be called directly instead.

I don't have much experience submitting bugs to the kernel. I'll try to help as I can.

Regards.