1 /* GCC Quad-Precision Math Library
2 Copyright (C) 2010 Free Software Foundation, Inc.
3 Written by Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 Libiberty is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with libiberty; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19 Boston, MA 02110-1301, USA. */
25 #define ABS(x) ((x) >= 0 ? (x) : -(x))
30 format (char * res
, const __float128 x
, size_t n
)
35 memset (buffer
, 0, sizeof(buffer
));
37 g_Qfmt (buffer
, &x
, n
+ 1, sizeof(buffer
) - 3);
38 p
= buffer
+ (*buffer
== '-' ? 1 : 0);
40 // The sign is the easiest part
41 res
[0] = (signbitq (x
) ? '-' : '+');
45 // We have a number smaller than 1, without exponent
49 for (c
= p
+1; *c
== '0'; c
++)
52 // We move the string "exp" characters left
53 size_t l
= strlen (p
+1+exp
);
54 memcpy (res
+ 2, p
+ 1 + exp
, l
);
55 memset (res
+ 2 + l
, '0', n
- l
+ 1);
56 sprintf (res
+ n
+ 3, "e-%02d", exp
+ 1);
64 // Now, do we already have an exponent
66 for (c
= p
; *c
&& *c
!= 'e'; c
++)
70 int exp
= strtol (c
+ 1, NULL
, 10);
74 memcpy (res
+ 1, p
, l
);
76 memset (res
+ 1 + l
, '0', (int) n
- l
+ 2);
78 sprintf (res
+ n
+ 3, "e%c%02d", exp
>= 0 ? '+' : '-', ABS(exp
));
84 // If we have no exponent, normalize and add the exponent
85 for (c
= p
; *c
&& *c
!= '.'; c
++)
92 memcpy (res
+ 3, p
+ 1, l
);
93 size_t l2
= strlen (c
+ 1);
94 memcpy (res
+ 2 + l
, c
+ 1, l2
);
95 memset (res
+ 2 + l
+ l2
, '0', n
- (l
+ l2
) + 1);
96 sprintf (res
+ n
+ 3, "e+%02d", l
- 1);
104 quadmath_dtoaq (char *s
, size_t size
, size_t n
, __float128 x
)
107 memset (buffer
, 0, sizeof(buffer
));
108 format (buffer
, x
, n
);
109 memcpy (s
, buffer
, size
);