Re: how cast works?

Liste des GroupesRevenir à cl c  
Sujet : Re: how cast works?
De : bc (at) *nospam* freeuk.com (Bart)
Groupes : comp.lang.c
Date : 09. Aug 2024, 17:25:41
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v95cdl$pek8$1@dont-email.me>
References : 1 2 3 4 5 6 7
User-Agent : Mozilla Thunderbird
On 09/08/2024 11:57, Thiago Adams wrote:
Em 8/8/2024 6:41 PM, Bart escreveu:
On 08/08/2024 21:34, Thiago Adams wrote:
On 08/08/2024 16:42, Keith Thompson wrote:
Thiago Adams <thiago.adams@gmail.com> writes:
On 07/08/2024 17:00, Dan Purgert wrote:
On 2024-08-07, Thiago Adams wrote:
[...]
How about floating point?
Floating point is a huge mess, and has a few variations for
encoding;
though I think most C implementations use the one from the IEEE on 1985
(uh, IEEE754, I think?)
>
I didn't specify properly , but my question was more about floating
point registers. I think in this case they have specialized registers.
>
Who is "they"?
>
Some CPUs have floating-point registers, some don't.  C says nothing
about registers.
>
What exactly is your question?  Is it not already answered by reading
the "Conversions" section of the C standard?
>
>
>
This part is related with the previous question about the origins of integer promotions.
>
We don't have "char" register or signed/unsigned register. But I believe we may have double and float registers. So float does not need to be converted to double.
>
There is no specif question here, just trying to understand the rationally behind the conversions rules.
>
The rules have little to do with concrete machines with registers.
>
Your initial post showed come confusion about how conversions work. They are not performed 'in-place', any more than writing `a + 1` changes the value of `a`.
>
Take:
>
     int a; double x;
>
     x = (double)a;
>
The cast is implicit here but I've written it out to make it clear. My C compiler produces intermediate code like this before converting it to native code:
>
     push x   r64                   # r64 means float64
     fix      r64 -> i32
     pop  a   i32
>
I could choose to interprete this code just as it is. Then, in this execution model, there are no registers at all, only a stack that can hold data of any type.
>
The 'fix' instruction pops the double value from the stack, converts it to int (which involves changing both the bit-pattern, and the bit-width), and pushes it back onto the stack.
>
Registers come into it when running it directly on a real machine. But you seem more concerned with safety and correctness than performance, so there's probably no real need to look at actual generated native code.
>
That'll just be confusing (especially if you follow the advice to generate only optimised code).
>
>
>
>
This part was always clear to me:
 "They  are not performed 'in-place', any more than writing `a + 1` changes the value of `a`."
 Lets take double to int.
 In this case the bits of double needs to be reinterpreted (copied to) int.
 So the answer "how it works" can be
 always/generally machine has a instruction to do this
If it supports those types in hardware, then it will probably have conversion instructions.
But I've also used machines without FP hardware, so it had to be emulated in software. Then such a conversion would be done by a function in the runtime library.
(Typically, also, such a machine had a FP type which might have been twice the width of a machine word, eg. `int` was `i16`, and `float` was `f32`. So those functions had to deal with that too.
Currently we're in a golden age where nearly everything is 64 bits, and both 64-bit ints and floats suffice for most purposes. So that aspect is no longer an issue.
Other than you now have to juggle 4 integer widths instead of just 2 or 3.)

 

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