The <fastitoa.h> Header File

A family of functions designed as fast replacements for sprintf.

These functions write an unsigned short or unsigned long, in base 10 or 16 (uppercase or lowercase letters for base 16) into a string.
Functions with name terminated by _extended also feature a minimal number of digits and the choice between padding zeros/padding spaces.
Compared to sprintf, the speed gain is huge (see below). Of course, that's partly because there is no need to search for the way the numbers have to be printed, and because parameters are passed by registers. But, sprintf is not too optimized, especially for base 16: it repeatedly divides the number by 16 = 2^4...

Note: Only functions taking unsigned numbers are provided. Displaying signed numbers is not difficult, and you can do it easily with these functions...

All functions in this header use parameter passing by register, this increases speed and decreases program size. This feature has been in TIGCC for a long time now.


The speed benches are based on the following code:

// Yes, this stops the clock on AMS 2.07+...
  OSSetSR(0x0400);
  OSFreeTimer(USER_TIMER);
  OSRegisterTimer(USER_TIMER,200000);

// for (nl=0xFFFFUL;(nl--);)
// or:
//  for(nl=0xFFFFFFFFUL;nl>=0xFFFF0000UL;nl--)
// for unsigned long numbers (nl is unsigned long);
//
// for (ns=65535U;(ns--);)
// for unsigned short numbers (ns is unsigned short).
  {
//    [call function];
  }

  printf_xy(0,10,"%lu",OSTimerCurVal(USER_TIMER));

// Do this with both sprintf and the corresponding function from this header.

All functions printing a number (unsigned short or unsigned long) in base 16, are more than ten times faster than sprintf.
Functions printing a number in base 10, are up to five or six times faster than sprintf.

Functions

itoa_ulong_10
Converts an unsigned long number to a string, using base 10.
itoa_ulong_10_extended
Converts an unsigned long number to a string, using base 10, with support for a minimal number of digits (minchars), and the choice between spaces and zeros for the padding bytes (padtype).
itoa_ulong_16_lower
Converts an unsigned long number to a string, using base 16 (lowercase letters).
itoa_ulong_16_lower_extended
Converts an unsigned long number to a string, using base 16 (lowercase letters), with support for a minimal number of digits (minchars), and the choice between spaces and zeros for the padding bytes (padtype).
itoa_ulong_16_upper
Converts an unsigned long number to a string, using base 16 (uppercase letters).
itoa_ulong_16_upper_extended
Converts an unsigned long number to a string, using base 16 (uppercase letters), with support for a minimal number of digits (minchars), and the choice between spaces and zeros for the padding bytes (padtype).
itoa_ushort_10
Converts an unsigned short number to a string, using base 10.
itoa_ushort_10_extended
Converts an unsigned short number to a string, using base 10, with support for a minimal number of digits (minchars), and the choice between spaces and zeros for the padding bytes (padtype).
itoa_ushort_16_lower
Converts an unsigned short number to a string, using base 16 (lowercase letters).
itoa_ushort_16_lower_extended
Converts an unsigned short number to a string, using base 16 (uppercase letters), with support for a minimal number of digits (minchars), and the choice between spaces and zeros for the padding bytes (padtype).
itoa_ushort_16_upper
Converts an unsigned short number to a string, using base 16 (uppercase letters).
itoa_ushort_16_upper_extended
Converts an unsigned short number to a string, using base 16 (uppercase letters), with support for a minimal number of digits, and the choice between spaces and zeros for the padding bytes.

See also: stdio.h, string.h


itoa_ulong_10

short itoa_ulong_10(unsigned char *str, unsigned long number);

Converts an unsigned long number to a string, using base 10.

itoa_ulong_10(buffer,number);

is equivalent to:

sprintf(buffer,"%lu",number);

but about 3 times faster.
itoa_ulong_10 returns the number of bytes output.

See also: fastitoa.h


itoa_ulong_10_extended

short itoa_ulong_10_extended(register unsigned char *str asm("a0"), register unsigned long number asm("d0"), unsigned short minchars, Bool padtype) __attribute__((__stkparm__));

Converts an unsigned long number to a string, using base 10, with support for a minimal number of digits (minchars), and the choice between spaces and zeros for the padding bytes (padtype).

itoa_ulong_10_extended(buffer,number,7,TRUE);

is equivalent to:

sprintf(buffer,"%07lu",number);

itoa_ulong_10_extended(buffer,number,7,FALSE);

is equivalent to:

sprintf(buffer,"%7lu",number);

itoa_ulong_10_extended returns the number of bytes output.

See also: fastitoa.h


itoa_ulong_16_lower

short itoa_ulong_16_lower(register unsigned char *str asm("a0"), register unsigned long number asm("d0"));

Converts an unsigned long number to a string, using base 16 (lowercase letters).

itoa_ulong_16_lower(buffer,number);

is equivalent to:

sprintf(buffer,"%lx",number);

but as high as 13 times faster.
itoa_ulong_16_lower returns the number of bytes output.

See also: fastitoa.h


itoa_ulong_16_lower_extended

short itoa_ulong_16_lower_extended(register unsigned char *str asm("a0"), register unsigned long number asm("d0"), unsigned short minchars, Bool padtype) __attribute__((__stkparm__));

Converts an unsigned long number to a string, using base 16 (lowercase letters), with support for a minimal number of digits (minchars), and the choice between spaces and zeros for the padding bytes (padtype).

