Commit b30cddc
authored
[integer] Implement Burnikel-Ziegler fast recursive division algorithm for
This PR implements the Burnikel-Ziegler fast recursive division
algorithm which significantly improves the performance of large-number
divisions (particularly when there are thousands of words). It achieves
a realized time complexity of O(n^1.585) (3x time when digits are
doubled) instead of the O(n^2) (4x time when digits are doubled).
## Major Division Algorithm Improvements
### 1. **Burnikel-Ziegler Algorithm Implementation**
- **New fast recursive division algorithm** for large numbers
- Added `floor_divide_burnikel_ziegler()` function with cutoff at 64
words
- Implemented supporting functions:
- `floor_divide_two_by_one()`
- `floor_divide_three_by_two()`
- `floor_divide_three_by_two_uint32()`
- `floor_divide_four_by_two_uint32()`
### 2. **Division Strategy Refactoring**
- **Renamed** `floor_divide_general()` → `floor_divide_school()`
- **Smart algorithm selection** in `floor_divide()`:
- Small numbers (≤64 words): Use schoolbook division
- Large numbers (>64 words): Use Burnikel-Ziegler algorithm
- **Extracted normalization logic** into
`calculate_normalization_factor()` helper function
## Benchmark and Testing Enhancements
### 3. **Extended Test Coverage**
- **Increased test range** from 2^16 to 2^18 words (262,144 words)
- **Added validation** to compare Mojo vs Python results
- **Reduced iterations** for large number tests (100 → 10)
- **Added new test cases** for extremely large divisions
### 4. **Improved Benchmarking**
- **Extended Python string limit** from 1M to 10M digits
- **Enhanced logging** with result validation
- **Better error handling** for mismatched results
## Code Quality Improvements
### 5. **Constructor Safety**
- **Fixed BigUInt constructor** to handle empty word lists
- **Added validation** to prevent zero-length word arrays
### 6. **Build Process**
- **Improved formatting command** to target specific directories
- **Better project organization**
## Performance Impact
The main achievement is **dramatically improved performance for large
number division**:
- **O(n²) → O(n^1.585)** complexity reduction using Burnikel-Ziegler
- **Automatic algorithm selection** based on input size
- **Maintains compatibility** with existing API
This is a significant algorithmic improvement that should provide
substantial speedups for large number operations while maintaining
correctness for all input sizes.BigUInt (#103)1 parent d9bb737 commit b30cddc
5 files changed
Lines changed: 456 additions & 64 deletions
File tree
- benches/biguint
- src/decimojo/biguint
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
157 | 157 | | |
158 | 158 | | |
159 | 159 | | |
160 | | - | |
| 160 | + | |
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
| |||
171 | 171 | | |
172 | 172 | | |
173 | 173 | | |
| 174 | + | |
| 175 | + | |
174 | 176 | | |
175 | 177 | | |
176 | 178 | | |
| |||
183 | 185 | | |
184 | 186 | | |
185 | 187 | | |
186 | | - | |
| 188 | + | |
187 | 189 | | |
188 | 190 | | |
189 | 191 | | |
| |||
206 | 208 | | |
207 | 209 | | |
208 | 210 | | |
209 | | - | |
| 211 | + | |
210 | 212 | | |
211 | 213 | | |
212 | 214 | | |
| |||
274 | 276 | | |
275 | 277 | | |
276 | 278 | | |
277 | | - | |
| 279 | + | |
278 | 280 | | |
279 | 281 | | |
280 | 282 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
91 | 100 | | |
92 | 101 | | |
93 | 102 | | |
| |||
155 | 164 | | |
156 | 165 | | |
157 | 166 | | |
| 167 | + | |
158 | 168 | | |
159 | 169 | | |
160 | 170 | | |
| |||
492 | 502 | | |
493 | 503 | | |
494 | 504 | | |
495 | | - | |
| 505 | + | |
496 | 506 | | |
497 | 507 | | |
498 | | - | |
| 508 | + | |
499 | 509 | | |
500 | 510 | | |
501 | 511 | | |
502 | 512 | | |
503 | 513 | | |
504 | 514 | | |
505 | | - | |
| 515 | + | |
506 | 516 | | |
507 | 517 | | |
508 | | - | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
509 | 539 | | |
510 | 540 | | |
511 | 541 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| |||
0 commit comments