aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Kunze <eric.kunze@arm.com>2024-01-23 17:56:35 -0800
committerEric Kunze <eric.kunze@arm.com>2024-01-29 21:33:37 -0800
commit2d7e4b13d2c3022ae8176d59e2a11d5584ea1d0b (patch)
treec210cca2c5c1d51fbea9c521d55fedc05cfc1c6c
parent7f7d4255a5cd4e157cd385155d0c5c550648d2c4 (diff)
downloadreference_model-2d7e4b13d2c3022ae8176d59e2a11d5584ea1d0b.tar.gz
Simplify bfloat16 truncate and verify code
Signed-off-by: Eric Kunze <eric.kunze@arm.com> Change-Id: I85595b1ea82e3a9db8e5f654767a911fa58c842f
-rw-r--r--reference_model/src/arith_util.h45
-rw-r--r--reference_model/src/main.cpp2
2 files changed, 4 insertions, 43 deletions
diff --git a/reference_model/src/arith_util.h b/reference_model/src/arith_util.h
index cab43ba..fb491db 100644
--- a/reference_model/src/arith_util.h
+++ b/reference_model/src/arith_util.h
@@ -201,21 +201,8 @@ constexpr T saturate(const uint32_t width, const intmax_t value)
inline void float_trunc_bytes(float* src)
{
/* Set the least significant two bytes to zero for the input float value.*/
- char src_as_bytes[sizeof(float)];
- memcpy(src_as_bytes, src, sizeof(float));
-
- if (g_func_config.float_is_big_endian)
- {
- src_as_bytes[2] = '\000';
- src_as_bytes[3] = '\000';
- }
- else
- {
- src_as_bytes[0] = '\000';
- src_as_bytes[1] = '\000';
- }
-
- memcpy(src, &src_as_bytes, sizeof(float));
+ uint32_t* ptr = reinterpret_cast<uint32_t*>(src);
+ *ptr = *ptr & UINT32_C(0xffff0000);
}
inline void truncateFloatToBFloat(float* src, int64_t size)
@@ -233,32 +220,8 @@ inline void truncateFloatToBFloat(float* src, int64_t size)
inline bool checkValidBFloat(float src)
{
/* Checks if the least significant two bytes are zero. */
- char src_as_bytes[sizeof(float)];
- memcpy(src_as_bytes, &src, sizeof(float));
-
- if (g_func_config.float_is_big_endian)
- {
- return (src_as_bytes[2] == '\000' && src_as_bytes[3] == '\000');
- }
- else
- {
- return (src_as_bytes[0] == '\000' && src_as_bytes[1] == '\000');
- }
-}
-
-inline bool float_is_big_endian()
-{
- /* Compares float values 1.0 and -1.0 by checking whether the
- negation causes the first or the last byte to change.
- First byte changing would indicate the float representation
- is big-endian.*/
- float f = 1.0;
- char f_as_bytes[sizeof(float)];
- memcpy(f_as_bytes, &f, sizeof(float));
- f = -f;
- char f_neg_as_bytes[sizeof(float)];
- memcpy(f_neg_as_bytes, &f, sizeof(float));
- return f_as_bytes[0] != f_neg_as_bytes[0];
+ uint32_t* ptr = reinterpret_cast<uint32_t*>(&src);
+ return (*ptr & UINT32_C(0x0000ffff)) == 0;
}
template <TOSA_REF_TYPE Dtype>
diff --git a/reference_model/src/main.cpp b/reference_model/src/main.cpp
index 8539a0b..f72dd35 100644
--- a/reference_model/src/main.cpp
+++ b/reference_model/src/main.cpp
@@ -76,8 +76,6 @@ int main(int argc, char** argv)
return TOSA_VERSION_MISMATCH;
}
- g_func_config.float_is_big_endian = float_is_big_endian();
-
json test_desc;
// Initialize test descriptor