2010-11-27 François Dumont <francois.cppdevs@free.fr>
[official-gcc.git] / libquadmath / quadmath_io.c
blob82084f245b2978632ade11b69fef5bc6f90913cd
1 #include "quadmath.h"
2 #include <stdio.h>
3 #include <string.h>
5 #define ABS(x) ((x) >= 0 ? (x) : -(x))
9 static void
10 format (char * res, const __float128 x, size_t n)
12 char buffer[1024];
13 char *p;
15 memset (buffer, 0, sizeof(buffer));
17 g_Qfmt (buffer, &x, n + 1, sizeof(buffer) - 3);
18 p = buffer + (*buffer == '-' ? 1 : 0);
20 // The sign is the easiest part
21 res[0] = (signbitq (x) ? '-' : '+');
23 if (*p == '.')
25 // We have a number smaller than 1, without exponent
26 int exp = 0;
27 char *c;
29 for (c = p+1; *c == '0'; c++)
30 exp++;
32 // We move the string "exp" characters left
33 size_t l = strlen (p+1+exp);
34 memcpy (res + 2, p + 1 + exp, l);
35 memset (res + 2 + l, '0', n - l + 1);
36 sprintf (res + n + 3, "e-%02d", exp + 1);
38 res[1] = res[2];
39 res[2] = '.';
41 return;
44 // Now, do we already have an exponent
45 char *c;
46 for (c = p; *c && *c != 'e'; c++)
48 if (*c)
50 int exp = strtol (c + 1, NULL, 10);
52 size_t l = c - p;
54 memcpy (res + 1, p, l);
55 if (l <= n + 1)
56 memset (res + 1 + l, '0', (int) n - l + 2);
58 sprintf (res + n + 3, "e%c%02d", exp >= 0 ? '+' : '-', ABS(exp));
60 return;
62 else
64 // If we have no exponent, normalize and add the exponent
65 for (c = p; *c && *c != '.'; c++)
68 res[1] = *p;
69 res[2] = '.';
71 size_t l = c - p;
72 memcpy (res + 3, p + 1, l);
73 size_t l2 = strlen (c + 1);
74 memcpy (res + 2 + l, c + 1, l2);
75 memset (res + 2 + l + l2, '0', n - (l + l2) + 1);
76 sprintf (res + n + 3, "e+%02d", l - 1);
78 return;
83 void
84 quadmath_dtoaq (char *s, size_t size, size_t n, __float128 x)
86 char buffer[1024];
87 memset (buffer, 0, sizeof(buffer));
88 format (buffer, x, n);
89 memcpy (s, buffer, size);