Remove spurious '.' in math/libm-test.inc comment.
[glibc.git] / misc / efgcvt.c
blobd7d15a1846ce1be44bb96276ae609c26b27f6f08
1 /* Compatibility functions for floating point formatting.
2 Copyright (C) 1995, 1996, 1997, 1999, 2002, 2006
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <math.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/param.h>
24 #include <float.h>
25 #include <bits/libc-lock.h>
26 #include <math_ldbl_opt.h>
28 #ifndef FLOAT_TYPE
29 # define FLOAT_TYPE double
30 # define FUNC_PREFIX
31 # define FLOAT_FMT_FLAG
32 /* Actually we have to write (DBL_DIG + log10 (DBL_MAX_10_EXP)) but we
33 don't have log10 available in the preprocessor. */
34 # define MAXDIG (NDIGIT_MAX + 3)
35 # define FCVT_MAXDIG (DBL_MAX_10_EXP + MAXDIG)
36 # if DBL_MANT_DIG == 53
37 # define NDIGIT_MAX 17
38 # elif DBL_MANT_DIG == 24
39 # define NDIGIT_MAX 9
40 # elif DBL_MANT_DIG == 56
41 # define NDIGIT_MAX 18
42 # else
43 /* See IEEE 854 5.6, table 2 for this formula. Unfortunately we need a
44 compile time constant here, so we cannot use it. */
45 # error "NDIGIT_MAX must be precomputed"
46 # define NDIGIT_MAX (lrint (ceil (M_LN2 / M_LN10 * DBL_MANT_DIG + 1.0)))
47 # endif
48 #else
49 # define LONG_DOUBLE_CVT
50 #endif
52 #define APPEND(a, b) APPEND2 (a, b)
53 #define APPEND2(a, b) a##b
54 #define __APPEND(a, b) __APPEND2 (a, b)
55 #define __APPEND2(a, b) __##a##b
58 #define FCVT_BUFFER APPEND (FUNC_PREFIX, fcvt_buffer)
59 #define FCVT_BUFPTR APPEND (FUNC_PREFIX, fcvt_bufptr)
60 #define ECVT_BUFFER APPEND (FUNC_PREFIX, ecvt_buffer)
63 static char FCVT_BUFFER[MAXDIG];
64 static char ECVT_BUFFER[MAXDIG];
65 libc_freeres_ptr (static char *FCVT_BUFPTR);
67 char *
68 __APPEND (FUNC_PREFIX, fcvt) (value, ndigit, decpt, sign)
69 FLOAT_TYPE value;
70 int ndigit, *decpt, *sign;
72 if (FCVT_BUFPTR == NULL)
74 if (__APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign,
75 FCVT_BUFFER, MAXDIG) != -1)
76 return FCVT_BUFFER;
78 FCVT_BUFPTR = (char *) malloc (FCVT_MAXDIG);
79 if (FCVT_BUFPTR == NULL)
80 return FCVT_BUFFER;
83 (void) __APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign,
84 FCVT_BUFPTR, FCVT_MAXDIG);
86 return FCVT_BUFPTR;
90 char *
91 __APPEND (FUNC_PREFIX, ecvt) (value, ndigit, decpt, sign)
92 FLOAT_TYPE value;
93 int ndigit, *decpt, *sign;
95 (void) __APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign,
96 ECVT_BUFFER, MAXDIG);
98 return ECVT_BUFFER;
101 char *
102 __APPEND (FUNC_PREFIX, gcvt) (value, ndigit, buf)
103 FLOAT_TYPE value;
104 int ndigit;
105 char *buf;
107 sprintf (buf, "%.*" FLOAT_FMT_FLAG "g", MIN (ndigit, NDIGIT_MAX), value);
108 return buf;
111 #if LONG_DOUBLE_COMPAT (libc, GLIBC_2_0)
112 # ifdef LONG_DOUBLE_CVT
113 # define cvt_symbol(symbol) \
114 cvt_symbol_1 (libc, __APPEND (FUNC_PREFIX, symbol), \
115 APPEND (FUNC_PREFIX, symbol), GLIBC_2_4)
116 # define cvt_symbol_1(lib, local, symbol, version) \
117 versioned_symbol (lib, local, symbol, version)
118 # else
119 # define cvt_symbol(symbol) \
120 cvt_symbol_1 (libc, __APPEND (FUNC_PREFIX, symbol), \
121 APPEND (q, symbol), GLIBC_2_0); \
122 strong_alias (__APPEND (FUNC_PREFIX, symbol), APPEND (FUNC_PREFIX, symbol))
123 # define cvt_symbol_1(lib, local, symbol, version) \
124 compat_symbol (lib, local, symbol, version)
125 # endif
126 #else
127 # define cvt_symbol(symbol) \
128 strong_alias (__APPEND (FUNC_PREFIX, symbol), APPEND (FUNC_PREFIX, symbol))
129 #endif
130 cvt_symbol(fcvt);
131 cvt_symbol(ecvt);
132 cvt_symbol(gcvt);