5 #define ABS(x) ((x) >= 0 ? (x) : -(x))
10 format (char * res
, const __float128 x
, size_t n
)
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
) ? '-' : '+');
25 // We have a number smaller than 1, without exponent
29 for (c
= p
+1; *c
== '0'; c
++)
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);
44 // Now, do we already have an exponent
46 for (c
= p
; *c
&& *c
!= 'e'; c
++)
50 int exp
= strtol (c
+ 1, NULL
, 10);
54 memcpy (res
+ 1, p
, l
);
56 memset (res
+ 1 + l
, '0', (int) n
- l
+ 2);
58 sprintf (res
+ n
+ 3, "e%c%02d", exp
>= 0 ? '+' : '-', ABS(exp
));
64 // If we have no exponent, normalize and add the exponent
65 for (c
= p
; *c
&& *c
!= '.'; c
++)
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);
84 quadmath_dtoaq (char *s
, size_t size
, size_t n
, __float128 x
)
87 memset (buffer
, 0, sizeof(buffer
));
88 format (buffer
, x
, n
);
89 memcpy (s
, buffer
, size
);