itoa_ulong_16_lower_extended(buffer,number,7,TRUE);

is equivalent to:

sprintf(buffer,"%07lx",number);

itoa_ulong_16_lower_extended(buffer,number,7,FALSE);

is equivalent to:

sprintf(buffer,"%7lx",number);

itoa_ulong_16_lower_extended returns the number of bytes output.

See also: fastitoa.h


itoa_ulong_16_upper

short itoa_ulong_16_upper(register unsigned char *str asm("a0"), register unsigned long number asm("d0"));

Converts an unsigned long number to a string, using base 16 (uppercase letters).

itoa_ulong_16_upper(buffer,number);

is equivalent to:

sprintf(buffer,"%lX",number);

but as high as 13 times faster.
itoa_ulong_16_upper returns the number of bytes output.

See also: fastitoa.h


itoa_ulong_16_upper_extended

short itoa_ulong_16_upper_extended(register unsigned char *str asm("a0"), register unsigned long number asm("d0"), unsigned short minchars, Bool padtype) __attribute__((__stkparm__));

Converts an unsigned long number to a string, using base 16 (uppercase letters), with support for a minimal number of digits (minchars), and the choice between spaces and zeros for the padding bytes (padtype).

itoa_ulong_16_upper_extended(buffer,number,7,TRUE);

is equivalent to:

sprintf(buffer,"%07lX",number);

itoa_ulong_16_upper_extended(buffer,number,7,FALSE);

is equivalent to:

sprintf(buffer,"%7lX",number);

itoa_ulong_16_upper_extended returns the number of bytes output.

See also: fastitoa.h


itoa_ushort_10

short itoa_ushort_10(register unsigned char *str asm("%a0"), register unsigned short number asm("%d0"));

Converts an unsigned short number to a string, using base 10.

itoa_ushort_10(buffer,number);

is equivalent to:

sprintf(buffer,"%hu",number);

but up to 6 times faster.
itoa_ushort_10 returns the number of bytes output.

If you want to print very small numbers that fit in a the range of an unsigned char, see CharNumber. It may be faster than itoa_ushort_10 for values between 10 and several dozens, due to the fact itoa_ushort_10 uses divisions and a reverse copy loop (divisions return the digits in the reverse order).

See also: fastitoa.h, CharNumber


itoa_ushort_10_extended

short itoa_ushort_10_extended(register unsigned char *str asm("a0"), register unsigned short number asm("d0"), unsigned short minchars, Bool padtype) __attribute__((__stkparm__));

Converts an unsigned short number to a string, using base 10, with support for a minimal number of digits (minchars), and the choice between spaces and zeros for the padding bytes (padtype).

itoa_ushort_10_extended(buffer,number,7,TRUE);

is equivalent to:

sprintf(buffer,"%07hu",number);

itoa_ushort_10_extended(buffer,number,7,FALSE);

is equivalent to:

sprintf(buffer,"%7hu",number);

itoa_ushort_10_extended returns the number of bytes output.

See also: fastitoa.h


itoa_ushort_16_lower

short itoa_ushort_16_lower(register unsigned char *str asm("a0"), register unsigned short number asm("d0"));

Converts an unsigned short number to a string, using base 16 (lowercase letters).

itoa_ushort_16_lower(buffer,number);

is equivalent to:

sprintf(buffer,"%hx",number);

but about 10 times faster.
itoa_ushort_16_lower returns the number of bytes output.

See also: fastitoa.h


itoa_ushort_16_lower_extended

short itoa_ushort_16_lower_extended(register unsigned char *str asm("a0"), register unsigned short number asm("d0"), unsigned short minchars, Bool padtype) __attribute__((__stkparm__));

Converts an unsigned short number to a string, using base 16 (uppercase letters), with support for a minimal number of digits (minchars), and the choice between spaces and zeros for the padding bytes (padtype).

itoa_ushort_16_lower_extended(buffer,number,7,TRUE);

is equivalent to:

sprintf(buffer,"%07hx",number);

itoa_ushort_16_lower_extended(buffer,number,7,FALSE);

is equivalent to:

sprintf(buffer,"%7hx",number);

itoa_ushort_16_lower_extended returns the number of bytes output.

See also: fastitoa.h


itoa_ushort_16_upper

short itoa_ushort_16_upper(register unsigned char *str asm("a0"), register unsigned short number asm("d0"));

Converts an unsigned short number to a string, using base 16 (uppercase letters).

itoa_ushort_16_upper(buffer,number);

is equivalent to:

sprintf(buffer,"%hX",number);

but about 10 times faster.
itoa_ushort_16_upper returns the number of bytes output.

See also: fastitoa.h


itoa_ushort_16_upper_extended

short itoa_ushort_16_upper_extended(register unsigned char *str asm("a0"), register unsigned short number asm("d0"), unsigned short, BOOL) __attribute__((__stkparm__));

Converts an unsigned short number to a string, using base 16 (uppercase letters), with support for a minimal number of digits, and the choice between spaces and zeros for the padding bytes.

itoa_ushort_16_upper_extended(buffer,number,7,TRUE);

is equivalent to:

sprintf(buffer,"%07hX",number);

itoa_ushort_16_upper_extended(buffer,number,7,FALSE);

is equivalent to:

sprintf(buffer,"%7hX",number);

itoa_ushort_16_upper_extended returns the number of bytes output.

See also: fastitoa.h


Return to the main index