Otok public index

apotocki/gmp

GMP, the GNU Multiple Precision Arithmetic Library: integers, rationals and floating-point numbers of any size.

Library
gmp
Newest
6.3.0 (r1, windows)
Targets
windows
License
LGPL-3.0-or-later OR GPL-2.0-or-later
Homepage
https://gmplib.org
Documentation
https://gmplib.org/manual/
Keywords
bignum, arbitrary-precision, math, integer, rational

Use it

In otok.toml:

[deps.gmp]
recipe = "apotocki/gmp"
version = "~> 6.3.0"

Versions

VersionTargetChannelPublishedStatus
6.3.0 (r1, windows) windows stable 2026-11-10

6.3.0 (r1, windows)

Platforms

windows

Toolchains

Recipe files

build.lua 3259 bytes · raw
-- GMP's configure and make under the MSYS2 of the make recipe, with MSVC: cl compiles the C and the C++ through the
-- compile script of the release (automake's, which gives cl -o and -l as it takes them), m4 of the tree expands GMP's
-- assembly and GNU as of the tree assembles it (gas.sh of the recipe: as GMP builds it with MinGW, for Windows' calling
-- convention), GNU ar and nm of the tree make and read the archives. One library for every x86_64 CPU: --enable-fat
-- chooses the code of the CPU the program runs on when it first calls GMP.

-- A path as the programs of MSYS2 name it: C:/x is /c/x.
local function msys(path)
    return (path:gsub("^(%a):/", function(drive) return "/" .. drive:lower() .. "/" end))
end

-- MSVC's options with '-': MSYS2 takes /O2 for a path and turns it into C:/…/O2 for native programs
local function joined(flags, extra)
    local all = {}
    for _, f in ipairs(flags) do all[#all + 1] = (f:gsub("^/", "-")) end
    for _, f in ipairs(extra or {}) do all[#all + 1] = f end
    return table.concat(all, " ")
end

function build(slice, options, deps)
    local t = slice.toolchain
    local bin = msys(slice.toolchains.as):match("^(.*)/[^/]*$")
    local cl = msys(slice.source) .. "/compile cl -nologo"
    local args = {
        -- the build machine and the host as GMP knows Windows on x86_64: its assembly for Windows' calling convention
        "--build=x86_64-pc-mingw64", "--host=x86_64-pc-mingw64",
        "--enable-fat", "--enable-cxx",
        "CC=" .. cl, "CXX=" .. cl,
        "CFLAGS=" .. joined(t.cflags),
        "CXXFLAGS=" .. joined(t.cxxflags, { "-EHsc" }),
        "CCAS=sh " .. msys(slice.recipe) .. "/gas.sh " .. msys(slice.toolchains.as),
        "M4=" .. msys(slice.toolchains.m4), "AR=" .. bin .. "/ar", "NM=" .. bin .. "/nm",
        -- what configure cannot find out with cl: memset is an intrinsic, the assembler's word directive is .word, no
        -- libm; and `restrict`, which its C test would define for the C++ of gmpxx too, breaking the UCRT's headers
        "ac_cv_func_memset=yes", "gmp_cv_asm_w32=.word", "gmp_cv_check_libm_for_build=no", "ac_cv_c_restrict=no",
        -- the manual is in the release; nothing remakes it
        "MAKEINFO=true",
    }
    -- configure by a relative path: GMP's tests include "$srcdir/gmp-h.in", which cl cannot open as /c/…
    otok.autotools{
        configure = "../" .. slice.source:match("([^/]+)$") .. "/configure", args = args, cwd = slice.build,
        env = { CC = false, CXX = false, CFLAGS = false, CXXFLAGS = false, CPPFLAGS = false, LDFLAGS = false },
    }

    -- gmp.lib and gmpxx.lib, static; gmp.dll.lib and gmpxx.dll.lib, the import libraries of bin/gmp-10.dll and
    -- bin/gmpxx-4.dll
    local p = slice.prefix
    local libraries = {}
    for _, name in ipairs{ "gmp", "gmpxx" } do
        if slice.linkage == "dynamic" then
            local dll = otok.glob("bin/" .. name .. "-*.dll", p)[1]
            assert(dll, "GMP installed no " .. name .. "-*.dll")
            libraries[name] = { link = p .. "/lib/" .. name .. ".dll.lib", runtime = dll }
        else
            libraries[name] = p .. "/lib/" .. name .. ".lib"
        end
    end
    return { libraries = libraries, headers = p .. "/include" }
end
gas.sh 658 bytes · raw
#!/bin/sh
# CCAS of GMP's build with MSVC: GNU as of the MSYS2 tree (its path the first argument) for GMP's x86_64 assembly,
# which m4 has expanded. GMP gives the assembler the C compiler's options, cl's here, which as does not take: they are
# dropped, and the include directories kept. The object is -o's, or the source's name with .obj (configure's tests).
as=$1
shift
out=
src=
inc=
while [ $# -gt 0 ]; do
    case "$1" in
        -o) out=$2; shift ;;
        -I) inc="$inc -I$2"; shift ;;
        -I*) inc="$inc $1" ;;
        -*) ;;
        *) src=$1 ;;
    esac
    shift
