ArmNN
 20.02
BFloat16 Class Reference

#include <BFloat16.hpp>

Public Member Functions

 BFloat16 ()
 
 BFloat16 (uint16_t v)
 
 BFloat16 (float v)
 
 operator float () const
 
BFloat16operator= (const BFloat16 &other)
 
BFloat16operator= (float v)
 
bool operator== (const BFloat16 &r) const
 
float ToFloat32 () const
 
uint16_t Val () const
 

Static Public Member Functions

static BFloat16 Float32ToBFloat16 (const float v)
 
static BFloat16 Max ()
 
static BFloat16 Nan ()
 
static BFloat16 Inf ()
 

Detailed Description

Definition at line 14 of file BFloat16.hpp.

Constructor & Destructor Documentation

◆ BFloat16() [1/3]

BFloat16 ( )
inline

Definition at line 17 of file BFloat16.hpp.

Referenced by BFloat16::Inf(), BFloat16::Max(), and BFloat16::Nan().

18  : m_Value(0)
19  {}

◆ BFloat16() [2/3]

BFloat16 ( uint16_t  v)
inlineexplicit

Definition at line 21 of file BFloat16.hpp.

22  : m_Value(v)
23  {}

◆ BFloat16() [3/3]

BFloat16 ( float  v)
inlineexplicit

Definition at line 25 of file BFloat16.hpp.

References BFloat16::Float32ToBFloat16(), and BFloat16::Val().

26  {
27  m_Value = Float32ToBFloat16(v).Val();
28  }
uint16_t Val() const
Definition: BFloat16.hpp:94
static BFloat16 Float32ToBFloat16(const float v)
Definition: BFloat16.hpp:52

Member Function Documentation

◆ Float32ToBFloat16()

static BFloat16 Float32ToBFloat16 ( const float  v)
inlinestatic

Definition at line 52 of file BFloat16.hpp.

References armnn::error, and BFloat16::Nan().

Referenced by BFloat16::BFloat16(), BOOST_AUTO_TEST_CASE(), and BFloat16::operator=().

53  {
54  if (std::isnan(v))
55  {
56  return Nan();
57  }
58  else
59  {
60  // Round value to the nearest even
61  // Float32
62  // S EEEEEEEE MMMMMMLRMMMMMMMMMMMMMMM
63  // BFloat16
64  // S EEEEEEEE MMMMMML
65  // LSB (L): Least significat bit of BFloat16 (last bit of the Mantissa of BFloat16)
66  // R: Rounding bit
67  // LSB = 0, R = 0 -> round down
68  // LSB = 1, R = 0 -> round down
69  // LSB = 0, R = 1, all the rest = 0 -> round down
70  // LSB = 1, R = 1 -> round up
71  // LSB = 0, R = 1 -> round up
72  const uint32_t* u32 = reinterpret_cast<const uint32_t*>(&v);
73  uint16_t u16 = static_cast<uint16_t>(*u32 >> 16u);
74  // Mark the LSB
75  const uint16_t lsb = u16 & 0x0001;
76  // Mark the error to be truncate (the rest of 16 bits of FP32)
77  const uint16_t error = static_cast<const uint16_t>((*u32 & 0x0000FFFF));
78  if ((error > 0x8000 || (error == 0x8000 && lsb == 1)))
79  {
80  u16++;
81  }
82  BFloat16 b(u16);
83  return b;
84  }
85  }
static BFloat16 Nan()
Definition: BFloat16.hpp:105

◆ Inf()

static BFloat16 Inf ( )
inlinestatic

Definition at line 111 of file BFloat16.hpp.

References BFloat16::BFloat16().

Referenced by BOOST_AUTO_TEST_CASE().

112  {
113  uint16_t infVal = 0x7F80;
114  return BFloat16(infVal);
115  }

◆ Max()

static BFloat16 Max ( )
inlinestatic

Definition at line 99 of file BFloat16.hpp.

References BFloat16::BFloat16().

100  {
101  uint16_t max = 0x7F7F;
102  return BFloat16(max);
103  }

◆ Nan()

static BFloat16 Nan ( )
inlinestatic

Definition at line 105 of file BFloat16.hpp.

References BFloat16::BFloat16().

Referenced by BOOST_AUTO_TEST_CASE(), and BFloat16::Float32ToBFloat16().

106  {
107  uint16_t nan = 0x7FC0;
108  return BFloat16(nan);
109  }

◆ operator float()

operator float ( ) const
inline

Definition at line 30 of file BFloat16.hpp.

References BFloat16::ToFloat32().

31  {
32  return ToFloat32();
33  }
float ToFloat32() const
Definition: BFloat16.hpp:87

◆ operator=() [1/2]

BFloat16& operator= ( const BFloat16 other)
inline

Definition at line 35 of file BFloat16.hpp.

References BFloat16::Val().

36  {
37  m_Value = other.Val();
38  return *this;
39  }

◆ operator=() [2/2]

BFloat16& operator= ( float  v)
inline

Definition at line 41 of file BFloat16.hpp.

References BFloat16::Float32ToBFloat16(), and BFloat16::Val().

42  {
43  m_Value = Float32ToBFloat16(v).Val();
44  return *this;
45  }
uint16_t Val() const
Definition: BFloat16.hpp:94
static BFloat16 Float32ToBFloat16(const float v)
Definition: BFloat16.hpp:52

◆ operator==()

bool operator== ( const BFloat16 r) const
inline

Definition at line 47 of file BFloat16.hpp.

References BFloat16::Val().

48  {
49  return m_Value == r.Val();
50  }

◆ ToFloat32()

float ToFloat32 ( ) const
inline

Definition at line 87 of file BFloat16.hpp.

Referenced by BOOST_AUTO_TEST_CASE(), FloatingPointConverter::ConvertBFloat16ToFloat32(), BFloat16::operator float(), and armnn::operator<<().

88  {
89  const uint32_t u32 = static_cast<const uint32_t>(m_Value << 16u);
90  const float* f32 = reinterpret_cast<const float*>(&u32);
91  return *f32;
92  }

◆ Val()

uint16_t Val ( ) const
inline

Definition at line 94 of file BFloat16.hpp.

Referenced by BFloat16::BFloat16(), BOOST_AUTO_TEST_CASE(), armnn::operator<<(), BFloat16::operator=(), and BFloat16::operator==().

95  {
96  return m_Value;
97  }

The documentation for this class was generated from the following file: