Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor times inverse of sqrt 2 calculation #21293

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions quantum/mousekey.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down