done
[ -n "$out" ] || out=${src%.*}.obj
exec "$as" --64 $inc -o "$out" "$src"
patches/0001-msvc.patch 5554 bytes · raw
GMP built with MSVC (cl) as a fat binary, static or as a DLL.

- gmp-impl.h, mpn/x86_64/fat/sqr_basecase.c: the thresholds of a fat binary are variables, and the arrays the generic
  code sizes by them are variable-length arrays, which MSVC has none of: they take the largest threshold of the CPUs,
  which configure writes in fat.h (SQR_TOOM2_THRESHOLD_LIMIT, MUL_TOOM22_THRESHOLD_LIMIT).
- gmp-impl.h: the functions of struct cpuvec_t are declared with __GMP_DECLSPEC, __declspec(dllexport) in a DLL,
  which MSVC takes on no pointer of a struct.
- gmp-h.in: the inline functions of gmp.h static with MSVC, whose C __inline functions are external definitions that
  every object of a DLL would define again.
- configure, configure.ac: no options of GNU ld for a DLL (--export-all-symbols, --output-def): libtool exports
  the symbols MSVC's link is given in a .def file of its own.
- gmp-h.in: no __GMP_CC and __GMP_CFLAGS, the compiler and the flags of the build, with its paths and its
  configuration, which would make gmp.h differ from one build to the next.

diff --git a/configure b/configure
index 7910aa0..774a736 100644
--- a/configure
+++ b/configure
@@ -12183,9 +12183,7 @@ Use \"--disable-static --enable-shared\" to build just a DLL." "$LINENO" 5
     # __isascii, but for some reason not the plain isascii.
     #
     if test "$enable_shared" = yes; then
-      GMP_LDFLAGS="$GMP_LDFLAGS -no-undefined -Wl,--export-all-symbols"
-      LIBGMP_LDFLAGS="$LIBGMP_LDFLAGS -Wl,--output-def,.libs/libgmp-3.dll.def"
-      LIBGMPXX_LDFLAGS="$LIBGMP_LDFLAGS -Wl,--output-def,.libs/libgmpxx-3.dll.def"
+      GMP_LDFLAGS="$GMP_LDFLAGS -no-undefined"
       LIBGMP_DLL=1
     fi
     ;;
diff --git a/configure.ac b/configure.ac
index 082077b..4f8efd5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2764,9 +2764,7 @@ Use "--disable-static --enable-shared" to build just a DLL.])
     # __isascii, but for some reason not the plain isascii.
     #
     if test "$enable_shared" = yes; then
