From e3fe51dde5afed99a393e3b1b1f611fde011d9f3 Mon Sep 17 00:00:00 2001 From: a1xd <68629610+a1xd@users.noreply.github.com> Date: Thu, 8 Apr 2021 12:38:08 -0400 Subject: fix some things --- common/accel-natural.hpp | 4 ++-- wrapper/wrapper.cpp | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/common/accel-natural.hpp b/common/accel-natural.hpp index 8d25351..9f76d1a 100644 --- a/common/accel-natural.hpp +++ b/common/accel-natural.hpp @@ -28,7 +28,7 @@ namespace rawaccel { double offset_x = offset - x; double decay = exp(accel * offset_x); - return limit * (1 - (decay * offset_x + offset) / x) + 1; + return limit * (1 - (offset - decay * offset_x) / x) + 1; } using natural_base::natural_base; @@ -43,7 +43,7 @@ namespace rawaccel { double offset_x = offset - x; double decay = exp(accel * offset_x); - double output = limit * (offset_x + decay / accel) + constant; + double output = limit * (decay / accel - offset_x) + constant; return output / x + 1; } diff --git a/wrapper/wrapper.cpp b/wrapper/wrapper.cpp index 7992de6..6376100 100644 --- a/wrapper/wrapper.cpp +++ b/wrapper/wrapper.cpp @@ -517,9 +517,9 @@ public: auto settings = gcnew ExtendedSettings(); Marshal::PtrToStructure(IntPtr(&instance->data.args), settings->baseSettings); settings->tables.x = extract(instance->data.args.argsv.x.lut_args.mode, - instance->data.mod.accels.x); + instance->data.mod.accel.x); settings->tables.y = extract(instance->data.args.argsv.y.lut_args.mode, - instance->data.mod.accels.y); + instance->data.mod.accel.y); return settings; } @@ -530,11 +530,11 @@ public: instance->inv = ra::invokers(instance->data.args); if (val->tables.x) { - val->tables.x->SetData(instance->data.mod.accels.x); + val->tables.x->SetData(instance->data.mod.accel.x); } if (val->tables.y) { - val->tables.y->SetData(instance->data.mod.accels.y); + val->tables.y->SetData(instance->data.mod.accel.y); } } -- cgit v1.2.3 From 6197390760eba8ca1123d03637cbb9af0414c5f7 Mon Sep 17 00:00:00 2001 From: a1xd <68629610+a1xd@users.noreply.github.com> Date: Thu, 8 Apr 2021 12:45:43 -0400 Subject: fix conversions in arbitrary constructor --- common/accel-lookup.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/common/accel-lookup.hpp b/common/accel-lookup.hpp index 24b61e5..ff89fa7 100644 --- a/common/accel-lookup.hpp +++ b/common/accel-lookup.hpp @@ -262,9 +262,9 @@ namespace rawaccel { last_arbitrary_index = length - 2; last_point_speed = points[last_arbitrary_index].x; - double start = (int)floor(log(first_point_speed)); - double end = (int)floor(log(last_point_speed)); - double num = (int)floor(LUT_CAPACITY / (end - start)); + int start = static_cast(log(first_point_speed)); + int end = static_cast(log(last_point_speed)); + int num = static_cast(LUT_CAPACITY / (end - start)); range = fp_rep_range{ start, end, num }; last_log_lookup_index = num * (end - start) - 1; -- cgit v1.2.3 From 74ffcb8553795f4b50e544a1b2a0e53aec32a860 Mon Sep 17 00:00:00 2001 From: a1xd <68629610+a1xd@users.noreply.github.com> Date: Thu, 8 Apr 2021 21:48:17 -0400 Subject: make sizeof arbitrary close to others refactor constructor/fix conversions --- common/accel-lookup.hpp | 57 +++++++++++++++++++++++++++----------------- common/accel-union.hpp | 3 ++- common/rawaccel-base.hpp | 2 +- common/rawaccel-validate.hpp | 2 +- wrapper/wrapper.cpp | 2 +- 5 files changed, 40 insertions(+), 26 deletions(-) diff --git a/common/accel-lookup.hpp b/common/accel-lookup.hpp index ff89fa7..6ebc90b 100644 --- a/common/accel-lookup.hpp +++ b/common/accel-lookup.hpp @@ -3,6 +3,8 @@ #include "rawaccel-base.hpp" #include "utility.hpp" +#include + namespace rawaccel { struct linear_range { @@ -55,7 +57,7 @@ namespace rawaccel { template struct lut_base { - enum { capacity = LUT_CAPACITY }; + enum { capacity = SPACED_LUT_CAPACITY }; using value_t = float; template @@ -150,19 +152,21 @@ namespace rawaccel { }; struct si_pair { - double slope = 0; - double intercept = 0; + float slope = 0; + float intercept = 0; }; struct arbitrary_lut_point { - double applicable_speed = 0; + float applicable_speed = 0; si_pair slope_intercept = {}; }; struct arbitrary_lut { + enum { capacity = SPACED_LUT_CAPACITY / 4 }; + fp_rep_range range; - arbitrary_lut_point data[LUT_CAPACITY] = {}; - int log_lookup[LUT_CAPACITY] = {}; + arbitrary_lut_point data[capacity] = {}; + int log_lookup[capacity] = {}; double first_point_speed; double last_point_speed; int last_arbitrary_index; @@ -226,8 +230,20 @@ namespace rawaccel { return pair.slope + pair.intercept / speed; } - void fill(vec2d* points, int length) + + void init(vec2d* points, int length) { + first_point_speed = points[0].x; + // -2 because the last index in the arbitrary array is used for slope-intercept only + last_arbitrary_index = length - 2; + last_point_speed = points[last_arbitrary_index].x; + + int start = static_cast(floor(log(first_point_speed))); + int end = static_cast(floor(log(last_point_speed))); + int num = static_cast(capacity / (end - start)); + range = fp_rep_range{ start, end, num }; + last_log_lookup_index = num * (end - start) - 1; + vec2d current = {0, 0}; vec2d next; int log_index = 0; @@ -240,14 +256,20 @@ namespace rawaccel { next = points[i]; double slope = (next.y - current.y) / (next.x - current.x); double intercept = next.y - slope * next.x; - si_pair current_si = { slope, intercept }; - arbitrary_lut_point current_lut_point = { next.x, current_si }; + si_pair current_si = { + static_cast(slope), + static_cast(intercept) + }; + arbitrary_lut_point current_lut_point = { + static_cast(next.x), + current_si + }; this->data[i] = current_lut_point; while (log_value < next.x) { - this->log_lookup[log_index] = log_value; + this->log_lookup[log_index] = static_cast(log_value); log_index++; log_inner_iterator += log_inner_slice; log_value = pow(2, log_inner_iterator); @@ -257,18 +279,9 @@ namespace rawaccel { arbitrary_lut(vec2d* points, int length) { - first_point_speed = points[0].x; - // -2 because the last index in the arbitrary array is used for slope-intercept only - last_arbitrary_index = length - 2; - last_point_speed = points[last_arbitrary_index].x; - - int start = static_cast(log(first_point_speed)); - int end = static_cast(log(last_point_speed)); - int num = static_cast(LUT_CAPACITY / (end - start)); - range = fp_rep_range{ start, end, num }; - last_log_lookup_index = num * (end - start) - 1; - - fill(points, length); + init(points, length); } + + arbitrary_lut(const accel_args&) {} }; } diff --git a/common/accel-union.hpp b/common/accel-union.hpp index 7f3d5d5..f5c26ba 100644 --- a/common/accel-union.hpp +++ b/common/accel-union.hpp @@ -66,7 +66,7 @@ namespace rawaccel { case internal_mode::motivity_gain: return vis(u.motivity_g); case internal_mode::lut_log: return vis(u.log_lut); case internal_mode::lut_lin: return vis(u.lin_lut); - case internal_mode::lut_arb: + case internal_mode::lut_arb: return vis(u.arb_lut); default: return vis(u.noaccel); } } @@ -84,6 +84,7 @@ namespace rawaccel { motivity motivity_g; linear_lut lin_lut; binlog_lut log_lut; + arbitrary_lut arb_lut; accel_noaccel noaccel = {}; accel_union(const accel_args& args) diff --git a/common/rawaccel-base.hpp b/common/rawaccel-base.hpp index dde56f5..2f49ec0 100644 --- a/common/rawaccel-base.hpp +++ b/common/rawaccel-base.hpp @@ -15,7 +15,7 @@ namespace rawaccel { inline constexpr size_t MAX_DEV_ID_LEN = 200; - inline constexpr size_t LUT_CAPACITY = 1025; + inline constexpr size_t SPACED_LUT_CAPACITY = 1025; inline constexpr double MAX_NORM = 16; inline constexpr double PI = 3.14159265358979323846; diff --git a/common/rawaccel-validate.hpp b/common/rawaccel-validate.hpp index 4f7dd9c..ef6f667 100644 --- a/common/rawaccel-validate.hpp +++ b/common/rawaccel-validate.hpp @@ -27,7 +27,7 @@ namespace rawaccel { }; auto check_accel = [&error](const accel_args& args) { - static_assert(LUT_CAPACITY == 1025, "update error msg"); + static_assert(SPACED_LUT_CAPACITY == 1025, "update error msg"); const auto& lut_args = args.lut_args; diff --git a/wrapper/wrapper.cpp b/wrapper/wrapper.cpp index 6376100..f7da5d4 100644 --- a/wrapper/wrapper.cpp +++ b/wrapper/wrapper.cpp @@ -210,7 +210,7 @@ public ref struct SpacedLut abstract : public LutBase void SetDataBase(ra::accel_union& accel) { - if (size_t(data->LongLength) > ra::LUT_CAPACITY) { + if (size_t(data->LongLength) > ra::SPACED_LUT_CAPACITY) { throw gcnew InteropException("data is too large"); } } -- cgit v1.2.3 From 1494d248953ceedadd7a8e2fef7c957fa1f7350e Mon Sep 17 00:00:00 2001 From: a1xd <68629610+a1xd@users.noreply.github.com> Date: Thu, 8 Apr 2021 22:00:08 -0400 Subject: make it safe --- common/accel-lookup.hpp | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/common/accel-lookup.hpp b/common/accel-lookup.hpp index 6ebc90b..6eb16d9 100644 --- a/common/accel-lookup.hpp +++ b/common/accel-lookup.hpp @@ -175,27 +175,34 @@ namespace rawaccel { double operator()(double speed) const { int index = 0; + int last_arb_index = last_arbitrary_index; + int last_log_index = last_log_lookup_index; - if (speed < first_point_speed) + if (unsigned(last_arb_index) < capacity && + unsigned(last_log_index) < capacity && + speed > first_point_speed) { - // Apply from 0 index - } - else if (speed > last_point_speed) - { - index = last_arbitrary_index; - } - else if (speed > range.stop) - { - index = search_from(log_lookup[last_log_lookup_index], speed); - } - else if (speed < range.start) - { - index = search_from(0, speed); - } - else - { - int log_lookup = get_log_index(speed); - index = search_from(log_lookup, speed); + if (speed > last_point_speed) + { + index = last_arb_index; + } + else if (speed > range.stop) + { + int last_log = log_lookup[last_log_index]; + if (unsigned(last_log) >= capacity) return 1; + index = search_from(last_log, last_arb_index, speed); + } + else if (speed < range.start) + { + index = search_from(0, last_arb_index, speed); + } + else + { + int log_index = get_log_index(speed); + if (unsigned(log_index) >= capacity) return 1; + index = search_from(log_index, last_arb_index, speed); + } + } return apply(index, speed); @@ -208,7 +215,7 @@ namespace rawaccel { return index; } - int inline search_from(int index, double speed) const + int inline search_from(int index, int last, double speed) const { int prev_index; @@ -217,7 +224,7 @@ namespace rawaccel { prev_index = index; index++; } - while (index <= last_arbitrary_index && data[index].applicable_speed < speed); + while (index <= last && data[index].applicable_speed < speed); index--; -- cgit v1.2.3