8000 BUG: Fix build on ppc64 when the baseline set to Power9 or higher by seiko2plus · Pull Request #24806 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fix build on ppc64 when the baseline set to Power9 or higher #24806

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 4 commits into from
Nov 7, 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
7 changes: 7 additions & 0 deletions .github/workflows/linux_qemu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ jobs:
"-Dallow-noblas=true",
"test_kind or test_multiarray or test_simd or test_umath or test_ufunc",
]
- [
"ppc64le - baseline(Power9)",
"powerpc64le-linux-gnu",
"ppc64le/ubuntu:22.04",
"-Dallow-noblas=true -Dcpu-baseline=vsx3",
"test_kind or test_multiarray or test_simd or test_umath or test_ufunc",
]
- [
"s390x",
"s390x-linux-gnu",
Expand Down
3 changes: 3 additions & 0 deletions meson_cpu/ppc64/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ VSX3 = mod_features.new(
'VSX3', 3, implies: VSX2, args: {'val': '-mcpu=power9', 'match': '.*[mcpu=|vsx].*'},
detect: {'val': 'VSX3', 'match': 'VSX.*'},
test_code: files(source_root + '/numpy/distutils/checks/cpu_vsx3.c')[0],
extra_tests: {
'VSX3_HALF_DOUBLE': files(source_root + '/numpy/distutils/checks/extra_vsx3_half_double.c')[0]
}
)
VSX4 = mod_features.new(
'VSX4', 4, implies: VSX3, args: {'val': '-mcpu=power10', 'match': '.*[mcpu=|vsx].*'},
Expand Down
23 changes: 10 additions & 13 deletions numpy/_core/src/common/half.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Half final {
#endif
) || (
std::is_same_v<T, double> &&
#if defined(NPY_HAVE_AVX512FP16) || defined(NPY_HAVE_VSX3)
#if defined(NPY_HAVE_AVX512FP16) || (defined(NPY_HAVE_VSX3) && defined(NPY_HAVE_VSX3_HALF_DOUBLE))
true
#else
false
Expand Down Expand Up @@ -73,11 +73,8 @@ class Half final {
#if defined(NPY_HAVE_AVX512FP16)
__m128d md = _mm_load_sd(&f);
bits_ = static_cast<uint16_t>(_mm_cvtsi128_si32(_mm_castph_si128(_mm_cvtpd_ph(md))));
#elif defined(NPY_HAVE_VSX3) && defined(NPY_HAVE_VSX_ASM)
__vector double vf64 = vec_splats(f);
__vector unsigned short vf16;
__asm__ __volatile__ ("xvcvdphp %x0,%x1" : "=wa" (vf16) : "wa" (vf64));
bits_ = vec_extract(vf16, 0);
#elif defined(NPY_HAVE_VSX3) && defined(NPY_HAVE_VSX3_HALF_DOUBLE)
__asm__ __volatile__ ("xscvdphp %x0,%x1" : "=wa" (bits_) : "wa" (f));
#else
bits_ = half_private::FromDoubleBits(BitCast<uint64_t>(f));
#endif
Expand All @@ -96,7 +93,7 @@ class Half final {
__vector float vf32;
__asm__ __volatile__("xvcvhpsp %x0,%x1"
: "=wa"(vf32)
: "wa"(vec_splats(bits_.u)));
: "wa"(vec_splats(bits_)));
return vec_extract(vf32, 0);
#else
return BitCast<float>(half_private::ToFloatBits(bits_));
Expand All @@ -110,12 +107,12 @@ class Half final {
double ret;
_mm_store_sd(&ret, _mm_cvtph_pd(_mm_castsi128_ph(_mm_cvtsi32_si128(bits_))));
return ret;
#elif defined(NPY_HAVE_VSX3) && defined(NPY_HAVE_VSX_ASM)
__vector float vf64;
__asm__ __volatile__("xvcvhpdp %x0,%x1"
: "=wa"(vf32)
: "wa"(vec_splats(bits_)));
return vec_extract(vf64, 0);
#elif defined(NPY_HAVE_VSX3) && defined(NPY_HAVE_VSX3_HALF_DOUBLE)
double f64;
__asm__ __volatile__("xscvhpdp %x0,%x1"
: "=wa"(f64)
: "wa"(bits_));
return f64;
#else
return BitCast<double>(half_private::ToDoubleBits(bits_));
#endif
Expand Down
24 changes: 16 additions & 8 deletions numpy/_core/tests/test_half.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ def setup_method(self):
# An array of all possible float16 values
self.all_f16 = np.arange(0x10000, dtype=uint16)
self.all_f16.dtype = float16
self.all_f32 = np.array(self.all_f16, dtype=float32)
self.all_f64 = np.array(self.all_f16, dtype=float64)

# NaN value can cause an invalid FP exception if HW is been used
with np.errstate(invalid='ignore'):
self.all_f32 = np.array(self.all_f16, dtype=float32)
self.all_f64 = np.array(self.all_f16, dtype=float64)

# An array of all non-NaN float16 values, in sorted order
self.nonan_f16 = np.concatenate(
Expand All @@ -44,14 +47,19 @@ def test_half_conversions(self):
# value is preserved when converting to/from other floats.

# Convert from float32 back to float16
b = np.array(self.all_f32, dtype=float16)
assert_equal(self.all_f16.view(dtype=uint16),
b.view(dtype=uint16))
with np.errstate(invalid='ignore'):
b = np.array(self.all_f32, dtype=float16)
# avoid testing NaNs due to differ bits wither Q/SNaNs
b_nn = b == b
assert_equal(self.all_f16[b_nn].view(dtype=uint16),
b[b_nn].view(dtype=uint16))

# Convert from float64 back to float16
b = np.array(self.all_f64, dtype=float16)
assert_equal(self.all_f16.view(dtype=uint16),
b.view(dtype=uint16))
with np.errstate(invalid='ignore'):
b = np.array(self.all_f64, dtype=float16)
b_nn = b == b
assert_equal(self.all_f16[b_nn].view(dtype=uint16),
b[b_nn].view(dtype=uint16))

# Convert float16 to longdouble and back
# This doesn't necessarily preserve the extra NaN bits,
Expand Down
3 changes: 2 additions & 1 deletion numpy/distutils/ccompiler_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ class _Config:
## Power8/ISA 2.07
VSX2 = dict(interest=2, implies="VSX", implies_detect=False),
## Power9/ISA 3.00
VSX3 = dict(interest=3, implies="VSX2", implies_detect=False),
VSX3 = dict(interest=3, implies="VSX2", implies_detect=False,
extra_checks="VSX3_HALF_DOUBLE"),
## Power10/ISA 3.1
VSX4 = dict(interest=4, implies="VSX3", implies_detect=False,
extra_checks="VSX4_MMA"),
Expand Down
12 changes: 12 additions & 0 deletions numpy/distutils/checks/extra_vsx3_half_double.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Assembler may not fully support the following VSX3 scalar
* instructions, even though compilers report VSX3 support.
*/
int main(void)
{
unsigned short bits = 0xFF;
double f;
__asm__ __volatile__("xscvhpdp %x0,%x1" : "=wa"(f) : "wa"(bits));
__asm__ __volatile__ ("xscvdphp %x0,%x1" : "=wa" (bits) : "wa" (f));
return bits;
}
0