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