Skip to content

Commit 95a81d0

Browse files
committed
Add a test for complex norm and abs
ChangeLog: * tests/complex-math.cpp: New file.
1 parent 450b002 commit 95a81d0

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

tests/complex-math.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause */
2+
/* Copyright © 2024–2026 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
3+
* Matthias Kretz <m.kretz@gsi.de>
4+
*/
5+
// requires complex
6+
7+
#include "unittest.h"
8+
9+
static constexpr bool is_iec559 =
10+
#ifdef __GCC_IEC_559
11+
__GCC_IEC_559 >= 2;
12+
#elif defined __STDC_IEC_559__
13+
__STDC_IEC_559__ == 1;
14+
#else
15+
false;
16+
#endif
17+
18+
// no tests for non-complex types
19+
template <typename V>
20+
struct Tests {};
21+
22+
template <typename V>
23+
requires complex_like<typename V::value_type>
24+
struct Tests<V>
25+
{
26+
using T = typename V::value_type;
27+
using M = typename V::mask_type;
28+
using L = std::numeric_limits<T>;
29+
using RV = simd::rebind_t<typename T::value_type, V>;
30+
31+
ADD_TEST(Norm) {
32+
std::tuple {test_iota<V>},
33+
[](auto& t, const V x) {
34+
const V y = {x.real(), x.real() / 3};
35+
t.verify_equal(norm(x), RV([&](int i) { return std::norm(x[i]); }));
36+
t.verify_equal(norm(y), RV([&](int i) { return std::norm(y[i]); }));
37+
}
38+
};
39+
40+
ADD_TEST(Abs) {
41+
std::tuple {test_iota<V>},
42+
[](auto& t, const V x) {
43+
if !consteval
44+
{
45+
const V y = {x.real(), x.real() / 3};
46+
t.verify_equal(abs(x), RV([&](int i) { return std::abs(x[i]); }));
47+
t.verify_equal(abs(y), RV([&](int i) { return std::abs(y[i]); }));
48+
}
49+
}
50+
};
51+
};

0 commit comments

Comments
 (0)