gpu: nvgpu: add safe u32 to u8/s8 cast ops

Add safe casts ops:
- nvgpu_safe_cast_u32_to_u8
- nvgpu_safe_cast_u32_to_s8

JIRA NVGPU-3519

Change-Id: I4f8fe0ce80de7849183b00421b39495ef0365037
Signed-off-by: Philip Elcan <pelcan@nvidia.com>
Reviewed-on: https://git-master.nvidia.com/r/2126825
Reviewed-by: mobile promotions <svcmobile_promotions@nvidia.com>
Tested-by: mobile promotions <svcmobile_promotions@nvidia.com>
This commit is contained in:
Philip Elcan
2019-05-28 17:35:37 -04:00
committed by mobile promotions
parent ff77452d89
commit d7e3872da0
2 changed files with 24 additions and 0 deletions

View File

@@ -172,6 +172,24 @@ static inline u16 nvgpu_safe_cast_u32_to_u16(u32 ui_a)
}
}
static inline u8 nvgpu_safe_cast_u32_to_u8(u32 ui_a)
{
if (ui_a > UCHAR_MAX) {
BUG();
} else {
return (u8)ui_a;
}
}
static inline s8 nvgpu_safe_cast_u32_to_s8(u32 ui_a)
{
if (ui_a > SCHAR_MAX) {
BUG();
} else {
return (s8)ui_a;
}
}
static inline s32 nvgpu_safe_cast_u32_to_s32(u32 ui_a)
{
if (ui_a > INT_MAX) {

View File

@@ -73,4 +73,10 @@
#define U64_MAX U64(~U64(0))
#endif
#if defined(__KERNEL__) && !defined(UCHAR_MAX)
/* Linux doesn't define these max values, and we can't use limits.h */
#define UCHAR_MAX U8_MAX
#define SCHAR_MAX (U8_MAX/2)
#endif
#endif /* NVGPU_TYPES_H */