Blame misc/efgcvt.c

Packit 6c4009
/* Compatibility functions for floating point formatting.
Packit 6c4009
   Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <sys/param.h>
Packit 6c4009
#include <float.h>
Packit 6c4009
#include <libc-lock.h>
Packit 6c4009
#include <math_ldbl_opt.h>
Packit 6c4009
Packit 6c4009
#ifndef FLOAT_TYPE
Packit 6c4009
# define FLOAT_TYPE double
Packit 6c4009
# define FUNC_PREFIX
Packit 6c4009
# define FLOAT_FMT_FLAG
Packit 6c4009
/* Actually we have to write (DBL_DIG + log10 (DBL_MAX_10_EXP)) but we
Packit 6c4009
   don't have log10 available in the preprocessor.  */
Packit 6c4009
# define MAXDIG (NDIGIT_MAX + 3)
Packit 6c4009
# define FCVT_MAXDIG (DBL_MAX_10_EXP + MAXDIG)
Packit 6c4009
# if DBL_MANT_DIG == 53
Packit 6c4009
#  define NDIGIT_MAX 17
Packit 6c4009
# elif DBL_MANT_DIG == 24
Packit 6c4009
#  define NDIGIT_MAX 9
Packit 6c4009
# elif DBL_MANT_DIG == 56
Packit 6c4009
#  define NDIGIT_MAX 18
Packit 6c4009
# else
Packit 6c4009
/* See IEEE 854 5.6, table 2 for this formula.  Unfortunately we need a
Packit 6c4009
   compile time constant here, so we cannot use it.  */
Packit 6c4009
#  error "NDIGIT_MAX must be precomputed"
Packit 6c4009
#  define NDIGIT_MAX (lrint (ceil (M_LN2 / M_LN10 * DBL_MANT_DIG + 1.0)))
Packit 6c4009
# endif
Packit 6c4009
#else
Packit 6c4009
# define LONG_DOUBLE_CVT
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#define APPEND(a, b) APPEND2 (a, b)
Packit 6c4009
#define APPEND2(a, b) a##b
Packit 6c4009
#define __APPEND(a, b) __APPEND2 (a, b)
Packit 6c4009
#define __APPEND2(a, b) __##a##b
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define FCVT_BUFFER APPEND (FUNC_PREFIX, fcvt_buffer)
Packit 6c4009
#define FCVT_BUFPTR APPEND (FUNC_PREFIX, fcvt_bufptr)
Packit 6c4009
#define ECVT_BUFFER APPEND (FUNC_PREFIX, ecvt_buffer)
Packit 6c4009
Packit 6c4009
Packit 6c4009
static char FCVT_BUFFER[MAXDIG];
Packit 6c4009
static char ECVT_BUFFER[MAXDIG];
Packit 6c4009
libc_freeres_ptr (static char *FCVT_BUFPTR);
Packit 6c4009
Packit 6c4009
char *
Packit 6c4009
__APPEND (FUNC_PREFIX, fcvt) (FLOAT_TYPE value, int ndigit, int *decpt,
Packit 6c4009
			      int *sign)
Packit 6c4009
{
Packit 6c4009
  if (FCVT_BUFPTR == NULL)
Packit 6c4009
    {
Packit 6c4009
      if (__APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign,
Packit 6c4009
					  FCVT_BUFFER, MAXDIG) != -1)
Packit 6c4009
	return FCVT_BUFFER;
Packit 6c4009
Packit 6c4009
      FCVT_BUFPTR = (char *) malloc (FCVT_MAXDIG);
Packit 6c4009
      if (FCVT_BUFPTR == NULL)
Packit 6c4009
	return FCVT_BUFFER;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  (void) __APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign,
Packit 6c4009
					 FCVT_BUFPTR, FCVT_MAXDIG);
Packit 6c4009
Packit 6c4009
  return FCVT_BUFPTR;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
char *
Packit 6c4009
__APPEND (FUNC_PREFIX, ecvt) (FLOAT_TYPE value, int ndigit, int *decpt,
Packit 6c4009
			      int *sign)
Packit 6c4009
{
Packit 6c4009
  (void) __APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign,
Packit 6c4009
					 ECVT_BUFFER, MAXDIG);
Packit 6c4009
Packit 6c4009
  return ECVT_BUFFER;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
char *
Packit 6c4009
__APPEND (FUNC_PREFIX, gcvt) (FLOAT_TYPE value, int ndigit, char *buf)
Packit 6c4009
{
Packit 6c4009
  sprintf (buf, "%.*" FLOAT_FMT_FLAG "g", MIN (ndigit, NDIGIT_MAX), value);
Packit 6c4009
  return buf;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#if LONG_DOUBLE_COMPAT (libc, GLIBC_2_0)
Packit 6c4009
# ifdef LONG_DOUBLE_CVT
Packit 6c4009
#  define cvt_symbol(symbol) \
Packit 6c4009
  cvt_symbol_1 (libc, __APPEND (FUNC_PREFIX, symbol), \
Packit 6c4009
	      APPEND (FUNC_PREFIX, symbol), GLIBC_2_4)
Packit 6c4009
#  define cvt_symbol_1(lib, local, symbol, version) \
Packit 6c4009
    versioned_symbol (lib, local, symbol, version)
Packit 6c4009
# else
Packit 6c4009
#  define cvt_symbol(symbol) \
Packit 6c4009
  cvt_symbol_1 (libc, __APPEND (FUNC_PREFIX, symbol), \
Packit 6c4009
	      APPEND (q, symbol), GLIBC_2_0); \
Packit 6c4009
  strong_alias (__APPEND (FUNC_PREFIX, symbol), APPEND (FUNC_PREFIX, symbol))
Packit 6c4009
#  define cvt_symbol_1(lib, local, symbol, version) \
Packit 6c4009
  compat_symbol (lib, local, symbol, version)
Packit 6c4009
# endif
Packit 6c4009
#else
Packit 6c4009
# define cvt_symbol(symbol) \
Packit 6c4009
  strong_alias (__APPEND (FUNC_PREFIX, symbol), APPEND (FUNC_PREFIX, symbol))
Packit 6c4009
#endif
Packit 6c4009
cvt_symbol(fcvt);
Packit 6c4009
cvt_symbol(ecvt);
Packit 6c4009
cvt_symbol(gcvt);