Bug 208113 - Wrong return range in fixp_sin16/fixp_cos16
Summary: Wrong return range in fixp_sin16/fixp_cos16
Status: NEW
Alias: None
Product: Drivers
Classification: Unclassified
Component: Other (show other bugs)
Hardware: All Linux
: P1 normal
Assignee: drivers_other
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-06-09 15:39 UTC by Bernat Arlandis
Modified: 2020-06-09 15:39 UTC (History)
0 users

See Also:
Kernel Version: 5.7
Subsystem:
Regression: No
Bisected commit-id:


Attachments

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.

Note You need to log in before you can comment on or make changes to this bug.