Skip to content

Commit fcafb65

Browse files
authored
Merge pull request numpy#22538 from rgommers/getinfo-npymath
DOC: update libnpymath docs on its status and how to consume it
2 parents 2081f29 + ec37b2b commit fcafb65

File tree

2 files changed

+66
-37
lines changed

2 files changed

+66
-37
lines changed

doc/source/reference/c-api/coremath.rst

+63-35
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ NumPy core libraries
33

44
.. sectionauthor:: David Cournapeau
55

6-
.. versionadded:: 1.3.0
7-
86
Starting from numpy 1.3.0, we are working on separating the pure C,
97
"computational" code from the python dependent code. The goal is twofolds:
108
making the code cleaner, and enabling code reuse by other extensions outside
@@ -16,10 +14,19 @@ NumPy core math library
1614
The numpy core math library ('npymath') is a first step in this direction. This
1715
library contains most math-related C99 functionality, which can be used on
1816
platforms where C99 is not well supported. The core math functions have the
19-
same API as the C99 ones, except for the npy_* prefix.
17+
same API as the C99 ones, except for the ``npy_*`` prefix.
18+
19+
The available functions are defined in ``<numpy/npy_math.h>`` - please refer to
20+
this header when in doubt.
21+
22+
.. note::
2023

21-
The available functions are defined in <numpy/npy_math.h> - please refer to this header when
22-
in doubt.
24+
An effort is underway to make ``npymath`` smaller (since C99 compatibility
25+
of compilers has improved over time) and more easily vendorable or usable as
26+
a header-only dependency. That will avoid problems with shipping a static
27+
library built with a compiler which may not match the compiler used by a
28+
downstream package or end user. See
29+
`gh-20880 <https://github.com/numpy/numpy/issues/20880>`__ for details.
2330

2431
Floating point classification
2532
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -75,8 +82,6 @@ Floating point classification
7582
as y. Works for any value, including inf and nan. Single and extended
7683
precisions are available with suffix f and l.
7784

78-
.. versionadded:: 1.4.0
79-
8085
Useful math constants
8186
~~~~~~~~~~~~~~~~~~~~~
8287

@@ -140,41 +145,29 @@ Those can be useful for precise floating point comparison.
140145
floating point value from x in the direction of y. Single and extended
141146
precisions are available with suffix f and l.
142147
143-
.. versionadded:: 1.4.0
144-
145148
.. c:function:: double npy_spacing(double x)
146149
147150
This is a function equivalent to Fortran intrinsic. Return distance between
148151
x and next representable floating point value from x, e.g. spacing(1) ==
149152
eps. spacing of nan and +/- inf return nan. Single and extended precisions
150153
are available with suffix f and l.
151154
152-
.. versionadded:: 1.4.0
153-
154155
.. c:function:: void npy_set_floatstatus_divbyzero()
155156
156157
Set the divide by zero floating point exception
157158
158-
.. versionadded:: 1.6.0
159-
160159
.. c:function:: void npy_set_floatstatus_overflow()
161160
162161
Set the overflow floating point exception
163162
164-
.. versionadded:: 1.6.0
165-
166163
.. c:function:: void npy_set_floatstatus_underflow()
167164
168165
Set the underflow floating point exception
169166
170-
.. versionadded:: 1.6.0
171-
172167
.. c:function:: void npy_set_floatstatus_invalid()
173168
174169
Set the invalid floating point exception
175170
176-
.. versionadded:: 1.6.0
177-
178171
.. c:function:: int npy_get_floatstatus()
179172
180173
Get floating point status. Returns a bitmask with following possible flags:
@@ -188,8 +181,6 @@ Those can be useful for precise floating point comparison.
188181
aggressive compiler optimizations reordering the call relative to
189182
the code setting the status, which could lead to incorrect results.
190183
191-
.. versionadded:: 1.9.0
192-
193184
.. c:function:: int npy_get_floatstatus_barrier(char*)
194185
195186
Get floating point status. A pointer to a local variable is passed in to
@@ -214,8 +205,6 @@ Those can be useful for precise floating point comparison.
214205
prevents aggressive compiler optimizations reordering the call relative to
215206
the code setting the status, which could lead to incorrect results.
216207
217-
.. versionadded:: 1.9.0
218-
219208
.. c:function:: int npy_clear_floatstatus_barrier(char*)
220209
221210
Clears the floating point status. A pointer to a local variable is passed in to
@@ -227,11 +216,10 @@ Those can be useful for precise floating point comparison.
227216
Complex functions
228217
~~~~~~~~~~~~~~~~~
229218
230-
.. versionadded:: 1.4.0
231-
232219
C99-like complex functions have been added. Those can be used if you wish to
233220
implement portable C extensions. Since we still support platforms without C99
234-
complex type, you need to restrict to C90-compatible syntax, e.g.:
221+
complex type (most importantly Windows, where MSVC doesn't support C99 complex
222+
types as of Nov 2022), you need to restrict to C90-compatible syntax, e.g.:
235223
236224
.. code-block:: c
237225
@@ -241,13 +229,31 @@ complex type, you need to restrict to C90-compatible syntax, e.g.:
241229
242230
b = npy_log(a);
243231
232+
.. _linking-npymath:
233+
244234
Linking against the core math library in an extension
245235
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
246236

