Commit aa11881b

aa11881b2533Merged

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

Description

feat(runtime): portable overflow-check wrapper + --no-overflow-check flag

Followup to f2eadf0. Two extensions to the integer overflow detection:

  • Portability: wrap __builtin_add_overflow / _sub_overflow / _mul_overflow behind sp_int_*_overflow_p helpers with a feature-test (__GNUC__ >= 5 || __has_builtin(...)). When the builtins aren't available, fall back to a portable unsigned- arithmetic implementation that mirrors mruby's mrb_int_*_overflow in include/mruby/numeric.h: add / sub detect signed overflow via the unsigned-wrap sign-bit XOR trick; mul checks bounds against MRB_INT_MAX / MRB_INT_MIN before multiplying. Both paths produce the same RangeError on overflow.

  • Opt-out: a new --no-overflow-check flag on the spinel wrapper passes -DSP_NO_OVERFLOW_CHECK to cc. With that define set, sp_int_add / sub / mul / neg expand to bare + - * -- silent wrap, no branch, matches spinel's pre-overflow-detection behaviour. Verified: optcarrot with the flag returns to baseline fps (~680) and still produces checksum 59662 (no real overflow ever happens in NES emulation, so the bare ops give the same result as the checked ops).

Promote-on-overflow mode (BIGINT.md option α) still deferred -- the user wants raise-by-default with opt-in promote via a future --bigint flag and via pattern detection of obviously- bigint code. Both are separate followups.

Performance: optcarrot fps drops to ~620 with the wrapper (vs ~634 from the direct-builtin version), about 8% from baseline. The extra ~2-3% likely from the helper function call layer that the macro form preserves around the builtin; could be tightened with __attribute__((always_inline)) on the helpers but matching mruby's style for now keeps the door open for the same portable build.

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

Details

Commit
aa11881b2533
Parent
d399c8278022

lib/sp_runtime.hmodifiedView file

