Click or drag to resize
Welcome to the GMP Native Interface for .NET Library

The GMP Native Interface for .NET Library exposes to .NET (through P-Invoke and .NET types) all of the functionality of the GNU MP Library (version 6.1.2). It automatically loads at runtime the 32-bit or 64-bit GNU MP library that matches the current CPU architecture, thus allowing building Visual Studio Projects for Any CPU, x86, or x64. It is based on the GNU MP "fat" build which automatically detects the current CPU type, and selects any available assembly language code optimization for that CPU, thus providing best performance.

Source Code

The source code of the library is available on GitHub in the project Math.Gmp.Native.

NuGet Package

You can use the library by loading it from the NuGet package Math.Gmp.Native.NET.

Overview

The gmp_lib class has a static method for each one of the GNU MP functions. Other types are defined to mimic struct's and typedef's of the GNU MP and C libraries, as well as C language constructs such as char * and void *.

The GMP Native Interface for .NET Library relies on pre-built 32-bit and 64-bit versions of the GNU MP Library. Instructions for building the GNU MP Library on Windows are given below.

For convenience, this help file has been created from the GNU MP manual version 6.1.2. It shows with examples how each GNU MP function is called in .NET. For an introduction to GNU MP, refer to the GNU MP Manual.

C and .NET Types Equivalence

The table below shows how each C type maps to .NET. Note that the mp_limb_t and size_t C types map to the CPU word, i.e., 32 or 64 bits. In particular, because mp_limb_t is the type of the integers that make up multi-precision numbers, matching the CPU word size ensures maximum performance. Unless you intend to use low-level (mpn) functions, you do not need to take into account the CPU word size, and can build for the "Any CPU" platform.

C Types

.NET Types

short

Int16 / short (C#) / Short (VB.NET)

int

Int32 / int (C#) / Integer (VB.NET)

long

Int32 / int (C#) / Integer (VB.NET)

long long

Int64 / long (C#) / Long (VB.NET)

mp_bitcnt_t

UInt32 / uint (C#) / UInteger (VB.NET)

mp_exp_t

Int32 / int (C#) / Integer (VB.NET)

mp_size_t

Int32 / int (C#) / Integer (VB.NET)

mp_limb_t

UInt32 (on 32-bit CPU) / UInt64 (on 64-bit CPU)

size_t

UInt32 (on 32-bit CPU) / UInt64 (on 64-bit CPU)

Building the GNU MP Library on Windows
  1. Install MSYS2.

    On a 64-bit computer, install msys2-x86_64-20161025.exe, and on a 32-bit computer, install msys2-i686-20161025.exe. You can also check for a more recent version of MSYS2 here. Install MSYS2 to its default location.

    After installation, you need to updates MSYS2 packages. From the Windows Start Menu, start MSYS2 MSYS. In the shell command window, enter the command:

    • pacman -Syuu

    and follow instructions. You will have to close the command window, reopen a new one, and reenter the command pacman -Syuu.

    Finally, in order to build software, you need to install a number of packages with the command:

    • pacman -S --needed base-devel mingw-w64-i686-toolchain mingw-w64-x86_64-toolchain git subversion mercurial mingw-w64-i686-cmake mingw-w64-x86_64-cmake

    run from the same command window as in the previous step.

    To build 32-bit software, use the MSYS2 MinGW 32-bit command from the Windows Start Menu, and for 64-bit software, use MSYS2 MinGW 64-bit.

  2. Install yasm.

    On a 64-bit computer, copy yasm-1.3.0-win64.exe to C:\msys64\usr\bin, and rename it to yasm.exe.

    Similarly on a 32-bit computer, copy yasm-1.3.0-win32.exe to C:\msys32\usr\bin, and rename it to yasm.exe.

  3. Build GNU MP.

    Create folders C:\Temp\x86 and C:\Temp\x64. These are the folder where the compiled 32-bit and 64-bit versions of GNU MP will be installed. Unzip gmp-6.1.2.tar.bz2 in folder C:\Temp. This puts GNU MP in subfolder gmp-6.1.2.

    In each one of the command windows openend with the commands MSYS2 MinGW 32-bit and MSYS2 MinGW 64-bit from the Windows Start Menu, run the commands below:

    • cd /c/Temp/gmp-6.1.2
      ./configure --enable-fat --disable-static --enable-shared --prefix=/c/Temp/x86 or x64
      make
      make check
      make install

    The --prefix specifies the install folder. Note that the Windows C:\ drive is specified as the root /C/ folder in the MinGW window. Note also that the configure and make commands are to be run against a fresly uncompressed GNU MP source. The make install command creates libgmp-10.dll in the C:\Temp\x86 and C:\Temp\x64 folders. These two compiled versions of the GNU MP library are to be copied to the x86 and x64 folders of the Math.Gmp.Native Visual Studio projects. They can also be copied directly into the x86 and x64 folders of the bin/Debug or bin/Release folders.

    The 32-bit and 64-bit make check commands generate some warnings, but all tests passed successfully.

Building the GNU MP Library for a Specific CPU Type on Windows

The --enable-fat build option above creates a library where optimized low level subroutines are chosen at runtime according to the CPU detected. By using instead the --host option, you can build a library for a specific CPU type. You will end up with a library that runs only on that CPU type, but the library will be samller. See the Build Options from the GNU MPFR Manual for the supported CPU types.

Using the GNU MP Library in a Visual Studio C++ Project

Although our main goal was to compile GNU MP in order to use it from .NET, the compiled 32-bit and 64-bit GNU MP libraries may be used directly in Visual Studio C++ projects. For example, create a default Visual Studio C++ Console Application. Set the Platform to x64. Copy from the C:\Temp\x64 folder the files include\gmp.h, bin\libgmp-10.dll, and lib\libgmp.dll.a to the Visual Studio C++ project folder. Include gmp.h in your C++ source file. In the Linker, Input Property Page of the project, add libgmp.dll.a to the Additional Dependencies. Build your C++ project, and copy libgmp-10.dll to the output bin folder. Run your application.

See ConsoleApplication12.zip for a sample Visual Studio C++ project.

See Also