SIZEOF_BDIGITS is added.
(bary_mul_precheck): Reduce comparisons.
(bary_mul): Invoke bary_sq_fast or bary_mul1 if the bignum size is
small.
(bigfixize): Resize the argument bignum here.
(bignorm): Don't call bigtrunc after bigfixize.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42028 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Based on patch by Juanito Fatas [Fixes GH-360]
https://github.com/ruby/ruby/pull/360
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(nlz32): Use __builtin_clz or __builtin_clzl if possible.
(nlz64): Use __builtin_clzl or __builtin_clzll if possible.
(nlz128): Use __builtin_clzll if possible.
* configure.in: Check __builtin_clz, __builtin_clzl and
__builtin_clzll.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41976 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
__builtin_bswap64.
* internal.h (swap32): Use the configure result for the condition to
use __builtin_bswap32.
(swap64): Use the configure result for the condition to use
__builtin_bswap64.
* bignum.c (ones): Use the configure result for the condition to use
__builtin_popcountl.
(bary_unpack_internal): Use appropriate types for swap argument.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41963 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(bigsub_core): Removed.
(bigsub): Don't compare before subtraction. Just subtract and
get the two's complement if the subtraction causes a borrow.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41962 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
big_lshift and big_rshift are merged.
(big_shift2): New function.
(big_lshift): Use big_shift3.
(big_rshift): Ditto.
(check_shiftdown): Removed.
(rb_big_lshift): Use big_shift2 and big_shift3.
(rb_big_rshift): Ditto.
(big_lshift): Removed.
(big_rshift): Ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41942 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(bigrsh_bang): Ditto.
(bigmul1_toom3): Use bary_small_lshift and bary_small_rshift.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41935 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(mulfunc_t): New type.
(bary_mul_toom3_start): Renamed from bary_mul.
(bary_mul_karatsuba_start): Renamed from bary_mul.
(bary_mul_balance_with_mulfunc): Renamed from bary_mul_balance and
new argument, mulfunc, is added.
(rb_big_mul_balance): Invoke bary_mul_balance_with_mulfunc with
bary_mul_toom3_start.
(bary_mul_karatsuba): Invoke bary_mul_karatsuba_start instead of
bary_mul.
(bary_mul_precheck): Extracted from bary_mul.
(bary_mul_karatsuba_branch): Extracted from bary_mul.
(bary_mul_karatsuba_start): New function to call bary_mul_precheck
and bary_mul_karatsuba_branch.
(bary_mul_toom3_branch): Extracted from bary_mul.
(bary_mul_toom3_start): New function to call bary_mul_precheck and
bary_mul_toom3_branch.
(bary_mul): Just call bary_mul_toom3_start.
Arguments for work memory are removed.
(rb_cstr_to_inum): Follow the bary_mul change.
(bigmul0): Ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(bary_add_one): Replaced by the implementation of bary_plus_one.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41897 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* bignum.c (maxpow_in_bdigit_dbl, maxpow_in_bdigit): select by
preprocessor conditions to reduce dead code and suppress warnings.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41888 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(bary_sub): Returns a borrow flag. Use bary_subb.
(bary_subb): New function for actually calculating subtraction with
borrow.
(bary_sub_one): New function.
(bigadd_core): Use bary_add.
(bary_add): Returns a carry flag. Use bary_addc.
(bary_addc): New function for actually calculating addition with
carry.
(bary_add_one): New function.
(bary_muladd_1xN): Extracted from bary_mul_normal.
(bigmul1_normal): Removed.
(bary_mul_karatsuba): New function.
(bary_mul1): Invoke rb_thread_check_ints after bary_mul_normal.
(bary_mul): Remove most and least significant zeros before actual
multiplication. Use bary_sq_fast, bary_mul_balance,
bary_mul_karatsuba and bigmul1_toom3 as bigmul0.
(bigmul1_balance): Removed.
(bigmul1_karatsuba): Removed.
(bigsqr_fast): Removed.
(bary_sparse_p): Extracted from big_sparse_p.
(big_sparse_p): Removed.
(bigmul0): Use bary_mul.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41821 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
ensure not to access zds[2*xn].
(bigsqr_fast): Allocate the result bignum with 2*xn words.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41813 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(maxpow_in_bdigit): Ditto.
(U16): New macro.
(U32): Ditto.
(U64): Ditto.
(U128): Ditto.
(maxpow16_exp): New table.
(maxpow16_num): New table.
(maxpow32_exp): New table.
(maxpow32_num): New table.
(maxpow64_exp): New table.
(maxpow64_num): New table.
(maxpow128_exp): New table.
(maxpow128_num): New table.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41773 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(BDIGIT_DBL_MAX): New macro.
(maxpow_in_bdigit_dbl): New function.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41747 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(rb_cstr_to_inum): Use a better algorithm to compose the result
if input length is very long.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41726 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(nlz32): Ditto.
(nlz64): Ditto.
(nlz128): Ditto.
(nlz): Redefined using an above function.
(bitsize): New macro.
(rb_cstr_to_inum): Use bitsize instead of nlz.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41716 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(bary_plus_one): Extracted from bary_2comp.
(bary_2comp): Use bary_neg and bary_plus_one.
(big_extend_carry): Extracted from get2comp.
(get2comp): Use big_extend_carry.
(rb_integer_unpack): Use big_extend_carry.
(rb_big_neg): Use bary_neg.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41695 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(bigxor_int): Take xn and hibitsx arguments. Use twocomp2abs_bang.
(rb_big_xor): Use abs2twocomp and twocomp2abs_bang.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41692 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(bigor_int): Take xn and hibitsx arguments. Use twocomp2abs_bang.
(rb_big_or): Use abs2twocomp and twocomp2abs_bang.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(abs2twocomp): Take n_ret argument to return actual length.
(rb_big_and): Follow above change.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41681 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(abs2twocomp_bang): New function.
(abs2twocomp): New function.
(twocomp2abs_bang): New function.
(rb_big_and): Use abs2twocomp and twocomp2abs_bang.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41680 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(bigor_int): The allocated bignum should have enough size
to store long.
This fixes (bignum fits in a BDIGIT) | (fixnum bigger than BDIGIT)
on platforms which SIZEOF_BDIGITS < SIZEOF_LONG,
such as LP64 with 32bit BDIGIT (no int128).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41639 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(rb_big_aref): Call big2ulong with TRUE for "check" argument.
It should be non-effective.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41621 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
"-u" to suppress warning (C4146) by Visual Studio.
Reported by ko1 via IRC.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41616 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(BDIGIT_MSB): Defined using BIGRAD_HALF.
(bary_2comp): Apply BIGLO after possible over flow of BDIGIT.
(get2comp): Ditto.
(bary_unpack_internal): Use BDIGIT_MSB.
Apply BIGLO after possible over flow of BDIGIT.
(rb_integer_unpack): Use BDIGIT_MSB.
(calc_hbase): Use BDIGMAX.
(big2dbl): Use BDIGMAX.
Apply BIGLO after possible over flow of BDIGIT.
(rb_big_neg): Apply BIGLO after possible over flow of BDIGIT.
(biglsh_bang): Ditto.
(bigrsh_bang): Ditto.
(bary_divmod): Use BDIGIT_MSB.
(bigdivrem): Ditto.
(bigxor_int): Apply BIGLO after possible over flow of BDIGIT.
* marshal.c (shortlen): Use SIZEOF_BDIGITS instead of sizeof(BDIGIT).
* ext/openssl/ossl_bn.c (ossl_bn_initialize): Use SIZEOF_BDIGITS
instead of sizeof(BDIGIT).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41613 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* bignum.c (big2ulong): suppress shorten-64-to-32 warning. BDIGIT can
be bigger than long now.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41612 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
of x is bigger than BDIGIT_DBL.
(big2ulong): Use unsigned long to store the result.
(big2ull): Use unsigned LONG_LONG to store the result.
(bigand_int): Use long for num to avoid data loss.
(bigor_int): Ditto.
(bigxor_int): Ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41588 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(bary_pack): Use bytes_2comp.
(rb_quad_pack): Use rb_integer_pack.
(rb_quad_unpack): Use rb_integer_unpack.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41570 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(bary_unpack): Support INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION.
(rb_integer_unpack): Support INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41566 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION flag.
Fix byte order and word order handling in code specialized for
wordsize % SIZEOF_BDIGITS == 0.
* internal.h (INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION): Defined.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41565 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(RSHIFTX): Ditto.
(CLEAR_LOWBITS): Use LSHIFTX and RSHIFTX.
(FILL_LOWBITS): Use LSHIFTX.
Reported by ko1 via IRC.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41554 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(HOST_BIGENDIAN_P): New macro.
(ALIGNOF): New macro.
(CLEAR_LOWBITS): New macro.
(FILL_LOWBITS): New macro.
(swap_bdigit): New macro.
(bary_2comp): Returns an int.
* internal.h (swap16): Moved from pack.c
(swap32): Ditto.
(swap64): Ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41544 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(bary_unpack): Set the overflowed bit if an extra BDIGIT exists.
(rb_integer_unpack): Set the overflowed bit.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41494 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(bigfixize): Refine an ifdef condition.
(rb_absint_size): Use bdigit_roomof.
(rb_absint_singlebit_p): Ditto.
(rb_integer_pack): Ditto.
(integer_pack_fill_dd): Use BITSPERDIG.
(integer_unpack_push_bits): Use BITSPERDIG, BIGLO and BIGDN.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41446 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(DIGSPERLL): Don't define if BDIGIT is bigger than LONG_LONG
(rb_absint_size): Consider environments BDIGIT is bigger than long.
Use BIGLO and BIGDN.
(rb_absint_singlebit_p): Ditto.
(rb_integer_pack): Ditto.
(bigsub_int): Consider environments BDIGIT is bigger than long.
Use SIZEOF_BDIGITS instead of sizeof(BDIGIT).
(bigadd_int): Ditto.
(bigand_int): Ditto.
(bigor_int): Ditto.
(bigxor_int): Ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41428 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* include/ruby/defines.h (BDIGIT_DBL): Use uint128_t if it is available.
(BDIGIT): Use uint64_t if uint128_t is available.
(SIZEOF_BDIGITS): Defined for above case.
(BDIGIT_DBL_SIGNED): Ditto.
(PRI_BDIGIT_PREFIX): Ditto.
* include/ruby/ruby.h (PRI_64_PREFIX): Defined.
* bignum.c (rb_big_pow): Don't use BITSPERDIG for the condition which
rb_big_pow returns Float or Bignum.
[ruby-dev:47413] [Feature #8509]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41379 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(integer_unpack_num_bdigits): Extracted from
rb_integer_unpack_internal.
(bary_unpack_internal): Renamed from bary_unpack and support
INTEGER_PACK_2COMP.
(bary_unpack): New function to validate arguments and invoke
bary_unpack_internal.
(rb_integer_unpack_internal): Removed.
(rb_integer_unpack): Invoke bary_unpack_internal.
(rb_integer_unpack_2comp): Removed.
* internal.h (rb_integer_unpack_2comp): Removed.
* pack.c: Follow the above change.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41337 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(bary_zero_p): Extracted from bigzero_p.
(absint_numwords_generic): Use bary_zero_p and bary_add.
(bary_mul): Fix an argument for bary_mul_single.
(bary_divmod): Use size_t for arguments.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41328 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
(absint_numwords_generic): Use bary_mul.
(bary_mul_single): Extracted from bigmul1_single.
(bary_mul_normal): Extracted from bigmul1_normal.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41313 b2dd03c8-39d4-4d8f-98ff-823fe69b080e