From 8c4b073537cdde960eaa577c18c46bc075a52b6f Mon Sep 17 00:00:00 2001 From: Philip Elcan Date: Mon, 17 Sep 2018 11:39:37 -0400 Subject: gpu: nvgpu: posix: fix implementation of fls() The POSIX implementation of fls() wasn't compliant with the Linux which returns fls(0)=0 fls(1)=1, etc. Bug found as result of JIRA NVGPU-1042. Change-Id: Id0279e36332ffe236ed792c013c32f2da841f557 Signed-off-by: Philip Elcan Reviewed-on: https://git-master.nvidia.com/r/1828361 Reviewed-by: Automatic_Commit_Validation_User Reviewed-by: svc-misra-checker GVS: Gerrit_Virtual_Submit Reviewed-by: Alex Waterman Reviewed-by: mobile promotions Tested-by: mobile promotions --- drivers/gpu/nvgpu/os/posix/bitmap.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'drivers/gpu/nvgpu/os/posix') diff --git a/drivers/gpu/nvgpu/os/posix/bitmap.c b/drivers/gpu/nvgpu/os/posix/bitmap.c index f25f6e64..99ba62c3 100644 --- a/drivers/gpu/nvgpu/os/posix/bitmap.c +++ b/drivers/gpu/nvgpu/os/posix/bitmap.c @@ -37,7 +37,18 @@ unsigned long __nvgpu_posix_ffs(unsigned long word) unsigned long __nvgpu_posix_fls(unsigned long word) { - return ((sizeof(unsigned long) * 8UL) - 1UL) - __builtin_clzl(word); + unsigned long ret; + + if (word == 0UL) { + /* __builtin_clzl() below is undefined for 0, so we have + * to handle that as a special case. + */ + ret = 0UL; + } else { + ret = (sizeof(unsigned long) * 8UL) - __builtin_clzl(word); + } + + return ret; } static unsigned long __find_next_bit(const unsigned long *addr, -- cgit v1.2.2