Path: ...!weretis.net!feeder9.news.weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.c Subject: Re: "undefined behavior"? Date: Sat, 15 Jun 2024 23:13:01 +0100 Organization: A noiseless patient Spider Lines: 31 Message-ID: <87msnlx3bm.fsf@bsb.me.uk> References: <666a095a$0$952$882e4bbb@reader.netnews.com> <8t3k6j5ikf5mvimvksv2t91gbt11ljdfgb@4ax.com> <666a18de$0$958$882e4bbb@reader.netnews.com> <87y1796bfn.fsf@nosuchdomain.example.com> <666a2a30$0$952$882e4bbb@reader.netnews.com> <87tthx65qu.fsf@nosuchdomain.example.com> <87o784xusf.fsf@bsb.me.uk> <87ikybycj6.fsf@bsb.me.uk> <87cyojxlgj.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Injection-Date: Sun, 16 Jun 2024 00:13:00 +0200 (CEST) Injection-Info: dont-email.me; posting-host="4454bce243354ff30bc38dcd2528bf41"; logging-data="3844200"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX196MOPTw0YtGk6ieRRsxVj8MvOmpj/Ni7E=" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:ys09jQmWrJRbB5HjQKyrpVESUe8= sha1:48Solxb7NuGekHw9IaqxUXQ9yB0= X-BSB-Auth: 1.47f0773d2b75af17a1b7.20240615231301BST.87msnlx3bm.fsf@bsb.me.uk Bytes: 2740 Richard Harnden 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.