• AVR Freaks

Hot!24 bit integers in MPLAB X IDE

Page: 12 > Showing page 1 of 2
Author
didierleplae
Super Member
  • Total Posts : 223
  • Reward points : 0
  • Joined: 2019/03/31 16:57:50
  • Location: 0
  • Status: offline
2021/01/18 16:03:28 (permalink)
0

24 bit integers in MPLAB X IDE

When I use type uint24_t in MPLAB X IDE it has a red underline, but builds anyway. At first I figured it was working and the red underline was just a bug. Now I'm realizing that it the uint24_t variables are just defaulting to 16 bit.
What do I need to do to get 24 bit variables to actually work?
#1

22 Replies Related Threads

    1and0
    Access is Denied
    • Total Posts : 12086
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: 24 bit integers in MPLAB X IDE 2021/01/18 16:08:18 (permalink)
    +2 (2)
    didierleplae
    When I use type uint24_t in MPLAB X IDE it has a red underline, but builds anyway. At first I figured it was working and the red underline was just a bug.

    If it builds successfully, then ignore the red underline.
     
    didierleplae
    Now I'm realizing that it the uint24_t variables are just defaulting to 16 bit.

    How you determine that it's 16 bits?
     
    didierleplae
    What do I need to do to get 24 bit variables to actually work?

    In C90 mode it is uint24_t, while in C99 mode it is __uint24.
     
    <edit> Just tested, both uint24_t and __uint24 types work in both C90 and C99 modes of XC8 v2.30. ;)
     
    post edited by 1and0 - 2021/01/18 16:24:08
    #2
    ric
    Super Member
    • Total Posts : 29870
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: 24 bit integers in MPLAB X IDE 2021/01/18 16:40:37 (permalink)
    +1 (1)
    We just have to assume the OP is using XC8.
    It would help if they revealed which version (1.xx, 2.xx), and which mode it is in (C90, C99).
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #3
    didierleplae
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: 24 bit integers in MPLAB X IDE 2021/01/18 16:47:32 (permalink)
    0
    Sorry, I just did another test and it does seem to be working. I must have gotten myself confused with a different issue in my code
    #4
    Mysil
    Super Member
    • Total Posts : 4114
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: 24 bit integers in MPLAB X IDE 2021/01/18 17:00:11 (permalink)
    0
    Hi,
     
    When XC8    version 2.xx was first released, compiler writers were a little too eager to follow the C standard,
    and didn't  fully get all details around extensions and implementation alternatives right.
     
    Use of uint24_t  may still depend upon what compiler options have been selected  in 'Project Properties'.
    If 'Strict' standard conformance or 'Pedantic', or 'CCI' is selected, then use of uint24_t  may not be allowed.
    I do not know if  __uint24  will also be prohibited.
     
        Mysil
    #5
    didierleplae
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: 24 bit integers in MPLAB X IDE 2021/01/18 17:11:09 (permalink)
    +1 (1)
    At least the __uint24 gets rid of the red line.Smile: Smile
     
    For whatever it's worth I'm running:
    MPLAB X IDE v5.15
    XC8 2.05
    C99
    #6
    didierleplae
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: 24 bit integers in MPLAB X IDE 2021/01/18 17:31:18 (permalink)
    0
    Ok, so I think I figured out what was confusing me. 
    I have a PIC16F1769 set up with an audio frequency oscillator connected that resets a timer and counts overflows.
    It also has a single digit 7 segment display.
    if I do this:

    DISPLAY(overflow);

    I the 7 seg display shows numbers 0-9 as I sweep the frequency of the oscillator
     
    As a test I did this:

    period = overflow << 16;
    DISPLAY(period >> 16);

    and all I see is "0"
     
    If I do this:

    period = overflow * 0x10000;
    DISPLAY(period >> 16);

    It's all good. So, I wasn't thinking that since underneath the 24bit int is just hiding some 8bit arrays so I can't shift beyond 8 bits without getting erroneous results.
     
    Is there trick to doing this more efficiently than just multiplication? 
     
    post edited by didierleplae - 2021/01/18 17:36:08
    #7
    1and0
    Access is Denied
    • Total Posts : 12086
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: 24 bit integers in MPLAB X IDE 2021/01/18 17:41:55 (permalink)
    +2 (2)
    didierleplae
    As a test I did this:

    period = overflow << 16;
    DISPLAY(period >> 16);

    and all I see is "0"

    Let me guess, "overflow" is 16-bit or smaller, right?  What do you think you'll get when you shift a 16-bit value 16 bits?
     
    didierleplae
    Is there trick to doing this more efficiently than just multiplication? 

    Cast "overflow" to a 24-bit type.
     
    <edit> If the compiler is smart enough, it will replace the multiplication with shift but that may not be guaranteed. ;)
     
    post edited by 1and0 - 2021/01/18 17:45:13
    #8
    ric
    Super Member
    • Total Posts : 29870
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: 24 bit integers in MPLAB X IDE 2021/01/18 17:45:42 (permalink)
    +2 (2)
    +1
    "overflow * 0x10000" worked because 0x10000 is more than 16 bits, so the whole expression was evaluated using 32 bit math, then truncated to 24 bits to write into "period".
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #9
    didierleplae
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: 24 bit integers in MPLAB X IDE 2021/01/18 17:47:51 (permalink)
    0
    1and0
    Let me guess, "overflow" is 16-bit or smaller, right?  

    Yes, overflow is an 8-bit type

    1and0
    Cast "overflow" to a 24-bit type. <edit> If the compiler is smart enough, it will replace the multiplication with shift but that may not be guaranteed. ;) 
    [/quote




    I am trying to avoid using more 24bit variables than necessary to save on precious memory
    post edited by didierleplae - 2021/01/18 17:50:57
    #10
    ric
    Super Member
    • Total Posts : 29870
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: 24 bit integers in MPLAB X IDE 2021/01/18 17:49:52 (permalink)
    +2 (2)
    didierleplae
    I am trying to avoid using more 24bit variables than necessary to save on memory

    "casting" the value in an expression doesn't make your variable use more memory. It is the CORRECT way to avoid this problem.
     
    If you are unclear, 1and0 is suggesting you change your expression to:
    period = (__uint24) overflow << 16;

     
    The alternative would be to recode it as
    period = overflow;
    period <<= 16;

    But that may be less efficient (take more instructions) than the first version.
     
     
    post edited by ric - 2021/01/18 17:52:42

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #11
    didierleplae
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: 24 bit integers in MPLAB X IDE 2021/01/18 18:05:36 (permalink)
    0
    Ahh, ok. So, this is strange to me, if I multiply by 0xFFFF then the display shows only “0” but if I multiply by decimal number 65535 then it works. Why is that?
    #12
    1and0
    Access is Denied
    • Total Posts : 12086
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: 24 bit integers in MPLAB X IDE 2021/01/18 18:07:05 (permalink)
    +1 (1)
    didierleplae
    I am trying to avoid using more 24bit variables than necessary to save on precious memory

    Your "overflow" variable is 8-bit wide. If it's shifted by 16 or more bits, you will get 0.

    Type casting an 8-bit variable to 24-bit tells the compiler to convert the 8-bit variable into a 24-bit "intermediate" variable, and then shifting this 24-bit variable 16 bits will give the correct result.

    Notice the compiler may or may not actually use a 24-bit intermediate variable; that is, it can do this too:
        low byte of period = 0;
       high byte of period = 0;
      upper byte of period = overflow;

    depending on how smart is the compiler. ;)
     
    post edited by 1and0 - 2021/01/18 21:10:01
    #13
    ric
    Super Member
    • Total Posts : 29870
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: 24 bit integers in MPLAB X IDE 2021/01/18 18:07:50 (permalink)
    0
    didierleplae
    Ahh, ok. So, this is strange to me, if I multiply by 0xFFFF then the display shows only “0” but if I multiply by decimal number 65535 then it works. Why is that?

    I explained why in post#10.
    0xFFFF fits in a 16 bit variable, so the calculation is done using 16 bits.
    0x10000 is too big for a 16 bit variable, so it steps up to a 32 bit calculation.
    These are the normal rules in C. Calculations are done to handle the largest argument involved. They do NOT pay any attention to the size of the variable you are going to store the result in.
    Better explanation by 1and0 in the following post. I misread "65535" as "65536".
     
     
    post edited by ric - 2021/01/18 18:21:22

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #14
    1and0
    Access is Denied
    • Total Posts : 12086
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: 24 bit integers in MPLAB X IDE 2021/01/18 18:17:44 (permalink)
    +1 (1)
    didierleplae
    Ahh, ok. So, this is strange to me, if I multiply by 0xFFFF then the display shows only “0” but if I multiply by decimal number 65535 then it works. Why is that?

    The hex number 0xFFFF is an unsigned int (16-bit), so the compiler uses 16-bit math. <edit> Maybe it's signed int, I forgot, but it's still 16-bit. </edit>
     
    The decimal number 65535 (and 65536) is a signed long int (32-bit), so the compiler uses 32-bit math.
     
    post edited by 1and0 - 2021/01/18 18:29:14
    #15
    didierleplae
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: 24 bit integers in MPLAB X IDE 2021/01/18 19:03:03 (permalink)
    0
    Thank you all, this is helpful!
    But I still am a bit unclear why 65535 and 0xFFFF are different. I thought they were the same number just expressed in a different way that the compiler would translate into binary numbers.
    #16
    ric
    Super Member
    • Total Posts : 29870
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: 24 bit integers in MPLAB X IDE 2021/01/18 19:21:12 (permalink)
    +3 (3)
    didierleplae
    Thank you all, this is helpful!
    But I still am a bit unclear why 65535 and 0xFFFF are different. I thought they were the same number just expressed in a different way that the compiler would translate into binary numbers.

    0xFFFF will be treated as an unsigned integer, but decimal constants default to "signed".
    65535 cannot be stored in a signed 16 bit integer, so it it promoted to a signed 32 bit integer.
    See: "5.3.7.1 Integral Constants" in "MPLAB_XC8_C_Compiler_User_Guide_for_PIC.pdf"
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #17
    1and0
    Access is Denied
    • Total Posts : 12086
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: 24 bit integers in MPLAB X IDE 2021/01/18 19:31:59 (permalink)
    +2 (2)
    didierleplae
    But I still am a bit unclear why 65535 and 0xFFFF are different. I thought they were the same number just expressed in a different way that the compiler would translate into binary numbers.

    It is the definitions of the C language. :)  The type of the integer literal is the first type of the corresponding list in which its value can be fitted and be represented.

    C90 Standard
    • unsuffixed decimal: int, long int, unsigned long int
    • unsuffixed hexadecimal: int, unsigned int, long int, unsigned long int

    C99 Standard
    • unsuffixed decimal: int, long int, long long int
    • unsuffixed hexadecimal: int, unsigned int, long int, unsigned long int, long long int, unsigned long long int


    65535 cannot fit inside an int, so the next type is long int which it can be represented.

    0xFFFF cannot fit inside an int, so the next type is unsigned int.
    #18
    didierleplae
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: 24 bit integers in MPLAB X IDE 2021/01/18 20:35:23 (permalink)
    0
    Ok, thank you!
    #19
    didierleplae
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: 24 bit integers in MPLAB X IDE 2021/01/19 14:19:23 (permalink)
    0
    ric
    "casting" the value in an expression doesn't make your variable use more memory. It is the CORRECT way to avoid this problem.
     
    If you are unclear, 1and0 is suggesting you change your expression to:
    period = (__uint24) overflow << 16;


    Thank you for explaining this!
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2021 APG vNext Commercial Version 4.5