xref: /netbsd-src/external/gpl3/gcc.old/dist/libquadmath/printf/_itoa.h (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1*627f7eb2Smrg /* Internal function for converting integers to ASCII.
2*627f7eb2Smrg    Copyright (C) 1994-1999,2002,2003,2007 Free Software Foundation, Inc.
3*627f7eb2Smrg    This file is part of the GNU C Library.
4*627f7eb2Smrg 
5*627f7eb2Smrg    The GNU C Library is free software; you can redistribute it and/or
6*627f7eb2Smrg    modify it under the terms of the GNU Lesser General Public
7*627f7eb2Smrg    License as published by the Free Software Foundation; either
8*627f7eb2Smrg    version 2.1 of the License, or (at your option) any later version.
9*627f7eb2Smrg 
10*627f7eb2Smrg    The GNU C Library is distributed in the hope that it will be useful,
11*627f7eb2Smrg    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*627f7eb2Smrg    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13*627f7eb2Smrg    Lesser General Public License for more details.
14*627f7eb2Smrg 
15*627f7eb2Smrg    You should have received a copy of the GNU Lesser General Public
16*627f7eb2Smrg    License along with the GNU C Library; if not, write to the Free
17*627f7eb2Smrg    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18*627f7eb2Smrg    02111-1307 USA.  */
19*627f7eb2Smrg 
20*627f7eb2Smrg #ifndef _ITOA_H
21*627f7eb2Smrg #define _ITOA_H
22*627f7eb2Smrg 
23*627f7eb2Smrg /* Convert VALUE into ASCII in base BASE (2..16).
24*627f7eb2Smrg    Write backwards starting the character just before BUFLIM.
25*627f7eb2Smrg    Return the address of the first (left-to-right) character in the number.
26*627f7eb2Smrg    Use upper case letters iff UPPER_CASE is nonzero.  */
27*627f7eb2Smrg 
28*627f7eb2Smrg static const char _itoa_lower_digits[16] = "0123456789abcdef";
29*627f7eb2Smrg static const char _itoa_upper_digits[16] = "0123456789ABCDEF";
30*627f7eb2Smrg 
31*627f7eb2Smrg static inline char * __attribute__ ((unused, always_inline))
_itoa_word(unsigned long value,char * buflim,unsigned int base,int upper_case)32*627f7eb2Smrg _itoa_word (unsigned long value, char *buflim,
33*627f7eb2Smrg 	    unsigned int base, int upper_case)
34*627f7eb2Smrg {
35*627f7eb2Smrg   const char *digits = (upper_case ? _itoa_upper_digits : _itoa_lower_digits);
36*627f7eb2Smrg 
37*627f7eb2Smrg   switch (base)
38*627f7eb2Smrg     {
39*627f7eb2Smrg # define SPECIAL(Base)							      \
40*627f7eb2Smrg     case Base:								      \
41*627f7eb2Smrg       do								      \
42*627f7eb2Smrg 	*--buflim = digits[value % Base];				      \
43*627f7eb2Smrg       while ((value /= Base) != 0);					      \
44*627f7eb2Smrg       break
45*627f7eb2Smrg 
46*627f7eb2Smrg       SPECIAL (10);
47*627f7eb2Smrg       SPECIAL (16);
48*627f7eb2Smrg       SPECIAL (8);
49*627f7eb2Smrg     default:
50*627f7eb2Smrg       do
51*627f7eb2Smrg 	*--buflim = digits[value % base];
52*627f7eb2Smrg       while ((value /= base) != 0);
53*627f7eb2Smrg     }
54*627f7eb2Smrg   return buflim;
55*627f7eb2Smrg }
56*627f7eb2Smrg 
57*627f7eb2Smrg static inline char * __attribute__ ((unused, always_inline))
_itoa(uint64_t value,char * buflim,unsigned int base,int upper_case)58*627f7eb2Smrg _itoa (uint64_t value, char *buflim,
59*627f7eb2Smrg        unsigned int base, int upper_case)
60*627f7eb2Smrg {
61*627f7eb2Smrg   const char *digits = (upper_case ? _itoa_upper_digits : _itoa_lower_digits);
62*627f7eb2Smrg 
63*627f7eb2Smrg   switch (base)
64*627f7eb2Smrg     {
65*627f7eb2Smrg       SPECIAL (10);
66*627f7eb2Smrg       SPECIAL (16);
67*627f7eb2Smrg       SPECIAL (8);
68*627f7eb2Smrg     default:
69*627f7eb2Smrg       do
70*627f7eb2Smrg 	*--buflim = digits[value % base];
71*627f7eb2Smrg       while ((value /= base) != 0);
72*627f7eb2Smrg     }
73*627f7eb2Smrg   return buflim;
74*627f7eb2Smrg }
75*627f7eb2Smrg # undef SPECIAL
76*627f7eb2Smrg 
77*627f7eb2Smrg #endif	/* itoa.h */
78