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 EIGEN_DIAGNOSTICS(push)
212 EIGEN_DISABLE_DEPRECATED_WARNING
213 static EIGEN_CONSTEXPR
const std::float_denorm_style has_denorm = std::denorm_present;
214 static EIGEN_CONSTEXPR
const bool has_denorm_loss =
false;
215 EIGEN_DIAGNOSTICS(pop)
216 static EIGEN_CONSTEXPR
const std::float_round_style round_style = std::round_to_nearest;
217 static EIGEN_CONSTEXPR
const bool is_iec559 =
true;
220 static EIGEN_CONSTEXPR
const bool is_bounded =
true;
221 static EIGEN_CONSTEXPR
const bool is_modulo =
false;
222 static EIGEN_CONSTEXPR
const int digits = 11;
223 static EIGEN_CONSTEXPR
const int digits10 =
225 static EIGEN_CONSTEXPR
const int max_digits10 =
227 static EIGEN_CONSTEXPR
const int radix = std::numeric_limits<float>::radix;
228 static EIGEN_CONSTEXPR
const int min_exponent = -13;
229 static EIGEN_CONSTEXPR
const int min_exponent10 = -4;
230 static EIGEN_CONSTEXPR
const int max_exponent = 16;
231 static EIGEN_CONSTEXPR
const int max_exponent10 = 4;
232 static EIGEN_CONSTEXPR
const bool traps = std::numeric_limits<float>::traps;
235 static EIGEN_CONSTEXPR
const bool tinyness_before = std::numeric_limits<float>::tinyness_before;
237 static EIGEN_CONSTEXPR Eigen::half(min)() {
return Eigen::half_impl::raw_uint16_to_half(0x0400); }
238 static EIGEN_CONSTEXPR Eigen::half lowest() {
return Eigen::half_impl::raw_uint16_to_half(0xfbff); }
239 static EIGEN_CONSTEXPR Eigen::half(max)() {
return Eigen::half_impl::raw_uint16_to_half(0x7bff); }
240 static EIGEN_CONSTEXPR Eigen::half epsilon() {
return Eigen::half_impl::raw_uint16_to_half(0x1400); }
241 static EIGEN_CONSTEXPR Eigen::half round_error() {
return Eigen::half_impl::raw_uint16_to_half(0x3800); }
242 static EIGEN_CONSTEXPR Eigen::half infinity() {
return Eigen::half_impl::raw_uint16_to_half(0x7c00); }
243 static EIGEN_CONSTEXPR Eigen::half quiet_NaN() {
return Eigen::half_impl::raw_uint16_to_half(0x7e00); }
244 static EIGEN_CONSTEXPR Eigen::half signaling_NaN() {
return Eigen::half_impl::raw_uint16_to_half(0x7d00); }
245 static EIGEN_CONSTEXPR Eigen::half denorm_min() {
return Eigen::half_impl::raw_uint16_to_half(0x0001); }
249EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::is_specialized;
251EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::is_signed;
253EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::is_integer;
255EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::is_exact;
257EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::has_infinity;
259EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::has_quiet_NaN;
261EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::has_signaling_NaN;
262EIGEN_DIAGNOSTICS(push)
263EIGEN_DISABLE_DEPRECATED_WARNING
265EIGEN_CONSTEXPR
const std::float_denorm_style numeric_limits_half_impl<T>::has_denorm;
267EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::has_denorm_loss;
268EIGEN_DIAGNOSTICS(pop)
270EIGEN_CONSTEXPR
const std::float_round_style numeric_limits_half_impl<T>::round_style;
272EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::is_iec559;
274EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::is_bounded;
276EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::is_modulo;
278EIGEN_CONSTEXPR
const int numeric_limits_half_impl<T>::digits;
280EIGEN_CONSTEXPR
const int numeric_limits_half_impl<T>::digits10;
282EIGEN_CONSTEXPR
const int numeric_limits_half_impl<T>::max_digits10;
284EIGEN_CONSTEXPR
const int numeric_limits_half_impl<T>::radix;
286EIGEN_CONSTEXPR
const int numeric_limits_half_impl<T>::min_exponent;
288EIGEN_CONSTEXPR
const int numeric_limits_half_impl<T>::min_exponent10;
290EIGEN_CONSTEXPR
const int numeric_limits_half_impl<T>::max_exponent;
292EIGEN_CONSTEXPR
const int numeric_limits_half_impl<T>::max_exponent10;
294EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::traps;
296EIGEN_CONSTEXPR
const bool numeric_limits_half_impl<T>::tinyness_before;
306class numeric_limits<Eigen::half> :
public Eigen::half_impl::numeric_limits_half_impl<> {};
308class numeric_limits<const Eigen::half> :
public numeric_limits<Eigen::half> {};
310class numeric_limits<volatile Eigen::half> :
public numeric_limits<Eigen::half> {};
312class numeric_limits<const volatile Eigen::half> :
public numeric_limits<Eigen::half> {};
319#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
320 (defined(EIGEN_HAS_HIP_FP16) && defined(HIP_DEVICE_COMPILE))
324#define EIGEN_HAS_NATIVE_FP16
332#if defined(EIGEN_HAS_NATIVE_FP16)
333EIGEN_STRONG_INLINE __device__ half operator+(
const half& a,
const half& b) {
334#if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
335 return __hadd(::__half(a), ::__half(b));
340EIGEN_STRONG_INLINE __device__ half operator*(
const half& a,
const half& b) {
return __hmul(a, b); }
341EIGEN_STRONG_INLINE __device__ half operator-(
const half& a,
const half& b) {
return __hsub(a, b); }
342EIGEN_STRONG_INLINE __device__ half operator/(
const half& a,
const half& b) {
343#if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
346 float num = __half2float(a);
347 float denom = __half2float(b);
348 return __float2half(num / denom);
351EIGEN_STRONG_INLINE __device__ half operator-(
const half& a) {
return __hneg(a); }
352EIGEN_STRONG_INLINE __device__ half& operator+=(half& a,
const half& b) {
356EIGEN_STRONG_INLINE __device__ half& operator*=(half& a,
const half& b) {
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__
bool operator==(
const half& a,
const half& b) {
return __heq(a, b); }
369EIGEN_STRONG_INLINE __device__
bool operator!=(
const half& a,
const half& b) {
return __hne(a, b); }
370EIGEN_STRONG_INLINE __device__
bool operator<(
const half& a,
const half& b) {
return __hlt(a, b); }
371EIGEN_STRONG_INLINE __device__
bool operator<=(
const half& a,
const half& b) {
return __hle(a, b); }
372EIGEN_STRONG_INLINE __device__
bool operator>(
const half& a,
const half& b) {
return __hgt(a, b); }
373EIGEN_STRONG_INLINE __device__
bool operator>=(
const half& a,
const half& b) {
return __hge(a, b); }
376#if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC) && !defined(EIGEN_GPU_COMPILE_PHASE)
377EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator+(
const half& a,
const half& b) {
return half(vaddh_f16(a.x, b.x)); }
378EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator*(
const half& a,
const half& b) {
return half(vmulh_f16(a.x, b.x)); }
379EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator-(
const half& a,
const half& b) {
return half(vsubh_f16(a.x, b.x)); }
380EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator/(
const half& a,
const half& b) {
return half(vdivh_f16(a.x, b.x)); }
381EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator-(
const half& a) {
return half(vnegh_f16(a.x)); }
382EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator+=(half& a,
const half& b) {
383 a = half(vaddh_f16(a.x, b.x));
386EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator*=(half& a,
const half& b) {
387 a = half(vmulh_f16(a.x, b.x));
390EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator-=(half& a,
const half& b) {
391 a = half(vsubh_f16(a.x, b.x));
394EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator/=(half& a,
const half& b) {
395 a = half(vdivh_f16(a.x, b.x));
398EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator==(
const half& a,
const half& b) {
return vceqh_f16(a.x, b.x); }
399EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator!=(
const half& a,
const half& b) {
return !vceqh_f16(a.x, b.x); }
400EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator<(
const half& a,
const half& b) {
return vclth_f16(a.x, b.x); }
401EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator<=(
const half& a,
const half& b) {
return vcleh_f16(a.x, b.x); }
402EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator>(
const half& a,
const half& b) {
return vcgth_f16(a.x, b.x); }
403EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator>=(
const half& a,
const half& b) {
return vcgeh_f16(a.x, b.x); }
407#elif !defined(EIGEN_HAS_NATIVE_FP16) || (EIGEN_COMP_CLANG && !EIGEN_COMP_NVCC)
409#if EIGEN_COMP_CLANG && defined(EIGEN_GPUCC)
411#pragma push_macro("EIGEN_DEVICE_FUNC")
412#undef EIGEN_DEVICE_FUNC
413#if defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_HAS_NATIVE_FP16)
414#define EIGEN_DEVICE_FUNC __host__
416#define EIGEN_DEVICE_FUNC __host__ __device__
422EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator+(
const half& a,
const half& b) {
return half(
float(a) +
float(b)); }
423EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator*(
const half& a,
const half& b) {
return half(
float(a) *
float(b)); }
424EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator-(
const half& a,
const half& b) {
return half(
float(a) -
float(b)); }
425EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator/(
const half& a,
const half& b) {
return half(
float(a) /
float(b)); }
426EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator-(
const half& a) {
428 result.x = a.x ^ 0x8000;
431EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator+=(half& a,
const half& b) {
432 a = half(
float(a) +
float(b));
435EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator*=(half& a,
const half& b) {
436 a = half(
float(a) *
float(b));
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
bool operator==(
const half& a,
const half& b) {
448 return numext::equal_strict(
float(a),
float(b));
450EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator!=(
const half& a,
const half& b) {
451 return numext::not_equal_strict(
float(a),
float(b));
453EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator<(
const half& a,
const half& b) {
return float(a) < float(b); }
454EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator<=(
const half& a,
const half& b) {
return float(a) <= float(b); }
455EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator>(
const half& a,
const half& b) {
return float(a) > float(b); }
456EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator>=(
const half& a,
const half& b) {
return float(a) >= float(b); }
458#if EIGEN_COMP_CLANG && defined(EIGEN_GPUCC)
459#pragma pop_macro("EIGEN_DEVICE_FUNC")
465EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator/(
const half& a,
Index b) {
466 return half(
static_cast<float>(a) /
static_cast<float>(b));
469EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator++(half& a) {
474EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator--(half& a) {
479EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator++(half& a,
int) {
480 half original_value = a;
482 return original_value;
485EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator--(half& a,
int) {
486 half original_value = a;
488 return original_value;
496EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw raw_uint16_to_half(numext::uint16_t x) {
503#if defined(EIGEN_HAS_GPU_FP16)
508 return __half_raw(x);
512EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC numext::uint16_t raw_half_as_uint16(
const __half_raw& h) {
516#if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
517 return numext::bit_cast<numext::uint16_t>(h.x);
518#elif defined(SYCL_DEVICE_ONLY)
519 return numext::bit_cast<numext::uint16_t>(h);
530EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half_raw float_to_half_rtne(
float ff) {
531#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
532 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
533 __half tmp_ff = __float2half(ff);
534 return *(__half_raw*)&tmp_ff;
536#elif defined(EIGEN_HAS_FP16_C)
540 h.x = _mm_extract_epi16(_mm_cvtps_ph(_mm_set_ss(ff), 0), 0);
542 h.x = _cvtss_sh(ff, 0);
546#elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
548 h.x =
static_cast<__fp16
>(ff);
555 const float32_bits f32infty = {255 << 23};
556 const float32_bits f16max = {(127 + 16) << 23};
557 const float32_bits denorm_magic = {((127 - 15) + (23 - 10) + 1) << 23};
558 unsigned int sign_mask = 0x80000000u;
560 o.x =
static_cast<numext::uint16_t
>(0x0u);
562 unsigned int sign = f.u & sign_mask;
570 if (f.u >= f16max.u) {
571 o.x = (f.u > f32infty.u) ? 0x7e00 : 0x7c00;
573 if (f.u < (113 << 23)) {
577 f.f += denorm_magic.f;
580 o.x =
static_cast<numext::uint16_t
>(f.u - denorm_magic.u);
582 unsigned int mant_odd = (f.u >> 13) & 1;
591 o.x =
static_cast<numext::uint16_t
>(f.u >> 13);
595 o.x |=
static_cast<numext::uint16_t
>(
sign >> 16);
600EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
float half_to_float(__half_raw h) {
601#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
602 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
603 return __half2float(h);
604#elif defined(EIGEN_HAS_FP16_C)
607 return _mm_cvtss_f32(_mm_cvtph_ps(_mm_set1_epi16(h.x)));
609 return _cvtsh_ss(h.x);
611#elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
612 return static_cast<float>(h.x);
614 const float32_bits magic = {113 << 23};
615 const unsigned int shifted_exp = 0x7c00 << 13;
618 o.u = (h.x & 0x7fff) << 13;
619 unsigned int exp = shifted_exp & o.u;
620 o.u += (127 - 15) << 23;
623 if (exp == shifted_exp) {
624 o.u += (128 - 16) << 23;
625 }
else if (exp == 0) {
630 o.u |= (h.x & 0x8000) << 16;
637EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool(isinf)(
const half& a) {
638#ifdef EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC
639 return (numext::bit_cast<numext::uint16_t>(a.x) & 0x7fff) == 0x7c00;
641 return (a.x & 0x7fff) == 0x7c00;
644EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool(isnan)(
const half& a) {
645#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
646 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
648#elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
649 return (numext::bit_cast<numext::uint16_t>(a.x) & 0x7fff) > 0x7c00;
651 return (a.x & 0x7fff) > 0x7c00;
654EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool(isfinite)(
const half& a) {
655 return !(isinf EIGEN_NOT_A_MACRO(a)) && !(isnan EIGEN_NOT_A_MACRO(a));
658EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half abs(
const half& a) {
659#if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
660 return half(vabsh_f16(a.x));
663 result.x = a.x & 0x7FFF;
667EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half exp(
const half& a) {
668#if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 530) || \
669 defined(EIGEN_HIP_DEVICE_COMPILE)
670 return half(hexp(a));
672 return half(::expf(
float(a)));
675EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half exp2(
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(hexp2(a));
680 return half(::exp2f(
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 B01_Experimental.dox:1
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)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:82
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition Meta.h:522