41 #if defined(EIGEN_HAS_GPU_FP16) || defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
48 #pragma push_macro("EIGEN_CONSTEXPR")
49 #undef EIGEN_CONSTEXPR
50 #define EIGEN_CONSTEXPR
53 #define F16_PACKET_FUNCTION(PACKET_F, PACKET_F16, METHOD) \
55 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC EIGEN_UNUSED \
56 PACKET_F16 METHOD<PACKET_F16>(const PACKET_F16& _x) { \
57 return float2half(METHOD<PACKET_F>(half2float(_x))); \
66 #if !defined(EIGEN_HAS_GPU_FP16)
69 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
__half_raw() : x(0) {}
70 #if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
71 explicit EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
__half_raw(numext::uint16_t raw) : x(numext::bit_cast<__fp16>(raw)) {
75 explicit EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
__half_raw(numext::uint16_t raw) : x(raw) {}
80 #elif defined(EIGEN_HAS_HIP_FP16)
83 #elif defined(EIGEN_HAS_CUDA_FP16)
84 #if EIGEN_CUDA_SDK_VER < 90000
87 #endif // defined(EIGEN_HAS_CUDA_FP16)
88 #elif defined(SYCL_DEVICE_ONLY)
92 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
__half_raw raw_uint16_to_half(numext::uint16_t x);
93 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
__half_raw float_to_half_rtne(
float ff);
94 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
float half_to_float(
__half_raw h);
97 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
half_base() {}
100 #if defined(EIGEN_HAS_GPU_FP16)
101 #if defined(EIGEN_HAS_HIP_FP16)
102 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
half_base(
const __half& h) { x = __half_as_ushort(h); }
103 #elif defined(EIGEN_HAS_CUDA_FP16)
104 #if EIGEN_CUDA_SDK_VER >= 90000
118 #if !defined(EIGEN_HAS_GPU_FP16)
120 #elif defined(EIGEN_HAS_HIP_FP16)
123 #elif defined(EIGEN_HAS_CUDA_FP16)
127 #if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER < 90000
132 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
half() {}
136 #if defined(EIGEN_HAS_GPU_FP16)
137 #if defined(EIGEN_HAS_HIP_FP16)
139 #elif defined(EIGEN_HAS_CUDA_FP16)
140 #if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
147 explicit EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
half(
bool b)
150 explicit EIGEN_DEVICE_FUNC
half(
const T& val)
152 explicit EIGEN_DEVICE_FUNC
half(
float f)
157 template<
typename RealScalar>
158 explicit EIGEN_DEVICE_FUNC
half(std::complex<RealScalar> c)
161 EIGEN_DEVICE_FUNC
operator float()
const {
162 return half_impl::half_to_float(*
this);
170 struct numeric_limits<
Eigen::half> {
171 static const bool is_specialized =
true;
172 static const bool is_signed =
true;
173 static const bool is_integer =
false;
174 static const bool is_exact =
false;
175 static const bool has_infinity =
true;
176 static const bool has_quiet_NaN =
true;
177 static const bool has_signaling_NaN =
true;
178 static const float_denorm_style has_denorm = denorm_present;
179 static const bool has_denorm_loss =
false;
180 static const std::float_round_style round_style = std::round_to_nearest;
181 static const bool is_iec559 =
false;
182 static const bool is_bounded =
false;
183 static const bool is_modulo =
false;
184 static const int digits = 11;
185 static const int digits10 = 3;
186 static const int max_digits10 = 5;
187 static const int radix = 2;
188 static const int min_exponent = -13;
189 static const int min_exponent10 = -4;
190 static const int max_exponent = 16;
191 static const int max_exponent10 = 4;
192 static const bool traps =
true;
193 static const bool tinyness_before =
false;
195 static Eigen::half (min)() {
return Eigen::half_impl::raw_uint16_to_half(0x400); }
196 static Eigen::half lowest() {
return Eigen::half_impl::raw_uint16_to_half(0xfbff); }
197 static Eigen::half (max)() {
return Eigen::half_impl::raw_uint16_to_half(0x7bff); }
198 static Eigen::half epsilon() {
return Eigen::half_impl::raw_uint16_to_half(0x0800); }
200 static Eigen::half infinity() {
return Eigen::half_impl::raw_uint16_to_half(0x7c00); }
201 static Eigen::half quiet_NaN() {
return Eigen::half_impl::raw_uint16_to_half(0x7e00); }
202 static Eigen::half signaling_NaN() {
return Eigen::half_impl::raw_uint16_to_half(0x7d00); }
203 static Eigen::half denorm_min() {
return Eigen::half_impl::raw_uint16_to_half(0x1); }
211 struct numeric_limits<const
Eigen::half> : numeric_limits<Eigen::half> {};
213 struct numeric_limits<volatile
Eigen::half> : numeric_limits<Eigen::half> {};
215 struct numeric_limits<const volatile
Eigen::half> : numeric_limits<Eigen::half> {};
220 namespace half_impl {
222 #if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && \
223 EIGEN_CUDA_ARCH >= 530) || \
224 (defined(EIGEN_HAS_HIP_FP16) && defined(HIP_DEVICE_COMPILE))
228 #define EIGEN_HAS_NATIVE_FP16
236 #if defined(EIGEN_HAS_NATIVE_FP16)
237 EIGEN_STRONG_INLINE __device__ half operator + (
const half& a,
const half& b) {
238 #if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
239 return __hadd(::__half(a), ::__half(b));
244 EIGEN_STRONG_INLINE __device__ half
operator * (
const half& a,
const half& b) {
247 EIGEN_STRONG_INLINE __device__ half operator - (
const half& a,
const half& b) {
250 EIGEN_STRONG_INLINE __device__ half operator / (
const half& a,
const half& b) {
251 #if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
254 float num = __half2float(a);
255 float denom = __half2float(b);
256 return __float2half(num / denom);
259 EIGEN_STRONG_INLINE __device__ half operator - (
const half& a) {
262 EIGEN_STRONG_INLINE __device__ half& operator += (half& a,
const half& b) {
266 EIGEN_STRONG_INLINE __device__ half& operator *= (half& a,
const half& b) {
270 EIGEN_STRONG_INLINE __device__ half& operator -= (half& a,
const half& b) {
274 EIGEN_STRONG_INLINE __device__ half& operator /= (half& a,
const half& b) {
278 EIGEN_STRONG_INLINE __device__
bool operator == (
const half& a,
const half& b) {
281 EIGEN_STRONG_INLINE __device__
bool operator != (
const half& a,
const half& b) {
284 EIGEN_STRONG_INLINE __device__
bool operator < (
const half& a,
const half& b) {
287 EIGEN_STRONG_INLINE __device__
bool operator <= (
const half& a,
const half& b) {
290 EIGEN_STRONG_INLINE __device__
bool operator > (
const half& a,
const half& b) {
293 EIGEN_STRONG_INLINE __device__
bool operator >= (
const half& a,
const half& b) {
298 #if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
299 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator + (
const half& a,
const half& b) {
300 return half(vaddh_f16(a.x, b.x));
302 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half
operator * (
const half& a,
const half& b) {
303 return half(vmulh_f16(a.x, b.x));
305 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator - (
const half& a,
const half& b) {
306 return half(vsubh_f16(a.x, b.x));
308 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator / (
const half& a,
const half& b) {
309 return half(vdivh_f16(a.x, b.x));
311 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator - (
const half& a) {
312 return half(vnegh_f16(a.x));
314 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator += (half& a,
const half& b) {
315 a = half(vaddh_f16(a.x, b.x));
318 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator *= (half& a,
const half& b) {
319 a = half(vmulh_f16(a.x, b.x));
322 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator -= (half& a,
const half& b) {
323 a = half(vsubh_f16(a.x, b.x));
326 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator /= (half& a,
const half& b) {
327 a = half(vdivh_f16(a.x, b.x));
330 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator == (
const half& a,
const half& b) {
331 return vceqh_f16(a.x, b.x);
333 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator != (
const half& a,
const half& b) {
334 return !vceqh_f16(a.x, b.x);
336 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator < (
const half& a,
const half& b) {
337 return vclth_f16(a.x, b.x);
339 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator <= (
const half& a,
const half& b) {
340 return vcleh_f16(a.x, b.x);
342 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator > (
const half& a,
const half& b) {
343 return vcgth_f16(a.x, b.x);
345 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator >= (
const half& a,
const half& b) {
346 return vcgeh_f16(a.x, b.x);
351 #elif !defined(EIGEN_HAS_NATIVE_FP16) || (EIGEN_COMP_CLANG && !EIGEN_COMP_NVCC) // Emulate support for half floats
353 #if EIGEN_COMP_CLANG && defined(EIGEN_CUDACC)
355 #pragma push_macro("EIGEN_DEVICE_FUNC")
356 #undef EIGEN_DEVICE_FUNC
357 #if defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_HAS_NATIVE_FP16)
358 #define EIGEN_DEVICE_FUNC __host__
359 #else // both host and device need emulated ops.
360 #define EIGEN_DEVICE_FUNC __host__ __device__
366 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator + (
const half& a,
const half& b) {
367 return half(
float(a) +
float(b));
369 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half
operator * (
const half& a,
const half& b) {
370 return half(
float(a) *
float(b));
372 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator - (
const half& a,
const half& b) {
373 return half(
float(a) -
float(b));
375 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator / (
const half& a,
const half& b) {
376 return half(
float(a) /
float(b));
378 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator - (
const half& a) {
380 result.x = a.x ^ 0x8000;
383 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator += (half& a,
const half& b) {
384 a = half(
float(a) +
float(b));
387 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator *= (half& a,
const half& b) {
388 a = half(
float(a) *
float(b));
391 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator -= (half& a,
const half& b) {
392 a = half(
float(a) -
float(b));
395 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator /= (half& a,
const half& b) {
396 a = half(
float(a) /
float(b));
399 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator == (
const half& a,
const half& b) {
400 return numext::equal_strict(
float(a),
float(b));
402 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator != (
const half& a,
const half& b) {
403 return numext::not_equal_strict(
float(a),
float(b));
405 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator < (
const half& a,
const half& b) {
406 return float(a) < float(b);
408 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator <= (
const half& a,
const half& b) {
409 return float(a) <= float(b);
411 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator > (
const half& a,
const half& b) {
412 return float(a) > float(b);
414 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool operator >= (
const half& a,
const half& b) {
415 return float(a) >= float(b);
418 #if defined(__clang__) && defined(__CUDA__)
419 #pragma pop_macro("EIGEN_DEVICE_FUNC")
421 #endif // Emulate support for half floats
425 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator / (
const half& a,
Index b) {
426 return half(
static_cast<float>(a) /
static_cast<float>(b));
434 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw raw_uint16_to_half(numext::uint16_t x) {
441 #if defined(EIGEN_HAS_GPU_FP16)
446 return __half_raw(x);
450 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC numext::uint16_t raw_half_as_uint16(
const __half_raw& h) {
454 #if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
455 return numext::bit_cast<numext::uint16_t>(h.x);
456 #elif defined(SYCL_DEVICE_ONLY)
457 return numext::bit_cast<numext::uint16_t>(h);
468 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
__half_raw float_to_half_rtne(
float ff) {
469 #if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
470 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
471 __half tmp_ff = __float2half(ff);
474 #elif defined(EIGEN_HAS_FP16_C)
476 h.x = _cvtss_sh(ff, 0);
479 #elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
481 h.x =
static_cast<__fp16
>(ff);
488 const float32_bits f16max = { (127 + 16) << 23 };
489 const float32_bits denorm_magic = { ((127 - 15) + (23 - 10) + 1) << 23 };
490 unsigned int sign_mask = 0x80000000u;
492 o.x =
static_cast<numext::uint16_t
>(0x0u);
494 unsigned int sign = f.u & sign_mask;
502 if (f.u >= f16max.u) {
503 o.x = (f.u > f32infty.u) ? 0x7e00 : 0x7c00;
505 if (f.u < (113 << 23)) {
509 f.f += denorm_magic.f;
512 o.x =
static_cast<numext::uint16_t
>(f.u - denorm_magic.u);
514 unsigned int mant_odd = (f.u >> 13) & 1;
523 o.x =
static_cast<numext::uint16_t
>(f.u >> 13);
527 o.x |=
static_cast<numext::uint16_t
>(sign >> 16);
532 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
float half_to_float(__half_raw h) {
533 #if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
534 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
535 return __half2float(h);
536 #elif defined(EIGEN_HAS_FP16_C)
537 return _cvtsh_ss(h.x);
538 #elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
539 return static_cast<float>(h.x);
541 const float32_bits magic = { 113 << 23 };
542 const unsigned int shifted_exp = 0x7c00 << 13;
545 o.u = (h.x & 0x7fff) << 13;
546 unsigned int exp = shifted_exp & o.u;
547 o.u += (127 - 15) << 23;
550 if (exp == shifted_exp) {
551 o.u += (128 - 16) << 23;
552 }
else if (exp == 0) {
557 o.u |= (h.x & 0x8000) << 16;
564 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool (isinf)(
const half& a) {
565 #ifdef EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC
566 return (numext::bit_cast<numext::uint16_t>(a.x) & 0x7fff) == 0x7c00;
568 return (a.x & 0x7fff) == 0x7c00;
571 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool (isnan)(
const half& a) {
572 #if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
573 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
575 #elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
576 return (numext::bit_cast<numext::uint16_t>(a.x) & 0x7fff) > 0x7c00;
578 return (a.x & 0x7fff) > 0x7c00;
581 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool (isfinite)(
const half& a) {
582 return !(isinf EIGEN_NOT_A_MACRO (a)) && !(isnan EIGEN_NOT_A_MACRO (a));
585 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half abs(
const half& a) {
586 #if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
587 return half(vabsh_f16(a.x));
590 result.x = a.x & 0x7FFF;
594 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half exp(
const half& a) {
595 #if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 530) || \
596 defined(EIGEN_HIP_DEVICE_COMPILE)
597 return half(hexp(a));
599 return half(::expf(
float(a)));
602 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half expm1(
const half& a) {
603 return half(numext::expm1(
float(a)));
605 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log(
const half& a) {
606 #if (defined(EIGEN_HAS_CUDA_FP16) && EIGEN_CUDA_SDK_VER >= 80000 && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
607 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
608 return half(::hlog(a));
610 return half(::logf(
float(a)));
613 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log1p(
const half& a) {
614 return half(numext::log1p(
float(a)));
616 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log10(
const half& a) {
617 return half(::log10f(
float(a)));
619 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log2(
const half& a) {
620 return half(
static_cast<float>(EIGEN_LOG2E) * ::logf(
float(a)));
623 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half sqrt(
const half& a) {
624 #if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 530) || \
625 defined(EIGEN_HIP_DEVICE_COMPILE)
626 return half(hsqrt(a));
628 return half(::sqrtf(
float(a)));
631 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half pow(
const half& a,
const half& b) {
632 return half(::powf(
float(a),
float(b)));
634 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half sin(
const half& a) {
635 return half(::sinf(
float(a)));
637 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half cos(
const half& a) {
638 return half(::cosf(
float(a)));
640 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half tan(
const half& a) {
641 return half(::tanf(
float(a)));
643 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half tanh(
const half& a) {
644 return half(::tanhf(
float(a)));
646 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half asin(
const half& a) {
647 return half(::asinf(
float(a)));
649 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half acos(
const half& a) {
650 return half(::acosf(
float(a)));
652 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half floor(
const half& a) {
653 #if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 300) || \
654 defined(EIGEN_HIP_DEVICE_COMPILE)
655 return half(hfloor(a));
657 return half(::floorf(
float(a)));
660 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half rint(
const half& a) {
661 return half(::rintf(
float(a)));
663 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half ceil(
const half& a) {
664 #if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 300) || \
665 defined(EIGEN_HIP_DEVICE_COMPILE)
666 return half(hceil(a));
668 return half(::ceilf(
float(a)));
672 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half (min)(
const half& a,
const half& b) {
673 #if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
674 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
675 return __hlt(b, a) ? b : a;
677 const float f1 =
static_cast<float>(a);
678 const float f2 =
static_cast<float>(b);
679 return f2 < f1 ? b : a;
682 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half (max)(
const half& a,
const half& b) {
683 #if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
684 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
685 return __hlt(a, b) ? b : a;
687 const float f1 =
static_cast<float>(a);
688 const float f2 =
static_cast<float>(b);
689 return f1 < f2 ? b : a;
694 EIGEN_ALWAYS_INLINE std::ostream& operator << (std::ostream& os,
const half& v) {
695 os << static_cast<float>(v);
712 return x + (y-x) *
half(
float(std::rand()) / float(RAND_MAX));
714 static inline half run()
731 RequireInitialization =
false
734 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
static EIGEN_STRONG_INLINE
Eigen::half epsilon() {
735 return half_impl::raw_uint16_to_half(0x0800);
737 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
static EIGEN_STRONG_INLINE
Eigen::half dummy_precision() {
738 return half_impl::raw_uint16_to_half(0x211f);
740 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
static EIGEN_STRONG_INLINE
Eigen::half highest() {
741 return half_impl::raw_uint16_to_half(0x7bff);
743 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
static EIGEN_STRONG_INLINE
Eigen::half lowest() {
744 return half_impl::raw_uint16_to_half(0xfbff);
746 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
static EIGEN_STRONG_INLINE
Eigen::half infinity() {
747 return half_impl::raw_uint16_to_half(0x7c00);
749 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
static EIGEN_STRONG_INLINE
Eigen::half quiet_NaN() {
750 return half_impl::raw_uint16_to_half(0x7e00);
756 #if defined(EIGEN_HAS_GPU_FP16) || defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
757 #pragma pop_macro("EIGEN_CONSTEXPR")
762 #if __cplusplus > 199711L
764 struct hash<
Eigen::half> {
765 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::size_t operator()(
const Eigen::half& a)
const {
766 return static_cast<std::size_t
>(a.x);
776 #if defined(EIGEN_GPU_COMPILE_PHASE)
779 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool(isnan)(
const Eigen::half& h) {
780 return (half_impl::isnan)(h);
784 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool(isinf)(
const Eigen::half& h) {
785 return (half_impl::isinf)(h);
789 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool(isfinite)(
const Eigen::half& h) {
790 return (half_impl::isfinite)(h);
796 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
Eigen::half bit_cast<Eigen::half, uint16_t>(
const uint16_t& src) {
797 return Eigen::half(Eigen::half_impl::raw_uint16_to_half(src));
801 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC uint16_t bit_cast<uint16_t, Eigen::half>(
const Eigen::half& src) {
802 return Eigen::half_impl::raw_half_as_uint16(src);
819 #if (defined(EIGEN_CUDACC) && (!defined(EIGEN_CUDA_ARCH) || EIGEN_CUDA_ARCH >= 300)) \
820 || defined(EIGEN_HIPCC)
822 #if defined(EIGEN_HAS_CUDA_FP16) && EIGEN_CUDA_SDK_VER >= 90000
824 __device__ EIGEN_STRONG_INLINE
Eigen::half __shfl_sync(
unsigned mask,
Eigen::half var,
int srcLane,
int width=warpSize) {
825 return static_cast<Eigen::half>(__shfl_sync(mask,
static_cast<__half
>(var), srcLane, width));
828 __device__ EIGEN_STRONG_INLINE
Eigen::half __shfl_up_sync(
unsigned mask,
Eigen::half var,
unsigned int delta,
int width=warpSize) {
829 return static_cast<Eigen::half>(__shfl_up_sync(mask,
static_cast<__half
>(var), delta, width));
832 __device__ EIGEN_STRONG_INLINE
Eigen::half __shfl_down_sync(
unsigned mask,
Eigen::half var,
unsigned int delta,
int width=warpSize) {
833 return static_cast<Eigen::half>(__shfl_down_sync(mask,
static_cast<__half
>(var), delta, width));
836 __device__ EIGEN_STRONG_INLINE
Eigen::half __shfl_xor_sync(
unsigned mask,
Eigen::half var,
int laneMask,
int width=warpSize) {
837 return static_cast<Eigen::half>(__shfl_xor_sync(mask,
static_cast<__half
>(var), laneMask, width));
840 #else // HIP or CUDA SDK < 9.0
843 const int ivar =
static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
844 return Eigen::numext::bit_cast<Eigen::half>(
static_cast<Eigen::numext::uint16_t
>(__shfl(ivar, srcLane, width)));
847 __device__ EIGEN_STRONG_INLINE
Eigen::half __shfl_up(
Eigen::half var,
unsigned int delta,
int width=warpSize) {
848 const int ivar =
static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
849 return Eigen::numext::bit_cast<Eigen::half>(
static_cast<Eigen::numext::uint16_t
>(__shfl_up(ivar, delta, width)));
852 __device__ EIGEN_STRONG_INLINE
Eigen::half __shfl_down(
Eigen::half var,
unsigned int delta,
int width=warpSize) {
853 const int ivar =
static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
854 return Eigen::numext::bit_cast<Eigen::half>(
static_cast<Eigen::numext::uint16_t
>(__shfl_down(ivar, delta, width)));
857 __device__ EIGEN_STRONG_INLINE
Eigen::half __shfl_xor(
Eigen::half var,
int laneMask,
int width=warpSize) {
858 const int ivar =
static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
859 return Eigen::numext::bit_cast<Eigen::half>(
static_cast<Eigen::numext::uint16_t
>(__shfl_xor(ivar, laneMask, width)));
862 #endif // HIP vs CUDA
866 #if (defined(EIGEN_CUDACC) && (!defined(EIGEN_CUDA_ARCH) || EIGEN_CUDA_ARCH >= 350)) \
867 || defined(EIGEN_HIPCC)
869 return Eigen::half_impl::raw_uint16_to_half(__ldg(
reinterpret_cast<const Eigen::numext::uint16_t*
>(ptr)));
873 #endif // EIGEN_HALF_H