Loading...
Searching...
No Matches
Float16.h
Go to the documentation of this file.
#ifndef HALIDE_FLOAT16_H
#define HALIDE_FLOAT16_H
#include "runtime/HalideRuntime.h"
#include <cstdint>
#include <string>
namespace Halide {
/** Class that provides a type that implements half precision
* floating point (IEEE754 2008 binary16) in software.
*
* This type is enforced to be 16-bits wide and maintains no state
* other than the raw IEEE754 binary16 bits so that it can passed
* to code that checks a type's size and used for halide_buffer_t allocation.
* */
struct float16_t {
static const int mantissa_bits = 10;
static const uint16_t sign_mask = 0x8000;
static const uint16_t exponent_mask = 0x7c00;
static const uint16_t mantissa_mask = 0x03ff;
/// \name Constructors
/// @{
/** Construct from a float, double, or int using
* round-to-nearest-ties-to-even. Out-of-range values become +/-
* infinity.
*/
// @{
explicit float16_t(float value);
explicit float16_t(double value);
explicit float16_t(int value);
// @}
/** Construct a float16_t with the bits initialised to 0. This represents
* positive zero.*/
float16_t() = default;
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
/** Construct a float16_t from compiler's built-in _Float16 type. */
explicit float16_t(_Float16 value) {
memcpy(&data, &value, sizeof(_Float16));
}
#endif
/// @}
// Use explicit to avoid accidently raising the precision
/** Cast to float */
explicit operator float() const;
/** Cast to double */
explicit operator double() const;
/** Cast to int */
explicit operator int() const;
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
/** Cast to compiler's built-in _Float16 type. */
explicit operator _Float16() const {
_Float16 result;
memcpy(&result, &data, sizeof(_Float16));
return result;
}
#endif
/** Get a new float16_t that represents a special value */
// @{
static float16_t make_zero();
static float16_t make_negative_zero();
static float16_t make_infinity();
static float16_t make_negative_infinity();
static float16_t make_nan();
// @}
/** Get a new float16_t with the given raw bits
*
* \param bits The bits conformant to IEEE754 binary16
*/
static float16_t make_from_bits(uint16_t bits);
/** Return a new float16_t with a negated sign bit*/
float16_t operator-() const;
/** Arithmetic operators. */
// @{
float16_t operator+(float16_t rhs) const;
float16_t operator-(float16_t rhs) const;
float16_t operator*(float16_t rhs) const;
float16_t operator/(float16_t rhs) const;
float16_t operator+=(float16_t rhs) {
return (*this = *this + rhs);
}
float16_t operator-=(float16_t rhs) {
return (*this = *this - rhs);
}
float16_t operator*=(float16_t rhs) {
return (*this = *this * rhs);
}
float16_t operator/=(float16_t rhs) {
return (*this = *this / rhs);
}
// @}
/** Comparison operators */
// @{
bool operator==(float16_t rhs) const;
bool operator!=(float16_t rhs) const {
return !(*this == rhs);
}
bool operator>(float16_t rhs) const;
bool operator<(float16_t rhs) const;
bool operator>=(float16_t rhs) const {
return (*this > rhs) || (*this == rhs);
}
bool operator<=(float16_t rhs) const {
return (*this < rhs) || (*this == rhs);
}
// @}
/** Properties */
// @{
bool is_nan() const;
bool is_infinity() const;
bool is_negative() const;
bool is_zero() const;
// @}
/** Returns the bits that represent this float16_t.
*
* An alternative method to access the bits is to cast a pointer
* to this instance as a pointer to a uint16_t.
**/
uint16_t to_bits() const;
private:
// The raw bits.
uint16_t data = 0;
};
static_assert(sizeof(float16_t) == 2, "float16_t should occupy two bytes");
} // namespace Halide
template<>
HALIDE_ALWAYS_INLINE constexpr halide_type_t halide_type_of<Halide::float16_t>() {
return halide_type_t(halide_type_float, 16);
}
namespace Halide {
/** Class that provides a type that implements half precision
* floating point using the bfloat16 format.
*
* This type is enforced to be 16-bits wide and maintains no state
* other than the raw bits so that it can passed to code that checks
* a type's size and used for halide_buffer_t allocation. */
struct bfloat16_t {
static const int mantissa_bits = 7;
static const uint16_t sign_mask = 0x8000;
static const uint16_t exponent_mask = 0x7f80;
static const uint16_t mantissa_mask = 0x007f;
static const bfloat16_t zero, negative_zero, infinity, negative_infinity, nan;
/// \name Constructors
/// @{
/** Construct from a float, double, or int using
* round-to-nearest-ties-to-even. Out-of-range values become +/-
* infinity.
*/
// @{
explicit bfloat16_t(float value);
explicit bfloat16_t(double value);
explicit bfloat16_t(int value);
// @}
/** Construct a bfloat16_t with the bits initialised to 0. This represents
* positive zero.*/
bfloat16_t() = default;
/// @}
// Use explicit to avoid accidently raising the precision
/** Cast to float */
explicit operator float() const;
/** Cast to double */
explicit operator double() const;
/** Cast to int */
explicit operator int() const;
/** Get a new bfloat16_t that represents a special value */
// @{
static bfloat16_t make_zero();
static bfloat16_t make_negative_zero();
static bfloat16_t make_infinity();
static bfloat16_t make_negative_infinity();
static bfloat16_t make_nan();
// @}
/** Get a new bfloat16_t with the given raw bits
*
* \param bits The bits conformant to IEEE754 binary16
*/
static bfloat16_t make_from_bits(uint16_t bits);
/** Return a new bfloat16_t with a negated sign bit*/
bfloat16_t operator-() const;
/** Arithmetic operators. */
// @{
bfloat16_t operator+(bfloat16_t rhs) const;
bfloat16_t operator-(bfloat16_t rhs) const;
bfloat16_t operator*(bfloat16_t rhs) const;
bfloat16_t operator/(bfloat16_t rhs) const;
bfloat16_t operator+=(bfloat16_t rhs) {
return (*this = *this + rhs);
}
bfloat16_t operator-=(bfloat16_t rhs) {
return (*this = *this - rhs);
}
bfloat16_t operator*=(bfloat16_t rhs) {
return (*this = *this * rhs);
}
bfloat16_t operator/=(bfloat16_t rhs) {
return (*this = *this / rhs);
}
// @}
/** Comparison operators */
// @{
bool operator==(bfloat16_t rhs) const;
bool operator!=(bfloat16_t rhs) const {
return !(*this == rhs);
}
bool operator>(bfloat16_t rhs) const;
bool operator<(bfloat16_t rhs) const;
bool operator>=(bfloat16_t rhs) const {
return (*this > rhs) || (*this == rhs);
}
bool operator<=(bfloat16_t rhs) const {
return (*this < rhs) || (*this == rhs);
}
// @}
/** Properties */
// @{
bool is_nan() const;
bool is_infinity() const;
bool is_negative() const;
bool is_zero() const;
// @}
/** Returns the bits that represent this bfloat16_t.
*
* An alternative method to access the bits is to cast a pointer
* to this instance as a pointer to a uint16_t.
**/
uint16_t to_bits() const;
private:
// The raw bits.
uint16_t data = 0;
};
static_assert(sizeof(bfloat16_t) == 2, "bfloat16_t should occupy two bytes");
} // namespace Halide
template<>
HALIDE_ALWAYS_INLINE constexpr halide_type_t halide_type_of<Halide::bfloat16_t>() {
return halide_type_t(halide_type_bfloat, 16);
}
#endif
halide_type_of< Halide::bfloat16_t >
HALIDE_ALWAYS_INLINE constexpr halide_type_t halide_type_of< Halide::bfloat16_t >()
Definition Float16.h:273
halide_type_of< Halide::float16_t >
HALIDE_ALWAYS_INLINE constexpr halide_type_t halide_type_of< Halide::float16_t >()
Definition Float16.h:146
HalideRuntime.h
This file declares the routines used by Halide internally in its runtime.
halide_type_float
@ halide_type_float
IEEE floating point numbers.
Definition HalideRuntime.h:483
halide_type_bfloat
@ halide_type_bfloat
floating point numbers in the bfloat format
Definition HalideRuntime.h:485
HALIDE_ALWAYS_INLINE
#define HALIDE_ALWAYS_INLINE
Definition HalideRuntime.h:49
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition AbstractGenerator.h:19
uint16_t
unsigned __INT16_TYPE__ uint16_t
Definition runtime_internal.h:27
memcpy
void * memcpy(void *s1, const void *s2, size_t n)
Halide::bfloat16_t
Class that provides a type that implements half precision floating point using the bfloat16 format.
Definition Float16.h:158
Halide::bfloat16_t::operator-=
bfloat16_t operator-=(bfloat16_t rhs)
Definition Float16.h:221
Halide::bfloat16_t::operator>
bool operator>(bfloat16_t rhs) const
Halide::bfloat16_t::operator<=
bool operator<=(bfloat16_t rhs) const
Definition Float16.h:243
Halide::bfloat16_t::zero
static const bfloat16_t zero
Definition Float16.h:165
Halide::bfloat16_t::operator-
bfloat16_t operator-() const
Return a new bfloat16_t with a negated sign bit.
Halide::bfloat16_t::operator<
bool operator<(bfloat16_t rhs) const
Halide::bfloat16_t::exponent_mask
static const uint16_t exponent_mask
Definition Float16.h:162
Halide::bfloat16_t::operator*
bfloat16_t operator*(bfloat16_t rhs) const
Halide::bfloat16_t::is_infinity
bool is_infinity() const
Halide::bfloat16_t::sign_mask
static const uint16_t sign_mask
Definition Float16.h:161
Halide::bfloat16_t::to_bits
uint16_t to_bits() const
Returns the bits that represent this bfloat16_t.
Halide::bfloat16_t::infinity
static const bfloat16_t infinity
Definition Float16.h:165
Halide::bfloat16_t::mantissa_mask
static const uint16_t mantissa_mask
Definition Float16.h:163
Halide::bfloat16_t::operator==
bool operator==(bfloat16_t rhs) const
Comparison operators.
Halide::bfloat16_t::nan
static const bfloat16_t nan
Definition Float16.h:165
Halide::bfloat16_t::operator+
bfloat16_t operator+(bfloat16_t rhs) const
Arithmetic operators.
Halide::bfloat16_t::operator*=
bfloat16_t operator*=(bfloat16_t rhs)
Definition Float16.h:224
Halide::bfloat16_t::negative_zero
static const bfloat16_t negative_zero
Definition Float16.h:165
Halide::bfloat16_t::operator-
bfloat16_t operator-(bfloat16_t rhs) const
Halide::bfloat16_t::operator/=
bfloat16_t operator/=(bfloat16_t rhs)
Definition Float16.h:227
Halide::bfloat16_t::is_negative
bool is_negative() const
Halide::bfloat16_t::bfloat16_t
bfloat16_t()=default
Construct a bfloat16_t with the bits initialised to 0.
Halide::bfloat16_t::operator/
bfloat16_t operator/(bfloat16_t rhs) const
Halide::bfloat16_t::make_zero
static bfloat16_t make_zero()
Get a new bfloat16_t that represents a special value.
Halide::bfloat16_t::make_negative_zero
static bfloat16_t make_negative_zero()
Halide::bfloat16_t::mantissa_bits
static const int mantissa_bits
Definition Float16.h:160
Halide::bfloat16_t::make_infinity
static bfloat16_t make_infinity()
Halide::bfloat16_t::bfloat16_t
bfloat16_t(float value)
Construct from a float, double, or int using round-to-nearest-ties-to-even.
Halide::bfloat16_t::make_from_bits
static bfloat16_t make_from_bits(uint16_t bits)
Get a new bfloat16_t with the given raw bits.
Halide::bfloat16_t::is_zero
bool is_zero() const
Halide::bfloat16_t::bfloat16_t
bfloat16_t(int value)
Construct from a float, double, or int using round-to-nearest-ties-to-even.
Halide::bfloat16_t::is_nan
bool is_nan() const
Properties.
Halide::bfloat16_t::make_negative_infinity
static bfloat16_t make_negative_infinity()
Halide::bfloat16_t::bfloat16_t
bfloat16_t(double value)
Construct from a float, double, or int using round-to-nearest-ties-to-even.
Halide::bfloat16_t::operator!=
bool operator!=(bfloat16_t rhs) const
Definition Float16.h:235
Halide::bfloat16_t::negative_infinity
static const bfloat16_t negative_infinity
Definition Float16.h:165
Halide::bfloat16_t::operator+=
bfloat16_t operator+=(bfloat16_t rhs)
Definition Float16.h:218
Halide::bfloat16_t::operator>=
bool operator>=(bfloat16_t rhs) const
Definition Float16.h:240
Halide::bfloat16_t::make_nan
static bfloat16_t make_nan()
Halide::float16_t
Class that provides a type that implements half precision floating point (IEEE754 2008 binary16) in s...
Definition Float16.h:17
Halide::float16_t::make_infinity
static float16_t make_infinity()
Halide::float16_t::operator/
float16_t operator/(float16_t rhs) const
Halide::float16_t::is_negative
bool is_negative() const
Halide::float16_t::float16_t
float16_t(double value)
Construct from a float, double, or int using round-to-nearest-ties-to-even.
Halide::float16_t::sign_mask
static const uint16_t sign_mask
Definition Float16.h:20
Halide::float16_t::operator+
float16_t operator+(float16_t rhs) const
Arithmetic operators.
Halide::float16_t::is_zero
bool is_zero() const
Halide::float16_t::float16_t
float16_t(int value)
Construct from a float, double, or int using round-to-nearest-ties-to-even.
Halide::float16_t::operator>=
bool operator>=(float16_t rhs) const
Definition Float16.h:113
Halide::float16_t::make_zero
static float16_t make_zero()
Get a new float16_t that represents a special value.
Halide::float16_t::to_bits
uint16_t to_bits() const
Returns the bits that represent this float16_t.
Halide::float16_t::operator<
bool operator<(float16_t rhs) const
Halide::float16_t::mantissa_mask
static const uint16_t mantissa_mask
Definition Float16.h:22
Halide::float16_t::operator==
bool operator==(float16_t rhs) const
Comparison operators.
Halide::float16_t::make_negative_zero
static float16_t make_negative_zero()
Halide::float16_t::make_from_bits
static float16_t make_from_bits(uint16_t bits)
Get a new float16_t with the given raw bits.
Halide::float16_t::operator/=
float16_t operator/=(float16_t rhs)
Definition Float16.h:100
Halide::float16_t::make_nan
static float16_t make_nan()
Halide::float16_t::float16_t
float16_t(float value)
Construct from a float, double, or int using round-to-nearest-ties-to-even.
Halide::float16_t::float16_t
float16_t()=default
Construct a float16_t with the bits initialised to 0.
Halide::float16_t::exponent_mask
static const uint16_t exponent_mask
Definition Float16.h:21
Halide::float16_t::operator-
float16_t operator-(float16_t rhs) const
Halide::float16_t::is_nan
bool is_nan() const
Properties.
Halide::float16_t::make_negative_infinity
static float16_t make_negative_infinity()
Halide::float16_t::mantissa_bits
static const int mantissa_bits
Definition Float16.h:19
Halide::float16_t::is_infinity
bool is_infinity() const
Halide::float16_t::operator-=
float16_t operator-=(float16_t rhs)
Definition Float16.h:94
Halide::float16_t::operator*
float16_t operator*(float16_t rhs) const
Halide::float16_t::operator-
float16_t operator-() const
Return a new float16_t with a negated sign bit.
Halide::float16_t::operator!=
bool operator!=(float16_t rhs) const
Definition Float16.h:108
Halide::float16_t::operator<=
bool operator<=(float16_t rhs) const
Definition Float16.h:116
Halide::float16_t::operator>
bool operator>(float16_t rhs) const
Halide::float16_t::operator*=
float16_t operator*=(float16_t rhs)
Definition Float16.h:97
Halide::float16_t::operator+=
float16_t operator+=(float16_t rhs)
Definition Float16.h:91
halide_type_t
A runtime tag for a type in the halide type system.
Definition HalideRuntime.h:503