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) 2016 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_GPU_H
12#define EIGEN_TYPE_CASTING_GPU_H
13
14// IWYU pragma: private
15#include "../../InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21#if defined(EIGEN_GPU_COMPILE_PHASE)
22
23// Both ratios follow from the packet sizes (8 halves, 4 floats), which is exactly what
24// vectorized_type_casting_traits computes; restating them by hand let them drift from the packets.
25template <>
26struct type_casting_traits<Eigen::half, float> : vectorized_type_casting_traits<Eigen::half, float> {};
27
28template <>
29struct type_casting_traits<float, Eigen::half> : vectorized_type_casting_traits<float, Eigen::half> {};
30
31// Widening: one Packet4h2 covers two float4, so the evaluator calls this once per output packet with the same
32// source and takes the first four lanes, then the second four. Narrowing: two float4 make one Packet4h2.
33// CoreEvaluators.h loads a source segment of DstPacketSize elements for TgtCoeffRatio == 2 and TensorConversion.h
34// does the same, so the one-argument form returning the low half is the protocol, not a truncation bug.
35template <>
36EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE float4 pcast<Packet4h2, float4>(const Packet4h2& a) {
37 const float2 low = __half22float2(lane_half2(a, 0));
38 const float2 high = __half22float2(lane_half2(a, 1));
39 return make_float4(low.x, low.y, high.x, high.y);
40}
41
42template <>
43EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet4h2 pcast<float4, Packet4h2>(const float4& a, const float4& b) {
44 return make_packet4h2(__floats2half2_rn(a.x, a.y), __floats2half2_rn(a.z, a.w), __floats2half2_rn(b.x, b.y),
45 __floats2half2_rn(b.z, b.w));
46}
47
48#endif
49
50} // end namespace internal
51
52} // end namespace Eigen
53
54#endif // EIGEN_TYPE_CASTING_GPU_H