@@ -33,6 +33,7 @@
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#endif
|
||||
|
||||
#include "cutlass/cutlass.h"
|
||||
@@ -76,7 +77,13 @@ struct alignas(2) bfloat16_t {
|
||||
asm("cvt.rn.bf16.f32 %0, %1;\n" : "=h"(storage) : "f"(x));
|
||||
|
||||
#else
|
||||
uint32_t bits = reinterpret_cast<uint32_t &>(x);
|
||||
uint32_t bits;
|
||||
|
||||
#if defined(__CUDA_ARCH__)
|
||||
bits = reinterpret_cast<uint32_t &>(x);
|
||||
#else
|
||||
std::memcpy(&bits, &x, sizeof(bits));
|
||||
#endif
|
||||
|
||||
if ((bits & 0x7f800000) != 0x7f800000) {
|
||||
|
||||
@@ -106,14 +113,28 @@ struct alignas(2) bfloat16_t {
|
||||
CUTLASS_HOST_DEVICE
|
||||
explicit bfloat16_t(int x) {
|
||||
float flt = static_cast<float>(x);
|
||||
storage = uint16_t(reinterpret_cast<uint32_t const &>(flt) >> 16);
|
||||
uint32_t bits;
|
||||
|
||||
#if defined(__CUDA_ARCH__)
|
||||
bits = reinterpret_cast<uint32_t &>(flt);
|
||||
#else
|
||||
std::memcpy(&bits, &flt, sizeof(bits));
|
||||
#endif
|
||||
|
||||
storage = uint16_t(bits >> 16);
|
||||
}
|
||||
|
||||
/// Converts to float
|
||||
CUTLASS_HOST_DEVICE
|
||||
operator float() const {
|
||||
unsigned bits = (unsigned(storage) << 16);
|
||||
#if defined(__CUDA_ARCH__)
|
||||
return reinterpret_cast<float const &>(bits);
|
||||
#else
|
||||
float flt;
|
||||
std::memcpy(&flt, &bits, sizeof(flt));
|
||||
return flt;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// Converts to float
|
||||
@@ -237,11 +258,22 @@ cutlass::bfloat16_t sqrt(cutlass::bfloat16_t const& h) {
|
||||
CUTLASS_HOST_DEVICE
|
||||
bfloat16_t copysign(bfloat16_t const& a, bfloat16_t const& b) {
|
||||
|
||||
uint16_t a_mag = (reinterpret_cast<uint16_t const &>(a) & 0x7fff);
|
||||
uint16_t b_sign = (reinterpret_cast<uint16_t const &>(b) & 0x8000);
|
||||
uint16_t a_bits;
|
||||
uint16_t b_bits;
|
||||
|
||||
#if defined(__CUDA_ARCH__)
|
||||
a_bits = reinterpret_cast<uint16_t const &>(a);
|
||||
b_bits = reinterpret_cast<uint16_t const &>(b);
|
||||
#else
|
||||
std::memcpy(&a_bits, &a, sizeof(a_bits));
|
||||
std::memcpy(&b_bits, &b, sizeof(b_bits));
|
||||
#endif
|
||||
|
||||
uint16_t a_mag = (a_bits & 0x7fff);
|
||||
uint16_t b_sign = (b_bits & 0x8000);
|
||||
uint16_t result = (a_mag | b_sign);
|
||||
|
||||
return reinterpret_cast<bfloat16_t const &>(result);
|
||||
return bfloat16_t::bitcast(result);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user