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):
complexregistered alongsidetime/rangein c_type and the value-type default initialiser.Complexadded 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 * complexlower to sp_complex_add / sp_complex_mul; infer_call_type returnscomplexso chained results stay typed..real/.imaginaryfield-access (returns float),.conjugatereturns a fresh sp_Complex.iq += complex_expr/iq *= complex_exprdesugars toiq = 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
typedef struct{mrb_int first;mrb_int last;}sp_Range;static sp_Range sp_range_new(mrb_int f,mrb_int l){sp_Range r;r.first=f;r.last=l;return r;}/* ---- Complex runtime ---- *//* Value-type Cartesian Complex: 16 bytes, passed by value. Used by optcarrot's nestopia palette generator; the palette is precomputed in the default code path so this is exercised only with `--nestopia-palette`. */typedef struct{mrb_float re;mrb_float im;}sp_Complex;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;}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;}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;}static inline sp_Complex sp_complex_conjugate(sp_Complex a){sp_Complex c;c.re=a.re;c.im=-a.im;return c;}/* ---- Time runtime ---- *//* sp_Time keeps Time.now / Time.at as value-typed structs. d78149b's sub-second precision is preserved by inlining tv_sec + tv_nsec/1e9spinel_codegen.rbmodifiedView file
Loading diff…
test/complex_polar.rbaddedView file
Loading diff…
test/complex_polar.rb.expectedaddedView file
Loading diff…