Medial Code Documentation
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// Copyright (C) 2019 Rasmus Munk Larsen <rmlarsen@google.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
11#ifndef EIGEN_GENERIC_TYPE_CASTING_H
12#define EIGEN_GENERIC_TYPE_CASTING_H
13
14namespace Eigen {
15
16namespace internal {
17
18template<>
19struct scalar_cast_op<float, Eigen::half> {
20 EIGEN_EMPTY_STRUCT_CTOR(scalar_cast_op)
22 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Eigen::half operator() (const float& a) const {
23 #if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
24 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
25 return __float2half(a);
26 #else
27 return Eigen::half(a);
28 #endif
29 }
30};
31
32template<>
34{ enum { Cost = NumTraits<float>::AddCost, PacketAccess = false }; };
35
36
37template<>
38struct scalar_cast_op<int, Eigen::half> {
39 EIGEN_EMPTY_STRUCT_CTOR(scalar_cast_op)
41 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Eigen::half operator() (const int& a) const {
42 #if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
43 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
44 return __float2half(static_cast<float>(a));
45 #else
46 return Eigen::half(static_cast<float>(a));
47 #endif
48 }
49};
50
51template<>
53{ enum { Cost = NumTraits<float>::AddCost, PacketAccess = false }; };
54
55
56template<>
57struct scalar_cast_op<Eigen::half, float> {
58 EIGEN_EMPTY_STRUCT_CTOR(scalar_cast_op)
59 typedef float result_type;
60 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE float operator() (const Eigen::half& a) const {
61 #if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
62 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
63 return __half2float(a);
64 #else
65 return static_cast<float>(a);
66 #endif
67 }
68};
69
70template<>
72{ enum { Cost = NumTraits<float>::AddCost, PacketAccess = false }; };
73
74
75template<>
76struct scalar_cast_op<float, Eigen::bfloat16> {
77 EIGEN_EMPTY_STRUCT_CTOR(scalar_cast_op)
79 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Eigen::bfloat16 operator() (const float& a) const {
80 return Eigen::bfloat16(a);
81 }
82};
83
84template<>
86{ enum { Cost = NumTraits<float>::AddCost, PacketAccess = false }; };
87
88
89template<>
91 EIGEN_EMPTY_STRUCT_CTOR(scalar_cast_op)
93 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Eigen::bfloat16 operator() (const int& a) const {
94 return Eigen::bfloat16(static_cast<float>(a));
95 }
96};
97
98template<>
100{ enum { Cost = NumTraits<float>::AddCost, PacketAccess = false }; };
101
102
103template<>
105 EIGEN_EMPTY_STRUCT_CTOR(scalar_cast_op)
106 typedef float result_type;
107 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE float operator() (const Eigen::bfloat16& a) const {
108 return static_cast<float>(a);
109 }
110};
111
112template<>
114{ enum { Cost = NumTraits<float>::AddCost, PacketAccess = false }; };
115
116
117}
118}
119
120#endif // EIGEN_GENERIC_TYPE_CASTING_H
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
Namespace containing all symbols from the Eigen library.
Definition LDLT.h:16
Holds information about the various numeric (i.e.
Definition NumTraits.h:236
Definition BFloat16.h:58
Definition Half.h:140
Definition XprHelper.h:176
Definition UnaryFunctors.h:160