[PATCH v2 05/47] arm64: cputype: Silence -Wshorten-64-to-32 warnings

Ian Rogers posted 47 patches 7 months, 3 weeks ago
[PATCH v2 05/47] arm64: cputype: Silence -Wshorten-64-to-32 warnings
Posted by Ian Rogers 7 months, 3 weeks ago
The clang warning -Wshorten-64-to-32 can be useful to catch
inadvertent truncation. In some instances this truncation can lead to
changing the sign of a result, for example, truncation to return an
int to fit a sort routine. Silence the warning by making the implicit
truncation explicit.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/arch/arm64/include/asm/cputype.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/arch/arm64/include/asm/cputype.h b/tools/arch/arm64/include/asm/cputype.h
index 488f8e751349..d7289b9d2758 100644
--- a/tools/arch/arm64/include/asm/cputype.h
+++ b/tools/arch/arm64/include/asm/cputype.h
@@ -227,7 +227,7 @@
 
 #include <asm/sysreg.h>
 
-#define read_cpuid(reg)			read_sysreg_s(SYS_ ## reg)
+#define read_cpuid(reg)			((u32)read_sysreg_s(SYS_ ## reg))
 
 /*
  * Represent a range of MIDR values for a given CPU model and a
-- 
2.49.0.906.g1f30a19c02-goog
Re: [PATCH v2 05/47] arm64: cputype: Silence -Wshorten-64-to-32 warnings
Posted by Mark Rutland 7 months, 2 weeks ago
On Wed, Apr 30, 2025 at 10:49:53AM -0700, Ian Rogers wrote:
> The clang warning -Wshorten-64-to-32 can be useful to catch
> inadvertent truncation. In some instances this truncation can lead to
> changing the sign of a result, for example, truncation to return an
> int to fit a sort routine. Silence the warning by making the implicit
> truncation explicit.
> 
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/arch/arm64/include/asm/cputype.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/tools/arch/arm64/include/asm/cputype.h b/tools/arch/arm64/include/asm/cputype.h
> index 488f8e751349..d7289b9d2758 100644
> --- a/tools/arch/arm64/include/asm/cputype.h
> +++ b/tools/arch/arm64/include/asm/cputype.h
> @@ -227,7 +227,7 @@
>  
>  #include <asm/sysreg.h>
>  
> -#define read_cpuid(reg)			read_sysreg_s(SYS_ ## reg)
> +#define read_cpuid(reg)			((u32)read_sysreg_s(SYS_ ## reg))

This isn't right.

Architecturally, system registers are 64-bit wide, and some of the ID
registers have allocated fields in the upper 32 bits, e.g. in MPIDR,
where we this will silently discard those when accessed via read_cpuid_mpidr():

static inline u64 __attribute_const__ read_cpuid_mpidr(void)
{
        return read_cpuid(MPIDR_EL1);
}

Mark.