From 8e49222f313fcdcbe088570c806ad86a8abf53f3 Mon Sep 17 00:00:00 2001 From: Martin Chang Date: Wed, 11 Dec 2024 17:25:34 +0800 Subject: [PATCH] Optimize bfloat16 construction (#15823) --- tt_metal/common/bfloat16.hpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/tt_metal/common/bfloat16.hpp b/tt_metal/common/bfloat16.hpp index c03a58513cf..b332a9d2aee 100644 --- a/tt_metal/common/bfloat16.hpp +++ b/tt_metal/common/bfloat16.hpp @@ -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(&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(&float_num)) >> 16; } // store lower 16 as 16-bit uint