• AVR Freaks

LockedXC8 and PIC16F1829 limitation error on large numbers

Page: 123 > Showing page 1 of 3
Author
viki2000
Super Member
  • Total Posts : 344
  • Reward points : 0
  • Joined: 2013/05/08 16:54:07
  • Location: Germany
  • Status: offline
2017/11/17 03:42:04 (permalink)
0

XC8 and PIC16F1829 limitation error on large numbers

Here is code used:
#include <xc.h>
#include <pic16f1829.h>
#include "LCD.h"
#include <stdlib.h>

#define _XTAL_FREQ 32000000

// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)

// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = ON // PLL Enable (4x PLL enabled)
#pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)

void init();

void main(){
init();
Lcd_Init();

signed long long int TFINE, P2, P3, var1, temp1, temp2;
char buf [10];

TFINE=170000;
P2=-10835;
P3=3024;

var1 = (((signed long long int)TFINE)>>1) - (signed long long int)64000;
temp1 = (((signed long long int)P3 * (((var1>>2)*(var1>>2)) >> 13)) >>3);
temp2 = ((((signed long long int)P2) * var1)>>1);
//var1 = (temp1+temp2)>>18;
var1 = ((((signed long long int)P3 * (((var1>>2)*(var1>>2)) >> 13)) >>3) + ((((signed long long int)P2) * var1)>>1))>>18;

Lcd_Clear();
Lcd_Set_Cursor(1,1);
ltoa(buf, var1, 10);
Lcd_Write_String(buf); // buf contains the converted string

while(1);
}

void init(){
    OSCCON = 0xF2; //oscillation frequency 32 MHz
    ANSELA = 0x00;
    ANSELB = 0x00;
    ANSELC = 0x00;
    TRISA = 0x00;
    TRISB = 0x00;
    TRISC = 0x00;
    PORTA=0;
    PORTB=0;
    PORTC=0;
}

Let’s focus on next part of the code:
signed long long int TFINE, P2, P3, var1, temp1, temp2;

TFINE=170000;
P2=-10835;
P3=3024;

var1 = (((signed long long int)TFINE)>>1) - (signed long long int)64000;
temp1 = (((signed long long int)P3 * (((var1>>2)*(var1>>2)) >> 13)) >>3);
temp2 = ((((signed long long int)P2) * var1)>>1);
//var1 = (temp1+temp2)>>18;
var1 = ((((signed long long int)P3 * (((var1>>2)*(var1>>2)) >> 13)) >>3) + ((((signed long long int)P2) * var1)>>1))>>18;

When the long expression var1 above is split in 2 using temp1 and temp2, then it gives correct result -430.
When var1 is written as long expression, then it is evaluated wrong, giving a false result +7762.
The same code written with CCS compiler and the same PIC/hardware, gives a good result, no matter if var1 is written long or split in 2.
 
Is this a limitation error of XC8? Specific for PIC161829? Or Pic16F?
With PIC18F4550 and XC8 I have no problems.
XC8 used version 1.41.
post edited by viki2000 - 2017/11/17 03:43:43
#1

