Commit e992d02e

e992d02efb02Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(codegen): Time#- subtracts int fields before float conversion

Follow-up to #237 — Gemini's medium-priority review feedback that the float conversion should happen after the integer subtraction, not before. With the original (mrb_float)a.tv_sec - (mrb_float)b.tv_sec shape, both sides round to the nearest representable double before the subtraction, so when tv_sec is past 2^53 (year 9876 onward, or any synthetic large epoch) the low-order bits of the difference are lost even though int64_t would represent them exactly.

Reordered to subtract first, convert second: (mrb_float)(a.tv_sec - b.tv_sec) + (mrb_float)(a.tv_nsec - b.tv_nsec) / 1e9

Same shape applies to the nsec term. Practical impact today is minor (current epoch is ~1.7e9, well inside the 2^53 ≈ 9e15 window), but the int-first form is unconditionally safer at zero cost.

290 tests pass.

Refs #237

Co-Authored-By: Tomoyuki INOUE [email protected] Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
e992d02efb02
Parent
f809e55cf47f

spinel_codegen.rbmodifiedView file

@@ -18309,11 +18309,16 @@
1830918309 return "sp_poly_sub(" + compile_expr(recv) + ", " + box_expr_to_poly(get_args(args_id)[0]) + ")"
1831018310 end
1831118311 # Hoist both sides into temps: each may be a fresh sp_time_now()
18312- # call, and tv_sec / tv_nsec are read twice each.
18312+ # call, and tv_sec / tv_nsec are read twice each. Subtract the
18313+ # integer fields *before* converting to float so we don't lose
18314+ # bits when both timestamps are large (`tv_sec` past 2^53 seconds
18315+ # falls outside double's exact-integer range, where the difference
18316+ # `(double)a - (double)b` could miss the low-order bits even
18317+ # though `(int64_t)(a - b)` is exact).
1831318318 if lt == "time"
1831418319 rhs_id = get_args(args_id)[0]
1831518320 if infer_type(rhs_id) == "time"
18316- return "({ sp_Time _a = " + compile_expr(recv) + "; sp_Time _b = " + compile_expr(rhs_id) + "; ((mrb_float)_a.tv_sec - (mrb_float)_b.tv_sec) + ((mrb_float)_a.tv_nsec - (mrb_float)_b.tv_nsec) / 1e9; })"
18321+ return "({ sp_Time _a = " + compile_expr(recv) + "; sp_Time _b = " + compile_expr(rhs_id) + "; (mrb_float)(_a.tv_sec - _b.tv_sec) + (mrb_float)(_a.tv_nsec - _b.tv_nsec) / 1e9; })"
1831718322 end
1831818323 end
1831918324 r = compile_array_setop_expr(nid, recv, "difference", lt)