[4.19,195/229] drm/amd/display: fix overflow on MIN_I64 definition

Message ID 20221024113005.440623785@linuxfoundation.org
State New
Headers
Series None |

Commit Message

Greg KH Oct. 24, 2022, 11:31 a.m. UTC
  From: David Gow <davidgow@google.com>

[ Upstream commit 6ae0632d17759852c07e2d1e0a31c728eb6ba246 ]

The definition of MIN_I64 in bw_fixed.c can cause gcc to whinge about
integer overflow, because it is treated as a positive value, which is
then negated. The temporary positive value is not necessarily
representable.

This causes the following warning:
../drivers/gpu/drm/amd/amdgpu/../display/dc/dml/calcs/bw_fixed.c:30:19:
warning: integer overflow in expression ‘-9223372036854775808’ of type
‘long long int’ results in ‘-9223372036854775808’ [-Woverflow]
  30 |         (int64_t)(-(1LL << 63))
     |                   ^

Writing out (-MAX_I64 - 1) works instead.

Signed-off-by: David Gow <davidgow@google.com>
Signed-off-by: Tales Aparecida <tales.aparecida@gmail.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 drivers/gpu/drm/amd/display/dc/calcs/bw_fixed.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
  

Comments

Pavel Machek Oct. 25, 2022, 8:22 a.m. UTC | #1
Hi!

> From: David Gow <davidgow@google.com>
> 
> [ Upstream commit 6ae0632d17759852c07e2d1e0a31c728eb6ba246 ]
> 
> The definition of MIN_I64 in bw_fixed.c can cause gcc to whinge about
> integer overflow, because it is treated as a positive value, which is
> then negated. The temporary positive value is not necessarily
> representable.
> 
> This causes the following warning:
> ../drivers/gpu/drm/amd/amdgpu/../display/dc/dml/calcs/bw_fixed.c:30:19:
> warning: integer overflow in expression ‘-9223372036854775808’ of type
> ‘long long int’ results in ‘-9223372036854775808’ [-Woverflow]
>   30 |         (int64_t)(-(1LL << 63))
>      |                   ^
> 
> Writing out (-MAX_I64 - 1) works instead.

While this probably fixes the warning, better fix would be to include
limits.h which already has equivalent definitions.

Thanks and best regards,
								Pavel

> -#define MIN_I64 \
> -	(int64_t)(-(1LL << 63))
> -
>  #define MAX_I64 \
>  	(int64_t)((1ULL << 63) - 1)
>  
> +#define MIN_I64 \
> +	(-MAX_I64 - 1)
> +
>  #define FRACTIONAL_PART_MASK \
>  	((1ULL << BW_FIXED_BITS_PER_FRACTIONAL_PART) - 1)
>  
> -- 
> 2.35.1
> 
>
  

Patch

diff --git a/drivers/gpu/drm/amd/display/dc/calcs/bw_fixed.c b/drivers/gpu/drm/amd/display/dc/calcs/bw_fixed.c
index 6ca288fb5fb9..2d46bc527b21 100644
--- a/drivers/gpu/drm/amd/display/dc/calcs/bw_fixed.c
+++ b/drivers/gpu/drm/amd/display/dc/calcs/bw_fixed.c
@@ -26,12 +26,12 @@ 
 #include "bw_fixed.h"
 
 
-#define MIN_I64 \
-	(int64_t)(-(1LL << 63))
-
 #define MAX_I64 \
 	(int64_t)((1ULL << 63) - 1)
 
+#define MIN_I64 \
+	(-MAX_I64 - 1)
+
 #define FRACTIONAL_PART_MASK \
 	((1ULL << BW_FIXED_BITS_PER_FRACTIONAL_PART) - 1)