Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
Loading...
Searching...
No Matches
AltiVec/PacketMath.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2016 Konstantinos Margaritis <[email protected]>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_PACKET_MATH_ALTIVEC_H
11#define EIGEN_PACKET_MATH_ALTIVEC_H
12
13// IWYU pragma: private
14#include "../../InternalHeaderCheck.h"
15
16namespace Eigen {
17
18namespace internal {
19
20#ifndef EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD
21#define EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD 4
22#endif
23
24#ifndef EIGEN_HAS_SINGLE_INSTRUCTION_MADD
25#define EIGEN_HAS_SINGLE_INSTRUCTION_MADD
26#endif
27
28// NOTE Altivec has 32 registers, but Eigen only accepts a value of 8 or 16
29#ifndef EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS
30#define EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS 32
31#endif
32
33typedef __vector float Packet4f;
34typedef __vector int Packet4i;
35typedef __vector unsigned int Packet4ui;
36typedef __vector __bool int Packet4bi;
37typedef __vector short int Packet8s;
38typedef __vector unsigned short int Packet8us;
39typedef __vector __bool short Packet8bi;
40typedef __vector signed char Packet16c;
41typedef __vector unsigned char Packet16uc;
42typedef eigen_packet_wrapper<__vector unsigned short int, 0> Packet8bf;
43
44// We don't want to write the same code all the time, but we need to reuse the constants
45// and it doesn't really work to declare them global, so we define macros instead
46#define EIGEN_DECLARE_CONST_FAST_Packet4f(NAME, X) Packet4f p4f_##NAME = {X, X, X, X}
47
48#define EIGEN_DECLARE_CONST_FAST_Packet4i(NAME, X) Packet4i p4i_##NAME = vec_splat_s32(X)
49
50#define EIGEN_DECLARE_CONST_FAST_Packet4ui(NAME, X) Packet4ui p4ui_##NAME = {X, X, X, X}
51
52#define EIGEN_DECLARE_CONST_FAST_Packet8us(NAME, X) Packet8us p8us_##NAME = {X, X, X, X, X, X, X, X}
53
54#define EIGEN_DECLARE_CONST_FAST_Packet16uc(NAME, X) \
55 Packet16uc p16uc_##NAME = {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X}
56
57#define EIGEN_DECLARE_CONST_Packet4f(NAME, X) Packet4f p4f_##NAME = pset1<Packet4f>(X)
58
59#define EIGEN_DECLARE_CONST_Packet4i(NAME, X) Packet4i p4i_##NAME = pset1<Packet4i>(X)
60
61#define EIGEN_DECLARE_CONST_Packet2d(NAME, X) Packet2d p2d_##NAME = pset1<Packet2d>(X)
62
63#define EIGEN_DECLARE_CONST_Packet2l(NAME, X) Packet2l p2l_##NAME = pset1<Packet2l>(X)
64
65#define EIGEN_DECLARE_CONST_Packet4f_FROM_INT(NAME, X) \
66 const Packet4f p4f_##NAME = reinterpret_cast<Packet4f>(pset1<Packet4i>(X))
67
68#define DST_CHAN 1
69#define DST_CTRL(size, count, stride) (((size) << 24) | ((count) << 16) | (stride))
70#define __UNPACK_TYPE__(PACKETNAME) typename unpacket_traits<PACKETNAME>::type
71
72// These constants are endian-agnostic
73static EIGEN_DECLARE_CONST_FAST_Packet4f(ZERO, 0); //{ 0.0, 0.0, 0.0, 0.0}
74static EIGEN_DECLARE_CONST_FAST_Packet4i(ZERO, 0); //{ 0, 0, 0, 0,}
75static EIGEN_DECLARE_CONST_FAST_Packet4i(ONE, 1); //{ 1, 1, 1, 1}
76static EIGEN_DECLARE_CONST_FAST_Packet4i(MINUS16, -16); //{ -16, -16, -16, -16}
77static EIGEN_DECLARE_CONST_FAST_Packet4i(MINUS1, -1); //{ -1, -1, -1, -1}
78static EIGEN_DECLARE_CONST_FAST_Packet4ui(SIGN, 0x80000000u);
79static EIGEN_DECLARE_CONST_FAST_Packet4ui(PREV0DOT5, 0x3EFFFFFFu);
80static EIGEN_DECLARE_CONST_FAST_Packet8us(ONE, 1); //{ 1, 1, 1, 1, 1, 1, 1, 1}
81static Packet4f p4f_MZERO =
82 (Packet4f)vec_sl((Packet4ui)p4i_MINUS1, (Packet4ui)p4i_MINUS1); //{ 0x80000000, 0x80000000, 0x80000000, 0x80000000}
83#ifndef __VSX__
84static Packet4f p4f_ONE = vec_ctf(p4i_ONE, 0); //{ 1.0, 1.0, 1.0, 1.0}
85#endif
86
87static Packet4f p4f_COUNTDOWN = {0.0, 1.0, 2.0, 3.0};
88static Packet4i p4i_COUNTDOWN = {0, 1, 2, 3};
89static Packet8s p8s_COUNTDOWN = {0, 1, 2, 3, 4, 5, 6, 7};
90static Packet8us p8us_COUNTDOWN = {0, 1, 2, 3, 4, 5, 6, 7};
91
92static Packet16c p16c_COUNTDOWN = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
93static Packet16uc p16uc_COUNTDOWN = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
94
95static Packet16uc p16uc_REVERSE32 = {12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3};
96static Packet16uc p16uc_REVERSE16 = {14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1};
97static Packet16uc p16uc_REVERSE8 = {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
98
99#ifdef _BIG_ENDIAN
100static Packet16uc p16uc_DUPLICATE32_HI = {0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 4, 5, 6, 7};
101#endif
102static const Packet16uc p16uc_DUPLICATE16_EVEN = {0, 1, 0, 1, 4, 5, 4, 5, 8, 9, 8, 9, 12, 13, 12, 13};
103static const Packet16uc p16uc_DUPLICATE16_ODD = {2, 3, 2, 3, 6, 7, 6, 7, 10, 11, 10, 11, 14, 15, 14, 15};
104
105static Packet16uc p16uc_QUADRUPLICATE16_HI = {0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3};
106static Packet16uc p16uc_QUADRUPLICATE16 = {0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3};
107
108static Packet16uc p16uc_MERGEE16 = {0, 1, 16, 17, 4, 5, 20, 21, 8, 9, 24, 25, 12, 13, 28, 29};
109static Packet16uc p16uc_MERGEO16 = {2, 3, 18, 19, 6, 7, 22, 23, 10, 11, 26, 27, 14, 15, 30, 31};
110#ifdef _BIG_ENDIAN
111static Packet16uc p16uc_MERGEH16 = {0, 1, 4, 5, 8, 9, 12, 13, 16, 17, 20, 21, 24, 25, 28, 29};
112#else
113static Packet16uc p16uc_MERGEL16 = {2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31};
114#endif
115
116// Handle endianness properly while loading constants
117// Define global static constants:
118#ifdef _BIG_ENDIAN
119static Packet16uc p16uc_FORWARD = vec_lvsl(0, (float*)0);
120static Packet16uc p16uc_PSET32_WODD =
121 vec_sld((Packet16uc)vec_splat((Packet4ui)p16uc_FORWARD, 0), (Packet16uc)vec_splat((Packet4ui)p16uc_FORWARD, 2),
122 8); //{ 0,1,2,3, 0,1,2,3, 8,9,10,11, 8,9,10,11 };
123static Packet16uc p16uc_PSET32_WEVEN = vec_sld(p16uc_DUPLICATE32_HI, (Packet16uc)vec_splat((Packet4ui)p16uc_FORWARD, 3),
124 8); //{ 4,5,6,7, 4,5,6,7, 12,13,14,15, 12,13,14,15 };
125static Packet16uc p16uc_HALF64_0_16 = vec_sld((Packet16uc)p4i_ZERO, vec_splat((Packet16uc)vec_abs(p4i_MINUS16), 3),
126 8); //{ 0,0,0,0, 0,0,0,0, 16,16,16,16, 16,16,16,16};
127#else
128static Packet16uc p16uc_FORWARD = p16uc_REVERSE32;
129static Packet16uc p16uc_PSET32_WODD =
130 vec_sld((Packet16uc)vec_splat((Packet4ui)p16uc_FORWARD, 1), (Packet16uc)vec_splat((Packet4ui)p16uc_FORWARD, 3),
131 8); //{ 0,1,2,3, 0,1,2,3, 8,9,10,11, 8,9,10,11 };
132static Packet16uc p16uc_PSET32_WEVEN =
133 vec_sld((Packet16uc)vec_splat((Packet4ui)p16uc_FORWARD, 0), (Packet16uc)vec_splat((Packet4ui)p16uc_FORWARD, 2),
134 8); //{ 4,5,6,7, 4,5,6,7, 12,13,14,15, 12,13,14,15 };
135static Packet16uc p16uc_HALF64_0_16 = vec_sld(vec_splat((Packet16uc)vec_abs(p4i_MINUS16), 0), (Packet16uc)p4i_ZERO,
136 8); //{ 0,0,0,0, 0,0,0,0, 16,16,16,16, 16,16,16,16};
137#endif // _BIG_ENDIAN
138
139static Packet16uc p16uc_PSET64_HI = (Packet16uc)vec_mergeh(
140 (Packet4ui)p16uc_PSET32_WODD, (Packet4ui)p16uc_PSET32_WEVEN); //{ 0,1,2,3, 4,5,6,7, 0,1,2,3, 4,5,6,7 };
141static Packet16uc p16uc_PSET64_LO = (Packet16uc)vec_mergel(
142 (Packet4ui)p16uc_PSET32_WODD, (Packet4ui)p16uc_PSET32_WEVEN); //{ 8,9,10,11, 12,13,14,15, 8,9,10,11, 12,13,14,15 };
143static Packet16uc p16uc_TRANSPOSE64_HI =
144 p16uc_PSET64_HI + p16uc_HALF64_0_16; //{ 0,1,2,3, 4,5,6,7, 16,17,18,19, 20,21,22,23};
145static Packet16uc p16uc_TRANSPOSE64_LO =
146 p16uc_PSET64_LO + p16uc_HALF64_0_16; //{ 8,9,10,11, 12,13,14,15, 24,25,26,27, 28,29,30,31};
147
148static Packet16uc p16uc_COMPLEX32_REV =
149 vec_sld(p16uc_REVERSE32, p16uc_REVERSE32, 8); //{ 4,5,6,7, 0,1,2,3, 12,13,14,15, 8,9,10,11 };
150
151#if EIGEN_HAS_BUILTIN(__builtin_prefetch) || EIGEN_COMP_GNUC
152#define EIGEN_PPC_PREFETCH(ADDR) __builtin_prefetch(ADDR);
153#else
154#define EIGEN_PPC_PREFETCH(ADDR) asm(" dcbt [%[addr]]\n" ::[addr] "r"(ADDR) : "cc");
155#endif
156
157#if EIGEN_COMP_LLVM
158#define LOAD_STORE_UNROLL_16 _Pragma("unroll 16")
159#else
160#define LOAD_STORE_UNROLL_16 _Pragma("GCC unroll(16)")
161#endif
162
163template <>
164struct packet_traits<float> : default_packet_traits {
165 typedef Packet4f type;
166 typedef Packet4f half;
167 enum {
168 Vectorizable = 1,
169 AlignedOnScalar = 1,
170 size = 4,
171
172 HasAdd = 1,
173 HasSub = 1,
174 HasMul = 1,
175 HasDiv = 1,
176 HasMin = 1,
177 HasMax = 1,
178 HasAbs = 1,
179 HasSin = EIGEN_FAST_MATH,
180 HasCos = EIGEN_FAST_MATH,
181 HasACos = 1,
182 HasASin = 1,
183 HasATan = 1,
184 HasATanh = 1,
185 HasLog = 1,
186 HasExp = 1,
187#ifdef EIGEN_VECTORIZE_VSX
188 HasSqrt = 1,
189#if !EIGEN_COMP_CLANG
190 HasRsqrt = 1,
191#else
192 HasRsqrt = 0,
193#endif
194 HasTanh = EIGEN_FAST_MATH,
195 HasErf = EIGEN_FAST_MATH,
196#else
197 HasSqrt = 0,
198 HasRsqrt = 0,
199 HasTanh = 0,
200 HasErf = 0,
201#endif
202 HasNegate = 1,
203 HasBlend = 1
204 };
205};
206template <>
207struct packet_traits<bfloat16> : default_packet_traits {
208 typedef Packet8bf type;
209 typedef Packet8bf half;
210 enum {
211 Vectorizable = 1,
212 AlignedOnScalar = 1,
213 size = 8,
214
215 HasAdd = 1,
216 HasSub = 1,
217 HasMul = 1,
218 HasDiv = 1,
219 HasMin = 1,
220 HasMax = 1,
221 HasAbs = 1,
222 HasSin = EIGEN_FAST_MATH,
223 HasCos = EIGEN_FAST_MATH,
224 HasLog = 1,
225 HasExp = 1,
226#ifdef EIGEN_VECTORIZE_VSX
227 HasSqrt = 1,
228#if !EIGEN_COMP_CLANG
229 HasRsqrt = 1,
230#else
231 HasRsqrt = 0,
232#endif
233#else
234 HasSqrt = 0,
235 HasRsqrt = 0,
236#endif
237 HasTanh = 0,
238 HasErf = 0,
239 HasNegate = 1,
240 HasBlend = 1
241 };
242};
243
244template <>
245struct packet_traits<int> : default_packet_traits {
246 typedef Packet4i type;
247 typedef Packet4i half;
248 enum {
249 Vectorizable = 1,
250 AlignedOnScalar = 1,
251 size = 4,
252
253 HasAdd = 1,
254 HasSub = 1,
255 HasShift = 1,
256 HasMul = 1,
257#if defined(_ARCH_PWR10) && (EIGEN_COMP_LLVM || EIGEN_GNUC_STRICT_AT_LEAST(11, 0, 0))
258 HasDiv = 1,
259#else
260 HasDiv = 0,
261#endif
262 HasBlend = 1,
263 HasCmp = 1
264 };
265};
266
267template <>
268struct packet_traits<short int> : default_packet_traits {
269 typedef Packet8s type;
270 typedef Packet8s half;
271 enum {
272 Vectorizable = 1,
273 AlignedOnScalar = 1,
274 size = 8,
275
276 HasAdd = 1,
277 HasSub = 1,
278 HasMul = 1,
279 HasDiv = 0,
280 HasBlend = 1,
281 HasCmp = 1
282 };
283};
284
285template <>
286struct packet_traits<unsigned short int> : default_packet_traits {
287 typedef Packet8us type;
288 typedef Packet8us half;
289 enum {
290 Vectorizable = 1,
291 AlignedOnScalar = 1,
292 size = 8,
293
294 HasAdd = 1,
295 HasSub = 1,
296 HasMul = 1,
297 HasDiv = 0,
298 HasBlend = 1,
299 HasCmp = 1
300 };
301};
302
303template <>
304struct packet_traits<signed char> : default_packet_traits {
305 typedef Packet16c type;
306 typedef Packet16c half;
307 enum {
308 Vectorizable = 1,
309 AlignedOnScalar = 1,
310 size = 16,
311
312 HasAdd = 1,
313 HasSub = 1,
314 HasMul = 1,
315 HasDiv = 0,
316 HasBlend = 1,
317 HasCmp = 1
318 };
319};
320
321template <>
322struct packet_traits<unsigned char> : default_packet_traits {
323 typedef Packet16uc type;
324 typedef Packet16uc half;
325 enum {
326 Vectorizable = 1,
327 AlignedOnScalar = 1,
328 size = 16,
329
330 HasAdd = 1,
331 HasSub = 1,
332 HasMul = 1,
333 HasDiv = 0,
334 HasBlend = 1,
335 HasCmp = 1
336 };
337};
338
339template <>
340struct unpacket_traits<Packet4f> {
341 typedef float type;
342 typedef Packet4f half;
343 typedef Packet4i integer_packet;
344 enum {
345 size = 4,
346 alignment = Aligned16,
347 vectorizable = true,
348 masked_load_available = false,
349 masked_store_available = false
350 };
351};
352template <>
353struct unpacket_traits<Packet4i> {
354 typedef int type;
355 typedef Packet4i half;
356 enum {
357 size = 4,
358 alignment = Aligned16,
359 vectorizable = true,
360 masked_load_available = false,
361 masked_store_available = false
362 };
363};
364template <>
365struct unpacket_traits<Packet8s> {
366 typedef short int type;
367 typedef Packet8s half;
368 enum {
369 size = 8,
370 alignment = Aligned16,
371 vectorizable = true,
372 masked_load_available = false,
373 masked_store_available = false
374 };
375};
376template <>
377struct unpacket_traits<Packet8us> {
378 typedef unsigned short int type;
379 typedef Packet8us half;
380 enum {
381 size = 8,
382 alignment = Aligned16,
383 vectorizable = true,
384 masked_load_available = false,
385 masked_store_available = false
386 };
387};
388
389template <>
390struct unpacket_traits<Packet16c> {
391 typedef signed char type;
392 typedef Packet16c half;
393 enum {
394 size = 16,
395 alignment = Aligned16,
396 vectorizable = true,
397 masked_load_available = false,
398 masked_store_available = false
399 };
400};
401template <>
402struct unpacket_traits<Packet16uc> {
403 typedef unsigned char type;
404 typedef Packet16uc half;
405 enum {
406 size = 16,
407 alignment = Aligned16,
408 vectorizable = true,
409 masked_load_available = false,
410 masked_store_available = false
411 };
412};
413
414template <>
415struct unpacket_traits<Packet8bf> {
416 typedef bfloat16 type;
417 typedef Packet8bf half;
418 enum {
419 size = 8,
420 alignment = Aligned16,
421 vectorizable = true,
422 masked_load_available = false,
423 masked_store_available = false
424 };
425};
426inline std::ostream& operator<<(std::ostream& s, const Packet16c& v) {
427 union {
428 Packet16c v;
429 signed char n[16];
430 } vt;
431 vt.v = v;
432 for (int i = 0; i < 16; i++) s << vt.n[i] << ", ";
433 return s;
434}
435
436inline std::ostream& operator<<(std::ostream& s, const Packet16uc& v) {
437 union {
438 Packet16uc v;
439 unsigned char n[16];
440 } vt;
441 vt.v = v;
442 for (int i = 0; i < 16; i++) s << vt.n[i] << ", ";
443 return s;
444}
445
446inline std::ostream& operator<<(std::ostream& s, const Packet4f& v) {
447 union {
448 Packet4f v;
449 float n[4];
450 } vt;
451 vt.v = v;
452 s << vt.n[0] << ", " << vt.n[1] << ", " << vt.n[2] << ", " << vt.n[3];
453 return s;
454}
455
456inline std::ostream& operator<<(std::ostream& s, const Packet4i& v) {
457 union {
458 Packet4i v;
459 int n[4];
460 } vt;
461 vt.v = v;
462 s << vt.n[0] << ", " << vt.n[1] << ", " << vt.n[2] << ", " << vt.n[3];
463 return s;
464}
465
466inline std::ostream& operator<<(std::ostream& s, const Packet4ui& v) {
467 union {
468 Packet4ui v;
469 unsigned int n[4];
470 } vt;
471 vt.v = v;
472 s << vt.n[0] << ", " << vt.n[1] << ", " << vt.n[2] << ", " << vt.n[3];
473 return s;
474}
475
476template <typename Packet>
477EIGEN_STRONG_INLINE Packet pload_common(const __UNPACK_TYPE__(Packet) * from) {
478 // some versions of GCC throw "unused-but-set-parameter".
479 // ignoring these warnings for now.
480 EIGEN_UNUSED_VARIABLE(from);
481 EIGEN_DEBUG_ALIGNED_LOAD
482#ifdef EIGEN_VECTORIZE_VSX
483 return vec_xl(0, const_cast<__UNPACK_TYPE__(Packet)*>(from));
484#else
485 return vec_ld(0, from);
486#endif
487}
488
489// Need to define them first or we get specialization after instantiation errors
490template <>
491EIGEN_STRONG_INLINE Packet4f pload<Packet4f>(const float* from) {
492 return pload_common<Packet4f>(from);
493}
494
495template <>
496EIGEN_STRONG_INLINE Packet4i pload<Packet4i>(const int* from) {
497 return pload_common<Packet4i>(from);
498}
499
500template <>
501EIGEN_STRONG_INLINE Packet8s pload<Packet8s>(const short int* from) {
502 return pload_common<Packet8s>(from);
503}
504
505template <>
506EIGEN_STRONG_INLINE Packet8us pload<Packet8us>(const unsigned short int* from) {
507 return pload_common<Packet8us>(from);
508}
509
510template <>
511EIGEN_STRONG_INLINE Packet16c pload<Packet16c>(const signed char* from) {
512 return pload_common<Packet16c>(from);
513}
514
515template <>
516EIGEN_STRONG_INLINE Packet16uc pload<Packet16uc>(const unsigned char* from) {
517 return pload_common<Packet16uc>(from);
518}
519
520template <>
521EIGEN_STRONG_INLINE Packet8bf pload<Packet8bf>(const bfloat16* from) {
522 return pload_common<Packet8us>(reinterpret_cast<const unsigned short int*>(from));
523}
524
525template <typename Packet>
526EIGEN_ALWAYS_INLINE Packet pload_ignore(const __UNPACK_TYPE__(Packet) * from) {
527 // some versions of GCC throw "unused-but-set-parameter".
528 // ignoring these warnings for now.
529 EIGEN_UNUSED_VARIABLE(from);
530 EIGEN_DEBUG_ALIGNED_LOAD
531 // Ignore partial input memory initialized
532#if !EIGEN_COMP_LLVM
533#pragma GCC diagnostic push
534#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
535#endif
536#ifdef EIGEN_VECTORIZE_VSX
537 return vec_xl(0, const_cast<__UNPACK_TYPE__(Packet)*>(from));
538#else
539 return vec_ld(0, from);
540#endif
541#if !EIGEN_COMP_LLVM
542#pragma GCC diagnostic pop
543#endif
544}
545
546template <>
547EIGEN_ALWAYS_INLINE Packet8bf pload_ignore<Packet8bf>(const bfloat16* from) {
548 return pload_ignore<Packet8us>(reinterpret_cast<const unsigned short int*>(from));
549}
550
551template <typename Packet>
552EIGEN_ALWAYS_INLINE Packet pload_partial_common(const __UNPACK_TYPE__(Packet) * from, const Index n,
553 const Index offset) {
554 // some versions of GCC throw "unused-but-set-parameter".
555 // ignoring these warnings for now.
556 const Index packet_size = unpacket_traits<Packet>::size;
557 eigen_internal_assert(n + offset <= packet_size && "number of elements plus offset will read past end of packet");
558 const Index size = sizeof(__UNPACK_TYPE__(Packet));
559#ifdef _ARCH_PWR9
560 EIGEN_UNUSED_VARIABLE(packet_size);
561 EIGEN_DEBUG_ALIGNED_LOAD
562 EIGEN_UNUSED_VARIABLE(from);
563 Packet load = vec_xl_len(const_cast<__UNPACK_TYPE__(Packet)*>(from), n * size);
564 if (offset) {
565 Packet16uc shift = pset1<Packet16uc>(offset * 8 * size);
566#ifdef _BIG_ENDIAN
567 load = Packet(vec_sro(Packet16uc(load), shift));
568#else
569 load = Packet(vec_slo(Packet16uc(load), shift));
570#endif
571 }
572 return load;
573#else
574 if (n) {
575 EIGEN_ALIGN16 __UNPACK_TYPE__(Packet) load[packet_size];
576 unsigned char* load2 = reinterpret_cast<unsigned char*>(load + offset);
577 unsigned char* from2 = reinterpret_cast<unsigned char*>(const_cast<__UNPACK_TYPE__(Packet)*>(from));
578 Index n2 = n * size;
579 if (16 <= n2) {
580 pstoreu(load2, ploadu<Packet16uc>(from2));
581 } else {
582 memcpy((void*)load2, (void*)from2, n2);
583 }
584 return pload_ignore<Packet>(load);
585 } else {
586 return Packet(pset1<Packet16uc>(0));
587 }
588#endif
589}
590
591template <>
592EIGEN_ALWAYS_INLINE Packet4f pload_partial<Packet4f>(const float* from, const Index n, const Index offset) {
593 return pload_partial_common<Packet4f>(from, n, offset);
594}
595
596template <>
597EIGEN_ALWAYS_INLINE Packet4i pload_partial<Packet4i>(const int* from, const Index n, const Index offset) {
598 return pload_partial_common<Packet4i>(from, n, offset);
599}
600
601template <>
602EIGEN_ALWAYS_INLINE Packet8s pload_partial<Packet8s>(const short int* from, const Index n, const Index offset) {
603 return pload_partial_common<Packet8s>(from, n, offset);
604}
605
606template <>
607EIGEN_ALWAYS_INLINE Packet8us pload_partial<Packet8us>(const unsigned short int* from, const Index n,
608 const Index offset) {
609 return pload_partial_common<Packet8us>(from, n, offset);
610}
611
612template <>
613EIGEN_ALWAYS_INLINE Packet8bf pload_partial<Packet8bf>(const bfloat16* from, const Index n, const Index offset) {
614 return pload_partial_common<Packet8us>(reinterpret_cast<const unsigned short int*>(from), n, offset);
615}
616
617template <>
618EIGEN_ALWAYS_INLINE Packet16c pload_partial<Packet16c>(const signed char* from, const Index n, const Index offset) {
619 return pload_partial_common<Packet16c>(from, n, offset);
620}
621
622template <>
623EIGEN_ALWAYS_INLINE Packet16uc pload_partial<Packet16uc>(const unsigned char* from, const Index n, const Index offset) {
624 return pload_partial_common<Packet16uc>(from, n, offset);
625}
626
627template <typename Packet>
628EIGEN_STRONG_INLINE void pstore_common(__UNPACK_TYPE__(Packet) * to, const Packet& from) {
629 // some versions of GCC throw "unused-but-set-parameter" (float *to).
630 // ignoring these warnings for now.
631 EIGEN_UNUSED_VARIABLE(to);
632 EIGEN_DEBUG_ALIGNED_STORE
633#ifdef EIGEN_VECTORIZE_VSX
634 vec_xst(from, 0, to);
635#else
636 vec_st(from, 0, to);
637#endif
638}
639
640template <>
641EIGEN_STRONG_INLINE void pstore<float>(float* to, const Packet4f& from) {
642 pstore_common<Packet4f>(to, from);
643}
644
645template <>
646EIGEN_STRONG_INLINE void pstore<int>(int* to, const Packet4i& from) {
647 pstore_common<Packet4i>(to, from);
648}
649
650template <>
651EIGEN_STRONG_INLINE void pstore<short int>(short int* to, const Packet8s& from) {
652 pstore_common<Packet8s>(to, from);
653}
654
655template <>
656EIGEN_STRONG_INLINE void pstore<unsigned short int>(unsigned short int* to, const Packet8us& from) {
657 pstore_common<Packet8us>(to, from);
658}
659
660template <>
661EIGEN_STRONG_INLINE void pstore<bfloat16>(bfloat16* to, const Packet8bf& from) {
662 pstore_common<Packet8us>(reinterpret_cast<unsigned short int*>(to), from.m_val);
663}
664
665template <>
666EIGEN_STRONG_INLINE void pstore<signed char>(signed char* to, const Packet16c& from) {
667 pstore_common<Packet16c>(to, from);
668}
669
670template <>
671EIGEN_STRONG_INLINE void pstore<unsigned char>(unsigned char* to, const Packet16uc& from) {
672 pstore_common<Packet16uc>(to, from);
673}
674
675template <typename Packet>
676EIGEN_ALWAYS_INLINE void pstore_partial_common(__UNPACK_TYPE__(Packet) * to, const Packet& from, const Index n,
677 const Index offset) {
678 // some versions of GCC throw "unused-but-set-parameter" (float *to).
679 // ignoring these warnings for now.
680 const Index packet_size = unpacket_traits<Packet>::size;
681 eigen_internal_assert(n + offset <= packet_size && "number of elements plus offset will write past end of packet");
682 const Index size = sizeof(__UNPACK_TYPE__(Packet));
683#ifdef _ARCH_PWR9
684 EIGEN_UNUSED_VARIABLE(packet_size);
685 EIGEN_UNUSED_VARIABLE(to);
686 EIGEN_DEBUG_ALIGNED_STORE
687 Packet store = from;
688 if (offset) {
689 Packet16uc shift = pset1<Packet16uc>(offset * 8 * size);
690#ifdef _BIG_ENDIAN
691 store = Packet(vec_slo(Packet16uc(store), shift));
692#else
693 store = Packet(vec_sro(Packet16uc(store), shift));
694#endif
695 }
696 vec_xst_len(store, to, n * size);
697#else
698 if (n) {
699 EIGEN_ALIGN16 __UNPACK_TYPE__(Packet) store[packet_size];
700 pstore(store, from);
701 unsigned char* store2 = reinterpret_cast<unsigned char*>(store + offset);
702 unsigned char* to2 = reinterpret_cast<unsigned char*>(to);
703 Index n2 = n * size;
704 if (16 <= n2) {
705 pstore(to2, ploadu<Packet16uc>(store2));
706 } else {
707 memcpy((void*)to2, (void*)store2, n2);
708 }
709 }
710#endif
711}
712
713template <>
714EIGEN_ALWAYS_INLINE void pstore_partial<float>(float* to, const Packet4f& from, const Index n, const Index offset) {
715 pstore_partial_common<Packet4f>(to, from, n, offset);
716}
717
718template <>
719EIGEN_ALWAYS_INLINE void pstore_partial<int>(int* to, const Packet4i& from, const Index n, const Index offset) {
720 pstore_partial_common<Packet4i>(to, from, n, offset);
721}
722
723template <>
724EIGEN_ALWAYS_INLINE void pstore_partial<short int>(short int* to, const Packet8s& from, const Index n,
725 const Index offset) {
726 pstore_partial_common<Packet8s>(to, from, n, offset);
727}
728
729template <>
730EIGEN_ALWAYS_INLINE void pstore_partial<unsigned short int>(unsigned short int* to, const Packet8us& from,
731 const Index n, const Index offset) {
732 pstore_partial_common<Packet8us>(to, from, n, offset);
733}
734
735template <>
736EIGEN_ALWAYS_INLINE void pstore_partial<bfloat16>(bfloat16* to, const Packet8bf& from, const Index n,
737 const Index offset) {
738 pstore_partial_common<Packet8us>(reinterpret_cast<unsigned short int*>(to), from.m_val, n, offset);
739}
740
741template <>
742EIGEN_ALWAYS_INLINE void pstore_partial<signed char>(signed char* to, const Packet16c& from, const Index n,
743 const Index offset) {
744 pstore_partial_common<Packet16c>(to, from, n, offset);
745}
746
747template <>
748EIGEN_ALWAYS_INLINE void pstore_partial<unsigned char>(unsigned char* to, const Packet16uc& from, const Index n,
749 const Index offset) {
750 pstore_partial_common<Packet16uc>(to, from, n, offset);
751}
752
753template <typename Packet>
754EIGEN_STRONG_INLINE Packet pset1_size4(const __UNPACK_TYPE__(Packet) & from) {
755 Packet v = {from, from, from, from};
756 return v;
757}
758
759template <typename Packet>
760EIGEN_STRONG_INLINE Packet pset1_size8(const __UNPACK_TYPE__(Packet) & from) {
761 Packet v = {from, from, from, from, from, from, from, from};
762 return v;
763}
764
765template <typename Packet>
766EIGEN_STRONG_INLINE Packet pset1_size16(const __UNPACK_TYPE__(Packet) & from) {
767 Packet v = {from, from, from, from, from, from, from, from, from, from, from, from, from, from, from, from};
768 return v;
769}
770
771template <>
772EIGEN_STRONG_INLINE Packet4f pset1<Packet4f>(const float& from) {
773 return pset1_size4<Packet4f>(from);
774}
775
776template <>
777EIGEN_STRONG_INLINE Packet4i pset1<Packet4i>(const int& from) {
778 return pset1_size4<Packet4i>(from);
779}
780
781template <>
782EIGEN_STRONG_INLINE Packet8s pset1<Packet8s>(const short int& from) {
783 return pset1_size8<Packet8s>(from);
784}
785
786template <>
787EIGEN_STRONG_INLINE Packet8us pset1<Packet8us>(const unsigned short int& from) {
788 return pset1_size8<Packet8us>(from);
789}
790
791template <>
792EIGEN_STRONG_INLINE Packet16c pset1<Packet16c>(const signed char& from) {
793 return pset1_size16<Packet16c>(from);
794}
795
796template <>
797EIGEN_STRONG_INLINE Packet16uc pset1<Packet16uc>(const unsigned char& from) {
798 return pset1_size16<Packet16uc>(from);
799}
800
801template <>
802EIGEN_STRONG_INLINE Packet4f pset1frombits<Packet4f>(unsigned int from) {
803 return reinterpret_cast<Packet4f>(pset1<Packet4i>(from));
804}
805
806template <>
807EIGEN_STRONG_INLINE Packet8bf pset1<Packet8bf>(const bfloat16& from) {
808 return pset1_size8<Packet8us>(reinterpret_cast<const unsigned short int&>(from));
809}
810
811template <typename Packet>
812EIGEN_STRONG_INLINE void pbroadcast4_common(const __UNPACK_TYPE__(Packet) * a, Packet& a0, Packet& a1, Packet& a2,
813 Packet& a3) {
814 a3 = pload<Packet>(a);
815 a0 = vec_splat(a3, 0);
816 a1 = vec_splat(a3, 1);
817 a2 = vec_splat(a3, 2);
818 a3 = vec_splat(a3, 3);
819}
820
821template <>
822EIGEN_STRONG_INLINE void pbroadcast4<Packet4f>(const float* a, Packet4f& a0, Packet4f& a1, Packet4f& a2, Packet4f& a3) {
823 pbroadcast4_common<Packet4f>(a, a0, a1, a2, a3);
824}
825template <>
826EIGEN_STRONG_INLINE void pbroadcast4<Packet4i>(const int* a, Packet4i& a0, Packet4i& a1, Packet4i& a2, Packet4i& a3) {
827 pbroadcast4_common<Packet4i>(a, a0, a1, a2, a3);
828}
829
830template <typename Packet>
831EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Packet pgather_common(const __UNPACK_TYPE__(Packet) * from, Index stride,
832 const Index n = unpacket_traits<Packet>::size) {
833 EIGEN_ALIGN16 __UNPACK_TYPE__(Packet) a[unpacket_traits<Packet>::size];
834 eigen_internal_assert(n <= unpacket_traits<Packet>::size && "number of elements will gather past end of packet");
835 if (stride == 1) {
836 if (n == unpacket_traits<Packet>::size) {
837 return ploadu<Packet>(from);
838 } else {
839 return ploadu_partial<Packet>(from, n);
840 }
841 } else {
842 LOAD_STORE_UNROLL_16
843 for (Index i = 0; i < n; i++) {
844 a[i] = from[i * stride];
845 }
846 // Leave rest of the array uninitialized
847 return pload_ignore<Packet>(a);
848 }
849}
850
851template <>
852EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Packet4f pgather<float, Packet4f>(const float* from, Index stride) {
853 return pgather_common<Packet4f>(from, stride);
854}
855
856template <>
857EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Packet4i pgather<int, Packet4i>(const int* from, Index stride) {
858 return pgather_common<Packet4i>(from, stride);
859}
860
861template <>
862EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Packet8s pgather<short int, Packet8s>(const short int* from, Index stride) {
863 return pgather_common<Packet8s>(from, stride);
864}
865
866template <>
867EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Packet8us pgather<unsigned short int, Packet8us>(const unsigned short int* from,
868 Index stride) {
869 return pgather_common<Packet8us>(from, stride);
870}
871
872template <>
873EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Packet8bf pgather<bfloat16, Packet8bf>(const bfloat16* from, Index stride) {
874 return pgather_common<Packet8bf>(from, stride);
875}
876
877template <>
878EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Packet16c pgather<signed char, Packet16c>(const signed char* from, Index stride) {
879 return pgather_common<Packet16c>(from, stride);
880}
881
882template <>
883EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Packet16uc pgather<unsigned char, Packet16uc>(const unsigned char* from,
884 Index stride) {
885 return pgather_common<Packet16uc>(from, stride);
886}
887
888template <>
889EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Packet4f pgather_partial<float, Packet4f>(const float* from, Index stride,
890 const Index n) {
891 return pgather_common<Packet4f>(from, stride, n);
892}
893
894template <>
895EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Packet4i pgather_partial<int, Packet4i>(const int* from, Index stride,
896 const Index n) {
897 return pgather_common<Packet4i>(from, stride, n);
898}
899
900template <>
901EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Packet8s pgather_partial<short int, Packet8s>(const short int* from, Index stride,
902 const Index n) {
903 return pgather_common<Packet8s>(from, stride, n);
904}
905
906template <>
907EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Packet8us
908pgather_partial<unsigned short int, Packet8us>(const unsigned short int* from, Index stride, const Index n) {
909 return pgather_common<Packet8us>(from, stride, n);
910}
911
912template <>
913EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Packet8bf pgather_partial<bfloat16, Packet8bf>(const bfloat16* from, Index stride,
914 const Index n) {
915 return pgather_common<Packet8bf>(from, stride, n);
916}
917
918template <>
919EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Packet16c pgather_partial<signed char, Packet16c>(const signed char* from,
920 Index stride, const Index n) {
921 return pgather_common<Packet16c>(from, stride, n);
922}
923
924template <>
925EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Packet16uc pgather_partial<unsigned char, Packet16uc>(const unsigned char* from,
926 Index stride,
927 const Index n) {
928 return pgather_common<Packet16uc>(from, stride, n);
929}
930
931template <typename Packet>
932EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void pscatter_common(__UNPACK_TYPE__(Packet) * to, const Packet& from,
933 Index stride,
934 const Index n = unpacket_traits<Packet>::size) {
935 EIGEN_ALIGN16 __UNPACK_TYPE__(Packet) a[unpacket_traits<Packet>::size];
936 eigen_internal_assert(n <= unpacket_traits<Packet>::size && "number of elements will scatter past end of packet");
937 if (stride == 1) {
938 if (n == unpacket_traits<Packet>::size) {
939 return pstoreu(to, from);
940 } else {
941 return pstoreu_partial(to, from, n);
942 }
943 } else {
944 pstore<__UNPACK_TYPE__(Packet)>(a, from);
945 LOAD_STORE_UNROLL_16
946 for (Index i = 0; i < n; i++) {
947 to[i * stride] = a[i];
948 }
949 }
950}
951
952template <>
953EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void pscatter<float, Packet4f>(float* to, const Packet4f& from, Index stride) {
954 pscatter_common<Packet4f>(to, from, stride);
955}
956
957template <>
958EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void pscatter<int, Packet4i>(int* to, const Packet4i& from, Index stride) {
959 pscatter_common<Packet4i>(to, from, stride);
960}
961
962template <>
963EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void pscatter<short int, Packet8s>(short int* to, const Packet8s& from,
964 Index stride) {
965 pscatter_common<Packet8s>(to, from, stride);
966}
967
968template <>
969EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void pscatter<unsigned short int, Packet8us>(unsigned short int* to,
970 const Packet8us& from,
971 Index stride) {
972 pscatter_common<Packet8us>(to, from, stride);
973}
974
975template <>
976EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void pscatter<bfloat16, Packet8bf>(bfloat16* to, const Packet8bf& from,
977 Index stride) {
978 pscatter_common<Packet8bf>(to, from, stride);
979}
980
981template <>
982EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void pscatter<signed char, Packet16c>(signed char* to, const Packet16c& from,
983 Index stride) {
984 pscatter_common<Packet16c>(to, from, stride);
985}
986
987template <>
988EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void pscatter<unsigned char, Packet16uc>(unsigned char* to,
989 const Packet16uc& from, Index stride) {
990 pscatter_common<Packet16uc>(to, from, stride);
991}
992
993template <>
994EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void pscatter_partial<float, Packet4f>(float* to, const Packet4f& from,
995 Index stride, const Index n) {
996 pscatter_common<Packet4f>(to, from, stride, n);
997}
998
999template <>
1000EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void pscatter_partial<int, Packet4i>(int* to, const Packet4i& from, Index stride,
1001 const Index n) {
1002 pscatter_common<Packet4i>(to, from, stride, n);
1003}
1004
1005template <>
1006EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void pscatter_partial<short int, Packet8s>(short int* to, const Packet8s& from,
1007 Index stride, const Index n) {
1008 pscatter_common<Packet8s>(to, from, stride, n);
1009}
1010
1011template <>
1012EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void pscatter_partial<unsigned short int, Packet8us>(unsigned short int* to,
1013 const Packet8us& from,
1014 Index stride,
1015 const Index n) {
1016 pscatter_common<Packet8us>(to, from, stride, n);
1017}
1018
1019template <>
1020EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void pscatter_partial<bfloat16, Packet8bf>(bfloat16* to, const Packet8bf& from,
1021 Index stride, const Index n) {
1022 pscatter_common<Packet8bf>(to, from, stride, n);
1023}
1024
1025template <>
1026EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void pscatter_partial<signed char, Packet16c>(signed char* to,
1027 const Packet16c& from, Index stride,
1028 const Index n) {
1029 pscatter_common<Packet16c>(to, from, stride, n);
1030}
1031
1032template <>
1033EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void pscatter_partial<unsigned char, Packet16uc>(unsigned char* to,
1034 const Packet16uc& from,
1035 Index stride, const Index n) {
1036 pscatter_common<Packet16uc>(to, from, stride, n);
1037}
1038
1039template <>
1040EIGEN_STRONG_INLINE Packet4f plset<Packet4f>(const float& a) {
1041 return pset1<Packet4f>(a) + p4f_COUNTDOWN;
1042}
1043template <>
1044EIGEN_STRONG_INLINE Packet4i plset<Packet4i>(const int& a) {
1045 return pset1<Packet4i>(a) + p4i_COUNTDOWN;
1046}
1047template <>
1048EIGEN_STRONG_INLINE Packet8s plset<Packet8s>(const short int& a) {
1049 return pset1<Packet8s>(a) + p8s_COUNTDOWN;
1050}
1051template <>
1052EIGEN_STRONG_INLINE Packet8us plset<Packet8us>(const unsigned short int& a) {
1053 return pset1<Packet8us>(a) + p8us_COUNTDOWN;
1054}
1055template <>
1056EIGEN_STRONG_INLINE Packet16c plset<Packet16c>(const signed char& a) {
1057 return pset1<Packet16c>(a) + p16c_COUNTDOWN;
1058}
1059template <>
1060EIGEN_STRONG_INLINE Packet16uc plset<Packet16uc>(const unsigned char& a) {
1061 return pset1<Packet16uc>(a) + p16uc_COUNTDOWN;
1062}
1063
1064template <>
1065EIGEN_STRONG_INLINE Packet4f padd<Packet4f>(const Packet4f& a, const Packet4f& b) {
1066 return a + b;
1067}
1068template <>
1069EIGEN_STRONG_INLINE Packet4i padd<Packet4i>(const Packet4i& a, const Packet4i& b) {
1070 return a + b;
1071}
1072template <>
1073EIGEN_STRONG_INLINE Packet4ui padd<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
1074 return a + b;
1075}
1076template <>
1077EIGEN_STRONG_INLINE Packet8s padd<Packet8s>(const Packet8s& a, const Packet8s& b) {
1078 return a + b;
1079}
1080template <>
1081EIGEN_STRONG_INLINE Packet8us padd<Packet8us>(const Packet8us& a, const Packet8us& b) {
1082 return a + b;
1083}
1084template <>
1085EIGEN_STRONG_INLINE Packet16c padd<Packet16c>(const Packet16c& a, const Packet16c& b) {
1086 return a + b;
1087}
1088template <>
1089EIGEN_STRONG_INLINE Packet16uc padd<Packet16uc>(const Packet16uc& a, const Packet16uc& b) {
1090 return a + b;
1091}
1092
1093template <>
1094EIGEN_STRONG_INLINE Packet4f psub<Packet4f>(const Packet4f& a, const Packet4f& b) {
1095 return a - b;
1096}
1097template <>
1098EIGEN_STRONG_INLINE Packet4i psub<Packet4i>(const Packet4i& a, const Packet4i& b) {
1099 return a - b;
1100}
1101template <>
1102EIGEN_STRONG_INLINE Packet8s psub<Packet8s>(const Packet8s& a, const Packet8s& b) {
1103 return a - b;
1104}
1105template <>
1106EIGEN_STRONG_INLINE Packet8us psub<Packet8us>(const Packet8us& a, const Packet8us& b) {
1107 return a - b;
1108}
1109template <>
1110EIGEN_STRONG_INLINE Packet16c psub<Packet16c>(const Packet16c& a, const Packet16c& b) {
1111 return a - b;
1112}
1113template <>
1114EIGEN_STRONG_INLINE Packet16uc psub<Packet16uc>(const Packet16uc& a, const Packet16uc& b) {
1115 return a - b;
1116}
1117
1118template <>
1119EIGEN_STRONG_INLINE Packet4f pnegate(const Packet4f& a) {
1120#ifdef __POWER8_VECTOR__
1121 return vec_neg(a);
1122#else
1123 return vec_xor(a, p4f_MZERO);
1124#endif
1125}
1126template <>
1127EIGEN_STRONG_INLINE Packet16c pnegate(const Packet16c& a) {
1128#ifdef __POWER8_VECTOR__
1129 return vec_neg(a);
1130#else
1131 return reinterpret_cast<Packet16c>(p4i_ZERO) - a;
1132#endif
1133}
1134template <>
1135EIGEN_STRONG_INLINE Packet8s pnegate(const Packet8s& a) {
1136#ifdef __POWER8_VECTOR__
1137 return vec_neg(a);
1138#else
1139 return reinterpret_cast<Packet8s>(p4i_ZERO) - a;
1140#endif
1141}
1142template <>
1143EIGEN_STRONG_INLINE Packet4i pnegate(const Packet4i& a) {
1144#ifdef __POWER8_VECTOR__
1145 return vec_neg(a);
1146#else
1147 return p4i_ZERO - a;
1148#endif
1149}
1150
1151template <>
1152EIGEN_STRONG_INLINE Packet4f pconj(const Packet4f& a) {
1153 return a;
1154}
1155template <>
1156EIGEN_STRONG_INLINE Packet4i pconj(const Packet4i& a) {
1157 return a;
1158}
1159
1160template <>
1161EIGEN_STRONG_INLINE Packet4f pmul<Packet4f>(const Packet4f& a, const Packet4f& b) {
1162 return vec_madd(a, b, p4f_MZERO);
1163}
1164template <>
1165EIGEN_STRONG_INLINE Packet4i pmul<Packet4i>(const Packet4i& a, const Packet4i& b) {
1166 return a * b;
1167}
1168template <>
1169EIGEN_STRONG_INLINE Packet8s pmul<Packet8s>(const Packet8s& a, const Packet8s& b) {
1170 return vec_mul(a, b);
1171}
1172template <>
1173EIGEN_STRONG_INLINE Packet8us pmul<Packet8us>(const Packet8us& a, const Packet8us& b) {
1174 return vec_mul(a, b);
1175}
1176template <>
1177EIGEN_STRONG_INLINE Packet16c pmul<Packet16c>(const Packet16c& a, const Packet16c& b) {
1178 return vec_mul(a, b);
1179}
1180template <>
1181EIGEN_STRONG_INLINE Packet16uc pmul<Packet16uc>(const Packet16uc& a, const Packet16uc& b) {
1182 return vec_mul(a, b);
1183}
1184
1185template <>
1186EIGEN_STRONG_INLINE Packet4f pdiv<Packet4f>(const Packet4f& a, const Packet4f& b) {
1187#ifndef __VSX__ // VSX actually provides a div instruction
1188 Packet4f t, y_0, y_1;
1189
1190 // Altivec does not offer a divide instruction, we have to do a reciprocal approximation
1191 y_0 = vec_re(b);
1192
1193 // Do one Newton-Raphson iteration to get the needed accuracy
1194 t = vec_nmsub(y_0, b, p4f_ONE);
1195 y_1 = vec_madd(y_0, t, y_0);
1196
1197 return vec_madd(a, y_1, p4f_MZERO);
1198#else
1199 return vec_div(a, b);
1200#endif
1201}
1202
1203template <>
1204EIGEN_STRONG_INLINE Packet4i pdiv<Packet4i>(const Packet4i& a, const Packet4i& b) {
1205#if defined(_ARCH_PWR10) && (EIGEN_COMP_LLVM || EIGEN_GNUC_STRICT_AT_LEAST(11, 0, 0))
1206 return vec_div(a, b);
1207#else
1208 EIGEN_UNUSED_VARIABLE(a);
1209 EIGEN_UNUSED_VARIABLE(b);
1210 eigen_assert(false && "packet integer division are not supported by AltiVec");
1211 return pset1<Packet4i>(0);
1212#endif
1213}
1214
1215// for some weird raisons, it has to be overloaded for packet of integers
1216template <>
1217EIGEN_STRONG_INLINE Packet4f pmadd(const Packet4f& a, const Packet4f& b, const Packet4f& c) {
1218 return vec_madd(a, b, c);
1219}
1220template <>
1221EIGEN_STRONG_INLINE Packet4i pmadd(const Packet4i& a, const Packet4i& b, const Packet4i& c) {
1222 return a * b + c;
1223}
1224template <>
1225EIGEN_STRONG_INLINE Packet8s pmadd(const Packet8s& a, const Packet8s& b, const Packet8s& c) {
1226 return vec_madd(a, b, c);
1227}
1228template <>
1229EIGEN_STRONG_INLINE Packet8us pmadd(const Packet8us& a, const Packet8us& b, const Packet8us& c) {
1230 return vec_madd(a, b, c);
1231}
1232
1233#ifdef EIGEN_VECTORIZE_VSX
1234template <>
1235EIGEN_STRONG_INLINE Packet4f pmsub(const Packet4f& a, const Packet4f& b, const Packet4f& c) {
1236 return vec_msub(a, b, c);
1237}
1238template <>
1239EIGEN_STRONG_INLINE Packet4f pnmadd(const Packet4f& a, const Packet4f& b, const Packet4f& c) {
1240 return vec_nmsub(a, b, c);
1241}
1242template <>
1243EIGEN_STRONG_INLINE Packet4f pnmsub(const Packet4f& a, const Packet4f& b, const Packet4f& c) {
1244 return vec_nmadd(a, b, c);
1245}
1246#endif
1247
1248template <>
1249EIGEN_STRONG_INLINE Packet4f pmin<Packet4f>(const Packet4f& a, const Packet4f& b) {
1250#ifdef EIGEN_VECTORIZE_VSX
1251 // NOTE: about 10% slower than vec_min, but consistent with std::min and SSE regarding NaN
1252 Packet4f ret;
1253 __asm__("xvcmpgesp %x0,%x1,%x2\n\txxsel %x0,%x1,%x2,%x0" : "=&wa"(ret) : "wa"(a), "wa"(b));
1254 return ret;
1255#else
1256 return vec_min(a, b);
1257#endif
1258}
1259template <>
1260EIGEN_STRONG_INLINE Packet4i pmin<Packet4i>(const Packet4i& a, const Packet4i& b) {
1261 return vec_min(a, b);
1262}
1263template <>
1264EIGEN_STRONG_INLINE Packet8s pmin<Packet8s>(const Packet8s& a, const Packet8s& b) {
1265 return vec_min(a, b);
1266}
1267template <>
1268EIGEN_STRONG_INLINE Packet8us pmin<Packet8us>(const Packet8us& a, const Packet8us& b) {
1269 return vec_min(a, b);
1270}
1271template <>
1272EIGEN_STRONG_INLINE Packet16c pmin<Packet16c>(const Packet16c& a, const Packet16c& b) {
1273 return vec_min(a, b);
1274}
1275template <>
1276EIGEN_STRONG_INLINE Packet16uc pmin<Packet16uc>(const Packet16uc& a, const Packet16uc& b) {
1277 return vec_min(a, b);
1278}
1279
1280template <>
1281EIGEN_STRONG_INLINE Packet4f pmax<Packet4f>(const Packet4f& a, const Packet4f& b) {
1282#ifdef EIGEN_VECTORIZE_VSX
1283 // NOTE: about 10% slower than vec_max, but consistent with std::max and SSE regarding NaN
1284 Packet4f ret;
1285 __asm__("xvcmpgtsp %x0,%x2,%x1\n\txxsel %x0,%x1,%x2,%x0" : "=&wa"(ret) : "wa"(a), "wa"(b));
1286 return ret;
1287#else
1288 return vec_max(a, b);
1289#endif
1290}
1291template <>
1292EIGEN_STRONG_INLINE Packet4i pmax<Packet4i>(const Packet4i& a, const Packet4i& b) {
1293 return vec_max(a, b);
1294}
1295template <>
1296EIGEN_STRONG_INLINE Packet8s pmax<Packet8s>(const Packet8s& a, const Packet8s& b) {
1297 return vec_max(a, b);
1298}
1299template <>
1300EIGEN_STRONG_INLINE Packet8us pmax<Packet8us>(const Packet8us& a, const Packet8us& b) {
1301 return vec_max(a, b);
1302}
1303template <>
1304EIGEN_STRONG_INLINE Packet16c pmax<Packet16c>(const Packet16c& a, const Packet16c& b) {
1305 return vec_max(a, b);
1306}
1307template <>
1308EIGEN_STRONG_INLINE Packet16uc pmax<Packet16uc>(const Packet16uc& a, const Packet16uc& b) {
1309 return vec_max(a, b);
1310}
1311
1312template <>
1313EIGEN_STRONG_INLINE Packet4f pcmp_le(const Packet4f& a, const Packet4f& b) {
1314 return reinterpret_cast<Packet4f>(vec_cmple(a, b));
1315}
1316// To fix bug with vec_cmplt on older versions
1317#ifdef EIGEN_VECTORIZE_VSX
1318template <>
1319EIGEN_STRONG_INLINE Packet4f pcmp_lt(const Packet4f& a, const Packet4f& b) {
1320 return reinterpret_cast<Packet4f>(vec_cmplt(a, b));
1321}
1322#endif
1323template <>
1324EIGEN_STRONG_INLINE Packet4f pcmp_eq(const Packet4f& a, const Packet4f& b) {
1325 return reinterpret_cast<Packet4f>(vec_cmpeq(a, b));
1326}
1327template <>
1328EIGEN_STRONG_INLINE Packet4f pcmp_lt_or_nan(const Packet4f& a, const Packet4f& b) {
1329 Packet4f c = reinterpret_cast<Packet4f>(vec_cmpge(a, b));
1330 return vec_nor(c, c);
1331}
1332
1333#ifdef EIGEN_VECTORIZE_VSX
1334template <>
1335EIGEN_STRONG_INLINE Packet4i pcmp_le(const Packet4i& a, const Packet4i& b) {
1336 return reinterpret_cast<Packet4i>(vec_cmple(a, b));
1337}
1338#endif
1339template <>
1340EIGEN_STRONG_INLINE Packet4i pcmp_lt(const Packet4i& a, const Packet4i& b) {
1341 return reinterpret_cast<Packet4i>(vec_cmplt(a, b));
1342}
1343template <>
1344EIGEN_STRONG_INLINE Packet4i pcmp_eq(const Packet4i& a, const Packet4i& b) {
1345 return reinterpret_cast<Packet4i>(vec_cmpeq(a, b));
1346}
1347#ifdef EIGEN_VECTORIZE_VSX
1348template <>
1349EIGEN_STRONG_INLINE Packet8s pcmp_le(const Packet8s& a, const Packet8s& b) {
1350 return reinterpret_cast<Packet8s>(vec_cmple(a, b));
1351}
1352#endif
1353template <>
1354EIGEN_STRONG_INLINE Packet8s pcmp_lt(const Packet8s& a, const Packet8s& b) {
1355 return reinterpret_cast<Packet8s>(vec_cmplt(a, b));
1356}
1357template <>
1358EIGEN_STRONG_INLINE Packet8s pcmp_eq(const Packet8s& a, const Packet8s& b) {
1359 return reinterpret_cast<Packet8s>(vec_cmpeq(a, b));
1360}
1361#ifdef EIGEN_VECTORIZE_VSX
1362template <>
1363EIGEN_STRONG_INLINE Packet8us pcmp_le(const Packet8us& a, const Packet8us& b) {
1364 return reinterpret_cast<Packet8us>(vec_cmple(a, b));
1365}
1366#endif
1367template <>
1368EIGEN_STRONG_INLINE Packet8us pcmp_lt(const Packet8us& a, const Packet8us& b) {
1369 return reinterpret_cast<Packet8us>(vec_cmplt(a, b));
1370}
1371template <>
1372EIGEN_STRONG_INLINE Packet8us pcmp_eq(const Packet8us& a, const Packet8us& b) {
1373 return reinterpret_cast<Packet8us>(vec_cmpeq(a, b));
1374}
1375#ifdef EIGEN_VECTORIZE_VSX
1376template <>
1377EIGEN_STRONG_INLINE Packet16c pcmp_le(const Packet16c& a, const Packet16c& b) {
1378 return reinterpret_cast<Packet16c>(vec_cmple(a, b));
1379}
1380#endif
1381template <>
1382EIGEN_STRONG_INLINE Packet16c pcmp_lt(const Packet16c& a, const Packet16c& b) {
1383 return reinterpret_cast<Packet16c>(vec_cmplt(a, b));
1384}
1385template <>
1386EIGEN_STRONG_INLINE Packet16c pcmp_eq(const Packet16c& a, const Packet16c& b) {
1387 return reinterpret_cast<Packet16c>(vec_cmpeq(a, b));
1388}
1389#ifdef EIGEN_VECTORIZE_VSX
1390template <>
1391EIGEN_STRONG_INLINE Packet16uc pcmp_le(const Packet16uc& a, const Packet16uc& b) {
1392 return reinterpret_cast<Packet16uc>(vec_cmple(a, b));
1393}
1394#endif
1395template <>
1396EIGEN_STRONG_INLINE Packet16uc pcmp_lt(const Packet16uc& a, const Packet16uc& b) {
1397 return reinterpret_cast<Packet16uc>(vec_cmplt(a, b));
1398}
1399template <>
1400EIGEN_STRONG_INLINE Packet16uc pcmp_eq(const Packet16uc& a, const Packet16uc& b) {
1401 return reinterpret_cast<Packet16uc>(vec_cmpeq(a, b));
1402}
1403
1404template <>
1405EIGEN_STRONG_INLINE Packet4f pand<Packet4f>(const Packet4f& a, const Packet4f& b) {
1406 return vec_and(a, b);
1407}
1408template <>
1409EIGEN_STRONG_INLINE Packet4i pand<Packet4i>(const Packet4i& a, const Packet4i& b) {
1410 return vec_and(a, b);
1411}
1412template <>
1413EIGEN_STRONG_INLINE Packet4ui pand<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
1414 return vec_and(a, b);
1415}
1416template <>
1417EIGEN_STRONG_INLINE Packet8us pand<Packet8us>(const Packet8us& a, const Packet8us& b) {
1418 return vec_and(a, b);
1419}
1420template <>
1421EIGEN_STRONG_INLINE Packet8bf pand<Packet8bf>(const Packet8bf& a, const Packet8bf& b) {
1422 return pand<Packet8us>(a, b);
1423}
1424
1425template <>
1426EIGEN_STRONG_INLINE Packet4f por<Packet4f>(const Packet4f& a, const Packet4f& b) {
1427 return vec_or(a, b);
1428}
1429template <>
1430EIGEN_STRONG_INLINE Packet4i por<Packet4i>(const Packet4i& a, const Packet4i& b) {
1431 return vec_or(a, b);
1432}
1433template <>
1434EIGEN_STRONG_INLINE Packet8s por<Packet8s>(const Packet8s& a, const Packet8s& b) {
1435 return vec_or(a, b);
1436}
1437template <>
1438EIGEN_STRONG_INLINE Packet8us por<Packet8us>(const Packet8us& a, const Packet8us& b) {
1439 return vec_or(a, b);
1440}
1441template <>
1442EIGEN_STRONG_INLINE Packet8bf por<Packet8bf>(const Packet8bf& a, const Packet8bf& b) {
1443 return por<Packet8us>(a, b);
1444}
1445
1446template <>
1447EIGEN_STRONG_INLINE Packet4f pxor<Packet4f>(const Packet4f& a, const Packet4f& b) {
1448 return vec_xor(a, b);
1449}
1450template <>
1451EIGEN_STRONG_INLINE Packet4i pxor<Packet4i>(const Packet4i& a, const Packet4i& b) {
1452 return vec_xor(a, b);
1453}
1454template <>
1455EIGEN_STRONG_INLINE Packet8us pxor<Packet8us>(const Packet8us& a, const Packet8us& b) {
1456 return vec_xor(a, b);
1457}
1458template <>
1459EIGEN_STRONG_INLINE Packet8bf pxor<Packet8bf>(const Packet8bf& a, const Packet8bf& b) {
1460 return pxor<Packet8us>(a, b);
1461}
1462
1463template <>
1464EIGEN_STRONG_INLINE Packet4f pandnot<Packet4f>(const Packet4f& a, const Packet4f& b) {
1465 return vec_andc(a, b);
1466}
1467template <>
1468EIGEN_STRONG_INLINE Packet4i pandnot<Packet4i>(const Packet4i& a, const Packet4i& b) {
1469 return vec_andc(a, b);
1470}
1471
1472template <>
1473EIGEN_STRONG_INLINE Packet4f pselect(const Packet4f& mask, const Packet4f& a, const Packet4f& b) {
1474 return vec_sel(b, a, reinterpret_cast<Packet4ui>(mask));
1475}
1476
1477template <>
1478EIGEN_STRONG_INLINE Packet4f pround<Packet4f>(const Packet4f& a) {
1479 Packet4f t = vec_add(
1480 reinterpret_cast<Packet4f>(vec_or(vec_and(reinterpret_cast<Packet4ui>(a), p4ui_SIGN), p4ui_PREV0DOT5)), a);
1481 Packet4f res;
1482
1483#ifdef EIGEN_VECTORIZE_VSX
1484 __asm__("xvrspiz %x0, %x1\n\t" : "=&wa"(res) : "wa"(t));
1485#else
1486 __asm__("vrfiz %0, %1\n\t" : "=v"(res) : "v"(t));
1487#endif
1488
1489 return res;
1490}
1491template <>
1492EIGEN_STRONG_INLINE Packet4f pceil<Packet4f>(const Packet4f& a) {
1493 return vec_ceil(a);
1494}
1495template <>
1496EIGEN_STRONG_INLINE Packet4f pfloor<Packet4f>(const Packet4f& a) {
1497 return vec_floor(a);
1498}
1499template <>
1500EIGEN_STRONG_INLINE Packet4f ptrunc<Packet4f>(const Packet4f& a) {
1501 return vec_trunc(a);
1502}
1503#ifdef EIGEN_VECTORIZE_VSX
1504template <>
1505EIGEN_STRONG_INLINE Packet4f print<Packet4f>(const Packet4f& a) {
1506 Packet4f res;
1507
1508 __asm__("xvrspic %x0, %x1\n\t" : "=&wa"(res) : "wa"(a));
1509
1510 return res;
1511}
1512#endif
1513
1514template <typename Packet>
1515EIGEN_STRONG_INLINE Packet ploadu_common(const __UNPACK_TYPE__(Packet) * from) {
1516 EIGEN_DEBUG_ALIGNED_LOAD
1517#if defined(EIGEN_VECTORIZE_VSX) || !defined(_BIG_ENDIAN)
1518 EIGEN_DEBUG_UNALIGNED_LOAD
1519 return vec_xl(0, const_cast<__UNPACK_TYPE__(Packet)*>(from));
1520#else
1521 Packet16uc MSQ, LSQ;
1522 Packet16uc mask;
1523 MSQ = vec_ld(0, (unsigned char*)from); // most significant quadword
1524 LSQ = vec_ld(15, (unsigned char*)from); // least significant quadword
1525 mask = vec_lvsl(0, from); // create the permute mask
1526 // TODO: Add static_cast here
1527 return (Packet)vec_perm(MSQ, LSQ, mask); // align the data
1528#endif
1529}
1530
1531template <>
1532EIGEN_STRONG_INLINE Packet4f ploadu<Packet4f>(const float* from) {
1533 return ploadu_common<Packet4f>(from);
1534}
1535template <>
1536EIGEN_STRONG_INLINE Packet4i ploadu<Packet4i>(const int* from) {
1537 return ploadu_common<Packet4i>(from);
1538}
1539template <>
1540EIGEN_STRONG_INLINE Packet8s ploadu<Packet8s>(const short int* from) {
1541 return ploadu_common<Packet8s>(from);
1542}
1543template <>
1544EIGEN_STRONG_INLINE Packet8us ploadu<Packet8us>(const unsigned short int* from) {
1545 return ploadu_common<Packet8us>(from);
1546}
1547template <>
1548EIGEN_STRONG_INLINE Packet8bf ploadu<Packet8bf>(const bfloat16* from) {
1549 return ploadu_common<Packet8us>(reinterpret_cast<const unsigned short int*>(from));
1550}
1551template <>
1552EIGEN_STRONG_INLINE Packet16c ploadu<Packet16c>(const signed char* from) {
1553 return ploadu_common<Packet16c>(from);
1554}
1555template <>
1556EIGEN_STRONG_INLINE Packet16uc ploadu<Packet16uc>(const unsigned char* from) {
1557 return ploadu_common<Packet16uc>(from);
1558}
1559
1560template <typename Packet>
1561EIGEN_ALWAYS_INLINE Packet ploadu_partial_common(const __UNPACK_TYPE__(Packet) * from, const Index n,
1562 const Index offset) {
1563 const Index packet_size = unpacket_traits<Packet>::size;
1564 eigen_internal_assert(n + offset <= packet_size && "number of elements plus offset will read past end of packet");
1565 const Index size = sizeof(__UNPACK_TYPE__(Packet));
1566#ifdef _ARCH_PWR9
1567 EIGEN_UNUSED_VARIABLE(packet_size);
1568 EIGEN_DEBUG_ALIGNED_LOAD
1569 EIGEN_DEBUG_UNALIGNED_LOAD
1570 Packet load = vec_xl_len(const_cast<__UNPACK_TYPE__(Packet)*>(from), n * size);
1571 if (offset) {
1572 Packet16uc shift = pset1<Packet16uc>(offset * 8 * size);
1573#ifdef _BIG_ENDIAN
1574 load = Packet(vec_sro(Packet16uc(load), shift));
1575#else
1576 load = Packet(vec_slo(Packet16uc(load), shift));
1577#endif
1578 }
1579 return load;
1580#else
1581 if (n) {
1582 EIGEN_ALIGN16 __UNPACK_TYPE__(Packet) load[packet_size];
1583 unsigned char* load2 = reinterpret_cast<unsigned char*>(load + offset);
1584 unsigned char* from2 = reinterpret_cast<unsigned char*>(const_cast<__UNPACK_TYPE__(Packet)*>(from));
1585 Index n2 = n * size;
1586 if (16 <= n2) {
1587 pstoreu(load2, ploadu<Packet16uc>(from2));
1588 } else {
1589 memcpy((void*)load2, (void*)from2, n2);
1590 }
1591 return pload_ignore<Packet>(load);
1592 } else {
1593 return Packet(pset1<Packet16uc>(0));
1594 }
1595#endif
1596}
1597
1598template <>
1599EIGEN_ALWAYS_INLINE Packet4f ploadu_partial<Packet4f>(const float* from, const Index n, const Index offset) {
1600 return ploadu_partial_common<Packet4f>(from, n, offset);
1601}
1602template <>
1603EIGEN_ALWAYS_INLINE Packet4i ploadu_partial<Packet4i>(const int* from, const Index n, const Index offset) {
1604 return ploadu_partial_common<Packet4i>(from, n, offset);
1605}
1606template <>
1607EIGEN_ALWAYS_INLINE Packet8s ploadu_partial<Packet8s>(const short int* from, const Index n, const Index offset) {
1608 return ploadu_partial_common<Packet8s>(from, n, offset);
1609}
1610template <>
1611EIGEN_ALWAYS_INLINE Packet8us ploadu_partial<Packet8us>(const unsigned short int* from, const Index n,
1612 const Index offset) {
1613 return ploadu_partial_common<Packet8us>(from, n, offset);
1614}
1615template <>
1616EIGEN_ALWAYS_INLINE Packet8bf ploadu_partial<Packet8bf>(const bfloat16* from, const Index n, const Index offset) {
1617 return ploadu_partial_common<Packet8us>(reinterpret_cast<const unsigned short int*>(from), n, offset);
1618}
1619template <>
1620EIGEN_ALWAYS_INLINE Packet16c ploadu_partial<Packet16c>(const signed char* from, const Index n, const Index offset) {
1621 return ploadu_partial_common<Packet16c>(from, n, offset);
1622}
1623template <>
1624EIGEN_ALWAYS_INLINE Packet16uc ploadu_partial<Packet16uc>(const unsigned char* from, const Index n,
1625 const Index offset) {
1626 return ploadu_partial_common<Packet16uc>(from, n, offset);
1627}
1628
1629template <typename Packet>
1630EIGEN_STRONG_INLINE Packet ploaddup_common(const __UNPACK_TYPE__(Packet) * from) {
1631 Packet p;
1632 if ((std::ptrdiff_t(from) % 16) == 0)
1633 p = pload<Packet>(from);
1634 else
1635 p = ploadu<Packet>(from);
1636 return vec_mergeh(p, p);
1637}
1638template <>
1639EIGEN_STRONG_INLINE Packet4f ploaddup<Packet4f>(const float* from) {
1640 return ploaddup_common<Packet4f>(from);
1641}
1642template <>
1643EIGEN_STRONG_INLINE Packet4i ploaddup<Packet4i>(const int* from) {
1644 return ploaddup_common<Packet4i>(from);
1645}
1646
1647template <>
1648EIGEN_STRONG_INLINE Packet8s ploaddup<Packet8s>(const short int* from) {
1649 Packet8s p;
1650 if ((std::ptrdiff_t(from) % 16) == 0)
1651 p = pload<Packet8s>(from);
1652 else
1653 p = ploadu<Packet8s>(from);
1654 return vec_mergeh(p, p);
1655}
1656
1657template <>
1658EIGEN_STRONG_INLINE Packet8us ploaddup<Packet8us>(const unsigned short int* from) {
1659 Packet8us p;
1660 if ((std::ptrdiff_t(from) % 16) == 0)
1661 p = pload<Packet8us>(from);
1662 else
1663 p = ploadu<Packet8us>(from);
1664 return vec_mergeh(p, p);
1665}
1666
1667template <>
1668EIGEN_STRONG_INLINE Packet8s ploadquad<Packet8s>(const short int* from) {
1669 Packet8s p;
1670 if ((std::ptrdiff_t(from) % 16) == 0)
1671 p = pload<Packet8s>(from);
1672 else
1673 p = ploadu<Packet8s>(from);
1674 return vec_perm(p, p, p16uc_QUADRUPLICATE16_HI);
1675}
1676
1677template <>
1678EIGEN_STRONG_INLINE Packet8us ploadquad<Packet8us>(const unsigned short int* from) {
1679 Packet8us p;
1680 if ((std::ptrdiff_t(from) % 16) == 0)
1681 p = pload<Packet8us>(from);
1682 else
1683 p = ploadu<Packet8us>(from);
1684 return vec_perm(p, p, p16uc_QUADRUPLICATE16_HI);
1685}
1686
1687template <>
1688EIGEN_STRONG_INLINE Packet8bf ploadquad<Packet8bf>(const bfloat16* from) {
1689 return ploadquad<Packet8us>(reinterpret_cast<const unsigned short int*>(from));
1690}
1691
1692template <>
1693EIGEN_STRONG_INLINE Packet16c ploaddup<Packet16c>(const signed char* from) {
1694 Packet16c p;
1695 if ((std::ptrdiff_t(from) % 16) == 0)
1696 p = pload<Packet16c>(from);
1697 else
1698 p = ploadu<Packet16c>(from);
1699 return vec_mergeh(p, p);
1700}
1701
1702template <>
1703EIGEN_STRONG_INLINE Packet16uc ploaddup<Packet16uc>(const unsigned char* from) {
1704 Packet16uc p;
1705 if ((std::ptrdiff_t(from) % 16) == 0)
1706 p = pload<Packet16uc>(from);
1707 else
1708 p = ploadu<Packet16uc>(from);
1709 return vec_mergeh(p, p);
1710}
1711
1712template <>
1713EIGEN_STRONG_INLINE Packet16c ploadquad<Packet16c>(const signed char* from) {
1714 Packet16c p;
1715 if ((std::ptrdiff_t(from) % 16) == 0)
1716 p = pload<Packet16c>(from);
1717 else
1718 p = ploadu<Packet16c>(from);
1719 return vec_perm(p, p, p16uc_QUADRUPLICATE16);
1720}
1721
1722template <>
1723EIGEN_STRONG_INLINE Packet16uc ploadquad<Packet16uc>(const unsigned char* from) {
1724 Packet16uc p;
1725 if ((std::ptrdiff_t(from) % 16) == 0)
1726 p = pload<Packet16uc>(from);
1727 else
1728 p = ploadu<Packet16uc>(from);
1729 return vec_perm(p, p, p16uc_QUADRUPLICATE16);
1730}
1731
1732template <typename Packet>
1733EIGEN_STRONG_INLINE void pstoreu_common(__UNPACK_TYPE__(Packet) * to, const Packet& from) {
1734 EIGEN_DEBUG_UNALIGNED_STORE
1735#if defined(EIGEN_VECTORIZE_VSX) || !defined(_BIG_ENDIAN)
1736 vec_xst(from, 0, to);
1737#else
1738 // Taken from http://developer.apple.com/hardwaredrivers/ve/alignment.html
1739 // Warning: not thread safe!
1740 Packet16uc MSQ, LSQ, edges;
1741 Packet16uc edgeAlign, align;
1742
1743 MSQ = vec_ld(0, (unsigned char*)to); // most significant quadword
1744 LSQ = vec_ld(15, (unsigned char*)to); // least significant quadword
1745 edgeAlign = vec_lvsl(0, to); // permute map to extract edges
1746 edges = vec_perm(LSQ, MSQ, edgeAlign); // extract the edges
1747 align = vec_lvsr(0, to); // permute map to misalign data
1748 MSQ = vec_perm(edges, (Packet16uc)from, align); // misalign the data (MSQ)
1749 LSQ = vec_perm((Packet16uc)from, edges, align); // misalign the data (LSQ)
1750 vec_st(LSQ, 15, (unsigned char*)to); // Store the LSQ part first
1751 vec_st(MSQ, 0, (unsigned char*)to); // Store the MSQ part second
1752#endif
1753}
1754template <>
1755EIGEN_STRONG_INLINE void pstoreu<float>(float* to, const Packet4f& from) {
1756 pstoreu_common<Packet4f>(to, from);
1757}
1758template <>
1759EIGEN_STRONG_INLINE void pstoreu<int>(int* to, const Packet4i& from) {
1760 pstoreu_common<Packet4i>(to, from);
1761}
1762template <>
1763EIGEN_STRONG_INLINE void pstoreu<short int>(short int* to, const Packet8s& from) {
1764 pstoreu_common<Packet8s>(to, from);
1765}
1766template <>
1767EIGEN_STRONG_INLINE void pstoreu<unsigned short int>(unsigned short int* to, const Packet8us& from) {
1768 pstoreu_common<Packet8us>(to, from);
1769}
1770template <>
1771EIGEN_STRONG_INLINE void pstoreu<bfloat16>(bfloat16* to, const Packet8bf& from) {
1772 pstoreu_common<Packet8us>(reinterpret_cast<unsigned short int*>(to), from.m_val);
1773}
1774template <>
1775EIGEN_STRONG_INLINE void pstoreu<signed char>(signed char* to, const Packet16c& from) {
1776 pstoreu_common<Packet16c>(to, from);
1777}
1778template <>
1779EIGEN_STRONG_INLINE void pstoreu<unsigned char>(unsigned char* to, const Packet16uc& from) {
1780 pstoreu_common<Packet16uc>(to, from);
1781}
1782
1783template <typename Packet>
1784EIGEN_ALWAYS_INLINE void pstoreu_partial_common(__UNPACK_TYPE__(Packet) * to, const Packet& from, const Index n,
1785 const Index offset) {
1786 const Index packet_size = unpacket_traits<Packet>::size;
1787 eigen_internal_assert(n + offset <= packet_size && "number of elements plus offset will write past end of packet");
1788 const Index size = sizeof(__UNPACK_TYPE__(Packet));
1789#ifdef _ARCH_PWR9
1790 EIGEN_UNUSED_VARIABLE(packet_size);
1791 EIGEN_DEBUG_UNALIGNED_STORE
1792 Packet store = from;
1793 if (offset) {
1794 Packet16uc shift = pset1<Packet16uc>(offset * 8 * size);
1795#ifdef _BIG_ENDIAN
1796 store = Packet(vec_slo(Packet16uc(store), shift));
1797#else
1798 store = Packet(vec_sro(Packet16uc(store), shift));
1799#endif
1800 }
1801 vec_xst_len(store, to, n * size);
1802#else
1803 if (n) {
1804 EIGEN_ALIGN16 __UNPACK_TYPE__(Packet) store[packet_size];
1805 pstore(store, from);
1806 unsigned char* store2 = reinterpret_cast<unsigned char*>(store + offset);
1807 unsigned char* to2 = reinterpret_cast<unsigned char*>(to);
1808 Index n2 = n * size;
1809 if (16 <= n2) {
1810 pstoreu(to2, ploadu<Packet16uc>(store2));
1811 } else {
1812 memcpy((void*)to2, (void*)store2, n2);
1813 }
1814 }
1815#endif
1816}
1817
1818template <>
1819EIGEN_ALWAYS_INLINE void pstoreu_partial<float>(float* to, const Packet4f& from, const Index n, const Index offset) {
1820 pstoreu_partial_common<Packet4f>(to, from, n, offset);
1821}
1822template <>
1823EIGEN_ALWAYS_INLINE void pstoreu_partial<int>(int* to, const Packet4i& from, const Index n, const Index offset) {
1824 pstoreu_partial_common<Packet4i>(to, from, n, offset);
1825}
1826template <>
1827EIGEN_ALWAYS_INLINE void pstoreu_partial<short int>(short int* to, const Packet8s& from, const Index n,
1828 const Index offset) {
1829 pstoreu_partial_common<Packet8s>(to, from, n, offset);
1830}
1831template <>
1832EIGEN_ALWAYS_INLINE void pstoreu_partial<unsigned short int>(unsigned short int* to, const Packet8us& from,
1833 const Index n, const Index offset) {
1834 pstoreu_partial_common<Packet8us>(to, from, n, offset);
1835}
1836template <>
1837EIGEN_ALWAYS_INLINE void pstoreu_partial<bfloat16>(bfloat16* to, const Packet8bf& from, const Index n,
1838 const Index offset) {
1839 pstoreu_partial_common<Packet8us>(reinterpret_cast<unsigned short int*>(to), from, n, offset);
1840}
1841template <>
1842EIGEN_ALWAYS_INLINE void pstoreu_partial<signed char>(signed char* to, const Packet16c& from, const Index n,
1843 const Index offset) {
1844 pstoreu_partial_common<Packet16c>(to, from, n, offset);
1845}
1846template <>
1847EIGEN_ALWAYS_INLINE void pstoreu_partial<unsigned char>(unsigned char* to, const Packet16uc& from, const Index n,
1848 const Index offset) {
1849 pstoreu_partial_common<Packet16uc>(to, from, n, offset);
1850}
1851
1852template <>
1853EIGEN_STRONG_INLINE void prefetch<float>(const float* addr) {
1854 EIGEN_PPC_PREFETCH(addr);
1855}
1856template <>
1857EIGEN_STRONG_INLINE void prefetch<int>(const int* addr) {
1858 EIGEN_PPC_PREFETCH(addr);
1859}
1860
1861template <>
1862EIGEN_STRONG_INLINE float pfirst<Packet4f>(const Packet4f& a) {
1863 EIGEN_ALIGN16 float x;
1864 vec_ste(a, 0, &x);
1865 return x;
1866}
1867template <>
1868EIGEN_STRONG_INLINE int pfirst<Packet4i>(const Packet4i& a) {
1869 EIGEN_ALIGN16 int x;
1870 vec_ste(a, 0, &x);
1871 return x;
1872}
1873
1874template <typename Packet>
1875EIGEN_STRONG_INLINE __UNPACK_TYPE__(Packet) pfirst_common(const Packet& a) {
1876 EIGEN_ALIGN16 __UNPACK_TYPE__(Packet) x;
1877 vec_ste(a, 0, &x);
1878 return x;
1879}
1880
1881template <>
1882EIGEN_STRONG_INLINE short int pfirst<Packet8s>(const Packet8s& a) {
1883 return pfirst_common<Packet8s>(a);
1884}
1885
1886template <>
1887EIGEN_STRONG_INLINE unsigned short int pfirst<Packet8us>(const Packet8us& a) {
1888 return pfirst_common<Packet8us>(a);
1889}
1890
1891template <>
1892EIGEN_STRONG_INLINE signed char pfirst<Packet16c>(const Packet16c& a) {
1893 return pfirst_common<Packet16c>(a);
1894}
1895
1896template <>
1897EIGEN_STRONG_INLINE unsigned char pfirst<Packet16uc>(const Packet16uc& a) {
1898 return pfirst_common<Packet16uc>(a);
1899}
1900
1901template <>
1902EIGEN_STRONG_INLINE Packet4f preverse(const Packet4f& a) {
1903 return reinterpret_cast<Packet4f>(
1904 vec_perm(reinterpret_cast<Packet16uc>(a), reinterpret_cast<Packet16uc>(a), p16uc_REVERSE32));
1905}
1906template <>
1907EIGEN_STRONG_INLINE Packet4i preverse(const Packet4i& a) {
1908 return reinterpret_cast<Packet4i>(
1909 vec_perm(reinterpret_cast<Packet16uc>(a), reinterpret_cast<Packet16uc>(a), p16uc_REVERSE32));
1910}
1911template <>
1912EIGEN_STRONG_INLINE Packet8s preverse(const Packet8s& a) {
1913 return reinterpret_cast<Packet8s>(
1914 vec_perm(reinterpret_cast<Packet16uc>(a), reinterpret_cast<Packet16uc>(a), p16uc_REVERSE16));
1915}
1916template <>
1917EIGEN_STRONG_INLINE Packet8us preverse(const Packet8us& a) {
1918 return reinterpret_cast<Packet8us>(
1919 vec_perm(reinterpret_cast<Packet16uc>(a), reinterpret_cast<Packet16uc>(a), p16uc_REVERSE16));
1920}
1921template <>
1922EIGEN_STRONG_INLINE Packet16c preverse(const Packet16c& a) {
1923 return vec_perm(a, a, p16uc_REVERSE8);
1924}
1925template <>
1926EIGEN_STRONG_INLINE Packet16uc preverse(const Packet16uc& a) {
1927 return vec_perm(a, a, p16uc_REVERSE8);
1928}
1929template <>
1930EIGEN_STRONG_INLINE Packet8bf preverse(const Packet8bf& a) {
1931 return preverse<Packet8us>(a);
1932}
1933
1934template <>
1935EIGEN_STRONG_INLINE Packet4f pabs(const Packet4f& a) {
1936 return vec_abs(a);
1937}
1938template <>
1939EIGEN_STRONG_INLINE Packet4i pabs(const Packet4i& a) {
1940 return vec_abs(a);
1941}
1942template <>
1943EIGEN_STRONG_INLINE Packet8s pabs(const Packet8s& a) {
1944 return vec_abs(a);
1945}
1946template <>
1947EIGEN_STRONG_INLINE Packet8us pabs(const Packet8us& a) {
1948 return a;
1949}
1950template <>
1951EIGEN_STRONG_INLINE Packet16c pabs(const Packet16c& a) {
1952 return vec_abs(a);
1953}
1954template <>
1955EIGEN_STRONG_INLINE Packet16uc pabs(const Packet16uc& a) {
1956 return a;
1957}
1958template <>
1959EIGEN_STRONG_INLINE Packet8bf pabs(const Packet8bf& a) {
1960 EIGEN_DECLARE_CONST_FAST_Packet8us(abs_mask, 0x7FFF);
1961 return pand<Packet8us>(p8us_abs_mask, a);
1962}
1963
1964template <>
1965EIGEN_STRONG_INLINE Packet8bf psignbit(const Packet8bf& a) {
1966 return vec_sra(a.m_val, vec_splat_u16(15));
1967}
1968template <>
1969EIGEN_STRONG_INLINE Packet4f psignbit(const Packet4f& a) {
1970 return (Packet4f)vec_sra((Packet4i)a, vec_splats((unsigned int)(31)));
1971}
1972
1973template <int N>
1974EIGEN_STRONG_INLINE Packet4i parithmetic_shift_right(const Packet4i& a) {
1975 return vec_sra(a, reinterpret_cast<Packet4ui>(pset1<Packet4i>(N)));
1976}
1977template <int N>
1978EIGEN_STRONG_INLINE Packet4i plogical_shift_right(const Packet4i& a) {
1979 return vec_sr(a, reinterpret_cast<Packet4ui>(pset1<Packet4i>(N)));
1980}
1981template <int N>
1982EIGEN_STRONG_INLINE Packet4i plogical_shift_left(const Packet4i& a) {
1983 return vec_sl(a, reinterpret_cast<Packet4ui>(pset1<Packet4i>(N)));
1984}
1985template <int N>
1986EIGEN_STRONG_INLINE Packet4f plogical_shift_left(const Packet4f& a) {
1987 const EIGEN_DECLARE_CONST_FAST_Packet4ui(mask, N);
1988 Packet4ui r = vec_sl(reinterpret_cast<Packet4ui>(a), p4ui_mask);
1989 return reinterpret_cast<Packet4f>(r);
1990}
1991
1992template <int N>
1993EIGEN_STRONG_INLINE Packet4f plogical_shift_right(const Packet4f& a) {
1994 const EIGEN_DECLARE_CONST_FAST_Packet4ui(mask, N);
1995 Packet4ui r = vec_sr(reinterpret_cast<Packet4ui>(a), p4ui_mask);
1996 return reinterpret_cast<Packet4f>(r);
1997}
1998
1999template <int N>
2000EIGEN_STRONG_INLINE Packet4ui plogical_shift_right(const Packet4ui& a) {
2001 const EIGEN_DECLARE_CONST_FAST_Packet4ui(mask, N);
2002 return vec_sr(a, p4ui_mask);
2003}
2004
2005template <int N>
2006EIGEN_STRONG_INLINE Packet4ui plogical_shift_left(const Packet4ui& a) {
2007 const EIGEN_DECLARE_CONST_FAST_Packet4ui(mask, N);
2008 return vec_sl(a, p4ui_mask);
2009}
2010
2011template <int N>
2012EIGEN_STRONG_INLINE Packet8us plogical_shift_left(const Packet8us& a) {
2013 const EIGEN_DECLARE_CONST_FAST_Packet8us(mask, N);
2014 return vec_sl(a, p8us_mask);
2015}
2016template <int N>
2017EIGEN_STRONG_INLINE Packet8us plogical_shift_right(const Packet8us& a) {
2018 const EIGEN_DECLARE_CONST_FAST_Packet8us(mask, N);
2019 return vec_sr(a, p8us_mask);
2020}
2021
2022EIGEN_STRONG_INLINE Packet4f Bf16ToF32Even(const Packet8bf& bf) {
2023 return plogical_shift_left<16>(reinterpret_cast<Packet4f>(bf.m_val));
2024}
2025
2026EIGEN_STRONG_INLINE Packet4f Bf16ToF32Odd(const Packet8bf& bf) {
2027 const EIGEN_DECLARE_CONST_FAST_Packet4ui(high_mask, 0xFFFF0000);
2028 return pand<Packet4f>(reinterpret_cast<Packet4f>(bf.m_val), reinterpret_cast<Packet4f>(p4ui_high_mask));
2029}
2030
2031EIGEN_ALWAYS_INLINE Packet8us pmerge(Packet4ui even, Packet4ui odd) {
2032#ifdef _BIG_ENDIAN
2033 return vec_perm(reinterpret_cast<Packet8us>(odd), reinterpret_cast<Packet8us>(even), p16uc_MERGEO16);
2034#else
2035 return vec_perm(reinterpret_cast<Packet8us>(even), reinterpret_cast<Packet8us>(odd), p16uc_MERGEE16);
2036#endif
2037}
2038
2039// Simple interleaving of bool masks, prevents true values from being
2040// converted to NaNs.
2041EIGEN_STRONG_INLINE Packet8bf F32ToBf16Bool(Packet4f even, Packet4f odd) {
2042 return pmerge(reinterpret_cast<Packet4ui>(even), reinterpret_cast<Packet4ui>(odd));
2043}
2044
2045// #define SUPPORT_BF16_SUBNORMALS
2046
2047#ifndef __VEC_CLASS_FP_NAN
2048#define __VEC_CLASS_FP_NAN (1 << 6)
2049#endif
2050
2051#if defined(SUPPORT_BF16_SUBNORMALS) && !defined(__VEC_CLASS_FP_SUBNORMAL)
2052#define __VEC_CLASS_FP_SUBNORMAL_P (1 << 1)
2053#define __VEC_CLASS_FP_SUBNORMAL_N (1 << 0)
2054
2055#define __VEC_CLASS_FP_SUBNORMAL (__VEC_CLASS_FP_SUBNORMAL_P | __VEC_CLASS_FP_SUBNORMAL_N)
2056#endif
2057
2058EIGEN_STRONG_INLINE Packet8bf F32ToBf16(Packet4f p4f) {
2059#ifdef _ARCH_PWR10
2060 return reinterpret_cast<Packet8us>(__builtin_vsx_xvcvspbf16(reinterpret_cast<Packet16uc>(p4f)));
2061#else
2062 Packet4ui input = reinterpret_cast<Packet4ui>(p4f);
2063 Packet4ui lsb = plogical_shift_right<16>(input);
2064 lsb = pand<Packet4ui>(lsb, reinterpret_cast<Packet4ui>(p4i_ONE));
2065
2066 EIGEN_DECLARE_CONST_FAST_Packet4ui(BIAS, 0x7FFFu);
2067 Packet4ui rounding_bias = padd<Packet4ui>(lsb, p4ui_BIAS);
2068 input = padd<Packet4ui>(input, rounding_bias);
2069
2070 const EIGEN_DECLARE_CONST_FAST_Packet4ui(nan, 0x7FC00000);
2071#ifdef _ARCH_PWR9
2072 Packet4bi nan_selector = vec_test_data_class(p4f, __VEC_CLASS_FP_NAN);
2073 input = vec_sel(input, p4ui_nan, nan_selector);
2074
2075#ifdef SUPPORT_BF16_SUBNORMALS
2076 Packet4bi subnormal_selector = vec_test_data_class(p4f, __VEC_CLASS_FP_SUBNORMAL);
2077 input = vec_sel(input, reinterpret_cast<Packet4ui>(p4f), subnormal_selector);
2078#endif
2079#else
2080#ifdef SUPPORT_BF16_SUBNORMALS
2081 // Test NaN and Subnormal
2082 const EIGEN_DECLARE_CONST_FAST_Packet4ui(exp_mask, 0x7F800000);
2083 Packet4ui exp = pand<Packet4ui>(p4ui_exp_mask, reinterpret_cast<Packet4ui>(p4f));
2084
2085 const EIGEN_DECLARE_CONST_FAST_Packet4ui(mantissa_mask, 0x7FFFFF);
2086 Packet4ui mantissa = pand<Packet4ui>(p4ui_mantissa_mask, reinterpret_cast<Packet4ui>(p4f));
2087
2088 Packet4bi is_max_exp = vec_cmpeq(exp, p4ui_exp_mask);
2089 Packet4bi is_mant_zero = vec_cmpeq(mantissa, reinterpret_cast<Packet4ui>(p4i_ZERO));
2090
2091 Packet4ui nan_selector =
2092 pandnot<Packet4ui>(reinterpret_cast<Packet4ui>(is_max_exp), reinterpret_cast<Packet4ui>(is_mant_zero));
2093
2094 Packet4bi is_zero_exp = vec_cmpeq(exp, reinterpret_cast<Packet4ui>(p4i_ZERO));
2095
2096 Packet4ui subnormal_selector =
2097 pandnot<Packet4ui>(reinterpret_cast<Packet4ui>(is_zero_exp), reinterpret_cast<Packet4ui>(is_mant_zero));
2098
2099 input = vec_sel(input, p4ui_nan, nan_selector);
2100 input = vec_sel(input, reinterpret_cast<Packet4ui>(p4f), subnormal_selector);
2101#else
2102 // Test only NaN
2103 Packet4bi nan_selector = vec_cmpeq(p4f, p4f);
2104
2105 input = vec_sel(p4ui_nan, input, nan_selector);
2106#endif
2107#endif
2108
2109 input = plogical_shift_right<16>(input);
2110 return reinterpret_cast<Packet8us>(input);
2111#endif
2112}
2113
2114#ifdef _BIG_ENDIAN
2120template <bool lohi>
2121EIGEN_ALWAYS_INLINE Packet8bf Bf16PackHigh(Packet4f lo, Packet4f hi) {
2122 if (lohi) {
2123 return vec_perm(reinterpret_cast<Packet8us>(lo), reinterpret_cast<Packet8us>(hi), p16uc_MERGEH16);
2124 } else {
2125 return vec_perm(reinterpret_cast<Packet8us>(hi), reinterpret_cast<Packet8us>(lo), p16uc_MERGEE16);
2126 }
2127}
2128
2134template <bool lohi>
2135EIGEN_ALWAYS_INLINE Packet8bf Bf16PackLow(Packet4f lo, Packet4f hi) {
2136 if (lohi) {
2137 return vec_pack(reinterpret_cast<Packet4ui>(lo), reinterpret_cast<Packet4ui>(hi));
2138 } else {
2139 return vec_perm(reinterpret_cast<Packet8us>(hi), reinterpret_cast<Packet8us>(lo), p16uc_MERGEO16);
2140 }
2141}
2142#else
2143template <bool lohi>
2144EIGEN_ALWAYS_INLINE Packet8bf Bf16PackLow(Packet4f hi, Packet4f lo) {
2145 if (lohi) {
2146 return vec_pack(reinterpret_cast<Packet4ui>(hi), reinterpret_cast<Packet4ui>(lo));
2147 } else {
2148 return vec_perm(reinterpret_cast<Packet8us>(hi), reinterpret_cast<Packet8us>(lo), p16uc_MERGEE16);
2149 }
2150}
2151
2152template <bool lohi>
2153EIGEN_ALWAYS_INLINE Packet8bf Bf16PackHigh(Packet4f hi, Packet4f lo) {
2154 if (lohi) {
2155 return vec_perm(reinterpret_cast<Packet8us>(hi), reinterpret_cast<Packet8us>(lo), p16uc_MERGEL16);
2156 } else {
2157 return vec_perm(reinterpret_cast<Packet8us>(hi), reinterpret_cast<Packet8us>(lo), p16uc_MERGEO16);
2158 }
2159}
2160#endif
2161
2167template <bool lohi = true>
2168EIGEN_ALWAYS_INLINE Packet8bf F32ToBf16Two(Packet4f lo, Packet4f hi) {
2169 Packet8us p4f = Bf16PackHigh<lohi>(lo, hi);
2170 Packet8us p4f2 = Bf16PackLow<lohi>(lo, hi);
2171
2172 Packet8us lsb = pand<Packet8us>(p4f, p8us_ONE);
2173 EIGEN_DECLARE_CONST_FAST_Packet8us(BIAS, 0x7FFFu);
2174 lsb = padd<Packet8us>(lsb, p8us_BIAS);
2175 lsb = padd<Packet8us>(lsb, p4f2);
2176
2177 Packet8bi rounding_bias = vec_cmplt(lsb, p4f2);
2178 Packet8us input = psub<Packet8us>(p4f, reinterpret_cast<Packet8us>(rounding_bias));
2179
2180#ifdef _ARCH_PWR9
2181 Packet4bi nan_selector_lo = vec_test_data_class(lo, __VEC_CLASS_FP_NAN);
2182 Packet4bi nan_selector_hi = vec_test_data_class(hi, __VEC_CLASS_FP_NAN);
2183 Packet8us nan_selector =
2184 Bf16PackLow<lohi>(reinterpret_cast<Packet4f>(nan_selector_lo), reinterpret_cast<Packet4f>(nan_selector_hi));
2185
2186 input = vec_sel(input, p8us_BIAS, nan_selector);
2187
2188#ifdef SUPPORT_BF16_SUBNORMALS
2189 Packet4bi subnormal_selector_lo = vec_test_data_class(lo, __VEC_CLASS_FP_SUBNORMAL);
2190 Packet4bi subnormal_selector_hi = vec_test_data_class(hi, __VEC_CLASS_FP_SUBNORMAL);
2191 Packet8us subnormal_selector = Bf16PackLow<lohi>(reinterpret_cast<Packet4f>(subnormal_selector_lo),
2192 reinterpret_cast<Packet4f>(subnormal_selector_hi));
2193
2194 input = vec_sel(input, reinterpret_cast<Packet8us>(p4f), subnormal_selector);
2195#endif
2196#else
2197#ifdef SUPPORT_BF16_SUBNORMALS
2198 // Test NaN and Subnormal
2199 const EIGEN_DECLARE_CONST_FAST_Packet8us(exp_mask, 0x7F80);
2200 Packet8us exp = pand<Packet8us>(p8us_exp_mask, p4f);
2201
2202 const EIGEN_DECLARE_CONST_FAST_Packet8us(mantissa_mask, 0x7Fu);
2203 Packet8us mantissa = pand<Packet8us>(p8us_mantissa_mask, p4f);
2204
2205 Packet8bi is_max_exp = vec_cmpeq(exp, p8us_exp_mask);
2206 Packet8bi is_mant_zero = vec_cmpeq(mantissa, reinterpret_cast<Packet8us>(p4i_ZERO));
2207
2208 Packet8us nan_selector =
2209 pandnot<Packet8us>(reinterpret_cast<Packet8us>(is_max_exp), reinterpret_cast<Packet8us>(is_mant_zero));
2210
2211 Packet8bi is_zero_exp = vec_cmpeq(exp, reinterpret_cast<Packet8us>(p4i_ZERO));
2212
2213 Packet8us subnormal_selector =
2214 pandnot<Packet8us>(reinterpret_cast<Packet8us>(is_zero_exp), reinterpret_cast<Packet8us>(is_mant_zero));
2215
2216 // Using BIAS as NaN (since any or all of the last 7 bits can be set)
2217 input = vec_sel(input, p8us_BIAS, nan_selector);
2218 input = vec_sel(input, reinterpret_cast<Packet8us>(p4f), subnormal_selector);
2219#else
2220 // Test only NaN
2221 Packet4bi nan_selector_lo = vec_cmpeq(lo, lo);
2222 Packet4bi nan_selector_hi = vec_cmpeq(hi, hi);
2223 Packet8us nan_selector =
2224 Bf16PackLow<lohi>(reinterpret_cast<Packet4f>(nan_selector_lo), reinterpret_cast<Packet4f>(nan_selector_hi));
2225
2226 input = vec_sel(p8us_BIAS, input, nan_selector);
2227#endif
2228#endif
2229
2230 return input;
2231}
2232
2236EIGEN_STRONG_INLINE Packet8bf F32ToBf16Both(Packet4f lo, Packet4f hi) {
2237#ifdef _ARCH_PWR10
2238 Packet8bf fp16_0 = F32ToBf16(lo);
2239 Packet8bf fp16_1 = F32ToBf16(hi);
2240 return vec_pack(reinterpret_cast<Packet4ui>(fp16_0.m_val), reinterpret_cast<Packet4ui>(fp16_1.m_val));
2241#else
2242 return F32ToBf16Two(lo, hi);
2243#endif
2244}
2245
2249EIGEN_STRONG_INLINE Packet8bf F32ToBf16(Packet4f even, Packet4f odd) {
2250#ifdef _ARCH_PWR10
2251 return pmerge(reinterpret_cast<Packet4ui>(F32ToBf16(even).m_val), reinterpret_cast<Packet4ui>(F32ToBf16(odd).m_val));
2252#else
2253 return F32ToBf16Two<false>(even, odd);
2254#endif
2255}
2256#define BF16_TO_F32_UNARY_OP_WRAPPER(OP, A) \
2257 Packet4f a_even = Bf16ToF32Even(A); \
2258 Packet4f a_odd = Bf16ToF32Odd(A); \
2259 Packet4f op_even = OP(a_even); \
2260 Packet4f op_odd = OP(a_odd); \
2261 return F32ToBf16(op_even, op_odd);
2262
2263#define BF16_TO_F32_BINARY_OP_WRAPPER(OP, A, B) \
2264 Packet4f a_even = Bf16ToF32Even(A); \
2265 Packet4f a_odd = Bf16ToF32Odd(A); \
2266 Packet4f b_even = Bf16ToF32Even(B); \
2267 Packet4f b_odd = Bf16ToF32Odd(B); \
2268 Packet4f op_even = OP(a_even, b_even); \
2269 Packet4f op_odd = OP(a_odd, b_odd); \
2270 return F32ToBf16(op_even, op_odd);
2271
2272#define BF16_TO_F32_BINARY_OP_WRAPPER_BOOL(OP, A, B) \
2273 Packet4f a_even = Bf16ToF32Even(A); \
2274 Packet4f a_odd = Bf16ToF32Odd(A); \
2275 Packet4f b_even = Bf16ToF32Even(B); \
2276 Packet4f b_odd = Bf16ToF32Odd(B); \
2277 Packet4f op_even = OP(a_even, b_even); \
2278 Packet4f op_odd = OP(a_odd, b_odd); \
2279 return F32ToBf16Bool(op_even, op_odd);
2280
2281template <>
2282EIGEN_STRONG_INLINE Packet8bf padd<Packet8bf>(const Packet8bf& a, const Packet8bf& b) {
2283 BF16_TO_F32_BINARY_OP_WRAPPER(padd<Packet4f>, a, b);
2284}
2285
2286template <>
2287EIGEN_STRONG_INLINE Packet8bf pmul<Packet8bf>(const Packet8bf& a, const Packet8bf& b) {
2288 BF16_TO_F32_BINARY_OP_WRAPPER(pmul<Packet4f>, a, b);
2289}
2290
2291template <>
2292EIGEN_STRONG_INLINE Packet8bf pdiv<Packet8bf>(const Packet8bf& a, const Packet8bf& b) {
2293 BF16_TO_F32_BINARY_OP_WRAPPER(pdiv<Packet4f>, a, b);
2294}
2295
2296template <>
2297EIGEN_STRONG_INLINE Packet8bf pnegate<Packet8bf>(const Packet8bf& a) {
2298 EIGEN_DECLARE_CONST_FAST_Packet8us(neg_mask, 0x8000);
2299 return pxor<Packet8us>(p8us_neg_mask, a);
2300}
2301
2302template <>
2303EIGEN_STRONG_INLINE Packet8bf psub<Packet8bf>(const Packet8bf& a, const Packet8bf& b) {
2304 BF16_TO_F32_BINARY_OP_WRAPPER(psub<Packet4f>, a, b);
2305}
2306
2307template <>
2308EIGEN_STRONG_INLINE Packet8bf pexp<Packet8bf>(const Packet8bf& a) {
2309 BF16_TO_F32_UNARY_OP_WRAPPER(pexp_float, a);
2310}
2311
2312template <>
2313EIGEN_STRONG_INLINE Packet4f pldexp<Packet4f>(const Packet4f& a, const Packet4f& exponent) {
2314 return pldexp_generic(a, exponent);
2315}
2316template <>
2317EIGEN_STRONG_INLINE Packet8bf pldexp<Packet8bf>(const Packet8bf& a, const Packet8bf& exponent) {
2318 BF16_TO_F32_BINARY_OP_WRAPPER(pldexp<Packet4f>, a, exponent);
2319}
2320
2321template <>
2322EIGEN_STRONG_INLINE Packet4f pfrexp<Packet4f>(const Packet4f& a, Packet4f& exponent) {
2323 return pfrexp_generic(a, exponent);
2324}
2325template <>
2326EIGEN_STRONG_INLINE Packet8bf pfrexp<Packet8bf>(const Packet8bf& a, Packet8bf& e) {
2327 Packet4f a_even = Bf16ToF32Even(a);
2328 Packet4f a_odd = Bf16ToF32Odd(a);
2329 Packet4f e_even;
2330 Packet4f e_odd;
2331 Packet4f op_even = pfrexp<Packet4f>(a_even, e_even);
2332 Packet4f op_odd = pfrexp<Packet4f>(a_odd, e_odd);
2333 e = F32ToBf16(e_even, e_odd);
2334 return F32ToBf16(op_even, op_odd);
2335}
2336
2337template <>
2338EIGEN_STRONG_INLINE Packet8bf psin<Packet8bf>(const Packet8bf& a) {
2339 BF16_TO_F32_UNARY_OP_WRAPPER(psin_float, a);
2340}
2341template <>
2342EIGEN_STRONG_INLINE Packet8bf pcos<Packet8bf>(const Packet8bf& a) {
2343 BF16_TO_F32_UNARY_OP_WRAPPER(pcos_float, a);
2344}
2345template <>
2346EIGEN_STRONG_INLINE Packet8bf plog<Packet8bf>(const Packet8bf& a) {
2347 BF16_TO_F32_UNARY_OP_WRAPPER(plog_float, a);
2348}
2349template <>
2350EIGEN_STRONG_INLINE Packet8bf pfloor<Packet8bf>(const Packet8bf& a) {
2351 BF16_TO_F32_UNARY_OP_WRAPPER(pfloor<Packet4f>, a);
2352}
2353template <>
2354EIGEN_STRONG_INLINE Packet8bf pceil<Packet8bf>(const Packet8bf& a) {
2355 BF16_TO_F32_UNARY_OP_WRAPPER(pceil<Packet4f>, a);
2356}
2357template <>
2358EIGEN_STRONG_INLINE Packet8bf pround<Packet8bf>(const Packet8bf& a) {
2359 BF16_TO_F32_UNARY_OP_WRAPPER(pround<Packet4f>, a);
2360}
2361template <>
2362EIGEN_STRONG_INLINE Packet8bf ptrunc<Packet8bf>(const Packet8bf& a) {
2363 BF16_TO_F32_UNARY_OP_WRAPPER(ptrunc<Packet4f>, a);
2364}
2365#ifdef EIGEN_VECTORIZE_VSX
2366template <>
2367EIGEN_STRONG_INLINE Packet8bf print<Packet8bf>(const Packet8bf& a) {
2368 BF16_TO_F32_UNARY_OP_WRAPPER(print<Packet4f>, a);
2369}
2370#endif
2371template <>
2372EIGEN_STRONG_INLINE Packet8bf pmadd(const Packet8bf& a, const Packet8bf& b, const Packet8bf& c) {
2373 Packet4f a_even = Bf16ToF32Even(a);
2374 Packet4f a_odd = Bf16ToF32Odd(a);
2375 Packet4f b_even = Bf16ToF32Even(b);
2376 Packet4f b_odd = Bf16ToF32Odd(b);
2377 Packet4f c_even = Bf16ToF32Even(c);
2378 Packet4f c_odd = Bf16ToF32Odd(c);
2379 Packet4f pmadd_even = pmadd<Packet4f>(a_even, b_even, c_even);
2380 Packet4f pmadd_odd = pmadd<Packet4f>(a_odd, b_odd, c_odd);
2381 return F32ToBf16(pmadd_even, pmadd_odd);
2382}
2383
2384template <>
2385EIGEN_STRONG_INLINE Packet8bf pmin<Packet8bf>(const Packet8bf& a, const Packet8bf& b) {
2386 BF16_TO_F32_BINARY_OP_WRAPPER(pmin<Packet4f>, a, b);
2387}
2388
2389template <>
2390EIGEN_STRONG_INLINE Packet8bf pmax<Packet8bf>(const Packet8bf& a, const Packet8bf& b) {
2391 BF16_TO_F32_BINARY_OP_WRAPPER(pmax<Packet4f>, a, b);
2392}
2393
2394template <>
2395EIGEN_STRONG_INLINE Packet8bf pcmp_lt(const Packet8bf& a, const Packet8bf& b) {
2396 BF16_TO_F32_BINARY_OP_WRAPPER_BOOL(pcmp_lt<Packet4f>, a, b);
2397}
2398template <>
2399EIGEN_STRONG_INLINE Packet8bf pcmp_lt_or_nan(const Packet8bf& a, const Packet8bf& b) {
2400 BF16_TO_F32_BINARY_OP_WRAPPER_BOOL(pcmp_lt_or_nan<Packet4f>, a, b);
2401}
2402template <>
2403EIGEN_STRONG_INLINE Packet8bf pcmp_le(const Packet8bf& a, const Packet8bf& b) {
2404 BF16_TO_F32_BINARY_OP_WRAPPER_BOOL(pcmp_le<Packet4f>, a, b);
2405}
2406template <>
2407EIGEN_STRONG_INLINE Packet8bf pcmp_eq(const Packet8bf& a, const Packet8bf& b) {
2408 BF16_TO_F32_BINARY_OP_WRAPPER_BOOL(pcmp_eq<Packet4f>, a, b);
2409}
2410
2411template <>
2412EIGEN_STRONG_INLINE bfloat16 pfirst(const Packet8bf& a) {
2413 return Eigen::bfloat16_impl::raw_uint16_to_bfloat16((pfirst<Packet8us>(a)));
2414}
2415
2416template <>
2417EIGEN_STRONG_INLINE Packet8bf ploaddup<Packet8bf>(const bfloat16* from) {
2418 return ploaddup<Packet8us>(reinterpret_cast<const unsigned short int*>(from));
2419}
2420
2421template <>
2422EIGEN_STRONG_INLINE Packet8bf plset<Packet8bf>(const bfloat16& a) {
2423 bfloat16 countdown[8] = {bfloat16(0), bfloat16(1), bfloat16(2), bfloat16(3),
2424 bfloat16(4), bfloat16(5), bfloat16(6), bfloat16(7)};
2425 return padd<Packet8bf>(pset1<Packet8bf>(a), pload<Packet8bf>(countdown));
2426}
2427
2428template <>
2429EIGEN_STRONG_INLINE float predux<Packet4f>(const Packet4f& a) {
2430 Packet4f b, sum;
2431 b = vec_sld(a, a, 8);
2432 sum = a + b;
2433 b = vec_sld(sum, sum, 4);
2434 sum += b;
2435 return pfirst(sum);
2436}
2437
2438template <>
2439EIGEN_STRONG_INLINE int predux<Packet4i>(const Packet4i& a) {
2440 Packet4i b, sum;
2441 b = vec_sld(a, a, 8);
2442 sum = a + b;
2443 b = vec_sld(sum, sum, 4);
2444 sum += b;
2445 return pfirst(sum);
2446}
2447
2448template <>
2449EIGEN_STRONG_INLINE bfloat16 predux<Packet8bf>(const Packet8bf& a) {
2450 float redux_even = predux<Packet4f>(Bf16ToF32Even(a));
2451 float redux_odd = predux<Packet4f>(Bf16ToF32Odd(a));
2452 float f32_result = redux_even + redux_odd;
2453 return bfloat16(f32_result);
2454}
2455template <typename Packet>
2456EIGEN_STRONG_INLINE __UNPACK_TYPE__(Packet) predux_size8(const Packet& a) {
2457 union {
2458 Packet v;
2459 __UNPACK_TYPE__(Packet) n[8];
2460 } vt;
2461 vt.v = a;
2462
2463 EIGEN_ALIGN16 int first_loader[4] = {vt.n[0], vt.n[1], vt.n[2], vt.n[3]};
2464 EIGEN_ALIGN16 int second_loader[4] = {vt.n[4], vt.n[5], vt.n[6], vt.n[7]};
2465 Packet4i first_half = pload<Packet4i>(first_loader);
2466 Packet4i second_half = pload<Packet4i>(second_loader);
2467
2468 return static_cast<__UNPACK_TYPE__(Packet)>(predux(first_half) + predux(second_half));
2469}
2470
2471template <>
2472EIGEN_STRONG_INLINE short int predux<Packet8s>(const Packet8s& a) {
2473 return predux_size8<Packet8s>(a);
2474}
2475
2476template <>
2477EIGEN_STRONG_INLINE unsigned short int predux<Packet8us>(const Packet8us& a) {
2478 return predux_size8<Packet8us>(a);
2479}
2480
2481template <typename Packet>
2482EIGEN_STRONG_INLINE __UNPACK_TYPE__(Packet) predux_size16(const Packet& a) {
2483 union {
2484 Packet v;
2485 __UNPACK_TYPE__(Packet) n[16];
2486 } vt;
2487 vt.v = a;
2488
2489 EIGEN_ALIGN16 int first_loader[4] = {vt.n[0], vt.n[1], vt.n[2], vt.n[3]};
2490 EIGEN_ALIGN16 int second_loader[4] = {vt.n[4], vt.n[5], vt.n[6], vt.n[7]};
2491 EIGEN_ALIGN16 int third_loader[4] = {vt.n[8], vt.n[9], vt.n[10], vt.n[11]};
2492 EIGEN_ALIGN16 int fourth_loader[4] = {vt.n[12], vt.n[13], vt.n[14], vt.n[15]};
2493
2494 Packet4i first_quarter = pload<Packet4i>(first_loader);
2495 Packet4i second_quarter = pload<Packet4i>(second_loader);
2496 Packet4i third_quarter = pload<Packet4i>(third_loader);
2497 Packet4i fourth_quarter = pload<Packet4i>(fourth_loader);
2498
2499 return static_cast<__UNPACK_TYPE__(Packet)>(predux(first_quarter) + predux(second_quarter) + predux(third_quarter) +
2500 predux(fourth_quarter));
2501}
2502
2503template <>
2504EIGEN_STRONG_INLINE signed char predux<Packet16c>(const Packet16c& a) {
2505 return predux_size16<Packet16c>(a);
2506}
2507
2508template <>
2509EIGEN_STRONG_INLINE unsigned char predux<Packet16uc>(const Packet16uc& a) {
2510 return predux_size16<Packet16uc>(a);
2511}
2512
2513// Other reduction functions:
2514// mul
2515template <>
2516EIGEN_STRONG_INLINE float predux_mul<Packet4f>(const Packet4f& a) {
2517 Packet4f prod;
2518 prod = pmul(a, vec_sld(a, a, 8));
2519 return pfirst(pmul(prod, vec_sld(prod, prod, 4)));
2520}
2521
2522template <>
2523EIGEN_STRONG_INLINE int predux_mul<Packet4i>(const Packet4i& a) {
2524 EIGEN_ALIGN16 int aux[4];
2525 pstore(aux, a);
2526 return aux[0] * aux[1] * aux[2] * aux[3];
2527}
2528
2529template <>
2530EIGEN_STRONG_INLINE short int predux_mul<Packet8s>(const Packet8s& a) {
2531 Packet8s pair, quad, octo;
2532
2533 pair = vec_mul(a, vec_sld(a, a, 8));
2534 quad = vec_mul(pair, vec_sld(pair, pair, 4));
2535 octo = vec_mul(quad, vec_sld(quad, quad, 2));
2536
2537 return pfirst(octo);
2538}
2539
2540template <>
2541EIGEN_STRONG_INLINE unsigned short int predux_mul<Packet8us>(const Packet8us& a) {
2542 Packet8us pair, quad, octo;
2543
2544 pair = vec_mul(a, vec_sld(a, a, 8));
2545 quad = vec_mul(pair, vec_sld(pair, pair, 4));
2546 octo = vec_mul(quad, vec_sld(quad, quad, 2));
2547
2548 return pfirst(octo);
2549}
2550
2551template <>
2552EIGEN_STRONG_INLINE bfloat16 predux_mul<Packet8bf>(const Packet8bf& a) {
2553 float redux_even = predux_mul<Packet4f>(Bf16ToF32Even(a));
2554 float redux_odd = predux_mul<Packet4f>(Bf16ToF32Odd(a));
2555 float f32_result = redux_even * redux_odd;
2556 return bfloat16(f32_result);
2557}
2558
2559template <>
2560EIGEN_STRONG_INLINE signed char predux_mul<Packet16c>(const Packet16c& a) {
2561 Packet16c pair, quad, octo, result;
2562
2563 pair = vec_mul(a, vec_sld(a, a, 8));
2564 quad = vec_mul(pair, vec_sld(pair, pair, 4));
2565 octo = vec_mul(quad, vec_sld(quad, quad, 2));
2566 result = vec_mul(octo, vec_sld(octo, octo, 1));
2567
2568 return pfirst(result);
2569}
2570
2571template <>
2572EIGEN_STRONG_INLINE unsigned char predux_mul<Packet16uc>(const Packet16uc& a) {
2573 Packet16uc pair, quad, octo, result;
2574
2575 pair = vec_mul(a, vec_sld(a, a, 8));
2576 quad = vec_mul(pair, vec_sld(pair, pair, 4));
2577 octo = vec_mul(quad, vec_sld(quad, quad, 2));
2578 result = vec_mul(octo, vec_sld(octo, octo, 1));
2579
2580 return pfirst(result);
2581}
2582
2583// min
2584template <typename Packet>
2585EIGEN_STRONG_INLINE __UNPACK_TYPE__(Packet) predux_min4(const Packet& a) {
2586 Packet b, res;
2587 b = vec_min(a, vec_sld(a, a, 8));
2588 res = vec_min(b, vec_sld(b, b, 4));
2589 return pfirst(res);
2590}
2591
2592template <>
2593EIGEN_STRONG_INLINE float predux_min<Packet4f>(const Packet4f& a) {
2594 return predux_min4<Packet4f>(a);
2595}
2596
2597template <>
2598EIGEN_STRONG_INLINE int predux_min<Packet4i>(const Packet4i& a) {
2599 return predux_min4<Packet4i>(a);
2600}
2601
2602template <>
2603EIGEN_STRONG_INLINE bfloat16 predux_min<Packet8bf>(const Packet8bf& a) {
2604 float redux_even = predux_min<Packet4f>(Bf16ToF32Even(a));
2605 float redux_odd = predux_min<Packet4f>(Bf16ToF32Odd(a));
2606 float f32_result = (std::min)(redux_even, redux_odd);
2607 return bfloat16(f32_result);
2608}
2609
2610template <>
2611EIGEN_STRONG_INLINE short int predux_min<Packet8s>(const Packet8s& a) {
2612 Packet8s pair, quad, octo;
2613
2614 // pair = { Min(a0,a4), Min(a1,a5), Min(a2,a6), Min(a3,a7) }
2615 pair = vec_min(a, vec_sld(a, a, 8));
2616
2617 // quad = { Min(a0, a4, a2, a6), Min(a1, a5, a3, a7) }
2618 quad = vec_min(pair, vec_sld(pair, pair, 4));
2619
2620 // octo = { Min(a0, a4, a2, a6, a1, a5, a3, a7) }
2621 octo = vec_min(quad, vec_sld(quad, quad, 2));
2622 return pfirst(octo);
2623}
2624
2625template <>
2626EIGEN_STRONG_INLINE unsigned short int predux_min<Packet8us>(const Packet8us& a) {
2627 Packet8us pair, quad, octo;
2628
2629 // pair = { Min(a0,a4), Min(a1,a5), Min(a2,a6), Min(a3,a7) }
2630 pair = vec_min(a, vec_sld(a, a, 8));
2631
2632 // quad = { Min(a0, a4, a2, a6), Min(a1, a5, a3, a7) }
2633 quad = vec_min(pair, vec_sld(pair, pair, 4));
2634
2635 // octo = { Min(a0, a4, a2, a6, a1, a5, a3, a7) }
2636 octo = vec_min(quad, vec_sld(quad, quad, 2));
2637 return pfirst(octo);
2638}
2639
2640template <>
2641EIGEN_STRONG_INLINE signed char predux_min<Packet16c>(const Packet16c& a) {
2642 Packet16c pair, quad, octo, result;
2643
2644 pair = vec_min(a, vec_sld(a, a, 8));
2645 quad = vec_min(pair, vec_sld(pair, pair, 4));
2646 octo = vec_min(quad, vec_sld(quad, quad, 2));
2647 result = vec_min(octo, vec_sld(octo, octo, 1));
2648
2649 return pfirst(result);
2650}
2651
2652template <>
2653EIGEN_STRONG_INLINE unsigned char predux_min<Packet16uc>(const Packet16uc& a) {
2654 Packet16uc pair, quad, octo, result;
2655
2656 pair = vec_min(a, vec_sld(a, a, 8));
2657 quad = vec_min(pair, vec_sld(pair, pair, 4));
2658 octo = vec_min(quad, vec_sld(quad, quad, 2));
2659 result = vec_min(octo, vec_sld(octo, octo, 1));
2660
2661 return pfirst(result);
2662}
2663// max
2664template <typename Packet>
2665EIGEN_STRONG_INLINE __UNPACK_TYPE__(Packet) predux_max4(const Packet& a) {
2666 Packet b, res;
2667 b = vec_max(a, vec_sld(a, a, 8));
2668 res = vec_max(b, vec_sld(b, b, 4));
2669 return pfirst(res);
2670}
2671
2672template <>
2673EIGEN_STRONG_INLINE float predux_max<Packet4f>(const Packet4f& a) {
2674 return predux_max4<Packet4f>(a);
2675}
2676
2677template <>
2678EIGEN_STRONG_INLINE int predux_max<Packet4i>(const Packet4i& a) {
2679 return predux_max4<Packet4i>(a);
2680}
2681
2682template <>
2683EIGEN_STRONG_INLINE bfloat16 predux_max<Packet8bf>(const Packet8bf& a) {
2684 float redux_even = predux_max<Packet4f>(Bf16ToF32Even(a));
2685 float redux_odd = predux_max<Packet4f>(Bf16ToF32Odd(a));
2686 float f32_result = (std::max)(redux_even, redux_odd);
2687 return bfloat16(f32_result);
2688}
2689
2690template <>
2691EIGEN_STRONG_INLINE short int predux_max<Packet8s>(const Packet8s& a) {
2692 Packet8s pair, quad, octo;
2693
2694 // pair = { Max(a0,a4), Max(a1,a5), Max(a2,a6), Max(a3,a7) }
2695 pair = vec_max(a, vec_sld(a, a, 8));
2696
2697 // quad = { Max(a0, a4, a2, a6), Max(a1, a5, a3, a7) }
2698 quad = vec_max(pair, vec_sld(pair, pair, 4));
2699
2700 // octo = { Max(a0, a4, a2, a6, a1, a5, a3, a7) }
2701 octo = vec_max(quad, vec_sld(quad, quad, 2));
2702 return pfirst(octo);
2703}
2704
2705template <>
2706EIGEN_STRONG_INLINE unsigned short int predux_max<Packet8us>(const Packet8us& a) {
2707 Packet8us pair, quad, octo;
2708
2709 // pair = { Max(a0,a4), Max(a1,a5), Max(a2,a6), Max(a3,a7) }
2710 pair = vec_max(a, vec_sld(a, a, 8));
2711
2712 // quad = { Max(a0, a4, a2, a6), Max(a1, a5, a3, a7) }
2713 quad = vec_max(pair, vec_sld(pair, pair, 4));
2714
2715 // octo = { Max(a0, a4, a2, a6, a1, a5, a3, a7) }
2716 octo = vec_max(quad, vec_sld(quad, quad, 2));
2717 return pfirst(octo);
2718}
2719
2720template <>
2721EIGEN_STRONG_INLINE signed char predux_max<Packet16c>(const Packet16c& a) {
2722 Packet16c pair, quad, octo, result;
2723
2724 pair = vec_max(a, vec_sld(a, a, 8));
2725 quad = vec_max(pair, vec_sld(pair, pair, 4));
2726 octo = vec_max(quad, vec_sld(quad, quad, 2));
2727 result = vec_max(octo, vec_sld(octo, octo, 1));
2728
2729 return pfirst(result);
2730}
2731
2732template <>
2733EIGEN_STRONG_INLINE unsigned char predux_max<Packet16uc>(const Packet16uc& a) {
2734 Packet16uc pair, quad, octo, result;
2735
2736 pair = vec_max(a, vec_sld(a, a, 8));
2737 quad = vec_max(pair, vec_sld(pair, pair, 4));
2738 octo = vec_max(quad, vec_sld(quad, quad, 2));
2739 result = vec_max(octo, vec_sld(octo, octo, 1));
2740
2741 return pfirst(result);
2742}
2743
2744template <>
2745EIGEN_STRONG_INLINE bool predux_any(const Packet4f& x) {
2746 return vec_any_ne(x, pzero(x));
2747}
2748
2749template <typename T>
2750EIGEN_DEVICE_FUNC inline void ptranpose_common(PacketBlock<T, 4>& kernel) {
2751 T t0, t1, t2, t3;
2752 t0 = vec_mergeh(kernel.packet[0], kernel.packet[2]);
2753 t1 = vec_mergel(kernel.packet[0], kernel.packet[2]);
2754 t2 = vec_mergeh(kernel.packet[1], kernel.packet[3]);
2755 t3 = vec_mergel(kernel.packet[1], kernel.packet[3]);
2756 kernel.packet[0] = vec_mergeh(t0, t2);
2757 kernel.packet[1] = vec_mergel(t0, t2);
2758 kernel.packet[2] = vec_mergeh(t1, t3);
2759 kernel.packet[3] = vec_mergel(t1, t3);
2760}
2761
2762EIGEN_DEVICE_FUNC inline void ptranspose(PacketBlock<Packet4f, 4>& kernel) { ptranpose_common<Packet4f>(kernel); }
2763
2764EIGEN_DEVICE_FUNC inline void ptranspose(PacketBlock<Packet4i, 4>& kernel) { ptranpose_common<Packet4i>(kernel); }
2765
2766EIGEN_DEVICE_FUNC inline void ptranspose(PacketBlock<Packet8s, 4>& kernel) {
2767 Packet8s t0, t1, t2, t3;
2768 t0 = vec_mergeh(kernel.packet[0], kernel.packet[2]);
2769 t1 = vec_mergel(kernel.packet[0], kernel.packet[2]);
2770 t2 = vec_mergeh(kernel.packet[1], kernel.packet[3]);
2771 t3 = vec_mergel(kernel.packet[1], kernel.packet[3]);
2772 kernel.packet[0] = vec_mergeh(t0, t2);
2773 kernel.packet[1] = vec_mergel(t0, t2);
2774 kernel.packet[2] = vec_mergeh(t1, t3);
2775 kernel.packet[3] = vec_mergel(t1, t3);
2776}
2777
2778EIGEN_DEVICE_FUNC inline void ptranspose(PacketBlock<Packet8us, 4>& kernel) {
2779 Packet8us t0, t1, t2, t3;
2780 t0 = vec_mergeh(kernel.packet[0], kernel.packet[2]);
2781 t1 = vec_mergel(kernel.packet[0], kernel.packet[2]);
2782 t2 = vec_mergeh(kernel.packet[1], kernel.packet[3]);
2783 t3 = vec_mergel(kernel.packet[1], kernel.packet[3]);
2784 kernel.packet[0] = vec_mergeh(t0, t2);
2785 kernel.packet[1] = vec_mergel(t0, t2);
2786 kernel.packet[2] = vec_mergeh(t1, t3);
2787 kernel.packet[3] = vec_mergel(t1, t3);
2788}
2789
2790EIGEN_DEVICE_FUNC inline void ptranspose(PacketBlock<Packet8bf, 4>& kernel) {
2791 Packet8us t0, t1, t2, t3;
2792
2793 t0 = vec_mergeh(kernel.packet[0].m_val, kernel.packet[2].m_val);
2794 t1 = vec_mergel(kernel.packet[0].m_val, kernel.packet[2].m_val);
2795 t2 = vec_mergeh(kernel.packet[1].m_val, kernel.packet[3].m_val);
2796 t3 = vec_mergel(kernel.packet[1].m_val, kernel.packet[3].m_val);
2797 kernel.packet[0] = vec_mergeh(t0, t2);
2798 kernel.packet[1] = vec_mergel(t0, t2);
2799 kernel.packet[2] = vec_mergeh(t1, t3);
2800 kernel.packet[3] = vec_mergel(t1, t3);
2801}
2802
2803EIGEN_DEVICE_FUNC inline void ptranspose(PacketBlock<Packet16c, 4>& kernel) {
2804 Packet16c t0, t1, t2, t3;
2805 t0 = vec_mergeh(kernel.packet[0], kernel.packet[2]);
2806 t1 = vec_mergel(kernel.packet[0], kernel.packet[2]);
2807 t2 = vec_mergeh(kernel.packet[1], kernel.packet[3]);
2808 t3 = vec_mergel(kernel.packet[1], kernel.packet[3]);
2809 kernel.packet[0] = vec_mergeh(t0, t2);
2810 kernel.packet[1] = vec_mergel(t0, t2);
2811 kernel.packet[2] = vec_mergeh(t1, t3);
2812 kernel.packet[3] = vec_mergel(t1, t3);
2813}
2814
2815EIGEN_DEVICE_FUNC inline void ptranspose(PacketBlock<Packet16uc, 4>& kernel) {
2816 Packet16uc t0, t1, t2, t3;
2817 t0 = vec_mergeh(kernel.packet[0], kernel.packet[2]);
2818 t1 = vec_mergel(kernel.packet[0], kernel.packet[2]);
2819 t2 = vec_mergeh(kernel.packet[1], kernel.packet[3]);
2820 t3 = vec_mergel(kernel.packet[1], kernel.packet[3]);
2821 kernel.packet[0] = vec_mergeh(t0, t2);
2822 kernel.packet[1] = vec_mergel(t0, t2);
2823 kernel.packet[2] = vec_mergeh(t1, t3);
2824 kernel.packet[3] = vec_mergel(t1, t3);
2825}
2826
2827EIGEN_DEVICE_FUNC inline void ptranspose(PacketBlock<Packet8s, 8>& kernel) {
2828 Packet8s v[8], sum[8];
2829
2830 v[0] = vec_mergeh(kernel.packet[0], kernel.packet[4]);
2831 v[1] = vec_mergel(kernel.packet[0], kernel.packet[4]);
2832 v[2] = vec_mergeh(kernel.packet[1], kernel.packet[5]);
2833 v[3] = vec_mergel(kernel.packet[1], kernel.packet[5]);
2834 v[4] = vec_mergeh(kernel.packet[2], kernel.packet[6]);
2835 v[5] = vec_mergel(kernel.packet[2], kernel.packet[6]);
2836 v[6] = vec_mergeh(kernel.packet[3], kernel.packet[7]);
2837 v[7] = vec_mergel(kernel.packet[3], kernel.packet[7]);
2838 sum[0] = vec_mergeh(v[0], v[4]);
2839 sum[1] = vec_mergel(v[0], v[4]);
2840 sum[2] = vec_mergeh(v[1], v[5]);
2841 sum[3] = vec_mergel(v[1], v[5]);
2842 sum[4] = vec_mergeh(v[2], v[6]);
2843 sum[5] = vec_mergel(v[2], v[6]);
2844 sum[6] = vec_mergeh(v[3], v[7]);
2845 sum[7] = vec_mergel(v[3], v[7]);
2846
2847 kernel.packet[0] = vec_mergeh(sum[0], sum[4]);
2848 kernel.packet[1] = vec_mergel(sum[0], sum[4]);
2849 kernel.packet[2] = vec_mergeh(sum[1], sum[5]);
2850 kernel.packet[3] = vec_mergel(sum[1], sum[5]);
2851 kernel.packet[4] = vec_mergeh(sum[2], sum[6]);
2852 kernel.packet[5] = vec_mergel(sum[2], sum[6]);
2853 kernel.packet[6] = vec_mergeh(sum[3], sum[7]);
2854 kernel.packet[7] = vec_mergel(sum[3], sum[7]);
2855}
2856
2857EIGEN_DEVICE_FUNC inline void ptranspose(PacketBlock<Packet8us, 8>& kernel) {
2858 Packet8us v[8], sum[8];
2859
2860 v[0] = vec_mergeh(kernel.packet[0], kernel.packet[4]);
2861 v[1] = vec_mergel(kernel.packet[0], kernel.packet[4]);
2862 v[2] = vec_mergeh(kernel.packet[1], kernel.packet[5]);
2863 v[3] = vec_mergel(kernel.packet[1], kernel.packet[5]);
2864 v[4] = vec_mergeh(kernel.packet[2], kernel.packet[6]);
2865 v[5] = vec_mergel(kernel.packet[2], kernel.packet[6]);
2866 v[6] = vec_mergeh(kernel.packet[3], kernel.packet[7]);
2867 v[7] = vec_mergel(kernel.packet[3], kernel.packet[7]);
2868 sum[0] = vec_mergeh(v[0], v[4]);
2869 sum[1] = vec_mergel(v[0], v[4]);
2870 sum[2] = vec_mergeh(v[1], v[5]);
2871 sum[3] = vec_mergel(v[1], v[5]);
2872 sum[4] = vec_mergeh(v[2], v[6]);
2873 sum[5] = vec_mergel(v[2], v[6]);
2874 sum[6] = vec_mergeh(v[3], v[7]);
2875 sum[7] = vec_mergel(v[3], v[7]);
2876
2877 kernel.packet[0] = vec_mergeh(sum[0], sum[4]);
2878 kernel.packet[1] = vec_mergel(sum[0], sum[4]);
2879 kernel.packet[2] = vec_mergeh(sum[1], sum[5]);
2880 kernel.packet[3] = vec_mergel(sum[1], sum[5]);
2881 kernel.packet[4] = vec_mergeh(sum[2], sum[6]);
2882 kernel.packet[5] = vec_mergel(sum[2], sum[6]);
2883 kernel.packet[6] = vec_mergeh(sum[3], sum[7]);
2884 kernel.packet[7] = vec_mergel(sum[3], sum[7]);
2885}
2886
2887EIGEN_DEVICE_FUNC inline void ptranspose(PacketBlock<Packet8bf, 8>& kernel) {
2888 Packet8bf v[8], sum[8];
2889
2890 v[0] = vec_mergeh(kernel.packet[0].m_val, kernel.packet[4].m_val);
2891 v[1] = vec_mergel(kernel.packet[0].m_val, kernel.packet[4].m_val);
2892 v[2] = vec_mergeh(kernel.packet[1].m_val, kernel.packet[5].m_val);
2893 v[3] = vec_mergel(kernel.packet[1].m_val, kernel.packet[5].m_val);
2894 v[4] = vec_mergeh(kernel.packet[2].m_val, kernel.packet[6].m_val);
2895 v[5] = vec_mergel(kernel.packet[2].m_val, kernel.packet[6].m_val);
2896 v[6] = vec_mergeh(kernel.packet[3].m_val, kernel.packet[7].m_val);
2897 v[7] = vec_mergel(kernel.packet[3].m_val, kernel.packet[7].m_val);
2898 sum[0] = vec_mergeh(v[0].m_val, v[4].m_val);
2899 sum[1] = vec_mergel(v[0].m_val, v[4].m_val);
2900 sum[2] = vec_mergeh(v[1].m_val, v[5].m_val);
2901 sum[3] = vec_mergel(v[1].m_val, v[5].m_val);
2902 sum[4] = vec_mergeh(v[2].m_val, v[6].m_val);
2903 sum[5] = vec_mergel(v[2].m_val, v[6].m_val);
2904 sum[6] = vec_mergeh(v[3].m_val, v[7].m_val);
2905 sum[7] = vec_mergel(v[3].m_val, v[7].m_val);
2906
2907 kernel.packet[0] = vec_mergeh(sum[0].m_val, sum[4].m_val);
2908 kernel.packet[1] = vec_mergel(sum[0].m_val, sum[4].m_val);
2909 kernel.packet[2] = vec_mergeh(sum[1].m_val, sum[5].m_val);
2910 kernel.packet[3] = vec_mergel(sum[1].m_val, sum[5].m_val);
2911 kernel.packet[4] = vec_mergeh(sum[2].m_val, sum[6].m_val);
2912 kernel.packet[5] = vec_mergel(sum[2].m_val, sum[6].m_val);
2913 kernel.packet[6] = vec_mergeh(sum[3].m_val, sum[7].m_val);
2914 kernel.packet[7] = vec_mergel(sum[3].m_val, sum[7].m_val);
2915}
2916
2917EIGEN_DEVICE_FUNC inline void ptranspose(PacketBlock<Packet16c, 16>& kernel) {
2918 Packet16c step1[16], step2[16], step3[16];
2919
2920 step1[0] = vec_mergeh(kernel.packet[0], kernel.packet[8]);
2921 step1[1] = vec_mergel(kernel.packet[0], kernel.packet[8]);
2922 step1[2] = vec_mergeh(kernel.packet[1], kernel.packet[9]);
2923 step1[3] = vec_mergel(kernel.packet[1], kernel.packet[9]);
2924 step1[4] = vec_mergeh(kernel.packet[2], kernel.packet[10]);
2925 step1[5] = vec_mergel(kernel.packet[2], kernel.packet[10]);
2926 step1[6] = vec_mergeh(kernel.packet[3], kernel.packet[11]);
2927 step1[7] = vec_mergel(kernel.packet[3], kernel.packet[11]);
2928 step1[8] = vec_mergeh(kernel.packet[4], kernel.packet[12]);
2929 step1[9] = vec_mergel(kernel.packet[4], kernel.packet[12]);
2930 step1[10] = vec_mergeh(kernel.packet[5], kernel.packet[13]);
2931 step1[11] = vec_mergel(kernel.packet[5], kernel.packet[13]);
2932 step1[12] = vec_mergeh(kernel.packet[6], kernel.packet[14]);
2933 step1[13] = vec_mergel(kernel.packet[6], kernel.packet[14]);
2934 step1[14] = vec_mergeh(kernel.packet[7], kernel.packet[15]);
2935 step1[15] = vec_mergel(kernel.packet[7], kernel.packet[15]);
2936
2937 step2[0] = vec_mergeh(step1[0], step1[8]);
2938 step2[1] = vec_mergel(step1[0], step1[8]);
2939 step2[2] = vec_mergeh(step1[1], step1[9]);
2940 step2[3] = vec_mergel(step1[1], step1[9]);
2941 step2[4] = vec_mergeh(step1[2], step1[10]);
2942 step2[5] = vec_mergel(step1[2], step1[10]);
2943 step2[6] = vec_mergeh(step1[3], step1[11]);
2944 step2[7] = vec_mergel(step1[3], step1[11]);
2945 step2[8] = vec_mergeh(step1[4], step1[12]);
2946 step2[9] = vec_mergel(step1[4], step1[12]);
2947 step2[10] = vec_mergeh(step1[5], step1[13]);
2948 step2[11] = vec_mergel(step1[5], step1[13]);
2949 step2[12] = vec_mergeh(step1[6], step1[14]);
2950 step2[13] = vec_mergel(step1[6], step1[14]);
2951 step2[14] = vec_mergeh(step1[7], step1[15]);
2952 step2[15] = vec_mergel(step1[7], step1[15]);
2953
2954 step3[0] = vec_mergeh(step2[0], step2[8]);
2955 step3[1] = vec_mergel(step2[0], step2[8]);
2956 step3[2] = vec_mergeh(step2[1], step2[9]);
2957 step3[3] = vec_mergel(step2[1], step2[9]);
2958 step3[4] = vec_mergeh(step2[2], step2[10]);
2959 step3[5] = vec_mergel(step2[2], step2[10]);
2960 step3[6] = vec_mergeh(step2[3], step2[11]);
2961 step3[7] = vec_mergel(step2[3], step2[11]);
2962 step3[8] = vec_mergeh(step2[4], step2[12]);
2963 step3[9] = vec_mergel(step2[4], step2[12]);
2964 step3[10] = vec_mergeh(step2[5], step2[13]);
2965 step3[11] = vec_mergel(step2[5], step2[13]);
2966 step3[12] = vec_mergeh(step2[6], step2[14]);
2967 step3[13] = vec_mergel(step2[6], step2[14]);
2968 step3[14] = vec_mergeh(step2[7], step2[15]);
2969 step3[15] = vec_mergel(step2[7], step2[15]);
2970
2971 kernel.packet[0] = vec_mergeh(step3[0], step3[8]);
2972 kernel.packet[1] = vec_mergel(step3[0], step3[8]);
2973 kernel.packet[2] = vec_mergeh(step3[1], step3[9]);
2974 kernel.packet[3] = vec_mergel(step3[1], step3[9]);
2975 kernel.packet[4] = vec_mergeh(step3[2], step3[10]);
2976 kernel.packet[5] = vec_mergel(step3[2], step3[10]);
2977 kernel.packet[6] = vec_mergeh(step3[3], step3[11]);
2978 kernel.packet[7] = vec_mergel(step3[3], step3[11]);
2979 kernel.packet[8] = vec_mergeh(step3[4], step3[12]);
2980 kernel.packet[9] = vec_mergel(step3[4], step3[12]);
2981 kernel.packet[10] = vec_mergeh(step3[5], step3[13]);
2982 kernel.packet[11] = vec_mergel(step3[5], step3[13]);
2983 kernel.packet[12] = vec_mergeh(step3[6], step3[14]);
2984 kernel.packet[13] = vec_mergel(step3[6], step3[14]);
2985 kernel.packet[14] = vec_mergeh(step3[7], step3[15]);
2986 kernel.packet[15] = vec_mergel(step3[7], step3[15]);
2987}
2988
2989EIGEN_DEVICE_FUNC inline void ptranspose(PacketBlock<Packet16uc, 16>& kernel) {
2990 Packet16uc step1[16], step2[16], step3[16];
2991
2992 step1[0] = vec_mergeh(kernel.packet[0], kernel.packet[8]);
2993 step1[1] = vec_mergel(kernel.packet[0], kernel.packet[8]);
2994 step1[2] = vec_mergeh(kernel.packet[1], kernel.packet[9]);
2995 step1[3] = vec_mergel(kernel.packet[1], kernel.packet[9]);
2996 step1[4] = vec_mergeh(kernel.packet[2], kernel.packet[10]);
2997 step1[5] = vec_mergel(kernel.packet[2], kernel.packet[10]);
2998 step1[6] = vec_mergeh(kernel.packet[3], kernel.packet[11]);
2999 step1[7] = vec_mergel(kernel.packet[3], kernel.packet[11]);
3000 step1[8] = vec_mergeh(kernel.packet[4], kernel.packet[12]);
3001 step1[9] = vec_mergel(kernel.packet[4], kernel.packet[12]);
3002 step1[10] = vec_mergeh(kernel.packet[5], kernel.packet[13]);
3003 step1[11] = vec_mergel(kernel.packet[5], kernel.packet[13]);
3004 step1[12] = vec_mergeh(kernel.packet[6], kernel.packet[14]);
3005 step1[13] = vec_mergel(kernel.packet[6], kernel.packet[14]);
3006 step1[14] = vec_mergeh(kernel.packet[7], kernel.packet[15]);
3007 step1[15] = vec_mergel(kernel.packet[7], kernel.packet[15]);
3008
3009 step2[0] = vec_mergeh(step1[0], step1[8]);
3010 step2[1] = vec_mergel(step1[0], step1[8]);
3011 step2[2] = vec_mergeh(step1[1], step1[9]);
3012 step2[3] = vec_mergel(step1[1], step1[9]);
3013 step2[4] = vec_mergeh(step1[2], step1[10]);
3014 step2[5] = vec_mergel(step1[2], step1[10]);
3015 step2[6] = vec_mergeh(step1[3], step1[11]);
3016 step2[7] = vec_mergel(step1[3], step1[11]);
3017 step2[8] = vec_mergeh(step1[4], step1[12]);
3018 step2[9] = vec_mergel(step1[4], step1[12]);
3019 step2[10] = vec_mergeh(step1[5], step1[13]);
3020 step2[11] = vec_mergel(step1[5], step1[13]);
3021 step2[12] = vec_mergeh(step1[6], step1[14]);
3022 step2[13] = vec_mergel(step1[6], step1[14]);
3023 step2[14] = vec_mergeh(step1[7], step1[15]);
3024 step2[15] = vec_mergel(step1[7], step1[15]);
3025
3026 step3[0] = vec_mergeh(step2[0], step2[8]);
3027 step3[1] = vec_mergel(step2[0], step2[8]);
3028 step3[2] = vec_mergeh(step2[1], step2[9]);
3029 step3[3] = vec_mergel(step2[1], step2[9]);
3030 step3[4] = vec_mergeh(step2[2], step2[10]);
3031 step3[5] = vec_mergel(step2[2], step2[10]);
3032 step3[6] = vec_mergeh(step2[3], step2[11]);
3033 step3[7] = vec_mergel(step2[3], step2[11]);
3034 step3[8] = vec_mergeh(step2[4], step2[12]);
3035 step3[9] = vec_mergel(step2[4], step2[12]);
3036 step3[10] = vec_mergeh(step2[5], step2[13]);
3037 step3[11] = vec_mergel(step2[5], step2[13]);
3038 step3[12] = vec_mergeh(step2[6], step2[14]);
3039 step3[13] = vec_mergel(step2[6], step2[14]);
3040 step3[14] = vec_mergeh(step2[7], step2[15]);
3041 step3[15] = vec_mergel(step2[7], step2[15]);
3042
3043 kernel.packet[0] = vec_mergeh(step3[0], step3[8]);
3044 kernel.packet[1] = vec_mergel(step3[0], step3[8]);
3045 kernel.packet[2] = vec_mergeh(step3[1], step3[9]);
3046 kernel.packet[3] = vec_mergel(step3[1], step3[9]);
3047 kernel.packet[4] = vec_mergeh(step3[2], step3[10]);
3048 kernel.packet[5] = vec_mergel(step3[2], step3[10]);
3049 kernel.packet[6] = vec_mergeh(step3[3], step3[11]);
3050 kernel.packet[7] = vec_mergel(step3[3], step3[11]);
3051 kernel.packet[8] = vec_mergeh(step3[4], step3[12]);
3052 kernel.packet[9] = vec_mergel(step3[4], step3[12]);
3053 kernel.packet[10] = vec_mergeh(step3[5], step3[13]);
3054 kernel.packet[11] = vec_mergel(step3[5], step3[13]);
3055 kernel.packet[12] = vec_mergeh(step3[6], step3[14]);
3056 kernel.packet[13] = vec_mergel(step3[6], step3[14]);
3057 kernel.packet[14] = vec_mergeh(step3[7], step3[15]);
3058 kernel.packet[15] = vec_mergel(step3[7], step3[15]);
3059}
3060
3061template <typename Packet>
3062EIGEN_STRONG_INLINE Packet pblend4(const Selector<4>& ifPacket, const Packet& thenPacket, const Packet& elsePacket) {
3063 Packet4ui select = {ifPacket.select[0], ifPacket.select[1], ifPacket.select[2], ifPacket.select[3]};
3064 Packet4ui mask = reinterpret_cast<Packet4ui>(pnegate(reinterpret_cast<Packet4i>(select)));
3065 return vec_sel(elsePacket, thenPacket, mask);
3066}
3067
3068template <>
3069EIGEN_STRONG_INLINE Packet4i pblend(const Selector<4>& ifPacket, const Packet4i& thenPacket,
3070 const Packet4i& elsePacket) {
3071 return pblend4<Packet4i>(ifPacket, thenPacket, elsePacket);
3072}
3073
3074template <>
3075EIGEN_STRONG_INLINE Packet4f pblend(const Selector<4>& ifPacket, const Packet4f& thenPacket,
3076 const Packet4f& elsePacket) {
3077 return pblend4<Packet4f>(ifPacket, thenPacket, elsePacket);
3078}
3079
3080template <>
3081EIGEN_STRONG_INLINE Packet8s pblend(const Selector<8>& ifPacket, const Packet8s& thenPacket,
3082 const Packet8s& elsePacket) {
3083 Packet8us select = {ifPacket.select[0], ifPacket.select[1], ifPacket.select[2], ifPacket.select[3],
3084 ifPacket.select[4], ifPacket.select[5], ifPacket.select[6], ifPacket.select[7]};
3085 Packet8us mask = reinterpret_cast<Packet8us>(pnegate(reinterpret_cast<Packet8s>(select)));
3086 Packet8s result = vec_sel(elsePacket, thenPacket, mask);
3087 return result;
3088}
3089
3090template <>
3091EIGEN_STRONG_INLINE Packet8us pblend(const Selector<8>& ifPacket, const Packet8us& thenPacket,
3092 const Packet8us& elsePacket) {
3093 Packet8us select = {ifPacket.select[0], ifPacket.select[1], ifPacket.select[2], ifPacket.select[3],
3094 ifPacket.select[4], ifPacket.select[5], ifPacket.select[6], ifPacket.select[7]};
3095 Packet8us mask = reinterpret_cast<Packet8us>(pnegate(reinterpret_cast<Packet8s>(select)));
3096 return vec_sel(elsePacket, thenPacket, mask);
3097}
3098
3099template <>
3100EIGEN_STRONG_INLINE Packet8bf pblend(const Selector<8>& ifPacket, const Packet8bf& thenPacket,
3101 const Packet8bf& elsePacket) {
3102 return pblend<Packet8us>(ifPacket, thenPacket, elsePacket);
3103}
3104
3105template <>
3106EIGEN_STRONG_INLINE Packet16c pblend(const Selector<16>& ifPacket, const Packet16c& thenPacket,
3107 const Packet16c& elsePacket) {
3108 Packet16uc select = {ifPacket.select[0], ifPacket.select[1], ifPacket.select[2], ifPacket.select[3],
3109 ifPacket.select[4], ifPacket.select[5], ifPacket.select[6], ifPacket.select[7],
3110 ifPacket.select[8], ifPacket.select[9], ifPacket.select[10], ifPacket.select[11],
3111 ifPacket.select[12], ifPacket.select[13], ifPacket.select[14], ifPacket.select[15]};
3112
3113 Packet16uc mask = reinterpret_cast<Packet16uc>(pnegate(reinterpret_cast<Packet16c>(select)));
3114 return vec_sel(elsePacket, thenPacket, mask);
3115}
3116
3117template <>
3118EIGEN_STRONG_INLINE Packet16uc pblend(const Selector<16>& ifPacket, const Packet16uc& thenPacket,
3119 const Packet16uc& elsePacket) {
3120 Packet16uc select = {ifPacket.select[0], ifPacket.select[1], ifPacket.select[2], ifPacket.select[3],
3121 ifPacket.select[4], ifPacket.select[5], ifPacket.select[6], ifPacket.select[7],
3122 ifPacket.select[8], ifPacket.select[9], ifPacket.select[10], ifPacket.select[11],
3123 ifPacket.select[12], ifPacket.select[13], ifPacket.select[14], ifPacket.select[15]};
3124
3125 Packet16uc mask = reinterpret_cast<Packet16uc>(pnegate(reinterpret_cast<Packet16c>(select)));
3126 return vec_sel(elsePacket, thenPacket, mask);
3127}
3128
3129//---------- double ----------
3130#ifdef EIGEN_VECTORIZE_VSX
3131typedef __vector double Packet2d;
3132typedef __vector unsigned long long Packet2ul;
3133typedef __vector long long Packet2l;
3134#if EIGEN_COMP_CLANG
3135typedef Packet2ul Packet2bl;
3136#else
3137typedef __vector __bool long Packet2bl;
3138#endif
3139
3140static Packet2l p2l_ZERO = reinterpret_cast<Packet2l>(p4i_ZERO);
3141static Packet2ul p2ul_SIGN = {0x8000000000000000ull, 0x8000000000000000ull};
3142static Packet2ul p2ul_PREV0DOT5 = {0x3FDFFFFFFFFFFFFFull, 0x3FDFFFFFFFFFFFFFull};
3143static Packet2d p2d_ONE = {1.0, 1.0};
3144static Packet2d p2d_ZERO = reinterpret_cast<Packet2d>(p4f_ZERO);
3145static Packet2d p2d_MZERO = {numext::bit_cast<double>(0x8000000000000000ull),
3146 numext::bit_cast<double>(0x8000000000000000ull)};
3147
3148#ifdef _BIG_ENDIAN
3149static Packet2d p2d_COUNTDOWN =
3150 reinterpret_cast<Packet2d>(vec_sld(reinterpret_cast<Packet4f>(p2d_ZERO), reinterpret_cast<Packet4f>(p2d_ONE), 8));
3151#else
3152static Packet2d p2d_COUNTDOWN =
3153 reinterpret_cast<Packet2d>(vec_sld(reinterpret_cast<Packet4f>(p2d_ONE), reinterpret_cast<Packet4f>(p2d_ZERO), 8));
3154#endif
3155
3156template <int index>
3157Packet2d vec_splat_dbl(Packet2d& a) {
3158 return vec_splat(a, index);
3159}
3160
3161template <>
3162struct packet_traits<double> : default_packet_traits {
3163 typedef Packet2d type;
3164 typedef Packet2d half;
3165 enum {
3166 Vectorizable = 1,
3167 AlignedOnScalar = 1,
3168 size = 2,
3169
3170 HasAdd = 1,
3171 HasSub = 1,
3172 HasMul = 1,
3173 HasDiv = 1,
3174 HasMin = 1,
3175 HasMax = 1,
3176 HasAbs = 1,
3177 HasSin = EIGEN_FAST_MATH,
3178 HasCos = EIGEN_FAST_MATH,
3179 HasATan = 0,
3180 HasLog = 0,
3181 HasExp = 1,
3182 HasSqrt = 1,
3183#if !EIGEN_COMP_CLANG
3184 HasRsqrt = 1,
3185#else
3186 HasRsqrt = 0,
3187#endif
3188 HasNegate = 1,
3189 HasBlend = 1
3190 };
3191};
3192
3193template <>
3194struct unpacket_traits<Packet2d> {
3195 typedef double type;
3196 typedef Packet2l integer_packet;
3197 enum {
3198 size = 2,
3199 alignment = Aligned16,
3200 vectorizable = true,
3201 masked_load_available = false,
3202 masked_store_available = false
3203 };
3204 typedef Packet2d half;
3205};
3206template <>
3207struct unpacket_traits<Packet2l> {
3208 typedef int64_t type;
3209 typedef Packet2l half;
3210 enum {
3211 size = 2,
3212 alignment = Aligned16,
3213 vectorizable = false,
3214 masked_load_available = false,
3215 masked_store_available = false
3216 };
3217};
3218
3219inline std::ostream& operator<<(std::ostream& s, const Packet2l& v) {
3220 union {
3221 Packet2l v;
3222 int64_t n[2];
3223 } vt;
3224 vt.v = v;
3225 s << vt.n[0] << ", " << vt.n[1];
3226 return s;
3227}
3228
3229inline std::ostream& operator<<(std::ostream& s, const Packet2d& v) {
3230 union {
3231 Packet2d v;
3232 double n[2];
3233 } vt;
3234 vt.v = v;
3235 s << vt.n[0] << ", " << vt.n[1];
3236 return s;
3237}
3238
3239// Need to define them first or we get specialization after instantiation errors
3240template <>
3241EIGEN_STRONG_INLINE Packet2d pload<Packet2d>(const double* from) {
3242 EIGEN_DEBUG_ALIGNED_LOAD
3243 return vec_xl(0, const_cast<double*>(from)); // cast needed by Clang
3244}
3245
3246template <>
3247EIGEN_ALWAYS_INLINE Packet2d pload_partial<Packet2d>(const double* from, const Index n, const Index offset) {
3248 return pload_partial_common<Packet2d>(from, n, offset);
3249}
3250
3251template <>
3252EIGEN_STRONG_INLINE void pstore<double>(double* to, const Packet2d& from) {
3253 EIGEN_DEBUG_ALIGNED_STORE
3254 vec_xst(from, 0, to);
3255}
3256
3257template <>
3258EIGEN_ALWAYS_INLINE void pstore_partial<double>(double* to, const Packet2d& from, const Index n, const Index offset) {
3259 pstore_partial_common<Packet2d>(to, from, n, offset);
3260}
3261
3262template <>
3263EIGEN_STRONG_INLINE Packet2d pset1<Packet2d>(const double& from) {
3264 Packet2d v = {from, from};
3265 return v;
3266}
3267template <>
3268EIGEN_STRONG_INLINE Packet2l pset1<Packet2l>(const int64_t& from) {
3269 Packet2l v = {from, from};
3270 return v;
3271}
3272
3273template <>
3274EIGEN_STRONG_INLINE Packet2d pset1frombits<Packet2d>(unsigned long from) {
3275 Packet2l v = {static_cast<long long>(from), static_cast<long long>(from)};
3276 return reinterpret_cast<Packet2d>(v);
3277}
3278
3279template <>
3280EIGEN_STRONG_INLINE void pbroadcast4<Packet2d>(const double* a, Packet2d& a0, Packet2d& a1, Packet2d& a2,
3281 Packet2d& a3) {
3282 // This way is faster than vec_splat (at least for doubles in Power 9)
3283 a0 = pset1<Packet2d>(a[0]);
3284 a1 = pset1<Packet2d>(a[1]);
3285 a2 = pset1<Packet2d>(a[2]);
3286 a3 = pset1<Packet2d>(a[3]);
3287}
3288
3289template <>
3290EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Packet2d pgather<double, Packet2d>(const double* from, Index stride) {
3291 return pgather_common<Packet2d>(from, stride);
3292}
3293template <>
3294EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Packet2d pgather_partial<double, Packet2d>(const double* from, Index stride,
3295 const Index n) {
3296 return pgather_common<Packet2d>(from, stride, n);
3297}
3298template <>
3299EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void pscatter<double, Packet2d>(double* to, const Packet2d& from, Index stride) {
3300 pscatter_common<Packet2d>(to, from, stride);
3301}
3302template <>
3303EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void pscatter_partial<double, Packet2d>(double* to, const Packet2d& from,
3304 Index stride, const Index n) {
3305 pscatter_common<Packet2d>(to, from, stride, n);
3306}
3307
3308template <>
3309EIGEN_STRONG_INLINE Packet2d plset<Packet2d>(const double& a) {
3310 return pset1<Packet2d>(a) + p2d_COUNTDOWN;
3311}
3312
3313template <>
3314EIGEN_STRONG_INLINE Packet2d padd<Packet2d>(const Packet2d& a, const Packet2d& b) {
3315 return a + b;
3316}
3317
3318template <>
3319EIGEN_STRONG_INLINE Packet2d psub<Packet2d>(const Packet2d& a, const Packet2d& b) {
3320 return a - b;
3321}
3322
3323template <>
3324EIGEN_STRONG_INLINE Packet2d pnegate(const Packet2d& a) {
3325#ifdef __POWER8_VECTOR__
3326 return vec_neg(a);
3327#else
3328 return vec_xor(a, p2d_MZERO);
3329#endif
3330}
3331
3332template <>
3333EIGEN_STRONG_INLINE Packet2d pconj(const Packet2d& a) {
3334 return a;
3335}
3336
3337template <>
3338EIGEN_STRONG_INLINE Packet2d pmul<Packet2d>(const Packet2d& a, const Packet2d& b) {
3339 return vec_madd(a, b, p2d_MZERO);
3340}
3341template <>
3342EIGEN_STRONG_INLINE Packet2d pdiv<Packet2d>(const Packet2d& a, const Packet2d& b) {
3343 return vec_div(a, b);
3344}
3345
3346// for some weird raisons, it has to be overloaded for packet of integers
3347template <>
3348EIGEN_STRONG_INLINE Packet2d pmadd(const Packet2d& a, const Packet2d& b, const Packet2d& c) {
3349 return vec_madd(a, b, c);
3350}
3351template <>
3352EIGEN_STRONG_INLINE Packet2d pmsub(const Packet2d& a, const Packet2d& b, const Packet2d& c) {
3353 return vec_msub(a, b, c);
3354}
3355template <>
3356EIGEN_STRONG_INLINE Packet2d pnmadd(const Packet2d& a, const Packet2d& b, const Packet2d& c) {
3357 return vec_nmsub(a, b, c);
3358}
3359template <>
3360EIGEN_STRONG_INLINE Packet2d pnmsub(const Packet2d& a, const Packet2d& b, const Packet2d& c) {
3361 return vec_nmadd(a, b, c);
3362}
3363
3364template <>
3365EIGEN_STRONG_INLINE Packet2d pmin<Packet2d>(const Packet2d& a, const Packet2d& b) {
3366 // NOTE: about 10% slower than vec_min, but consistent with std::min and SSE regarding NaN
3367 Packet2d ret;
3368 __asm__("xvcmpgedp %x0,%x1,%x2\n\txxsel %x0,%x1,%x2,%x0" : "=&wa"(ret) : "wa"(a), "wa"(b));
3369 return ret;
3370}
3371
3372template <>
3373EIGEN_STRONG_INLINE Packet2d pmax<Packet2d>(const Packet2d& a, const Packet2d& b) {
3374 // NOTE: about 10% slower than vec_max, but consistent with std::max and SSE regarding NaN
3375 Packet2d ret;
3376 __asm__("xvcmpgtdp %x0,%x2,%x1\n\txxsel %x0,%x1,%x2,%x0" : "=&wa"(ret) : "wa"(a), "wa"(b));
3377 return ret;
3378}
3379
3380template <>
3381EIGEN_STRONG_INLINE Packet2d pcmp_le(const Packet2d& a, const Packet2d& b) {
3382 return reinterpret_cast<Packet2d>(vec_cmple(a, b));
3383}
3384template <>
3385EIGEN_STRONG_INLINE Packet2d pcmp_lt(const Packet2d& a, const Packet2d& b) {
3386 return reinterpret_cast<Packet2d>(vec_cmplt(a, b));
3387}
3388template <>
3389EIGEN_STRONG_INLINE Packet2d pcmp_eq(const Packet2d& a, const Packet2d& b) {
3390 return reinterpret_cast<Packet2d>(vec_cmpeq(a, b));
3391}
3392template <>
3393EIGEN_STRONG_INLINE Packet2l pcmp_eq(const Packet2l& a, const Packet2l& b) {
3394 return reinterpret_cast<Packet2l>(vec_cmpeq(a, b));
3395}
3396template <>
3397EIGEN_STRONG_INLINE Packet2d pcmp_lt_or_nan(const Packet2d& a, const Packet2d& b) {
3398 Packet2d c = reinterpret_cast<Packet2d>(vec_cmpge(a, b));
3399 return vec_nor(c, c);
3400}
3401
3402template <>
3403EIGEN_STRONG_INLINE Packet2d pand<Packet2d>(const Packet2d& a, const Packet2d& b) {
3404 return vec_and(a, b);
3405}
3406
3407template <>
3408EIGEN_STRONG_INLINE Packet2d por<Packet2d>(const Packet2d& a, const Packet2d& b) {
3409 return vec_or(a, b);
3410}
3411
3412template <>
3413EIGEN_STRONG_INLINE Packet2d pxor<Packet2d>(const Packet2d& a, const Packet2d& b) {
3414 return vec_xor(a, b);
3415}
3416
3417template <>
3418EIGEN_STRONG_INLINE Packet2d pandnot<Packet2d>(const Packet2d& a, const Packet2d& b) {
3419 return vec_and(a, vec_nor(b, b));
3420}
3421
3422template <>
3423EIGEN_STRONG_INLINE Packet2d pround<Packet2d>(const Packet2d& a) {
3424 Packet2d t = vec_add(
3425 reinterpret_cast<Packet2d>(vec_or(vec_and(reinterpret_cast<Packet2ul>(a), p2ul_SIGN), p2ul_PREV0DOT5)), a);
3426 Packet2d res;
3427
3428 __asm__("xvrdpiz %x0, %x1\n\t" : "=&wa"(res) : "wa"(t));
3429
3430 return res;
3431}
3432template <>
3433EIGEN_STRONG_INLINE Packet2d pceil<Packet2d>(const Packet2d& a) {
3434 return vec_ceil(a);
3435}
3436template <>
3437EIGEN_STRONG_INLINE Packet2d pfloor<Packet2d>(const Packet2d& a) {
3438 return vec_floor(a);
3439}
3440template <>
3441EIGEN_STRONG_INLINE Packet2d ptrunc<Packet2d>(const Packet2d& a) {
3442 return vec_trunc(a);
3443}
3444template <>
3445EIGEN_STRONG_INLINE Packet2d print<Packet2d>(const Packet2d& a) {
3446 Packet2d res;
3447
3448 __asm__("xvrdpic %x0, %x1\n\t" : "=&wa"(res) : "wa"(a));
3449
3450 return res;
3451}
3452
3453template <>
3454EIGEN_STRONG_INLINE Packet2d ploadu<Packet2d>(const double* from) {
3455 EIGEN_DEBUG_UNALIGNED_LOAD
3456 return vec_xl(0, const_cast<double*>(from));
3457}
3458
3459template <>
3460EIGEN_ALWAYS_INLINE Packet2d ploadu_partial<Packet2d>(const double* from, const Index n, const Index offset) {
3461 return ploadu_partial_common<Packet2d>(from, n, offset);
3462}
3463
3464template <>
3465EIGEN_STRONG_INLINE Packet2d ploaddup<Packet2d>(const double* from) {
3466 Packet2d p;
3467 if ((std::ptrdiff_t(from) % 16) == 0)
3468 p = pload<Packet2d>(from);
3469 else
3470 p = ploadu<Packet2d>(from);
3471 return vec_splat_dbl<0>(p);
3472}
3473
3474template <>
3475EIGEN_STRONG_INLINE void pstoreu<double>(double* to, const Packet2d& from) {
3476 EIGEN_DEBUG_UNALIGNED_STORE
3477 vec_xst(from, 0, to);
3478}
3479
3480template <>
3481EIGEN_ALWAYS_INLINE void pstoreu_partial<double>(double* to, const Packet2d& from, const Index n, const Index offset) {
3482 pstoreu_partial_common<Packet2d>(to, from, n, offset);
3483}
3484
3485template <>
3486EIGEN_STRONG_INLINE void prefetch<double>(const double* addr) {
3487 EIGEN_PPC_PREFETCH(addr);
3488}
3489
3490template <>
3491EIGEN_STRONG_INLINE double pfirst<Packet2d>(const Packet2d& a) {
3492 EIGEN_ALIGN16 double x[2];
3493 pstore<double>(x, a);
3494 return x[0];
3495}
3496
3497template <>
3498EIGEN_STRONG_INLINE Packet2d preverse(const Packet2d& a) {
3499 return vec_sld(a, a, 8);
3500}
3501template <>
3502EIGEN_STRONG_INLINE Packet2d pabs(const Packet2d& a) {
3503 return vec_abs(a);
3504}
3505#ifdef __POWER8_VECTOR__
3506template <>
3507EIGEN_STRONG_INLINE Packet2d psignbit(const Packet2d& a) {
3508 return (Packet2d)vec_sra((Packet2l)a, vec_splats((unsigned long long)(63)));
3509}
3510#else
3511#ifdef _BIG_ENDIAN
3512static Packet16uc p16uc_DUPSIGN = {0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 8};
3513#else
3514static Packet16uc p16uc_DUPSIGN = {7, 7, 7, 7, 7, 7, 7, 7, 15, 15, 15, 15, 15, 15, 15, 15};
3515#endif
3516
3517template <>
3518EIGEN_STRONG_INLINE Packet2d psignbit(const Packet2d& a) {
3519 Packet16c tmp = vec_sra(reinterpret_cast<Packet16c>(a), vec_splats((unsigned char)(7)));
3520 return reinterpret_cast<Packet2d>(vec_perm(tmp, tmp, p16uc_DUPSIGN));
3521}
3522#endif
3523
3524template <>
3525inline Packet2l pcast<Packet2d, Packet2l>(const Packet2d& x);
3526
3527template <>
3528inline Packet2d pcast<Packet2l, Packet2d>(const Packet2l& x);
3529
3530// Packet2l shifts.
3531// For POWER8 we simply use vec_sr/l.
3532//
3533// Things are more complicated for POWER7. There is actually a
3534// vec_xxsxdi intrinsic but it is not supported by some gcc versions.
3535// So we need to shift by N % 32 and rearrage bytes.
3536#ifdef __POWER8_VECTOR__
3537
3538template <int N>
3539EIGEN_STRONG_INLINE Packet2l plogical_shift_left(const Packet2l& a) {
3540 const Packet2ul shift = {N, N};
3541 return vec_sl(a, shift);
3542}
3543
3544template <int N>
3545EIGEN_STRONG_INLINE Packet2l plogical_shift_right(const Packet2l& a) {
3546 const Packet2ul shift = {N, N};
3547 return vec_sr(a, shift);
3548}
3549
3550#else
3551
3552// Shifts [A, B, C, D] to [B, 0, D, 0].
3553// Used to implement left shifts for Packet2l.
3554EIGEN_ALWAYS_INLINE Packet4i shift_even_left(const Packet4i& a) {
3555 static const Packet16uc perm = {0x14, 0x15, 0x16, 0x17, 0x00, 0x01, 0x02, 0x03,
3556 0x1c, 0x1d, 0x1e, 0x1f, 0x08, 0x09, 0x0a, 0x0b};
3557#ifdef _BIG_ENDIAN
3558 return vec_perm(p4i_ZERO, a, perm);
3559#else
3560 return vec_perm(a, p4i_ZERO, perm);
3561#endif
3562}
3563
3564// Shifts [A, B, C, D] to [0, A, 0, C].
3565// Used to implement right shifts for Packet2l.
3566EIGEN_ALWAYS_INLINE Packet4i shift_odd_right(const Packet4i& a) {
3567 static const Packet16uc perm = {0x04, 0x05, 0x06, 0x07, 0x10, 0x11, 0x12, 0x13,
3568 0x0c, 0x0d, 0x0e, 0x0f, 0x18, 0x19, 0x1a, 0x1b};
3569#ifdef _BIG_ENDIAN
3570 return vec_perm(p4i_ZERO, a, perm);
3571#else
3572 return vec_perm(a, p4i_ZERO, perm);
3573#endif
3574}
3575
3576template <int N, typename EnableIf = void>
3577struct plogical_shift_left_impl;
3578
3579template <int N>
3580struct plogical_shift_left_impl<N, std::enable_if_t<(N < 32) && (N >= 0)> > {
3581 static EIGEN_STRONG_INLINE Packet2l run(const Packet2l& a) {
3582 static const unsigned n = static_cast<unsigned>(N);
3583 const Packet4ui shift = {n, n, n, n};
3584 const Packet4i ai = reinterpret_cast<Packet4i>(a);
3585 static const unsigned m = static_cast<unsigned>(32 - N);
3586 const Packet4ui shift_right = {m, m, m, m};
3587 const Packet4i out_hi = vec_sl(ai, shift);
3588 const Packet4i out_lo = shift_even_left(vec_sr(ai, shift_right));
3589 return reinterpret_cast<Packet2l>(por<Packet4i>(out_hi, out_lo));
3590 }
3591};
3592
3593template <int N>
3594struct plogical_shift_left_impl<N, std::enable_if_t<(N >= 32)> > {
3595 static EIGEN_STRONG_INLINE Packet2l run(const Packet2l& a) {
3596 static const unsigned m = static_cast<unsigned>(N - 32);
3597 const Packet4ui shift = {m, m, m, m};
3598 const Packet4i ai = reinterpret_cast<Packet4i>(a);
3599 return reinterpret_cast<Packet2l>(shift_even_left(vec_sl(ai, shift)));
3600 }
3601};
3602
3603template <int N>
3604EIGEN_STRONG_INLINE Packet2l plogical_shift_left(const Packet2l& a) {
3605 return plogical_shift_left_impl<N>::run(a);
3606}
3607
3608template <int N, typename EnableIf = void>
3609struct plogical_shift_right_impl;
3610
3611template <int N>
3612struct plogical_shift_right_impl<N, std::enable_if_t<(N < 32) && (N >= 0)> > {
3613 static EIGEN_STRONG_INLINE Packet2l run(const Packet2l& a) {
3614 static const unsigned n = static_cast<unsigned>(N);
3615 const Packet4ui shift = {n, n, n, n};
3616 const Packet4i ai = reinterpret_cast<Packet4i>(a);
3617 static const unsigned m = static_cast<unsigned>(32 - N);
3618 const Packet4ui shift_left = {m, m, m, m};
3619 const Packet4i out_lo = vec_sr(ai, shift);
3620 const Packet4i out_hi = shift_odd_right(vec_sl(ai, shift_left));
3621 return reinterpret_cast<Packet2l>(por<Packet4i>(out_hi, out_lo));
3622 }
3623};
3624
3625template <int N>
3626struct plogical_shift_right_impl<N, std::enable_if_t<(N >= 32)> > {
3627 static EIGEN_STRONG_INLINE Packet2l run(const Packet2l& a) {
3628 static const unsigned m = static_cast<unsigned>(N - 32);
3629 const Packet4ui shift = {m, m, m, m};
3630 const Packet4i ai = reinterpret_cast<Packet4i>(a);
3631 return reinterpret_cast<Packet2l>(shift_odd_right(vec_sr(ai, shift)));
3632 }
3633};
3634
3635template <int N>
3636EIGEN_STRONG_INLINE Packet2l plogical_shift_right(const Packet2l& a) {
3637 return plogical_shift_right_impl<N>::run(a);
3638}
3639#endif
3640
3641template <>
3642EIGEN_STRONG_INLINE Packet2d pldexp<Packet2d>(const Packet2d& a, const Packet2d& exponent) {
3643 // Clamp exponent to [-2099, 2099]
3644 const Packet2d max_exponent = pset1<Packet2d>(2099.0);
3645 const Packet2l e = pcast<Packet2d, Packet2l>(pmin(pmax(exponent, pnegate(max_exponent)), max_exponent));
3646
3647 // Split 2^e into four factors and multiply:
3648 const Packet2l bias = {1023, 1023};
3649 Packet2l b = plogical_shift_right<2>(e); // floor(e/4)
3650 Packet2d c = reinterpret_cast<Packet2d>(plogical_shift_left<52>(b + bias));
3651 Packet2d out = pmul(pmul(pmul(a, c), c), c); // a * 2^(3b)
3652 b = psub(psub(psub(e, b), b), b); // e - 3b
3653 c = reinterpret_cast<Packet2d>(plogical_shift_left<52>(b + bias)); // 2^(e - 3b)
3654 out = pmul(out, c); // a * 2^e
3655 return out;
3656}
3657
3658// Extract exponent without existence of Packet2l.
3659template <>
3660EIGEN_STRONG_INLINE Packet2d pfrexp_generic_get_biased_exponent(const Packet2d& a) {
3661 return pcast<Packet2l, Packet2d>(plogical_shift_right<52>(reinterpret_cast<Packet2l>(pabs(a))));
3662}
3663
3664template <>
3665EIGEN_STRONG_INLINE Packet2d pfrexp<Packet2d>(const Packet2d& a, Packet2d& exponent) {
3666 return pfrexp_generic(a, exponent);
3667}
3668
3669template <>
3670EIGEN_STRONG_INLINE double predux<Packet2d>(const Packet2d& a) {
3671 Packet2d b, sum;
3672 b = reinterpret_cast<Packet2d>(vec_sld(reinterpret_cast<Packet4f>(a), reinterpret_cast<Packet4f>(a), 8));
3673 sum = a + b;
3674 return pfirst<Packet2d>(sum);
3675}
3676
3677// Other reduction functions:
3678// mul
3679template <>
3680EIGEN_STRONG_INLINE double predux_mul<Packet2d>(const Packet2d& a) {
3681 return pfirst(
3682 pmul(a, reinterpret_cast<Packet2d>(vec_sld(reinterpret_cast<Packet4ui>(a), reinterpret_cast<Packet4ui>(a), 8))));
3683}
3684
3685// min
3686template <>
3687EIGEN_STRONG_INLINE double predux_min<Packet2d>(const Packet2d& a) {
3688 return pfirst(
3689 pmin(a, reinterpret_cast<Packet2d>(vec_sld(reinterpret_cast<Packet4ui>(a), reinterpret_cast<Packet4ui>(a), 8))));
3690}
3691
3692// max
3693template <>
3694EIGEN_STRONG_INLINE double predux_max<Packet2d>(const Packet2d& a) {
3695 return pfirst(
3696 pmax(a, reinterpret_cast<Packet2d>(vec_sld(reinterpret_cast<Packet4ui>(a), reinterpret_cast<Packet4ui>(a), 8))));
3697}
3698
3699EIGEN_DEVICE_FUNC inline void ptranspose(PacketBlock<Packet2d, 2>& kernel) {
3700 Packet2d t0, t1;
3701 t0 = vec_mergeh(kernel.packet[0], kernel.packet[1]);
3702 t1 = vec_mergel(kernel.packet[0], kernel.packet[1]);
3703 kernel.packet[0] = t0;
3704 kernel.packet[1] = t1;
3705}
3706
3707template <>
3708EIGEN_STRONG_INLINE Packet2d pblend(const Selector<2>& ifPacket, const Packet2d& thenPacket,
3709 const Packet2d& elsePacket) {
3710 Packet2l select = {ifPacket.select[0], ifPacket.select[1]};
3711 Packet2ul mask = reinterpret_cast<Packet2ul>(pnegate(reinterpret_cast<Packet2l>(select)));
3712 return vec_sel(elsePacket, thenPacket, mask);
3713}
3714
3715#endif // __VSX__
3716} // end namespace internal
3717
3718} // end namespace Eigen
3719
3720#endif // EIGEN_PACKET_MATH_ALTIVEC_H
@ Aligned16
Definition Constants.h:237
Namespace containing all symbols from the Eigen library.
Definition Core:137
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_exp_op< typename Derived::Scalar >, const Derived > exp(const Eigen::ArrayBase< Derived > &x)