On Fri, Feb 09, 2024 at 08:43:21PM +0100, Mats Kindahl wrote: > QQ: right now it looks like this: > > static inline int > pg_cmp_u16(uint16 a, uint16 b) > { > > return (int32)a - (int32)b; > > } > > > and > > static inline int > pg_cmp_u32(uint32 a, uint32 b) > { > > return (a > b) - (a < b); > > } > > > I think that is clear enough, but do you want more casts added for the > return value as well?
I think that is reasonably clear. The latter does require you to know that < and > return (int) 0 or (int) 1, which might be worth a short comment. But that's just nitpicking...
Hi all,
Split the code into two patches: one that just adds the functions (including the new pg_cmp_size()) to common/int.h and one that starts using them. I picked the name "pg_cmp_size" rather than "pg_cmp_size_t" since "_t" is usually used as a suffix for types.
I added a comment to the (a > b) - (a < b) return and have also added casts to (int32) for the int16 and uint16 functions (we need a signed int for uin16 since we need to be able to get a negative number).
Changed the type of two instances that had an implicit cast from size_t to int and used the new pg_,cmp_size() function.
Also fixed the missed replacements in the "contrib" directory.