xref: /netbsd-src/external/gpl3/gcc.old/dist/libquadmath/printf/quadmath-printf.h (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1*627f7eb2Smrg /* GCC Quad-Precision Math Library
2*627f7eb2Smrg    Copyright (C) 2011 Free Software Foundation, Inc.
3*627f7eb2Smrg    Written by Jakub Jelinek  <jakub@redhat.com>
4*627f7eb2Smrg 
5*627f7eb2Smrg This file is part of the libquadmath library.
6*627f7eb2Smrg Libquadmath is free software; you can redistribute it and/or
7*627f7eb2Smrg modify it under the terms of the GNU Library General Public
8*627f7eb2Smrg License as published by the Free Software Foundation; either
9*627f7eb2Smrg version 2 of the License, or (at your option) any later version.
10*627f7eb2Smrg 
11*627f7eb2Smrg Libquadmath is distributed in the hope that it will be useful,
12*627f7eb2Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
13*627f7eb2Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14*627f7eb2Smrg Library General Public License for more details.
15*627f7eb2Smrg 
16*627f7eb2Smrg You should have received a copy of the GNU Library General Public
17*627f7eb2Smrg License along with libquadmath; see the file COPYING.LIB.  If
18*627f7eb2Smrg not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19*627f7eb2Smrg Boston, MA 02110-1301, USA.  */
20*627f7eb2Smrg 
21*627f7eb2Smrg #include <stdlib.h>
22*627f7eb2Smrg #include <stdio.h>
23*627f7eb2Smrg #ifdef HAVE_LIMITS_H
24*627f7eb2Smrg #include <limits.h>
25*627f7eb2Smrg #endif
26*627f7eb2Smrg #ifdef HAVE_LANGINFO_H
27*627f7eb2Smrg #include <langinfo.h>
28*627f7eb2Smrg #endif
29*627f7eb2Smrg #ifdef HAVE_CTYPE_H
30*627f7eb2Smrg #include <ctype.h>
31*627f7eb2Smrg #endif
32*627f7eb2Smrg #ifdef HAVE_WCHAR_H
33*627f7eb2Smrg #include <wchar.h>
34*627f7eb2Smrg #endif
35*627f7eb2Smrg #ifdef HAVE_WCTYPE_H
36*627f7eb2Smrg #include <wctype.h>
37*627f7eb2Smrg #endif
38*627f7eb2Smrg #ifdef HAVE_PRINTF_HOOKS
39*627f7eb2Smrg #include <printf.h>
40*627f7eb2Smrg #endif
41*627f7eb2Smrg #ifdef HAVE_LOCALE_H
42*627f7eb2Smrg #include <locale.h>
43*627f7eb2Smrg #endif
44*627f7eb2Smrg #include "quadmath-imp.h"
45*627f7eb2Smrg #include "gmp-impl.h"
46*627f7eb2Smrg 
47*627f7eb2Smrg #ifdef HAVE_WCHAR_H
48*627f7eb2Smrg #define L_(x) L##x
49*627f7eb2Smrg #else
50*627f7eb2Smrg #define L_(x) x
51*627f7eb2Smrg #undef wchar_t
52*627f7eb2Smrg #undef wint_t
53*627f7eb2Smrg #undef putwc
54*627f7eb2Smrg #undef WEOF
55*627f7eb2Smrg #define wchar_t char
56*627f7eb2Smrg #define wint_t int
57*627f7eb2Smrg #define putwc(c,f) putc(c,f)
58*627f7eb2Smrg #define WEOF EOF
59*627f7eb2Smrg #endif
60*627f7eb2Smrg 
61*627f7eb2Smrg #ifndef HAVE_CTYPE_H
62*627f7eb2Smrg /* Won't work for EBCDIC.  */
63*627f7eb2Smrg #undef isupper
64*627f7eb2Smrg #undef isdigit
65*627f7eb2Smrg #undef isxdigit
66*627f7eb2Smrg #undef tolower
67*627f7eb2Smrg #define isupper(x) \
68*627f7eb2Smrg   ({__typeof(x) __is_x = (x); __is_x >= 'A' && __is_x <= 'Z'; })
69*627f7eb2Smrg #define isdigit(x) \
70*627f7eb2Smrg   ({__typeof(x) __is_x = (x); __is_x >= '0' && __is_x <= '9'; })
71*627f7eb2Smrg #define isxdigit(x) \
72*627f7eb2Smrg   ({__typeof(x) __is_x = (x); \
73*627f7eb2Smrg     (__is_x >= '0' && __is_x <= '9') \
74*627f7eb2Smrg     || ((x) >= 'A' && (x) <= 'F') \
75*627f7eb2Smrg     || ((x) >= 'a' && (x) <= 'f'); })
76*627f7eb2Smrg #define tolower(x) \
77*627f7eb2Smrg   ({__typeof(x) __is_x = (x); \
78*627f7eb2Smrg     (__is_x >= 'A' && __is_x <= 'Z') ? __is_x - 'A' + 'a' : __is_x; })
79*627f7eb2Smrg #endif
80*627f7eb2Smrg 
81*627f7eb2Smrg #ifndef CHAR_MAX
82*627f7eb2Smrg #ifdef __CHAR_UNSIGNED__
83*627f7eb2Smrg #define CHAR_MAX (2 * __SCHAR_MAX__ + 1)
84*627f7eb2Smrg #else
85*627f7eb2Smrg #define CHAR_MAX __SCHAR_MAX__
86*627f7eb2Smrg #endif
87*627f7eb2Smrg #endif
88*627f7eb2Smrg 
89*627f7eb2Smrg #ifndef HAVE_PRINTF_HOOKS
90*627f7eb2Smrg #define printf_info __quadmath_printf_info
91*627f7eb2Smrg struct printf_info
92*627f7eb2Smrg {
93*627f7eb2Smrg   int prec;			/* Precision.  */
94*627f7eb2Smrg   int width;			/* Width.  */
95*627f7eb2Smrg   wchar_t spec;			/* Format letter.  */
96*627f7eb2Smrg   unsigned int is_long_double:1;/* L flag.  */
97*627f7eb2Smrg   unsigned int is_short:1;	/* h flag.  */
98*627f7eb2Smrg   unsigned int is_long:1;	/* l flag.  */
99*627f7eb2Smrg   unsigned int alt:1;		/* # flag.  */
100*627f7eb2Smrg   unsigned int space:1;		/* Space flag.  */
101*627f7eb2Smrg   unsigned int left:1;		/* - flag.  */
102*627f7eb2Smrg   unsigned int showsign:1;	/* + flag.  */
103*627f7eb2Smrg   unsigned int group:1;		/* ' flag.  */
104*627f7eb2Smrg   unsigned int extra:1;		/* For special use.  */
105*627f7eb2Smrg   unsigned int is_char:1;	/* hh flag.  */
106*627f7eb2Smrg   unsigned int wide:1;		/* Nonzero for wide character streams.  */
107*627f7eb2Smrg   unsigned int i18n:1;		/* I flag.  */
108*627f7eb2Smrg   unsigned short int user;	/* Bits for user-installed modifiers.  */
109*627f7eb2Smrg   wchar_t pad;			/* Padding character.  */
110*627f7eb2Smrg };
111*627f7eb2Smrg #endif
112*627f7eb2Smrg 
113*627f7eb2Smrg struct __quadmath_printf_file
114*627f7eb2Smrg {
115*627f7eb2Smrg   FILE *fp;
116*627f7eb2Smrg   char *str;
117*627f7eb2Smrg   size_t size;
118*627f7eb2Smrg   size_t len;
119*627f7eb2Smrg   int file_p;
120*627f7eb2Smrg };
121*627f7eb2Smrg 
122*627f7eb2Smrg int
123*627f7eb2Smrg __quadmath_printf_fp (struct __quadmath_printf_file *fp,
124*627f7eb2Smrg 		      const struct printf_info *info,
125*627f7eb2Smrg 		      const void *const *args) attribute_hidden;
126*627f7eb2Smrg int
127*627f7eb2Smrg __quadmath_printf_fphex (struct __quadmath_printf_file *fp,
128*627f7eb2Smrg 			 const struct printf_info *info,
129*627f7eb2Smrg 			 const void *const *args) attribute_hidden;
130*627f7eb2Smrg 
131*627f7eb2Smrg size_t __quadmath_do_pad (struct __quadmath_printf_file *fp, int wide,
132*627f7eb2Smrg 			  int c, size_t n) attribute_hidden;
133*627f7eb2Smrg 
134*627f7eb2Smrg static inline __attribute__((__unused__)) size_t
__quadmath_do_put(struct __quadmath_printf_file * fp,int wide,const char * s,size_t n)135*627f7eb2Smrg __quadmath_do_put (struct __quadmath_printf_file *fp, int wide,
136*627f7eb2Smrg 		   const char *s, size_t n)
137*627f7eb2Smrg {
138*627f7eb2Smrg   size_t len;
139*627f7eb2Smrg   if (fp->file_p)
140*627f7eb2Smrg     {
141*627f7eb2Smrg       if (wide)
142*627f7eb2Smrg 	{
143*627f7eb2Smrg 	  size_t cnt;
144*627f7eb2Smrg 	  const wchar_t *ls = (const wchar_t *) s;
145*627f7eb2Smrg 	  for (cnt = 0; cnt < n; cnt++)
146*627f7eb2Smrg 	    if (putwc (ls[cnt], fp->fp) == WEOF)
147*627f7eb2Smrg 	      break;
148*627f7eb2Smrg 	  return cnt;
149*627f7eb2Smrg 	}
150*627f7eb2Smrg       return fwrite (s, 1, n, fp->fp);
151*627f7eb2Smrg     }
152*627f7eb2Smrg   len = MIN (fp->size, n);
153*627f7eb2Smrg   memcpy (fp->str, s, len);
154*627f7eb2Smrg   fp->str += len;
155*627f7eb2Smrg   fp->size -= len;
156*627f7eb2Smrg   fp->len += n;
157*627f7eb2Smrg   return n;
158*627f7eb2Smrg }
159*627f7eb2Smrg 
160*627f7eb2Smrg static inline __attribute__((__unused__)) int
__quadmath_do_putc(struct __quadmath_printf_file * fp,int wide,wchar_t c)161*627f7eb2Smrg __quadmath_do_putc (struct __quadmath_printf_file *fp, int wide,
162*627f7eb2Smrg 		    wchar_t c)
163*627f7eb2Smrg {
164*627f7eb2Smrg   if (fp->file_p)
165*627f7eb2Smrg     return wide ? (int) putwc (c, fp->fp) : putc (c, fp->fp);
166*627f7eb2Smrg   if (fp->size)
167*627f7eb2Smrg     {
168*627f7eb2Smrg       *(fp->str++) = c;
169*627f7eb2Smrg       fp->size--;
170*627f7eb2Smrg     }
171*627f7eb2Smrg   fp->len++;
172*627f7eb2Smrg   return (unsigned char) c;
173*627f7eb2Smrg }
174*627f7eb2Smrg 
175*627f7eb2Smrg #define PUT(f, s, n) __quadmath_do_put (f, wide, s, n)
176*627f7eb2Smrg #define PAD(f, c, n) __quadmath_do_pad (f, wide, c, n)
177*627f7eb2Smrg #define PUTC(c, f) __quadmath_do_putc (f, wide, c)
178*627f7eb2Smrg 
179*627f7eb2Smrg #define nl_langinfo_wc(x) \
180*627f7eb2Smrg   ({ union { const char *mb; wchar_t wc; } u; u.mb = nl_langinfo (x); u.wc; })
181*627f7eb2Smrg 
182*627f7eb2Smrg #undef _itoa
183*627f7eb2Smrg #define _itoa __quadmath_itoa
184*627f7eb2Smrg 
185*627f7eb2Smrg #undef NAN
186*627f7eb2Smrg #define NAN __builtin_nanf ("")
187