OpenASIP  2.0
Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
HalfFloatWord Class Reference

#include <HalfFloatWord.hh>

Collaboration diagram for HalfFloatWord:
Collaboration graph

Public Member Functions

 operator float () const
 
 HalfFloatWord (uint16_t binaryRep)
 
 HalfFloatWord (float value)
 
 HalfFloatWord (const HalfFloatWord &hw)
 
 HalfFloatWord ()
 
HalfFloatWord operator+ (const HalfFloatWord &right) const
 
HalfFloatWord operator- (const HalfFloatWord &right) const
 
HalfFloatWord operator* (const HalfFloatWord &right) const
 
HalfFloatWord operator/ (const HalfFloatWord &right) const
 
const HalfFloatWordoperator= (float value)
 
const HalfFloatWordoperator= (const HalfFloatWord &value)
 
uint16_t getBinaryRep () const
 

Static Public Member Functions

static uint16_t convertFloatToHalfWordRep (float value)
 
static float convertToFloat (HalfFloatWord value)
 

Private Attributes

uint16_t binaryRep_
 

Detailed Description

Half-precision, IEE-754-2008 16-bit floating point number.

Definition at line 41 of file HalfFloatWord.hh.

Constructor & Destructor Documentation

◆ HalfFloatWord() [1/4]

HalfFloatWord::HalfFloatWord ( uint16_t  binaryRep)
explicit

Definition at line 50 of file HalfFloatWord.cc.

50 : binaryRep_(binaryRep) {}

◆ HalfFloatWord() [2/4]

HalfFloatWord::HalfFloatWord ( float  value)
explicit

Definition at line 143 of file HalfFloatWord.cc.

143  :

◆ HalfFloatWord() [3/4]

HalfFloatWord::HalfFloatWord ( const HalfFloatWord hw)

Definition at line 147 of file HalfFloatWord.cc.

147  :
148  binaryRep_(hw.binaryRep_) {}

◆ HalfFloatWord() [4/4]

HalfFloatWord::HalfFloatWord ( )

Definition at line 53 of file HalfFloatWord.cc.

53 : binaryRep_(0) {}

Referenced by operator*(), operator+(), operator-(), and operator/().

Member Function Documentation

◆ convertFloatToHalfWordRep()

uint16_t HalfFloatWord::convertFloatToHalfWordRep ( float  value)
static

Definition at line 56 of file HalfFloatWord.cc.

56  {
58  u.f = value;
59  int binary16 = (u.i & 0x80000000) >> 16;
60 
61  int expon = (u.i & 0x7f800000) >> 23;
62  if(expon == 255) {
63  // Handle NAN
64  return binary16 | 0x7e00;
65  }
66  expon += 15-127;
67  if(expon <= 0) {
68  // Underflow, return zero with correct sign
69  // TODO: support denormals?
70  return binary16;
71  }
72  if(expon >= 31) {
73  // Overflow, return inf with correct sign
74  binary16 |= 0x7c00;
75  return binary16;
76  }
77  binary16 |= expon << 10;
78  binary16 |= (u.i & 0x007FFFFF) >> 13;
79 
80  //Round to nearest even. Comment following code out
81  //for Round to Zero behavior.
82  int l, g, r, s;
83  l = (u.i >> 13) & 1;
84  g = (u.i >> 12) & 1;
85  r = (u.i >> 11) & 1;
86  s = (u.i & ((1<<11)-1)) ? 1 : 0;
87  if(g && (l || (r||s)))
88  binary16++;
89 
90  return binary16;
91 }

References FloatConvUnion::f, and FloatConvUnion::i.

Referenced by operator=().

◆ convertToFloat()

float HalfFloatWord::convertToFloat ( HalfFloatWord  value)
static

Exact and slower version of operator float()-function.

Attempts to retain original presentation of the source half float.

Definition at line 99 of file HalfFloatWord.cc.

99  {
100  FloatConvUnion u;
101 
102  static const uint32_t half_inf_mask = 0x7C00;
103  static const uint32_t half_mant_mask = 0x03FF;
104  static const uint32_t float_inf_mask = 0x7F800000;
105 
106  uint32_t sign = (value.getBinaryRep() & 0x8000) << 16;
107  uint32_t exp = (value.getBinaryRep() & half_inf_mask) >> 10; // Biased form
108  uint32_t mant = (value.getBinaryRep() & half_mant_mask);
109 
110  if (exp == 0x1F) { // is +-inf or NaN?
111  u.u = sign | float_inf_mask | mant << 13;
112  return u.f;
113  }
114 
115  if ((exp == 0 && mant != 0)) { // is denormal?
116  // normalize
117  exp = 127;
118  while (!(mant & 0x400)) {
119  exp--;
120  mant <<= 1;
121  }
122  mant &= 0x400;
123  u.u = sign | exp << 23 | mant << 13;
124  return u.f;
125  } else {
126  u.u = sign | (exp-15+127) << 23 | mant << 13;
127  return u.f;
128  }
129 }

References FloatConvUnion::f, getBinaryRep(), and FloatConvUnion::u.

Here is the call graph for this function:

◆ getBinaryRep()

uint16_t HalfFloatWord::getBinaryRep ( ) const
inline

Definition at line 60 of file HalfFloatWord.hh.

60 { return binaryRep_; }

References binaryRep_.

Referenced by convertToFloat(), SimValue::operator=(), and SimValue::operator==().

◆ operator float()

HalfFloatWord::operator float ( ) const

Definition at line 152 of file HalfFloatWord.cc.

152  {
153  if (binaryRep_ == 0xFC00) {
154  return -INFINITY;
155  }
156  if (binaryRep_ == 0x7C00) {
157  return INFINITY;
158  }
159 
160  bool sgn = ((binaryRep_ & 0x8000) >> 15);
161  int exp = (binaryRep_ & 0x7C00) >> 10;
162  int mant = binaryRep_ & 0x03FF;
163 
164  if (exp == 0x1F && mant != 0) {
165  return NAN;
166  }
167 
168  float value = (exp == 0) ? mant : mant | 0x0400; // 1.x if not denormal
169  value /= 0x400;
170  float mul = exp2(exp - 15);
171  value *= mul;
172  if (sgn) {
173  value *= -1;
174  }
175  return value;
176 }

References INFINITY, and NAN.

◆ operator*()

HalfFloatWord HalfFloatWord::operator* ( const HalfFloatWord right) const

Definition at line 190 of file HalfFloatWord.cc.

190  {
191  return HalfFloatWord(float(*this) * float(right));
192 }

References HalfFloatWord().

Here is the call graph for this function:

◆ operator+()

HalfFloatWord HalfFloatWord::operator+ ( const HalfFloatWord right) const

Definition at line 182 of file HalfFloatWord.cc.

182  {
183  return HalfFloatWord(float(*this) + float(right));
184 }

References HalfFloatWord().

Here is the call graph for this function:

◆ operator-()

HalfFloatWord HalfFloatWord::operator- ( const HalfFloatWord right) const

Definition at line 186 of file HalfFloatWord.cc.

186  {
187  return HalfFloatWord(float(*this) - float(right));
188 }

References HalfFloatWord().

Here is the call graph for this function:

◆ operator/()

HalfFloatWord HalfFloatWord::operator/ ( const HalfFloatWord right) const

Definition at line 194 of file HalfFloatWord.cc.

194  {
195  return HalfFloatWord(float(*this) / float(right));
196 }

References HalfFloatWord().

Here is the call graph for this function:

◆ operator=() [1/2]

const HalfFloatWord & HalfFloatWord::operator= ( const HalfFloatWord value)

Definition at line 138 of file HalfFloatWord.cc.

138  {
139  binaryRep_ = hf.binaryRep_;
140  return *this;
141 }

References binaryRep_.

◆ operator=() [2/2]

const HalfFloatWord & HalfFloatWord::operator= ( float  value)

Definition at line 133 of file HalfFloatWord.cc.

133  {
135  return *this;
136 }

References binaryRep_, and convertFloatToHalfWordRep().

Here is the call graph for this function:

Member Data Documentation

◆ binaryRep_

uint16_t HalfFloatWord::binaryRep_
private

Definition at line 66 of file HalfFloatWord.hh.

Referenced by getBinaryRep(), and operator=().


The documentation for this class was generated from the following files:
INFINITY
#define INFINITY
Definition: HalfFloatWord.cc:36
HalfFloatWord::getBinaryRep
uint16_t getBinaryRep() const
Definition: HalfFloatWord.hh:60
NAN
#define NAN
Definition: HalfFloatWord.cc:40
FloatConvUnion::u
unsigned int u
Definition: HalfFloatWord.cc:45
HalfFloatWord::convertFloatToHalfWordRep
static uint16_t convertFloatToHalfWordRep(float value)
Definition: HalfFloatWord.cc:56
FloatConvUnion::f
float f
Definition: HalfFloatWord.cc:46
HalfFloatWord::binaryRep_
uint16_t binaryRep_
Definition: HalfFloatWord.hh:66
FloatConvUnion
Definition: HalfFloatWord.cc:43
FloatConvUnion::i
int i
Definition: HalfFloatWord.cc:44
HalfFloatWord::HalfFloatWord
HalfFloatWord()
Definition: HalfFloatWord.cc:53