Deutsch English Français Italiano |
<v95sij$1arjo$3@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: how cast works? Date: Fri, 9 Aug 2024 22:01:22 +0200 Organization: A noiseless patient Spider Lines: 64 Message-ID: <v95sij$1arjo$3@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> <v95j4r$qh1q$3@dont-email.me> <v95okr$2oa92$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 09 Aug 2024 22:01:23 +0200 (CEST) Injection-Info: dont-email.me; posting-host="20dc90042b48f6417a3734e9d0abbff5"; logging-data="1404536"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+RAW55KrthyNuiSWYcKoeh7FzcuaGZ+V4=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:pSoBKy6LGDodzkypDvGXoAAN4p8= Content-Language: en-GB, nb-NO In-Reply-To: <v95okr$2oa92$1@dont-email.me> Bytes: 3901 On 09/08/2024 20:54, Thiago Adams wrote: > > Everything is a bit mixed up, but I'll try to explain the part about > registers that I have in mind. > > In C, when you have an expression like char + char, each char is > promoted to int. The computation then occurs as int + int. > Yes. In C, there are no arithmetic operations on types smaller than "int". > On the other hand, when you have float + float, it remains as float + > float. > Yes. The rules for promotions are quite clear in the standards. You can also read about them here: <https://en.cppreference.com/w/c/language/conversion> > My guess for this design is that computations involving char are done > using registers that are the size of an int. > It is quite possible that this was the original motivation. But you keep referring to "the computer". There is no "the computer" in C. There are processors with 128 64-bit integer registers, and processors with a single 8-bit register. Some have no floating point hardware at all, some have 128-bit floating point hardware. C is defined in a manner that is mostly independent of the hardware, with only a few points that are dependent (such as the width of integer types). But design decisions in C can certainly have been inspired by real existing hardware. > But, float + float is not promoted to double, so I assume that the > computer has specific float registers or similar operation instructions > for float. > > 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 don't know what you are referring to here. But if you are using compiler explorer, I encourage you to look at the generated output for a wide range of targets, including 8-bit AVR, 16-bit MSP430, 32-bit ARM, and 64-bit x86. Use gcc -O1 or -O2 in every case. (Ignore Bart's ignorant blatherings about optimisation.) > 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? > Without specifying "the computer", the question is not particularly meaningful. However, it's fair to say that on most processors most arithmetic operations are the same for signed and unsigned types as long as the operation is done at a size that the target supports (otherwise it may need sign or zero extensions if it only supports larger sizes).