247-
.. versionadded:: 1.4.0
237+
To use the core math library that NumPy ships as a static library in your own
238+
Python extension, you need to add the npymath compile and link options to your
239+
extension. The exact steps to take will depend on the build system you are using.
240+
The generic steps to take are:
241+
242+
1. Add the numpy include directory (= the value of ``np.get_include()``) to
243+
your include directories,
244+
2. The ``npymath`` static library resides in the ``lib`` directory right next
245+
to numpy's include directory (i.e., ``pathlib.Path(np.get_include()) / '..'
246+
/ 'lib'``). Add that to your library search directories,
247+
3. Link with ``libnpymath`` and ``libm``.
248248

249-
To use the core math library in your own extension, you need to add the npymath
250-
compile and link options to your extension in your setup.py:
249+
.. note::
250+
251+
Keep in mind that when you are cross compiling, you must use the ``numpy``
252+
for the platform you are building for, not the native one for the build
253+
machine. Otherwise you pick up a static library built for the wrong
254+
architecture.
255+
256+
When you build with ``numpy.distutils`` (deprecated), then use this in your ``setup.py``:
251257

252258
.. hidden in a comment so as to be included in refguide but not rendered documentation
253259
>>> import numpy.distutils.misc_util
@@ -258,15 +264,37 @@ compile and link options to your extension in your setup.py:
258264
>>> info = get_info('npymath')
259265
>>> _ = config.add_extension('foo', sources=['foo.c'], extra_info=info)
260266

261-
In other words, the usage of info is exactly the same as when using blas_info
262-
and co.
267+
In other words, the usage of ``info`` is exactly the same as when using
268+
``blas_info`` and co.
269+
270+
When you are building with `Meson <https://mesonbuild.com>`__, use::
271+
272+
# Note that this will get easier in the future, when Meson has
273+
# support for numpy built in; most of this can then be replaced
274+
# by `dependency('numpy')`.
275+
incdir_numpy = run_command(py3,
276+
[
277+
'-c',
278+
'import os; os.chdir(".."); import numpy; print(numpy.get_include())'
279+
],
280+
check: true
281+
).stdout().strip()
282+
283+
inc_np = include_directories(incdir_numpy)
284+
285+
cc = meson.get_compiler('c')
286+
npymath_path = incdir_numpy / '..' / 'lib'
287+
npymath_lib = cc.find_library('npymath', dirs: npymath_path)
288+
289+
py3.extension_module('module_name',
290+
...
291+
include_directories: inc_np,
292+
dependencies: [npymath_lib],
263293

264294
Half-precision functions
265295
~~~~~~~~~~~~~~~~~~~~~~~~
266296

267-
.. versionadded:: 1.6.0
268-
269-
The header file <numpy/halffloat.h> provides functions to work with
297+
The header file ``<numpy/halffloat.h>`` provides functions to work with
270298
IEEE 754-2008 16-bit floating point values. While this format is
271299
not typically used for numerical computations, it is useful for
272300
storing values which require floating point but do not need much precision.
@@ -281,7 +309,7 @@ between the different signed zeros, you will get -0.0 != 0.0
281309
(0x8000 != 0x0000), which is incorrect.
282310

283311
For these reasons, NumPy provides an API to work with npy_half values
284-
accessible by including <numpy/halffloat.h> and linking to 'npymath'.
312+
accessible by including ``<numpy/halffloat.h>`` and linking to ``npymath``.
285313
For functions that are not provided directly, such as the arithmetic
286314
operations, the preferred method is to convert to float
287315
or double and back again, as in the following example.

doc/source/reference/random/c-api.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ C API for random
88
Access to various distributions below is available via Cython or C-wrapper
99
libraries like CFFI. All the functions accept a :c:type:`bitgen_t` as their
1010
first argument. To access these from Cython or C, you must link with the
11-
``npyrandom`` library which is part of the NumPy distribution, located in
12-
``numpy/random/lib``.
11+
``npyrandom`` static library which is part of the NumPy distribution, located
12+
in ``numpy/random/lib``. Note that you must *also* link with ``npymath``,
13+
see :ref:`linking-npymath`.
1314

1415

1516
.. c:type:: bitgen_t

0 commit comments

Comments
 (0)