954 Commits
Author SHA1 Message Date
Burdette Lamar 57509f1d21 [DOC] Tweaks for Integer#bit_count (#19398) 2026-10-19 08:30:42 +09:00
Hiroshi SHIBATAandClaude Opus 5.5 b377234135 Add rb_len2long and rb_out_of_long
Like `rb_len2int`, `rb_len2long` narrows an `rb_len_t` with a range check, for extensions that keep a length in `long` and for intentional truncations where `rb_len_t` is wider than `long`. `rb_out_of_int` cannot be reused since its message names 'int'.

https://bugs.ruby-lang.org/issues/22400

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 08:34:50 +09:00
Hiroshi SHIBATAandClaude Opus 5 593b42ec70 Use rb_len_t for string lengths in numeric conversions
rb_big_pack() and rb_big_unpack() keep `long`, because num_longs counts the words of an `unsigned long` buffer rather than a String or Array length.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-10-09 14:00:15 +09:00
Hiroshi SHIBATAandClaude Opus 5.5 c901944ccb Remove Cray UNICOS/mp support
UNICOS/mp ran only on the Cray X1, and Cray moved to Linux based systems after it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 09:00:26 +09:00
Shintaro Kakutani 34d21828d3 Fix Integer#/ documentation for floor semantics 2026-08-11 08:36:43 +09:00
Nobuyoshi Nakada b51ae934f7 [Feature #22215] Remove vm_core dependency from gc header
Keep `internal/gc.h` usable without VM-private definitions so bundled
extensions do not inherit `vm_core.h` dependencies.
2026-08-07 09:59:15 +09:00
Ajay Krishnan b99b602f8c [DOC] Fix Float#ceil granularity definition 2026-07-26 11:32:57 -04:00
John Hawthorn 23c056c83c [Feature #20163] Add Integer#bit_count 2026-07-10 11:55:58 -07:00
Peter Zhu 96311d13c2 [DOC] Add missing half kwags to Numeric#round 2026-07-04 19:22:37 -04:00
Nobuyoshi Nakada 09dacb9c3d [Bug #22079] Fix Float#ceil and Float#floor edge cases 2026-05-23 16:12:01 +09:00
Nobuyoshi Nakada 97ec327efc Refine rb_flo_round_by_rational
Use already broken down arguments.
2026-05-23 16:12:01 +09:00
Nobuyoshi Nakada 4e8dbfdc1b Move the test for [Bug #14635]
Float-specific test should be in test_float.rb.
2026-05-23 16:12:01 +09:00
Chris Hasińskiandtomoya ishida 4a0072d5f2 Speed up Integer#to_s with a two digit lookup table (#16719)
* numeric: emit two decimal digits per iteration in rb_fix2str

Replace the digit-at-a-time loop in rb_fix2str with the standard
itoa 2-digit lookup table for base 10.  Each iteration now
writes two digits using a single (u % 100, u / 100) pair, so the
number of loop iterations is halved for multi-digit integers.
The classic per-digit loop is kept for non-base-10 conversion.

Benchmark (Apple M-series, 5M-10M ops, best of 3 runs):

  case            base       patch      delta
  ---------       -----      -----      -----
  1-digit   (5)   64 ns/op   64 ns/op    -0%
  2-digit   (42)  64 ns/op   65 ns/op    +2%  (noise)
  3-digit   (400) 66 ns/op   64 ns/op    -3%
  5-digit   (12345)          69 ns/op   67 ns/op    -3%
  10-digit  (1234567890)     77 ns/op   67 ns/op   -13%
  19-digit  (2^62-1)        111 ns/op   75 ns/op   -33%

The crossover is at ~3 digits: below that the constant setup
dominates and the benefit is within noise, above that the halved
iteration count shows up linearly.  Typical Rails payloads mix
short IDs (1-5 digits) and longer values (timestamps, nanos,
large counts), so the win is workload-dependent but strictly
non-negative for real code.

Correctness: 100k random fuzz across the full fixnum range plus
targeted edges (0, ±1, ±99, ±100, 2^30-1, 2^62-1, etc.) all pass.
make test-all shows 34694 tests, 7325860 assertions, 0 new
failures (same pre-existing TestArgf#test_puts flake as on
master) — test_integer.rb alone runs 38 tests / 421628 assertions
of which Integer#to_s exercises the bulk, all pass.

The 200-byte lookup table sits in .rodata and fits in a single
cache line of its own (3 lines for the whole table).  No change
to public API, no change to bignum conversion, no change to
non-base-10 conversion paths.

* bignum: emit two decimal digits per iteration in big2str_2bdigits

Extend the 2-digit lookup-table itoa optimisation from rb_fix2str to
the inner conversion loop used by Bignum#to_s.  big2str_2bdigits has
two code paths — a leading-chunk path that emits variable-length
digits, and a recursive-chunk path that emits a fixed-width zero-
padded block — and both gain from the halved division count.  The
classic per-digit loop is preserved for non-base-10 conversion.

Moves the ruby_decimal_digit_pairs table from a file-static in
numeric.c to bignum.c next to ruby_digitmap, and exposes it through
internal/bignum.h so both files share the same 200-byte .rodata
instance.

Benchmark (Apple M-series, best of 3 runs, measures bignum-only
speedup against the preceding fixnum commit):

  case            base      patch     delta
  ---------       -----     -----     -----
  big_20dig   10^19+...  146 ns/op 124 ns/op  -15%
  big_40dig   10^39+...  174 ns/op 152 ns/op  -13%
  big_100dig  10^99+42   236 ns/op 213 ns/op  -10%
  big_500dig  10^499+7  1119 ns/op 1086 ns/op  -3%
  big_1000dig 10^999    3490 ns/op 3459 ns/op  -1%
  fix_19dig   2^62-1      76 ns/op   76 ns/op   0% (unchanged path)

Wins concentrate in the 20-100 digit range where big2str_2bdigits
is the dominant cost.  Above ~500 digits the Karatsuba divmod
recursion dominates and the digit-emission saving shrinks to the
noise floor.  The 20-100 range is what actual Ruby code exercises
(financial high-precision sums, nanosecond timestamps, large
counters); crypto-size (1000+ digit) bignums are rare in to_s paths.

Correctness: 100k random fixnum fuzz unchanged, 500 random bignum
fuzz up to 2^256 with cross-check against sprintf("%d"), bases
2/8/16/36 round-trip, plus edge cases (0, just-above-fixnum, ±2^100,
20-digit strings near the fixnum boundary).  test/ruby/test_integer.rb
stays at 38 tests / 421628 assertions / 0 failures, test_bignum.rb
passes 74 / 607 / 0 failures, full make test-all reports 34694
tests / 0 new failures (same TestArgf#test_puts pre-existing flake
as master).

* benchmark: add int_to_s yaml for Integer#to_s

Reproducible benchmark for the two preceding commits.  Covers:

- 1/2/3/5/10/19-digit positive fixnums (spans the break-even point
  and the two large-number wins at the top)
- A negative fixnum (exercises the minus-sign prepend path)
- 20/40/100-digit bignums (spans the big2str_2bdigits win range)
- Two string-interpolation scenarios, so reviewers can see how much
  of the Integer#to_s speedup reaches real code that allocates the
  result string too

Intended to be consumed by benchmark-driver against master vs
int-to-s-twodigit for A/B comparison.  Matches the numbers in the
commit messages of 5bfb7e02a2 and c5df6de835.

---------

Co-authored-by: tomoya ishida <tomoyapenguin@gmail.com>
2026-05-09 02:10:12 +09:00
Jean Boussier e4667e5109 Refactor NEWOB_OF for the common protected use case
Almost every objects are now WB protected, with just a few
exceptions, hence we can provide a much simpler interface.

It's also much easier to find the remaining unprotected objects.
2026-04-28 11:11:29 +09:00
Jean Boussier c202bc08e1 Simplify NEWOBJ_OF and introduce EC_NEWOBJ_OF
We only very rarely have the execution context available, so
it's preferable to provide a simpler default macro.
2026-04-28 11:11:29 +09:00
Jean Boussier 92cba2ef72 Get rid of RGENGC_WB_PROTECTED_FLOAT
It has been the default for a very long time and there is no good reason to
disable it.
2026-04-28 09:39:07 +09:00
Chris Hasiński 03f1b569b1 ZJIT: Keep flo_to_i static, add rb_flo_to_i public wrapper
The previous commit exposed flo_to_i as a non-static global, which
tripped tool/leaked-globals because it lacks the rb_ prefix.

Keep flo_to_i static and add a new public wrapper rb_flo_to_i in
numeric.c that delegates to it. ZJIT now calls rb_flo_to_i directly
from Rust (no zjit.c wrapper needed).
2026-04-15 15:01:02 -07:00
Chris Hasiński aaad9e0f06 ZJIT: Move flo_to_i helper to zjit.c and use original flo_to_i
Address review feedback:
1. Move the helper from jit.c (shared YJIT/ZJIT glue) to zjit.c since
   it is only used by ZJIT.
2. Instead of duplicating the truncation logic, export flo_to_i from
   numeric.c and call it from the ZJIT helper, keeping the
   implementation centralized.

The new rb_zjit_flo_to_i wrapper in zjit.c delegates directly to
flo_to_i, so there is no deviation from the Float#to_i semantics.
2026-04-15 15:01:02 -07:00
Nobuyoshi Nakada 1b8c6c1e33 Suppress a sign-compare warning
This cast is safe because `rb_absint_size` returns `nlz` within the
range `0...CHAR_BIT`.
2026-03-09 16:14:02 +09:00
Ernie Miller 9aca729140 Improve comparison error message.
In certain cases, things like Array#sort can result in a confusing error
message. For instance where a and b are characters in a string,
`"string"`:

```ruby
array.sort { |a, b| string.index(a) <=> string.index(b) }
```

If one of the index calls returns nil, we will get "comparison of String
with String failed", which is somewhat unhelpful, since it's easy to be
confused, given that what is really being compared is a Fixnum or
NilClass (the cause of the error). Yes, as far as Array#sort is
concerned, the two characters are the things being sorted, but it's
useful to call attention to the return value of the comparison in this
case.

This patch adds a "reason" argument to rb_cmperr, which will provide an
error message of "comparison of String with String failed: comparator
returned nil" in the case above, or, in the case of:

```ruby
1.upto('10').to_a
```

it will provide the message: "comparison of Fixnum with String failed:
coercion was not possible"
2026-03-06 14:02:00 +09:00
Nobuyoshi Nakada 74a4e2e66d [Bug #21851] Divide as double if accurate 2026-02-11 22:09:26 +09:00
Jean BoussierandBenoit Daloze d066b9e01c Refactor type error to be more consistent
[Bug #21864]

Co-Authored-By:  Benoit Daloze <eregontp@gmail.com>
2026-02-07 10:06:36 +01:00
Peter Zhu 491e38902c [DOC] Fix links to What's Here
The RDoc link format has changed so these are all broken links.
2026-01-26 17:18:19 -05:00
Burdette Lamar 3d242a82a3 [DOC] Harmonize #> methods 2026-01-09 17:36:26 -05:00
Burdette Lamar 58fb95af36 [DOC] Harmonize #== methods (#15805) 2026-01-06 17:13:15 -05:00
BurdetteLamar a25f468917 [DOC] Harmonize #>= methods 2026-01-05 18:23:55 -05:00
BurdetteLamar b7d4d7c911 [DOC] Harmonize several <=> methods 2025-12-19 21:36:46 -05:00
BurdetteLamar d540903ee7 [DOC] Harmonize <= methods 2025-12-19 17:09:10 -05:00
Burdette Lamar b14f2f0116 [DOC] Harmonize lt methods 2025-12-18 19:21:00 -05:00
BurdetteLamar 2d01406644 [DOC] Harmonize rb_div methods 2025-12-16 17:47:08 -05:00
BurdetteLamar cfd41cbf03 [DOC] Harmonize #-@ methods 2025-12-15 17:28:35 -05:00
BurdetteLamar acbf55f4e6 [DOC] Harmonize #- methods 2025-12-15 17:27:54 -05:00
BurdetteLamar 7fbf321d23 [DOC] Harmonize #** methods 2025-12-15 17:27:24 -05:00
Peter Zhu 3e5c4ebe4b [DOC] Fix backticks in Numeric#ceil 2025-12-14 13:37:36 -05:00
Peter Zhu cd27337d0b [DOC] Fix backticks in Numeric#floor 2025-12-14 13:37:36 -05:00
Peter Zhu 55ea3ec00f Fix strict aliasing warning in rb_int128_to_numeric
If we don't have uint128, then rb_int128_to_numeric emits a strict
aliasing warning:

    numeric.c:3641:39: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
    3641 |         return rb_uint128_to_numeric(*(rb_uint128_t*)&n);
         |                                       ^~~~~~~~~~~~~~~~~
2025-12-08 15:01:05 -08:00
Samuel Williams a7dc53b91c Add support for u128, U128, s128 and S128 integers to IO::Buffer. (#15399) 2025-12-06 15:55:32 +13:00
Burdette Lamar 6e723bee45 [DOC] About Float Imprecision (#15293) 2025-12-02 21:41:21 -05:00
Burdette Lamar b2f110651c [DOC] Harmonize #+ methods 2025-12-02 21:39:25 -05:00
BurdetteLamar 688350dacc [DOC] Avoid term 'derived'; use 'subclass' 2025-11-28 17:24:13 -08:00
BurdetteLamar 7789aaca37 [DOC] Tweaks for Integer#** 2025-11-21 14:16:33 -08:00
BurdetteLamar b47b37c12c [DOC] Harmonize #* methods 2025-11-21 14:11:54 -08:00
BurdetteLamar 826e91a7e2 [DOC] Harmonize mod methods 2025-11-20 14:06:41 -08:00
Nobuyoshi Nakada cdb9893c55 Win32: Drop support for older than MSVC 8.0/_MSC_VER 1400
Visual C++ 2005 (8.0):
- _MSC_VER: 1400
- MSVCRT_VERSION: 80
2025-11-19 11:03:42 +09:00
tomoya ishida f4f728b319 [Bug #21680] Fix (base**power_of_two).digits(base) bug (#15144)
Fix wrong condition in base multiplying loop.
2025-11-12 13:58:38 +09:00
Nobuyoshi Nakada 61df125325 [DOC] Markup code in Float::MIN document 2025-09-16 00:13:37 +09:00
tomoya ishida a66e4f2154 Improve performance of bignum[beg, len] (#14007)
Implement rb_big_aref2.
Taking a small slice from large bignum was slow in rb_int_aref2.
2025-07-29 16:34:13 +00:00
Yusuke Endoh 3a7b9ca93b Fix Integer.sqrt to never exceed actual value
`Integer.sqrt` uses `sqrt(3)` from libm for small values.
This method must return a value less than or equal to the actual integer
square root, but libm's sqrt does not always guarantee that.

This change corrects that by decrementing the result if necessary.

Fixes [Bug #21217]
2025-04-07 11:08:10 +09:00
Nobuyoshi Nakada 9394d2084c [DOC] Fix indentation
Not to make preceding paragraphs a code block.
2025-02-27 13:00:34 +09:00
Nobuyoshi Nakada e433e6515e [DOC] Exclude 'Class' and 'Module' from RDoc's autolinking 2025-01-02 12:36:06 +09:00