diff options
author | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-03-08 09:15:18 +0000 |
---|---|---|
committer | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-03-08 09:15:18 +0000 |
commit | 0bbaa95c10324c4ea95ab7c145d26767b0634cdc (patch) | |
tree | 39c7e7af80089b88d8fd18224bf9ff3b58c068f5 /internal.h | |
parent | 61137b821824ddcd0382037cb82ef62421e47e12 (diff) | |
download | ruby-0bbaa95c10324c4ea95ab7c145d26767b0634cdc.tar.gz |
* intern.h (rb_divmod): assume compilers `/` and `%` comply C99
and reduce branching. If a compiler doesn't comply, add #ifdefs.
* intern.h (rb_div): added for Ruby's behavior.
* intern.h (rb_mod): added for Ruby's behavior.
* insns.def (opt_div): use rb_div.
* insns.def (opt_mod): use rb_mod.
* numeric.c (fixdivmod): removed.
* numeric.c (fix_divide): use rb_div.
* numeric.c (fix_mod): use rb_mod.
* numeric.c (fix_divmod): use rb_divmod.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54029 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'internal.h')
-rw-r--r-- | internal.h | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/internal.h b/internal.h index edf7f4f6c4..76746cc3b7 100644 --- a/internal.h +++ b/internal.h @@ -266,6 +266,47 @@ nlz_int128(uint128_t x) } #endif +/* + * This behaves different from C99 for negative arguments. + * Note that div may overflow fixnum. + */ +static inline void +rb_divmod(long x, long y, long *divp, long *modp) { + /* assume / and % comply C99. + * ldiv(3) won't be inlined by GCC and clang. + * I expect / and % are compiled as single idiv. + */ + long div = x / y; + long mod = x % y; + if (y > 0 ? mod < 0 : mod > 0) { + mod += y; + div -= 1; + } + if (divp) *divp = div; + if (modp) *modp = mod; +} + +/* div() for Ruby + * This behaves different from C99 for negative arguments. + * Note that div may overflow fixnum + */ +static inline long +rb_div(long x, long y) { + long div; + rb_divmod(x, y, &div, NULL); + return div; +} + +/* mod() for Ruby + * This behaves different from C99 for negative arguments. + */ +static inline long +rb_mod(long x, long y) { + long mod; + rb_divmod(x, y, NULL, &mod); + return mod; +} + #if defined(HAVE_UINT128_T) # define bit_length(x) \ (sizeof(x) <= SIZEOF_INT ? SIZEOF_INT * CHAR_BIT - nlz_int((unsigned int)(x)) : \ |