Commit a7fc57ad

a7fc57ad28abMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

codegen,analyze,runtime: support clamp on a poly receiver

x.clamp(lo, hi) / x.clamp(a..b) on a widened (poly) receiver had no infer or codegen arm, so it degraded to an unsupported value. Add sp_poly_clamp (tag-dispatching int/float clamp with the existing min>max and NaN checks), infer poly.clamp -> poly, and emit the 2-arg and range forms through it. Fixes clamp_bounds.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
a7fc57ad28ab
Parent
898dcb3e5f03

lib/sp_runtime.hmodifiedView file

@@ -3350,6 +3350,13 @@
33503350 static mrb_bool sp_poly_ge(sp_RbVal a, sp_RbVal b) { mrb_bool comparable; mrb_int cmp = sp_poly_cmp(a, b, &comparable); return comparable ? (cmp >= 0) : FALSE; }
33513351 static sp_RbVal sp_poly_div(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_FLT || b.tag == SP_TAG_FLT) return sp_box_float(sp_poly_to_f(a) / sp_poly_to_f(b)); return sp_box_int(sp_idiv(sp_poly_to_i(a), sp_poly_to_i(b))); }
33523352 static sp_RbVal sp_poly_mod(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_FLT || b.tag == SP_TAG_FLT) return sp_box_float(fmod(sp_poly_to_f(a), sp_poly_to_f(b))); return sp_box_int(sp_imod(sp_poly_to_i(a), sp_poly_to_i(b))); }
3353+/* clamp on a boxed numeric: float path when any operand is a float (NaN/order
3354+ checks via sp_float_clamp_ck), else int path; result keeps the float/int kind. */
3355+static sp_RbVal sp_poly_clamp(sp_RbVal v, sp_RbVal lo, sp_RbVal hi) {
3356+ if (v.tag == SP_TAG_FLT || lo.tag == SP_TAG_FLT || hi.tag == SP_TAG_FLT)
3357+ return sp_box_float(sp_float_clamp_ck(sp_poly_to_f(v), sp_poly_to_f(lo), sp_poly_to_f(hi)));
3358+ return sp_box_int(sp_int_clamp_ck(sp_poly_to_i(v), sp_poly_to_i(lo), sp_poly_to_i(hi)));
3359+}
33533360 /* Integer #** : Spinel has no Rational, so a negative integer exponent --
33543361 which CRuby evaluates to a Rational like (1/2) -- raises RangeError rather
33553362 than silently truncating toward 0. Float ** negative stays a float