• AVR Freaks

Hot!Does XC8 version 2.10 support any form of the atoi function?

Author
markm23
Starting Member
  • Total Posts : 28
  • Reward points : 0
  • Joined: 2019/11/21 12:51:59
  • Location: 0
  • Status: offline
2020/01/14 15:11:34 (permalink)
0

Does XC8 version 2.10 support any form of the atoi function?

I needed to convert an unsigned integer (maximum possible value 16,383 decimal) to an ASCII string, in a PIC16F15356. In most C compilers, the simplest way is the atoi or uatoi function, although these are not in the C standard. But the XC8 user guide does not list these functions, nor would the compiler recognize it, even with all the libraries I could think of #included. So I used sprintf, but:
 
1) It used 40% of the program memory! And this chip has plenty of flash memory, for a PIC16.
 
2) It also generated a warning that the 16-level call stack might be exceeded - even though I was only about 5 levels deep where I called this. sprintf calls vaprintf, which calls... How deep does that go? 
 
So I'm copying a small uatoi function I found somewhere on the web. It's in C, not tuned to any particular architecture, and probably twice the size and a tenth or hundredth the speed of hand-coded assembly language, but that's not important _this_ time.
 
Does Microchip have any plans to add these functions to XC8?
#1

14 Replies Related Threads

    ric
    Super Member
    • Total Posts : 28336
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Does XC8 version 2.10 support any form of the atoi function? 2020/01/14 15:17:12 (permalink)
    +1 (1)
    Did you see this reply advising you to use the C90 libraries?
    https://www.microchip.com/forums/FindPost/1124790
     

    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!
    #2
    katela
    Super Member
    • Total Posts : 1861
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: Does XC8 version 2.10 support any form of the atoi function? 2020/01/14 15:18:22 (permalink)
    +1 (1)
    Are you using your compiler in C99 or C90 standard? Try to switch to C90 standard.

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #3
    NKurzman
    A Guy on the Net
    • Total Posts : 18898
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Does XC8 version 2.10 support any form of the atoi function? 2020/01/14 16:16:28 (permalink)
    +1 (1)
    So posting the same question again will get a different answer?

    You other option is to code it yourself.
    It is not very complicated.
    #4
    1and0
    Access is Denied
    • Total Posts : 11127
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Does XC8 version 2.10 support any form of the atoi function? 2020/01/14 18:24:43 (permalink)
    +3 (3)
    NKurzman
    So posting the same question again will get a different answer?

    Albert Einstein defined that as "insanity". ;)
     
    #5
    ric
    Super Member
    • Total Posts : 28336
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Does XC8 version 2.10 support any form of the atoi function? 2020/01/14 19:05:40 (permalink)
    +1 (1)
    There seems to be some confusion in the question.
    The thread title asks for an "atoi()" function, and it's also mentioned in the text, but the requirement is clearly for an itoa() function...
     

    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!
    #6
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11982
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Does XC8 version 2.10 support any form of the atoi function? 2020/01/14 20:48:09 (permalink)
    +2 (2)
    If you want to use non-standard functions, get their source code and add it to your project.  That will work with any compiler and it eliminates all the faffing about  with compiler options.  You only have to do this one time in your career.
    #7
    ric
    Super Member
    • Total Posts : 28336
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Does XC8 version 2.10 support any form of the atoi function? 2020/01/14 20:50:02 (permalink)
    0
    markm23
    1) It used 40% of the program memory! And this chip has plenty of flash memory, for a PIC16.



    How much of that was sprintf()?
    This code:
    #include <xc.h>
    #include <stdio.h>

    void putch(char inchar)
    {
        while (!TX1IF);
        TX1REG = inchar;           
    }

    void main(void) {
        volatile unsigned int inval = 12345;
        char outbuf[10];
        
        sprintf(outbuf,"%d",inval);
        puts(outbuf);
     
        while (1);
    }

    Compiled with XC8 v2.10 in C99 mode and optimisation 0 gives:
    Memory Summary:
        Program space        used   5C3h (  1475) of  4000h words   (  9.0%)
        Data space           used    98h (   152) of   800h bytes   (  7.4%)
        EEPROM space         None available
        Data stack space     used     0h (     0) of   767h bytes   (  0.0%)
        Configuration bits   used     5h (     5) of     5h words   (100.0%)
        ID Location space    used     0h (     0) of     4h bytes   (  0.0%)

     
    Setting optimisation 2 (still in free mode), gives a slight improvement
    Memory Summary:
        Program space        used   54Dh (  1357) of  4000h words   (  8.3%)
        Data space           used    98h (   152) of   800h bytes   (  7.4%)
        EEPROM space         None available
        Data stack space     used     0h (     0) of   767h bytes   (  0.0%)
        Configuration bits   used     5h (     5) of     5h words   (100.0%)
        ID Location space    used     0h (     0) of     4h bytes   (  0.0%)

     
    Compiled with XC8 v2.10 in C90 mode and optimisation 0 gives:
    Memory Summary:
        Program space        used   19Fh (   415) of  4000h words   (  2.5%)
        Data space           used    2Bh (    43) of   800h bytes   (  2.1%)
        EEPROM space         None available
        Data stack space     used     0h (     0) of   7D5h bytes   (  0.0%)
        Configuration bits   used     5h (     5) of     5h words   (100.0%)
        ID Location space    used     0h (     0) of     4h bytes   (  0.0%)

     

    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!
    #8
    ric
    Super Member
    • Total Posts : 28336
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Does XC8 version 2.10 support any form of the atoi function? 2020/01/14 20:53:28 (permalink)
    0
    Changing sprintf() to itoa() in C90 mode just drops the Program space from 2.5% to 2.0%
     

    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
    1and0
    Access is Denied
    • Total Posts : 11127
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Does XC8 version 2.10 support any form of the atoi function? 2020/01/14 21:04:57 (permalink)
    +1 (1)
    OUCH!  C99 mode uses over three times more program memory and data memory than C90 mode. :(
     
    #10
    ric
    Super Member
    • Total Posts : 28336
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Does XC8 version 2.10 support any form of the atoi function? 2020/01/14 21:08:34 (permalink)
    +1 (1)
    I suspect it's not nearly as good at analysing the format strings and discarding unused functionality (if it's doing that at all).
    HiTech did a lot of work dynamically reconfiguring their printf() function to only include the required functionality, which may only be available in the C90 libraries.
     

    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
    markm23
    Starting Member
    • Total Posts : 28
    • Reward points : 0
    • Joined: 2019/11/21 12:51:59
    • Location: 0
    • Status: offline
    Re: Does XC8 version 2.10 support any form of the atoi function? 2020/01/15 12:22:06 (permalink)
    0
    Yes, it's itoa() that I need - or better, uitoa(). The compiler also had to remind me not to transpose those letters! 
     
    Changing to C90 is not practical. I had thousands of lines running under C99 before I discovered the need for a conversion in just one place. So I had to find an implementation of the function that I could paste into my code. There are now a couple of different implementations posted here: https://www.microchip.com/forums/m1123940.aspx
     
    Commenting out a single line - 
     
          snprintf(formatted,20,"   v%d.%d",raw[0],raw[1]);
     
    - dropped the program memory from 79% to 40%, about 6,000 words.  This call was a little more complex than Ric's test, but it must be that C99 dragged in the entire library for one call, rather than identifying just the parts needed. 
     
    I have noticed funny things in the XC8 v2.10 - C99 count of the program memory used. I can type all day and barely budge it, but then a 10-line function makes it jump several percent. And it sometimes seemed like I'd added a little code without removing anything and the count _dropped_ a little. 
    #12
    katela
    Super Member
    • Total Posts : 1861
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: Does XC8 version 2.10 support any form of the atoi function? 2020/01/15 12:29:13 (permalink)
    +2 (2)
    markm23
     There are now a couple of different implementations posted here: https://www.microchip.com/forums/m1123940.aspx
     

    You see now the problem of posting the same question in two different places. Now the people who will try to help you have to move between threads to assist you, sometime repeating what has already been said in another thread.

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #13
    DickD
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2013/10/27 19:06:27
    • Location: Whidbey Island, WA
    • Status: offline
    Re: Does XC8 version 2.10 support any form of the atoi function? 2020/01/28 04:50:01 (permalink)
    -1 (1)
    I find an atoi.c in the xc8\v2.10\pic\sources\c99\common directory. But not atoi.h.
     
    I understand that in C99 atoi has been replaced by a number of functions that start with "strto" within <stdlib.h>
    strtol (long)
    strtoull (unsigned long)
    etc.
     
    Dick
    #14
    ric
    Super Member
    • Total Posts : 28336
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Does XC8 version 2.10 support any form of the atoi function? 2020/01/28 06:05:40 (permalink)
    +2 (2)
    Please don't hijack other people's topics.
    You already have your own topic asking exactly the same question.
     

    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!
    #15
    Jump to:
    © 2020 APG vNext Commercial Version 4.5