aboutsummaryrefslogtreecommitdiffhomepage
path: root/Eigen/src/Core/products/SelfadjointMatrixVector.h
blob: 65210ed3367af2016e964feed3592a1c92fc33d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
//
// Eigen is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// Alternatively, you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// Eigen. If not, see <http://www.gnu.org/licenses/>.

#ifndef EIGEN_SELFADJOINT_MATRIX_VECTOR_H
#define EIGEN_SELFADJOINT_MATRIX_VECTOR_H

template<bool Conjugate> struct ei_conj_if {
  template<typename Scalar> Scalar operator() (const Scalar& x) const { return ei_conj(x); }
};

template<> struct ei_conj_if<false> {
  template<typename Scalar> Scalar& operator() (Scalar& x) const { return x; }
};

/* Optimized col-major selfadjoint matrix * vector product:
 * This algorithm processes 2 columns at onces that allows to both reduce
 * the number of load/stores of the result by a factor 2 and to reduce
 * the instruction dependency.
 */
template<typename Scalar, int StorageOrder, int UpLo>
static EIGEN_DONT_INLINE void ei_product_selfadjoint_vector(
  int size,
  const Scalar* lhs, int lhsStride,
  const Scalar* rhs, //int rhsIncr,
  Scalar* res)
{
  typedef typename ei_packet_traits<Scalar>::type Packet;
  const int PacketSize = sizeof(Packet)/sizeof(Scalar);

  enum {
    IsRowMajor = StorageOrder==RowMajorBit ? 1 : 0,
    IsLower = UpLo == LowerTriangularBit ? 1 : 0,
    FirstTriangular = IsRowMajor == IsLower
  };

  ei_conj_if<NumTraits<Scalar>::IsComplex && IsRowMajor> conj0;
  ei_conj_if<NumTraits<Scalar>::IsComplex && !IsRowMajor> conj1;

  for (int i=0;i<size;i++)
    res[i] = 0;

  int bound = std::max(0,size-8) & 0xfffffffE;
  if (FirstTriangular)
    bound = size - bound;

  for (int j=FirstTriangular ? bound : 0;
       j<(FirstTriangular ? size : bound);j+=2)
  {
    register const Scalar* EIGEN_RESTRICT A0 = lhs + j*lhsStride;
    register const Scalar* EIGEN_RESTRICT A1 = lhs + (j+1)*lhsStride;

    Scalar t0 = rhs[j];
    Packet ptmp0 = ei_pset1(t0);
    Scalar t1 = rhs[j+1];
    Packet ptmp1 = ei_pset1(t1);

    Scalar t2 = 0;
    Packet ptmp2 = ei_pset1(t2);
    Scalar t3 = 0;
    Packet ptmp3 = ei_pset1(t3);

    size_t starti = FirstTriangular ? 0 : j+2;
    size_t endi   = FirstTriangular ? j : size;
    size_t alignedEnd = starti;
    size_t alignedStart = (starti) + ei_alignmentOffset(&res[starti], endi-starti);
    alignedEnd = alignedStart + ((endi-alignedStart)/(PacketSize))*(PacketSize);

    res[j]   += t0 * conj0(A0[j]);
    if(FirstTriangular)
    {
      res[j+1]   += t1 * conj0(A1[j+1]);
      res[j] += t1 * conj0(A1[j]);
      t3 += conj1(A1[j]) * rhs[j];
    }
    else
    {
      res[j+1] += t0 * conj0(A0[j+1]) + t1 * conj0(A1[j+1]);
      t2 += conj1(A0[j+1]) * rhs[j+1];
    }

    for (size_t i=starti; i<alignedStart; ++i)
    {
      res[i] += t0 * A0[i] + t1 * A1[i];
      t2 += ei_conj(A0[i]) * rhs[i];
      t3 += ei_conj(A1[i]) * rhs[i];
    }
    for (size_t i=alignedStart; i<alignedEnd; i+=PacketSize)
    {
      Packet A0i = ei_ploadu(&A0[i]);
      Packet A1i = ei_ploadu(&A1[i]);
      Packet Bi = ei_ploadu(&rhs[i]); // FIXME should be aligned in most cases
      Packet Xi = ei_pload(&res[i]);

      Xi = ei_padd(ei_padd(Xi, ei_pmul(ptmp0, conj0(A0i))), ei_pmul(ptmp1, conj0(A1i)));
      ptmp2 = ei_padd(ptmp2, ei_pmul(conj1(A0i), Bi));
      ptmp3 = ei_padd(ptmp3, ei_pmul(conj1(A1i), Bi));
      ei_pstore(&res[i],Xi);
    }
    for (size_t i=alignedEnd; i<endi; i++)
    {
      res[i] += t0 * conj0(A0[i]) + t1 * conj0(A1[i]);
      t2 += conj1(A0[i]) * rhs[i];
      t3 += conj1(A1[i]) * rhs[i];
    }

    res[j]   += t2 + ei_predux(ptmp2);
    res[j+1] += t3 + ei_predux(ptmp3);
  }
  for (int j=FirstTriangular ? 0 : bound;j<(FirstTriangular ? bound : size);j++)
  {
    register const Scalar* EIGEN_RESTRICT A0 = lhs + j*lhsStride;

    Scalar t1 = rhs[j];
    Scalar t2 = 0;
    res[j] += t1 * conj0(A0[j]);
    for (int i=FirstTriangular ? 0 : j+1; i<(FirstTriangular ? j : size); i++) {
      res[i] += t1 * conj0(A0[i]);
      t2 += conj1(A0[i]) * rhs[i];
    }
    res[j] += t2;
  }
}


#endif // EIGEN_SELFADJOINT_MATRIX_VECTOR_H