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) 2015 Benoit Steiner <benoit.steiner.goog@gmail.com>
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_TYPE_CASTING_SSE_H
12#define EIGEN_TYPE_CASTING_SSE_H
13
14// IWYU pragma: private
15#include "../../InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21#ifndef EIGEN_VECTORIZE_AVX
22template <>
23struct type_casting_traits<float, bool> : vectorized_type_casting_traits<float, bool> {};
24template <>
25struct type_casting_traits<bool, float> : vectorized_type_casting_traits<bool, float> {};
26
27template <>
28struct type_casting_traits<float, int> : vectorized_type_casting_traits<float, int> {};
29template <>
30struct type_casting_traits<int, float> : vectorized_type_casting_traits<int, float> {};
31
32template <>
33struct type_casting_traits<float, double> : vectorized_type_casting_traits<float, double> {};
34template <>
35struct type_casting_traits<double, float> : vectorized_type_casting_traits<double, float> {};
36
37template <>
38struct type_casting_traits<double, int> : vectorized_type_casting_traits<double, int> {};
39template <>
40struct type_casting_traits<int, double> : vectorized_type_casting_traits<int, double> {};
41
42#ifndef EIGEN_VECTORIZE_AVX2
43template <>
44struct type_casting_traits<double, int64_t> : vectorized_type_casting_traits<double, int64_t> {};
45template <>
46struct type_casting_traits<int64_t, double> : vectorized_type_casting_traits<int64_t, double> {};
47#endif
48#endif
49
50template <>
51EIGEN_STRONG_INLINE Packet16b pcast<Packet4f, Packet16b>(const Packet4f& a, const Packet4f& b, const Packet4f& c,
52 const Packet4f& d) {
53 __m128 zero = pzero(a);
54 __m128 nonzero_a = _mm_cmpneq_ps(a, zero);
55 __m128 nonzero_b = _mm_cmpneq_ps(b, zero);
56 __m128 nonzero_c = _mm_cmpneq_ps(c, zero);
57 __m128 nonzero_d = _mm_cmpneq_ps(d, zero);
58 __m128i ab_bytes = _mm_packs_epi32(_mm_castps_si128(nonzero_a), _mm_castps_si128(nonzero_b));
59 __m128i cd_bytes = _mm_packs_epi32(_mm_castps_si128(nonzero_c), _mm_castps_si128(nonzero_d));
60 __m128i merged = _mm_packs_epi16(ab_bytes, cd_bytes);
61 return _mm_and_si128(merged, _mm_set1_epi8(1));
62}
63
64template <>
65EIGEN_STRONG_INLINE Packet4f pcast<Packet16b, Packet4f>(const Packet16b& a) {
66 const __m128 cst_one = _mm_set_ps1(1.0f);
67#ifdef EIGEN_VECTORIZE_SSE4_1
68 __m128i a_extended = _mm_cvtepi8_epi32(a);
69 __m128i abcd = _mm_cmpeq_epi32(a_extended, _mm_setzero_si128());
70#else
71 __m128i abcd_efhg_ijkl_mnop = _mm_cmpeq_epi8(a, _mm_setzero_si128());
72 __m128i aabb_ccdd_eeff_gghh = _mm_unpacklo_epi8(abcd_efhg_ijkl_mnop, abcd_efhg_ijkl_mnop);
73 __m128i abcd = _mm_unpacklo_epi8(aabb_ccdd_eeff_gghh, aabb_ccdd_eeff_gghh);
74#endif
75 __m128 result = _mm_andnot_ps(_mm_castsi128_ps(abcd), cst_one);
76 return result;
77}
78
79template <>
80EIGEN_STRONG_INLINE Packet4i pcast<Packet4f, Packet4i>(const Packet4f& a) {
81 return _mm_cvttps_epi32(a);
82}
83
84template <>
85EIGEN_STRONG_INLINE Packet4i pcast<Packet2d, Packet4i>(const Packet2d& a, const Packet2d& b) {
86 return _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(_mm_cvttpd_epi32(a)), _mm_castsi128_ps(_mm_cvttpd_epi32(b)),
87 (1 << 2) | (1 << 6)));
88}
89
90template <>
91EIGEN_STRONG_INLINE Packet2l pcast<Packet2d, Packet2l>(const Packet2d& a) {
92#if EIGEN_ARCH_x86_64
93 return _mm_set_epi64x(_mm_cvttsd_si64(preverse(a)), _mm_cvttsd_si64(a));
94#else
95 return _mm_set_epi64x(static_cast<int64_t>(pfirst(preverse(a))), static_cast<int64_t>(pfirst(a)));
96#endif
97}
98
99template <>
100EIGEN_STRONG_INLINE Packet2d pcast<Packet2l, Packet2d>(const Packet2l& a) {
101 EIGEN_ALIGN16 int64_t aux[2];
102 pstore(aux, a);
103 return _mm_set_pd(static_cast<double>(aux[1]), static_cast<double>(aux[0]));
104}
105
106template <>
107EIGEN_STRONG_INLINE Packet4f pcast<Packet4i, Packet4f>(const Packet4i& a) {
108 return _mm_cvtepi32_ps(a);
109}
110
111template <>
112EIGEN_STRONG_INLINE Packet4f pcast<Packet2d, Packet4f>(const Packet2d& a, const Packet2d& b) {
113 return _mm_shuffle_ps(_mm_cvtpd_ps(a), _mm_cvtpd_ps(b), (1 << 2) | (1 << 6));
114}
115
116template <>
117EIGEN_STRONG_INLINE Packet2d pcast<Packet4i, Packet2d>(const Packet4i& a) {
118 // Simply discard the second half of the input
119 return _mm_cvtepi32_pd(a);
120}
121
122template <>
123EIGEN_STRONG_INLINE Packet2d pcast<Packet4f, Packet2d>(const Packet4f& a) {
124 // Simply discard the second half of the input
125 return _mm_cvtps_pd(a);
126}
127
128template <>
129EIGEN_STRONG_INLINE Packet2d preinterpret<Packet2d, Packet4f>(const Packet4f& a) {
130 return _mm_castps_pd(a);
131}
132
133template <>
134EIGEN_STRONG_INLINE Packet4f preinterpret<Packet4f, Packet2d>(const Packet2d& a) {
135 return _mm_castpd_ps(a);
136}
137
138template <>
139EIGEN_STRONG_INLINE Packet4i preinterpret<Packet4i, Packet4f>(const Packet4f& a) {
140 return _mm_castps_si128(a);
141}
142
143template <>
144EIGEN_STRONG_INLINE Packet4f preinterpret<Packet4f, Packet4i>(const Packet4i& a) {
145 return _mm_castsi128_ps(a);
146}
147
148template <>
149EIGEN_STRONG_INLINE Packet2d preinterpret<Packet2d, Packet4i>(const Packet4i& a) {
150 return _mm_castsi128_pd(a);
151}
152
153template <>
154EIGEN_STRONG_INLINE Packet2d preinterpret<Packet2d, Packet2l>(const Packet2l& a) {
155 return _mm_castsi128_pd(a);
156}
157template <>
158EIGEN_STRONG_INLINE Packet2l preinterpret<Packet2l, Packet2d>(const Packet2d& a) {
159 return _mm_castpd_si128(a);
160}
161
162template <>
163EIGEN_STRONG_INLINE Packet4i preinterpret<Packet4i, Packet2d>(const Packet2d& a) {
164 return _mm_castpd_si128(a);
165}
166
167template <>
168EIGEN_STRONG_INLINE Packet4ui preinterpret<Packet4ui, Packet4i>(const Packet4i& a) {
169 return Packet4ui(a);
170}
171
172template <>
173EIGEN_STRONG_INLINE Packet4i preinterpret<Packet4i, Packet4ui>(const Packet4ui& a) {
174 return Packet4i(a);
175}
176
177} // end namespace internal
178
179} // end namespace Eigen
180
181#endif // EIGEN_TYPE_CASTING_SSE_H