Repository navigation
v1.0.0
Sdcb.Arithmetic 1.0.0
First stable release of the pure managed C# rewrite of Sdcb.Arithmetic: high-performance arbitrary-precision integers, rationals and correctly-rounded floating point, with no native dependencies.
dotnet add package Sdcb.Arithmetic
Relationship to the previous Sdcb.Arithmetic
The previous generation, sdcb/Sdcb.Arithmetic, is a set of P/Invoke bindings (Sdcb.Arithmetic.Gmp / Sdcb.Arithmetic.Mpfr) over native GMP and MPFR. It needs native runtime packages per platform, and those native libraries are LGPL.
This repository is a from-scratch, 100% C# implementation:
- Pure managed, no native libraries. Runs anywhere .NET 10 runs: x64, ARM64, AOT, with no
runtime.*packages to ship. - MIT licensed. It is an independent implementation of published algorithms and contains no GMP or MPFR source code, so there is no LGPL obligation. Native GMP/MPFR are used only as black-box references for tests and benchmarks.
- Slightly slower than native. It is a managed implementation, so expect a small gap to GMP/MPFR. It is already faster than
System.Numerics.BigIntegerin most cases. See the numbers below. - The old P/Invoke bindings remain available in the old repository if you specifically need the native libraries.
What's included
| Type | Models | Description |
|---|---|---|
BigInt |
GMP mpz_t |
Immutable arbitrary-precision signed integer: arithmetic, bitwise ops, GCD, modular power, primality (BPSW + Miller-Rabin), combinatorics, random numbers, string conversion in any base 2..62 |
BigRational |
GMP mpq_t |
Immutable rational number, always kept in canonical form |
Real |
MPFR mpfr_t |
Correctly-rounded binary floating point with arbitrary precision: five rounding modes, NaN / ±∞ / ±0, 100+ functions (elementary, transcendental, hyperbolic, Gamma/Beta, Bessel, Airy, erf, and more) |
- All three types implement .NET generic math (
IBinaryInteger<T>,INumber<T>,IBinaryFloatingPointIeee754<T>) and interoperate withSystem.Numerics.BigInteger. - SIMD acceleration with AVX-512 / AVX2 / SSE / NEON where available, with portable fallbacks elsewhere.
- Large-number algorithms include NTT multiplication, Burnikel-Ziegler and Newton division, and Montgomery modular arithmetic.
Quick start
using Sdcb.Arithmetic;
BigInt p = BigInt.Pow(2, 521) - 1; // Mersenne prime M521
bool prime = p.IsProbablePrime();
BigRational r = new BigRational(1, 3) + new BigRational(1, 6); // 1/2
Real.DefaultPrecision = 256; // bits
Real pi = Real.Pi();
Real z = Real.Exp(Real.Sin(Real.Parse("1.5")), precision: 512, RealRounding.TowardPositive);Performance
Measured on AMD Ryzen 7 5800X (AVX2), .NET 10. Full tables and methodology are in docs/performance.md.
| Operation | Speedup over BigInteger |
Time vs native GMP 6.3 (lower is better) |
|---|---|---|
| Multiply | 1.9-28x depending on size | 1.3-1.6x (>= 16 limbs) |
| Divide 2N/N | 2.2-5.9x | 1.5-2.6x |
| Modular power | 4.2-7x | 1.3-2.8x |
| GCD | 1.5-5.8x | 0.9-1.4x |
Real against native MPFR 4.2 (53-16384 bits, both correctly rounded): add/mul 1.3-2.5x, div/sqrt 1.3-3.3x, exp/sin/atan 0.9-1.8x, log/pow 1.6-2.7x of MPFR's time. So expect results in the same ballpark as native, slightly slower.
Correctness
Every BigInt operation is cross-checked against BigInteger and native GMP, and every Real function is checked bit-for-bit against MPFR in all five rounding modes.
Documentation
- README / 中文 README
- Examples: 1,000,000 digits of π, RSA, rounding modes, generic math (also shipped as LINQPad samples in the NuGet package)
- Real: features and functions
- GMP / MPFR to BigInt / Real API mapping
- Design, algorithms and verification
中文简介
Sdcb.Arithmetic 1.0.0 是纯 C# 重写的高性能任意精度算术库,提供 BigInt(整数)、BigRational(有理数)和 Real(正确舍入的任意精度浮点,5 种舍入模式,NaN/±∞/±0,100+ 函数)。
前身是 sdcb/Sdcb.Arithmetic,那是对原生 GMP/MPFR 的 P/Invoke 封装,需要按平台带原生库(LGPL)。这一版:
- 纯 C#,无任何原生依赖,x64 / ARM64 / AOT 都能跑
- MIT 开源,是对公开算法的独立实现,不含 GMP/MPFR 源码,没有 LGPL 约束
- 性能比原生略差一点:多数运算约为原生 GMP/MPFR 耗时的 1-3 倍,但大多数场景仍明显快于
System.Numerics.BigInteger - 实现了 .NET 泛型数学接口,并使用 AVX-512/AVX2/SSE/NEON 加速
- 所有运算都与
BigInteger、原生 GMP/MPFR 做过对拍验证