8000 Merge pull request #23298 from seiko2plus/cpp_half_support · numpy/numpy@6f3e1f4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6f3e1f4

Browse files
authored
Merge pull request #23298 from seiko2plus/cpp_half_support
ENH: Extend the functionlty of C++ type `np::Half`
2 parents e90c836 + ea15a57 commit 6f3e1f4

File tree

11 files changed

+1021
-585
lines changed

11 files changed

+1021
-585
lines changed

numpy/core/meson.build

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ _numpyconfig_h = configure_file(
430430
# ----------------------------
431431

432432
staticlib_cflags = []
433+
staticlib_cppflags = []
433434
if cc.get_id() == 'msvc'
434435
# Disable voltbl section for vc142 to allow link using mingw-w64; see:
435436
# https://github.com/matthew-brett/dll_investigation/issues/1#issuecomment-1100468171
@@ -443,6 +444,7 @@ endif
443444
# https://mesonbuild.com/Build-options.html#build-options
444445
if get_option('disable-simd-optimizations')
445446
staticlib_cflags += '-DNPY_DISABLE_OPTIMIZATION'
447+
staticlib_cppflags += '-DNPY_DISABLE_OPTIMIZATION'
446448
endif
447449

448450
npy_math_internal_h = custom_target(
@@ -455,12 +457,13 @@ npymath_sources = [
455457
src_file.process('src/npymath/ieee754.c.src'),
456458
src_file.process('src/npymath/npy_math_complex.c.src'),
457459
npy_math_internal_h,
458-
'src/npymath/halffloat.c',
460+
'src/npymath/halffloat.cpp',
459461
'src/npymath/npy_math.c',
460462
]
461463
npymath_lib = static_library('npymath',
462464
npymath_sources,
463465
c_args: staticlib_cflags,
466+
cpp_args: staticlib_cppflags,
464467
include_directories: ['include', 'src/npymath', 'src/common'],
465468
dependencies: py_dep,
466469
install: true,

numpy/core/setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ def get_mathlib_info(*args):
669669
# join('src', 'npymath', 'ieee754.cpp'),
670670
join('src', 'npymath', 'ieee754.c.src'),
671671
join('src', 'npymath', 'npy_math_complex.c.src'),
672-
join('src', 'npymath', 'halffloat.c'),
672+
join('src', 'npymath', 'halffloat.cpp'),
673673
]
674674

675675
config.add_installed_library('npymath',
@@ -727,7 +727,8 @@ def get_mathlib_info(*args):
727727
join('src', 'common', 'numpyos.h'),
728728
join('src', 'common', 'npy_cpu_dispatch.h'),
729729
join('src', 'common', 'simd', 'simd.h'),
730-
]
730+
join('src', 'common', 'common.hpp'),
731+
]
731732

732733
common_src = [
733734
join('src', 'common', 'array_assign.c'),

numpy/core/src/common/common.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
* The following C++ headers are safe to be used standalone, however,
55
* they are gathered to make it easy for us and for the future need to support PCH.
66
*/
7+
#include "npdef.hpp"
8+
#include "utils.hpp"
79
#include "npstd.hpp"
810
#include "half.hpp"
911
#include "meta.hpp"
12+
#include "float_status.hpp"
1013

1114
#endif // NUMPY_CORE_SRC_COMMON_COMMON_HPP
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#ifndef NUMPY_CORE_SRC_COMMON_FLOAT_STATUS_HPP
2+
#define NUMPY_CORE_SRC_COMMON_FLOAT_STATUS_HPP
3+
4+
#include "npstd.hpp"
5+
6+
#include <fenv.h>
7+
8+
namespace np {
9+
10+
/// @addtogroup cpp_core_utility
11+
/// @{
12+
/**
13+
* Class wraps floating-point environment operations,
14+
* provides lazy access to its functionality.
15+
*/
16+
class FloatStatus {
17+
public:
18+
/*
19+
* According to the C99 standard FE_DIVBYZERO, etc. may not be provided when
20+
* unsupported. In such cases NumPy will not report these correctly, but we
21+
* should still allow compiling (whether tests pass or not).
22+
* By defining them as 0 locally, we make them no-ops. Unlike these defines,
23+
* for example `musl` still defines all of the functions (as no-ops):
24+
* https://git.musl-libc.org/cgit/musl/tree/src/fenv/fenv.c
25+
* and does similar replacement in its tests:
26+
* http://nsz.repo.hu/git/?p=libc-test;a=blob;f=src/common/mtest.h;h=706c1ba23ea8989b17a2f72ed1a919e187c06b6a;hb=HEAD#l30
27+
*/
28+
#ifdef FE_DIVBYZERO
29+
static constexpr int kDivideByZero = FE_DIVBYZERO;
30+
#else
31+
static constexpr int kDivideByZero = 0;
32+
#endif
33+
#ifdef FE_INVALID
34+
static constexpr int kInvalid = FE_INVALID;
35+
#else
36+
static constexpr int kInvalid = 0;
37+
#endif
38+
#ifdef FE_INEXACT
39+
static constexpr int kInexact = FE_INEXACT;
40+
#else
41+
static constexpr int kInexact = 0;
42+
#endif
43+
#ifdef FE_OVERFLOW
44+
static constexpr int kOverflow = FE_OVERFLOW;
45+
#else
46+
static constexpr int kOverflow = 0;
47+
#endif
48+
#ifdef FE_UNDERFLOW
49+
static constexpr int kUnderflow = FE_UNDERFLOW;
50+
#else
51+
static constexpr int kUnderflow = 0;
52+
#endif
53+
static constexpr int kAllExcept = (kDivideByZero | kInvalid | kInexact |
54+
kOverflow | kUnderflow);
55+
56+
FloatStatus(bool clear_on_dst=true)
57+
: clear_on_dst_(clear_on_dst)
58+
{
59+
if constexpr (kAllExcept != 0) {
60+
fpstatus_ = fetestexcept(kAllExcept);
61+
}
62+
else {
63+
fpstatus_ = 0;
64+
}
65+
}
66+
~FloatStatus()
67+
{
68+
if constexpr (kAllExcept != 0) {
69+
if (fpstatus_ != 0 && clear_on_dst_) {
70+
feclearexcept(kAllExcept);
71+
}
72+
}
73+
}
74+
constexpr bool IsDivideByZero() const
75+
{
76+
return (fpstatus_ & kDivideByZero) != 0;
77+
}
78+
constexpr bool IsInexact() const
79+
{
80+
return (fpstatus_ & kInexact) != 0;
81+
}
82+
constexpr bool IsInvalid() const
83+
{
84+
return (fpstatus_ & kInvalid) != 0;
85+
}
86+
constexpr bool IsOverFlow() const
87+
{
88+
return (fpstatus_ & kOverflow) != 0;
89+
}
90+
constexpr bool IsUnderFlow() const
91+
{
92+
return (fpstatus_ & kUnderflow) != 0;
93+
}
94+
static void RaiseDivideByZero()
95+
{
96+
if constexpr (kDivideByZero != 0) {
97+
feraiseexcept(kDivideByZero);
98+
}
99+
}
100+
static void RaiseInexact()
101+
{
102+
if constexpr (kInexact != 0) {
103+
feraiseexcept(kInexact);
104+
}
105+
}
106+
static void RaiseInvalid()
107+
{
108+
if constexpr (kInvalid != 0) {
109+
feraiseexcept(kInvalid);
110+
}
111+
}
112+
static void RaiseOverflow()
113+
{
114+
if constexpr (kOverflow != 0) {
115+
feraiseexcept(kOverflow);
116+
}
117+
}
118+
static void RaiseUnderflow()
119+
{
120+
if constexpr (kUnderflow != 0) {
121+
feraiseexcept(kUnderflow);
122+
}
123+
}
124+
125+
private:
126+
bool clear_on_dst_;
127+
int fpstatus_;
128+
};
129+
130+
/// @} cpp_core_utility
131+
} // namespace np
132+
133+
#endif // NUMPY_CORE_SRC_COMMON_FLOAT_STATUS_HPP
134+

0 commit comments

Comments
 (0)
0