Commit f9c166c8

f9c166c8e26bMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 7 files · +2 added · ~5 modified

Description

codegen: implement Rational arithmetic over mrb_int (Phase 1)

Rational was only scaffolded -- the type, 0r literals, Integer#quo, the numerator/denominator accessors and inspect existed, but Rational(n, d) and all arithmetic raised "unsupported" at compile time. Wire up a usable Rational over fixed mrb_int num/den:

  • Rational(n, d) / Rational(n) Kernel constructor.
        • / ** (Integer exponent -> Rational, else Float), unary -@/+@, abs.
  • < > <= >= <=> == != (cross-multiplied, equality via canonical form).
  • to_f / to_i / to_r / truncate, and to_s (1/3) distinct from inspect ((1/3)) -- previously to_s wrongly used the inspect form.
  • Integer Rational coerces the Integer to n/1 (covers 2/3r, 1 + r).
  • puts / interpolation / p of a Rational (and Complex, which had the same puts/interp gap).

Arithmetic is computed in a wider intermediate (__int128 on 64-bit) and a result that does not fit back into mrb_int raises RangeError. mruby promotes to Bigint here instead; a later phase can do the same -- the value-type struct stays {mrb_int num, den} for now. Storing a Rational in a poly array is still unsupported (it has no sp_RbVal box form, same as Complex); the operand guards fall through rather than miscompiling that case.

Adds test/rational_arithmetic.rb. Gates: test 1000/0/0, bench 57/57, optcarrot checksum 59662.

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

Details

Commit
f9c166c8e26b
Parent
4819d64b94ad

lib/sp_runtime.hmodifiedView file

@@ -336,6 +336,83 @@
336336 o[n] = 0;
337337 return o;
338338 }
339+/* Rational#to_s: bare `num/den` (no parens, unlike #inspect). */
340+static const char *sp_rational_to_s(sp_Rational r) {
341+ char buf[64];
342+ int n = snprintf(buf, sizeof(buf), "%lld/%lld", (long long)r.num, (long long)r.den);
343+ if (n < 0) n = 0;
344+ char *o = sp_str_alloc_raw(n + 1);
345+ memcpy(o, buf, n);
346+ o[n] = 0;
347+ return o;
348+}
349+/* Phase-1 Rational arithmetic over fixed mrb_int num/den. Intermediate
350+ products are computed in a wider type and any result that does not fit back
351+ into mrb_int raises RangeError (mruby promotes to Bigint here -- a later
352+ phase can do the same; see docs). __int128 covers the 64-bit build; the
353+ 32-bit build's int64 intermediate covers two int32 operands losslessly. */
354+#if INTPTR_MAX > 0x7fffffff
355+typedef __int128 sp_rat_wide;
356+#else
357+typedef long long sp_rat_wide;
358+#endif
359+static inline mrb_int sp_rat_fit(sp_rat_wide v) {
360+ if (v > (sp_rat_wide)INTPTR_MAX || v < (sp_rat_wide)(-INTPTR_MAX))
361+ sp_raise_cls("RangeError", "Rational out of mrb_int range");
362+ return (mrb_int)v;
363+}
364+static sp_Rational sp_rational_new_wide(sp_rat_wide n, sp_rat_wide d) {
365+ if (d == 0) sp_raise_cls("ZeroDivisionError", "divided by 0");
366+ if (d < 0) { n = -n; d = -d; }
367+ sp_rat_wide a = n < 0 ? -n : n, b = d;
368+ while (b) { sp_rat_wide t = b; b = a % b; a = t; }
369+ if (a <= 0) a = 1;
370+ sp_Rational r;
371+ r.num = sp_rat_fit(n / a);
372+ r.den = sp_rat_fit(d / a);
373+ return r;
374+}
375+static inline sp_Rational sp_rational_add(sp_Rational a, sp_Rational b) {
376+ return sp_rational_new_wide((sp_rat_wide)a.num * b.den + (sp_rat_wide)b.num * a.den,
377+ (sp_rat_wide)a.den * b.den);
378+}
379+static inline sp_Rational sp_rational_sub(sp_Rational a, sp_Rational b) {
380+ return sp_rational_new_wide((sp_rat_wide)a.num * b.den - (sp_rat_wide)b.num * a.den,
381+ (sp_rat_wide)a.den * b.den);
382+}
383+static inline sp_Rational sp_rational_mul(sp_Rational a, sp_Rational b) {
384+ return sp_rational_new_wide((sp_rat_wide)a.num * b.num, (sp_rat_wide)a.den * b.den);
385+}
386+static inline sp_Rational sp_rational_div(sp_Rational a, sp_Rational b) {
387+ if (b.num == 0) sp_raise_cls("ZeroDivisionError", "divided by 0");
388+ return sp_rational_new_wide((sp_rat_wide)a.num * b.den, (sp_rat_wide)a.den * b.num);
389+}
390+static inline sp_Rational sp_rational_neg(sp_Rational a) { a.num = -a.num; return a; }
391+static inline sp_Rational sp_rational_abs(sp_Rational a) { if (a.num < 0) a.num = -a.num; return a; }
392+static inline mrb_int sp_rational_cmp(sp_Rational a, sp_Rational b) {
393+ sp_rat_wide l = (sp_rat_wide)a.num * b.den, r = (sp_rat_wide)b.num * a.den;
394+ return l < r ? -1 : (l > r ? 1 : 0);
395+}
396+static inline mrb_bool sp_rational_eq(sp_Rational a, sp_Rational b) {
397+ return a.num == b.num && a.den == b.den;
398+}
399+static inline mrb_float sp_rational_to_f(sp_Rational a) {
400+ return (mrb_float)a.num / (mrb_float)a.den;
401+}
402+static sp_rat_wide sp_rat_ipow(sp_rat_wide base, mrb_int e) {
403+ sp_rat_wide r = 1;
404+ for (mrb_int i = 0; i < e; i++) {
405+ r *= base;
406+ if (r > (sp_rat_wide)INTPTR_MAX || r < (sp_rat_wide)(-INTPTR_MAX))
407+ sp_raise_cls("RangeError", "Rational out of mrb_int range");
408+ }
409+ return r;
410+}
411+static sp_Rational sp_rational_pow(sp_Rational a, mrb_int e) {
412+ if (e >= 0) return sp_rational_new_wide(sp_rat_ipow(a.num, e), sp_rat_ipow(a.den, e));
413+ if (a.num == 0) sp_raise_cls("ZeroDivisionError", "divided by 0");
414+ return sp_rational_new_wide(sp_rat_ipow(a.den, -e), sp_rat_ipow(a.num, -e));
415+}
339416
340417 /* ---- Time runtime ---- */
341418 /* sp_Time and the libc-backed accessors / formatters live in