ArmNN
 20.02
BFloat16.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2020 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include <ostream>
9 #include <cmath>
10 #include <stdint.h>
11 
12 namespace armnn
13 {
14 class BFloat16
15 {
16 public:
18  : m_Value(0)
19  {}
20 
21  explicit BFloat16(uint16_t v)
22  : m_Value(v)
23  {}
24 
25  explicit BFloat16(float v)
26  {
27  m_Value = Float32ToBFloat16(v).Val();
28  }
29 
30  operator float() const
31  {
32  return ToFloat32();
33  }
34 
35  BFloat16& operator=(const BFloat16& other)
36  {
37  m_Value = other.Val();
38  return *this;
39  }
40 
41  BFloat16& operator=(float v)
42  {
43  m_Value = Float32ToBFloat16(v).Val();
44  return *this;
45  }
46 
47  bool operator==(const BFloat16& r) const
48  {
49  return m_Value == r.Val();
50  }
51 
52  static BFloat16 Float32ToBFloat16(const float v)
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  }
86 
87  float ToFloat32() const
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  }
93 
94  uint16_t Val() const
95  {
96  return m_Value;
97  }
98 
99  static BFloat16 Max()
100  {
101  uint16_t max = 0x7F7F;
102  return BFloat16(max);
103  }
104 
105  static BFloat16 Nan()
106  {
107  uint16_t nan = 0x7FC0;
108  return BFloat16(nan);
109  }
110 
111  static BFloat16 Inf()
112  {
113  uint16_t infVal = 0x7F80;
114  return BFloat16(infVal);
115  }
116 
117 private:
118  uint16_t m_Value;
119 };
120 
121 inline std::ostream& operator<<(std::ostream& os, const BFloat16& b)
122 {
123  os << b.ToFloat32() << "(0x" << std::hex << b.Val() << ")";
124  return os;
125 }
126 
127 } //namespace armnn
float ToFloat32() const
Definition: BFloat16.hpp:87
BFloat16(float v)
Definition: BFloat16.hpp:25
BFloat16 & operator=(const BFloat16 &other)
Definition: BFloat16.hpp:35
std::ostream & operator<<(std::ostream &os, const std::vector< Compute > &compute)
Deprecated function that will be removed together with the Compute enum.
Definition: BackendId.hpp:47
BFloat16 & operator=(float v)
Definition: BFloat16.hpp:41
static BFloat16 Max()
Definition: BFloat16.hpp:99
Copyright (c) 2020 ARM Limited.
static BFloat16 Inf()
Definition: BFloat16.hpp:111
uint16_t Val() const
Definition: BFloat16.hpp:94
static BFloat16 Float32ToBFloat16(const float v)
Definition: BFloat16.hpp:52
bool operator==(const BFloat16 &r) const
Definition: BFloat16.hpp:47
static BFloat16 Nan()
Definition: BFloat16.hpp:105
BFloat16(uint16_t v)
Definition: BFloat16.hpp:21