Skip to content

Commit 040ed2d

Browse files
authored
BLD: fix math func feature checks, fix FreeBSD build, add CI job (#24876)
* BLD: fix incorrect feature checks for mandatory math functions Should fix the build on FreeBSD and other OSes that are not C99-compliant. Closes gh-24873 * CI: add a FreeBSD job on Cirrus CI * BUG: define `_npy_scaled_cexpl` when ccoshl/csinhl are missing This was a regression in the 1.24.x branch, after a lot of churn in this file. In 1.22.x/1.23.x, the conditional is the same as in this fix. * TST: avoid failures for FPE errors/warnings in `abs` on BSDs
1 parent 195d85b commit 040ed2d

7 files changed

Lines changed: 85 additions & 19 deletions

File tree

‎numpy/core/meson.build‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,32 @@ endforeach
148148
# Mandatory functions: if not found, fail the build
149149
# Some of these can still be blocklisted if the C99 implementation
150150
# is buggy, see numpy/core/src/common/npy_config.h
151-
mandatory_funcs = [
151+
mandatory_math_funcs = [
152152
'sin', 'cos', 'tan', 'sinh', 'cosh', 'tanh', 'fabs',
153153
'floor', 'ceil', 'sqrt', 'log10', 'log', 'exp', 'asin',
154154
'acos', 'atan', 'fmod', 'modf', 'frexp', 'ldexp',
155155
'expm1', 'log1p', 'acosh', 'asinh', 'atanh',
156-
'rint', 'trunc', 'exp2',
157-
'copysign', 'nextafter', 'strtoll', 'strtoull', 'cbrt',
156+
'rint', 'trunc', 'exp2', 'copysign', 'nextafter', 'cbrt',
158157
'log2', 'pow', 'hypot', 'atan2',
159-
'csin', 'csinh', 'ccos', 'ccosh', 'ctan', 'ctanh',
160-
'creal', 'cimag', 'conj'
161158
]
162-
foreach func: mandatory_funcs
163-
if not cc.has_function(func)
164-
error('Function `{func}` not found')
159+
foreach func: mandatory_math_funcs
160+
if not cc.has_function(func, prefix: '#include <math.h>', dependencies: m_dep)
161+
error(f'Function `@func@` not found')
162+
endif
163+
endforeach
164+
165+
mandatory_complex_math_funcs = [
166+
'csin', 'csinh', 'ccos', 'ccosh', 'ctan', 'ctanh', 'creal', 'cimag', 'conj'
167+
]
168+
foreach func: mandatory_complex_math_funcs
169+
if not cc.has_function(func, prefix: '#include <complex.h>', dependencies: m_dep)
170+
error(f'Function `@func@` not found')
171+
endif
172+
endforeach
173+
174+
foreach func: ['strtoll', 'strtoull']
175+
if not cc.has_function(func, prefix: '#include <stdlib.h>')
176+
error(f'Function `@func@` not found')
165177
endif
166178
endforeach
167179

@@ -175,13 +187,13 @@ c99_complex_funcs = [
175187
foreach func: c99_complex_funcs
176188
func_single = func + 'f'
177189
func_longdouble = func + 'l'
178-
if cc.has_function(func)
190+
if cc.has_function(func, prefix: '#include <complex.h>', dependencies: m_dep)
179191
cdata.set10('HAVE_' + func.to_upper(), true)
180192
endif
181-
if cc.has_function(func_single)
193+
if cc.has_function(func_single, prefix: '#include <complex.h>', dependencies: m_dep)
182194
cdata.set10('HAVE_' + func_single.to_upper(), true)
183195
endif
184-
if cc.has_function(func_longdouble)
196+
if cc.has_function(func_longdouble, prefix: '#include <complex.h>', dependencies: m_dep)
185197
cdata.set10('HAVE_' + func_longdouble.to_upper(), true)
186198
endif
187199
endforeach
@@ -190,7 +202,7 @@ endforeach
190202
# libnpymath as a C99 compat layer, these may still be relevant.
191203
c99_macros = ['isfinite', 'isinf', 'isnan', 'signbit']
192204
foreach macro: c99_macros
193-
if cc.has_function(macro)
205+
if cc.has_function(macro, prefix: '#include <math.h>', dependencies: m_dep)
194206
cdata.set10('NPY_HAVE_DECL_' + macro.to_upper(), true)
195207
if not cc.has_header_symbol('Python.h', macro, dependencies: py_dep)
196208
# Add in config.h as well, except if it will clash with Python's config.h content

‎numpy/core/src/npymath/npy_math_complex.c.src‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ npy_carg@c@(@ctype@ z)
163163
#define SCALED_CEXP_LOWERL 11357.216553474703895L
164164
#define SCALED_CEXP_UPPERL 22756.021937783004509L
165165

166-
#if !defined(HAVE_CEXP@C@)
166+
#if !defined(HAVE_CSINH@C@) || \
167+
!defined(HAVE_CCOSH@C@) || \
168+
!defined(HAVE_CEXP@C@)
167169

168170
static
169171
@ctype@

‎numpy/core/tests/test_multiarray.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9706,9 +9706,12 @@ def test_ragged_comparison_fails(op):
97069706
def test_npymath_complex(fun, npfun, x, y, test_dtype):
97079707
# Smoketest npymath functions
97089708
z = test_dtype(complex(x, y))
9709-
got = fun(z)
9710-
expected = npfun(z)
9711-
assert_allclose(got, expected)
9709+
with np.errstate(invalid='ignore'):
9710+
# Fallback implementations may emit a warning for +-inf (see gh-24876):
9711+
# RuntimeWarning: invalid value encountered in absolute
9712+
got = fun(z)
9713+
expected = npfun(z)
9714+
assert_allclose(got, expected)
97129715

97139716

97149717
def test_npymath_real():

‎numpy/core/tests/test_numeric.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,11 @@ def assert_op_raises_fpe(self, fpeerr, flop, sc1, sc2):
916916
@pytest.mark.skipif(IS_WASM, reason="no wasm fp exception support")
917917
@pytest.mark.parametrize("typecode", np.typecodes["AllFloat"])
918918
def test_floating_exceptions(self, typecode):
919+
if 'bsd' in sys.platform and typecode in 'gG':
920+
pytest.skip(reason="Fallback impl for (c)longdouble may not raise "
921+
"FPE errors as expected on BSD OSes, "
922+
"see gh-24876, gh-23379")
923+
919924
# Test basic arithmetic function errors
920925
with np.errstate(all='raise'):
921926
ftype = obj2sctype(typecode)

‎numpy/core/tests/test_umath.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,8 @@ def test_sinh(self):
16451645
np.array(1200.0, dtype='d'))
16461646

16471647
@pytest.mark.skipif(IS_WASM, reason="fp errors don't work in wasm")
1648+
@pytest.mark.skipif('bsd' in sys.platform,
1649+
reason="fallback implementation may not raise, see gh-2487")
16481650
def test_cosh(self):
16491651
in_ = [np.nan, -np.nan, np.inf, -np.inf]
16501652
out = [np.nan, np.nan, np.inf, np.inf]

‎numpy/core/tests/test_umath_complex.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,8 @@ def test_array(self, stride, astype):
576576
complex(0., 0.),
577577
complex(np.nan, np.nan),
578578
complex(np.nan, np.nan)], dtype=astype)
579-
assert_equal(np.abs(arr[::stride]), abs_true[::stride])
580579
with np.errstate(invalid='ignore'):
580+
assert_equal(np.abs(arr[::stride]), abs_true[::stride])
581581
assert_equal(np.square(arr[::stride]), sq_true[::stride])
582582

