Re: how cast works?

Liste des GroupesRevenir à l c 
Sujet : Re: how cast works?
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.c
Date : 09. Aug 2024, 00:32:00
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v93h12$9vom$1@dont-email.me>
References : 1 2 3 4 5 6 7 8
User-Agent : Mozilla Thunderbird
On 08/08/2024 21:09, Bart wrote:
On 08/08/2024 18:58, David Brown wrote:
On 08/08/2024 19:29, Bart wrote:
On 08/08/2024 17:32, Michael S wrote:
 > On Thu, 8 Aug 2024 14:23:44 +0100
 > Bart <bc@freeuk.com> wrote:
 >> Try godbolt.org. Type in a fragment of code that does different kinds
 >> of casts (it needs to be well-formed, so inside a function), and see
 >> what code is produced with different C compilers.
 >>
 >> Use -O0 so that the code isn't optimised out of existence, and so
 >> that you can more easily match it to the C ource.
 >>
 >>
 >
 >
 > I'd recommend an opposite - use -O2 so the cast that does nothing
 > optimized away.
 >
 > int foo_i2i(int x) { return (int)x; }
 > int foo_u2i(unsigned x) { return (int)x; }
 > int foo_b2i(_Bool x) { return (int)x; }
 > int foo_d2i(double x) { return (int)x; }
The OP is curious as to what's involved when a conversion is done. Hiding or eliminating code isn't helpful in that case; the results can also be misleading:
>
Michael is correct - the OP should enable optimisation, precisely to avoid the issue you are concerned about.  Without optimisation, the results are misleading - they will only show things that are /not/ involved in the conversion, swamping the useful results with code that messes about putting data on and off the stack.  When optimised compilation shows that no code is generated, it is a very clear indication that no operations are needed for the conversions in question - unoptimized code hides that.
>
>
Take this example:
>
   void fred(void) {
    _Bool b;
      int i;
      i=b;
   }
>
Unoptimised, it generates this code:
>
         push    rbp
         mov     rbp, rsp
>
         mov     al, byte ptr [rbp - 1]
         and     al, 1
         movzx   eax, al
         mov     dword ptr [rbp - 8], eax
>
         pop     rbp
         ret
