10 #ifndef EIGEN_GENERAL_MATRIX_VECTOR_H
11 #define EIGEN_GENERAL_MATRIX_VECTOR_H
30 template<
typename Index,
typename LhsScalar,
bool ConjugateLhs,
typename RhsScalar,
bool ConjugateRhs,
int Version>
31 struct general_matrix_vector_product<Index,LhsScalar,
ColMajor,ConjugateLhs,RhsScalar,ConjugateRhs,Version>
33 typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType ResScalar;
36 Vectorizable = packet_traits<LhsScalar>::Vectorizable && packet_traits<RhsScalar>::Vectorizable
37 && int(packet_traits<LhsScalar>::size)==int(packet_traits<RhsScalar>::size),
38 LhsPacketSize = Vectorizable ? packet_traits<LhsScalar>::size : 1,
39 RhsPacketSize = Vectorizable ? packet_traits<RhsScalar>::size : 1,
40 ResPacketSize = Vectorizable ? packet_traits<ResScalar>::size : 1
43 typedef typename packet_traits<LhsScalar>::type _LhsPacket;
44 typedef typename packet_traits<RhsScalar>::type _RhsPacket;
45 typedef typename packet_traits<ResScalar>::type _ResPacket;
47 typedef typename conditional<Vectorizable,_LhsPacket,LhsScalar>::type LhsPacket;
48 typedef typename conditional<Vectorizable,_RhsPacket,RhsScalar>::type RhsPacket;
49 typedef typename conditional<Vectorizable,_ResPacket,ResScalar>::type ResPacket;
51 EIGEN_DONT_INLINE
static void run(
52 Index rows, Index cols,
53 const LhsScalar* lhs, Index lhsStride,
54 const RhsScalar* rhs, Index rhsIncr,
56 #ifdef EIGEN_INTERNAL_DEBUGGING
61 eigen_internal_assert(resIncr==1);
62 #ifdef _EIGEN_ACCUMULATE_PACKETS
63 #error _EIGEN_ACCUMULATE_PACKETS has already been defined
65 #define _EIGEN_ACCUMULATE_PACKETS(A0,A13,A2) \
67 padd(pload<ResPacket>(&res[j]), \
69 padd(pcj.pmul(EIGEN_CAT(ploa , A0)<LhsPacket>(&lhs0[j]), ptmp0), \
70 pcj.pmul(EIGEN_CAT(ploa , A13)<LhsPacket>(&lhs1[j]), ptmp1)), \
71 padd(pcj.pmul(EIGEN_CAT(ploa , A2)<LhsPacket>(&lhs2[j]), ptmp2), \
72 pcj.pmul(EIGEN_CAT(ploa , A13)<LhsPacket>(&lhs3[j]), ptmp3)) )))
74 conj_helper<LhsScalar,RhsScalar,ConjugateLhs,ConjugateRhs> cj;
75 conj_helper<LhsPacket,RhsPacket,ConjugateLhs,ConjugateRhs> pcj;
79 enum { AllAligned = 0, EvenAligned, FirstAligned, NoneAligned };
80 const Index columnsAtOnce = 4;
81 const Index peels = 2;
82 const Index LhsPacketAlignedMask = LhsPacketSize-1;
83 const Index ResPacketAlignedMask = ResPacketSize-1;
84 const Index size = rows;
88 Index alignedStart = internal::first_aligned(res,size);
89 Index alignedSize = ResPacketSize>1 ? alignedStart + ((size-alignedStart) & ~ResPacketAlignedMask) : 0;
90 const Index peeledSize = alignedSize - RhsPacketSize*peels - RhsPacketSize + 1;
92 const Index alignmentStep = LhsPacketSize>1 ? (LhsPacketSize - lhsStride % LhsPacketSize) & LhsPacketAlignedMask : 0;
93 Index alignmentPattern = alignmentStep==0 ? AllAligned
94 : alignmentStep==(LhsPacketSize/2) ? EvenAligned
98 const Index lhsAlignmentOffset = internal::first_aligned(lhs,size);
101 Index skipColumns = 0;
103 if( (
size_t(lhs)%
sizeof(LhsScalar)) || (
size_t(res)%
sizeof(ResScalar)) )
108 else if (LhsPacketSize>1)
110 eigen_internal_assert(
size_t(lhs+lhsAlignmentOffset)%
sizeof(LhsPacket)==0 || size<LhsPacketSize);
112 while (skipColumns<LhsPacketSize &&
113 alignedStart != ((lhsAlignmentOffset + alignmentStep*skipColumns)%LhsPacketSize))
115 if (skipColumns==LhsPacketSize)
118 alignmentPattern = NoneAligned;
123 skipColumns = (std::min)(skipColumns,cols);
127 eigen_internal_assert( (alignmentPattern==NoneAligned)
128 || (skipColumns + columnsAtOnce >= cols)
129 || LhsPacketSize > size
130 || (
size_t(lhs+alignedStart+lhsStride*skipColumns)%
sizeof(LhsPacket))==0);
132 else if(Vectorizable)
136 alignmentPattern = AllAligned;
139 Index offset1 = (FirstAligned && alignmentStep==1?3:1);
140 Index offset3 = (FirstAligned && alignmentStep==1?1:3);
142 Index columnBound = ((cols-skipColumns)/columnsAtOnce)*columnsAtOnce + skipColumns;
143 for (Index i=skipColumns; i<columnBound; i+=columnsAtOnce)
145 RhsPacket ptmp0 = pset1<RhsPacket>(alpha*rhs[i*rhsIncr]),
146 ptmp1 = pset1<RhsPacket>(alpha*rhs[(i+offset1)*rhsIncr]),
147 ptmp2 = pset1<RhsPacket>(alpha*rhs[(i+2)*rhsIncr]),
148 ptmp3 = pset1<RhsPacket>(alpha*rhs[(i+offset3)*rhsIncr]);
151 const LhsScalar *lhs0 = lhs + i*lhsStride, *lhs1 = lhs + (i+offset1)*lhsStride,
152 *lhs2 = lhs + (i+2)*lhsStride, *lhs3 = lhs + (i+offset3)*lhsStride;
158 for (Index j=0; j<alignedStart; ++j)
160 res[j] = cj.pmadd(lhs0[j], pfirst(ptmp0), res[j]);
161 res[j] = cj.pmadd(lhs1[j], pfirst(ptmp1), res[j]);
162 res[j] = cj.pmadd(lhs2[j], pfirst(ptmp2), res[j]);
163 res[j] = cj.pmadd(lhs3[j], pfirst(ptmp3), res[j]);
166 if (alignedSize>alignedStart)
168 switch(alignmentPattern)
171 for (Index j = alignedStart; j<alignedSize; j+=ResPacketSize)
172 _EIGEN_ACCUMULATE_PACKETS(d,d,d);
175 for (Index j = alignedStart; j<alignedSize; j+=ResPacketSize)
176 _EIGEN_ACCUMULATE_PACKETS(d,du,d);
180 Index j = alignedStart;
183 LhsPacket A00, A01, A02, A03, A10, A11, A12, A13;
186 A01 = pload<LhsPacket>(&lhs1[alignedStart-1]);
187 A02 = pload<LhsPacket>(&lhs2[alignedStart-2]);
188 A03 = pload<LhsPacket>(&lhs3[alignedStart-3]);
190 for (; j<peeledSize; j+=peels*ResPacketSize)
192 A11 = pload<LhsPacket>(&lhs1[j-1+LhsPacketSize]); palign<1>(A01,A11);
193 A12 = pload<LhsPacket>(&lhs2[j-2+LhsPacketSize]); palign<2>(A02,A12);
194 A13 = pload<LhsPacket>(&lhs3[j-3+LhsPacketSize]); palign<3>(A03,A13);
196 A00 = pload<LhsPacket>(&lhs0[j]);
197 A10 = pload<LhsPacket>(&lhs0[j+LhsPacketSize]);
198 T0 = pcj.pmadd(A00, ptmp0, pload<ResPacket>(&res[j]));
199 T1 = pcj.pmadd(A10, ptmp0, pload<ResPacket>(&res[j+ResPacketSize]));
201 T0 = pcj.pmadd(A01, ptmp1, T0);
202 A01 = pload<LhsPacket>(&lhs1[j-1+2*LhsPacketSize]); palign<1>(A11,A01);
203 T0 = pcj.pmadd(A02, ptmp2, T0);
204 A02 = pload<LhsPacket>(&lhs2[j-2+2*LhsPacketSize]); palign<2>(A12,A02);
205 T0 = pcj.pmadd(A03, ptmp3, T0);
207 A03 = pload<LhsPacket>(&lhs3[j-3+2*LhsPacketSize]); palign<3>(A13,A03);
208 T1 = pcj.pmadd(A11, ptmp1, T1);
209 T1 = pcj.pmadd(A12, ptmp2, T1);
210 T1 = pcj.pmadd(A13, ptmp3, T1);
211 pstore(&res[j+ResPacketSize],T1);
214 for (; j<alignedSize; j+=ResPacketSize)
215 _EIGEN_ACCUMULATE_PACKETS(d,du,du);
219 for (Index j = alignedStart; j<alignedSize; j+=ResPacketSize)
220 _EIGEN_ACCUMULATE_PACKETS(du,du,du);
227 for (Index j=alignedSize; j<size; ++j)
229 res[j] = cj.pmadd(lhs0[j], pfirst(ptmp0), res[j]);
230 res[j] = cj.pmadd(lhs1[j], pfirst(ptmp1), res[j]);
231 res[j] = cj.pmadd(lhs2[j], pfirst(ptmp2), res[j]);
232 res[j] = cj.pmadd(lhs3[j], pfirst(ptmp3), res[j]);
238 Index start = columnBound;
241 for (Index k=start; k<end; ++k)
243 RhsPacket ptmp0 = pset1<RhsPacket>(alpha*rhs[k*rhsIncr]);
244 const LhsScalar* lhs0 = lhs + k*lhsStride;
250 for (Index j=0; j<alignedStart; ++j)
251 res[j] += cj.pmul(lhs0[j], pfirst(ptmp0));
253 if ((
size_t(lhs0+alignedStart)%
sizeof(LhsPacket))==0)
254 for (Index i = alignedStart;i<alignedSize;i+=ResPacketSize)
255 pstore(&res[i], pcj.pmadd(ploadu<LhsPacket>(&lhs0[i]), ptmp0, pload<ResPacket>(&res[i])));
257 for (Index i = alignedStart;i<alignedSize;i+=ResPacketSize)
258 pstore(&res[i], pcj.pmadd(ploadu<LhsPacket>(&lhs0[i]), ptmp0, pload<ResPacket>(&res[i])));
262 for (Index i=alignedSize; i<size; ++i)
263 res[i] += cj.pmul(lhs0[i], pfirst(ptmp0));
273 }
while(Vectorizable);
274 #undef _EIGEN_ACCUMULATE_PACKETS
288 template<
typename Index,
typename LhsScalar,
bool ConjugateLhs,
typename RhsScalar,
bool ConjugateRhs,
int Version>
289 struct general_matrix_vector_product<Index,LhsScalar,
RowMajor,ConjugateLhs,RhsScalar,ConjugateRhs,Version>
291 typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType ResScalar;
294 Vectorizable = packet_traits<LhsScalar>::Vectorizable && packet_traits<RhsScalar>::Vectorizable
295 && int(packet_traits<LhsScalar>::size)==int(packet_traits<RhsScalar>::size),
296 LhsPacketSize = Vectorizable ? packet_traits<LhsScalar>::size : 1,
297 RhsPacketSize = Vectorizable ? packet_traits<RhsScalar>::size : 1,
298 ResPacketSize = Vectorizable ? packet_traits<ResScalar>::size : 1
301 typedef typename packet_traits<LhsScalar>::type _LhsPacket;
302 typedef typename packet_traits<RhsScalar>::type _RhsPacket;
303 typedef typename packet_traits<ResScalar>::type _ResPacket;
305 typedef typename conditional<Vectorizable,_LhsPacket,LhsScalar>::type LhsPacket;
306 typedef typename conditional<Vectorizable,_RhsPacket,RhsScalar>::type RhsPacket;
307 typedef typename conditional<Vectorizable,_ResPacket,ResScalar>::type ResPacket;
309 EIGEN_DONT_INLINE
static void run(
310 Index rows, Index cols,
311 const LhsScalar* lhs, Index lhsStride,
312 const RhsScalar* rhs, Index rhsIncr,
313 ResScalar* res, Index resIncr,
316 EIGEN_UNUSED_VARIABLE(rhsIncr);
317 eigen_internal_assert(rhsIncr==1);
318 #ifdef _EIGEN_ACCUMULATE_PACKETS
319 #error _EIGEN_ACCUMULATE_PACKETS has already been defined
322 #define _EIGEN_ACCUMULATE_PACKETS(A0,A13,A2) {\
323 RhsPacket b = pload<RhsPacket>(&rhs[j]); \
324 ptmp0 = pcj.pmadd(EIGEN_CAT(ploa,A0) <LhsPacket>(&lhs0[j]), b, ptmp0); \
325 ptmp1 = pcj.pmadd(EIGEN_CAT(ploa,A13)<LhsPacket>(&lhs1[j]), b, ptmp1); \
326 ptmp2 = pcj.pmadd(EIGEN_CAT(ploa,A2) <LhsPacket>(&lhs2[j]), b, ptmp2); \
327 ptmp3 = pcj.pmadd(EIGEN_CAT(ploa,A13)<LhsPacket>(&lhs3[j]), b, ptmp3); }
329 conj_helper<LhsScalar,RhsScalar,ConjugateLhs,ConjugateRhs> cj;
330 conj_helper<LhsPacket,RhsPacket,ConjugateLhs,ConjugateRhs> pcj;
332 enum { AllAligned=0, EvenAligned=1, FirstAligned=2, NoneAligned=3 };
333 const Index rowsAtOnce = 4;
334 const Index peels = 2;
335 const Index RhsPacketAlignedMask = RhsPacketSize-1;
336 const Index LhsPacketAlignedMask = LhsPacketSize-1;
337 const Index depth = cols;
342 Index alignedStart = internal::first_aligned(rhs, depth);
343 Index alignedSize = RhsPacketSize>1 ? alignedStart + ((depth-alignedStart) & ~RhsPacketAlignedMask) : 0;
344 const Index peeledSize = alignedSize - RhsPacketSize*peels - RhsPacketSize + 1;
346 const Index alignmentStep = LhsPacketSize>1 ? (LhsPacketSize - lhsStride % LhsPacketSize) & LhsPacketAlignedMask : 0;
347 Index alignmentPattern = alignmentStep==0 ? AllAligned
348 : alignmentStep==(LhsPacketSize/2) ? EvenAligned
352 const Index lhsAlignmentOffset = internal::first_aligned(lhs,depth);
357 if( (
sizeof(LhsScalar)!=
sizeof(RhsScalar)) || (
size_t(lhs)%
sizeof(LhsScalar)) || (
size_t(rhs)%
sizeof(RhsScalar)) )
362 else if (LhsPacketSize>1)
364 eigen_internal_assert(
size_t(lhs+lhsAlignmentOffset)%
sizeof(LhsPacket)==0 || depth<LhsPacketSize);
366 while (skipRows<LhsPacketSize &&
367 alignedStart != ((lhsAlignmentOffset + alignmentStep*skipRows)%LhsPacketSize))
369 if (skipRows==LhsPacketSize)
372 alignmentPattern = NoneAligned;
377 skipRows = (std::min)(skipRows,Index(rows));
380 eigen_internal_assert( alignmentPattern==NoneAligned
382 || (skipRows + rowsAtOnce >= rows)
383 || LhsPacketSize > depth
384 || (
size_t(lhs+alignedStart+lhsStride*skipRows)%
sizeof(LhsPacket))==0);
386 else if(Vectorizable)
390 alignmentPattern = AllAligned;
393 Index offset1 = (FirstAligned && alignmentStep==1?3:1);
394 Index offset3 = (FirstAligned && alignmentStep==1?1:3);
396 Index rowBound = ((rows-skipRows)/rowsAtOnce)*rowsAtOnce + skipRows;
397 for (Index i=skipRows; i<rowBound; i+=rowsAtOnce)
399 EIGEN_ALIGN16 ResScalar tmp0 = ResScalar(0);
400 ResScalar tmp1 = ResScalar(0), tmp2 = ResScalar(0), tmp3 = ResScalar(0);
403 const LhsScalar *lhs0 = lhs + i*lhsStride, *lhs1 = lhs + (i+offset1)*lhsStride,
404 *lhs2 = lhs + (i+2)*lhsStride, *lhs3 = lhs + (i+offset3)*lhsStride;
409 ResPacket ptmp0 = pset1<ResPacket>(ResScalar(0)), ptmp1 = pset1<ResPacket>(ResScalar(0)),
410 ptmp2 = pset1<ResPacket>(ResScalar(0)), ptmp3 = pset1<ResPacket>(ResScalar(0));
414 for (Index j=0; j<alignedStart; ++j)
416 RhsScalar b = rhs[j];
417 tmp0 += cj.pmul(lhs0[j],b); tmp1 += cj.pmul(lhs1[j],b);
418 tmp2 += cj.pmul(lhs2[j],b); tmp3 += cj.pmul(lhs3[j],b);
421 if (alignedSize>alignedStart)
423 switch(alignmentPattern)
426 for (Index j = alignedStart; j<alignedSize; j+=RhsPacketSize)
427 _EIGEN_ACCUMULATE_PACKETS(d,d,d);
430 for (Index j = alignedStart; j<alignedSize; j+=RhsPacketSize)
431 _EIGEN_ACCUMULATE_PACKETS(d,du,d);
435 Index j = alignedStart;
444 LhsPacket A01, A02, A03, A11, A12, A13;
445 A01 = pload<LhsPacket>(&lhs1[alignedStart-1]);
446 A02 = pload<LhsPacket>(&lhs2[alignedStart-2]);
447 A03 = pload<LhsPacket>(&lhs3[alignedStart-3]);
449 for (; j<peeledSize; j+=peels*RhsPacketSize)
451 RhsPacket b = pload<RhsPacket>(&rhs[j]);
452 A11 = pload<LhsPacket>(&lhs1[j-1+LhsPacketSize]); palign<1>(A01,A11);
453 A12 = pload<LhsPacket>(&lhs2[j-2+LhsPacketSize]); palign<2>(A02,A12);
454 A13 = pload<LhsPacket>(&lhs3[j-3+LhsPacketSize]); palign<3>(A03,A13);
456 ptmp0 = pcj.pmadd(pload<LhsPacket>(&lhs0[j]), b, ptmp0);
457 ptmp1 = pcj.pmadd(A01, b, ptmp1);
458 A01 = pload<LhsPacket>(&lhs1[j-1+2*LhsPacketSize]); palign<1>(A11,A01);
459 ptmp2 = pcj.pmadd(A02, b, ptmp2);
460 A02 = pload<LhsPacket>(&lhs2[j-2+2*LhsPacketSize]); palign<2>(A12,A02);
461 ptmp3 = pcj.pmadd(A03, b, ptmp3);
462 A03 = pload<LhsPacket>(&lhs3[j-3+2*LhsPacketSize]); palign<3>(A13,A03);
464 b = pload<RhsPacket>(&rhs[j+RhsPacketSize]);
465 ptmp0 = pcj.pmadd(pload<LhsPacket>(&lhs0[j+LhsPacketSize]), b, ptmp0);
466 ptmp1 = pcj.pmadd(A11, b, ptmp1);
467 ptmp2 = pcj.pmadd(A12, b, ptmp2);
468 ptmp3 = pcj.pmadd(A13, b, ptmp3);
471 for (; j<alignedSize; j+=RhsPacketSize)
472 _EIGEN_ACCUMULATE_PACKETS(d,du,du);
476 for (Index j = alignedStart; j<alignedSize; j+=RhsPacketSize)
477 _EIGEN_ACCUMULATE_PACKETS(du,du,du);
480 tmp0 += predux(ptmp0);
481 tmp1 += predux(ptmp1);
482 tmp2 += predux(ptmp2);
483 tmp3 += predux(ptmp3);
489 for (Index j=alignedSize; j<depth; ++j)
491 RhsScalar b = rhs[j];
492 tmp0 += cj.pmul(lhs0[j],b); tmp1 += cj.pmul(lhs1[j],b);
493 tmp2 += cj.pmul(lhs2[j],b); tmp3 += cj.pmul(lhs3[j],b);
495 res[i*resIncr] += alpha*tmp0;
496 res[(i+offset1)*resIncr] += alpha*tmp1;
497 res[(i+2)*resIncr] += alpha*tmp2;
498 res[(i+offset3)*resIncr] += alpha*tmp3;
503 Index start = rowBound;
506 for (Index i=start; i<end; ++i)
508 EIGEN_ALIGN16 ResScalar tmp0 = ResScalar(0);
509 ResPacket ptmp0 = pset1<ResPacket>(tmp0);
510 const LhsScalar* lhs0 = lhs + i*lhsStride;
513 for (Index j=0; j<alignedStart; ++j)
514 tmp0 += cj.pmul(lhs0[j], rhs[j]);
516 if (alignedSize>alignedStart)
519 if ((
size_t(lhs0+alignedStart)%
sizeof(LhsPacket))==0)
520 for (Index j = alignedStart;j<alignedSize;j+=RhsPacketSize)
521 ptmp0 = pcj.pmadd(pload<LhsPacket>(&lhs0[j]), pload<RhsPacket>(&rhs[j]), ptmp0);
523 for (Index j = alignedStart;j<alignedSize;j+=RhsPacketSize)
524 ptmp0 = pcj.pmadd(ploadu<LhsPacket>(&lhs0[j]), pload<RhsPacket>(&rhs[j]), ptmp0);
525 tmp0 += predux(ptmp0);
530 for (Index j=alignedSize; j<depth; ++j)
531 tmp0 += cj.pmul(lhs0[j], rhs[j]);
532 res[i*resIncr] += alpha*tmp0;
542 }
while(Vectorizable);
544 #undef _EIGEN_ACCUMULATE_PACKETS
552 #endif // EIGEN_GENERAL_MATRIX_VECTOR_H