From 723880e79fdda9f537a911f88983229d44006e0c Mon Sep 17 00:00:00 2001 From: Ben Lawson Date: Fri, 13 Jan 2023 18:55:56 +0000 Subject: [PATCH] pw_random: Fix implicit conversions in RandomGenerator Fix implicit conversion from int to uint8_t in RandomGenerator::CountLeadingZeros. This conversion is safe because the __builtin_clz functions always return a positive value <= 64. This fixed compilation on Fuchsia. Test: pw watch Change-Id: Id7e29f80607e160c0315512aa9fca8da013cb90b Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/126510 Reviewed-by: Ali Zhang Commit-Queue: Ben Lawson --- pw_random/public/pw_random/random.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pw_random/public/pw_random/random.h b/pw_random/public/pw_random/random.h index 38902bda7e..4be398c058 100644 --- a/pw_random/public/pw_random/random.h +++ b/pw_random/public/pw_random/random.h @@ -96,16 +96,17 @@ class RandomGenerator { template uint8_t CountLeadingZeros(T value) { if constexpr (std::is_same_v) { - return __builtin_clz(value); + return static_cast(__builtin_clz(value)); } else if constexpr (std::is_same_v) { - return __builtin_clzl(value); + return static_cast(__builtin_clzl(value)); } else if constexpr (std::is_same_v) { - return __builtin_clzll(value); + return static_cast(__builtin_clzll(value)); } else { static_assert(sizeof(T) < sizeof(unsigned)); // __builtin_clz returns the count of leading zeros in an unsigned , so we // need to subtract the size difference of T in bits. - return __builtin_clz(value) - ((sizeof(unsigned) - sizeof(T)) * CHAR_BIT); + return static_cast(__builtin_clz(value) - + ((sizeof(unsigned) - sizeof(T)) * CHAR_BIT)); } } };