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

The MPFR Native Interface for .NET Library exposes to .NET (through P-Invoke and .NET types) all of the functionality of the GNU MPFR Library (version 4.0.0). It automatically loads at runtime the 32-bit or 64-bit GNU MPFR library that matches the current CPU architecture, thus allowing building Visual Studio Projects for Any CPU, x86, or x64. MPFR depends on the GNU MP Library. The MPFR Native Interface library loads the required GNU MP library through the Math.Gmp.Native.NET NuGet package. As a result, all of the Math.Gmp.Native library functionality and help are available when loading the Math.Mpfr.Native library. The Math.Gmp.Native.NET NuGet package 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.Mpfr.Native.

NuGet Package

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

Overview

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

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

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

C and .NET Types Equivalence

The table below shows how each C type maps to .NET.

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)

mpfr_exp_t

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

mpfr_prec_t

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

mpfr_sign_t

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

intmax_t

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

uintmax_t

UInt64 / ulong (C#) / ULong (VB.NET)

Building the GNU MPFR 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.

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

  4. Build GNU MPFR.

    Unzip mpfr-4.0.0.tar.bz2 in folder C:\Temp. This puts GNU MPFR in subfolder mpfr-4.0.0.

    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/mpfr-4.0.0
      ./configure CC="gcc -static-libgcc" --with-gmp=/c/Temp/x86 or x64 --enable-static --prefix=/c/Temp/x86 or x64
      make

    To statically link libgcc dependencies, go to the src folder, use from the output window the last line like gcc -static-libgcc ... /libmpfr.dll.a, add to it: -static-libgcc -Wl,-Bstatic -lpthread, and run it. Go back to parent folder.

    • cd src
      gcc -static-libgcc ... /libmpfr.dll.a -static-libgcc -Wl,-Bstatic -lpthread
      cd ..
      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 MPFR source. The make install command creates libmpfr-4.dll in the C:\Temp\x86 and C:\Temp\x64 folders. These two compiled versions of the GNU MPFR library are to be copied to the x86 and x64 folders of the Math.Mpfr.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 MPFR Library for a Specific CPU Type on Windows

The MPFR library does not contain any CPU-specific code optimization. However, MPFR depends on GMP, and GMP can be built for a specific CPU. The --enable-fat build option above creates a GMP 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 GMP library (and as a result an MPFR library) for a specific CPU type. You will end up with libraries that runs only on that CPU type, but the GMP library will be smaller. See the Build Options from the GNU MP Manual for the supported CPU types.

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

Although our main goal was to compile GNU MPFR in order to use it from .NET, the compiled 32-bit and 64-bit GNU MPFR 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\mpfr.h, include\gmp.h, bin\libmpfr-4.dll, bin\libgmp-10.dll, lib\libgmp.dll.a, and lib\libmpfr.dll.a to the Visual Studio C++ project folder. Include gmp.h and mpfr.h in your C++ source file. In the Linker, Input Property Page of the project, add libgmp.dll.a and libmpfr.dll.a to the Additional Dependencies. Build your C++ project, and copy libgmp-10.dll and libmpfr-4.dll to the output bin folder. Run your application.

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

See Also