From bfd7b0e3862d2f1602ddc651c52778649e736ad7 Mon Sep 17 00:00:00 2001 From: Vaibhav Kachore Date: Thu, 23 May 2019 17:27:00 +0530 Subject: [PATCH] gpu: nvgpu: add safe typecast operations Add function to typecast s8 to u8 in safe way. This function throws an error if operand is more than CHAR_MAX. JIRA NVGPU-3432 JIRA NVGPU-3438 Change-Id: Ieb712e6bcf187d6f26aaa1f2e0d8e6d2a17bcc54 Signed-off-by: Vaibhav Kachore Reviewed-on: https://git-master.nvidia.com/r/2124258 Reviewed-by: Deepak Nibade GVS: Gerrit_Virtual_Submit Reviewed-by: Alex Waterman Reviewed-by: mobile promotions Tested-by: mobile promotions --- drivers/gpu/nvgpu/include/nvgpu/safe_ops.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/drivers/gpu/nvgpu/include/nvgpu/safe_ops.h b/drivers/gpu/nvgpu/include/nvgpu/safe_ops.h index 2946a5d75..e9eff356e 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/safe_ops.h +++ b/drivers/gpu/nvgpu/include/nvgpu/safe_ops.h @@ -95,4 +95,21 @@ static inline u32 nvgpu_safe_cast_bool_to_u32(bool bl_a) return bl_a == true ? 1U : 0U; } +static inline u8 nvgpu_safe_cast_s8_to_u8(s8 sc_a) +{ + if (sc_a < 0) { + BUG(); + } else { + return (u8)sc_a; + } +} + +static inline u32 nvgpu_safe_cast_s32_to_u32(s32 si_a) +{ + if (si_a < 0) { + BUG(); + } else { + return (u32)si_a; + } +} #endif /* NVGPU_SAFE_OPS_H */