summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authora1xd <[email protected]>2021-03-29 19:57:33 -0400
committera1xd <[email protected]>2021-03-29 19:57:33 -0400
commit11045335c14371847411b8fb5096f479e18fbf5e (patch)
treeb3d81ae9efcfeab78668d73d847682f1b185ad41
parentadd jump type (diff)
downloadrawaccel-11045335c14371847411b8fb5096f479e18fbf5e.tar.xz
rawaccel-11045335c14371847411b8fb5096f479e18fbf5e.zip
add zero/inf/nan guards
-rw-r--r--common/rawaccel.hpp8
-rw-r--r--common/utility.hpp12
-rw-r--r--driver/driver.cpp57
3 files changed, 49 insertions, 28 deletions
diff --git a/common/rawaccel.hpp b/common/rawaccel.hpp
index 67b4e61..f6bc0fd 100644
--- a/common/rawaccel.hpp
+++ b/common/rawaccel.hpp
@@ -218,8 +218,12 @@ namespace rawaccel {
movement.y *= scale;
}
else {
- movement.x *= accels.x.apply(fabs(movement.x) * norm);
- movement.y *= accels.y.apply(fabs(movement.y) * norm);
+ if (movement.x != 0) {
+ movement.x *= accels.x.apply(fabs(movement.x) * norm);
+ }
+ if (movement.y != 0) {
+ movement.y *= accels.y.apply(fabs(movement.y) * norm);
+ }
}
}
}
diff --git a/common/utility.hpp b/common/utility.hpp
index 40bc7c4..d7b63cf 100644
--- a/common/utility.hpp
+++ b/common/utility.hpp
@@ -28,3 +28,15 @@ inline constexpr double maxsd(double a, double b) {
inline constexpr double clampsd(double v, double lo, double hi) {
return minsd(maxsd(v, lo), hi);
}
+
+// returns the unbiased exponent of x if x is normal
+inline int ilogb(double x)
+{
+ union { double f; unsigned long long i; } u = { x };
+ return static_cast<int>((u.i >> 52) & 0x7ff) - 0x3ff;
+}
+
+inline bool infnan(double x)
+{
+ return ilogb(x) == 0x400;
+}
diff --git a/driver/driver.cpp b/driver/driver.cpp
index 235571c..5b9eca4 100644
--- a/driver/driver.cpp
+++ b/driver/driver.cpp
@@ -67,40 +67,45 @@ Arguments:
auto it = InputDataStart;
do {
- vec2d input = {
- static_cast<double>(it->LastX),
- static_cast<double>(it->LastY)
- };
-
- global.modifier.apply_rotation(input);
- global.modifier.apply_angle_snap(input);
-
- if (enable_accel) {
- auto time_supplier = [=] {
- counter_t now = KeQueryPerformanceCounter(NULL).QuadPart;
- counter_t ticks = now - devExt->counter;
- devExt->counter = now;
- milliseconds time = ticks * global.tick_interval;
- return clampsd(time, global.args.time_min, 100);
+ if (it->LastX || it->LastY) {
+ vec2d input = {
+ static_cast<double>(it->LastX),
+ static_cast<double>(it->LastY)
};
- global.modifier.apply_acceleration(input, time_supplier);
- }
+ global.modifier.apply_rotation(input);
+ global.modifier.apply_angle_snap(input);
+
+ if (enable_accel) {
+ auto time_supplier = [=] {
+ counter_t now = KeQueryPerformanceCounter(NULL).QuadPart;
+ counter_t ticks = now - devExt->counter;
+ devExt->counter = now;
+ milliseconds time = ticks * global.tick_interval;
+ return clampsd(time, global.args.time_min, 100);
+ };
- global.modifier.apply_sensitivity(input);
+ global.modifier.apply_acceleration(input, time_supplier);
+ }
- double carried_result_x = input.x + devExt->carry.x;
- double carried_result_y = input.y + devExt->carry.y;
+ global.modifier.apply_sensitivity(input);
- LONG out_x = static_cast<LONG>(carried_result_x);
- LONG out_y = static_cast<LONG>(carried_result_y);
+ double carried_result_x = input.x + devExt->carry.x;
+ double carried_result_y = input.y + devExt->carry.y;
- devExt->carry.x = carried_result_x - out_x;
- devExt->carry.y = carried_result_y - out_y;
+ LONG out_x = static_cast<LONG>(carried_result_x);
+ LONG out_y = static_cast<LONG>(carried_result_y);
- it->LastX = out_x;
- it->LastY = out_y;
+ double carry_x = carried_result_x - out_x;
+ double carry_y = carried_result_y - out_y;
+ if (!infnan(carry_x + carry_y)) {
+ devExt->carry.x = carried_result_x - out_x;
+ devExt->carry.y = carried_result_y - out_y;
+ it->LastX = out_x;
+ it->LastY = out_y;
+ }
+ }
} while (++it != InputDataEnd);
}