-      GMP_LDFLAGS="$GMP_LDFLAGS -no-undefined -Wl,--export-all-symbols"
-      LIBGMP_LDFLAGS="$LIBGMP_LDFLAGS -Wl,--output-def,.libs/libgmp-3.dll.def"
-      LIBGMPXX_LDFLAGS="$LIBGMP_LDFLAGS -Wl,--output-def,.libs/libgmpxx-3.dll.def"
+      GMP_LDFLAGS="$GMP_LDFLAGS -no-undefined"
       LIBGMP_DLL=1
     fi
     ;;
diff --git a/gmp-h.in b/gmp-h.in
index 8598e97..fa37296 100644
--- a/gmp-h.in
+++ b/gmp-h.in
@@ -402,7 +402,7 @@ typedef const __gmp_randstate_struct *gmp_randstate_srcptr;
 
 /* Microsoft's C compiler accepts __inline */
 #ifdef _MSC_VER
-#define __GMP_EXTERN_INLINE  __inline
+#define __GMP_EXTERN_INLINE  static __inline
 #endif
 
 /* Recent enough Sun C compilers want "inline" */
@@ -2330,10 +2330,6 @@ enum
   GMP_ERROR_MPZ_OVERFLOW = 16
 };
 
-/* Define CC and CFLAGS which were used to build this version of GMP */
-#define __GMP_CC "@CC@"
-#define __GMP_CFLAGS "@CFLAGS@"
-
 /* Major version number is the value of __GNU_MP__ too, above. */
 #define __GNU_MP_VERSION            6
 #define __GNU_MP_VERSION_MINOR      3
diff --git a/gmp-impl.h b/gmp-impl.h
index 2615af7..31f9f1b 100644
--- a/gmp-impl.h
+++ b/gmp-impl.h
@@ -2208,6 +2208,12 @@ __GMP_DECLSPEC mp_limb_t gmp_primesieve (mp_ptr, mp_limb_t);
 #ifndef SQRLO_BASECASE_THRESHOLD_LIMIT
 #define SQRLO_BASECASE_THRESHOLD_LIMIT  SQRLO_BASECASE_THRESHOLD
 #endif
+/* MSVC has no variable-length arrays, and the thresholds of a fat binary are variables: the arrays sized by the
+   limits take the largest of the CPUs, which configure writes in fat.h, SQRLO_DC_THRESHOLD_LIMIT as SQRLO_DC_THRESHOLD
+   follows from MUL_TOOM22_THRESHOLD (below).  */
+#if WANT_FAT_BINARY && defined (_MSC_VER) && ! defined (SQRLO_DC_THRESHOLD_LIMIT)
+#define SQRLO_DC_THRESHOLD_LIMIT  (2 * MUL_TOOM22_THRESHOLD_LIMIT)
+#endif
 #ifndef SQRLO_DC_THRESHOLD_LIMIT
 #define SQRLO_DC_THRESHOLD_LIMIT  SQRLO_DC_THRESHOLD
 #endif
