1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/// Defines how a parameter maps between plain and normalized values.
///
/// `Copy` because every variant is POD (two scalar fields). Lets format
/// wrappers pass `info.range` by value without `clone()` noise.
#[derive(Clone, Copy, Debug)]
pub enum ParamRange {
Linear { min: f64, max: f64 },
Logarithmic { min: f64, max: f64 },
Discrete { min: i64, max: i64 },
Enum { count: usize },
}
impl ParamRange {
/// Map a plain value to 0.0–1.0.
///
/// Degenerate bounds - `min == max` for `Linear` / `Discrete`,
/// non-positive or empty for `Logarithmic`, `count <= 1` for
/// `Enum` - collapse to `0.0`. Combined with [`Self::denormalize`]
/// returning `min` on the same inputs, the pair is round-trip
/// stable: the result always converges to the bottom of the
/// (degenerate) range rather than producing NaN or wrapping into
/// nonsense.
// `min == max` detects mathematically zero-width ranges; an epsilon
// would mis-route a user-defined `Linear { 1.0, 1.0 + EPSILON }`.
// `i64 → f64` casts on `Discrete` bounds are lossless in practice
// (no sane param has > 2^52 steps).
#[allow(clippy::float_cmp, clippy::cast_precision_loss)]
#[must_use]
pub fn normalize(&self, plain: f64) -> f64 {
match self {
Self::Linear { min, max } => {
if max == min {
return 0.0;
}
((plain - min) / (max - min)).clamp(0.0, 1.0)
}
Self::Logarithmic { min, max } => {
if *min <= 0.0 || *max <= 0.0 || min == max {
return 0.0;
}
// `plain.ln()` returns NaN for `plain <= 0`; the
// post-clamp leaves the NaN intact and a host that
// briefly overshoots automation below `min` ends up
// with a NaN normalized value flowing into saved
// state and the GUI round-trip.
if plain <= *min {
return 0.0;
}
if plain >= *max {
return 1.0;
}
let min_log = min.ln();
let max_log = max.ln();
((plain.ln() - min_log) / (max_log - min_log)).clamp(0.0, 1.0)
}
Self::Discrete { min, max } => {
if max == min {
return 0.0;
}
((plain - *min as f64) / (*max as f64 - *min as f64)).clamp(0.0, 1.0)
}
Self::Enum { count } => {
if *count <= 1 {
return 0.0;
}
(plain / (*count as f64 - 1.0)).clamp(0.0, 1.0)
}
}
}
/// Map 0.0–1.0 back to a plain value.
///
/// Degenerate bounds collapse to `min` (or `0.0` for `Enum` with
/// `count <= 1`). See [`Self::normalize`] for the round-trip
/// semantics.
// `min == max` detects mathematically zero-width ranges; matches
// `normalize`'s asymmetric handling so the pair stays stable.
// `i64 → f64` and `usize → f64` casts on `Discrete` / `Enum`
// bounds are lossless in practice (no sane param has > 2^52 steps).
#[allow(clippy::float_cmp, clippy::cast_precision_loss)]
#[must_use]
pub fn denormalize(&self, normalized: f64) -> f64 {
let n = normalized.clamp(0.0, 1.0);
match self {
Self::Linear { min, max } => min + n * (max - min),
Self::Logarithmic { min, max } => {
// Match `normalize`'s asymmetric handling of bad bounds:
// if either end is non-positive or the range is empty,
// both directions collapse to `min` (round-trip stable).
if *min <= 0.0 || *max <= 0.0 || min == max {
return *min;
}
let min_log = min.ln();
let max_log = max.ln();
(min_log + n * (max_log - min_log)).exp()
}
Self::Discrete { min, max } => {
((*min as f64) + n * (*max as f64 - *min as f64)).round()
}
Self::Enum { count } => {
if *count <= 1 {
return 0.0;
}
(n * (*count as f64 - 1.0)).round()
}
}
}
/// Plain-value minimum.
// `i64 → f64` is lossless for the bounds in practice (no sane
// param has > 2^52 steps).
#[allow(clippy::cast_precision_loss)]
#[must_use]
pub fn min(&self) -> f64 {
match self {
Self::Linear { min, .. } | Self::Logarithmic { min, .. } => *min,
Self::Discrete { min, .. } => *min as f64,
Self::Enum { .. } => 0.0,
}
}
/// Plain-value maximum.
// `i64 → f64` and `usize → f64` are lossless for the bounds in
// practice.
#[allow(clippy::cast_precision_loss)]
#[must_use]
pub fn max(&self) -> f64 {
match self {
Self::Linear { max, .. } | Self::Logarithmic { max, .. } => *max,
Self::Discrete { max, .. } => *max as f64,
Self::Enum { count } => (*count as f64 - 1.0).max(0.0),
}
}
/// Number of discrete steps for a quantized range.
///
/// `None` means continuous (Linear / Logarithmic). `Some(n)` means
/// the range covers `n + 1` distinct values (a step count of 3 →
/// 4 picker positions). Cross-format wrappers that serialize a
/// `0 = continuous` sentinel into a C struct should call
/// `.map(NonZeroU32::get).unwrap_or(0)` at the FFI boundary.
///
/// Discrete / Enum variants with degenerate bounds (`min > max`,
/// or `count <= 1`) return `None` - semantically continuous,
/// because there's nothing to step through.
#[must_use]
pub fn step_count(&self) -> Option<std::num::NonZeroU32> {
let raw: u32 = match self {
Self::Linear { .. } | Self::Logarithmic { .. } => 0,
// `max - min` as `i64` is fine, but `as u32` wraps for
// `min > max` or steps > u32::MAX. Saturate instead so a
// mis-specified `Discrete` range can't produce a bogus
// step count that callers might index with.
Self::Discrete { min, max } => {
// Result is `min`-clamped to `0..=u32::MAX`.
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let n = (max.saturating_sub(*min)).max(0).min(i64::from(u32::MAX)) as u32;
n
}
// Enum variant counts are well below `u32::MAX` in practice
// (typical < 100); the saturating_sub keeps `count = 0` honest.
#[allow(clippy::cast_possible_truncation)]
Self::Enum { count } => (*count as u32).saturating_sub(1),
};
std::num::NonZeroU32::new(raw)
}
/// `step_count` widened to `usize` with the continuous case
/// flattened to `1`. Convenience for UI code that loops over
/// discrete values and falls back to a single step for continuous
/// ranges.
#[must_use]
pub fn step_count_usize(&self) -> usize {
self.step_count().map_or(1, |n| n.get() as usize)
}
}
#[cfg(test)]
mod tests {
// Round-trip and degenerate-bounds tests assert exact float
// results (0.0, midpoints, fixed points) - equality is the
// contract being verified. Cast truncations in this module are
// bounded by the literal `count: 4` test fixtures.
#![allow(
clippy::float_cmp,
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::cast_precision_loss
)]
use super::*;
#[test]
fn linear_round_trip() {
let range = ParamRange::Linear {
min: -60.0,
max: 24.0,
};
for plain in [-60.0, -30.0, 0.0, 12.0, 24.0] {
let norm = range.normalize(plain);
let back = range.denormalize(norm);
assert!(
(back - plain).abs() < 1e-10,
"plain={plain}, norm={norm}, back={back}"
);
}
}
#[test]
fn log_round_trip() {
let range = ParamRange::Logarithmic {
min: 20.0,
max: 20000.0,
};
for plain in [20.0, 100.0, 1000.0, 10000.0, 20000.0] {
let norm = range.normalize(plain);
let back = range.denormalize(norm);
assert!(
(back - plain).abs() < 0.01,
"plain={plain}, norm={norm}, back={back}"
);
}
}
#[test]
fn enum_round_trip() {
let range = ParamRange::Enum { count: 4 };
for idx in 0..4 {
let norm = range.normalize(idx as f64);
let back = range.denormalize(norm);
assert_eq!(back as usize, idx);
}
}
/// Degenerate bounds (empty/non-positive/single-step) collapse the
/// round trip to a fixed point at `min` rather than producing NaN
/// or wrapping. Locks in `normalize → 0.0`, `denormalize(0.0) →
/// min`, and `normalize(min) → 0.0` for every range variant so a
/// future maintainer simplifying one branch can't accidentally
/// reintroduce divergent behavior.
#[test]
fn degenerate_bounds_round_trip_stable() {
let cases = [
ParamRange::Linear { min: 5.0, max: 5.0 },
ParamRange::Logarithmic {
min: 100.0,
max: 100.0,
},
ParamRange::Logarithmic {
min: -1.0,
max: 10.0,
},
ParamRange::Logarithmic { min: 1.0, max: 0.0 },
ParamRange::Discrete { min: 7, max: 7 },
ParamRange::Enum { count: 0 },
ParamRange::Enum { count: 1 },
];
for range in cases {
let bottom = range.min();
assert_eq!(range.normalize(bottom), 0.0, "normalize(min) for {range:?}");
assert_eq!(
range.normalize(42.0),
0.0,
"normalize(arbitrary) for {range:?}"
);
assert_eq!(
range.denormalize(0.0),
bottom,
"denormalize(0.0) for {range:?}"
);
assert_eq!(
range.denormalize(0.5),
bottom,
"denormalize(mid) for {range:?}"
);
// Double round trip lands at the same fixed point.
let once = range.denormalize(range.normalize(42.0));
let twice = range.denormalize(range.normalize(once));
assert_eq!(once, twice, "round-trip not stable for {range:?}");
}
}
/// `normalize` must never return NaN. A host that briefly
/// overshoots automation below `min` (or hands us a fresh
/// uninitialized -1.0) would feed `(-1.0).ln()` (= NaN) into
/// saved state and the editor round-trip without the clamp.
#[test]
fn logarithmic_normalize_never_nan() {
let range = ParamRange::Logarithmic {
min: 20.0,
max: 20000.0,
};
for plain in [-1.0, 0.0, 0.5, 19.99, f64::NEG_INFINITY] {
let n = range.normalize(plain);
assert!(!n.is_nan(), "NaN from normalize({plain})");
assert_eq!(n, 0.0, "normalize({plain}) should clamp to 0.0");
}
for plain in [20000.0, 20001.0, 1e9, f64::INFINITY] {
let n = range.normalize(plain);
assert!(!n.is_nan(), "NaN from normalize({plain})");
assert_eq!(n, 1.0, "normalize({plain}) should clamp to 1.0");
}
}
}