in gloo/types.h [96:204]
struct alignas(2) float16 {
uint16_t x;
float16() : x(0) {}
float16(const float16 &) = default;
explicit float16(int val) {
float16 res = cpu_float2half_rn(static_cast<float>(val));
x = res.x;
}
explicit float16(unsigned long val) {
float16 res = cpu_float2half_rn(static_cast<float>(val));
x = res.x;
}
explicit float16(unsigned long long val) {
float16 res = cpu_float2half_rn(static_cast<float>(val));
x = res.x;
}
explicit float16(double val) {
float16 res = cpu_float2half_rn(static_cast<float>(val));
x = res.x;
}
float16& operator=(const int& rhs) {
float16 res = cpu_float2half_rn(static_cast<float>(rhs));
x = res.x;
return *this;
}
float16& operator=(const float16& rhs) {
if (rhs != *this) {
x = rhs.x;
}
return *this;
}
bool operator==(const float16& rhs) const {
return x == rhs.x;
}
bool operator!=(const float16& rhs) const {
return !(*this == rhs.x);
}
bool operator==(const int& rhs) const {
float16 res = cpu_float2half_rn(static_cast<float>(rhs));
return x == res.x;
}
bool operator==(const unsigned long& rhs) const {
float16 res = cpu_float2half_rn(static_cast<float>(rhs));
return x == res.x;
}
bool operator==(const double& rhs) const {
float16 res = cpu_float2half_rn(static_cast<float>(rhs));
return x == res.x;
}
#ifdef __CUDA_ARCH__
float16(half h) {
#if CUDA_VERSION >= 9000
x = reinterpret_cast<__half_raw*>(&h)->x;
#else
x = h.x;
#endif // CUDA_VERSION
}
// half and float16 are supposed to have identical representation so implicit
// conversion should be fine
/* implicit */
operator half() const {
#if CUDA_VERSION >= 9000
__half_raw hr;
hr.x = this->x;
return half(hr);
#else
return (half) * this;
#endif // CUDA_VERSION
}
#endif // __CUDA_ARCH
float16& operator+=(const float16& rhs) {
float r = cpu_half2float(*this) + cpu_half2float(rhs);
*this = cpu_float2half_rn(r);
return *this;
}
float16& operator-=(const float16& rhs) {
float r = cpu_half2float(*this) - cpu_half2float(rhs);
*this = cpu_float2half_rn(r);
return *this;
}
float16& operator*=(const float16& rhs) {
float r = cpu_half2float(*this) * cpu_half2float(rhs);
*this = cpu_float2half_rn(r);
return *this;
}
float16& operator/=(const float16& rhs) {
float r = cpu_half2float(*this) / cpu_half2float(rhs);
*this = cpu_float2half_rn(r);
return *this;
}
};