38#include "../../InternalHeaderCheck.h"
40#if defined(EIGEN_HAS_GPU_FP16) || defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
47#pragma push_macro("EIGEN_CONSTEXPR")
49#define EIGEN_CONSTEXPR
52#define F16_PACKET_FUNCTION(PACKET_F, PACKET_F16, METHOD) \
54 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC EIGEN_UNUSED PACKET_F16 METHOD<PACKET_F16>(const PACKET_F16& _x) { \
55 return float2half(METHOD<PACKET_F>(half2float(_x))); \
83#if !defined(EIGEN_HAS_GPU_FP16) || !defined(EIGEN_GPU_COMPILE_PHASE)
86#if (defined(EIGEN_HAS_GPU_FP16) && !defined(EIGEN_GPU_COMPILE_PHASE))
92 EIGEN_DEVICE_FUNC __half_raw() {}
94 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw() : x(0) {}
96#if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
97 explicit EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw(numext::uint16_t raw) : x(numext::bit_cast<__fp16>(raw)) {}
100 explicit EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw(numext::uint16_t raw) : x(raw) {}
105#elif defined(EIGEN_HAS_HIP_FP16)
108#elif defined(EIGEN_HAS_CUDA_FP16)
109#if EIGEN_CUDA_SDK_VER < 90000
111typedef __half __half_raw;
113#elif defined(SYCL_DEVICE_ONLY)
114typedef cl::sycl::half __half_raw;
117EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw raw_uint16_to_half(numext::uint16_t x);
118EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half_raw float_to_half_rtne(
float ff);
119EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
float half_to_float(__half_raw h);
121struct half_base :
public __half_raw {
122 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base() {}
123 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base(
const __half_raw& h) : __half_raw(h) {}
125#if defined(EIGEN_HAS_GPU_FP16)
126#if defined(EIGEN_HAS_HIP_FP16)
127 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base(
const __half& h) { x = __half_as_ushort(h); }
128#elif defined(EIGEN_HAS_CUDA_FP16)
129#if EIGEN_CUDA_SDK_VER >= 90000
130 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base(
const __half& h) : __half_raw(*(__half_raw*)&h) {}
139struct half :
public half_impl::half_base {
142#if !defined(EIGEN_HAS_GPU_FP16) || !defined(EIGEN_GPU_COMPILE_PHASE)
146 typedef half_impl::__half_raw __half_raw;
147#elif defined(EIGEN_HAS_HIP_FP16)
150#elif defined(EIGEN_HAS_CUDA_FP16)
154#if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER < 90000
155 typedef half_impl::__half_raw __half_raw;
159 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half() {}
161 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(
const __half_raw& h) : half_impl::half_base(h) {}
163#if defined(EIGEN_HAS_GPU_FP16)
164#if defined(EIGEN_HAS_HIP_FP16)
165 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(
const __half& h) : half_impl::half_base(h) {}
166#elif defined(EIGEN_HAS_CUDA_FP16)
167#if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
168 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(
const __half& h) : half_impl::half_base(h) {}
173 explicit EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(
bool b)
174 : half_impl::half_base(half_impl::raw_uint16_to_half(b ? 0x3c00 : 0)) {}
176 explicit EIGEN_DEVICE_FUNC half(T val)
177 : half_impl::half_base(half_impl::float_to_half_rtne(static_cast<float>(val))) {}
178 explicit EIGEN_DEVICE_FUNC half(
float f) : half_impl::half_base(half_impl::float_to_half_rtne(f)) {}
182 template <
typename RealScalar>
183 explicit EIGEN_DEVICE_FUNC half(std::complex<RealScalar> c)
184 : half_impl::half_base(half_impl::float_to_half_rtne(static_cast<float>(c.
real()))) {}
186 EIGEN_DEVICE_FUNC
operator float()
const {
187 return half_impl::half_to_float(*
this);
190#if defined(EIGEN_HAS_GPU_FP16) && !defined(EIGEN_GPU_COMPILE_PHASE)
191 EIGEN_DEVICE_FUNC
operator __half()
const {
202template <
typename =
void>
203struct numeric_limits_half_impl {
204 static EIGEN_CONSTEXPR
const bool is_specialized =
true;
205 static EIGEN_CONSTEXPR
const bool is_signed =
true;
206 static EIGEN_CONSTEXPR
const bool is_integer =
false;
207 static EIGEN_CONSTEXPR
const bool is_exact =
false;
208 static EIGEN_CONSTEXPR
const bool has_infinity =
true;
209 static EIGEN_CONSTEXPR
const bool has_quiet_NaN =
true;
210 static EIGEN_CONSTEXPR
const bool has_signaling_NaN =
true;
211#if __cplusplus >= 202302L
212 EIGEN_DIAGNOSTICS(push)
213 EIGEN_DISABLE_DEPRECATED_WARNING
215 static EIGEN_CONSTEXPR
const std::float_denorm_style has_denorm = std::denorm_present;
216 static EIGEN_CONSTEXPR
const bool has_denorm_loss =
false;
217#if __cplusplus >= 202302L
218 EIGEN_DIAGNOSTICS(pop)
220 static EIGEN_CONSTEXPR
const std::float_round_style round_style = std::round_to_nearest;
221 static EIGEN_CONSTEXPR
const bool is_iec559 =
true;
224 static EIGEN_CONSTEXPR
const bool is_bounded =
true;
225 static EIGEN_CONSTEXPR
const bool is_modulo =
false;
226 static EIGEN_CONSTEXPR
const int digits = 11;
227 static EIGEN_CONSTEXPR
const int digits10 =
229 static EIGEN_CONSTEXPR
const int max_digits10 =
231 static EIGEN_CONSTEXPR
const int radix = std::numeric_limits<float>::radix;
232 static EIGEN_CONSTEXPR
const int min_exponent = -13;
233 static EIGEN_CONSTEXPR
const int min_exponent10 = -4;
234 static EIGEN_CONSTEXPR
const int max_exponent = 16;
235 static EIGEN_CONSTEXPR
const int max_exponent10 = 4;
236 static EIGEN_CONSTEXPR
const bool traps = std::numeric_limits<float>::traps;
239 static EIGEN_CONSTEXPR
const bool tinyness_before = std::numeric_limits<float>::tinyness_before;
241 static EIGEN_CONSTEXPR Eigen::half(min)() {
return Eigen::half_impl::raw_uint16_to_half(0x0400); }
242 static EIGEN_CONSTEXPR Eigen::half lowest() {
return Eigen::half_impl::raw_uint16_to_half(0xfbff); }
243 static EIGEN_CONSTEXPR Eigen::half(max)() {
return Eigen::half_impl::raw_uint16_to_half(0x7bff); }
244 static EIGEN_CONSTEXPR Eigen::half epsilon() {
return Eigen::half_impl::raw_uint16_to_half(0x1400); }
245 static EIGEN_CONSTEXPR Eigen::half round_error() {
return Eigen::half_impl::raw_uint16_to_half(0x3800); }
246 static EIGEN_CONSTEXPR Eigen::half infinity() {
return Eigen::half_impl::raw_uint16_to_half(0x7c00); }
247 static EIGEN_CONSTEXPR Eigen::half quiet_NaN() {
return Eigen::half_impl::raw_uint16_to_half(0x7e00); }
248 static EIGEN_CONSTEXPR Eigen::half signaling_NaN() {
return Eigen::half_impl::raw_uint16_to_half(0x7d00); }
249 static EIGEN_CONSTEXPR Eigen::half denorm_min() {
return Eigen::half_impl::raw_uint16_to_half(0x0001); }
253EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::is_specialized;
255EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::is_signed;
257EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::is_integer;
259EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::is_exact;
261EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::has_infinity;
263EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::has_quiet_NaN;
265EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::has_signaling_NaN;
266#if __cplusplus >= 202302L
267EIGEN_DIAGNOSTICS(push)
268EIGEN_DISABLE_DEPRECATED_WARNING
271EIGEN_CONSTEXPR
const std::float_denorm_style numeric_limits_half_impl<T>::has_denorm;
273EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::has_denorm_loss;
274#if __cplusplus >= 202302L
275EIGEN_DIAGNOSTICS(pop)
278EIGEN_CONSTEXPR
const std::float_round_style numeric_limits_half_impl<T>::round_style;
280EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::is_iec559;
282EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::is_bounded;
284EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::is_modulo;
286EIGEN_CONSTEXPR
const int numeric_limits_half_impl<T>::digits;
288EIGEN_CONSTEXPR
const int numeric_limits_half_impl<T>::digits10;
290EIGEN_CONSTEXPR
const int numeric_limits_half_impl<T>::max_digits10;
292EIGEN_CONSTEXPR
const int numeric_limits_half_impl<T>::radix;
294EIGEN_CONSTEXPR
const int numeric_limits_half_impl<T>::min_exponent;
296EIGEN_CONSTEXPR
const int numeric_limits_half_impl<T>::min_exponent10;
298EIGEN_CONSTEXPR
const int numeric_limits_half_impl<T>::max_exponent;
300EIGEN_CONSTEXPR
const int numeric_limits_half_impl<T>::max_exponent10;
302EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::traps;
304EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::tinyness_before;
314class numeric_limits<
Eigen::half> :
public Eigen::half_impl::numeric_limits_half_impl<> {};
316class numeric_limits<const
Eigen::half> :
public numeric_limits<Eigen::half> {};
318class numeric_limits<volatile
Eigen::half> :
public numeric_limits<Eigen::half> {};
320class numeric_limits<const volatile
Eigen::half> :
public numeric_limits<Eigen::half> {};
327#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
328 (defined(EIGEN_HAS_HIP_FP16) && defined(HIP_DEVICE_COMPILE))
332#define EIGEN_HAS_NATIVE_FP16
340#if defined(EIGEN_HAS_NATIVE_FP16)
341EIGEN_STRONG_INLINE __device__ half operator+(
const half& a,
const half& b) {
342#if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
343 return __hadd(::__half(a), ::__half(b));
348EIGEN_STRONG_INLINE __device__ half operator*(
const half& a,
const half& b) {
return __hmul(a, b); }
349EIGEN_STRONG_INLINE __device__ half operator-(
const half& a,
const half& b) {
return __hsub(a, b); }
350EIGEN_STRONG_INLINE __device__ half operator/(
const half& a,
const half& b) {
351#if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
354 float num = __half2float(a);
355 float denom = __half2float(b);
356 return __float2half(num / denom);
359EIGEN_STRONG_INLINE __device__ half operator-(
const half& a) {
return __hneg(a); }
360EIGEN_STRONG_INLINE __device__ half& operator+=(half& a,
const half& b) {
364EIGEN_STRONG_INLINE __device__ half& operator*=(half& a,
const half& b) {
368EIGEN_STRONG_INLINE __device__ half& operator-=(half& a,
const half& b) {
372EIGEN_STRONG_INLINE __device__ half& operator/=(half& a,
const half& b) {
376EIGEN_STRONG_INLINE __device__
bool operator==(
const half& a,
const half& b) {
return __heq(a, b); }
377EIGEN_STRONG_INLINE __device__
bool operator!=(
const half& a,
const half& b) {
return __hne(a, b); }
378EIGEN_STRONG_INLINE __device__
bool operator<(
const half& a,
const half& b) {
return __hlt(a, b); }
379EIGEN_STRONG_INLINE __device__
bool operator<=(
const half& a,
const half& b) {
return __hle(a, b); }
380EIGEN_STRONG_INLINE __device__
bool operator>(
const half& a,
const half& b) {
return __hgt(a, b); }
381EIGEN_STRONG_INLINE __device__
bool operator>=(
const half& a,
const half& b) {
return __hge(a, b); }
384#if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC) && !defined(EIGEN_GPU_COMPILE_PHASE)
385EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator+(
const half& a,
const half& b) {
return half(vaddh_f16(a.x, b.x)); }
386EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator*(
const half& a,
const half& b) {
return half(vmulh_f16(a.x, b.x)); }
387EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator-(
const half& a,
const half& b) {
return half(vsubh_f16(a.x, b.x)); }
388EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator/(
const half& a,
const half& b) {
return half(vdivh_f16(a.x, b.x)); }
389EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator-(
const half& a) {
return half(vnegh_f16(a.x)); }
390EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator+=(half& a,
const half& b) {
391 a = half(vaddh_f16(a.x, b.x));
394EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator*=(half& a,
const half& b) {
395 a = half(vmulh_f16(a.x, b.x));
398EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator-=(half& a,
const half& b) {
399 a = half(vsubh_f16(a.x, b.x));
402EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator/=(half& a,
const half& b) {
403 a = half(vdivh_f16(a.x, b.x));
406EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator==(
const half& a,
const half& b) {
return vceqh_f16(a.x, b.x); }
407EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator!=(
const half& a,
const half& b) {
return !vceqh_f16(a.x, b.x); }
408EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator<(
const half& a,
const half& b) {
return vclth_f16(a.x, b.x); }
409EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator<=(
const half& a,
const half& b) {
return vcleh_f16(a.x, b.x); }
410EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator>(
const half& a,
const half& b) {
return vcgth_f16(a.x, b.x); }
411EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator>=(
const half& a,
const half& b) {
return vcgeh_f16(a.x, b.x); }
415#elif !defined(EIGEN_HAS_NATIVE_FP16) || (EIGEN_COMP_CLANG && !EIGEN_COMP_NVCC)
417#if EIGEN_COMP_CLANG && defined(EIGEN_GPUCC)
419#pragma push_macro("EIGEN_DEVICE_FUNC")
420#undef EIGEN_DEVICE_FUNC
421#if defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_HAS_NATIVE_FP16)
422#define EIGEN_DEVICE_FUNC __host__
424#define EIGEN_DEVICE_FUNC __host__ __device__
430EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator+(
const half& a,
const half& b) {
return half(
float(a) +
float(b)); }
431EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator*(
const half& a,
const half& b) {
return half(
float(a) *
float(b)); }
432EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator-(
const half& a,
const half& b) {
return half(
float(a) -
float(b)); }
433EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator/(
const half& a,
const half& b) {
return half(
float(a) /
float(b)); }
434EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator-(
const half& a) {
436 result.x = a.x ^ 0x8000;
439EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator+=(half& a,
const half& b) {
440 a = half(
float(a) +
float(b));
443EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator*=(half& a,
const half& b) {
444 a = half(
float(a) *
float(b));
447EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator-=(half& a,
const half& b) {
448 a = half(
float(a) -
float(b));
451EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator/=(half& a,
const half& b) {
452 a = half(
float(a) /
float(b));
455EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator==(
const half& a,
const half& b) {
456 return numext::equal_strict(
float(a),
float(b));
458EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator!=(
const half& a,
const half& b) {
459 return numext::not_equal_strict(
float(a),
float(b));
461EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator<(
const half& a,
const half& b) {
return float(a) < float(b); }
462EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator<=(
const half& a,
const half& b) {
return float(a) <= float(b); }
463EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator>(
const half& a,
const half& b) {
return float(a) > float(b); }
464EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator>=(
const half& a,
const half& b) {
return float(a) >= float(b); }
466#if EIGEN_COMP_CLANG && defined(EIGEN_GPUCC)
467#pragma pop_macro("EIGEN_DEVICE_FUNC")
473EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator/(
const half& a, Index b) {
474 return half(
static_cast<float>(a) /
static_cast<float>(b));
477EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator++(half& a) {
482EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator--(half& a) {
487EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator++(half& a,
int) {
488 half original_value = a;
490 return original_value;
493EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator--(half& a,
int) {
494 half original_value = a;
496 return original_value;
504EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw raw_uint16_to_half(numext::uint16_t x) {
511#if defined(EIGEN_HAS_GPU_FP16)
516 return __half_raw(x);
520EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC numext::uint16_t raw_half_as_uint16(
const __half_raw& h) {
524#if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
525 return numext::bit_cast<numext::uint16_t>(h.x);
526#elif defined(SYCL_DEVICE_ONLY)
527 return numext::bit_cast<numext::uint16_t>(h);
538EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half_raw float_to_half_rtne(
float ff) {
539#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
540 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
541 __half tmp_ff = __float2half(ff);
542 return *(__half_raw*)&tmp_ff;
544#elif defined(EIGEN_HAS_FP16_C)
548 h.x = _mm_extract_epi16(_mm_cvtps_ph(_mm_set_ss(ff), 0), 0);
550 h.x = _cvtss_sh(ff, 0);
554#elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
556 h.x =
static_cast<__fp16
>(ff);
563 const float32_bits f32infty = {255 << 23};
564 const float32_bits f16max = {(127 + 16) << 23};
565 const float32_bits denorm_magic = {((127 - 15) + (23 - 10) + 1) << 23};
566 unsigned int sign_mask = 0x80000000u;
568 o.x =
static_cast<numext::uint16_t
>(0x0u);
570 unsigned int sign = f.u & sign_mask;
578 if (f.u >= f16max.u) {
579 o.x = (f.u > f32infty.u) ? 0x7e00 : 0x7c00;
581 if (f.u < (113 << 23)) {
585 f.f += denorm_magic.f;
588 o.x =
static_cast<numext::uint16_t
>(f.u - denorm_magic.u);
590 unsigned int mant_odd = (f.u >> 13) & 1;
599 o.x =
static_cast<numext::uint16_t
>(f.u >> 13);
603 o.x |=
static_cast<numext::uint16_t
>(
sign >> 16);
608EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
float half_to_float(__half_raw h) {
609#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
610 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
611 return __half2float(h);
612#elif defined(EIGEN_HAS_FP16_C)
615 return _mm_cvtss_f32(_mm_cvtph_ps(_mm_set1_epi16(h.x)));
617 return _cvtsh_ss(h.x);
619#elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
620 return static_cast<float>(h.x);
622 const float32_bits magic = {113 << 23};
623 const unsigned int shifted_exp = 0x7c00 << 13;
626 o.u = (h.x & 0x7fff) << 13;
627 unsigned int exp = shifted_exp & o.u;
628 o.u += (127 - 15) << 23;
631 if (exp == shifted_exp) {
632 o.u += (128 - 16) << 23;
633 }
else if (exp == 0) {
638 o.u |= (h.x & 0x8000) << 16;
645EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool(isinf)(
const half& a) {
646#ifdef EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC
647 return (numext::bit_cast<numext::uint16_t>(a.x) & 0x7fff) == 0x7c00;
649 return (a.x & 0x7fff) == 0x7c00;
652EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool(isnan)(
const half& a) {
653#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
654 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
656#elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
657 return (numext::bit_cast<numext::uint16_t>(a.x) & 0x7fff) > 0x7c00;
659 return (a.x & 0x7fff) > 0x7c00;
662EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool(isfinite)(
const half& a) {
663 return !(isinf EIGEN_NOT_A_MACRO(a)) && !(isnan EIGEN_NOT_A_MACRO(a));
666EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half abs(
const half& a) {
667#if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
668 return half(vabsh_f16(a.x));
671 result.x = a.x & 0x7FFF;
675EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half exp(
const half& a) {
676#if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 530) || \
677 defined(EIGEN_HIP_DEVICE_COMPILE)
678 return half(hexp(a));
680 return half(::expf(
float(a)));
683EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half expm1(
const half& a) {
return half(numext::expm1(
float(a))); }
684EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log(
const half& a) {
685#if (defined(EIGEN_HAS_CUDA_FP16) && EIGEN_CUDA_SDK_VER >= 80000 && defined(EIGEN_CUDA_ARCH) && \
686 EIGEN_CUDA_ARCH >= 530) || \
687 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
688 return half(::hlog(a));
690 return half(::logf(
float(a)));
693EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log1p(
const half& a) {
return half(numext::log1p(
float(a))); }
694EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log10(
const half& a) {
return half(::log10f(
float(a))); }
695EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log2(
const half& a) {
696 return half(
static_cast<float>(EIGEN_LOG2E) * ::logf(
float(a)));
699EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half sqrt(
const half& a) {
700#if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 530) || \
701 defined(EIGEN_HIP_DEVICE_COMPILE)
702 return half(hsqrt(a));
704 return half(::sqrtf(
float(a)));
707EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half pow(
const half& a,
const half& b) {
708 return half(::powf(
float(a),
float(b)));
710EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half atan2(
const half& a,
const half& b) {
711 return half(::atan2f(
float(a),
float(b)));
713EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half sin(
const half& a) {
return half(::sinf(
float(a))); }
714EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half cos(
const half& a) {
return half(::cosf(
float(a))); }
715EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half tan(
const half& a) {
return half(::tanf(
float(a))); }
716EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half tanh(
const half& a) {
return half(::tanhf(
float(a))); }
717EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half asin(
const half& a) {
return half(::asinf(
float(a))); }
718EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half acos(
const half& a) {
return half(::acosf(
float(a))); }
719EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half atan(
const half& a) {
return half(::atanf(
float(a))); }
720EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half atanh(
const half& a) {
return half(::atanhf(
float(a))); }
721EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half floor(
const half& a) {
722#if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 300) || \
723 defined(EIGEN_HIP_DEVICE_COMPILE)
724 return half(hfloor(a));
726 return half(::floorf(
float(a)));
729EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half ceil(
const half& a) {
730#if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 300) || \
731 defined(EIGEN_HIP_DEVICE_COMPILE)
732 return half(hceil(a));
734 return half(::ceilf(
float(a)));
737EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half rint(
const half& a) {
return half(::rintf(
float(a))); }
738EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half round(
const half& a) {
return half(::roundf(
float(a))); }
739EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half trunc(
const half& a) {
return half(::truncf(
float(a))); }
740EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half fmod(
const half& a,
const half& b) {
741 return half(::fmodf(
float(a),
float(b)));
744EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half(min)(
const half& a,
const half& b) {
745#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
746 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
747 return __hlt(b, a) ? b : a;
749 const float f1 =
static_cast<float>(a);
750 const float f2 =
static_cast<float>(b);
751 return f2 < f1 ? b : a;
754EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half(max)(
const half& a,
const half& b) {
755#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
756 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
757 return __hlt(a, b) ? b : a;
759 const float f1 =
static_cast<float>(a);
760 const float f2 =
static_cast<float>(b);
761 return f1 < f2 ? b : a;
766EIGEN_ALWAYS_INLINE std::ostream& operator<<(std::ostream& os,
const half& v) {
767 os << static_cast<float>(v);
780struct is_arithmetic<half> {
781 enum { value =
true };
785struct random_impl<half> {
786 enum :
int { MantissaBits = 10 };
787 using Impl = random_impl<float>;
788 static EIGEN_DEVICE_FUNC
inline half run(
const half& x,
const half& y) {
789 float result = Impl::run(x, y, MantissaBits);
792 static EIGEN_DEVICE_FUNC
inline half run() {
793 float result = Impl::run(MantissaBits);
801struct NumTraits<
Eigen::half> : GenericNumTraits<Eigen::half> {
802 enum { IsSigned =
true, IsInteger =
false, IsComplex =
false, RequireInitialization =
false };
804 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
static EIGEN_STRONG_INLINE Eigen::half epsilon() {
805 return half_impl::raw_uint16_to_half(0x0800);
807 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
static EIGEN_STRONG_INLINE Eigen::half dummy_precision() {
808 return half_impl::raw_uint16_to_half(0x211f);
810 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
static EIGEN_STRONG_INLINE Eigen::half highest() {
811 return half_impl::raw_uint16_to_half(0x7bff);
813 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
static EIGEN_STRONG_INLINE Eigen::half lowest() {
814 return half_impl::raw_uint16_to_half(0xfbff);
816 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
static EIGEN_STRONG_INLINE Eigen::half infinity() {
817 return half_impl::raw_uint16_to_half(0x7c00);
819 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
static EIGEN_STRONG_INLINE Eigen::half quiet_NaN() {
820 return half_impl::raw_uint16_to_half(0x7e00);
826#if defined(EIGEN_HAS_GPU_FP16) || defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
827#pragma pop_macro("EIGEN_CONSTEXPR")
833#if defined(EIGEN_GPU_COMPILE_PHASE)
836EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool(isnan)(
const Eigen::half& h) {
837 return (half_impl::isnan)(h);
841EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool(isinf)(
const Eigen::half& h) {
842 return (half_impl::isinf)(h);
846EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool(isfinite)(
const Eigen::half& h) {
847 return (half_impl::isfinite)(h);
853EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Eigen::half bit_cast<Eigen::half, uint16_t>(
const uint16_t& src) {
854 return Eigen::half(Eigen::half_impl::raw_uint16_to_half(src));
858EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC uint16_t bit_cast<uint16_t, Eigen::half>(
const Eigen::half& src) {
859 return Eigen::half_impl::raw_half_as_uint16(src);
876#if (defined(EIGEN_CUDACC) && (!defined(EIGEN_CUDA_ARCH) || EIGEN_CUDA_ARCH >= 300)) || defined(EIGEN_HIPCC)
878#if defined(EIGEN_HAS_CUDA_FP16) && EIGEN_CUDA_SDK_VER >= 90000
880__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_sync(
unsigned mask, Eigen::half var,
int srcLane,
881 int width = warpSize) {
882 const __half h = var;
883 return static_cast<Eigen::half
>(__shfl_sync(mask, h, srcLane, width));
886__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_up_sync(
unsigned mask, Eigen::half var,
unsigned int delta,
887 int width = warpSize) {
888 const __half h = var;
889 return static_cast<Eigen::half
>(__shfl_up_sync(mask, h, delta, width));
892__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_down_sync(
unsigned mask, Eigen::half var,
unsigned int delta,
893 int width = warpSize) {
894 const __half h = var;
895 return static_cast<Eigen::half
>(__shfl_down_sync(mask, h, delta, width));
898__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_xor_sync(
unsigned mask, Eigen::half var,
int laneMask,
899 int width = warpSize) {
900 const __half h = var;
901 return static_cast<Eigen::half
>(__shfl_xor_sync(mask, h, laneMask, width));
906__device__ EIGEN_STRONG_INLINE Eigen::half __shfl(Eigen::half var,
int srcLane,
int width = warpSize) {
907 const int ivar =
static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
908 return Eigen::numext::bit_cast<Eigen::half>(
static_cast<Eigen::numext::uint16_t
>(__shfl(ivar, srcLane, width)));
911__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_up(Eigen::half var,
unsigned int delta,
int width = warpSize) {
912 const int ivar =
static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
913 return Eigen::numext::bit_cast<Eigen::half>(
static_cast<Eigen::numext::uint16_t
>(__shfl_up(ivar, delta, width)));
916__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_down(Eigen::half var,
unsigned int delta,
int width = warpSize) {
917 const int ivar =
static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
918 return Eigen::numext::bit_cast<Eigen::half>(
static_cast<Eigen::numext::uint16_t
>(__shfl_down(ivar, delta, width)));
921__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_xor(Eigen::half var,
int laneMask,
int width = warpSize) {
922 const int ivar =
static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
923 return Eigen::numext::bit_cast<Eigen::half>(
static_cast<Eigen::numext::uint16_t
>(__shfl_xor(ivar, laneMask, width)));
930#if (defined(EIGEN_CUDACC) && (!defined(EIGEN_CUDA_ARCH) || EIGEN_CUDA_ARCH >= 350)) || defined(EIGEN_HIPCC)
931EIGEN_STRONG_INLINE __device__ Eigen::half __ldg(
const Eigen::half* ptr) {
932 return Eigen::half_impl::raw_uint16_to_half(__ldg(
reinterpret_cast<const Eigen::numext::uint16_t*
>(ptr)));
936#if EIGEN_HAS_STD_HASH
939struct hash<
Eigen::half> {
940 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::size_t operator()(
const Eigen::half& a)
const {
941 return static_cast<std::size_t
>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(a));
951struct cast_impl<float, half> {
952 EIGEN_DEVICE_FUNC
static inline half run(
const float& a) {
953#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
954 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
955 return __float2half(a);
963struct cast_impl<int, half> {
964 EIGEN_DEVICE_FUNC
static inline half run(
const int& a) {
965#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
966 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
967 return __float2half(
static_cast<float>(a));
969 return half(
static_cast<float>(a));
975struct cast_impl<half, float> {
976 EIGEN_DEVICE_FUNC
static inline float run(
const half& a) {
977#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
978 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
979 return __half2float(a);
981 return static_cast<float>(a);
Namespace containing all symbols from the Eigen library.
Definition Core:137
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_real_op< typename Derived::Scalar >, const Derived > real(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sign_op< typename Derived::Scalar >, const Derived > sign(const Eigen::ArrayBase< Derived > &x)