11.6Sriastrad/* $NetBSD: ldtoa.c,v 1.6 2019/08/01 02:27:43 riastradh 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.6Sriastrad__RCSID("$NetBSD: ldtoa.c,v 1.6 2019/08/01 02:27:43 riastradh 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.4Schristos#ifndef __vax__ 421.1Schristos#include <machine/ieee.h> 431.4Schristos#endif 441.1Schristos#include "gdtoaimp.h" 451.1Schristos 461.1Schristos/* 471.1Schristos * ldtoa() is a wrapper for gdtoa() that makes it smell like dtoa(), 481.1Schristos * except that the floating point argument is passed by reference. 491.1Schristos * When dtoa() is passed a NaN or infinity, it sets expt to 9999. 501.1Schristos * However, a long double could have a valid exponent of 9999, so we 511.1Schristos * use INT_MAX in ldtoa() instead. 521.1Schristos */ 531.1Schristoschar * 541.1Schristosldtoa(long double *ld, int mode, int ndigits, int *decpt, int *sign, char **rve) 551.1Schristos{ 561.1Schristos#ifdef EXT_EXPBITS 571.6Sriastrad static CONST FPI fpi = { 581.1Schristos LDBL_MANT_DIG, /* nbits */ 591.1Schristos LDBL_MIN_EXP - LDBL_MANT_DIG, /* emin */ 601.1Schristos LDBL_MAX_EXP - LDBL_MANT_DIG, /* emax */ 611.1Schristos FPI_Round_near, /* rounding */ 621.1Schristos#ifdef Sudden_Underflow /* unused, but correct anyway */ 631.1Schristos 1 641.1Schristos#else 651.1Schristos 0 661.1Schristos#endif 671.1Schristos }; 681.1Schristos int be, kind; 691.1Schristos char *ret; 701.1Schristos union ieee_ext_u u; 711.1Schristos uint32_t bits[(LDBL_MANT_DIG + 31) / 32]; 721.1Schristos 731.1Schristos u.extu_ld = *ld; 741.1Schristos *sign = u.extu_ext.ext_sign; 751.1Schristos be = u.extu_ext.ext_exp - (LDBL_MAX_EXP - 1) - (LDBL_MANT_DIG - 1); 761.1Schristos EXT_TO_ARRAY32(u, bits); 771.1Schristos 781.1Schristos switch (fpclassify(u.extu_ld)) { 791.1Schristos case FP_NORMAL: 801.1Schristos kind = STRTOG_Normal; 811.1Schristos#ifdef LDBL_IMPLICIT_NBIT 821.1Schristos bits[LDBL_MANT_DIG / 32] |= 1 << ((LDBL_MANT_DIG - 1) % 32); 831.1Schristos#endif /* LDBL_IMPLICIT_NBIT */ 841.1Schristos break; 851.1Schristos case FP_ZERO: 861.1Schristos kind = STRTOG_Zero; 871.1Schristos break; 881.1Schristos case FP_SUBNORMAL: 891.1Schristos kind = STRTOG_Denormal; 901.1Schristos be++; 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