Commit 0b2ceffd

0b2ceffdf060Merged

Islam Gagiev <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

feat(codegen,runtime): add Integer#ceildiv

Ceiling division (Ruby 3.2+). Returns the quotient rounded toward positive infinity. Uses sp_ceildiv runtime function to handle negative operands correctly.

  • lib/sp_runtime.h: add sp_ceildiv()
  • spinel_codegen.rb: infer_type + codegen
  • test/integer_ceildiv.rb: 10 cases covering exact, remainder, zero, negative, and large values

Details

Commit
0b2ceffdf060
Parent
84d241a676e0

lib/sp_runtime.hmodifiedView file

@@ -79,6 +79,7 @@
7979 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;}
8080 /* CRuby raises ZeroDivisionError on mod==0; we return 0 to avoid SIGFPE rather than crashing. */
8181 static mrb_int sp_powmod(mrb_int base,mrb_int exp,mrb_int mod){if(mod==0)return 0;mrb_int r=1;mrb_int m=mod<0?-mod:mod;if(m==1){r=0;}else{base=base%m;if(base<0)base+=m;while(exp>0){if(exp%2==1)r=r*base%m;exp=exp/2;base=base*base%m;}}if(mod<0&&r>0)r-=m;return r;}
82+static mrb_int sp_ceildiv(mrb_int a,mrb_int b){if(b==0)return 0;if(b==-1)return -a;mrb_int q=a/b;if(a%b!=0&&((a^b)>=0))q++;return q;}
8283 static mrb_int sp_int_clamp(mrb_int v,mrb_int lo,mrb_int hi){return v<lo?lo:v>hi?hi:v;}
8384 static inline char *sp_str_alloc_raw(size_t total_with_null); /* fwd decl */
8485 static const char*sp_int_chr(mrb_int n){char*s=sp_str_alloc_raw(2);s[0]=(char)n;s[1]=0;return s;}