Skip to content

Commit

Permalink
(stdlib) Replace BigInt library with libtommath implementation
Browse files Browse the repository at this point in the history
The previous C++-based implementation used strings for storing its
values. The new approach uses a C-based library which is much more
performant.
  • Loading branch information
perlun committed Feb 27, 2024
1 parent 52f7d7d commit 938986b
Show file tree
Hide file tree
Showing 65 changed files with 6,258 additions and 1,350 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ guide.

## License

[MIT](LICENSE)
[MIT (Expat)](LICENSE)

[perlang-install](scripts/perlang-install) is originally based on
[rustup-init](https://github.com/rust-lang/rustup/blob/master/rustup-init.sh),
Expand All @@ -151,6 +151,10 @@ Syed Faheel Ahmad's `BigInt` library, available at
https://github.com/faheel/BigInt, licensed under the terms of the MIT license.
Copyright (c) 2017 - 2018 Syed Faheel Ahmad.

[src/stdlib/src/libtommath](src/stdlib/src/libtommath/) includes content from
libtommath (https://github.com/libtom/libtommath), licensed under the Unlicense
(http://unlicense.org).

[src/stdlib/src/double-conversion](src/stdlib/src/double-conversion) includes
content from the Google `double-conversion` library, available at
https://github.com/google/double-conversion, licensed under the BSD 3-Clause
Expand Down
2 changes: 2 additions & 0 deletions release-notes/v0.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Initial support for `**` operator in compiled mode [[#420][420]]
- Use correct error message for unsupported operand types [[#421][421]]
- Support `**` for `BigInt` values [[#422][422]]
- Replace `BigInt` library with `libtommath` implementation [[#425][425]]

### Changed
#### Data types
Expand Down Expand Up @@ -53,3 +54,4 @@
[420]: https://github.com/perlang-org/perlang/pull/420
[421]: https://github.com/perlang-org/perlang/pull/421
[422]: https://github.com/perlang-org/perlang/pull/422
[425]: https://github.com/perlang-org/perlang/pull/425
3 changes: 0 additions & 3 deletions src/Perlang.Interpreter/Compiler/PerlangCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -831,9 +831,6 @@ private void AddGlobalClass(string name, PerlangClass perlangClass)
if (new[] { typeof(int), typeof(long), typeof(uint), typeof(ulong), typeof(BigInteger) }.Contains(expr.Left.TypeReference.ClrType) &&
expr.Right.TypeReference.ClrType == typeof(int))
{
// TODO: We need something lke BigInteger.Pow() in .NET to be able to accomplish this. Perhaps do
// TODO: like the .NET folks and implement something using this approach?
// TODO: https://en.wikipedia.org/wiki/Exponentiation_by_squaring
currentMethod.Append($"{leftCast}perlang::BigInt_pow({expr.Left.Accept(this)}, {rightCast}{expr.Right.Accept(this)})");
}
else if (new[] { typeof(int), typeof(long), typeof(uint), typeof(ulong), typeof(float), typeof(double) }.Contains(expr.Left.TypeReference.ClrType) &&
Expand Down
60 changes: 60 additions & 0 deletions src/stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,67 @@ set(headers
src/double-conversion/utils.h
)

set(libtommath_headers
src/libtommath/tommath.h
)

add_library(
stdlib
src/bigint.cpp
src/bigint_pow.cpp
src/Base64.cpp
src/print.cpp
src/libtommath/bn_cutoffs.c
src/libtommath/bn_mp_add.c
src/libtommath/bn_mp_clamp.c
src/libtommath/bn_mp_clear.c
src/libtommath/bn_mp_clear_multi.c
src/libtommath/bn_mp_cmp.c
src/libtommath/bn_mp_cmp_mag.c
src/libtommath/bn_mp_copy.c
src/libtommath/bn_mp_count_bits.c
src/libtommath/bn_mp_div.c
src/libtommath/bn_mp_div_d.c
src/libtommath/bn_mp_div_2.c
src/libtommath/bn_mp_div_2d.c
src/libtommath/bn_mp_div_3.c
src/libtommath/bn_mp_exch.c
src/libtommath/bn_mp_expt_u32.c
src/libtommath/bn_mp_grow.c
src/libtommath/bn_mp_init.c
src/libtommath/bn_mp_init_copy.c
src/libtommath/bn_mp_init_multi.c
src/libtommath/bn_mp_init_size.c
src/libtommath/bn_mp_lshd.c
src/libtommath/bn_mp_mod.c
src/libtommath/bn_mp_mod_2d.c
src/libtommath/bn_mp_mul.c
src/libtommath/bn_mp_mul_d.c
src/libtommath/bn_mp_mul_2.c
src/libtommath/bn_mp_mul_2d.c
src/libtommath/bn_mp_neg.c
src/libtommath/bn_mp_radix_size.c
src/libtommath/bn_mp_radix_smap.c
src/libtommath/bn_mp_rshd.c
src/libtommath/bn_mp_error_to_string.c
src/libtommath/bn_mp_to_radix.c
src/libtommath/bn_mp_set.c
src/libtommath/bn_mp_set_u64.c
src/libtommath/bn_mp_sqr.c
src/libtommath/bn_mp_sub.c
src/libtommath/bn_mp_zero.c
src/libtommath/bn_s_mp_add.c
src/libtommath/bn_s_mp_balance_mul.c
src/libtommath/bn_s_mp_karatsuba_mul.c
src/libtommath/bn_s_mp_karatsuba_sqr.c
src/libtommath/bn_s_mp_mul_digs.c
src/libtommath/bn_s_mp_mul_digs_fast.c
src/libtommath/bn_s_mp_sqr.c
src/libtommath/bn_s_mp_sqr_fast.c
src/libtommath/bn_s_mp_reverse.c
src/libtommath/bn_s_mp_sub.c
src/libtommath/bn_s_mp_toom_mul.c
src/libtommath/bn_s_mp_toom_sqr.c
)

# Enable C++ 17 support. This is required on e.g. AppleClang 13
Expand Down Expand Up @@ -46,6 +101,11 @@ install(
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)

install(
FILES ${libtommath_headers}
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/libtommath"
)

# The testing code is not yet ready for macOS, since it uses a different linker which doesn't support --wrap. We'll
# live with only building/running the tests on other platforms for now.
if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
Expand Down
Loading

0 comments on commit 938986b

Please sign in to comment.