dm: core: use {s,u}32 instead of int for dev_read_{s,u}32_default
cover dm: core: use {s,u}32 instead of int for dev_read_{s,u}32_default
Out of all the dev_read_*_default functions, only two do not properly use the type as argument and return type: dev_read_u32_default and dev_read_s32_default. They both use int instead of u32/s32.
Considering that it's generally not guaranteed that an int is 4 bytes but also for consistency sake, let's have them use the expected type.
Note that I have not tested this, just stumbled upon that inconsistency by chance.
---
Quentin Schulz (2):
dm: core: use u32 instead of int for dev_read_u32_default
dm: core: use s32 instead of int for dev_read_s32_default
drivers/core/read.c | 8 ++++---- include/dm/read.h | 14 +++++++------- 2 files changed, 11 insertions(+), 11 deletions(-)
---
base-commit: e04d137231f2e9e14708a32448c879125b8e308f
change-id: 20250528-dev_read_x32_default-73e09c95f64e
Best regards,
Signature · 1 line
Quentin Schulz <[email protected]>
PATCH 1/2 dm: core: use u32 instead of int for dev_read_u32_default
From: Quentin Schulz <[email protected]>
dev_read_u32_default is for getting a u32 from a Device Tree property and allows to take a default value if that property is missing.
Considering it calls ofnode_read_u32_default which takes a u32 and returns a u32, it should do the same instead of using an int, especially considering that int size is typically architecture-specific, as opposed to u32.
This incidentally matches all other dev_read_*_default functions (except dev_read_s32_default which will be tackled in the next commit).
--- drivers/core/read.c | 4 ++-- include/dm/read.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/core/read.c +2 -2
@@ -38,8 +38,8 @@ int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp)return ofnode_read_u32 (dev_ofnode (dev), propname, outp); } -int dev_read_u32_default (const struct udevice *dev,const char *propname, -int def) +u32 dev_read_u32_default (const struct udevice *dev,const char *propname, +u32 def) {return ofnode_read_u32_default (dev_ofnode (dev), propname, def); }
include/dm/read.h +3 -3
@@ -90,8 +90,8 @@ int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp);* @def: default value to return if the property has no value * Return: property value, or @def if not found */ -int dev_read_u32_default (const struct udevice *dev,const char *propname, -int def); +u32 dev_read_u32_default (const struct udevice *dev,const char *propname, +u32 def);/** * dev_read_u32_index() - read an indexed 32-bit integer from a device's DT
@@ -896,7 +896,7 @@ static inline int dev_read_u32(const struct udevice *dev, }static inline int dev_read_u32_default (const struct udevice *dev, -const char *propname,int def) +const char *propname,u32 def) {return ofnode_read_u32_default (dev_ofnode (dev), propname, def); }
Signature · 1 line
2.49.0
Re: [PATCH 1/2] dm: core: use u32 instead of int for dev_read_u32_default
Hi Quentin,
Quoted 14 lines · Quentin Schulz
From: Quentin Schulz <[email protected]>
dev_read_u32_defaultis for getting a u32 from a Device Tree property and allows to take a default value if that property is missing.Considering it calls
ofnode_read_u32_defaultwhich takes a u32 and returns a u32, it should do the same instead of using an int, especially considering that int size is typically architecture-specific, as opposed to u32.This incidentally matches all other dev_read_*_default functions (except
dev_read_s32_defaultwhich will be tackled in the next commit).
Quoted 6 lines
Fixes 47a0fd3bad38 ("dm: core: Implement live tree 'read' functions")Signed-off-by Quentin Schulz <[email protected]>--- drivers/core/read.c | 4 ++-- include/dm/read.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-)
Re your comment above 'int' seems to mean 32-bit on the toolchains we use. For example, in global_data we use int when we want a 32-bit value and long when we want a machine-word-sized value.
In general I try to use natural types (int, long, ulong) where possible, to avoid the compiler masking values before calling a function, which can increase code size. In this case I could make the argument either way.
Regards,
Simon
Re: [PATCH 1/2] dm: core: use u32 instead of int for dev_read_u32_default
Hi Simon,
Quoted 24 lines · Simon Glass
Hi Quentin,
Quoted 14 lines · Quentin Schulz
From: Quentin Schulz <[email protected]>
dev_read_u32_defaultis for getting a u32 from a Device Tree property and allows to take a default value if that property is missing.Considering it calls
ofnode_read_u32_defaultwhich takes a u32 and returns a u32, it should do the same instead of using an int, especially considering that int size is typically architecture-specific, as opposed to u32.This incidentally matches all other dev_read_*_default functions (except
dev_read_s32_defaultwhich will be tackled in the next commit).Quoted 6 lines
Fixes 47a0fd3bad38 ("dm: core: Implement live tree 'read' functions")Signed-off-by Quentin Schulz <[email protected]>--- drivers/core/read.c | 4 ++-- include/dm/read.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-)
Quoted 4 lines
Re your comment above 'int' seems to mean 32-bit on the toolchains we use. For example, in
global_datawe use int when we want a 32-bit value and long when we want a machine-word-sized value.
Fair enough, the only thing I can say - without real world experience - is that int isn't guaranteed to be 4 B by the standard? c.f. https://en.wikipedia.org/wiki/C_data_types#Main_types
Quoted 5 lines
In general I try to use natural types (int, long, ulong) where possible, to avoid the compiler masking values before calling a function, which can increase code size. In this case I could make the argument either way.
I'm happy to reword this commit log to be less confusing or alarmist. I can remove the "especially", would that be enough, do you have something else in mind?
Cheers,
Quentin
Re: [PATCH 1/2] dm: core: use u32 instead of int for dev_read_u32_default
Hi Quentin,
Quoted 28 lines · Quentin Schulz
Hi Simon,
Quoted 24 lines · Simon Glass
Hi Quentin,
Quoted 20 lines · Quentin Schulz
From: Quentin Schulz <[email protected]>
dev_read_u32_defaultis for getting a u32 from a Device Tree property and allows to take a default value if that property is missing.Considering it calls
ofnode_read_u32_defaultwhich takes a u32 and returns a u32, it should do the same instead of using an int, especially considering that int size is typically architecture-specific, as opposed to u32.This incidentally matches all other dev_read_*_default functions (except
dev_read_s32_defaultwhich will be tackled in the next commit).Fixes 47a0fd3bad38 ("dm: core: Implement live tree 'read' functions")Signed-off-by Quentin Schulz <[email protected]>--- drivers/core/read.c | 4 ++-- include/dm/read.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-)
Quoted 8 lines
Quoted 4 lines
Re your comment above 'int' seems to mean 32-bit on the toolchains we use. For example, in
global_datawe use int when we want a 32-bit value and long when we want a machine-word-sized value.Fair enough, the only thing I can say - without real world experience - is that int isn't guaranteed to be 4 B by the standard? c.f.
https://en.wikipedia.org/wiki/C_data_types#Main_types
That's right. I suppose we could include the compiler's stdint.h somehow and use int32_t but in practice Linux expects things to work as I described and other embedded projects follow that.
Quoted 10 lines
Quoted 6 lines
In general I try to use natural types (int, long, ulong) where possible, to avoid the compiler masking values before calling a function, which can increase code size. In this case I could make the argument either way.
I'm happy to reword this commit log to be less confusing or alarmist. I can remove the "especially", would that be enough, do you have something else in mind?
No, the patch is fine, I was just adding my comments.
Regards,
Simon
PATCH 2/2 dm: core: use s32 instead of int for dev_read_s32_default
In reply to Quentin SchulzFrom: Quentin Schulz <[email protected]>
dev_read_s32_default is for getting an s32 from a Device Tree property and allows to take a default value if that property is missing.
Considering it calls ofnode_read_u32_default which takes a u32 and returns a u32, it should do the same instead of using an int, especially considering that int size is typically architecture-specific, as opposed to s32/u32.
s32 and u32 being the same size, dev_read_s32* functions calling ofnode_read_u32_default shouldn't be an issue (at the type level at least) as the information will be stored appropriately in 4B regardless of the sign.
This incidentally matches all other dev_read_*_default functions.
--- drivers/core/read.c | 4 ++-- include/dm/read.h | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-)
drivers/core/read.c +2 -2
@@ -62,8 +62,8 @@ int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp)return ofnode_read_u32 (dev_ofnode (dev), propname, (u32 *)outp); } -int dev_read_s32_default (const struct udevice *dev,const char *propname, -int def) +s32 dev_read_s32_default (const struct udevice *dev,const char *propname, +s32 def) {return ofnode_read_u32_default (dev_ofnode (dev), propname, def); }
include/dm/read.h +4 -4
@@ -137,8 +137,8 @@ int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp);* @def: default value to return if the property has no value * Return: property value, or @def if not found */ -int dev_read_s32_default (const struct udevice *dev,const char *propname, -int def); +s32 dev_read_s32_default (const struct udevice *dev,const char *propname, +s32 def);/** * dev_read_u32u() - read a 32-bit integer from a device's DT property
@@ -921,8 +921,8 @@ static inline int dev_read_s32(const struct udevice *dev,return ofnode_read_s32 (dev_ofnode (dev), propname, outp); } -static inline int dev_read_s32_default (const struct udevice *dev, -const char *propname,int def) +static inline s32 dev_read_s32_default (const struct udevice *dev, +const char *propname,s32 def) {return ofnode_read_s32_default (dev_ofnode (dev), propname, def); }
Signature · 1 line
2.49.0
Re: [PATCH 2/2] dm: core: use s32 instead of int for dev_read_s32_default
Quoted 16 lines · Quentin Schulz
From: Quentin Schulz <[email protected]>
dev_read_s32_defaultis for getting an s32 from a Device Tree property and allows to take a default value if that property is missing.Considering it calls
ofnode_read_u32_defaultwhich takes a u32 and returns a u32, it should do the same instead of using an int, especially considering that int size is typically architecture-specific, as opposed to s32/u32.s32 and u32 being the same size, dev_read_s32* functions calling
ofnode_read_u32_defaultshouldn't be an issue (at the type level at least) as the information will be stored appropriately in 4B regardless of the sign.
Quoted 8 lines
This incidentally matches all other dev_read_*_default functions.
Fixes a1b17e4f4c82 ("dm: core: Add a function to read into a unsigned int")Signed-off-by Quentin Schulz <[email protected]>--- drivers/core/read.c | 4 ++-- include/dm/read.h | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-)
Quoted 8 lines · Quentin Schulz
Out of all the dev_read_*_default functions, only two do not properly use the type as argument and return type:
dev_read_u32_defaultanddev_read_s32_default. They both use int instead of u32/s32.Considering that it's generally not guaranteed that an int is 4 bytes but also for consistency sake, let's have them use the expected type.
[...]
Applied to u-boot/next, thanks!
[1/2] dm: core: use u32 instead of int for dev_read_u32_default
commit: b4b66e2f298e10d53749ce8d3ca086acbfc62f6a
[2/2] dm: core: use s32 instead of int for dev_read_s32_default
commit: ad03050e26b1a61b3fe10a645a36da11652355e7Signature · 1 line
Tom