@@ -104,30 +104,101 @@
104104 return r;
105105 }
106106 /* Overflow-checked integer arithmetic (BIGINT.md option β: raise on
107- overflow, keep locals at native mrb_int width). __builtin_*_overflow
108- compiles to a single ALU op + conditional branch on every supported
109- platform; the branch is well-predicted in hot loops so the cost is
110- close to one extra instruction per op.
107+ overflow, keep locals at native mrb_int width).
111108
112- Implemented as GCC statement-expression macros rather than static
113- inline functions: the inline variant produces wrong optcarrot
114- output even with __attribute__((always_inline)) (observed: checksum
115- diverges from 59662 to 4096, with no overflow ever firing). I
116- suspect a UB-assumption interaction between the optimizer and the
117- function-call-shaped expression that the macro form bypasses by
118- keeping every operand in its surrounding expression context. */
119-#define sp_int_add(a, b) ({ mrb_int _sp_a = (a), _sp_b = (b), _sp_r; \
120- if (__builtin_add_overflow(_sp_a, _sp_b, &_sp_r)) sp_raise_cls("RangeError", "integer overflow in +"); \
121- _sp_r; })
122-#define sp_int_sub(a, b) ({ mrb_int _sp_a = (a), _sp_b = (b), _sp_r; \
123- if (__builtin_sub_overflow(_sp_a, _sp_b, &_sp_r)) sp_raise_cls("RangeError", "integer overflow in -"); \
124- _sp_r; })
125-#define sp_int_mul(a, b) ({ mrb_int _sp_a = (a), _sp_b = (b), _sp_r; \
126- if (__builtin_mul_overflow(_sp_a, _sp_b, &_sp_r)) sp_raise_cls("RangeError", "integer overflow in *"); \
127- _sp_r; })
128-#define sp_int_neg(a) ({ mrb_int _sp_a = (a), _sp_r; \
129- if (__builtin_sub_overflow((mrb_int)0, _sp_a, &_sp_r)) sp_raise_cls("RangeError", "integer overflow in -@"); \
130- _sp_r; })
109+ Build modes (preprocessor toggles):
110+ default : overflow raises RangeError
111+ SP_NO_OVERFLOW_CHECK : bare `+ - *`, silent wrap (UB on signed
112+ overflow, but matches spinel's prior
113+ behaviour where the user asks for
114+ max speed and accepts the risk)
115+
116+ The actual overflow check is wrapped through
117+ `sp_int_*_overflow_p` helpers so non-gcc/clang compilers fall back
118+ to a portable unsigned-arithmetic implementation. The shape
119+ mirrors mruby's mrb_int_*_overflow in include/mruby/numeric.h.
120+
121+ The outer sp_int_add/sub/mul/neg are GCC statement-expression
122+ macros (`({ ... })`) rather than functions: an earlier inline-
123+ function variant produced wrong optcarrot output (checksum
124+ diverged 59662→4096) even with __attribute__((always_inline)),
125+ apparently from a UB-assumption interaction between the
126+ optimizer and the function-call-shaped expression that the
127+ macro form bypasses by keeping every operand in its surrounding
128+ expression context. The macros also let the compiler fold
129+ constant-operand cases at every call site. */
130+#ifndef __has_builtin
131+# define __has_builtin(x) 0
132+#endif
133+#if (defined(__GNUC__) && __GNUC__ >= 5) || \
134+ (__has_builtin(__builtin_add_overflow) && \
135+ __has_builtin(__builtin_sub_overflow) && \
136+ __has_builtin(__builtin_mul_overflow))
137+# define SP_HAVE_OVERFLOW_BUILTINS 1
138+#endif
139+
140+#ifdef SP_HAVE_OVERFLOW_BUILTINS
141+static inline mrb_bool sp_int_add_overflow_p(mrb_int a, mrb_int b, mrb_int *r) {
142+ return __builtin_add_overflow(a, b, r);
143+}
144+static inline mrb_bool sp_int_sub_overflow_p(mrb_int a, mrb_int b, mrb_int *r) {
145+ return __builtin_sub_overflow(a, b, r);
146+}
147+static inline mrb_bool sp_int_mul_overflow_p(mrb_int a, mrb_int b, mrb_int *r) {
148+ return __builtin_mul_overflow(a, b, r);
149+}
150+#else
151+/* Portable fallback. mrb_int is int64_t (see typedef above), so the
152+ add / sub fallbacks compute in mrb_uint (= uint64_t) -- unsigned
153+ integer overflow is well-defined wrap-around in C -- and detect
154+ signed overflow via the sign-bit XOR trick. Mul checks bounds
155+ before multiplying because the 128-bit intermediate isn't
156+ portable. */
157+#define SP_INT_OVERFLOW_MASK ((mrb_uint)1 << 63)
158+static inline mrb_bool sp_int_add_overflow_p(mrb_int a, mrb_int b, mrb_int *r) {
159+ mrb_uint x = (mrb_uint)a, y = (mrb_uint)b, z = x + y;
160+ *r = (mrb_int)z;
161+ return !!(((x ^ z) & (y ^ z)) & SP_INT_OVERFLOW_MASK);
162+}
163+static inline mrb_bool sp_int_sub_overflow_p(mrb_int a, mrb_int b, mrb_int *r) {
164+ mrb_uint x = (mrb_uint)a, y = (mrb_uint)b, z = x - y;
165+ *r = (mrb_int)z;
166+ return !!(((x ^ z) & (~y ^ z)) & SP_INT_OVERFLOW_MASK);
167+}
168+static inline mrb_bool sp_int_mul_overflow_p(mrb_int a, mrb_int b, mrb_int *r) {
169+ if (a > 0 && b > 0 && a > MRB_INT_MAX / b) { *r = a * b; return TRUE; }
170+ if (a < 0 && b > 0 && a < MRB_INT_MIN / b) { *r = a * b; return TRUE; }
171+ if (a > 0 && b < 0 && b < MRB_INT_MIN / a) { *r = a * b; return TRUE; }
172+ if (a < 0 && b < 0 && (a <= MRB_INT_MIN || b <= MRB_INT_MIN || -a > MRB_INT_MAX / -b)) {
173+ *r = a * b; return TRUE;
174+ }
175+ *r = a * b;
176+ return FALSE;
177+}
178+#undef SP_INT_OVERFLOW_MASK
179+#endif
180+
181+#ifdef SP_NO_OVERFLOW_CHECK
182+/* User opted out of overflow detection at compile time. Bare ops
183+ with no helper call, no branch, same risk as pre-#638 spinel. */
184+# define sp_int_add(a, b) ((a) + (b))
185+# define sp_int_sub(a, b) ((a) - (b))
186+# define sp_int_mul(a, b) ((a) * (b))
187+# define sp_int_neg(a) (-(a))
188+#else
189+# define sp_int_add(a, b) ({ mrb_int _sp_a = (a), _sp_b = (b), _sp_r; \
190+ if (sp_int_add_overflow_p(_sp_a, _sp_b, &_sp_r)) sp_raise_cls("RangeError", "integer overflow in +"); \
191+ _sp_r; })
192+# define sp_int_sub(a, b) ({ mrb_int _sp_a = (a), _sp_b = (b), _sp_r; \
193+ if (sp_int_sub_overflow_p(_sp_a, _sp_b, &_sp_r)) sp_raise_cls("RangeError", "integer overflow in -"); \
194+ _sp_r; })
195+# define sp_int_mul(a, b) ({ mrb_int _sp_a = (a), _sp_b = (b), _sp_r; \
196+ if (sp_int_mul_overflow_p(_sp_a, _sp_b, &_sp_r)) sp_raise_cls("RangeError", "integer overflow in *"); \
197+ _sp_r; })
198+# define sp_int_neg(a) ({ mrb_int _sp_a = (a), _sp_r; \
199+ if (sp_int_sub_overflow_p((mrb_int)0, _sp_a, &_sp_r)) sp_raise_cls("RangeError", "integer overflow in -@"); \
200+ _sp_r; })
201+#endif
131202
132203 static mrb_int sp_gcd(mrb_int a,mrb_int b){if(a<0)a=-a;if(b<0)b=-b;while(b){mrb_int t=b;b=a%b;a=t;}return a;}
133204 static mrb_int sp_lcm(mrb_int a,mrb_int b){if(a==0||b==0)return 0;mrb_int g=sp_gcd(a,b);if(a<0)a=-a;if(b<0)b=-b;return (a/g)*b;}