8000 ENH: Extend the functionlty of C++ type `np::Half` by seiko2plus · Pull Request #23298 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Extend the functionlty of C++ type np::Half #23298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion numpy/core/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ _numpyconfig_h = configure_file(
# ----------------------------

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

npy_math_internal_h = custom_target(
Expand All @@ -455,12 +457,13 @@ npymath_sources = [
src_file.process('src/npymath/ieee754.c.src'),
src_file.process('src/npymath/npy_math_complex.c.src'),
npy_math_internal_h,
'src/npymath/halffloat.c',
'src/npymath/halffloat.cpp',
'src/npymath/npy_math.c',
]
npymath_lib = static_library('npymath',
npymath_sources,
c_args: staticlib_cflags,
cpp_args: staticlib_cppflags,
include_directories: ['include', 'src/npymath', 'src/common'],
dependencies: py_dep,
install: true,
Expand Down
5 changes: 3 additions & 2 deletions numpy/core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ def get_mathlib_info(*args):
# join('src', 'npymath', 'ieee754.cpp'),
join('src', 'npymath', 'ieee754.c.src'),
join('src', 'npymath', 'npy_math_complex.c.src'),
join('src', 'npymath', 'halffloat.c'),
join('src', 'npymath', 'halffloat.cpp'),
]

config.add_installed_library('npymath',
Expand Down Expand Up @@ -727,7 +727,8 @@ def get_mathlib_info(*args):
join('src', 'common', 'numpyos.h'),
join('src', 'common', 'npy_cpu_dispatch.h'),
join('src', 'common', 'simd', 'simd.h'),
]
join('src', 'common', 'common.hpp'),
]

common_src = [
join('src', 'common', 'array_assign.c'),
Expand Down
3 changes: 3 additions & 0 deletions numpy/core/src/common/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
* The following C++ headers are safe to be used standalone, however,
* they are gathered to make it easy for us and for the future need to support PCH.
*/
#include "npdef.hpp"
#include "utils.hpp"
#include "npstd.hpp"
#include "half.hpp"
#include "meta.hpp"
#include "float_status.hpp"

#endif // NUMPY_CORE_SRC_COMMON_COMMON_HPP
134 changes: 134 additions & 0 deletions numpy/core/src/common/float_status.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#ifndef NUMPY_CORE_SRC_COMMON_FLOAT_STATUS_HPP
#define NUMPY_CORE_SRC_COMMON_FLOAT_STATUS_HPP

#include "npstd.hpp"

#include <fenv.h>

namespace np {

/// @addtogroup cpp_core_utility
/// @{
/**
* Class wraps floating-point environment operations,
* provides lazy access to its functionality.
*/
class FloatStatus {
public:
/*
* According to the C99 standard FE_DIVBYZERO, etc. may not be provided when
* unsupported. In such cases NumPy will not report these correctly, but we
* should still allow compiling (whether tests pass or not).
* By defining them as 0 locally, we make them no-ops. Unlike these defines,
* for example `musl` still defines all of the functions (as no-ops):
* https://git.musl-libc.org/cgit/musl/tree/src/fenv/fenv.c
* and does similar replacement in its tests:
* http://nsz.repo.hu/git/?p=libc-test;a=blob;f=src/common/mtest.h;h=706c1ba23ea8989b17a2f72ed1a919e187c06b6a;hb=HEAD#l30
*/
#ifdef FE_DIVBYZERO
static constexpr int kDivideByZero = FE_DIVBYZERO;
#else
static constexpr int kDivideByZero = 0;
#endif
#ifdef FE_INVALID
static constexpr int kInvalid = FE_INVALID;
#else
static constexpr int kInvalid = 0;
#endif
#ifdef FE_INEXACT
static constexpr int kInexact = FE_INEXACT;
#else
static constexpr int kInexact = 0;
#endif
#ifdef FE_OVERFLOW
static constexpr int kOverflow = FE_OVERFLOW;
#else
static constexpr int kOverflow = 0;
#endif
#ifdef FE_UNDERFLOW
static constexpr int kUnderflow = FE_UNDERFLOW;
#else
static constexpr int kUnderflow = 0;
#endif
static constexpr int kAllExcept = (kDivideByZero | kInvalid | kInexact |
kOverflow | kUnderflow);

FloatStatus(bool clear_on_dst=true)
: clear_on_dst_(clear_on_dst)
{
if constexpr (kAllExcept != 0) {
fpstatus_ = fetestexcept(kAllExcept);
}
else {
fpstatus_ = 0;
}
}
~FloatStatus()
{
if constexpr (kAllExcept != 0) {
if (fpstatus_ != 0 && clear_on_dst_) {
feclearexcept(kAllExcept);
}
}
}
constexpr bool IsDivideByZero() const
{
return (fpstatus_ & kDivideByZero) != 0;
}
constexpr bool IsInexact() const
{
return (fpstatus_ & kInexact) != 0;
}
constexpr bool IsInvalid() const
{
return (fpstatus_ & kInvalid) != 0;
}
constexpr bool IsOverFlow() const
{
return (fpstatus_ & kOverflow) != 0;
}
constexpr bool IsUnderFlow() const
{
return (fpstatus_ & kUnderflow) != 0;
}
static void RaiseDivideByZero()
{
if constexpr (kDivideByZero != 0) {
feraiseexcept(kDivideByZero);
}
}
static void RaiseInexact()
{
if constexpr (kInexact != 0) {
feraiseexcept(kInexact);
}
}
static void RaiseInvalid()
{
if constexpr (kInvalid != 0) {
feraiseexcept(kInvalid);
}
}
static void RaiseOverflow()
{
if constexpr (kOverflow != 0) {
feraiseexcept(kOverflow);
}
}
static void RaiseUnderflow()
{
if constexpr (kUnderflow != 0) {
feraiseexcept(kUnderflow);
}
}

private:
bool clear_on_dst_;
int fpstatus_;
};

/// @} cpp_core_utility
} // namespace np

#endif // NUMPY_CORE_SRC_COMMON_FLOAT_STATUS_HPP

Loading
0