MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / half2int

Function half2int

src/backend/common/half.hpp:864–917  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

862/// \param value The value to convert to integer
863template<std::float_round_style R, bool E, typename T>
864AF_CONSTEXPR T half2int(native_half_t value) {
865#ifdef __CUDA_ARCH__
866 AF_IF_CONSTEXPR(std::is_same<T, short>::value ||
867 std::is_same<T, char>::value ||
868 std::is_same<T, signed char>::value ||
869 std::is_same<T, unsigned char>::value) {
870 return __half2short_rn(value);
871 }
872 else AF_IF_CONSTEXPR(std::is_same<T, unsigned short>::value) {
873 return __half2ushort_rn(value);
874 }
875 else AF_IF_CONSTEXPR(std::is_same<T, long long>::value) {
876 return __half2ll_rn(value);
877 }
878 else AF_IF_CONSTEXPR(std::is_same<T, unsigned long long>::value) {
879 return __half2ull_rn(value);
880 }
881 else AF_IF_CONSTEXPR(std::is_same<T, int>::value) {
882 return __half2int_rn(value);
883 }
884 else {
885 return __half2uint_rn(value);
886 }
887#elif defined(AF_ONEAPI)
888 return static_cast<T>(value);
889#else
890 static_assert(std::is_integral<T>::value,
891 "half to int conversion only supports builtin integer types");
892 unsigned int e = value & 0x7FFF;
893 if (e >= 0x7C00)
894 return (value & 0x8000) ? std::numeric_limits<T>::min()
895 : std::numeric_limits<T>::max();
896 if (e < 0x3800) {
897 AF_IF_CONSTEXPR(R == std::round_toward_infinity)
898 return T(~(value >> 15) & (e != 0));
899 else AF_IF_CONSTEXPR(R == std::round_toward_neg_infinity) return -T(
900 value > 0x8000);
901 return T();
902 }
903 unsigned int m = (value & 0x3FF) | 0x400;
904 e >>= 10;
905 if (e < 25) {
906 AF_IF_CONSTEXPR(R == std::round_to_nearest)
907 m += (1 << (24 - e)) - (~(m >> (25 - e)) & E);
908 else AF_IF_CONSTEXPR(R == std::round_toward_infinity) m +=
909 ((value >> 15) - 1) & ((1 << (25 - e)) - 1U);
910 else AF_IF_CONSTEXPR(R == std::round_toward_neg_infinity) m +=
911 -(value >> 15) & ((1 << (25 - e)) - 1U);
912 m >>= 25 - e;
913 } else
914 m <<= e - 25;
915 return (value & 0x8000) ? -static_cast<T>(m) : static_cast<T>(m);
916#endif
917}
918
919namespace internal {
920/// Tag type for binary construction.

Callers

nothing calls this directly

Calls 3

AF_IF_CONSTEXPRFunction · 0.85
minFunction · 0.50
maxFunction · 0.50

Tested by

no test coverage detected