-
Notifications
You must be signed in to change notification settings - Fork 12
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
Showing
2 changed files
with
63 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
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,39 @@ | ||
/* | ||
*Copyright Redis Ltd. 2021 - present | ||
*Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or | ||
*the Server Side Public License v1 (SSPLv1). | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <cstring> | ||
#include <cmath> | ||
|
||
namespace vecsim_types { | ||
struct bfloat16 { | ||
uint16_t val; | ||
bfloat16() = default; | ||
explicit constexpr bfloat16(uint16_t val) : val(val) {} | ||
operator uint16_t() const { return val; } | ||
}; | ||
|
||
static inline bfloat16 float_to_bf16(const float ff) { | ||
uint32_t *p_f32 = (uint32_t *)&ff; | ||
uint32_t f32 = *p_f32; | ||
uint32_t lsb = (f32 >> 16) & 1; | ||
uint32_t round = lsb + 0x7FFF; | ||
f32 += round; | ||
return bfloat16(f32 >> 16); | ||
} | ||
|
||
template <bool is_little = true> | ||
inline float bfloat16_to_float32(bfloat16 val) { | ||
size_t constexpr bytes_offset = is_little ? 1 : 0; | ||
float result = 0; | ||
bfloat16 *p_result = (bfloat16 *)&result + bytes_offset; | ||
*p_result = val; | ||
return result; | ||
} | ||
|
||
} // namespace vecsim_types |