Please, help us to better know about our user community by answering the following short survey: https://forms.gle/wpyrxWi18ox9Z5ae9
Eigen  3.4.0
 
Loading...
Searching...
No Matches
Default/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)
21 typedef Eigen::half result_type;
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<>
33struct functor_traits<scalar_cast_op<float, Eigen::half> >
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)
40 typedef Eigen::half result_type;
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<>
52struct functor_traits<scalar_cast_op<int, Eigen::half> >
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<>
71struct functor_traits<scalar_cast_op<Eigen::half, float> >
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)
78 typedef Eigen::bfloat16 result_type;
79 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Eigen::bfloat16 operator() (const float& a) const {
80 return Eigen::bfloat16(a);
81 }
82};
83
84template<>
85struct functor_traits<scalar_cast_op<float, Eigen::bfloat16> >
86{ enum { Cost = NumTraits<float>::AddCost, PacketAccess = false }; };
87
88
89template<>
90struct scalar_cast_op<int, Eigen::bfloat16> {
91 EIGEN_EMPTY_STRUCT_CTOR(scalar_cast_op)
92 typedef Eigen::bfloat16 result_type;
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<>
99struct functor_traits<scalar_cast_op<int, Eigen::bfloat16> >
100{ enum { Cost = NumTraits<float>::AddCost, PacketAccess = false }; };
101
102
103template<>
104struct scalar_cast_op<Eigen::bfloat16, float> {
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<>
113struct functor_traits<scalar_cast_op<Eigen::bfloat16, float> >
114{ enum { Cost = NumTraits<float>::AddCost, PacketAccess = false }; };
115
116
117}
118}
119
120#endif // EIGEN_GENERIC_TYPE_CASTING_H
Namespace containing all symbols from the Eigen library.
Definition: Core:141