>
You can see from this that a Bool occupies one byte; it is masked to 0/1 (so it doesn't trust it to contain only 0/1), then it is widened to an int size.
>
>
No, you can't see that.  All you can see is garbage in, garbage out. You have to start with a function that has some meaning!
 Sorry but my function is perfectly valid. It's taking a Bool value and converting it to an int.
No, it is not.
Attempting to use the value of a non-static local variable that has not been initialised or assigned is undefined behaviour.  Your function is garbage.  No one can draw any conclusions about how meaningless code is compiled.

 Perhaps you don't understand x86 code? I'll tell you: it loads that /byte-sized/ value, masks it, and widens it to an int. I'm surprised you can't see that.
 
I understand x86 well enough - perhaps not as well as you, but well enough.  I do, however, understand C better than you, it seems.  The x86 code is irrelevant to the fact that your code has undefined behaviour. (And even if it were defined, it would still do nothing relevant.)

But I suspect a long gaslighting session coming on, where you refute the evidence that everyone else can see!
 
You are projecting.
Look, it is extraordinarily simple to write functions that /actually/ do the conversions under discussion.  Why waste time writing nonsense functions that do that?

 
>
With optimisation turned on, even at -O1, it produces this:
>
         ret
>
Try again with:
>
     int foo(bool x) { return x; }
>
     bool bar(int x) { return x; }
>
Try it with -O0 and -O1, and then tell us which you think gives a clearer indication of the operations needed.
 Michael is wrong and so are you.
 If you want to know what casting from bool to int entails, then testing it via a function call like this is the wrong way to do it, since half kj
of it depends on what happens when evaluating arguments at the call site.
 
Nope.
But if you prefer, just use external variables:
int i;
bool b;
void to_int_0(void) { i = b; }
void to_bool_0(void) { b = i; }
<https://www.godbolt.org/z/eT9Y84Gx4>
Again, look at the two functions with -O0 and -O1, and tell me which is clearer.

Especially if you let the compiler do what it likes, like using its knowledge of that call process, which is not displayed here in the optimised code of the function body.
  So I have some questions of you:
 * How exactly is a _Bool value (which occupies one byte) translated to a 32-bit signed integer? What is involved?
 
A _Bool is always either 0 or 1.  The conversion is whatever the compiler needs to give an int of value 0 or 1.
The implementation details depend entirely on the target.  Typically, if the _Bool is in memory, then a single byte is read and zero-extended to the width of a register.  If the _Bool is passed to a function as a parameter, it is usually already extended - but that will depend on the calling conventions.

This is machine independent other than the sizes mentioned.
>
The specification is independent - it is given by the C standards.  The implementation is most certainly not machine independent.  It is also not necessarily consistent - compilers can and do pick different code depending on the circumstances (where the _Bool came from, and how the int is going to be used).  You are even wrong in stating that a _Bool occupies one byte, since that is not a requirement for a C implementation (though I don't know of any real-world C implementations with larger _Bool's).

Given your answer, how does it correlate with either:
      mov eax,    edi     ; from your test; both optimised code
 
Looks fine.

    <nothing>           ; from my test
 
That's fine for the nonsense function you wrote.

  The advantage of unoptimised code is that it will contain everything that is normally involved; it doesn't throw anything away.
 
No, it does not - because normal code generated by C compilers used by C programmers who want sensible results will be the result of compiling with optimisation, and will look very different.

It doesn't require convincing the compiler that you're doing something useful to avoid it eliminating most or all your code, or turning it something that is just plain misleading.
 
You have /never/ understood how to look at generated code, have you?

That might be useful when compiling a huge production version of an app, but it is useless when trying to shed light on an isolated fragment of code.
 Look, just forget it, I'm not in the mood for another marathon subthread.
 
OK.  I expect the OP to understand these things better.

So, what's involved in turning Bool to int? According to your examples with -O1: nothing. You just copy 32 bits unchanged from one to the other. Mildly surprising, but you are of course right, right?
 
Yes, I am right - and I don't see that as even mildly surprising here. That's how you convert a _Bool in a register to an int in a register. You'll see equally little code when converting between signed and unsigned types, or various other integer types.

However, now *I* have a problem, figuring out why on earth C compiler does the conversion like this:
      movsx eax, byte [source]
 Because this must be wrong, right?
 
No.  You are asking it to do something else - you are asking it to load a _Bool from memory, not just convert it.

Date Sujet#  Auteur
7 Aug 24 * how cast works?122Thiago Adams
7 Aug 24 +* Re: how cast works?7Thiago Adams
7 Aug 24 i+- Re: how cast works?1Keith Thompson
12 Aug 24 i`* Re: how cast works?5Tim Rentsch
12 Aug 24 i `* Re: how cast works?4Vir Campestris
12 Aug 24 i  `* Challenge/exercise problem - signum() function3Tim Rentsch
12 Aug 24 i   `* Re: Challenge/exercise problem - signum() function2Lew Pitcher
12 Aug 24 i    `- Re: Challenge/exercise problem - signum() function1Tim Rentsch
7 Aug 24 +* Re: how cast works?107Dan Purgert
7 Aug 24 i+- Re: how cast works?1Keith Thompson
8 Aug 24 i+- Re: how cast works?1Lawrence D'Oliveiro
8 Aug 24 i+* Re: how cast works?101Thiago Adams
8 Aug 24 ii+* Re: how cast works?25Bart
8 Aug 24 iii`* Re: how cast works?24Michael S
8 Aug 24 iii +- Re: how cast works?1Thiago Adams
8 Aug 24 iii `* Re: how cast works?22Bart
8 Aug 24 iii  +* Re: how cast works?5Thiago Adams
8 Aug 24 iii  i+- Re: how cast works?1Thiago Adams
8 Aug 24 iii  i+* Re: how cast works?2Bart
8 Aug 24 iii  ii`- Re: how cast works?1Thiago Adams
8 Aug 24 iii  i`- Re: how cast works?1Keith Thompson
8 Aug 24 iii  `* Re: how cast works?16David Brown
8 Aug 24 iii   `* Re: how cast works?15Bart
9 Aug 24 iii    +* Re: how cast works?13David Brown
9 Aug 24 iii    i+* Re: how cast works?9Keith Thompson
9 Aug 24 iii    ii+* Re: how cast works?3Lawrence D'Oliveiro
9 Aug 24 iii    iii+- Re: how cast works?1Keith Thompson
9 Aug 24 iii    iii`- Re: how cast works?1James Kuyper
9 Aug 24 iii    ii`* Re: how cast works?5David Brown
9 Aug 24 iii    ii `* Re: how cast works?4Keith Thompson
12 Aug 24 iii    ii  `* Re: how cast works?3Tim Rentsch
12 Aug 24 iii    ii   `* Re: how cast works?2Keith Thompson
3 Sep 24 iii    ii    `- Re: how cast works?1Tim Rentsch
9 Aug 24 iii    i`* Re: how cast works?3Bart
9 Aug 24 iii    i `* Re: how cast works?2David Brown
10 Aug 24 iii    i  `- Re: how cast works?1Bart
9 Aug 24 iii    `- Re: how cast works?1Lawrence D'Oliveiro
8 Aug 24 ii`* Re: how cast works?75Keith Thompson
8 Aug 24 ii `* Re: how cast works?74Thiago Adams
8 Aug 24 ii  +* Re: how cast works?72Bart
9 Aug 24 ii  i+* Re: how cast works?47Keith Thompson
9 Aug 24 ii  ii+* Re: how cast works?38Bart
9 Aug 24 ii  iii+* Re: how cast works?2David Brown
12 Aug 24 ii  iiii`- Re: how cast works?1Bart
9 Aug 24 ii  iii+* Re: how cast works?29James Kuyper
9 Aug 24 ii  iiii+* Re: how cast works?14Bart
9 Aug 24 ii  iiiii+* Re: how cast works?3Keith Thompson
10 Aug 24 ii  iiiiii`* Re: how cast works?2Bart
10 Aug 24 ii  iiiiii `- Re: how cast works?1Keith Thompson
10 Aug 24 ii  iiiii`* Re: how cast works?10James Kuyper
13 Aug 24 ii  iiiii +- Re: how cast works?1David Brown
13 Aug 24 ii  iiiii +- Re: how cast works?1Bart
13 Aug 24 ii  iiiii `* Re: how cast works?7James Kuyper
13 Aug 24 ii  iiiii  `* Re: how cast works?6Bart
13 Aug 24 ii  iiiii   `* Re: how cast works?5Keith Thompson
13 Aug 24 ii  iiiii    `* Re: how cast works?4Bart
14 Aug 24 ii  iiiii     `* Re: how cast works?3Tim Rentsch
14 Aug 24 ii  iiiii      `* Re: how cast works?2Bart
18 Aug 24 ii  iiiii       `- Re: how cast works?1Tim Rentsch
9 Aug 24 ii  iiii+* Re: how cast works?2Keith Thompson
10 Aug 24 ii  iiiii`- Re: how cast works?1James Kuyper
9 Aug 24 ii  iiii`* Re: how cast works?12Kaz Kylheku
9 Aug 24 ii  iiii +* Re: how cast works?9Keith Thompson
10 Aug 24 ii  iiii i`* Re: how cast works?8Kaz Kylheku
10 Aug 24 ii  iiii i +* Re: how cast works?6Keith Thompson
10 Aug 24 ii  iiii i i+* Re: how cast works?3Kaz Kylheku
10 Aug 24 ii  iiii i ii+- Re: how cast works?1Keith Thompson
10 Aug 24 ii  iiii i ii`- Re: how cast works?1James Kuyper
10 Aug 24 ii  iiii i i`* Re: how cast works?2Bart
13 Aug 24 ii  iiii i i `- Re: how cast works?1David Brown
12 Aug 24 ii  iiii i `- Re: how cast works?1Tim Rentsch
10 Aug 24 ii  iiii +- Re: how cast works?1James Kuyper
12 Aug 24 ii  iiii `- Re: how cast works?1Tim Rentsch
9 Aug 24 ii  iii+* Re: how cast works?4Keith Thompson
9 Aug 24 ii  iiii`* Re: how cast works?3Bart
9 Aug 24 ii  iiii `* Re: how cast works?2Keith Thompson
9 Aug 24 ii  iiii  `- Re: how cast works?1Bart
12 Aug 24 ii  iii`* Re: how cast works?2Tim Rentsch
12 Aug 24 ii  iii `- Re: how cast works?1Bart
12 Aug 24 ii  ii`* Re: how cast works?8Tim Rentsch
12 Aug 24 ii  ii `* Re: how cast works?7Bart
12 Aug 24 ii  ii  +- Re: how cast works?1Keith Thompson
12 Aug 24 ii  ii  `* Re: how cast works?5Tim Rentsch
12 Aug 24 ii  ii   `* Re: how cast works?4Keith Thompson
12 Aug 24 ii  ii    `* Re: how cast works?3Ben Bacarisse
12 Aug 24 ii  ii     `* Re: how cast works?2Tim Rentsch
12 Aug 24 ii  ii      `- Re: how cast works?1Keith Thompson
9 Aug 24 ii  i`* Re: how cast works?24Thiago Adams
9 Aug 24 ii  i +* Re: how cast works?2Bart
9 Aug 24 ii  i i`- Re: how cast works?1Keith Thompson
9 Aug 24 ii  i +* Re: how cast works?19David Brown
9 Aug 24 ii  i i`* Re: how cast works?18Thiago Adams
9 Aug 24 ii  i i +* Re: how cast works?3Thiago Adams
9 Aug 24 ii  i i i+- Re: how cast works?1David Brown
9 Aug 24 ii  i i i`- Re: how cast works?1Keith Thompson
9 Aug 24 ii  i i +* Re: how cast works?11David Brown
10 Aug 24 ii  i i i`* Re: how cast works?10Bart
10 Aug 24 ii  i i i `* Re: how cast works?9Thiago Adams
10 Aug 24 ii  i i i  `* Re: how cast works?8Bart
11 Aug 24 ii  i i i   `* Re: how cast works?7Thiago Adams
11 Aug 24 ii  i i i    `* Re: how cast works?6Keith Thompson
9 Aug 24 ii  i i `* Re: how cast works?3Keith Thompson
9 Aug 24 ii  i `* Re: how cast works?2Keith Thompson
9 Aug 24 ii  `- Re: how cast works?1David Brown
8 Aug 24 i`* Re: how cast works?3Stefan Ram
7 Aug 24 +* Re: how cast works?6Keith Thompson
8 Aug 24 `- Re: how cast works?1Lawrence D'Oliveiro

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal