diff options
author | フィルターペーパー <[email protected]> | 2023-07-07 22:50:21 +0800 |
---|---|---|
committer | GitHub <[email protected]> | 2023-07-08 00:50:21 +1000 |
commit | 9b3ac793bca14dc5efd424ba28324937b7bed40d (patch) | |
tree | 08140953e45c1d6227614b5c40d50bc8ff438caa /quantum | |
parent | a0ea7a6b17026dc15a535053b3f0728dabf6d710 (diff) | |
download | qmk_firmware-9b3ac793bca14dc5efd424ba28324937b7bed40d.tar.gz qmk_firmware-9b3ac793bca14dc5efd424ba28324937b7bed40d.zip |
Refactor times inverse of sqrt 2 calculation (#21293)
Diffstat (limited to 'quantum')
-rw-r--r-- | quantum/mousekey.c | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/quantum/mousekey.c b/quantum/mousekey.c index aaaf7fff22..c982a2f40b 100644 --- a/quantum/mousekey.c +++ b/quantum/mousekey.c @@ -25,11 +25,16 @@ #include "mousekey.h" static inline int8_t times_inv_sqrt2(int8_t x) { - // 181/256 is pretty close to 1/sqrt(2) - // 0.70703125 0.707106781 - // 1 too small for x=99 and x=198 - // This ends up being a mult and discard lower 8 bits - return (x * 181) >> 8; + // 181/256 (0.70703125) is used as an approximation for 1/sqrt(2) + // because it is close to the exact value which is 0.707106781 + const int16_t n = x * 181; + const uint16_t d = 256; + + // To ensure that the integer result is rounded accurately after + // division, check the sign of the numerator: + // If negative, subtract half of the denominator before dividing + // Otherwise, add half of the denominator before dividing + return n < 0 ? (n - d / 2) / d : (n + d / 2) / d; } static report_mouse_t mouse_report = {0}; |