Deutsch   English   Français   Italiano  
<v95cdl$pek8$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bart <bc@freeuk.com>
Newsgroups: comp.lang.c
Subject: Re: how cast works?
Date: Fri, 9 Aug 2024 16:25:41 +0100
Organization: A noiseless patient Spider
Lines: 109
Message-ID: <v95cdl$pek8$1@dont-email.me>
References: <v8vlo9$2oc1v$1@dont-email.me> <slrnvb7kis.28a.dan@djph.net>
 <v929ah$3u7l7$1@dont-email.me> <87ttfu94yv.fsf@nosuchdomain.example.com>
 <v93a3t$6q7v$1@dont-email.me> <v93e2q$8put$1@dont-email.me>
 <v94smd$mgp8$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 09 Aug 2024 17:25:41 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="a5ef0e336b797e59eb82473df0f29c2d";
	logging-data="834184"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18GcRANZxNG3z5Wi0J3bGwd"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:F3iJNT1U4c+Zi9wMdepTpQdMWhw=
In-Reply-To: <v94smd$mgp8$1@dont-email.me>
Content-Language: en-GB
Bytes: 5045

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.)

> 
>