| 1 | #include <global.h> |
| 2 | #include <kassert.h> |
| 3 | #include <math/clamp.h> |
| 4 | #include <math/fixed.h> |
| 5 | #include <math/fixed_extended.h> |
| 6 | #include <math/range.h> |
| 7 | #include <scaled_param.h> |
| 8 | #include <smp/core.h> |
| 9 | #include <string.h> |
| 10 | |
| 11 | static size_t geom_interp(size_t a, size_t b, fx32_32_t t) { |
| 12 | if (t <= 0) |
| 13 | return a; |
| 14 | if (t >= FX_ONE) |
| 15 | return b; |
| 16 | if (a == b) |
| 17 | return a; |
| 18 | |
| 19 | fx32_32_t ln_a = fx_ln(x: fx_from_int(x: (int64_t) a)); |
| 20 | fx32_32_t ln_b = fx_ln(x: fx_from_int(x: (int64_t) b)); |
| 21 | fx32_32_t result = fx_exp(x: fx_add(a: ln_a, b: fx_mul(a: t, b: fx_sub(a: ln_b, b: ln_a)))); |
| 22 | |
| 23 | int64_t val = fx_to_int(x: result); |
| 24 | CLAMP(val, (int64_t) a, (int64_t) b); |
| 25 | return (size_t) val; |
| 26 | } |
| 27 | |
| 28 | static size_t linear_interp(size_t a, size_t b, fx32_32_t t) { |
| 29 | if (t <= 0) |
| 30 | return a; |
| 31 | if (t >= FX_ONE) |
| 32 | return b; |
| 33 | if (a == b) |
| 34 | return a; |
| 35 | |
| 36 | fx32_32_t span = fx_from_int(x: (int64_t) b - (int64_t) a); |
| 37 | int64_t val = fx_to_int(x: fx_add(a: fx_from_int(x: (int64_t) a), b: fx_mul(a: t, b: span))); |
| 38 | CLAMP(val, (int64_t) a, (int64_t) b); |
| 39 | return (size_t) val; |
| 40 | } |
| 41 | |
| 42 | static size_t scale_log(fx32_32_t value, size_t min_val, size_t def_val, |
| 43 | size_t max_val) { |
| 44 | value = fx_clamp(x: value, lo: 0, FX_ONE); |
| 45 | if (value == FX_HALF) |
| 46 | return def_val; |
| 47 | if (value < FX_HALF) |
| 48 | return geom_interp(a: min_val, b: def_val, t: value << 1); |
| 49 | return geom_interp(a: def_val, b: max_val, t: (value - FX_HALF) << 1); |
| 50 | } |
| 51 | |
| 52 | static size_t scale_linear(fx32_32_t value, size_t min_val, size_t def_val, |
| 53 | size_t max_val) { |
| 54 | value = fx_clamp(x: value, lo: 0, FX_ONE); |
| 55 | if (value == FX_HALF) |
| 56 | return def_val; |
| 57 | if (value < FX_HALF) |
| 58 | return linear_interp(a: min_val, b: def_val, t: value << 1); |
| 59 | return linear_interp(a: def_val, b: max_val, t: (value - FX_HALF) << 1); |
| 60 | } |
| 61 | |
| 62 | static size_t scale_core_multiplier(fx32_32_t value, size_t min_val, |
| 63 | size_t def_val, size_t max_val) { |
| 64 | size_t per_core = scale_linear(value, min_val, def_val, max_val); |
| 65 | return per_core * (global.core_count > 0 ? global.core_count : 1); |
| 66 | } |
| 67 | |
| 68 | static const scaling_fn_t scale_lut[SCALE_CURVE_MAX] = { |
| 69 | [SCALE_NONE] = NULL, |
| 70 | [SCALE_PIECEWISE_LOG] = scale_log, |
| 71 | [SCALE_PIECEWISE_LINEAR] = scale_linear, |
| 72 | [SCALE_CORE_MULTIPLIER] = scale_core_multiplier, |
| 73 | [SCALE_CUSTOM] = NULL, |
| 74 | }; |
| 75 | |
| 76 | size_t scaled_param_eval(const struct scaled_param *desc, fx32_32_t value) { |
| 77 | if (!desc || desc->curve == SCALE_NONE) |
| 78 | return 0; |
| 79 | |
| 80 | if (desc->curve == SCALE_CUSTOM) |
| 81 | return desc->custom_scale |
| 82 | ? desc->custom_scale(value, desc->min_val, desc->def_val, |
| 83 | desc->max_val) |
| 84 | : desc->def_val; |
| 85 | |
| 86 | kassert(IN_RANGE(desc->curve, SCALE_NONE, SCALE_CUSTOM)); |
| 87 | scaling_fn_t fn = scale_lut[desc->curve]; |
| 88 | return fn ? fn(value, desc->min_val, desc->def_val, desc->max_val) |
| 89 | : desc->def_val; |
| 90 | } |
| 91 | |
| 92 | size_t scaled_param_format(const struct scaled_param *desc, fx32_32_t value, |
| 93 | size_t scaled_val, char *buf, size_t cap) { |
| 94 | if (!desc || desc->curve == SCALE_NONE) |
| 95 | return 0; |
| 96 | if (desc->custom_print) |
| 97 | return desc->custom_print(value, scaled_val, buf, cap); |
| 98 | return snprintf(buffer: buf, buffer_len: cap, format: "%zu %s" , scaled_val, |
| 99 | desc->unit ? desc->unit : "units" ); |
| 100 | } |
| 101 | |