Skip to content

Add optional GMP-backed bignum multiplication#1008

Open
Peter1169 wants to merge 1 commit into
gambit:masterfrom
Peter1169:bounty/761-gmp-bignum-mul
Open

Add optional GMP-backed bignum multiplication#1008
Peter1169 wants to merge 1 commit into
gambit:masterfrom
Peter1169:bounty/761-gmp-bignum-mul

Conversation

@Peter1169

@Peter1169 Peter1169 commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Summary

This adds an opt-in --enable-gmp-bignum configure option that uses LibGMP for large bignum multiplication and squaring.

The default build remains unchanged. When the option is enabled, configure checks for gmp.h and -lgmp, defines ___USE_GMP_BIGNUM for the Gambit runtime library build, and large bignum multiplication tries the GMP path before falling back to Gambit's existing algorithms.

The GMP path keeps Gambit's bignum representation unchanged:

  • converts Gambit two's-complement bignums into temporary mpz_t values
  • uses mpz_mul for multiplication and squaring
  • exports back into a normal movable Gambit bignum
  • normalizes the result through the existing bignum normalization path

Related to #761.

Validation

Validated on Linux/WSL and in Docker gcc:9.3.0 with LibGMP installed.

  • default build: ./configure, make -j6 from-scratch
  • default GAMBITLIB_DEFS and _num.o compile lines do not include -D___USE_GMP_BIGNUM
  • default targeted number test passed: ../gsi/gsi -:tl,~~bin=../bin,~~lib=../lib,~~include=../include -f ./run-unit-tests.scm unit-tests/03-number/_num.scm
  • GMP build: ./configure --enable-gmp-bignum, make -j6 from-scratch
  • GMP build detected gmp.h and __gmpz_mul in -lgmp
  • GMP generated lib/_num.c contains the ___USE_GMP_BIGNUM guards and mpz_mul calls
  • GAMBITLIB_DEFS contains -D___USE_GMP_BIGNUM in makefile, lib/makefile, and lib/module-common.mk
  • _num.o compile lines include -D___USE_GMP_BIGNUM
  • GMP targeted number test passed: ../gsi/gsi -:tl,~~bin=../bin,~~lib=../lib,~~include=../include -f ./run-unit-tests.scm unit-tests/03-number/_num.scm
  • after rebasing on current master: git diff --check origin/master...HEAD

A local bignum multiplication/squaring probe used non-sparse operands and checked result tails modulo 1000000007; the default and GMP builds produced matching tails.

Case Default real GMP real Speedup Default allocation GMP allocation
50k-bit multiply, 20 reps 0.019825s 0.000939s 21.1x 8,127,200 254,080
50k-bit square, 20 reps 0.013814s 0.000669s 20.6x 5,503,200 254,080
200k-bit multiply, 5 reps 0.021878s 0.001537s 14.2x 8,117,880 251,360
200k-bit square, 5 reps 0.015129s 0.001080s 14.0x 5,495,840 251,400

Disclosure: I used Codex (GPT-5.5 xHigh) for a final code review pass after human programming.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant