-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e3d8b99
Showing
8 changed files
with
3,371 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//----------------------------------------------------------------------------- | ||
// | ||
// The typical 'half' float16 data type (IEEE 754-2008) uses the following bit | ||
// allocation: mantissa:10 exponent:5 sign:1. | ||
// https://en.wikipedia.org/wiki/Half-precision_floating-point_format | ||
// | ||
// An alternate float16 is essentially float32 (IEEE 754-2008) with the lowest | ||
// 16 of 23 mantissa bits chopped off: mantissa:7 exponent:8 sign:1 | ||
// https://en.wikipedia.org/wiki/Bfloat16_floating-point_format | ||
// | ||
//----------------------------------------------------------------------------- | ||
|
||
#pragma once | ||
|
||
struct float16m7e8s1_t | ||
{ | ||
float16m7e8s1_t() = default; | ||
float16m7e8s1_t(const float16m7e8s1_t&) = default; | ||
float16m7e8s1_t(float16m7e8s1_t&&) = default; | ||
|
||
float16m7e8s1_t(float floatValue) noexcept | ||
{ | ||
value = reinterpret_cast<uint32_t&>(floatValue) >> 16; | ||
} | ||
|
||
float16m7e8s1_t& operator =(const float16m7e8s1_t&) = default; | ||
|
||
float16m7e8s1_t& operator =(float floatValue) noexcept | ||
{ | ||
value = reinterpret_cast<uint32_t&>(floatValue) >> 16; | ||
return *this; | ||
} | ||
|
||
operator float() const noexcept | ||
{ | ||
float floatValue = 0.0; | ||
reinterpret_cast<uint32_t&>(floatValue) = value << 16; | ||
return floatValue; | ||
} | ||
|
||
uint16_t value; | ||
}; | ||
|
||
inline float16m7e8s1_t operator +(float16m7e8s1_t a, float16m7e8s1_t b) noexcept { return float(a) + float(b); } | ||
inline float16m7e8s1_t operator -(float16m7e8s1_t a, float16m7e8s1_t b) noexcept { return float(a) - float(b); } | ||
inline float16m7e8s1_t operator *(float16m7e8s1_t a, float16m7e8s1_t b) noexcept { return float(a) * float(b); } | ||
inline float16m7e8s1_t operator /(float16m7e8s1_t a, float16m7e8s1_t b) noexcept { return float(a) / float(b); } | ||
inline float16m7e8s1_t operator +(float16m7e8s1_t a, double b) noexcept { return float(a) + float(b); } | ||
inline float16m7e8s1_t operator -(float16m7e8s1_t a, double b) noexcept { return float(a) - float(b); } | ||
inline float16m7e8s1_t operator *(float16m7e8s1_t a, double b) noexcept { return float(a) * float(b); } | ||
inline float16m7e8s1_t operator /(float16m7e8s1_t a, double b) noexcept { return float(a) / float(b); } | ||
inline float16m7e8s1_t operator +(double a, float16m7e8s1_t b) noexcept { return float(a) + float(b); } | ||
inline float16m7e8s1_t operator -(double a, float16m7e8s1_t b) noexcept { return float(a) - float(b); } | ||
inline float16m7e8s1_t operator *(double a, float16m7e8s1_t b) noexcept { return float(a) * float(b); } | ||
inline float16m7e8s1_t operator /(double a, float16m7e8s1_t b) noexcept { return float(a) / float(b); } | ||
inline float16m7e8s1_t& operator +=(float16m7e8s1_t& a, float16m7e8s1_t b) noexcept { return a = (float(a) + float(b)); } | ||
inline float16m7e8s1_t& operator -=(float16m7e8s1_t& a, float16m7e8s1_t b) noexcept { return a = (float(a) - float(b)); } | ||
inline float16m7e8s1_t& operator *=(float16m7e8s1_t& a, float16m7e8s1_t b) noexcept { return a = (float(a) * float(b)); } | ||
inline float16m7e8s1_t& operator /=(float16m7e8s1_t& a, float16m7e8s1_t b) noexcept { return a = (float(a) / float(b)); } | ||
inline float16m7e8s1_t& operator ++(float16m7e8s1_t& a) noexcept { return a = float(a) + 1; } | ||
inline float16m7e8s1_t& operator --(float16m7e8s1_t& a) noexcept { return a = float(a) + 1; } | ||
inline bool operator==(float16m7e8s1_t lhs, float16m7e8s1_t rhs) noexcept { return float(lhs) == float(rhs); } | ||
inline bool operator!=(float16m7e8s1_t lhs, float16m7e8s1_t rhs) noexcept { return float(lhs) != float(rhs); } | ||
inline bool operator< (float16m7e8s1_t lhs, float16m7e8s1_t rhs) noexcept { return float(lhs) < float(rhs); } | ||
inline bool operator> (float16m7e8s1_t lhs, float16m7e8s1_t rhs) noexcept { return float(lhs) > float(rhs); } | ||
inline bool operator<=(float16m7e8s1_t lhs, float16m7e8s1_t rhs) noexcept { return float(lhs) <= float(rhs); } | ||
inline bool operator>=(float16m7e8s1_t lhs, float16m7e8s1_t rhs) noexcept { return float(lhs) >= float(rhs); } |
Oops, something went wrong.