Commit 58768ab
committed
Merge pull request #1906 from feature/bigint
256-bit big integer support
This PR introduces support for big integers in AtomVM, allowing arithmetic and
bitwise operations on integers up to 256-bit (sign + 255-bit magnitude).
This significantly extends AtomVM's numeric capabilities beyond the previous
64-bit limitation.
Core Big Integer Support:
- Implemented a new big integer representation using boxed terms with sign bit
- Added comprehensive big integer arithmetic through the new `intn` module
- `term_is_any_integer()` returns true for big integers
- Boxed integers now utilize the sign bit for efficient sign representation
Arithmetic Operations:
- All arithmetic operations (`+`, `-`, `*`, `div`, `rem`, `abs`, `neg`) now
support integers up to 256-bit
- All bitwise operations (`band`, `bor`, `bxor`, `bnot`, `bsl`, `bsr`) now
support integers up to 256-bit
- Float conversion functions now handle big integer conversions in both
directions
Serialization Support:
- Added big integer support in `binary_to_term/1` and `term_to_binary/1,2`
- External term format now encodes/decodes big integers as `SMALL_BIG_EXT`
JIT Enhancements:
- Added JIT support for big integer encoding
- Implemented big integer constant support in opcodes (JIT and Emu)
Overflow Checking (breaking):
- `bsl` (bitshift left) now properly checks for overflow. While this shouldn't
affect existing code (integers were previously limited to 64 bits), ensure
values are masked before left bitshifts: e.g., `(16#FFFF band 0xF) bsl 252`
Error Handling (breaking):
- `binary_to_integer/1` no longer accepts binaries with whitespace or prefixes
like `<<"0xFF">>` or `<<" 123">>`
- `binary_to_integer` and `list_to_integer` now raise `badarg` instead of
`overflow` when parsing integers exceeding 256 bits. Update error handling code
accordingly
Bug Fixes:
- Fixed `list_to_integer` bug with integers close to `INT64_MAX`
These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).
SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-laterFile tree
39 files changed
+9565
-722
lines changed- doc/src
- libs/jit
- include
- src
- src/libAtomVM
- tests
- erlang_tests
- libs/jit
39 files changed
+9565
-722
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
| 63 | + | |
62 | 64 | | |
63 | 65 | | |
64 | 66 | | |
| |||
69 | 71 | | |
70 | 72 | | |
71 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
72 | 81 | | |
73 | 82 | | |
74 | 83 | | |
| |||
79 | 88 | | |
80 | 89 | | |
81 | 90 | | |
| 91 | + | |
82 | 92 | | |
83 | 93 | | |
84 | 94 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
16 | 22 | | |
17 | 23 | | |
18 | 24 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
47 | 146 | | |
48 | 147 | | |
49 | 148 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
180 | 180 | | |
181 | 181 | | |
182 | 182 | | |
183 | | - | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
184 | 192 | | |
185 | 193 | | |
186 | 194 | | |
| |||
189 | 197 | | |
190 | 198 | | |
191 | 199 | | |
192 | | - | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
193 | 204 | | |
194 | | - | |
195 | | - | |
196 | | - | |
| 205 | + | |
| 206 | + | |
197 | 207 | | |
198 | 208 | | |
199 | 209 | | |
| |||
242 | 252 | | |
243 | 253 | | |
244 | 254 | | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
245 | 337 | | |
246 | 338 | | |
247 | 339 | | |
| |||
278 | 370 | | |
279 | 371 | | |
280 | 372 | | |
281 | | - | |
| 373 | + | |
282 | 374 | | |
283 | 375 | | |
284 | 376 | | |
| |||
300 | 392 | | |
301 | 393 | | |
302 | 394 | | |
303 | | - | |
| 395 | + | |
304 | 396 | | |
305 | 397 | | |
306 | 398 | | |
| |||
446 | 538 | | |
447 | 539 | | |
448 | 540 | | |
449 | | - | |
| 541 | + | |
450 | 542 | | |
451 | 543 | | |
452 | 544 | | |
| |||
464 | 556 | | |
465 | 557 | | |
466 | 558 | | |
467 | | - | |
| 559 | + | |
468 | 560 | | |
469 | 561 | | |
470 | 562 | | |
471 | 563 | | |
472 | 564 | | |
473 | 565 | | |
474 | 566 | | |
475 | | - | |
| 567 | + | |
476 | 568 | | |
477 | 569 | | |
478 | 570 | | |
| |||
630 | 722 | | |
631 | 723 | | |
632 | 724 | | |
| 725 | + | |
| 726 | + | |
633 | 727 | | |
634 | 728 | | |
635 | 729 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| |||
740 | 740 | | |
741 | 741 | | |
742 | 742 | | |
743 | | - | |
| 743 | + | |
744 | 744 | | |
745 | 745 | | |
746 | 746 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
23 | 27 | | |
24 | 28 | | |
25 | 29 | | |
| |||
0 commit comments