<html><head><metahttp-equiv="X-UA-Compatible"content="IE=edge"/><linkrel="shortcut icon"href="../icons/favicon.ico"/><linkrel="stylesheet"type="text/css"href="../styles/branding.css"/><linkrel="stylesheet"type="text/css"href="../styles/branding-en-US.css"/><scripttype="text/javascript"src="../scripts/branding.js"></script><metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"/><title>Welcome to the GMP Native Interface for .NET Library</title><metaname="Language"content="en-us"/><metaname="System.Keywords"content="Welcome"/><metaname="Microsoft.Help.Id"content="846f5c8a-6cba-433e-9f18-cde2ff5695cd"/><metaname="Description"content="The GMP Native Interface for .NET Library exposes to .NET (through P-Invoke and .NET types) all of the functionality of the GNU MP Libraryhttps://gmplib.org/ (version 6.1.2)."/><metaname="Microsoft.Help.ContentType"content="Concepts"/><metaname="BrandingAware"content="true"/><linkrel="stylesheet"type="text/css"href="../styles/branding-Website.css"/><scripttype="text/javascript"src="../scripts/jquery-1.11.0.min.js"></script><scripttype="text/javascript"src="../scripts/branding-Website.js"></script><scripttype="text/javascript"src="../scripts/clipboard.min.js"></script></head><bodyonload="OnLoad('cs')"><inputtype="hidden"id="userDataCache"class="userDataStyle"/><divclass="pageHeader"id="PageHeader">GMP Native Interface for .NET<formid="SearchForm"method="get"action="#"onsubmit="javascript:TransferToSearchPage(); return false;"><inputid="SearchTextBox"type="text"maxlength="200"/><buttonid="SearchButton"type="submit"></button></form></div><divclass="pageBody"><divclass="leftNav"id="leftNav"><divid="tocNav"><divclass="toclevel0"data-toclevel="0"data-childrenloaded="true"><aclass="tocExpanded"onclick="javascript: Toggle(this);"href="#!"/><adata-tochassubtree="true"href="846f5c8a-6cba-433e-9f18-cde2ff5695cd.htm"title="GMP Native Interface for .NET"tocid="roottoc">GMP Native Interface for .NET</a></div><divclass="toclevel1 current"data-toclevel="1"><adata-tochassubtree="false"href="846f5c8a-6cba-433e-9f18-cde2ff5695cd.htm"title="Welcome to the GMP Native Interface for .NET Library"tocid="846f5c8a-6cba-433e-9f18-cde2ff5695cd">Welcome to the GMP Native Interface for .NET Library</a></div><divclass="toclevel1"data-toclevel="1"><aclass="tocCollapsed"onclick="javascript: Toggle(this);"href="#!"/><adata-tochassubtree="true"href="d9c51b9f-0c54-770b-28ea-6eebd8fef368.htm"title="Math.Gmp.Native"tocid="d9c51b9f-0c54-770b-28ea-6eebd8fef368">Math.Gmp.Native</a></div></div><divid="tocResizableEW"onmousedown="OnMouseDown(event);"></div><divid="TocResize"class="tocResize"><imgid="ResizeImageIncrease"src="../icons/TocOpen.gif"onclick="OnIncreaseToc()"alt="Click or drag to resize"title="Click or drag to resize"/><imgid="ResizeImageReset"src="../icons/TocClose.gif"style="display:none"onclick="OnResetToc()"alt="Click or drag to resize"title="Click or drag to resize"/></div></div><divclass="topicContent"id="TopicContent"><tableclass="titleTable"><tr><tdclass="titleColumn">Welcome to the GMP Native Interface for .NET Library</td></tr></table><spanclass="introStyle"></span><divclass="introduction"><p>
The <strong>GMP Native Interface for .NET Library</strong> exposes to .NET (through P-Invoke and .NET types)
</p></div><divclass="collapsibleAreaRegion"><spanclass="collapsibleRegionTitle"onclick="SectionExpandCollapse('ID1RB')"onkeypress="SectionExpandCollapse_CheckKey('ID1RB', event)"tabindex="0"><imgid="ID1RBToggle"class="collapseToggle"src="../icons/SectionExpanded.png"/>C and .NET Types Equivalence</span></div><divid="ID1RBSection"class="collapsibleSection"><p>
The table below shows how each C type maps to .NET.
Note that the <ahref="bf1f694d-f9b2-6413-12e5-ebae603d5b49.htm">mp_limb_t</a>
and <ahref="d9761bfb-1882-59d2-e89b-c6460141f9a6.htm">size_t</a> C types map
to the CPU word, i.e., 32 or 64 bits.
In particular, because <ahref="bf1f694d-f9b2-6413-12e5-ebae603d5b49.htm">mp_limb_t</a>
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.
</p><divclass="tableSection"><table><tr><th><p>C Types</p></th><th><p>.NET Types</p></th></tr><tr><td><p>short</p></td><td><p>Int16</p></td></tr><tr><td><p>int</p></td><td><p>Int32</p></td></tr><tr><td><p>long</p></td><td><p>Int32</p></td></tr><tr><td><p>long long</p></td><td><p>Int64</p></td></tr><tr><td><p><ahref="1b2b1d64-20c1-7a64-0278-d9ff58ee9cc3.htm">mp_bitcnt_t</a></p></td><td><p>UInt32</p></td></tr><tr><td><p><ahref="e6a17880-791f-37d4-4942-cf26ce7d4cbf.htm">mp_exp_t</a></p></td><td><p>Int32</p></td></tr><tr><td><p><ahref="4b58a584-7a0e-b600-18fa-ebd493861eb3.htm">mp_size_t</a></p></td><td><p>Int32</p></td></tr><tr><td><p><ahref="bf1f694d-f9b2-6413-12e5-ebae603d5b49.htm">mp_limb_t</a></p></td><td><p>UInt32 (on 32-bit CPU) / UInt64 (on 64-bit CPU)</p></td></tr><tr><td><p><ahref="d9761bfb-1882-59d2-e89b-c6460141f9a6.htm">size_t</a></p></td><td><p>UInt32 (on 32-bit CPU) / UInt64 (on 64-bit CPU)</p></td></tr></table></div></div><divclass="collapsibleAreaRegion"><spanclass="collapsibleRegionTitle"onclick="SectionExpandCollapse('ID2RB')"onkeypress="SectionExpandCollapse_CheckKey('ID2RB', event)"tabindex="0"><imgid="ID2RBToggle"class="collapseToggle"src="../icons/SectionExpanded.png"/>Building the GNU MP Library on Windows</span></div><divid="ID2RBSection"class="collapsibleSection"><ol><li><p>
On a 64-bit computer, install <ahref="https://github.com/MachineCognitis/Math.Gmp.Native/blob/master/Math.Gmp.Native/Dependencies/"target="_blank">msys2-x86_64-20161025.exe</a>,
and on a 32-bit computer, install <ahref="https://github.com/MachineCognitis/Math.Gmp.Native/tree/master/Math.Gmp.Native/Dependencies/"target="_blank">msys2-i686-20161025.exe</a>.
You can also check for a more recent version of MSYS2 <ahref="https://github.com/msys2/msys2/wiki/MSYS2-installation"target="_blank">here</a>.
Install MSYS2 to its default location.
</p><p>
After installation, you need to updates MSYS2 packages. From the Windows Start Menu, start <spanclass="command">MSYS2 MSYS</span>. In the shell command window,
On a 64-bit computer, copy <ahref="https://github.com/MachineCognitis/Math.Gmp.Native/blob/master/Math.Gmp.Native/Dependencies/"target="_blank">yasm-1.3.0-win64.exe</a>
to <em>C:\msys64\usr\bin</em>, and rename it to <em>yasm.exe</em>.
</p><p>
Similarly on a 32-bit computer, copy <ahref="https://github.com/MachineCognitis/Math.Gmp.Native/blob/master/Math.Gmp.Native/Dependencies/"target="_blank">yasm-1.3.0-win32.exe</a>
to <em>C:\msys32\usr\bin</em>, and rename it to <em>yasm.exe</em>.
In each one of the command windows openend with the commands <spanclass="command">MSYS2 MinGW 32-bit</span>
and <spanclass="command">MSYS2 MinGW 64-bit</span> from the Windows Start Menu, run the commands below:
</p><ulclass="noBullet"><li><p><spanclass="input">cd /c/Temp/gmp-6.1.2</span><br/><spanclass="input">./configure --enable-fat --disable-static --enable-shared --prefix=/c/Temp/x86</span> or <spanclass="input">x64</span><br/><spanclass="input">make</span><br/><spanclass="input">make check</span><br/><spanclass="input">make install</span><br/></p></li></ul><p>
The <spanclass="input">--prefix</span> specifies the install folder.
Note that the Windows <em>C:\</em> drive is specified as the root <em>/C/</em> folder in the <spanclass="command">MinGW</span> window.
Note also that the <spanclass="input">configure</span> and <spanclass="input">make</span> commands are to be run against a fresly uncompressed GNU MP source.
The <spanclass="input">make install</span> command creates <em>libgmp-10.dll</em> in the <em>C:\Temp\x86</em> and <em>C:\Temp\x64</em> folders.
These two compiled versions of the GNU MP library are to be copied to the <em>x86</em> and <em>x64</em> folders of the <em>Math.Gmp.Native</em> Visual Studio projects.
They can also be copied directly into the <em>x86</em> and <em>x64</em> folders of the <em>bin/Debug</em> or <em>bin/Release</em> folders.
</p><p>
The 32-bit and 64-bit <spanclass="input">make check</span> commands generate some warnings, but all tests passed successfully.
</p></li></ol></div><divclass="collapsibleAreaRegion"><spanclass="collapsibleRegionTitle"onclick="SectionExpandCollapse('ID3RB')"onkeypress="SectionExpandCollapse_CheckKey('ID3RB', event)"tabindex="0"><imgid="ID3RBToggle"class="collapseToggle"src="../icons/SectionExpanded.png"/>Building the GNU MP Library for a Specific CPU Type on Windows</span></div><divid="ID3RBSection"class="collapsibleSection"><p>
The <spanclass="input">--enable-fat</span> build option above creates a library where optimized low level subroutines are chosen at runtime according to the CPU detected.
By using instead the <spanclass="input">--host</span> 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 <ahref="https://gmplib.org/manual/Build-Options.html#Build-Options/"target="_blank">Build Options</a> from the GNU MP Manual for the supported CPU types.
</p></div><divclass="collapsibleAreaRegion"><spanclass="collapsibleRegionTitle"onclick="SectionExpandCollapse('ID4RB')"onkeypress="SectionExpandCollapse_CheckKey('ID4RB', event)"tabindex="0"><imgid="ID4RBToggle"class="collapseToggle"src="../icons/SectionExpanded.png"/>Using the GNU MP Library in a Visual Studio C++ Project</span></div><divid="ID4RBSection"class="collapsibleSection"><p>
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 <strong>Platform</strong> to <strong>x64</strong>.
Copy from the <em>C:\Temp\x64</em> folder the files <em>include\gmp.h</em>, <em>bin\libgmp-10.dll</em>, and <em>lib\libgmp.dll.a</em> to the Visual Studio C++ project folder.
Include <em>gmp.h</em> in your C++ source file.
In the <strong>Linker</strong>, <strong>Input Property Page</strong> of the project, add <em>libgmp.dll.a</em> to the <strong>Additional Dependencies</strong>.
Build your C++ project, and copy <em>libgmp-10.dll</em> to the output <em>bin</em> folder.
Run your application.
</p><p>
See <ahref="https://github.com/MachineCognitis/Math.Gmp.Native/blob/master/Math.Gmp.Native/Dependencies/"target="_blank">ConsoleApplication12.zip</a>
for a sample Visual Studio C++ project.
</p></div><divclass="collapsibleAreaRegion"id="seeAlsoSection"><spanclass="collapsibleRegionTitle"onclick="SectionExpandCollapse('ID5RB')"onkeypress="SectionExpandCollapse_CheckKey('ID5RB', event)"tabindex="0"><imgid="ID5RBToggle"class="collapseToggle"src="../icons/SectionExpanded.png"/>See Also</span></div><divid="ID5RBSection"class="collapsibleSection"><h4class="subHeading">Other Resources</h4><divclass="seeAlsoStyle"><ahref="https://github.com/msys2/msys2/wiki/MSYS2-introduction"target="_blank">MSYS2</a></div><divclass="seeAlsoStyle"><ahref="http://yasm.tortall.net/Download.html"target="_blank">yasm</a></div><divclass="seeAlsoStyle"><ahref="https://gmplib.org/"target="_blank">GNU MP</a></div><divclass="seeAlsoStyle"><ahref="https://github.com/MachineCognitis/Math.Gmp.Native"target="_blank">Math.Gmp.Native on GitHub</a></div></div></div></div><divid="pageFooter"class="pageFooter"/></body></html>