gpu: nvgpu: posix: make round*_pow_of_two CERT-C friendly

CERT-C Rule INT30 requires checking unsigned arithmetic for potential
wrap. The macros roundup_pow_of_two and rounddown_pow_of_two could
potentially wrap if the passed parameter were 0.

JIRA NVGPU-3586

Change-Id: I9eba4c197b74db555055e1199ce72131b071062c
Signed-off-by: Philip Elcan <pelcan@nvidia.com>
Reviewed-on: https://git-master.nvidia.com/r/2131157
GVS: Gerrit_Virtual_Submit
Reviewed-by: Nitin Kumbhar <nkumbhar@nvidia.com>
Reviewed-by: mobile promotions <svcmobile_promotions@nvidia.com>
Tested-by: mobile promotions <svcmobile_promotions@nvidia.com>
This commit is contained in:
Philip Elcan
2019-06-05 14:51:32 -04:00
committed by mobile promotions
parent 7eb8ea9764
commit be7c7aa040

View File

@@ -33,8 +33,29 @@
fls_val; \ fls_val; \
}) })
#define roundup_pow_of_two(x) (1UL << fls((x) - 1UL)) #define roundup_pow_of_two(x) \
#define rounddown_pow_of_two(x) (1UL << (fls(x) - 1UL)) ({ \
unsigned long ret; \
\
if ((x) == 0UL) { \
BUG(); \
} else { \
ret = 1UL << fls((x) - 1UL); \
} \
ret; \
})
#define rounddown_pow_of_two(x) \
({ \
unsigned long ret; \
\
if ((x) == 0UL) { \
BUG(); \
} else { \
ret = 1UL << (fls(x) - 1UL); \
} \
ret; \
})
#define is_power_of_2(x) \ #define is_power_of_2(x) \
(bool)({ \ (bool)({ \