| // SPDX-FileCopyrightText: The Eigen Authors | |
| // SPDX-License-Identifier: MPL-2.0 | |
| #include "main.h" | |
| #include "fp_control.h" | |
| #if defined(EIGEN_VECTORIZE_AVX2) && defined(EIGEN_VECTORIZE_FMA) | |
| static void packetmath_cbrt_avx2_midpoints() { | |
| using namespace Eigen; | |
| using internal::Packet4d; | |
| // All three exponent remainders. The expected values were checked by exactly cubing the adjacent midpoints. | |
| const double x[] = {0.50099716660068738, -1.0008163376817305, 2.0051267258752654, -0.502038251003318}; | |
| const double expected[] = {0.79422781004956, -1.0002720385488939, 1.2609966763186524, -0.7947775710440278}; | |
| double out[4]; | |
| internal::pstoreu(out, internal::pcbrt(internal::ploadu<Packet4d>(x))); | |
| for (int lane = 0; lane < 4; ++lane) { | |
| VERIFY_IS_EQUAL(out[lane], expected[lane]); | |
| } | |
| ScopedFlushToZero gradual(FlushToZeroMode::None); | |
| const double inf = std::numeric_limits<double>::infinity(); | |
| const double nan = numext::bit_cast<double>(uint64_t(0x7ff8000000001234ull)); | |
| const double special[] = {-0.0, -inf, nan, -(std::numeric_limits<double>::denorm_min)()}; | |
| const double special_expected[] = {-0.0, -inf, nan, -std::ldexp(1.0, -358)}; | |
| for (int lane = 0; lane < 4; ++lane) { | |
| if (lane == 3 && !gradual.isSupported()) continue; | |
| double in[4] = {x[0], x[1], x[2], x[3]}; | |
| in[lane] = special[lane]; | |
| internal::pstoreu(out, internal::pcbrt(internal::ploadu<Packet4d>(in))); | |
| for (int i = 0; i < 4; ++i) { | |
| const double ref = i == lane ? special_expected[lane] : expected[i]; | |
| VERIFY_IS_EQUAL(numext::bit_cast<uint64_t>(out[i]), numext::bit_cast<uint64_t>(ref)); | |
| } | |
| } | |
| } | |
| static void packetmath_cbrt_avx2_subnormal() { | |
| using namespace Eigen; | |
| using internal::Packet4d; | |
| const uint64_t bits[] = {0x0000000000000001ull, 0x8000000000000008ull, 0x0008000000000000ull, 0x8000000000000000ull}; | |
| const double expected[] = {std::ldexp(1.0, -358), -std::ldexp(1.0, -357), std::ldexp(1.0, -341), -0.0}; | |
| for (const auto mode : {FlushToZeroMode::None, FlushToZeroMode::Both}) { | |
| ScopedFlushToZero flush(mode); | |
| if (!flush.isSupported()) continue; | |
| for (int start = 0; start < 4; ++start) { | |
| double input[4], output[4]; | |
| for (int lane = 0; lane < 4; ++lane) input[lane] = numext::bit_cast<double>(bits[(start + lane) % 4]); | |
| internal::pstoreu(output, internal::pcbrt(internal::ploadu<Packet4d>(input))); | |
| for (int lane = 0; lane < 4; ++lane) { | |
| VERIFY_IS_EQUAL(numext::bit_cast<uint64_t>(output[lane]), | |
| numext::bit_cast<uint64_t>(expected[(start + lane) % 4])); | |
| } | |
| } | |
| } | |
| } | |
| #endif | |
| EIGEN_DECLARE_TEST(packet_cbrt) { | |
| const Eigen::Array<double, 4, 1> cubes(8.0, -27.0, 64.0, -125.0); | |
| const Eigen::Array<double, 4, 1> roots(2.0, -3.0, 4.0, -5.0); | |
| VERIFY_IS_APPROX(cubes.cbrt().eval(), roots); | |
| #if defined(EIGEN_VECTORIZE_AVX2) && defined(EIGEN_VECTORIZE_FMA) | |
| packetmath_cbrt_avx2_midpoints(); | |
| packetmath_cbrt_avx2_subnormal(); | |
| #endif | |
| } |