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