// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2008-2009 Gael Guennebaud // Copyright (C) 2006-2008 Benoit Jacob // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. #include "main.h" // using namespace Eigen; namespace Eigen { namespace internal { template T negate(const T& x) { return -x; } } } template bool isApproxAbs(const Scalar& a, const Scalar& b, const typename NumTraits::Real& refvalue) { return internal::isMuchSmallerThan(a-b, refvalue); } template bool areApproxAbs(const Scalar* a, const Scalar* b, int size, const typename NumTraits::Real& refvalue) { for (int i=0; i >(a,size) << "]" << " != " << Map >(b,size) << "\n"; return false; } } return true; } template bool areApprox(const Scalar* a, const Scalar* b, int size) { for (int i=0; i >(a,size) << "]" << " != " << Map >(b,size) << "\n"; return false; } } return true; } #define CHECK_CWISE1(REFOP, POP) { \ for (int i=0; i(data1))); \ VERIFY(areApprox(ref, data2, PacketSize) && #POP); \ } template struct packet_helper { template inline Packet load(const T* from) const { return internal::pload(from); } template inline void store(T* to, const Packet& x) const { internal::pstore(to,x); } }; template struct packet_helper { template inline T load(const T* from) const { return *from; } template inline void store(T* to, const T& x) const { *to = x; } }; #define CHECK_CWISE1_IF(COND, REFOP, POP) if(COND) { \ packet_helper h; \ for (int i=0; i h; \ for (int i=0; i void packetmath() { using std::abs; typedef internal::packet_traits PacketTraits; typedef typename PacketTraits::type Packet; const int PacketSize = PacketTraits::size; typedef typename NumTraits::Real RealScalar; const int max_size = PacketSize > 4 ? PacketSize : 4; const int size = PacketSize*max_size; EIGEN_ALIGN_MAX Scalar data1[size]; EIGEN_ALIGN_MAX Scalar data2[size]; EIGEN_ALIGN_MAX Packet packets[PacketSize*2]; EIGEN_ALIGN_MAX Scalar ref[size]; RealScalar refvalue = 0; for (int i=0; i()/RealScalar(PacketSize); data2[i] = internal::random()/RealScalar(PacketSize); refvalue = (std::max)(refvalue,abs(data1[i])); } internal::pstore(data2, internal::pload(data1)); VERIFY(areApprox(data1, data2, PacketSize) && "aligned load/store"); for (int offset=0; offset(data1+offset)); VERIFY(areApprox(data1+offset, data2, PacketSize) && "internal::ploadu"); } for (int offset=0; offset(data1)); VERIFY(areApprox(data1, data2+offset, PacketSize) && "internal::pstoreu"); } for (int offset=0; offset(data1); packets[1] = internal::pload(data1+PacketSize); if (offset==0) internal::palign<0>(packets[0], packets[1]); else if (offset==1) internal::palign<1>(packets[0], packets[1]); else if (offset==2) internal::palign<2>(packets[0], packets[1]); else if (offset==3) internal::palign<3>(packets[0], packets[1]); else if (offset==4) internal::palign<4>(packets[0], packets[1]); else if (offset==5) internal::palign<5>(packets[0], packets[1]); else if (offset==6) internal::palign<6>(packets[0], packets[1]); else if (offset==7) internal::palign<7>(packets[0], packets[1]); internal::pstore(data2, packets[0]); for (int i=0; i::value) || (!PacketTraits::Vectorizable) || PacketTraits::HasDiv); CHECK_CWISE2_IF(PacketTraits::HasAdd, REF_ADD, internal::padd); CHECK_CWISE2_IF(PacketTraits::HasSub, REF_SUB, internal::psub); CHECK_CWISE2_IF(PacketTraits::HasMul, REF_MUL, internal::pmul); CHECK_CWISE2_IF(PacketTraits::HasDiv, REF_DIV, internal::pdiv); CHECK_CWISE1(internal::negate, internal::pnegate); CHECK_CWISE1(numext::conj, internal::pconj); for(int offset=0;offset<3;++offset) { for (int i=0; i(data1[offset])); VERIFY(areApprox(ref, data2, PacketSize) && "internal::pset1"); } { for (int i=0; i(data1, A0, A1, A2, A3); internal::pstore(data2+0*PacketSize, A0); internal::pstore(data2+1*PacketSize, A1); internal::pstore(data2+2*PacketSize, A2); internal::pstore(data2+3*PacketSize, A3); VERIFY(areApprox(ref, data2, 4*PacketSize) && "internal::pbroadcast4"); } { for (int i=0; i(data1, A0, A1); internal::pstore(data2+0*PacketSize, A0); internal::pstore(data2+1*PacketSize, A1); VERIFY(areApprox(ref, data2, 2*PacketSize) && "internal::pbroadcast2"); } VERIFY(internal::isApprox(data1[0], internal::pfirst(internal::pload(data1))) && "internal::pfirst"); if(PacketSize>1) { for(int offset=0;offset<4;++offset) { for(int i=0;i(data1+offset)); VERIFY(areApprox(ref, data2, PacketSize) && "ploaddup"); } } if(PacketSize>2) { for(int offset=0;offset<4;++offset) { for(int i=0;i(data1+offset)); VERIFY(areApprox(ref, data2, PacketSize) && "ploadquad"); } } ref[0] = 0; for (int i=0; i(data1)), refvalue) && "internal::predux"); { for (int i=0; i<4; ++i) ref[i] = 0; for (int i=0; i(data1))); VERIFY(areApprox(ref, data2, PacketSize>4?PacketSize/2:PacketSize) && "internal::predux4"); } ref[0] = 1; for (int i=0; i(data1))) && "internal::predux_mul"); for (int j=0; j(data1+j*PacketSize); } internal::pstore(data2, internal::preduxp(packets)); VERIFY(areApproxAbs(ref, data2, PacketSize, refvalue) && "internal::preduxp"); for (int i=0; i(data1))); VERIFY(areApprox(ref, data2, PacketSize) && "internal::preverse"); internal::PacketBlock kernel; for (int i=0; i(data1+i*PacketSize); } ptranspose(kernel); for (int i=0; i(data1); Packet elsePacket = internal::pload(data2); EIGEN_ALIGN_MAX internal::Selector selector; for (int i = 0; i < PacketSize; ++i) { selector.select[i] = i; } Packet blend = internal::pblend(selector, thenPacket, elsePacket); EIGEN_ALIGN_MAX Scalar result[size]; internal::pstore(result, blend); for (int i = 0; i < PacketSize; ++i) { VERIFY(isApproxAbs(result[i], (selector.select[i] ? data1[i] : data2[i]), refvalue)); } } } template void packetmath_real() { using std::abs; typedef internal::packet_traits PacketTraits; typedef typename PacketTraits::type Packet; const int PacketSize = PacketTraits::size; const int size = PacketSize*4; EIGEN_ALIGN_MAX Scalar data1[PacketTraits::size*4]; EIGEN_ALIGN_MAX Scalar data2[PacketTraits::size*4]; EIGEN_ALIGN_MAX Scalar ref[PacketTraits::size*4]; CHECK_CWISE1_IF(PacketTraits::HasRound, std::round, internal::pround); CHECK_CWISE1_IF(PacketTraits::HasCeil, std::ceil, internal::pceil); CHECK_CWISE1_IF(PacketTraits::HasFloor, std::floor, internal::pfloor); for (int i=0; i(-1,1) * std::pow(Scalar(10), internal::random(-3,3)); data2[i] = internal::random(-1,1) * std::pow(Scalar(10), internal::random(-3,3)); } CHECK_CWISE1_IF(PacketTraits::HasSin, std::sin, internal::psin); CHECK_CWISE1_IF(PacketTraits::HasCos, std::cos, internal::pcos); CHECK_CWISE1_IF(PacketTraits::HasTan, std::tan, internal::ptan); for (int i=0; i(-1,1); data2[i] = internal::random(-1,1); } CHECK_CWISE1_IF(PacketTraits::HasASin, std::asin, internal::pasin); CHECK_CWISE1_IF(PacketTraits::HasACos, std::acos, internal::pacos); for (int i=0; i(-87,88); data2[i] = internal::random(-87,88); } CHECK_CWISE1_IF(PacketTraits::HasExp, std::exp, internal::pexp); if(PacketTraits::HasExp && PacketTraits::size>=2) { data1[0] = std::numeric_limits::quiet_NaN(); data1[1] = std::numeric_limits::epsilon(); packet_helper h; h.store(data2, internal::pexp(h.load(data1))); VERIFY((numext::isnan)(data2[0])); VERIFY_IS_EQUAL(std::exp(std::numeric_limits::epsilon()), data2[1]); data1[0] = -std::numeric_limits::epsilon(); data1[1] = 0; h.store(data2, internal::pexp(h.load(data1))); VERIFY_IS_EQUAL(std::exp(-std::numeric_limits::epsilon()), data2[0]); VERIFY_IS_EQUAL(std::exp(0), data2[1]); data1[0] = (std::numeric_limits::min)(); data1[1] = -(std::numeric_limits::min)(); h.store(data2, internal::pexp(h.load(data1))); VERIFY_IS_EQUAL(std::exp((std::numeric_limits::min)()), data2[0]); VERIFY_IS_EQUAL(std::exp(-(std::numeric_limits::min)()), data2[1]); data1[0] = std::numeric_limits::denorm_min(); data1[1] = -std::numeric_limits::denorm_min(); h.store(data2, internal::pexp(h.load(data1))); VERIFY_IS_EQUAL(std::exp(std::numeric_limits::denorm_min()), data2[0]); VERIFY_IS_EQUAL(std::exp(-std::numeric_limits::denorm_min()), data2[1]); } for (int i=0; i(0,1) * std::pow(Scalar(10), internal::random(-6,6)); data2[i] = internal::random(0,1) * std::pow(Scalar(10), internal::random(-6,6)); } if(internal::random(0,1)<0.1) data1[internal::random(0, PacketSize)] = 0; CHECK_CWISE1_IF(PacketTraits::HasSqrt, std::sqrt, internal::psqrt); CHECK_CWISE1_IF(PacketTraits::HasLog, std::log, internal::plog); if(PacketTraits::HasLog && PacketTraits::size>=2) { data1[0] = std::numeric_limits::quiet_NaN(); data1[1] = std::numeric_limits::epsilon(); packet_helper h; h.store(data2, internal::plog(h.load(data1))); VERIFY((numext::isnan)(data2[0])); VERIFY_IS_EQUAL(std::log(std::numeric_limits::epsilon()), data2[1]); data1[0] = -std::numeric_limits::epsilon(); data1[1] = 0; h.store(data2, internal::plog(h.load(data1))); VERIFY((numext::isnan)(data2[0])); VERIFY_IS_EQUAL(std::log(0), data2[1]); data1[0] = (std::numeric_limits::min)(); data1[1] = -(std::numeric_limits::min)(); h.store(data2, internal::plog(h.load(data1))); VERIFY_IS_EQUAL(std::log((std::numeric_limits::min)()), data2[0]); VERIFY((numext::isnan)(data2[1])); data1[0] = std::numeric_limits::denorm_min(); data1[1] = -std::numeric_limits::denorm_min(); h.store(data2, internal::plog(h.load(data1))); // VERIFY_IS_EQUAL(std::log(std::numeric_limits::denorm_min()), data2[0]); VERIFY((numext::isnan)(data2[1])); data1[0] = -1.0f; h.store(data2, internal::plog(h.load(data1))); VERIFY((numext::isnan)(data2[0])); #if !EIGEN_FAST_MATH h.store(data2, internal::psqrt(h.load(data1))); VERIFY((numext::isnan)(data2[0])); VERIFY((numext::isnan)(data2[1])); #endif } } template void packetmath_notcomplex() { using std::abs; typedef internal::packet_traits PacketTraits; typedef typename PacketTraits::type Packet; const int PacketSize = PacketTraits::size; EIGEN_ALIGN_MAX Scalar data1[PacketTraits::size*4]; EIGEN_ALIGN_MAX Scalar data2[PacketTraits::size*4]; EIGEN_ALIGN_MAX Scalar ref[PacketTraits::size*4]; Array::Map(data1, PacketTraits::size*4).setRandom(); ref[0] = data1[0]; for (int i=0; i(data1))) && "internal::predux_min"); VERIFY((!PacketTraits::Vectorizable) || PacketTraits::HasMin); VERIFY((!PacketTraits::Vectorizable) || PacketTraits::HasMax); CHECK_CWISE2_IF(PacketTraits::HasMin, (std::min), internal::pmin); CHECK_CWISE2_IF(PacketTraits::HasMax, (std::max), internal::pmax); CHECK_CWISE1(abs, internal::pabs); ref[0] = data1[0]; for (int i=0; i(data1))) && "internal::predux_max"); for (int i=0; i(data1[0])); VERIFY(areApprox(ref, data2, PacketSize) && "internal::plset"); } template void test_conj_helper(Scalar* data1, Scalar* data2, Scalar* ref, Scalar* pval) { typedef internal::packet_traits PacketTraits; typedef typename PacketTraits::type Packet; const int PacketSize = PacketTraits::size; internal::conj_if cj0; internal::conj_if cj1; internal::conj_helper cj; internal::conj_helper pcj; for(int i=0;i(data1),internal::pload(data2))); VERIFY(areApprox(ref, pval, PacketSize) && "conj_helper pmul"); for(int i=0;i(data1),internal::pload(data2),internal::pload(pval))); VERIFY(areApprox(ref, pval, PacketSize) && "conj_helper pmadd"); } template void packetmath_complex() { typedef internal::packet_traits PacketTraits; typedef typename PacketTraits::type Packet; const int PacketSize = PacketTraits::size; const int size = PacketSize*4; EIGEN_ALIGN_MAX Scalar data1[PacketSize*4]; EIGEN_ALIGN_MAX Scalar data2[PacketSize*4]; EIGEN_ALIGN_MAX Scalar ref[PacketSize*4]; EIGEN_ALIGN_MAX Scalar pval[PacketSize*4]; for (int i=0; i() * Scalar(1e2); data2[i] = internal::random() * Scalar(1e2); } test_conj_helper (data1,data2,ref,pval); test_conj_helper (data1,data2,ref,pval); test_conj_helper (data1,data2,ref,pval); test_conj_helper (data1,data2,ref,pval); { for(int i=0;i(data1))); VERIFY(areApprox(ref, pval, PacketSize) && "pcplxflip"); } } template void packetmath_scatter_gather() { typedef internal::packet_traits PacketTraits; typedef typename PacketTraits::type Packet; typedef typename NumTraits::Real RealScalar; const int PacketSize = PacketTraits::size; EIGEN_ALIGN_MAX Scalar data1[PacketSize]; RealScalar refvalue = 0; for (int i=0; i()/RealScalar(PacketSize); } int stride = internal::random(1,20); EIGEN_ALIGN_MAX Scalar buffer[PacketSize*20]; memset(buffer, 0, 20*sizeof(Packet)); Packet packet = internal::pload(data1); internal::pscatter(buffer, packet, stride); for (int i = 0; i < PacketSize*20; ++i) { if ((i%stride) == 0 && i()/RealScalar(PacketSize); } packet = internal::pgather(buffer, 7); internal::pstore(data1, packet); for (int i = 0; i < PacketSize; ++i) { VERIFY(isApproxAbs(data1[i], buffer[i*7], refvalue) && "pgather"); } } void test_packetmath() { for(int i = 0; i < g_repeat; i++) { CALL_SUBTEST_1( packetmath() ); CALL_SUBTEST_2( packetmath() ); CALL_SUBTEST_3( packetmath() ); CALL_SUBTEST_4( packetmath >() ); CALL_SUBTEST_5( packetmath >() ); CALL_SUBTEST_1( packetmath_notcomplex() ); CALL_SUBTEST_2( packetmath_notcomplex() ); CALL_SUBTEST_3( packetmath_notcomplex() ); CALL_SUBTEST_1( packetmath_real() ); CALL_SUBTEST_2( packetmath_real() ); CALL_SUBTEST_4( packetmath_complex >() ); CALL_SUBTEST_5( packetmath_complex >() ); CALL_SUBTEST_1( packetmath_scatter_gather() ); CALL_SUBTEST_2( packetmath_scatter_gather() ); CALL_SUBTEST_3( packetmath_scatter_gather() ); CALL_SUBTEST_4( packetmath_scatter_gather >() ); CALL_SUBTEST_5( packetmath_scatter_gather >() ); } }