Commit ae1a0f32

ae1a0f329f3dMerged

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

Description

feat: add value-type Complex (sp_Complex) with polar / +/* / conjugate / real

Built-in Complex wired through as a 16-byte value type (sp_Complex with re/im fields, passed by value, no GC). Surfaced via optcarrot's nestopia palette generator (Complex.polar(s, theta) / iq += Complex.polar(...) / (Complex.polar(g, t) * iq.conjugate).real).

Runtime (lib/sp_runtime.h):

  • sp_Complex struct + sp_complex_polar / sp_complex_add / sp_complex_mul / sp_complex_conjugate inlines.

Codegen (spinel_codegen.rb):

  • complex registered alongside time/range in c_type and the value-type default initialiser.
  • Complex added to is_known_constant_name so the bare const doesn't warn.
  • Complex.polar(m, a) lowers to sp_complex_polar; args are sp_poly_to_f-unboxed when arriving as poly (e.g. destructured out of a poly_array, as optcarrot's [[105, 0.570], …].map { |angle, gain| ... } shape does).
  • complex + complex / complex * complex lower to sp_complex_add / sp_complex_mul; infer_call_type returns complex so chained results stay typed.
  • .real / .imaginary field-access (returns float), .conjugate returns a fresh sp_Complex.
  • iq += complex_expr / iq *= complex_expr desugars to iq = sp_complex_op(iq, expr) since C doesn't allow += on structs.

Suppresses 4 of the optcarrot warnings (Complex / polar / conjugate / real). The nestopia palette generator now actually produces correct output (was previously emitting 0 for every Complex op — a silent bug the existing checksum didn't catch because the default defacto_palette is precomputed).

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

Details

Commit
ae1a0f329f3d
Parent
18819eacc369

lib/sp_runtime.hmodifiedView file

@@ -99,6 +99,17 @@
9999 typedef struct{mrb_int first;mrb_int last;}sp_Range;
100100 static sp_Range sp_range_new(mrb_int f,mrb_int l){sp_Range r;r.first=f;r.last=l;return r;}
101101
102+/* ---- Complex runtime ---- */
103+/* Value-type Cartesian Complex: 16 bytes, passed by value. Used by
104+ optcarrot's nestopia palette generator; the palette is precomputed
105+ in the default code path so this is exercised only with
106+ `--nestopia-palette`. */
107+typedef struct{mrb_float re;mrb_float im;}sp_Complex;
108+static inline sp_Complex sp_complex_polar(mrb_float m,mrb_float a){sp_Complex c;c.re=m*cos(a);c.im=m*sin(a);return c;}
109+static inline sp_Complex sp_complex_add(sp_Complex a,sp_Complex b){sp_Complex c;c.re=a.re+b.re;c.im=a.im+b.im;return c;}
110+static inline sp_Complex sp_complex_mul(sp_Complex a,sp_Complex b){sp_Complex c;c.re=a.re*b.re-a.im*b.im;c.im=a.re*b.im+a.im*b.re;return c;}
111+static inline sp_Complex sp_complex_conjugate(sp_Complex a){sp_Complex c;c.re=a.re;c.im=-a.im;return c;}
112+
102113 /* ---- Time runtime ---- */
103114 /* sp_Time keeps Time.now / Time.at as value-typed structs. d78149b's
104115 sub-second precision is preserved by inlining tv_sec + tv_nsec/1e9