Skip to content

Commit 22cb547

Browse files
author
peng.li24
committed
refactor: namespace svml → detail — prevent direct use by applications
Rename numpy::svml → numpy::detail to follow the standard C++ convention for internal implementation namespaces. Applications should only use the public numpy::* API from core.h — never numpy::detail::exp() etc. Also update svml_bridge.h header comment to explicitly warn against direct use of the detail namespace.
1 parent 95986fd commit 22cb547

3 files changed

Lines changed: 21 additions & 16 deletions

File tree

numpy/core.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,37 +86,37 @@ inline void abs(const T* src, T* dst, size_t n) {
8686
/// numpy.exp(x, /, out=None, *, where=True, ...)
8787
template<typename T>
8888
inline void exp(const T* src, T* dst, size_t n) {
89-
NUMPY_UNROLL4(i, dst[i] = svml::exp(src[i]));
89+
NUMPY_UNROLL4(i, dst[i] = detail::exp(src[i]));
9090
}
9191

9292
/// numpy.log(x, /, out=None, *, where=True, ...)
9393
template<typename T>
9494
inline void log(const T* src, T* dst, size_t n) {
95-
NUMPY_UNROLL4(i, dst[i] = svml::log(src[i]));
95+
NUMPY_UNROLL4(i, dst[i] = detail::log(src[i]));
9696
}
9797

9898
/// numpy.sin(x, /, out=None, *, where=True, ...)
9999
template<typename T>
100100
inline void sin(const T* src, T* dst, size_t n) {
101-
NUMPY_UNROLL4(i, dst[i] = svml::sin(src[i]));
101+
NUMPY_UNROLL4(i, dst[i] = detail::sin(src[i]));
102102
}
103103

104104
/// numpy.cos(x, /, out=None, *, where=True, ...)
105105
template<typename T>
106106
inline void cos(const T* src, T* dst, size_t n) {
107-
NUMPY_UNROLL4(i, dst[i] = svml::cos(src[i]));
107+
NUMPY_UNROLL4(i, dst[i] = detail::cos(src[i]));
108108
}
109109

110110
/// numpy.tan(x, /, out=None, *, where=True, ...)
111111
template<typename T>
112112
inline void tan(const T* src, T* dst, size_t n) {
113-
NUMPY_UNROLL4(i, dst[i] = svml::tan(src[i]));
113+
NUMPY_UNROLL4(i, dst[i] = detail::tan(src[i]));
114114
}
115115

116116
/// numpy.power(x1, x2, /, out=None, *, where=True, ...)
117117
template<typename T>
118118
inline void power(const T* src, T* dst, size_t n, T exponent) {
119-
NUMPY_UNROLL4(i, dst[i] = svml::pow(src[i], exponent));
119+
NUMPY_UNROLL4(i, dst[i] = detail::pow(src[i], exponent));
120120
}
121121

122122
/// numpy.clip(a, a_min, a_max, out=None, **kwargs)
@@ -128,31 +128,31 @@ inline void clip(const T* src, T* dst, size_t n, T min_val, T max_val) {
128128
/// numpy.log10(x, /, out=None, *, where=True, ...)
129129
template<typename T>
130130
inline void log10(const T* src, T* dst, size_t n) {
131-
NUMPY_UNROLL4(i, dst[i] = svml::log10(src[i]));
131+
NUMPY_UNROLL4(i, dst[i] = detail::log10(src[i]));
132132
}
133133

134134
/// numpy.log2(x, /, out=None, *, where=True, ...)
135135
template<typename T>
136136
inline void log2(const T* src, T* dst, size_t n) {
137-
NUMPY_UNROLL4(i, dst[i] = svml::log2(src[i]));
137+
NUMPY_UNROLL4(i, dst[i] = detail::log2(src[i]));
138138
}
139139

140140
/// numpy.arcsin(x, /, out=None, *, where=True, ...)
141141
template<typename T>
142142
inline void arcsin(const T* src, T* dst, size_t n) {
143-
NUMPY_UNROLL4(i, dst[i] = svml::asin(src[i]));
143+
NUMPY_UNROLL4(i, dst[i] = detail::asin(src[i]));
144144
}
145145

146146
/// numpy.arccos(x, /, out=None, *, where=True, ...)
147147
template<typename T>
148148
inline void arccos(const T* src, T* dst, size_t n) {
149-
NUMPY_UNROLL4(i, dst[i] = svml::acos(src[i]));
149+
NUMPY_UNROLL4(i, dst[i] = detail::acos(src[i]));
150150
}
151151

152152
/// numpy.arctan(x, /, out=None, *, where=True, ...)
153153
template<typename T>
154154
inline void arctan(const T* src, T* dst, size_t n) {
155-
NUMPY_UNROLL4(i, dst[i] = svml::atan(src[i]));
155+
NUMPY_UNROLL4(i, dst[i] = detail::atan(src[i]));
156156
}
157157

158158
/// numpy.round(a, decimals=0, out=None)
@@ -414,12 +414,12 @@ inline void isfinite(const T* src, bool* dst, size_t n) {
414414
/// numpy.arctan2(x1, x2, /, out=None, *, where=True, ...) — array-array
415415
template<typename T>
416416
inline void arctan2_array(const T* a, const T* b, T* dst, size_t n) {
417-
NUMPY_UNROLL4(i, dst[i] = svml::atan2(a[i], b[i]));
417+
NUMPY_UNROLL4(i, dst[i] = detail::atan2(a[i], b[i]));
418418
}
419419
/// numpy.arctan2(x1, x2, /, out=None, *, where=True, ...) — array-scalar
420420
template<typename T>
421421
inline void arctan2_scalar(const T* src, T* dst, size_t n, T b) {
422-
NUMPY_UNROLL4(i, dst[i] = svml::atan2(src[i], b));
422+
NUMPY_UNROLL4(i, dst[i] = detail::atan2(src[i], b));
423423
}
424424
/// numpy.maximum(x1, x2, /, out=None, *, where=True, ...) — array-array
425425
template<typename T>

numpy/svml_bridge.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// INTERNAL HEADER — DO NOT INCLUDE DIRECTLY.
22
// Use #include "numpy/core.h" which pulls this in automatically.
33
//
4+
// All functions live in numpy::detail — do not call numpy::detail::exp()
5+
// directly. Use numpy::exp() from core.h.
6+
//
47
// SVML/npy bridge — bit-exact math on every x86_64 architecture.
58
//
69
// numpy uses different math implementations depending on CPU features:
@@ -25,7 +28,9 @@
2528
#endif
2629

2730
namespace numpy {
28-
namespace svml {
31+
namespace detail {
32+
// Internal dispatch namespace — use numpy::exp() etc., not numpy::detail::exp().
33+
// All math functions are resolved at runtime from numpy's _multiarray_umath.so.
2934

3035
inline void* g_svml_handle = nullptr;
3136

@@ -318,5 +323,5 @@ NUMPY_SVML_D1(sqrt)
318323
template<typename T> inline T pow(T x, T e) { return svml_impl<T>::pow(x, e); }
319324
template<typename T> inline T atan2(T y, T x) { return svml_impl<T>::atan2(y, x); }
320325

321-
} // namespace svml
326+
} // namespace detail
322327
} // namespace numpy

tests/module.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ PYBIND11_MODULE(numpycpp, m) {
4747
try {
4848
py::module_ np_core = py::module_::import("numpy.core._multiarray_umath");
4949
std::string umath_path = np_core.attr("__file__").cast<std::string>();
50-
numpy::svml::bridge_init(umath_path.c_str());
50+
numpy::detail::bridge_init(umath_path.c_str());
5151
} catch (...) {
5252
// Fall back: SVML → libm
5353
}

0 commit comments

Comments
 (0)