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