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