Eigen  5.0.1
 
Loading...
Searching...
No Matches
TypeCasting.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2019 Rasmus Munk Larsen <rmlarsen@gmail.com>
5// Copyright (C) 2023 Chip Kerchner (chip.kerchner@ibm.com)
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_TYPE_CASTING_ALTIVEC_H
13#define EIGEN_TYPE_CASTING_ALTIVEC_H
14
15// IWYU pragma: private
16#include "../../InternalHeaderCheck.h"
17
18namespace Eigen {
19
20namespace internal {
21template <>
22struct type_casting_traits<float, int> {
23 enum { VectorizedCast = 1, SrcCoeffRatio = 1, TgtCoeffRatio = 1 };
24};
25
26template <>
27struct type_casting_traits<int, float> {
28 enum { VectorizedCast = 1, SrcCoeffRatio = 1, TgtCoeffRatio = 1 };
29};
30
31template <>
32struct type_casting_traits<bfloat16, unsigned short int> {
33 enum { VectorizedCast = 1, SrcCoeffRatio = 1, TgtCoeffRatio = 1 };
34};
35
36template <>
37struct type_casting_traits<unsigned short int, bfloat16> {
38 enum { VectorizedCast = 1, SrcCoeffRatio = 1, TgtCoeffRatio = 1 };
39};
40
41template <>
42EIGEN_STRONG_INLINE Packet4i pcast<Packet4f, Packet4i>(const Packet4f& a) {
43 return vec_cts(a, 0);
44}
45
46template <>
47EIGEN_STRONG_INLINE Packet4ui pcast<Packet4f, Packet4ui>(const Packet4f& a) {
48 return vec_ctu(a, 0);
49}
50
51template <>
52EIGEN_STRONG_INLINE Packet4f pcast<Packet4i, Packet4f>(const Packet4i& a) {
53 return vec_ctf(a, 0);
54}
55
56template <>
57EIGEN_STRONG_INLINE Packet4f pcast<Packet4ui, Packet4f>(const Packet4ui& a) {
58 return vec_ctf(a, 0);
59}
60
61template <>
62EIGEN_STRONG_INLINE Packet8us pcast<Packet8bf, Packet8us>(const Packet8bf& a) {
63 Packet4f float_even = Bf16ToF32Even(a);
64 Packet4f float_odd = Bf16ToF32Odd(a);
65 Packet4ui int_even = pcast<Packet4f, Packet4ui>(float_even);
66 Packet4ui int_odd = pcast<Packet4f, Packet4ui>(float_odd);
67 const EIGEN_DECLARE_CONST_FAST_Packet4ui(low_mask, 0x0000FFFF);
68 Packet4ui low_even = pand<Packet4ui>(int_even, p4ui_low_mask);
69 Packet4ui low_odd = pand<Packet4ui>(int_odd, p4ui_low_mask);
70
71 // Check values that are bigger than USHRT_MAX (0xFFFF)
72 Packet4bi overflow_selector;
73 if (vec_any_gt(int_even, p4ui_low_mask)) {
74 overflow_selector = vec_cmpgt(int_even, p4ui_low_mask);
75 low_even = vec_sel(low_even, p4ui_low_mask, overflow_selector);
76 }
77 if (vec_any_gt(int_odd, p4ui_low_mask)) {
78 overflow_selector = vec_cmpgt(int_odd, p4ui_low_mask);
79 low_odd = vec_sel(low_even, p4ui_low_mask, overflow_selector);
80 }
81
82 return pmerge(low_even, low_odd);
83}
84
85template <>
86EIGEN_STRONG_INLINE Packet8bf pcast<Packet8us, Packet8bf>(const Packet8us& a) {
87 // short -> int -> float -> bfloat16
88 const EIGEN_DECLARE_CONST_FAST_Packet4ui(low_mask, 0x0000FFFF);
89 Packet4ui int_cast = reinterpret_cast<Packet4ui>(a);
90 Packet4ui int_even = pand<Packet4ui>(int_cast, p4ui_low_mask);
91 Packet4ui int_odd = plogical_shift_right<16>(int_cast);
92 Packet4f float_even = pcast<Packet4ui, Packet4f>(int_even);
93 Packet4f float_odd = pcast<Packet4ui, Packet4f>(int_odd);
94 return F32ToBf16(float_even, float_odd);
95}
96
97template <>
98struct type_casting_traits<bfloat16, float> {
99 enum { VectorizedCast = 1, SrcCoeffRatio = 1, TgtCoeffRatio = 2 };
100};
101
102template <>
103EIGEN_STRONG_INLINE Packet4f pcast<Packet8bf, Packet4f>(const Packet8bf& a) {
104 Packet8us z = pset1<Packet8us>(0);
105#ifdef _BIG_ENDIAN
106 return reinterpret_cast<Packet4f>(vec_mergeh(a.m_val, z));
107#else
108 return reinterpret_cast<Packet4f>(vec_mergeh(z, a.m_val));
109#endif
110}
111
112template <>
113struct type_casting_traits<float, bfloat16> {
114 enum { VectorizedCast = 1, SrcCoeffRatio = 2, TgtCoeffRatio = 1 };
115};
116
117template <>
118EIGEN_STRONG_INLINE Packet8bf pcast<Packet4f, Packet8bf>(const Packet4f& a, const Packet4f& b) {
119 return F32ToBf16Both(a, b);
120}
121
122template <>
123EIGEN_STRONG_INLINE Packet4i preinterpret<Packet4i, Packet4f>(const Packet4f& a) {
124 return reinterpret_cast<Packet4i>(a);
125}
126
127template <>
128EIGEN_STRONG_INLINE Packet4f preinterpret<Packet4f, Packet4i>(const Packet4i& a) {
129 return reinterpret_cast<Packet4f>(a);
130}
131
132#ifdef EIGEN_VECTORIZE_VSX
133template <>
134inline Packet2l pcast<Packet2d, Packet2l>(const Packet2d& x) {
135 EIGEN_ALIGN_TO_BOUNDARY(unpacket_traits<Packet2d>::alignment) double dtmp[2];
136 pstore(dtmp, x);
137 EIGEN_ALIGN_MAX long long itmp[2] = {static_cast<long long>(dtmp[0]), static_cast<long long>(dtmp[1])};
138 return vec_xl(0, itmp);
139}
140
141template <>
142inline Packet2d pcast<Packet2l, Packet2d>(const Packet2l& x) {
143 EIGEN_ALIGN_MAX long long itmp[2];
144 vec_xst(x, 0, itmp);
145 EIGEN_ALIGN_TO_BOUNDARY(unpacket_traits<Packet2d>::alignment)
146 double dtmp[2] = {static_cast<double>(itmp[0]), static_cast<double>(itmp[1])};
147 return pload<Packet2d>(dtmp);
148}
149#endif
150
151} // end namespace internal
152
153} // end namespace Eigen
154
155#endif // EIGEN_TYPE_CASTING_ALTIVEC_H