Sujet : Re: "undefined behavior"?
De : ben (at) *nospam* bsb.me.uk (Ben Bacarisse)
Groupes : comp.lang.cDate : 15. Jun 2024, 23:13:01
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <87msnlx3bm.fsf@bsb.me.uk>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
User-Agent : Gnus/5.13 (Gnus v5.13)
Richard Harnden <
richard.nospam@gmail.invalid> writes:
On 15/06/2024 19:57, David Brown wrote:
If you want BBX_RGBA to be a typedef for an unsigned 32-bit integer,
write:
typedef uint32_t BBX_RGBA;
If you want bbx_rgba() to be a function that is typesafe, correct, and
efficient (for any decent compiler), write :
static inline BBX_RGBA bbx_rgba(uint32_t r, uint32_t g,
uint32_t b, uint32_t a)
{
return (r << 24) | (g << 16) | (b << 8) | a;
}
>
Shouldn't that be ... ?
>
static inline BBX_RGBA bbx_rgba(uint8_t r, uint8_t g,
uint8_t b, uint8_t a)
Maybe, but the function then needs more care as uint8_t will promote to
int and then r << 24 can be undefined. One needs
((BBX_RGBA)r << 24) | (g << 16) | (b << 8) | a
(assuming that int is never going to be 16 bits or the same issue comes
up with the g << 16 shift). Given this assumption, I'd just check that
unsigned int is at least 32 bits are use that for BBX_RGBA.
-- Ben.