Skip to content

Commit

Permalink
Optimize bfloat16 construction (#15823)
Browse files Browse the repository at this point in the history
  • Loading branch information
marty1885 authored Dec 11, 2024
1 parent e8465bc commit 8e49222
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions tt_metal/common/bfloat16.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,14 @@ class bfloat16 {
uint16_t uint16_data;

public:
static const size_t SIZEOF = 2;
bfloat16() {}
static constexpr size_t SIZEOF = 2;
bfloat16() = default;

// create from float: no rounding, just truncate
bfloat16(float float_num) {
uint32_t uint32_data;
TT_ASSERT(sizeof float_num == sizeof uint32_data);
static_assert(sizeof float_num == 4, "float must have size 4");

uint32_data = *reinterpret_cast<uint32_t*>(&float_num);
// just move upper 16 to lower 16 (truncate)
uint32_data = (uint32_data >> 16);

// store lower 16 as 16-bit uint
uint16_data = (uint16_t)uint32_data;
uint16_data = (*reinterpret_cast<uint32_t*>(&float_num)) >> 16;
}

// store lower 16 as 16-bit uint
Expand Down

0 comments on commit 8e49222

Please sign in to comment.