Home | History | Annotate | Line # | Download | only in gdtoa
      1  1.4  riastrad /* $NetBSD: g_ffmt.c,v 1.4 2019/08/01 02:27:43 riastradh Exp $ */
      2  1.1    kleink 
      3  1.1    kleink /****************************************************************
      4  1.1    kleink 
      5  1.1    kleink The author of this software is David M. Gay.
      6  1.1    kleink 
      7  1.1    kleink Copyright (C) 1998 by Lucent Technologies
      8  1.1    kleink All Rights Reserved
      9  1.1    kleink 
     10  1.1    kleink Permission to use, copy, modify, and distribute this software and
     11  1.1    kleink its documentation for any purpose and without fee is hereby
     12  1.1    kleink granted, provided that the above copyright notice appear in all
     13  1.1    kleink copies and that both that the copyright notice and this
     14  1.1    kleink permission notice and warranty disclaimer appear in supporting
     15  1.1    kleink documentation, and that the name of Lucent or any of its entities
     16  1.1    kleink not be used in advertising or publicity pertaining to
     17  1.1    kleink distribution of the software without specific, written prior
     18  1.1    kleink permission.
     19  1.1    kleink 
     20  1.1    kleink LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
     21  1.1    kleink INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
     22  1.1    kleink IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
     23  1.1    kleink SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     24  1.1    kleink WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
     25  1.1    kleink IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
     26  1.1    kleink ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
     27  1.1    kleink THIS SOFTWARE.
     28  1.1    kleink 
     29  1.1    kleink ****************************************************************/
     30  1.1    kleink 
     31  1.1    kleink /* Please send bug reports to David M. Gay (dmg at acm dot org,
     32  1.1    kleink  * with " at " changed at "@" and " dot " changed to ".").	*/
     33  1.1    kleink 
     34  1.1    kleink #include "gdtoaimp.h"
     35  1.1    kleink 
     36  1.1    kleink  char*
     37  1.1    kleink #ifdef KR_headers
     38  1.3  christos g_ffmt(buf, f, ndig, bufsize) char *buf; float *f; int ndig; size_t bufsize;
     39  1.1    kleink #else
     40  1.3  christos g_ffmt(char *buf, float *f, int ndig, size_t bufsize)
     41  1.1    kleink #endif
     42  1.1    kleink {
     43  1.4  riastrad 	static CONST FPI fpi0 = { 24, 1-127-24+1,  254-127-24+1, 1, 0 };
     44  1.1    kleink 	char *b, *s, *se;
     45  1.1    kleink 	ULong bits[1], *L, sign;
     46  1.1    kleink 	int decpt, ex, i, mode;
     47  1.3  christos #ifdef Honor_FLT_ROUNDS
     48  1.3  christos #include "gdtoa_fltrnds.h"
     49  1.3  christos #else
     50  1.3  christos #define fpi &fpi0
     51  1.3  christos #endif
     52  1.1    kleink 
     53  1.1    kleink 	if (ndig < 0)
     54  1.1    kleink 		ndig = 0;
     55  1.1    kleink 	if (bufsize < ndig + 10)
     56  1.1    kleink 		return 0;
     57  1.1    kleink 
     58  1.1    kleink 	L = (ULong*)f;
     59  1.1    kleink 	sign = L[0] & 0x80000000L;
     60  1.1    kleink 	if ((L[0] & 0x7f800000) == 0x7f800000) {
     61  1.1    kleink 		/* Infinity or NaN */
     62  1.1    kleink 		if (L[0] & 0x7fffff) {
     63  1.1    kleink 			return strcp(buf, "NaN");
     64  1.1    kleink 			}
     65  1.1    kleink 		b = buf;
     66  1.1    kleink 		if (sign)
     67  1.1    kleink 			*b++ = '-';
     68  1.1    kleink 		return strcp(b, "Infinity");
     69  1.1    kleink 		}
     70  1.1    kleink 	if (*f == 0.) {
     71  1.1    kleink 		b = buf;
     72  1.1    kleink #ifndef IGNORE_ZERO_SIGN
     73  1.1    kleink 		if (L[0] & 0x80000000L)
     74  1.1    kleink 			*b++ = '-';
     75  1.1    kleink #endif
     76  1.1    kleink 		*b++ = '0';
     77  1.1    kleink 		*b = 0;
     78  1.1    kleink 		return b;
     79  1.1    kleink 		}
     80  1.1    kleink 	bits[0] = L[0] & 0x7fffff;
     81  1.1    kleink 	if ( (ex = (L[0] >> 23) & 0xff) !=0)
     82  1.1    kleink 		bits[0] |= 0x800000;
     83  1.1    kleink 	else
     84  1.1    kleink 		ex = 1;
     85  1.1    kleink 	ex -= 0x7f + 23;
     86  1.1    kleink 	mode = 2;
     87  1.1    kleink 	if (ndig <= 0) {
     88  1.1    kleink 		if (bufsize < 16)
     89  1.1    kleink 			return 0;
     90  1.1    kleink 		mode = 0;
     91  1.1    kleink 		}
     92  1.1    kleink 	i = STRTOG_Normal;
     93  1.3  christos 	s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se);
     94  1.2  christos 	if (s == NULL)
     95  1.2  christos 		return NULL;
     96  1.3  christos 	return g__fmt(buf, s, se, decpt, sign, bufsize);
     97  1.1    kleink 	}
     98