Commit c0068840
c00688401d3aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified
Description
Make mrb_int pointer-width (intptr_t) for 32-bit embedded targets
mrb_int was hardcoded int64_t. Switch it to intptr_t, decided at compile
time via INTPTR_MAX: int64_t on 64-bit hosts (PCs -- zero behavior change)
and int32_t on 32-bit embedded targets, where it gives native-word
arithmetic, half the memory for every integer / array element / hash slot,
and a pointer-width sp_RbVal union (the i and p members now match, no
8-vs-4 split). Pointers already travel through .v.p, never .v.i, so
narrowing .v.i to pointer width is safe.
Two width-sensitive constants follow suit:
- SP_INT_NIL: INT64_MIN -> INTPTR_MIN. INT64_MIN cast to int32 is 0, which would make the int? nil sentinel collide with the integer 0 -- a silent catastrophe on 32-bit. INTPTR_MIN is INT64_MIN on 64-bit (unchanged) and INT32_MIN on 32-bit (a representable Integer there, so the sentinel can collide with -2147483648, an accepted 32-bit limitation).
- sp_core.c's Integer#round/ceil/floor 10^p overflow guards: the p>=19 cap and INT64_MAX/MIN clamps become SP_INT_POW10_LIMIT (19 on 64-bit, 10 on 32-bit) and INTPTR_MAX/MIN. sp_core.h/.c helper ABI moves int64_t -> intptr_t to match mrb_int.
Verified both paths: 64-bit gates unchanged (679 tests, optcarrot 59662, bootstrap fixpoint -- intptr==int64 makes it a no-op); and a -m32 build runs correctly with mrb_int=int32 (arrays/strings/hash/Time/interpolation), int32 overflow raises RangeError as it should (1000000*1000000), and SP_INT_NIL is INT32_MIN (not 0).
sp_Time.tv_sec / bigint stay int64_t deliberately (they hold values wider than a 32-bit Integer). The narrowing of those foreign 64-bit values to a 32-bit Integer at the Ruby boundary (Time#to_i, Integer(huge_str)) is a follow-up: promote-mode keeps the value (bigint), otherwise RangeError, never a silent truncate.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- c00688401d3a
- Parent
- 0e85b2d10755
lib/sp_core.cmodifiedView file
#include <ctype.h>#include <errno.h>typedef int64_t mrb_int;/* Must match sp_types.h: mrb_int is pointer-width (int64 on 64-bit, int32 on 32-bit) so this TU's helper ABI agrees with the generated TU. */typedef intptr_t mrb_int;typedef double mrb_float;/* 10^p fits mrb_int only up to this exponent (p>= it collapses to 0): 10^19 > INT64_MAX, 10^10 > INT32_MAX. */#if INTPTR_MAX == INT32_MAX#define SP_INT_POW10_LIMIT 10#else#define SP_INT_POW10_LIMIT 19#endif/* Defined in the generated translation unit (sp_runtime.h); referenced here and resolved at link time. */ only for p<=18; p>=19 collapses to 0. Round-up multiply is overflow- guarded and falls back to the truncated value. */mrb_int sp_ipow10(mrb_int p){mrb_int f=1;mrb_int i=0;while(i<p){f*=10;i++;}return f;}mrb_int sp_int_round(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int p=-nd;if(p>=19)return 0;mrb_int f=sp_ipow10(p);mrb_int q=v/f,r=v%f,half=f/2;if(v>=0){if(r>=half&&q<INT64_MAX/f)return(q+1)*f;return q*f;}if(-r>=half&&q>INT64_MIN/f)return(q-1)*f;return q*f;}mrb_int sp_int_ceil(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int p=-nd;if(p>=19)return 0;mrb_int f=sp_ipow10(p);mrb_int q=v/f,r=v%f;if(r!=0&&v>0&&q<INT64_MAX/f)return(q+1)*f;return q*f;}mrb_int sp_int_floor(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int p=-nd;if(p>=19)return 0;mrb_int f=sp_ipow10(p);mrb_int q=v/f,r=v%f;if(r!=0&&v<0&&q>INT64_MIN/f)return(q-1)*f;return q*f;}mrb_int sp_int_truncate(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int p=-nd;if(p>=19)return 0;mrb_int f=sp_ipow10(p);return(v/f)*f;}mrb_int sp_int_round(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int p=-nd;if(p>=SP_INT_POW10_LIMIT)return 0;mrb_int f=sp_ipow10(p);mrb_int q=v/f,r=v%f,half=f/2;if(v>=0){if(r>=half&&q<INTPTR_MAX/f)return(q+1)*f;return q*f;}if(-r>=half&&q>INTPTR_MIN/f)return(q-1)*f;return q*f;}mrb_int sp_int_ceil(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int p=-nd;if(p>=SP_INT_POW10_LIMIT)return 0;mrb_int f=sp_ipow10(p);mrb_int q=v/f,r=v%f;if(r!=0&&v>0&&q<INTPTR_MAX/f)return(q+1)*f;return q*f;}mrb_int sp_int_floor(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int p=-nd;if(p>=SP_INT_POW10_LIMIT)return 0;mrb_int f=sp_ipow10(p);mrb_int q=v/f,r=v%f;if(r!=0&&v<0&&q>INTPTR_MIN/f)return(q-1)*f;return q*f;}mrb_int sp_int_truncate(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int p=-nd;if(p>=SP_INT_POW10_LIMIT)return 0;mrb_int f=sp_ipow10(p);return(v/f)*f;}/* String#oct: prefix auto-detection (0x=hex, 0b=bin, 0o/0=oct, else base-8). Matches CRuby. */mrb_int sp_str_oct(const char*s){if(!s)return 0;const char*p=s;while(*p==' '||*p=='\t')p++;if(p[0]=='0'){if(p[1]=='x'||p[1]=='X')return(mrb_int)strtoll(p,NULL,16);if(p[1]=='b'||p[1]=='B')return(mrb_int)strtoll(p+2,NULL,2);if(p[1]=='o'||p[1]=='O')return(mrb_int)strtoll(p+2,NULL,8);return(mrb_int)strtoll(p,NULL,8);}return(mrb_int)strtoll(p,NULL,8);}lib/sp_core.hmodifiedView file
Loading diff…
lib/sp_types.hmodifiedView file
Loading diff…