32 Bit multiplication with PIChttps://www.microchip.com/forums/(c) Microchip30Re: 32 Bit multiplication with PIC (LostInSpace)Ric is of course correct, unfortunately you don't know which 'assumptions to adjust' until one of them jumps up and bites you. ;-)<br/> <br/>https://www.microchip.com/forums/FindPost/1116031Mon, 21 Oct 2019 09:14:44 GMTRe: 32 Bit multiplication with PIC (NorthGuy)<blockquote class="quote"><span class="original">1and0</span><br/>In C99, is the unsuffixed decimal literal 18446744073709551615 an "unsigned long long" or it's out of range?<br/></blockquote><br><br><br/>"If an integer constant cannot be represented by any type in its list and has no extended integer type, then the integer constant has no type." (C99 - 6.4.4.1.6)<br/> <br/>https://www.microchip.com/forums/FindPost/1116024Mon, 21 Oct 2019 07:42:48 GMTRe: 32 Bit multiplication with PIC (JPortici)<blockquote class="quote"><span class="original">jtemples</span><br/>I don't know if XC8 C99 has a "pedantic" mode like gcc does<br></blockquote><br/> <br/>Yes, there is: you have to set warning levels at the minimum level possible and enable the treat warning as errors option. Annoying because it throws all the (752) warnings<br/> <br/>Annoying like you have to cast every single array index, let it be literal or variable<br/> <br/>-mwarn=level (from -9 to 9)<br/>-Wpedantic (which will warning that the c library may throw errors)<br/> <br/>no option to treat warnings as errors though<br/> <br/>https://www.microchip.com/forums/FindPost/1115991Sun, 20 Oct 2019 22:55:49 GMTRe: 32 Bit multiplication with PIC (jtemples)I don't know if XC8 C99 has a "pedantic" mode like gcc does, but this is what gcc -pedantic-errors -std=c99 gives:<br/><pre class="prettyprint">error: integer constant is so large that it is unsigned<br />18446744073709551615;</pre>Without -pedantic-errors, it just gives a warning similar to XC8.<br/>https://www.microchip.com/forums/FindPost/1115986Sun, 20 Oct 2019 20:07:37 GMTRe: 32 Bit multiplication with PIC (1and0)XC8 v2 in C99 mode gives this warning:<br/> <br/><font face="courier new,courier">warning: integer literal is too large to be represented in a signed integer type, interpreting as unsigned [-Wimplicitly-unsigned-literal]</font><br/>https://www.microchip.com/forums/FindPost/1115985Sun, 20 Oct 2019 19:36:59 GMTRe: 32 Bit multiplication with PIC (jtemples)Unsuffixed decimal constants are never unsigned in C99. The special case in C90 that allowed that was removed in C99.<br/>https://www.microchip.com/forums/FindPost/1115982Sun, 20 Oct 2019 18:24:19 GMTRe: 32 Bit multiplication with PIC (1and0)<blockquote class="quote"><span class="original">jtemples</span><br/><blockquote class="quote"><span class="original"></span>All decimal constants are signed.</blockquote><br/>In C90, an unsuffixed decimal constant can be "unsigned long".<br/></blockquote><br/>That brings me to this question. In C99, is the unsuffixed decimal literal 18446744073709551615 an "unsigned long long" or it's out of range?<br/> <br/>https://www.microchip.com/forums/FindPost/1115978Sun, 20 Oct 2019 17:39:41 GMTRe: 32 Bit multiplication with PIC (ric)This is the usual confusion when people have learnt C on a 32 bit machine, then change to an 8 bit machine without adjusting their assumptions. :)<br/> <br/>https://www.microchip.com/forums/FindPost/1115975Sun, 20 Oct 2019 16:00:08 GMTRe: 32 Bit multiplication with PIC (BEsmart)...you are rigth! :-)<br/>Everything works fine, if one of the operators is unsigned long.<br/><pre class="prettyprint">intervall = 18 * 5000UL;<br />sprintf (outstring , "%s%lu" , intervall) displays: 90000 -> Now OK!<br /><br />intervall = 18UL * 5000;<br />sprintf (outstring , "%s%lu" , intervall) displays: 90000 -> Now OK!<br /><br />uint32_t number_1;<br />number_1 = 18;<br />intervall = number_1 * 5000; <br />sprintf (outstring , "%s%lu" , intervall) displays: 90000 -> Now OK!<br /><br />uint8_t number_1;<br />number_1 = 18;<br />intervall = (uint32_t) number_1 * 5000; <br />sprintf (outstring , "%s%lu" , intervall) displays: 90000 -> Now OK!</pre><br/>Many Thanks!! <br/> <br/>https://www.microchip.com/forums/FindPost/1115972Sun, 20 Oct 2019 15:08:46 GMTRe: 32 Bit multiplication with PIC (1and0)… because C90 does not have long long int. ;)<br/>https://www.microchip.com/forums/FindPost/1115969Sun, 20 Oct 2019 14:37:14 GMTRe: 32 Bit multiplication with PIC (jtemples)<blockquote class="quote"><span class="original"></span>All decimal constants are signed.</blockquote><br/> <br/>In C90, an unsuffixed decimal constant can be "unsigned long".<br/> <br/>https://www.microchip.com/forums/FindPost/1115967Sun, 20 Oct 2019 14:21:33 GMTRe: 32 Bit multiplication with PIC (NorthGuy)<blockquote class="quote"><span class="original">Jack_M</span><br/>case 2: at least one of hard coded constants in the operation doesn't fit in a signed int, so compiler will use longs for calculations<br/></blockquote><br><br><br/>Yes, 50000 has "long int" type. All decimal constants are signed. However, since C99, a hexadecimal constant (such as 0xc350, which is seemingly the same) may be unsigned and will be "unsigned int" if it fits into int. So, "40000 + 50000" would produce a result different from "0x9c40 + 0xc350".<br/> <br/>https://www.microchip.com/forums/FindPost/1115963Sun, 20 Oct 2019 13:56:15 GMTRe: 32 Bit multiplication with PIC (1and0)<blockquote class="quote"><span class="original">Jack_M</span><br/>please someone correct me.<br/> <br/>case 1: hard coded constant doesn't fit in a signed int (16 bit), compiler will use longs (32bit) for calculations<br/>case 2: at least one of hard coded constants in the operation doesn't fit in a signed int, so compiler will use longs for calculations<br/>case 3: both operands fit in a signed int, compiler will use signed ints for the result.<br/></blockquote><br>Yes, yes, yes; and then the result is converted to unsigned long by the = operator.<br/>https://www.microchip.com/forums/FindPost/1115959Sun, 20 Oct 2019 13:26:17 GMTRe: 32 Bit multiplication with PIC (JPortici)please someone correct me.<br/> <br/>case 1: hard coded constant doesn't fit in a signed int (16 bit), compiler will use longs (32bit) for calculations<br/>case 2: at least one of hard coded constants in the operation doesn't fit in a signed int, so compiler will use longs for calculations<br/>case 3: both operands fit in a signed int, compiler will use signed ints for the result.<br/>https://www.microchip.com/forums/FindPost/1115958Sun, 20 Oct 2019 13:24:31 GMTRe: 32 Bit multiplication with PIC (1and0)<blockquote class="quote"><span class="original">BEsmart</span><br/>But still multiplication is weird:<br/><pre class="prettyprint">uint32_t intervall;<br> <br> <br> <br>intervall = 8 * 5000;<br />sprintf (outstring , "%s%lu" , intervall) displays: 24464 -> weird!!! </pre><br/>btw. I'm using a PIC18LF46K42 and XC8 2.10 <br/></blockquote><br>8 is an int and 5000 is int, so the multiplication is performed using 16-bit with a result of an int which is 16-bit; i.e. the result will be truncated to 16-bit. So use 32-bit multiplication, that is at least one of the operand must be 32-bit, such as 8UL which is a long.<br/>https://www.microchip.com/forums/FindPost/1115957Sun, 20 Oct 2019 13:23:23 GMTRe: 32 Bit multiplication with PIC (BEsmart)...yes, that's true! ;-)<br>But still multiplication is weird:<br/><pre class="prettyprint">uint32_t intervall;<br> <br>intervall = 90000;<br />sprintf (outstring , "%s%lu" , intervall) displays: 90000 -> OK!<br /><br />intervall = 40000 + 50000;<br />sprintf (outstring , "%s%lu" , intervall) displays: 90000 -> OK! <br /><br />intervall = 8 * 5000;<br />sprintf (outstring , "%s%lu" , intervall) displays: 24464 -> weird!!! </pre><br/>btw. I'm using a PIC18LF46K42 and XC8 2.10 <br/>https://www.microchip.com/forums/FindPost/1115952Sun, 20 Oct 2019 13:04:51 GMTRe: 32 Bit multiplication with PIC (1and0)%ul will print 24464l<br/>%lu will print 90000<br/>Smile: <img src="https://www.microchip.com/forums/upfiles/smiley/209.gif" alt="Smile" data-smiley="<img src="https://www.microchip.com/forums/upfiles/smiley/209.gif" alt="Smile" data-smiley="[:)]" />" /><br/>https://www.microchip.com/forums/FindPost/1115950Sun, 20 Oct 2019 11:40:56 GMTRe: 32 Bit multiplication with PIC (BEsmart)OK, I'll do that<br/>https://www.microchip.com/forums/FindPost/1115938Sun, 20 Oct 2019 07:09:36 GMTRe: 32 Bit multiplication with PIC (pcbbc)No it’s a truncation to a 16-bit int as previously described.<br/>If the entire project is too big, post a minimum sample project which demonstrates your problem.<br/>https://www.microchip.com/forums/FindPost/1115937Sun, 20 Oct 2019 06:56:49 GMTRe: 32 Bit multiplication with PIC (NorthGuy)<blockquote class="quote"><span class="original">BEsmart</span><br/>...OK.<br/>Using %ul only leads to 24464l displayed and still not affecting the time intervall on th IO-Port, where the value is meaured as intervall in ms. So it is easy to see, if the value is correct.<br/></blockquote><br><br><br/>It is %lu not %ul.<br/>https://www.microchip.com/forums/FindPost/1115936Sun, 20 Oct 2019 06:56:33 GMT