583583
class TestComplexAbsoluteAVX:

‎tools/ci/cirrus_arm.yml‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ macos_arm64_test_task:
9696
9797
RUNNER_OS="macOS"
9898
SDKROOT=/Applications/Xcode-14.0.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk
99-
99+
100100
# NOTE: OpenBLAS is not used in this job; if that's done in the future, ensure
101101
# PKG_CONFIG_PATH points to the directory containing the openblas.pc file
102102
# that's installed with the cibw_before_build.sh command.
@@ -110,8 +110,50 @@ macos_arm64_test_task:
110110
111111
pip install -r build_requirements.txt
112112
pip install pytest pytest-xdist hypothesis typing_extensions
113-
113+
114114
spin build -- -Dallow-noblas=true
115115
spin test -j auto
116116
117117
ccache -s
118+
119+
120+
freebsd_test_task:
121+
use_compute_credits: $CIRRUS_USER_COLLABORATOR == 'true'
122+
compute_engine_instance:
123+
image_project: freebsd-org-cloud-dev
124+
image: family/freebsd-13-2
125+
platform: freebsd
126+
cpu: 1
127+
memory: 4G
128+
129+
install_devtools_script: |
130+
pkg install -y git bash ninja ccache
131+
132+
<<: *MODIFIED_CLONE
133+
134+
ccache_cache:
135+
folder: .ccache
136+
populate_script:
137+
- mkdir -p .ccache
138+
fingerprint_key: ccache-freebsd
139+
140+
prepare_env_script: |
141+
# Create a venv (the `source` command needs bash, not the default sh shell)
142+
chsh -s /usr/local/bin/bash
143+
python -m venv .venv
144+
source .venv/bin/activate
145+
# Minimal build and test requirements
146+
python -m pip install -U pip
147+
python -m pip install meson-python Cython pytest hypothesis
148+
149+
build_script: |
150+
chsh -s /usr/local/bin/bash
151+
source .venv/bin/activate
152+
python -m pip install . --no-build-isolation -v -Csetup-args="-Dallow-noblas=true"
153+
154+
test_script: |
155+
chsh -s /usr/local/bin/bash
156+
source .venv/bin/activate
157+
cd tools
158+
python -m pytest --pyargs numpy -m "not slow"
159+
ccache -s

0 commit comments

Comments
 (0)