16 #ifdef __SYCL_DEVICE_ONLY__
24 #include <string_view>
25 #include <type_traits>
27 #if !defined(__has_builtin) || !__has_builtin(__builtin_expect)
28 #define __builtin_expect(a, b) (a)
31 #ifdef __SYCL_DEVICE_ONLY__
34 #define __SYCL_CONSTEXPR_HALF constexpr
35 #elif __cpp_lib_bit_cast || \
36 (defined(__has_builtin) && __has_builtin(__builtin_bit_cast))
37 #define __SYCL_CONSTEXPR_HALF constexpr
39 #define __SYCL_CONSTEXPR_HALF
43 inline namespace _V1 {
44 namespace detail::half_impl {
49 namespace ext::intel::esimd::detail {
50 class WrapperElementTypeProxy;
56 const uint32_t Bits = sycl::bit_cast<uint32_t>(Val);
59 const uint16_t Sign = (Bits & 0x80000000) >> 16;
61 const uint32_t Frac32 = Bits & 0x7fffff;
63 const uint8_t Exp32 = (Bits & 0x7f800000) >> 23;
64 const int16_t Exp32Diff = Exp32 - 127;
67 uint16_t Exp16 = 0, Frac16 = 0;
74 Exp16 = Exp32Diff + 15;
76 Frac16 = Frac32 >> 13;
80 uint32_t roundBits = Frac32 & 0x1fff;
81 uint32_t halfway = 0x1000;
82 if (roundBits > halfway)
85 else if (roundBits == halfway)
89 Frac16 = (Frac32 | (uint32_t(1) << 23)) >> (-Exp32Diff - 1);
109 const uint32_t Sign =
static_cast<uint32_t
>(Val & 0x8000) << 16;
111 const uint8_t Exp16 = (Val & 0x7c00) >> 10;
113 uint16_t Frac16 = Val & 0x3ff;
121 Exp32 =
static_cast<uint32_t
>(Exp16) + 112;
131 }
while ((Frac16 & 0x400) != 0x400);
134 Exp32 = 113 - OffSet;
137 uint32_t Frac32 = Frac16 << 13;
141 Bits |= (Exp32 << 23);
143 const float Result = sycl::bit_cast<float>(Bits);
147 namespace half_impl {
168 #ifdef __SYCL_DEVICE_ONLY__
186 #ifndef __SYCL_DEVICE_ONLY__
189 class [[__sycl_detail__::__uses_aspects__(aspect::fp16)]]
half {
196 #ifdef __SYCL_DEVICE_ONLY__
205 #ifdef __SYCL_DEVICE_ONLY__
227 *
this =
operator float() +
static_cast<float>(rhs);
232 *
this =
operator float() -
static_cast<float>(rhs);
237 *
this =
operator float() *
static_cast<float>(rhs);
242 *
this =
operator float() /
static_cast<float>(rhs);
271 #ifdef __SYCL_DEVICE_ONLY__
273 return half(-other.Data);
282 #define OP(op, op_eq) \
283 __SYCL_CONSTEXPR_HALF friend half operator op(const half lhs, \
289 __SYCL_CONSTEXPR_HALF friend double operator op(const half lhs, \
290 const double rhs) { \
295 __SYCL_CONSTEXPR_HALF friend double operator op(const double lhs, \
301 __SYCL_CONSTEXPR_HALF friend float operator op(const half lhs, \
307 __SYCL_CONSTEXPR_HALF friend float operator op(const float lhs, \
313 __SYCL_CONSTEXPR_HALF friend half operator op(const half lhs, \
319 __SYCL_CONSTEXPR_HALF friend half operator op(const int lhs, \
325 __SYCL_CONSTEXPR_HALF friend half operator op(const half lhs, \
331 __SYCL_CONSTEXPR_HALF friend half operator op(const long lhs, \
337 __SYCL_CONSTEXPR_HALF friend half operator op(const half lhs, \
338 const long long rhs) { \
343 __SYCL_CONSTEXPR_HALF friend half operator op(const long long lhs, \
349 __SYCL_CONSTEXPR_HALF friend half operator op(const half &lhs, \
350 const unsigned int &rhs) { \
355 __SYCL_CONSTEXPR_HALF friend half operator op(const unsigned int &lhs, \
361 __SYCL_CONSTEXPR_HALF friend half operator op(const half &lhs, \
362 const unsigned long &rhs) { \
367 __SYCL_CONSTEXPR_HALF friend half operator op(const unsigned long &lhs, \
373 __SYCL_CONSTEXPR_HALF friend half operator op( \
374 const half &lhs, const unsigned long long &rhs) { \
379 __SYCL_CONSTEXPR_HALF friend half operator op(const unsigned long long &lhs, \
394 __SYCL_CONSTEXPR_HALF friend bool operator op(const half &lhs, \
396 return lhs.getFPRep() op rhs.getFPRep(); \
398 __SYCL_CONSTEXPR_HALF friend bool operator op(const half &lhs, \
399 const double &rhs) { \
400 return lhs.getFPRep() op rhs; \
402 __SYCL_CONSTEXPR_HALF friend bool operator op(const double &lhs, \
404 return lhs op rhs.getFPRep(); \
406 __SYCL_CONSTEXPR_HALF friend bool operator op(const half &lhs, \
407 const float &rhs) { \
408 return lhs.getFPRep() op rhs; \
410 __SYCL_CONSTEXPR_HALF friend bool operator op(const float &lhs, \
412 return lhs op rhs.getFPRep(); \
414 __SYCL_CONSTEXPR_HALF friend bool operator op(const half &lhs, \
416 return lhs.getFPRep() op rhs; \
418 __SYCL_CONSTEXPR_HALF friend bool operator op(const int &lhs, \
420 return lhs op rhs.getFPRep(); \
422 __SYCL_CONSTEXPR_HALF friend bool operator op(const half &lhs, \
424 return lhs.getFPRep() op rhs; \
426 __SYCL_CONSTEXPR_HALF friend bool operator op(const long &lhs, \
428 return lhs op rhs.getFPRep(); \
430 __SYCL_CONSTEXPR_HALF friend bool operator op(const half &lhs, \
431 const long long &rhs) { \
432 return lhs.getFPRep() op rhs; \
434 __SYCL_CONSTEXPR_HALF friend bool operator op(const long long &lhs, \
436 return lhs op rhs.getFPRep(); \
438 __SYCL_CONSTEXPR_HALF friend bool operator op(const half &lhs, \
439 const unsigned int &rhs) { \
440 return lhs.getFPRep() op rhs; \
442 __SYCL_CONSTEXPR_HALF friend bool operator op(const unsigned int &lhs, \
444 return lhs op rhs.getFPRep(); \
446 __SYCL_CONSTEXPR_HALF friend bool operator op(const half &lhs, \
447 const unsigned long &rhs) { \
448 return lhs.getFPRep() op rhs; \
450 __SYCL_CONSTEXPR_HALF friend bool operator op(const unsigned long &lhs, \
452 return lhs op rhs.getFPRep(); \
454 __SYCL_CONSTEXPR_HALF friend bool operator op( \
455 const half &lhs, const unsigned long long &rhs) { \
456 return lhs.getFPRep() op rhs; \
458 __SYCL_CONSTEXPR_HALF friend bool operator op(const unsigned long long &lhs, \
460 return lhs op rhs.getFPRep(); \
472 #ifdef __SYCL_DEVICE_ONLY__
474 return static_cast<float>(Data);
483 O << static_cast<float>(rhs);
488 float ValFloat = 0.0f;
494 template <
typename Key>
friend struct std::hash;
496 friend class sycl::ext::intel::esimd::detail::WrapperElementTypeProxy;
502 #ifdef __SYCL_DEVICE_ONLY__
508 #ifndef __SYCL_DEVICE_ONLY__
512 constexpr
explicit half(RawHostHalfToken X) : Data(X.Value) {}
520 #ifndef __SYCL_DEVICE_ONLY__
536 return static_cast<float>(val);
550 return hash<uint16_t>{}(
reinterpret_cast<const uint16_t &
>(Key));
559 static constexpr
bool is_specialized =
true;
560 static constexpr
bool is_signed =
true;
561 static constexpr
bool is_integer =
false;
562 static constexpr
bool is_exact =
false;
563 static constexpr
bool has_infinity =
true;
564 static constexpr
bool has_quiet_NaN =
true;
565 static constexpr
bool has_signaling_NaN =
true;
566 static constexpr float_denorm_style has_denorm = denorm_present;
567 static constexpr
bool has_denorm_loss =
false;
568 static constexpr
bool tinyness_before =
false;
569 static constexpr
bool traps =
false;
570 static constexpr
int max_exponent10 = 4;
571 static constexpr
int max_exponent = 16;
572 static constexpr
int min_exponent10 = -4;
573 static constexpr
int min_exponent = -13;
574 static constexpr
int radix = 2;
575 static constexpr
int max_digits10 = 5;
576 static constexpr
int digits = 11;
577 static constexpr
bool is_bounded =
true;
578 static constexpr
int digits10 = 3;
579 static constexpr
bool is_modulo =
false;
580 static constexpr
bool is_iec559 =
true;
581 static constexpr float_round_style round_style = round_to_nearest;
584 return 6.103515625e-05f;
596 return 9.765625e-04f;
604 #ifdef __SYCL_DEVICE_ONLY__
605 return __builtin_huge_valf();
608 static_cast<uint16_t
>(0x7C00));
613 return __builtin_nanf(
"");
617 return __builtin_nansf(
"");
627 #undef __SYCL_CONSTEXPR_HALF
628 #undef _CPP14_CONSTEXPR
friend std::istream & operator>>(std::istream &I, sycl::half &rhs)
__SYCL_CONSTEXPR_HALF half & operator/=(const half &rhs)
__SYCL_CONSTEXPR_HALF half & operator+=(const half &rhs)
__SYCL_CONSTEXPR_HALF half & operator-=(const half &rhs)
__SYCL_CONSTEXPR_HALF half operator--(int)
__SYCL_CONSTEXPR_HALF half & operator*=(const half &rhs)
__SYCL_CONSTEXPR_HALF friend half operator-(const half other)
constexpr friend half CreateHostHalfRaw(uint16_t X)
constexpr half(half &&)=default
constexpr half(const half &)=default
constexpr half & operator=(const half &rhs)=default
__SYCL_CONSTEXPR_HALF half & operator--()
__SYCL_CONSTEXPR_HALF half(const float &rhs)
friend std::ostream & operator<<(std::ostream &O, sycl::half const &rhs)
__SYCL_CONSTEXPR_HALF half & operator++()
__SYCL_CONSTEXPR_HALF half operator++(int)
#define __builtin_expect(a, b)
#define __SYCL_CONSTEXPR_HALF
constexpr half CreateHostHalfRaw(uint16_t X)
T cast_if_host_half(T val)
__SYCL_CONSTEXPR_HALF uint16_t float2Half(const float &Val)
__SYCL_CONSTEXPR_HALF float half2Float(const uint16_t &Val)
sycl::detail::half_impl::half half
_Abi const simd< _Tp, _Abi > & noexcept
size_t operator()(sycl::half const &Key) const noexcept
static constexpr const sycl::half infinity() noexcept
static __SYCL_CONSTEXPR_HALF const sycl::half round_error() noexcept
static __SYCL_CONSTEXPR_HALF const sycl::half quiet_NaN() noexcept
static __SYCL_CONSTEXPR_HALF const sycl::half lowest() noexcept
static __SYCL_CONSTEXPR_HALF const sycl::half denorm_min() noexcept
static __SYCL_CONSTEXPR_HALF const sycl::half epsilon() noexcept
static __SYCL_CONSTEXPR_HALF const sycl::half signaling_NaN() noexcept
constexpr RawHostHalfToken(uint16_t Val)