lion03 / thrust

Automatically exported from code.google.com/p/thrust
Apache License 2.0
0 stars 0 forks source link

add a float16_t data type for half-float support #259

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
The half-float [1] type would interoperate with standard 32-bit and 64-bit 
floats.  Also, to avoid redundant conversions the arithmetic operators should 
yield a float32_t result.  This behavior is consistent with the use of 
half-floats as a compressed storage format.

This enhancement would likely depend on issue #216.

class float16_t
{
  unsigned short s;

  public:

  // constructors
  __host__ __device__
  float16_t()
    : s(float32_t()) {}

  __host__ __device__
  float16_t(const float32_t& f)
    : s(__float2half_rn(f)) {}

  // arithmetic
  __host__ __device__
  float32_t operator+(const float16_t& f) const
  {
    // note: return a float
    return static_cast<float32_t>(*this) + static_cast<float32_t>(f);
  }

  // cast to float
  __host__ __device__
  operator float32_t(void) const
  {
    return __half2float(s);
  }
};

[1] http://en.wikipedia.org/wiki/Half_precision_floating-point_format

Original issue reported on code.google.com by wnbell on 27 Oct 2010 at 3:03

GoogleCodeExporter commented 8 years ago
Forwarded to https://github.com/thrust/thrust/issues/91

Original comment by jaredhoberock on 7 May 2012 at 9:55