<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
eigen_half.hpp
Go to the documentation of this file.
1
3//
4// Copyright (c) 2021-2023, University of Colorado Denver. All rights reserved.
5//
6// This file is part of <T>LAPACK.
7// <T>LAPACK is free software: you can redistribute it and/or modify it under
8// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.
9
10#ifndef TLAPACK_EIGEN_HALF_HH
11#define TLAPACK_EIGEN_HALF_HH
12
13#include <Eigen/Core>
14
16
17namespace tlapack {
18
19namespace traits {
20 // Eigen::half is a real type that satisfies tlapack::concepts::Real
21 template <>
23 using type = Eigen::half;
24 constexpr static bool is_real = true;
25 };
26 // The complex type of Eigen::half is std::complex<Eigen::half>
27 template <>
29 using type = std::complex<Eigen::half>;
30 constexpr static bool is_complex = false;
31 };
32} // namespace traits
33
34inline Eigen::half pow(int base, const Eigen::half& exp)
35{
36 return Eigen::half_impl::pow(Eigen::half(base), exp);
37}
38
39// Reimplementation of std::sqrt for Eigen::half. See the discussion at
40// https://github.com/gcc-mirror/gcc/pull/84
41inline std::complex<Eigen::half> sqrt(const std::complex<Eigen::half>& z)
42{
43 const Eigen::half x = real(z);
44 const Eigen::half y = imag(z);
45 const Eigen::half zero(0);
46 const Eigen::half two(2);
47 const Eigen::half half(0.5);
48
49 if (isnan(x) || isnan(y))
50 return std::numeric_limits<Eigen::half>::quiet_NaN();
51 else if (isinf(x) || isinf(y))
52 return std::numeric_limits<Eigen::half>::infinity();
53 else if (x == zero) {
54 Eigen::half t = sqrt(half * abs(y));
55 return std::complex<Eigen::half>(t, (y < zero) ? -t : t);
56 }
57 else {
58 Eigen::half t = sqrt(two * (std::abs(z) + abs(x)));
59 Eigen::half u = half * t;
60 return (x > zero)
61 ? std::complex<Eigen::half>(u, y / t)
62 : std::complex<Eigen::half>(abs(y) / t, (y < zero) ? -u : u);
63 }
64}
65
66} // namespace tlapack
67
68inline std::istream& operator>>(std::istream& is, Eigen::half& x)
69{
70 float f;
71 is >> f;
72 x = Eigen::half(f);
73 return is;
74}
75
76#endif // TLAPACK_EIGEN_HALF_HH
typename traits::real_type_traits< Types..., int >::type real_type
The common real type of the list of types.
Definition scalar_type_traits.hpp:113
constexpr bool is_complex
True if T is a complex scalar type.
Definition scalar_type_traits.hpp:192
constexpr bool is_real
True if T is a real scalar type.
Definition scalar_type_traits.hpp:117
Complex type traits for the list of types Types.
Definition scalar_type_traits.hpp:145
Real type traits for the list of types Types.
Definition scalar_type_traits.hpp:71