Yukihiro "Matz" Matsumoto 12d77d447b mruby-bigint: optimize division with single-limb divisor fast path
Implement comprehensive single-limb division optimization providing
significant performance improvements for the common case of dividing
by small numbers.

Technical implementation:
- Added mpz_div_limb() function with three optimization strategies:
  * Power-of-2 divisors: use bit shifts (q = x >> log₂(d), r = x & (d-1))
  * Single-limb to single-limb: direct hardware division
  * Multi-limb to single-limb: optimized digit-by-digit algorithm
- Integrated fast path in udiv() for yy->sz == 1 condition
- Manual bit-shift implementation to avoid function dependencies
- Proper edge case handling (zero dividend, division by zero)

Performance improvements:
- Single-limb division: 1,156K ops/sec (3.4x vs multi-limb)
- Multi->single-limb: 457K ops/sec (1.3x vs multi-limb)
- Power-of-2 division: 437K ops/sec (1.3x vs multi-limb)
- Mixed small divisions: 662K ops/sec (1.9x vs multi-limb)

Algorithm benefits:
Power-of-2 detection using (d & (d-1)) == 0 enables ultra-fast bit
operations. Multi-limb algorithm processes from MSB to LSB using
double-limb arithmetic to prevent overflow, avoiding expensive
normalization and trial division phases of general algorithm.

Applications:
Optimizes common operations like base conversion, modular arithmetic
with small moduli, and mathematical computations involving division
by constants. Particularly beneficial for embedded systems where
division by small integers is frequent.

Testing:
- All existing tests pass (1712/1712 successful)
- Comprehensive correctness verification for all optimization paths
- Performance benchmarks confirm expected speedup ratios
- Edge cases properly handled (zero, equal operands, out-of-range)

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:52:46 +09:00
2025-07-25 16:10:03 +09:00
2024-04-21 22:46:19 +10:00
2024-03-12 08:41:33 +10:00
2024-11-20 14:51:11 +10:00
2024-11-16 21:21:51 +09:00
2022-12-30 14:05:16 +10:00
2024-11-16 21:21:51 +09:00
2025-07-03 06:52:32 +09:00
2025-05-14 02:01:02 +10:00
2024-11-17 14:43:21 +03:30
2021-06-17 10:26:38 +10:00
2025-05-14 02:01:02 +10:00

The mruby programming language

mruby

GitHub Super-Linter

What is mruby

mruby is the lightweight implementation of the Ruby language complying to (part of) the ISO standard with more recent features provided by Ruby 3.x. Also, its syntax is Ruby 3.x compatible except for pattern matching.

You can link and embed mruby within your application. The "mruby" interpreter program and the interactive "mirb" shell are provided as examples. You can also compile Ruby programs into compiled byte code using the "mrbc" compiler. All these tools are located in the "bin" directory. "mrbc" can also generate compiled byte code in a C source file. See the "mrbtest" program under the "test" directory for an example.

This achievement was sponsored by the Regional Innovation Creation R&D Programs of the Ministry of Economy, Trade and Industry of Japan.

How to get mruby

To get mruby, you can download the stable version 3.4.0 from the official mruby GitHub repository or clone the trunk of the mruby source tree with the "git clone" command. You can also install and compile mruby using ruby-install, ruby-build or rvm.

The latest development version of mruby can be downloaded via the following URL: https://github.com/mruby/mruby/zipball/master

The trunk of the mruby source tree can be checked out with the following command:

$ git clone https://github.com/mruby/mruby.git

mruby homepage

The URL of the mruby homepage is: https://mruby.org.

Mailing list

We don't have a mailing list, but you can use GitHub issues.

How to compile, test, and install (mruby and gems)

For the simplest case, type

rake all test

See the compile.md file for the detail.

Building documentation

There are two sets of documentation in mruby: the mruby API (generated by YARD) and C API (Doxygen and Graphviz)

To build both of them, simply go

rake doc

You can also view them in your browser

rake view_api
rake view_capi

How to customize mruby (mrbgems)

mruby contains a package manager called "mrbgems" that you can use to create extensions in C and/or Ruby. For a guide on how to use mrbgems, consult the mrbgems.md file, and for example code, refer to the examples/mrbgems/ folder.

Index of Document

License

mruby is released under the MIT License.

Note for License

mruby has chosen a MIT License due to its permissive license allowing developers to target various environments such as embedded systems. However, the license requires the display of the copyright notice and license information in manuals for instance. Doing so for big projects can be complicated or troublesome. This is why mruby has decided to display "mruby developers" as the copyright name to make it simple conventionally. In the future, mruby might ask you to distribute your new code (that you will commit,) under the MIT License as a member of "mruby developers" but contributors will keep their copyright. (We did not intend for contributors to transfer or waive their copyrights, actual copyright holder name (contributors) will be listed in the AUTHORS file.)

Please ask us if you want to distribute your code under another license.

How to Contribute

To contribute to mruby, please refer to the contribution guidelines and send a pull request to the mruby GitHub repository. By contributing, you grant non-exclusive rights to your code under the MIT License.

S
Description
Automated archival mirror of github.com/mruby/mruby
Readme MIT 44 MiB
Languages
C 69.9%
Ruby 23.9%
Yacc 4.7%
C++ 1.4%