bigint-mul

bigint-mul multiplies arbitrary-precision integers on the GPU by number-theoretic transform, loadable through kernels, returning an exact Python int. The reference baseline is GMP's mpz_mul via gmpy2, which it overtakes near one million bits and beats by up to 2.8x at four million.

Multiplying numbers with millions of digits is the substrate of computational number theory and record arithmetic, and it has been CPU territory: GMP switches through Karatsuba, Toom-Cook, and FFT multiplication as operands grow. This kernel moves the FFT regime onto the GPU: operands become limb sequences, the product becomes a convolution computed as a pointwise product of number-theoretic transforms, and carries settle in logarithmic depth, so a multiplication that costs GMP ten milliseconds returns in three and a half.

Operand waveforms transform into spectral lines, multiply pointwise, and collapse into the product wave as a carry scan sweeps across

Two 4,194,304-bit integers multiplied live: limb waveforms, their number-theoretic spectra, the pointwise product, the inverse transform, and the carry prefix-scan settling the 2,525,223-digit result, digit-for-digit equal to GMP; 3.5 ms against GMP's 9.8 ms on the same host.

Usage

import torch
from kernels import get_kernel

bm = get_kernel("phanerozoic/bigint-mul", version=1, trust_remote_code=True)

a = 3 ** 2_000_000
b = 5 ** 2_000_000
product = bm.mul(a, b)          # exact Python int
assert product == a * b

version selects the release branch; trust_remote_code is required by kernels for publishers without the trusted-publisher mark.

API

Symbol Purpose
mul(a, b) exact product of two integers, returned as a Python int

Operands up to 2^25 base-2^16 limbs, about half a billion bits each. Signed operands and zero are handled.

Method

Operands are split into base-2^16 limbs, making the product a linear convolution of the limb sequences. The convolution is a pointwise product of forward NTTs modulo the primes 15 * 2^27 + 1 and 27 * 2^26 + 1, both congruent to 1 modulo a large power of two, giving transform lengths to 2^26. Each coefficient is bounded by n * (2^16 - 1)^2 < 2^58, below the product of the primes, so CRT determines it uniquely.

Carry normalization is a prefix scan, not an iteration. A carry entering a run of maximal limbs advances one position per pass, so iterating "keep the low half, carry the rest" costs a pass per limb. Four coarse passes reduce every coefficient to at most 2^16 + 2, leaving single-bit carries; position i generates a carry when its high bit is set and propagates one when its low half is 0xffff, and composing generate-propagate pairs is associative, so a Hillis-Steele scan resolves all carries in logarithmic depth.

Limbs cross PCIe packed two per 32-bit word, an integer's own little-endian byte layout, so neither direction converts format.

Measured

Against GMP 6.x via gmpy2 and CPython's native multiplication, same host. Square operands of the stated bit length, best of several runs, complete call including transfer and integer assembly.

operand bits CPython GMP bigint-mul vs GMP
16,384 0.08 ms 0.01 ms 0.64 ms 0.02x
65,536 0.72 ms 0.07 ms 0.75 ms 0.09x
262,144 6.51 ms 0.37 ms 0.91 ms 0.41x
1,048,576 56.4 ms 1.78 ms 1.35 ms 1.31x
4,194,304 521 ms 9.66 ms 3.65 ms 2.65x
16,777,216 4658 ms 51.1 ms 33.9 ms 1.51x

Crossover against GMP is near 10^6 bits; below it, launch latency and the host round trip dominate a job GMP finishes in microseconds. Phase split at 16.8 x 10^6 bits: transform 2.6 ms, operand export 3.8 ms, device-to-host 0.3 ms, integer assembly 3.0 ms.

Verification

The suite compares against CPython's native integer multiplication at every size, digit for digit: random operands from 17 to 2^20 bits, asymmetric operand sizes, exact powers of two, all four sign combinations, and the all-maximal-limb pattern that is the worst case for carry propagation.

Requirements and limits

  • NVIDIA GPU with compute capability 8.0+.
  • Operands to 2^25 base-2^16 limbs (~5 x 10^8 bits) each.
  • GMP remains faster below roughly one million bits; this kernel is for the regime where a single multiplication is already milliseconds of CPU time.

References

Schoenhage-Strassen FFT multiplication; number-theoretic transforms over word-size primes; GMP (mpz_mul) as the reference implementation.

License

Apache-2.0.