@@ -4721,6 +4727,12 @@ __GMP_DECLSPEC int __gmp_doscan (const struct gmp_doscan_funs_t *, void *, const
 /* NOTE: The function pointers in this struct are also in CPUVEC_FUNCS_LIST
    in mpn/x86/x86-defs.m4 and mpn/x86_64/x86_64-defs.m4.  Be sure to update
    those when changing here.  */
+/* The DECL_ macros declare functions, which a DLL exports: MSVC takes no __declspec on the pointers of the struct.  */
+#ifdef _MSC_VER
+#pragma push_macro ("__GMP_DECLSPEC")
+#undef  __GMP_DECLSPEC
+#define __GMP_DECLSPEC
+#endif
 struct cpuvec_t {
   DECL_add_n           ((*add_n));
   DECL_addlsh1_n       ((*addlsh1_n));
@@ -4765,6 +4777,9 @@ struct cpuvec_t {
   mp_size_t            sqr_toom3_threshold;
   mp_size_t            bmod_1_to_mod_1_threshold;
 };
+#ifdef _MSC_VER
+#pragma pop_macro ("__GMP_DECLSPEC")
+#endif
 __GMP_DECLSPEC extern struct cpuvec_t __gmpn_cpuvec;
 __GMP_DECLSPEC extern int __gmpn_cpuvec_initialized;
 #endif /* x86 fat binary */
diff --git a/mpn/x86_64/fat/sqr_basecase.c b/mpn/x86_64/fat/sqr_basecase.c
index d1c5dcd..1dadbd0 100644
--- a/mpn/x86_64/fat/sqr_basecase.c
+++ b/mpn/x86_64/fat/sqr_basecase.c
@@ -29,4 +29,12 @@ GNU Lesser General Public License along with the GNU MP Library.  If not,
 see https://www.gnu.org/licenses/.  */
 
 
+/* The buffer of the generic code is sized by SQR_TOOM2_THRESHOLD, a variable in a fat binary, and MSVC has no
+   variable-length arrays: it takes the largest threshold of the CPUs, which configure writes in fat.h.  */
+#ifdef _MSC_VER
+#include "gmp-impl.h"
+#undef  SQR_TOOM2_THRESHOLD
+#define SQR_TOOM2_THRESHOLD  SQR_TOOM2_THRESHOLD_LIMIT
+#endif
+
 #include "mpn/generic/sqr_basecase.c"
recipe.toml 1876 bytes · raw
# The otok recipe of GMP (https://github.com/apotocki/otok): libgmp and the C++ class interface libgmpxx for Windows on
# x86_64, from the release archive, with GMP's configure and make under MSYS2 (the tree of the make recipe) and MSVC; its
# assembly, as GMP builds it with MinGW, by GNU as of that tree; a fat binary, which chooses the code of the CPU it runs
# on; static or dynamic.
[recipe]
api = 1
library = "gmp"
name = "gmp"
version = "6.3.0"
revision = 1
license = "LGPL-3.0-or-later OR GPL-2.0-or-later"
license-files = ["COPYING.LESSERv3", "COPYINGv3", "COPYINGv2"]
homepage = "https://gmplib.org"
documentation = "https://gmplib.org/manual/"
description = "GMP, the GNU Multiple Precision Arithmetic Library: integers, rationals and floating-point numbers of any size."
keywords = ["bignum", "arbitrary-precision", "math", "integer", "rational"]
linkage = ["static", "dynamic"]

# The archive of the release, signed by GMP's key 343C 2FF0 FBEE 5EC2 EDBE F399 F359 9FF8 28C6 7298 (gmp-6.3.0.tar.xz.sig)
[source]
url = "https://ftp.gnu.org/gnu/gmp/gmp-6.3.0.tar.xz"
sha256 = "a3c2b80201b89e68616f4ad30bc66aee4927c3ce50e33929ca819d5c43538898"
mirrors = ["https://ftpmirror.gnu.org/gnu/gmp/gmp-6.3.0.tar.xz", "https://gmplib.org/download/gmp/gmp-6.3.0.tar.xz"]
patches = ["patches/0001-msvc.patch"]

[platforms]
supported = ["windows"]
# the assembly of arm64 is not yet assembled for Windows
architectures = { windows = ["x86_64"] }
build-on = ["windows/msvc"]

[toolchains]
make = { version = ">= 4.4.1", default-recipe = "apotocki/make" }
# GMP's assembly is m4 macros for GNU as: both of the MSYS2 tree of the make recipe
m4 = { default-recipe = "apotocki/make" }
as = { default-recipe = "apotocki/make" }

[[product]]
name = "GMP"
libraries = ["gmp"]

[[product]]
name = "GMPXX"
libraries = ["gmpxx"]
headers = false
depends = ["GMP"]
link = { runtime = "c++" }