Re: "undefined behavior"?

Liste des GroupesRevenir à l c 
Sujet : Re: "undefined behavior"?
De : malcolm.arthur.mclean (at) *nospam* gmail.com (Malcolm McLean)
Groupes : comp.lang.c
Date : 16. Jun 2024, 15:44:53
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v4mq8l$2gm4$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
User-Agent : Mozilla Thunderbird
On 16/06/2024 11:53, David Brown wrote:
On 15/06/2024 21:27, Richard Harnden wrote:
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)
>
 As Ben says, that will not work on its own - "r" would get promoted to signed int before the shift, and we are back to undefined behaviour.
 I think there is plenty of scope for improvement in a variety of ways, depending on what the author is looking for.  For example, uint8_t might not exist on all platforms (indeed there are current processors that don't support it, not just dinosaur devices).  But any system that supports a general-purpose gui, such as Windows or *nix systems, will have these types and will also have a 32-bit int.  So the code author can balance portability with convenient assumptions.
 There are also balances to be found between run-time checking and efficiency, and how to handle bad data.  If the function can assume that no one calls it with values outside 0..255, or that it doesn't matter what happens if such values are used, then you don't need any checks. As it stands, with uint32_t parameters, out-of-range values will lead to fully defined but wrong results.  Switching to "uint8_t" types would give a different fully defined but wrong result.  Maybe the function should use saturation, or run-time checks and error messages - that will depend on where it is in the API, what the code author wants, and what users expect.
 
It's the general function which converts reba quads or rgb triplets (with a little wrapper) to opaque colour values to pass about to graphics functions. And currently it's not used to access the raster for BBX_Canvas elements.
But that's where Baby X does a lot of work which is a target for optimisation. And the opaque format might have to change to match the internal format used by the platform. However optimising the graphics isn't the priority for now, which is getting the API and the documentation right.
And what should happen if user passes wrong values? Since the function is provided as a macro rather than a subroutine, it's kind of accepted that it is too low-level for error checking. So user will usually draw the wrong colour on his screen, which might be a hard bug to diagnose.
But yes, this is absolutely the sort of thing you need to get right.
--
Check out my hobby project.
http://malcolmmclean.github.io/babyxrc

Date Sujet#  Auteur
12 Jun 24 * "undefined behavior"?77DFS
12 Jun 24 +* Re: "undefined behavior"?39Barry Schwarz
12 Jun 24 i`* Re: "undefined behavior"?38DFS
13 Jun 24 i `* Re: "undefined behavior"?37Keith Thompson
13 Jun 24 i  `* Re: "undefined behavior"?36DFS
13 Jun 24 i   `* Re: "undefined behavior"?35Keith Thompson
13 Jun 24 i    `* Re: "undefined behavior"?34Malcolm McLean
13 Jun 24 i     +- Re: "undefined behavior"?1Ben Bacarisse
13 Jun 24 i     +* Re: "undefined behavior"?29bart
13 Jun 24 i     i+* Re: "undefined behavior"?22Malcolm McLean
13 Jun 24 i     ii+* Re: "undefined behavior"?2Chris M. Thomasson
14 Jun 24 i     iii`- Re: "undefined behavior"?1Malcolm McLean
14 Jun 24 i     ii`* Re: "undefined behavior"?19Ben Bacarisse
14 Jun 24 i     ii `* Re: "undefined behavior"?18Malcolm McLean
14 Jun 24 i     ii  `* Re: "undefined behavior"?17Ben Bacarisse
14 Jun 24 i     ii   +* Re: "undefined behavior"?13Malcolm McLean
14 Jun 24 i     ii   i+* Re: "undefined behavior"?4Richard Harnden
14 Jun 24 i     ii   ii`* Re: "undefined behavior"?3Malcolm McLean
14 Jun 24 i     ii   ii `* Re: "undefined behavior"?2bart
14 Jun 24 i     ii   ii  `- Re: "undefined behavior"?1Malcolm McLean
14 Jun 24 i     ii   i`* Re: "undefined behavior"?8Ben Bacarisse
15 Jun 24 i     ii   i `* Re: "undefined behavior"?7Malcolm McLean
15 Jun 24 i     ii   i  +- Re: "undefined behavior"?1Ben Bacarisse
15 Jun 24 i     ii   i  `* Re: "undefined behavior"?5David Brown
15 Jun 24 i     ii   i   `* Re: "undefined behavior"?4Richard Harnden
16 Jun 24 i     ii   i    +- Re: "undefined behavior"?1Ben Bacarisse
16 Jun 24 i     ii   i    `* Re: "undefined behavior"?2David Brown
16 Jun 24 i     ii   i     `- Re: "undefined behavior"?1Malcolm McLean
14 Jun 24 i     ii   `* Re: "undefined behavior"?3Chris M. Thomasson
14 Jun 24 i     ii    `* Re: "undefined behavior"?2Ben Bacarisse
15 Jun 24 i     ii     `- Re: "undefined behavior"?1Chris M. Thomasson
14 Jun 24 i     i`* Re: "undefined behavior"?6Keith Thompson
14 Jun 24 i     i +- Re: "undefined behavior"?1bart
14 Jun 24 i     i +* Re: "undefined behavior"?3David Brown
14 Jun 24 i     i i`* Re: "undefined behavior"?2Keith Thompson
15 Jun 24 i     i i `- Re: "undefined behavior"?1David Brown
14 Jun 24 i     i `- Re: "undefined behavior"?1Keith Thompson
13 Jun 24 i     `* Re: "undefined behavior"?3Keith Thompson
14 Jun 24 i      `* Re: "undefined behavior"?2Malcolm McLean
14 Jun 24 i       `- Re: "undefined behavior"?1Keith Thompson
12 Jun 24 +* Re: "undefined behavior"?15David Brown
13 Jun 24 i+* Re: "undefined behavior"?6Keith Thompson
13 Jun 24 ii+* Re: "undefined behavior"?2David Brown
14 Jun 24 iii`- Re: "undefined behavior"?1Keith Thompson
19 Jun 24 ii`* Re: "undefined behavior"?3Tim Rentsch
19 Jun 24 ii `* Re: "undefined behavior"?2Keith Thompson
22 Jun 24 ii  `- Re: "undefined behavior"?1Tim Rentsch
13 Jun 24 i`* Re: "undefined behavior"?8DFS
13 Jun 24 i +* Re: "undefined behavior"?4Ike Naar
13 Jun 24 i i`* Re: "undefined behavior"?3DFS
13 Jun 24 i i `* Re: "undefined behavior"?2Lew Pitcher
13 Jun 24 i i  `- Re: "undefined behavior"?1DFS
13 Jun 24 i `* Re: "undefined behavior"?3David Brown
14 Jun 24 i  `* Re: "undefined behavior"?2Keith Thompson
14 Jun 24 i   `- Re: "undefined behavior"?1David Brown
12 Jun 24 +* Re: "undefined behavior"?19Janis Papanagnou
13 Jun 24 i`* Re: "undefined behavior"?18Keith Thompson
13 Jun 24 i +* Re: "undefined behavior"?2Janis Papanagnou
13 Jun 24 i i`- Re: "undefined behavior"?1David Brown
13 Jun 24 i `* Re: "undefined behavior"?15David Brown
13 Jun 24 i  `* Re: "undefined behavior"?14DFS
14 Jun 24 i   `* Re: "undefined behavior"?13David Brown
15 Jun 24 i    +* Re: "undefined behavior"?11DFS
15 Jun 24 i    i`* Re: "undefined behavior"?10Keith Thompson
15 Jun 24 i    i `* Re: "undefined behavior"?9DFS
15 Jun 24 i    i  `* Re: "undefined behavior"?8Keith Thompson
15 Jun 24 i    i   `* Re: "undefined behavior"?7DFS
15 Jun 24 i    i    +* Re: "undefined behavior"?2Janis Papanagnou
15 Jun 24 i    i    i`- Re: "undefined behavior"?1DFS
15 Jun 24 i    i    +- Re: "undefined behavior"?1James Kuyper
15 Jun 24 i    i    +- Re: "undefined behavior"?1Keith Thompson
15 Jun 24 i    i    +- Re: "undefined behavior"?1bart
15 Jun 24 i    i    `- Re: "undefined behavior"?1David Brown
15 Jun 24 i    `- Re: "undefined behavior"?1David Brown
12 Jun 24 +- Re: "undefined behavior"?1Keith Thompson
13 Jun 24 +- Re: "undefined behavior"?1bart
13 Jun 24 `- Re: "undefined behavior"?1Bonita Montero

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal