$darkmode
Eigen  5.0.1-dev
TypeCastingFP16.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2025 The Eigen Authors.
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_TYPE_CASTING_FP16_AVX512_H
11 #define EIGEN_TYPE_CASTING_FP16_AVX512_H
12 
13 // IWYU pragma: private
14 #include "../../InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 namespace internal {
18 
19 template <>
20 EIGEN_STRONG_INLINE Packet32s preinterpret<Packet32s, Packet32h>(const Packet32h& a) {
21  return _mm512_castph_si512(a);
22 }
23 template <>
24 EIGEN_STRONG_INLINE Packet16s preinterpret<Packet16s, Packet16h>(const Packet16h& a) {
25  return _mm256_castph_si256(a);
26 }
27 template <>
28 EIGEN_STRONG_INLINE Packet8s preinterpret<Packet8s, Packet8h>(const Packet8h& a) {
29  return _mm_castph_si128(a);
30 }
31 
32 template <>
33 EIGEN_STRONG_INLINE Packet32h preinterpret<Packet32h, Packet32s>(const Packet32s& a) {
34  return _mm512_castsi512_ph(a);
35 }
36 template <>
37 EIGEN_STRONG_INLINE Packet16h preinterpret<Packet16h, Packet16s>(const Packet16s& a) {
38  return _mm256_castsi256_ph(a);
39 }
40 template <>
41 EIGEN_STRONG_INLINE Packet8h preinterpret<Packet8h, Packet8s>(const Packet8s& a) {
42  return _mm_castsi128_ph(a);
43 }
44 
45 template <>
46 EIGEN_STRONG_INLINE Packet16f pcast<Packet16h, Packet16f>(const Packet16h& a) {
47  return half2float(a);
48 }
49 template <>
50 EIGEN_STRONG_INLINE Packet8f pcast<Packet8h, Packet8f>(const Packet8h& a) {
51  return half2float(a);
52 }
53 
54 template <>
55 EIGEN_STRONG_INLINE Packet16h pcast<Packet16f, Packet16h>(const Packet16f& a) {
56  return float2half(a);
57 }
58 template <>
59 EIGEN_STRONG_INLINE Packet8h pcast<Packet8f, Packet8h>(const Packet8f& a) {
60  return float2half(a);
61 }
62 
63 template <>
64 EIGEN_STRONG_INLINE Packet16f pcast<Packet32h, Packet16f>(const Packet32h& a) {
65  // Discard second-half of input.
66  Packet16h low = _mm256_castpd_ph(_mm512_extractf64x4_pd(_mm512_castph_pd(a), 0));
67  return _mm512_cvtxph_ps(low);
68 }
69 template <>
70 EIGEN_STRONG_INLINE Packet8f pcast<Packet16h, Packet8f>(const Packet16h& a) {
71  // Discard second-half of input.
72  Packet8h low = _mm_castps_ph(_mm256_extractf32x4_ps(_mm256_castph_ps(a), 0));
73  return _mm256_cvtxph_ps(low);
74 }
75 template <>
76 EIGEN_STRONG_INLINE Packet4f pcast<Packet8h, Packet4f>(const Packet8h& a) {
77  Packet8f full = _mm256_cvtxph_ps(a);
78  // Discard second-half of input.
79  return _mm256_extractf32x4_ps(full, 0);
80 }
81 
82 template <>
83 EIGEN_STRONG_INLINE Packet32h pcast<Packet16f, Packet32h>(const Packet16f& a, const Packet16f& b) {
84  __m512 result = _mm512_castsi512_ps(_mm512_castsi256_si512(_mm256_castph_si256(_mm512_cvtxps_ph(a))));
85  result = _mm512_insertf32x8(result, _mm256_castph_ps(_mm512_cvtxps_ph(b)), 1);
86  return _mm512_castps_ph(result);
87 }
88 template <>
89 EIGEN_STRONG_INLINE Packet16h pcast<Packet8f, Packet16h>(const Packet8f& a, const Packet8f& b) {
90  __m256 result = _mm256_castsi256_ps(_mm256_castsi128_si256(_mm_castph_si128(_mm256_cvtxps_ph(a))));
91  result = _mm256_insertf32x4(result, _mm_castph_ps(_mm256_cvtxps_ph(b)), 1);
92  return _mm256_castps_ph(result);
93 }
94 template <>
95 EIGEN_STRONG_INLINE Packet8h pcast<Packet4f, Packet8h>(const Packet4f& a, const Packet4f& b) {
96  __m256 result = _mm256_castsi256_ps(_mm256_castsi128_si256(_mm_castps_si128(a)));
97  result = _mm256_insertf128_ps(result, b, 1);
98  return _mm256_cvtxps_ph(result);
99 }
100 
101 template <>
102 EIGEN_STRONG_INLINE Packet32s pcast<Packet32h, Packet32s>(const Packet32h& a) {
103  return _mm512_cvtph_epi16(a);
104 }
105 template <>
106 EIGEN_STRONG_INLINE Packet16s pcast<Packet16h, Packet16s>(const Packet16h& a) {
107  return _mm256_cvtph_epi16(a);
108 }
109 template <>
110 EIGEN_STRONG_INLINE Packet8s pcast<Packet8h, Packet8s>(const Packet8h& a) {
111  return _mm_cvtph_epi16(a);
112 }
113 
114 template <>
115 EIGEN_STRONG_INLINE Packet32h pcast<Packet32s, Packet32h>(const Packet32s& a) {
116  return _mm512_cvtepi16_ph(a);
117 }
118 template <>
119 EIGEN_STRONG_INLINE Packet16h pcast<Packet16s, Packet16h>(const Packet16s& a) {
120  return _mm256_cvtepi16_ph(a);
121 }
122 template <>
123 EIGEN_STRONG_INLINE Packet8h pcast<Packet8s, Packet8h>(const Packet8s& a) {
124  return _mm_cvtepi16_ph(a);
125 }
126 
127 } // namespace internal
128 } // namespace Eigen
129 
130 #endif // EIGEN_TYPE_CASTING_FP16_AVX512_H
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1