Commit f5d42991
f5d42991c5b4Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified
Description
Merge pull request #175 from Halvanhelv/feat/integer-powmod
feat(codegen,runtime): add two-argument Integer#pow(exp, mod)
Conflicts:
spinel_codegen.rb
Details
- Commit
- f5d42991c5b4
- Parent
- 4b722f5789e0
- Merge parent
- 73596480aa87
lib/sp_runtime.hmodifiedView file
@@ -77,6 +77,7 @@
7777
7878
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;}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;}80+
static mrb_int sp_powmod(mrb_int base,mrb_int exp,mrb_int mod){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;}8081
static mrb_int sp_int_clamp(mrb_int v,mrb_int lo,mrb_int hi){return v<lo?lo:v>hi?hi:v;}8182
static inline char *sp_str_alloc_raw(size_t total_with_null); /* fwd decl */8283
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;}spinel_codegen.rbmodifiedView file
Loading diff…
test/integer_powmod.rbaddedView file
Loading diff…