ArmNN
 20.11
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  BFloat16(const BFloat16& v) = default;
22 
23  explicit BFloat16(uint16_t v)
24  : m_Value(v)
25  {}
26 
27  explicit BFloat16(float v)
28  {
29  m_Value = Float32ToBFloat16(v).Val();
30  }
31 
32  operator float() const
33  {
34  return ToFloat32();
35  }
36 
37  BFloat16& operator=(const BFloat16& other) = default;
38 
39  BFloat16& operator=(float v)
40  {
41  m_Value = Float32ToBFloat16(v).Val();
42  return *this;
43  }
44 
45  bool operator==(const BFloat16& r) const
46  {
47  return m_Value == r.Val();
48  }
49 
50  static BFloat16 Float32ToBFloat16(const float v)
51  {
52  if (std::isnan(v))
53  {
54  return Nan();
55  }
56  else
57  {
58  // Round value to the nearest even
59  // Float32
60  // S EEEEEEEE MMMMMMLRMMMMMMMMMMMMMMM
61  // BFloat16
62  // S EEEEEEEE MMMMMML
63  // LSB (L): Least significat bit of BFloat16 (last bit of the Mantissa of BFloat16)
64  // R: Rounding bit
65  // LSB = 0, R = 0 -> round down
66  // LSB = 1, R = 0 -> round down
67  // LSB = 0, R = 1, all the rest = 0 -> round down
68  // LSB = 1, R = 1 -> round up
69  // LSB = 0, R = 1 -> round up
70  const uint32_t* u32 = reinterpret_cast<const uint32_t*>(&v);
71  uint16_t u16 = static_cast<uint16_t>(*u32 >> 16u);
72  // Mark the LSB
73  const uint16_t lsb = u16 & 0x0001;
74  // Mark the error to be truncate (the rest of 16 bits of FP32)
75  const uint16_t error = static_cast<uint16_t>((*u32 & 0x0000FFFF));
76  if ((error > 0x8000 || (error == 0x8000 && lsb == 1)))
77  {
78  u16++;
79  }
80  BFloat16 b(u16);
81  return b;
82  }
83  }
84 
85  float ToFloat32() const
86  {
87  const uint32_t u32 = static_cast<uint32_t>(m_Value << 16u);
88  const float* f32 = reinterpret_cast<const float*>(&u32);
89  return *f32;
90  }
91 
92  uint16_t Val() const
93  {
94  return m_Value;
95  }
96 
97  static BFloat16 Max()
98  {
99  uint16_t max = 0x7F7F;
100  return BFloat16(max);
101  }
102 
103  static BFloat16 Nan()
104  {
105  uint16_t nan = 0x7FC0;
106  return BFloat16(nan);
107  }
108 
109  static BFloat16 Inf()
110  {
111  uint16_t infVal = 0x7F80;
112  return BFloat16(infVal);
113  }
114 
115 private:
116  uint16_t m_Value;
117 };
118 
119 inline std::ostream& operator<<(std::ostream& os, const BFloat16& b)
120 {
121  os << b.ToFloat32() << "(0x" << std::hex << b.Val() << ")";
122  return os;
123 }
124 
125 } //namespace armnn
float ToFloat32() const
Definition: BFloat16.hpp:85
BFloat16(float v)
Definition: BFloat16.hpp:27
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:39
static BFloat16 Max()
Definition: BFloat16.hpp:97
Copyright (c) 2020 ARM Limited.
static BFloat16 Inf()
Definition: BFloat16.hpp:109
uint16_t Val() const
Definition: BFloat16.hpp:92
static BFloat16 Float32ToBFloat16(const float v)
Definition: BFloat16.hpp:50
bool operator==(const BFloat16 &r) const
Definition: BFloat16.hpp:45
BFloat16 & operator=(const BFloat16 &other)=default
static BFloat16 Nan()
Definition: BFloat16.hpp:103
BFloat16(uint16_t v)
Definition: BFloat16.hpp:23