Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.swapon.de!fu-berlin.de!uni-berlin.de!news.dfncis.de!not-for-mail From: =?ISO-8859-1?Q?Hans-Bernhard_Br=F6ker?= Newsgroups: comp.arch.embedded,comp.lang.c Subject: Re: Getting started with AVR and C Date: Thu, 29 Nov 2012 01:47:53 +0100 Lines: 31 Message-ID: References: <22t1b8pt63pcb3fks5ic03tsnlnhlm6ef1@4ax.com> <86boem66on.fsf@gray.siamics.net> <0.4196fd606ae8a0c4ce3e.20121124221204GMT.87zk26y7kb.fsf@bsb.me.uk> <86pq2y1bcy.fsf@gray.siamics.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.dfncis.de d6xPR7f/svrJW/DDFJ96nAYbW21zvkLWz5GzoLg7na4a+++om+5wxKtDGymMvZIdtFNlqeLloo Cancel-Lock: sha1:+E3bj1rUtcyxNVjANT0KSSlU3Wk= User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/17.0 Thunderbird/17.0 In-Reply-To: Xref: csiph.com comp.arch.embedded:10612 comp.lang.c:28763 On 29.11.2012 01:35, David Brown wrote: > The correct behaviour for C standards compliance is that when you > multiply two operands of different int size, the smaller one is promoted > to the size of the larger one. Close, but not cigar. You forgot about types smaller than the platform's "int". Those will be converted up to either signed or unsigned int anyway, i.e. even if both operands are of the same size. > So the bit-size of the processor, and the bit-size of "int" on that > particular target, is irrelevant. Incorrect. It is very relevant as soon as either of the operands' types is smaller than "int" on the particular target. The rule to remember is that C never does arithmetic on anything smaller than an 'int'. > Given: > > int16_t a, b; > int32_t c > > If you write just "c = a * b", then the multiplication is carried it at > 16-bit, then promoted to 32-bit. Not if you're on a 32-bit target it isn't. Default conversion to 32-bit int takes place first, so both operands are first converted to 32-bit, then a 32 x 32 --> 32 bit multiply is carried out. At least in principle (that is: modulo the "as-if rule").