Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #387444

Re: how cast works?

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: how cast works?
Date 2024-08-09 13:26 -0700
Organization None to speak of
Message-ID <87ikw9788w.fsf@nosuchdomain.example.com> (permalink)
References (4 earlier) <v93a3t$6q7v$1@dont-email.me> <v93e2q$8put$1@dont-email.me> <v94smd$mgp8$1@dont-email.me> <v95j4r$qh1q$3@dont-email.me> <v95okr$2oa92$1@dont-email.me>

Show all headers | View raw


Thiago Adams <thiago.adams@gmail.com> writes:
> Em 8/9/2024 2:20 PM, David Brown escreveu:
>> On 09/08/2024 12: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
>>>
>>> or.. this is defined by the IIE ... standard as ...
>>>
>>>
>> It would be helpful if you made more of an effort to write clearly
>> here.   (We know you can do so when you want to.)  It is very
>> difficult to follow what you are referring to here - what is "this
>> case" here?  A conversion from a double to an int certainly does not
>> re-interpret or copy bits - like other conversions, it copies the
>> /value/ to the best possible extent given the limitations of the
>> types.
>
> Everything is a bit mixed up, but I'll try to explain the part about
> registers that I have in mind.

Why you keep talking about registers?  The C standard does not talk
about registers, and the promotion rules are based on which *operations*
are available, not what kinds of registers a given CPU happens to have.

> In C, when you have an expression like char + char, each char is
> promoted to int. The computation then occurs as int + int.

Right.  And the most important thing to keep in mind is that it works
that way *because the C standard says so*.

If you're looking for a rationale for this, it's probably because some
early C compilers were for target systems that didn't provide operations
on narrow types.  (The PDP-11 does have instructions that operate on
8-bit bytes, but the arithmetic instructions operate only on 16-bit
words.)  But the C standard rules apply to all conforming C
implementations.  If a target CPU happens to provide a 1-byte add
instruction, a C compiler can't use it unless it can prove that the
result is consistent with C semantics.

Knowing why the C standard says what it says can be interesting, but
it's not necessarily directly useful.

> On the other hand, when you have float + float, it remains as float + float.

That's implementation-defined.  Read the "Characteristics of floating
types <float.h>" section of the C standard (it's 5.2.5.3.3 in the N3220
draft) and look for FLT_EVAL_METHOD.  If FLT_EVAL_METHOD (defined in
<float.h>) is 0, float operations are done in type float.  If it's 1,
float operations are done in type double.  If it's 2, all floating-point
operations are done in type long double.

> My guess for this design is that computations involving char are done
> using registers that are the size of an int.

Who says the target CPU even has registers, or that computations can't
be done directly on values in memory?

> But, float + float is not promoted to double, so I assume that the
> computer has specific float registers or similar operation
> instructions for float.

Again, some CPUs have floating-point registers and some do not.  Those
that don't might store floating-point values in the same registers used
for integer values, applying different instructions to operate on them.
Of those that do have floating-point registers, some have registers that
can hold a double value (typically 64 bits); they may or may not be able
to treat a half-register as a 32-bot float value.

The rules in the C standard are, for the most part, based on the
capabilities of CPUs that existed when the standard was written, not
necessarily on modern CPUs (though there have been tweaks in later
editions).

> Regarding the part about signed/unsigned registers and operations, I
> must admit that I'm not sure. I was planning to check on Compiler
> Explorer, but I haven't done that yet.
>
> I can frame the question like this: Does the computer make a
> distinction when adding signed versus unsigned integers? Are there
> specific assembly instructions for signed versus unsigned operations,
> covering all possible combinations?

Maybe.