Downloads last month
-
apache-2.0
Supported hardwares new
CUDA
8.08.68.99.010.012.0
GPU
B300
288GB
NVIDIA SXM
B200
192GB
NVIDIA SXM
H200
141GB
NVIDIA SXM
H100
80GB
GPU
H800
80GB
GPU
H20
96GB
GPU
L40s
48GB
GPU
L40
48GB
GPU
L20
48GB
GPU
L4
24GB
DGX Spark
GB10
128GB
GPU
RTX PRO 6000 WS
96GB
GPU
RTX PRO 6000 Max-Q
96GB
GPU
RTX PRO 5000
48GB
GPU
RTX PRO 4500 WS
32GB
GPU
RTX PRO 4000
24GB
GPU
RTX PRO 4000 SFF
24GB
GPU
RTX PRO 2000
16GB
GPU
RTX 6000 Ada
48GB
GPU
RTX 5880 Ada
48GB
RTX
RTX 5000 Ada
32GB
GPU
RTX 4500 Ada
24GB
RTX
RTX 4000 Ada
20GB
RTX
RTX 4000 SFF Ada
20GB
GPU
RTX 3500 Ada Mobile
12GB
GPU
RTX 2000 Ada
16GB
GPU
RTX A6000
48GB
GPU
RTX A5000
8GB
GPU
RTX A5000 Max-Q
16GB
GPU
RTX A5000 Mobile
16GB
GPU
RTX A4000
16GB
GPU
RTX A4000 Max-Q
8GB
GPU
RTX A4000 Mobile
8GB
GPU
RTX A3000 Mobile
6GB
GPU
RTX A2000
6GB
GPU
RTX A2000 Embedded
4GB
GPU
RTX A2000 Max-Q
4GB
GPU
RTX A2000 Mobile
4GB
GPU
A800
40GB
GPU
A100
80GB
GPU
A40
48GB
GPU
A30
24GB
GPU
A10
24GB
GPU
A2
16GB
RTX
RTX 5090
32GB
RTX
RTX 5090 D
32GB
RTX
RTX 5090 Mobile
24GB
RTX
RTX 5080
16GB
RTX
RTX 5080 Mobile
16GB
RTX
RTX 5070
12GB
RTX
RTX 5070 Mobile
8GB
RTX
RTX 5070 Ti
16GB
RTX
RTX 5070 Ti Mobile
12GB
RTX
RTX 5060 Ti
16GB
RTX
RTX 5060
8GB
RTX
RTX 5060 Mobile
8GB
RTX
RTX 5050
8GB
RTX
RTX 5050 Mobile
8GB
RTX
RTX 4090
24GB
RTX
RTX 4090D
24GB
RTX
RTX 4090 Mobile
16GB
RTX
RTX 4080 SUPER
16GB
RTX
RTX 4080
16GB
RTX
RTX 4080 Mobile
12GB
RTX
RTX 4070
12GB
RTX
RTX 4070 Mobile
8GB
RTX
RTX 4070 Ti
12GB
RTX
RTX 4070 Super
12GB
RTX
RTX 4070 Ti Super
16GB
RTX
RTX 4060
8GB
RTX
RTX 4060 Ti
8GB
RTX
RTX 4090 Laptop
16GB
RTX
RTX 4080 Laptop
12GB
RTX
RTX 4070 Laptop
8GB
RTX
RTX 4060 Laptop
8GB
RTX
RTX 4050 Laptop
6GB
RTX
RTX 3090
24GB
RTX
RTX 3090 Ti
24GB
RTX
RTX 3080
12GB
RTX
RTX 3080 Ti
12GB
RTX
RTX 3080 Mobile
16GB
RTX
RTX 3070
8GB
RTX
RTX 3070 Ti
8GB
RTX
RTX 3070 Ti Mobile
8GB
RTX
RTX 3060 Ti
8GB
RTX
RTX 3060
12GB
RTX
RTX 3060 Mobile
6GB
RTX
RTX 3050 Mobile
4GB
GPU
RTX 2050 Mobile
4GB
Jetson
Jetson AGX Orin 64GB
64GB
Jetson
Jetson AGX Orin 32GB
32GB
Jetson
Jetson Orin NX 16GB
16GB
Jetson
Jetson Orin NX 8GB
8GB
Jetson
Jetson Orin Nano 8GB
8GB
Jetson
Jetson Orin Nano 4GB
4GB
OS
linux
Arch
x86_64aarch64
Kernel Builder
19aaa64