From c1ffe452fc5b43d7c90fae57b8913497d6c5da0e Mon Sep 17 00:00:00 2001 From: David Tellenbach Date: Thu, 23 Jul 2020 20:55:06 +0000 Subject: Fix bfloat16 casts If we have explicit conversion operators available (C++11) we define explicit casts from bfloat16 to other types. If not (C++03), we don't define conversion operators but rely on implicit conversion chains from bfloat16 over float to other types. --- Eigen/src/Core/arch/Default/BFloat16.h | 75 +++++----------------------------- 1 file changed, 10 insertions(+), 65 deletions(-) (limited to 'Eigen/src') diff --git a/Eigen/src/Core/arch/Default/BFloat16.h b/Eigen/src/Core/arch/Default/BFloat16.h index 30c998249..c963ece6c 100644 --- a/Eigen/src/Core/arch/Default/BFloat16.h +++ b/Eigen/src/Core/arch/Default/BFloat16.h @@ -13,16 +13,9 @@ See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ - #ifndef EIGEN_BFLOAT16_H #define EIGEN_BFLOAT16_H -#if __cplusplus > 199711L -#define EIGEN_EXPLICIT_CAST(tgt_type) explicit operator tgt_type() -#else -#define EIGEN_EXPLICIT_CAST(tgt_type) operator tgt_type() -#endif - #define BF16_PACKET_FUNCTION(PACKET_F, PACKET_BF16, METHOD) \ template <> \ EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED \ @@ -34,20 +27,6 @@ namespace Eigen { struct bfloat16; -// explicit conversion operators are no available before C++11 so we first cast -// bfloat16 to RealScalar rather than to std::complex directly -#if !EIGEN_HAS_CXX11 -namespace internal { -template -struct cast_impl > { - EIGEN_DEVICE_FUNC static inline std::complex run(const bfloat16 &x) - { - return static_cast >(static_cast(x)); - } -}; -} // namespace internal -#endif // EIGEN_HAS_CXX11 - namespace bfloat16_impl { // Make our own __bfloat16_raw definition. @@ -86,66 +65,32 @@ struct bfloat16 : public bfloat16_impl::bfloat16_base { explicit EIGEN_DEVICE_FUNC bfloat16(bool b) : bfloat16_impl::bfloat16_base(bfloat16_impl::raw_uint16_to_bfloat16(b ? 0x3f80 : 0)) {} + template explicit EIGEN_DEVICE_FUNC bfloat16(const T& val) : bfloat16_impl::bfloat16_base(bfloat16_impl::float_to_bfloat16_rtne::value>(static_cast(val))) {} + explicit EIGEN_DEVICE_FUNC bfloat16(float f) : bfloat16_impl::bfloat16_base(bfloat16_impl::float_to_bfloat16_rtne(f)) {} + // Following the convention of numpy, converting between complex and // float will lead to loss of imag value. template explicit EIGEN_DEVICE_FUNC bfloat16(const std::complex& val) : bfloat16_impl::bfloat16_base(bfloat16_impl::float_to_bfloat16_rtne(static_cast(val.real()))) {} + EIGEN_DEVICE_FUNC operator float() const { + return bfloat16_impl::bfloat16_to_float(*this); + } + +#if EIGEN_HAS_CXX11 EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(bool) const { // +0.0 and -0.0 become false, everything else becomes true. return (value & 0x7fff) != 0; } - EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(signed char) const { - return static_cast(bfloat16_impl::bfloat16_to_float(*this)); - } - EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(unsigned char) const { - return static_cast(bfloat16_impl::bfloat16_to_float(*this)); - } - EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(short) const { - return static_cast(bfloat16_impl::bfloat16_to_float(*this)); - } - EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(unsigned short) const { - return static_cast(bfloat16_impl::bfloat16_to_float(*this)); - } - EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(int) const { - return static_cast(bfloat16_impl::bfloat16_to_float(*this)); - } - EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(unsigned int) const { - return static_cast(bfloat16_impl::bfloat16_to_float(*this)); - } - EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(long) const { - return static_cast(bfloat16_impl::bfloat16_to_float(*this)); - } - EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(unsigned long) const { - return static_cast(bfloat16_impl::bfloat16_to_float(*this)); - } - EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(long long) const { - return static_cast(bfloat16_impl::bfloat16_to_float(*this)); - } - EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(unsigned long long) const { - return static_cast(bfloat16_to_float(*this)); - } - EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(float) const { - return bfloat16_impl::bfloat16_to_float(*this); - } - EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(double) const { - return static_cast(bfloat16_impl::bfloat16_to_float(*this)); - } - template - EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(std::complex) const { - return std::complex(static_cast(bfloat16_impl::bfloat16_to_float(*this)), RealScalar(0)); - } - EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(Eigen::half) const { - return static_cast(bfloat16_impl::bfloat16_to_float(*this)); - } -}; +#endif +}; } // end namespace Eigen namespace std { -- cgit v1.2.3