What is your goal here?  Are you trying to understand the history behind
the rules given in the C standard, or trying to understand the rules
themselves, or both?

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-07 08:28 -0300
  Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-07 08:33 -0300
    Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-07 13:13 -0700
    Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 17:43 -0700
      Re: how cast works? Vir Campestris <vir.campestris@invalid.invalid> - 2024-08-12 11:51 +0100
        Challenge/exercise problem - signum() function Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-12 08:17 -0700
          Re: Challenge/exercise problem - signum() function Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2024-08-12 16:07 +0000
            Re: Challenge/exercise problem - signum() function Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-12 09:57 -0700
  Re: how cast works? Dan Purgert <dan@djph.net> - 2024-08-07 20:00 +0000
    Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-07 13:26 -0700
    Re: how cast works? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-08-07 23:00 +0000
    Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-08 08:14 -0300
      Re: how cast works? Bart <bc@freeuk.com> - 2024-08-08 14:23 +0100
        Re: how cast works? Michael S <already5chosen@yahoo.com> - 2024-08-08 19:32 +0300
          Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-08 14:11 -0300
          Re: how cast works? Bart <bc@freeuk.com> - 2024-08-08 18:29 +0100
            Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-08 14:50 -0300
              Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-08 14:57 -0300
              Re: how cast works? Bart <bc@freeuk.com> - 2024-08-08 19:01 +0100
                Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-08 15:13 -0300
              Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-08 12:29 -0700
            Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-08 19:58 +0200
              Re: how cast works? Bart <bc@freeuk.com> - 2024-08-08 20:09 +0100
                Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-09 00:32 +0200
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-08 16:14 -0700
                Re: how cast works? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-08-09 02:47 +0000
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-08 22:55 -0700
                Re: how cast works? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-09 02:08 -0400
                Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-09 18:16 +0200
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 12:18 -0700
                Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 17:07 -0700
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-11 20:14 -0700
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-09 01:56 +0100
                Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-09 19:08 +0200
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-10 11:03 +0100
                Re: how cast works? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-08-09 02:45 +0000
      Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-08 12:42 -0700
        Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-08 17:34 -0300
          Re: how cast works? Bart <bc@freeuk.com> - 2024-08-08 22:41 +0100
            Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-08 16:17 -0700
              Re: how cast works? Bart <bc@freeuk.com> - 2024-08-09 11:04 +0100
                Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-09 19:12 +0200
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-12 15:36 +0100
                Re: how cast works? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-09 13:57 -0400
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-09 21:59 +0100
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 14:47 -0700
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-10 00:32 +0100
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 17:12 -0700
                Re: how cast works? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-09 18:29 -0400
                Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-13 11:18 +0200
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-13 11:34 +0100
                Re: how cast works? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-13 07:51 -0400
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-13 14:01 +0100
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-13 12:46 -0700
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-13 21:51 +0100
                Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-13 16:46 -0700
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-14 00:56 +0100
                Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-18 03:37 -0700
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 14:29 -0700
                Re: how cast works? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-09 18:35 -0400
                Re: how cast works? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-08-09 21:30 +0000
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 14:57 -0700
                Re: how cast works? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-08-09 23:14 +0000
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 16:58 -0700
                Re: how cast works? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-08-10 00:06 +0000
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 17:27 -0700
                Re: how cast works? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-09 20:31 -0400
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-10 01:11 +0100
                Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-13 11:23 +0200
                Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 17:32 -0700
                Re: how cast works? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-09 18:35 -0400
                Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 17:27 -0700
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 12:23 -0700
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-09 21:31 +0100
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 13:49 -0700
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-09 22:01 +0100
                Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-12 00:33 -0700
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-12 12:21 +0100
              Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 17:46 -0700
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-12 02:00 +0100
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-11 20:23 -0700
                Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 20:37 -0700
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-11 21:33 -0700
                Re: how cast works? Ben Bacarisse <ben@bsb.me.uk> - 2024-08-12 16:57 +0100
                Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-12 10:04 -0700
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-12 13:35 -0700
            Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-09 07:57 -0300
              Re: how cast works? Bart <bc@freeuk.com> - 2024-08-09 16:25 +0100
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 12:06 -0700
              Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-09 19:20 +0200
                Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-09 15:54 -0300
                Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-09 16:05 -0300
                Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-09 21:43 +0200
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 13:28 -0700
                Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-09 22:01 +0200
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-10 11:17 +0100
                Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-10 10:15 -0300
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-10 17:14 +0100
                Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-10 20:01 -0300
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-10 17:10 -0700
                Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-11 09:23 -0300
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-11 13:30 +0100
                Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-11 14:16 -0300
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-11 13:38 -0700
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-12 12:24 +0100
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 13:26 -0700
                Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-09 18:01 -0300
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 14:53 -0700
              Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 12:03 -0700
                Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-09 16:22 -0300
          Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-09 00:36 +0200
    Re: how cast works? Dan Purgert <dan@djph.net> - 2024-08-08 14:08 +0000
    Re: how cast works? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-08-09 02:42 +0000
  Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-07 13:08 -0700
    Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-08 08:35 -0300
      Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-08 12:39 -0700
      Re: how cast works? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-08 18:40 -0400
        Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 17:15 -0700
    Is there an audio book version (Was: how cast works?) gazelle@shell.xmission.com (Kenny McCormack) - 2024-08-08 16:19 +0000
  Re: how cast works? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-08-07 23:03 +0000

csiph-web