ldtoa.c revision 1.3
11.3Suwe/*	$NetBSD: ldtoa.c,v 1.3 2007/02/04 03:20:23 uwe Exp $	*/
21.1Schristos
31.1Schristos/*-
41.1Schristos * Copyright (c) 2003 David Schultz <das@FreeBSD.ORG>
51.1Schristos * All rights reserved.
61.1Schristos *
71.1Schristos * Redistribution and use in source and binary forms, with or without
81.1Schristos * modification, are permitted provided that the following conditions
91.1Schristos * are met:
101.1Schristos * 1. Redistributions of source code must retain the above copyright
111.1Schristos *    notice, this list of conditions and the following disclaimer.
121.1Schristos * 2. Redistributions in binary form must reproduce the above copyright
131.1Schristos *    notice, this list of conditions and the following disclaimer in the
141.1Schristos *    documentation and/or other materials provided with the distribution.
151.1Schristos *
161.1Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
171.1Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181.1Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191.1Schristos * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
201.1Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211.1Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221.1Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
231.1Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241.1Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Schristos * SUCH DAMAGE.
271.1Schristos */
281.1Schristos
291.1Schristos#include <sys/cdefs.h>
301.1Schristos#if 0
311.1Schristos__FBSDID("$FreeBSD: src/lib/libc/gdtoa/_ldtoa.c,v 1.2 2004/01/18 07:53:49 das Exp $");
321.1Schristos#else
331.3Suwe__RCSID("$NetBSD: ldtoa.c,v 1.3 2007/02/04 03:20:23 uwe Exp $");
341.1Schristos#endif
351.1Schristos
361.1Schristos#include <float.h>
371.1Schristos#include <inttypes.h>
381.1Schristos#include <limits.h>
391.1Schristos#include <math.h>
401.1Schristos#include <stdlib.h>
411.1Schristos#include <machine/ieee.h>
421.1Schristos#include "gdtoaimp.h"
431.1Schristos
441.1Schristos/*
451.1Schristos * ldtoa() is a wrapper for gdtoa() that makes it smell like dtoa(),
461.1Schristos * except that the floating point argument is passed by reference.
471.1Schristos * When dtoa() is passed a NaN or infinity, it sets expt to 9999.
481.1Schristos * However, a long double could have a valid exponent of 9999, so we
491.1Schristos * use INT_MAX in ldtoa() instead.
501.1Schristos */
511.1Schristoschar *
521.1Schristosldtoa(long double *ld, int mode, int ndigits, int *decpt, int *sign, char **rve)
531.1Schristos{
541.1Schristos#ifdef EXT_EXPBITS
551.1Schristos	static FPI fpi = {
561.1Schristos		LDBL_MANT_DIG,			/* nbits */
571.1Schristos		LDBL_MIN_EXP - LDBL_MANT_DIG,	/* emin */
581.1Schristos		LDBL_MAX_EXP - LDBL_MANT_DIG,	/* emax */
591.1Schristos		FPI_Round_near,	       		/* rounding */
601.1Schristos#ifdef Sudden_Underflow	/* unused, but correct anyway */
611.1Schristos		1
621.1Schristos#else
631.1Schristos		0
641.1Schristos#endif
651.1Schristos	};
661.1Schristos	int be, kind;
671.1Schristos	char *ret;
681.1Schristos	union ieee_ext_u u;
691.1Schristos	uint32_t bits[(LDBL_MANT_DIG + 31) / 32];
701.1Schristos
711.1Schristos	u.extu_ld = *ld;
721.1Schristos	*sign = u.extu_ext.ext_sign;
731.1Schristos	be = u.extu_ext.ext_exp - (LDBL_MAX_EXP - 1) - (LDBL_MANT_DIG - 1);
741.1Schristos	EXT_TO_ARRAY32(u, bits);
751.1Schristos
761.1Schristos	switch (fpclassify(u.extu_ld)) {
771.1Schristos	case FP_NORMAL:
781.1Schristos		kind = STRTOG_Normal;
791.1Schristos#ifdef	LDBL_IMPLICIT_NBIT
801.1Schristos		bits[LDBL_MANT_DIG / 32] |= 1 << ((LDBL_MANT_DIG - 1) % 32);
811.1Schristos#endif /* LDBL_IMPLICIT_NBIT */
821.1Schristos		break;
831.1Schristos	case FP_ZERO:
841.1Schristos		kind = STRTOG_Zero;
851.1Schristos		break;
861.1Schristos	case FP_SUBNORMAL:
871.1Schristos		kind = STRTOG_Denormal;
881.1Schristos#ifdef	LDBL_IMPLICIT_NBIT
891.1Schristos		be++;
901.1Schristos#endif
911.1Schristos		break;
921.1Schristos	case FP_INFINITE:
931.1Schristos		kind = STRTOG_Infinite;
941.1Schristos		break;
951.1Schristos	case FP_NAN:
961.1Schristos		kind = STRTOG_NaN;
971.1Schristos		break;
981.1Schristos	default:
991.1Schristos		abort();
1001.1Schristos	}
1011.1Schristos
1021.1Schristos	ret = gdtoa(&fpi, be, (ULong *)bits, &kind, mode, ndigits, decpt, rve);
1031.1Schristos	if (*decpt == -32768)
1041.1Schristos		*decpt = INT_MAX;
1051.1Schristos	return ret;
1061.1Schristos#else
1071.3Suwe	return dtoa((double)*ld, mode, ndigits, decpt, sign, rve);
1081.1Schristos#endif
1091.1Schristos}
110