Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
g_dfmt.c File Reference
#include "gdtoaimp.h"
Include dependency graph for g_dfmt.c:

Go to the source code of this file.

Functions

char * g_dfmt (char *buf, double *d, int ndig, unsigned bufsize)
 

Function Documentation

◆ g_dfmt()

char* g_dfmt ( char *  buf,
double *  d,
int  ndig,
unsigned  bufsize 
)

Definition at line 41 of file g_dfmt.c.

43 {
44  static FPI fpi = {53, 1 - 1023 - 53 + 1, 2046 - 1023 - 53 + 1, 1, 0};
45  char *b, *s, *se;
46  ULong bits[2], *L, sign;
47  int decpt, ex, i, mode;
48 
49  if(ndig < 0)
50  {
51  {
52  ndig = 0;
53  }
54  }
55  if(bufsize < (unsigned)ndig + 10)
56  {
57  return 0;
58  }
59 
60  L = (ULong*)d;
61  sign = L[_0] & 0x80000000L;
62  if((L[_0] & 0x7ff00000) == 0x7ff00000)
63  {
64  /* Infinity or NaN */
65  if(L[_0] & 0xfffff || L[_1])
66  {
67  return strcp(buf, "NaN");
68  }
69  b = buf;
70  if(sign)
71  {
72  {
73  *b++ = '-';
74  }
75  }
76  return strcp(b, "Infinity");
77  }
78  if(L[_1] == 0 && (L[_0] ^ sign) == 0 /*d == 0.*/)
79  {
80  b = buf;
81 #ifndef IGNORE_ZERO_SIGN
82  if(L[_0] & 0x80000000L)
83  {
84  {
85  *b++ = '-';
86  }
87  }
88 #endif
89  *b++ = '0';
90  *b = 0;
91  return b;
92  }
93  bits[0] = L[_1];
94  bits[1] = L[_0] & 0xfffff;
95  if((ex = (L[_0] >> 20) & 0x7ff) != 0)
96  {
97  {
98  bits[1] |= 0x100000;
99  }
100  }
101  else
102  {
103  {
104  ex = 1;
105  }
106  }
107  ex -= 0x3ff + 52;
108  mode = 2;
109  if(ndig <= 0)
110  {
111  if(bufsize < 25)
112  {
113  {
114  return 0;
115  }
116  }
117  mode = 0;
118  }
119  i = STRTOG_Normal;
120  s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se);
121  return g__fmt(buf, s, se, decpt, sign);
122 }
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
unsigned Long ULong
Definition: gdtoa.h:41
Definition: gdtoa.h:86
#define strcp
Definition: gdtoaimp.h:541

References g__fmt(), gdtoa(), strcp, and STRTOG_Normal.