Skip to content

Commit 4f07184

Browse files
committed
Use div_ceil in quantize to avoid overflow
Replace the manual ceil division in quantize with v.div_ceil(s) to avoid potential overflow and make the intent clearer. Also apply minor formatting tidies in src/tests.rs (align/compact inline comments) without changing test behavior.
1 parent 8b82a8d commit 4f07184

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/math.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ pub fn quantize(value: u16, step: u16, rounding: Rounding) -> u16 {
227227
let s = u32::from(step);
228228
let result = match rounding {
229229
Rounding::Floor => (v / s) * s,
230-
Rounding::Ceil => ((v + s - 1) / s) * s,
230+
Rounding::Ceil => v.div_ceil(s) * s,
231231
Rounding::Nearest => ((v + s / 2) / s) * s,
232232
};
233233
result.min(u32::from(u16::MAX)) as u16

src/tests.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -902,13 +902,13 @@ fn quantize_step_equals_max_u16() {
902902
fn tickless_ramp_to_max_with_large_step_not_skipped() {
903903
let curve = linear_curve();
904904
let schedule = curve.tickless_schedule(
905-
0, // t0_ms
906-
100, // duration_ms
907-
0, // start_val
908-
65535, // end_val
909-
2000, // step — triggers the overflow in old code
905+
0, // t0_ms
906+
100, // duration_ms
907+
0, // start_val
908+
65535, // end_val
909+
2000, // step — triggers the overflow in old code
910910
Rounding::Nearest,
911-
0, // min_dt_ms
911+
0, // min_dt_ms
912912
);
913913

914914
// At t=0, the ramp should NOT be finished — it should have intermediate steps.
@@ -948,13 +948,13 @@ fn tickless_ramp_to_max_with_large_step_not_skipped() {
948948
fn tickless_ramp_from_max_with_large_step_ceil() {
949949
let curve = linear_curve();
950950
let schedule = curve.tickless_schedule(
951-
0, // t0_ms
952-
100, // duration_ms
953-
65535, // start_val
954-
0, // end_val
955-
2000, // step
951+
0, // t0_ms
952+
100, // duration_ms
953+
65535, // start_val
954+
0, // end_val
955+
2000, // step
956956
Rounding::Ceil,
957-
0, // min_dt_ms
957+
0, // min_dt_ms
958958
);
959959

960960
let dl_start = schedule.next_deadline(0);

0 commit comments

Comments
 (0)