Back to index

glibc  2.9
efgcvt.c
Go to the documentation of this file.
00001 /* Compatibility functions for floating point formatting.
00002    Copyright (C) 1995, 1996, 1997, 1999, 2002, 2006
00003    Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <math.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <sys/param.h>
00025 #include <float.h>
00026 #include <bits/libc-lock.h>
00027 #include <math_ldbl_opt.h>
00028 
00029 #ifndef FLOAT_TYPE
00030 # define FLOAT_TYPE double
00031 # define FUNC_PREFIX
00032 # define FLOAT_FMT_FLAG
00033 /* Actually we have to write (DBL_DIG + log10 (DBL_MAX_10_EXP)) but we
00034    don't have log10 available in the preprocessor.  */
00035 # define MAXDIG (NDIGIT_MAX + 3)
00036 # define FCVT_MAXDIG (DBL_MAX_10_EXP + MAXDIG)
00037 # if DBL_MANT_DIG == 53
00038 #  define NDIGIT_MAX 17
00039 # elif DBL_MANT_DIG == 24
00040 #  define NDIGIT_MAX 9
00041 # elif DBL_MANT_DIG == 56
00042 #  define NDIGIT_MAX 18
00043 # else
00044 /* See IEEE 854 5.6, table 2 for this formula.  Unfortunately we need a
00045    compile time constant here, so we cannot use it.  */
00046 #  error "NDIGIT_MAX must be precomputed"
00047 #  define NDIGIT_MAX (lrint (ceil (M_LN2 / M_LN10 * DBL_MANT_DIG + 1.0)))
00048 # endif
00049 #else
00050 # define LONG_DOUBLE_CVT
00051 #endif
00052 
00053 #define APPEND(a, b) APPEND2 (a, b)
00054 #define APPEND2(a, b) a##b
00055 #define __APPEND(a, b) __APPEND2 (a, b)
00056 #define __APPEND2(a, b) __##a##b
00057 
00058 
00059 #define FCVT_BUFFER APPEND (FUNC_PREFIX, fcvt_buffer)
00060 #define FCVT_BUFPTR APPEND (FUNC_PREFIX, fcvt_bufptr)
00061 #define ECVT_BUFFER APPEND (FUNC_PREFIX, ecvt_buffer)
00062 
00063 
00064 static char FCVT_BUFFER[MAXDIG];
00065 static char ECVT_BUFFER[MAXDIG];
00066 libc_freeres_ptr (static char *FCVT_BUFPTR);
00067 
00068 char *
00069 __APPEND (FUNC_PREFIX, fcvt) (value, ndigit, decpt, sign)
00070      FLOAT_TYPE value;
00071      int ndigit, *decpt, *sign;
00072 {
00073   if (FCVT_BUFPTR == NULL)
00074     {
00075       if (__APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign,
00076                                      FCVT_BUFFER, MAXDIG) != -1)
00077        return FCVT_BUFFER;
00078 
00079       FCVT_BUFPTR = (char *) malloc (FCVT_MAXDIG);
00080       if (FCVT_BUFPTR == NULL)
00081        return FCVT_BUFFER;
00082     }
00083 
00084   (void) __APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign,
00085                                     FCVT_BUFPTR, FCVT_MAXDIG);
00086 
00087   return FCVT_BUFPTR;
00088 }
00089 
00090 
00091 char *
00092 __APPEND (FUNC_PREFIX, ecvt) (value, ndigit, decpt, sign)
00093      FLOAT_TYPE value;
00094      int ndigit, *decpt, *sign;
00095 {
00096   (void) __APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign,
00097                                     ECVT_BUFFER, MAXDIG);
00098 
00099   return ECVT_BUFFER;
00100 }
00101 
00102 char *
00103 __APPEND (FUNC_PREFIX, gcvt) (value, ndigit, buf)
00104      FLOAT_TYPE value;
00105      int ndigit;
00106      char *buf;
00107 {
00108   sprintf (buf, "%.*" FLOAT_FMT_FLAG "g", MIN (ndigit, NDIGIT_MAX), value);
00109   return buf;
00110 }
00111 
00112 #if LONG_DOUBLE_COMPAT (libc, GLIBC_2_0)
00113 # ifdef LONG_DOUBLE_CVT
00114 #  define cvt_symbol(symbol) \
00115   cvt_symbol_1 (libc, __APPEND (FUNC_PREFIX, symbol), \
00116              APPEND (FUNC_PREFIX, symbol), GLIBC_2_4)
00117 #  define cvt_symbol_1(lib, local, symbol, version) \
00118     versioned_symbol (lib, local, symbol, version)
00119 # else
00120 #  define cvt_symbol(symbol) \
00121   cvt_symbol_1 (libc, __APPEND (FUNC_PREFIX, symbol), \
00122              APPEND (q, symbol), GLIBC_2_0); \
00123   strong_alias (__APPEND (FUNC_PREFIX, symbol), APPEND (FUNC_PREFIX, symbol))
00124 #  define cvt_symbol_1(lib, local, symbol, version) \
00125   compat_symbol (lib, local, symbol, version)
00126 # endif
00127 #else
00128 # define cvt_symbol(symbol) \
00129   strong_alias (__APPEND (FUNC_PREFIX, symbol), APPEND (FUNC_PREFIX, symbol))
00130 #endif
00131 cvt_symbol(fcvt);
00132 cvt_symbol(ecvt);
00133 cvt_symbol(gcvt);