From 1428a7538ef4e9f10ae035f8594bc51d4fb9921d Mon Sep 17 00:00:00 2001 From: Sreekant Somasekharan Date: Mon, 5 Jun 2023 11:29:30 -0400 Subject: [PATCH] kfdtest: RoundToPowerOf2 function modified for compiler compliant bit shift values Compiler behavior is undefined if the right operand is negative, or greater than or equal to the width of the promoted left operand. For release builds with address sanitizer enabled, this compiler optimization behavior leads to unsupported queue size value since current method shifts till 128 bits on a 64 bit value. Signed-off-by: Sreekant Somasekharan Change-Id: Iafdc82d0dfb7f79e3012fb7bb70eda80e4b7a7a6 --- tests/kfdtest/src/KFDTestUtil.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/kfdtest/src/KFDTestUtil.cpp b/tests/kfdtest/src/KFDTestUtil.cpp index d02804eb09..6659bc036b 100644 --- a/tests/kfdtest/src/KFDTestUtil.cpp +++ b/tests/kfdtest/src/KFDTestUtil.cpp @@ -65,17 +65,18 @@ HSAKMT_STATUS fscanf_dec(const char *file, uint32_t *num) } uint64_t RoundToPowerOf2(uint64_t val) { - int bytes = sizeof(uint64_t); + val--; - val--; + /* Shift with amount larger than the bit width can result in + * undefined behavior by compiler for release builds. + * Shift till 32 bit only which is less than bit width of val. + */ + for (int i = 1; i <= 32; i *= 2) + val |= val >> i; - for (int i = 0; i < bytes; i++) { - val |= val >> (1 << i); - } + val++; - val++; - - return val; + return val; } bool WaitOnValue(const volatile unsigned int *buf, unsigned int value, unsigned int timeOut) {