1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=0.4"/>
<meta name="google" content="notranslate" />
<link rel="canonical" href="https://sleef.org/compile.xhtml" />
<link rel="icon" href="favicon.png" />
<link rel="stylesheet" type="text/css" href="texlike.css"/>
<link rel="stylesheet" type="text/css" href="sleef.css"/>
<title>SLEEF - Compiling and installing the library</title>
</head>
<body translate="no" class="notranslate">
<h1>SLEEF - Compiling and installing the library</h1>
<h2>Table of contents</h2>
<ul class="none" style="font-family: arial, sansserif; padding-left: 0.5cm;">
<li><a class="underlined" href="index.xhtml">Introduction</a></li>
<li> </li>
<li><a class="underlined" href="compile.xhtml">Compiling and installing the library</a></li>
<ul class="disc">
<li><a href="#preliminaries">Preliminaries</a></li>
<li><a href="#quickstart">Quick start</a></li>
<li><a href="#linux">Compiling and installing the library on Linux</a></li>
<li><a href="#MSVC">Compiling the library with Microsoft Visual C++</a></li>
<li><a href="#hello">Compiling and running "Hello SLEEF"</a></li>
<li><a href="#import">Importing SLEEF into your project</a></li>
<li><a href="#cross">Cross compilation for Linux</a></li>
</ul>
<li> </li>
<li><a class="underlined" href="purec.xhtml">Math library reference</a></li>
<li><a class="underlined" href="quad.xhtml"> Quad-precision math library reference</a></li>
<li><a class="underlined" href="dft.xhtml">DFT library reference</a></li>
<li><a class="underlined" href="misc.xhtml">Other tools included in the package</a></li>
<li><a class="underlined" href="benchmark.xhtml">Benchmark results</a></li>
<li><a class="underlined" href="additional.xhtml">Additional notes</a></li>
</ul>
<h2 id="preliminaries">Preliminaries</h2>
<p class="noindent">
In order to build SLEEF, you need <a class="underlined"
href="http://www.cmake.org/">CMake</a>, which is an open-source and
cross-platform building tool. In order to test the library, it is
better to have <a class="underlined" href="http://www.mpfr.org/">the
GNU MPFR Library</a>, <a class="underlined"
href="https://wiki.openssl.org/index.php/Libssl_API">Libssl</a> and
<a class="underlined" href="http://www.fftw.org/">FFTW</a>.
</p>
<p>
CMake works by allowing the developer to specify build parameters and
rules in a simple text file that cmake then processes to generate
project files for the actual native build tools (e.g. UNIX Makefiles,
Microsoft Visual Studio, Apple XCode, etc). If you are not already
familiar with cmake, please refer to the
<a class="underlined" href="https://cmake.org/documentation/">official documentation</a>
or
the <a class="underlined" href="https://gitlab.kitware.com/cmake/community/-/wikis/home">basic
introductions in the wiki</a>.
</p>
<h2 id="quickstart">Quick start</h2>
<p class="noindent">
You will find quick start instructions in the sources or via GitHub in the
<a class="underlined" href="https://github.com/shibatch/sleef/blob/master/README.adoc">README.adoc</a>
file.
</p>
<h2 id="linux">Compiling and installing the library on Linux</h2>
<p class="noindent">
In order to build the library, you may want to install OpenMP (optional).
In order to test the library, you need to install libmpfr, libssl and
libfftw3 (optional). Availability of these libraries are checked upon
execution of cmake.
Please change the directory to sleef-3.X and run the following commands.
<pre class="command">$ sudo apt-get install libmpfr-dev libssl-dev libfftw3-dev
$ cmake -S . -B build/
$ cmake --build build/ --clean-first -j `nproc`
$ ctest --test-dir build/ -j `nproc`
$ sudo cmake --install build/ --prefix /path/to/install/dir
</pre>
</p>
<p>
In order to uninstall the libraries and headers, run the following command.
</p>
<pre class="command" style="margin-top: 1em;">$ sudo xargs rm -v < build/install_manifest.txt</pre>
<h3 id="lto">Building the library with LTO support</h3>
<p class="noindent">
You can build the library with <a class="underlined"
href="additional.xhtml#lto">link time opimization(LTO)</a> support
with the following commands. Note that you can only build static
libraries with LTO support. You also have to use the same compiler
with the same version to build the library and other source codes.
</p>
<pre class="command">$ CC=gcc CXX=g++ cmake -DBUILD_SHARED_LIBS=FALSE -DSLEEF_ENABLE_LTO=TRUE ..</pre>
<p>
In order to build the library with thinLTO support with clang, you
need to specify LLVM AR command that exactly corresponds to the
clang compiler.
</p>
<pre class="command">$ CC=clang-19 CXX=clang++-19 cmake -DBUILD_SHARED_LIBS=FALSE -DSLEEF_ENABLE_LTO=TRUE -DSLEEF_LLVM_AR_COMMAND=llvm-ar-19 -DCMAKE_EXE_LINKER_FLAGS=-fuse-ld=lld-19 ..</pre>
<h3 id="inline">Building the header files for inlining the whole SLEEF functions</h3>
<p class="noindent">
<a class="underlined" href="additional.xhtml#inline">Header files
for inlining the whole SLEEF functions</a> can be built with the
following commands. With these header files, it may be easier to
inline the whole SLEEF functions than using LTO. You have to specify
"-ffp-contract=off" compiler option when compiling a source code
that includes one of these header files.
</p>
<pre class="command">$ cmake -DSLEEF_BUILD_INLINE_HEADERS=TRUE ..</pre>
<h2 id="MSVC">Compiling the library with Microsoft Visual C++</h2>
<p class="noindent">
Open developer command prompt for VS2022 and change directory to
sleef-3.X. You can use the accompanying batch file named
winbuild-msvc.bat to build the library. The arguments of this batch
file are passed to cmake.
</p>
<p>
Below is an example of commands for building SLEEF with Visual
Studio.
</p>
<pre class="command">D:\sleef-3.X> winbuild-msvc -DCMAKE_BUILD_TYPE=Release -DSLEEF_BUILD_DFT=True -DSLEEF_BUILD_QUAD=True
</pre>
<h3 id="cow">Compiling the library with Clang on Windows</h3>
<p class="noindent">
Install ninja and clang via VS2022 installer. Below is an example
of commands for building SLEEF with Clang on Windows.
</p>
<pre class="command">D:\sleef-3.X> winbuild-clang -DCMAKE_BUILD_TYPE=Release -DSLEEF_BUILD_DFT=True -DSLEEF_BUILD_QUAD=True
</pre>
<p>
You can choose your own installation of LLVM executable by setting
CLANGINSTALLDIR environment variable.
</p>
<pre class="command">D:\sleef-3.X> set CLANGINSTALLDIR=C:\Program Files\LLVM
D:\sleef-3.X> winbuild-clang -DCMAKE_BUILD_TYPE=Release -DSLEEF_BUILD_DFT=True -DSLEEF_BUILD_QUAD=True
</pre>
<h2 id="hello">Compiling and running "Hello SLEEF"</h2>
<p class="noindent">
Now, let's try compiling the <a class="underlined"
href="hellox86.c">source code shown in Fig. 2.1</a>.
</p>
<pre class="code">
<code>#include <stdio.h></code>
<code>#include <x86intrin.h></code>
<code>#include <sleef.h></code>
<code></code>
<code>int main(int argc, char **argv) {</code>
<code> double a[] = {2, 10};</code>
<code> double b[] = {3, 20};</code>
<code></code>
<code> __m128d va, vb, vc;</code>
<code> </code>
<code> va = _mm_loadu_pd(a);</code>
<code> vb = _mm_loadu_pd(b);</code>
<code></code>
<code> vc = Sleef_powd2_u10(va, vb);</code>
<code></code>
<code> double c[2];</code>
<code></code>
<code> _mm_storeu_pd(c, vc);</code>
<code></code>
<code> printf("pow(%g, %g) = %g\n", a[0], b[0], c[0]);</code>
<code> printf("pow(%g, %g) = %g\n", a[1], b[1], c[1]);</code>
<code>}</code>
</pre>
<p style="text-align:center;">
Fig. 2.1: <a href="hellox86.c">Source code for testing</a>
</p>
<p style="margin-top: 2cm;">
Fig.2.2 shows typical commands for compiling and executing the hello
code on Linux computers.
</p>
<pre class="command" style="margin-top: 2.0em; margin-bottom: 0.5cm;">$ gcc hellox86.c -o hellox86 -lsleef
$ ./hellox86
pow(2, 3) = 8
pow(10, 20) = 1e+20
$ █</pre>
<p style="text-align:center;">
Fig. 2.2: Commands for compiling and executing hellox86.c
</p>
<p style="margin-top: 2cm;">
You may need to set LD_LIBRARY_PATH environment variable
appropriately. If you are trying to execute the program on Mac OSX
or Windows, try copying the DLLs to the current directory.
</p>
<h2 id="import">Importing SLEEF into your project</h2>
<p class="noindent">
Below is an example <a class="underlined"
href="CMakeLists.txt">CMakeLists.txt</a> for compiling the above
hellox86.c. CMake will automatically download SLEEF from GitHub
repository, and thus there is no need to include SLEEF in your
software package. If you prefer importing SLEEF as a submodule in
git, you can use SOURCE_DIR option instead of GIT_REPOSITORY option
for ExternalProject_Add.
</p>
<pre class="code">
<code>cmake_minimum_required(VERSION 3.5.1)</code>
<code>include(ExternalProject)</code>
<code>find_package(Git REQUIRED)</code>
<code></code>
<code>ExternalProject_Add(libsleef</code>
<code> GIT_REPOSITORY https://github.com/shibatch/sleef</code>
<code> CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/contrib</code>
<code>)</code>
<code></code>
<code>include_directories(${CMAKE_BINARY_DIR}/contrib/include)</code>
<code>link_directories(${CMAKE_BINARY_DIR}/contrib/lib)</code>
<code></code>
<code>add_executable(hellox86 hellox86.c)</code>
<code>add_dependencies(hellox86 libsleef)</code>
<code>target_link_libraries(hellox86 sleef)</code>
</pre>
<p style="text-align:center;">
Fig. 2.3: <a href="CMakeLists.txt">Example CMakeLists.txt</a>
</p>
<h2 id="cross">Cross compilation for Linux</h2>
<p>
Below, we explain two methods for cross-compiling SLEEF. Both rely on
the toolchain files provided under toolchains directory. Below are
examples of cross-compiling SLEEF for AArch64 on a platform with
x86_64 Linux.
</p>
<h3 id="method1">Method 1</h3>
<p>
Please run the following from the root directory of SLEEF:
</p>
<br/>
<p>
1. First, compile the native SLEEF.
</p>
<pre class="command" style="margin-top: 2.0em; margin-bottom: 0.5cm;">$ cmake -S . -B build-native
$ cmake --build build-native -j --clean-first
</pre>
<p>
2. Cross-compile the target platform's SLEEF.
</p>
<pre class="command" style="margin-top: 2.0em; margin-bottom: 0.5cm;">$ cmake -DCMAKE_TOOLCHAIN_FILE=./toolchains/aarch64-gcc.cmake -DNATIVE_BUILD_DIR=$(pwd)/build-native/ -S . -B build
$ cmake --build build -j --clean-first
</pre>
<h3 id="method2">Method 2</h3>
<p>
If QEMU is available on your environement, there is no need to compile
the native SLEEF. Please run the following from the root directory of
SLEEF.
</p>
<br/>
<p>
1. Install qemu on Ubuntu.
</p>
<pre class="command" style="margin-top: 2.0em; margin-bottom: 0.5cm;">$ sudo apt install -y qemu-user-static binfmt-support
</pre>
<p>
2. Set the environment variable.
</p>
<pre class="command" style="margin-top: 2.0em; margin-bottom: 0.5cm;">$ export SLEEF_TARGET_EXEC_USE_QEMU=ON
</pre>
<p>
3. Set the dynamic linker/loader path.
</p>
<pre class="command" style="margin-top: 2.0em; margin-bottom: 0.5cm;">$ export QEMU_LD_PREFIX=/usr/aarch64-linux-gnu/
</pre>
<p>
4. Cross-compile the target platform's SLEEF.
</p>
<pre class="command" style="margin-top: 2.0em; margin-bottom: 0.5cm;">$ cmake -DCMAKE_TOOLCHAIN_FILE=./toolchains/aarch64-gcc.cmake -S . -B build
$ cmake --build build -j --clean-first
</pre>
<p class="footer">
Copyright © 2010-2025 SLEEF Project, Naoki Shibata and contributors.<br/>
SLEEF is open-source software and is distributed under the Boost Software License, Version 1.0.
</p>
<script type="text/javascript">
var sc_project=13098265;
var sc_invisible=1;
var sc_security="518de45e";
</script>
<script type="text/javascript"
src="https://www.statcounter.com/counter/counter.js"
async="async"></script>
<noscript><div class="statcounter"><a title="Web Analytics"
href="https://statcounter.com/" target="_blank"><img
class="statcounter"
src="https://c.statcounter.com/13098265/0/518de45e/1/"
alt="Web Analytics"
referrerPolicy="no-referrer-when-downgrade"></img></a></div></noscript>
</body>
</html>
|