48 Replies Related Threads

    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 03:48:33 (permalink)
    0
    Could you try
    TFINE=170000L;
    or
    TFINE=170000UL;

    GENOVA :D :D ! GODO
    #2
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 03:55:24 (permalink)
    +2 (2)
    There is no such type as "long long" in XC8.
    You can put as many "long"s as you want, you will only get the effect of a single "long".
     
    #3
    viki2000
    Super Member
    • Total Posts : 344
    • Reward points : 0
    • Joined: 2013/05/08 16:54:07
    • Location: Germany
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 04:44:18 (permalink)
    +1 (1)
    i have used long long because I have tried the code with different other compilers and C general says it like that:
    https://en.wikipedia.org/wiki/C_data_types
    I removed long long and I defined them only signed long and unsigned long.
    The error still remains.
    Then I have tried TFINE=170000L; and also TFINE=170000UL;
    The error is still the same.
    Then I tried PIC16F88 and also PIC16F1937.
    I got the same error.
    Only with PIC18F the error does not appear.
    So, what is wrong with XC8 and PIC16F on the above long expression?
    #4
    viki2000
    Super Member
    • Total Posts : 344
    • Reward points : 0
    • Joined: 2013/05/08 16:54:07
    • Location: Germany
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 04:49:52 (permalink)
    +1 (1)
    And by the way, XC8 user manual page 143:
    http://ww1.microchip.com/downloads/en/DeviceDoc/52053B.pdf
    there is "signed long long" and "unsigned long long" as 32bit size.
    And here with a different size (64):
    http://microchipdeveloper.com/tls2101:type-qualifiers
     
    #5
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 04:56:25 (permalink)
    +2 (2)
    viki2000
    And by the way, XC8 user manual page 143:
    http://ww1.microchip.com/downloads/en/DeviceDoc/52053B.pdf
    there is "signed long long" and "unsigned long long" as 32bit size.

    Which just tells you that "signed long" and "signed long long" give you the same thing.
    For that matter, you can do "signed long long long long" and you will still only get 32 bits.
     

    And here with a different size (64):
    http://microchipdeveloper.com/tls2101:type-qualifiers

    That is a generic C guide, it is not specifically about XC8.
    As you can see, it implies 64 bit long long, but the XC8 manual has already told you that will be treated as 32 bit.
    Putting "long long" in your source just makes it look like you are trying to use 64 bits.
     
    #6
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 04:58:08 (permalink)
    0
    All in all, I don't see a need for anything bigger than 32bits in the above code...

    GENOVA :D :D ! GODO
    #7
    1and0
    Access is Denied
    • Total Posts : 10999
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 05:00:22 (permalink)
    +2 (2)
    I have not looked into your code in details, but I think the issue here is shifting of signed values.
    #8
    1and0
    Access is Denied
    • Total Posts : 10999
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 05:08:47 (permalink)
    +1 (1)
    viki2000
    signed long long int TFINE, P2, P3, var1, temp1, temp2;

    TFINE=170000;
    P2=-10835;
    P3=3024;

    var1 = (((signed long long int)TFINE)>>1) - (signed long long int)64000;
    temp1 = (((signed long long int)P3 * (((var1>>2)*(var1>>2)) >> 13)) >>3);
    temp2 = ((((signed long long int)P2) * var1)>>1);
    //var1 = (temp1+temp2)>>18;
    var1 = ((((signed long long int)P3 * (((var1>>2)*(var1>>2)) >> 13)) >>3) + ((((signed long long int)P2) * var1)>>1))>>18;

    When the long expression var1 above is split in 2 using temp1 and temp2, then it gives correct result -430.
    When var1 is written as long expression, then it is evaluated wrong, giving a false result +7762.

    I see what are you doing now.  However, I just run your code quickly here and I got -430 for both expressions.
     
    #9
    viki2000
    Super Member
    • Total Posts : 344
    • Reward points : 0
    • Joined: 2013/05/08 16:54:07
    • Location: Germany
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 05:09:43 (permalink)
    +1 (1)
    If long long long does not matter, then let's just focus on the calculation error mentioned.
    If the "shifting of signed values" would be a problem, then why:
    - the expression is evaluated correctly when is spilt in 2?
    - works fine as longer expression with PIC18F, but not with PIC16F?
    #10
    viki2000
    Super Member
    • Total Posts : 344
    • Reward points : 0
    • Joined: 2013/05/08 16:54:07
    • Location: Germany
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 05:11:56 (permalink)
    0
    We were writing in the same time.
    When you say "I just run your code quickly here and I got -430 for both expressions.", could please tell me how, where?
    On simulator? On real device?
    What PIC? Did you try a PIC16F? Which one?
    I have that error with PIC16F1829, PIC16F88, PIC16F1937.
    And no problems with PIC18F4550.
     
    #11
    1and0
    Access is Denied
    • Total Posts : 10999
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 05:20:40 (permalink)
    +1 (1)
    At my computer with XC8 v1.44 Free mode.
    With MPLAB v8.92 Simulator. Not real device.
    All the PICs mentioned in your post. Yes, even a PIC16... F1829, F88, F1937.
    Same with PIC18F4550.
     
    All gives result of -430.
     
    #12
    du00000001
    Just Some Member
    • Total Posts : 3846
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 05:20:40 (permalink)
    +1 (1)
    long long is not supported.
    And the term P2 * var1 (-10835 * 170000) clearly exceeds the 32 Bit signed range. That's the reason.
    ("Anything else" should work with 32 bit signed/unsigned math.)
    What exactly are you complaining about ???

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #13
    1and0
    Access is Denied
    • Total Posts : 10999
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 05:25:50 (permalink)
    +1 (1)
    du00000001
    What exactly are you complaining about ???

    OP is complaining that 

    temp1 = (expression A);
    temp2 = (expression B);
    var1 = (temp1 + temp2) >> 18;

    gives different result comparing to

    var1 = ((expression A) + (expression B)) >> 18;

     
    #14
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 05:26:48 (permalink)
    0
    hmmm, var1 should be 170000/2=85000 - 64000 = 21000 ...

    GENOVA :D :D ! GODO
    #15
    1and0
    Access is Denied
    • Total Posts : 10999
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 05:28:50 (permalink)
    +1 (1)
    DarioG
    hmmm, var1 should be 170000/2=85000 - 64000 = 21000 ...

    It does.
    #16
    viki2000
    Super Member
    • Total Posts : 344
    • Reward points : 0
    • Joined: 2013/05/08 16:54:07
    • Location: Germany
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 05:31:06 (permalink)
    0
    In Simulator it works also for me.
    You must try with a real device PIC16F.
     
    I am complaining about the evaluation result of the next long expression:
    var1 = ((((signed long)P3 * (((var1>>2)*(var1>>2)) >> 13)) >>3) + ((((signed long)P2) * var1)>>1))>>18;

     
    If the same PIC16F with the above expression and type of variables signed int32 is used with CCS compiler, then it gives good result. When is used with XC8, then gives wrong result.
    If I used PIC18F instead of PIC16F with XC8, then I get good result.
    So, why only the combination of XC8 and PIC16F gives wrong results?
    #17
    1and0
    Access is Denied
    • Total Posts : 10999
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 05:35:04 (permalink)
    +1 (1)
    How are you checking the result on a real PIC16?
    #18
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 05:37:01 (permalink)
    +1 (1)
    Yep, if SIM works and HW not, then it's not a compiler issue...

    GENOVA :D :D ! GODO
    #19
    viki2000
    Super Member
    • Total Posts : 344
    • Reward points : 0
    • Joined: 2013/05/08 16:54:07
    • Location: Germany
    • Status: offline
    Re: XC8 and PIC16F1829 limitation error on large numbers 2017/11/17 05:37:24 (permalink)
    0
    i look on a LCD 4x20 attached to the PIC16F or PIC18F.
    The hardware stays the same when I test with CCS.
    #20
    Page: 123 > Showing page 1 of 3
    Jump to:
    © 2020 APG vNext Commercial Version 4.5