summaryrefslogtreecommitdiffstats
path: root/libgfortran
diff options
context:
space:
mode:
authorFrancois-Xavier Coudert <fxcoudert@gmail.com>2021-12-26 11:59:14 +0100
committerFrancois-Xavier Coudert <fxcoudert@gmail.com>2021-12-26 12:00:00 +0100
commit9525c26bf19318bed72d3bc3b99dceac5217102f (patch)
tree8ce575df2473b27132d04ecf9dc8de8f1730b953 /libgfortran
parentDaily bump. (diff)
downloadgcc-9525c26bf19318bed72d3bc3b99dceac5217102f.tar.gz
gcc-9525c26bf19318bed72d3bc3b99dceac5217102f.tar.bz2
gcc-9525c26bf19318bed72d3bc3b99dceac5217102f.tar.xz
Fortran: speed up decimal output of integers
libgfortran/ChangeLog: PR libfortran/98076 * runtime/string.c (itoa64, itoa64_pad19): New helper functions. (gfc_itoa): On targets with 128-bit integers, call fast 64-bit functions to avoid many slow divisions. gcc/testsuite/ChangeLog: PR libfortran/98076 * gfortran.dg/pr98076.f90: New test.
Diffstat (limited to 'libgfortran')
-rw-r--r--libgfortran/runtime/string.c65
1 files changed, 60 insertions, 5 deletions
diff --git a/libgfortran/runtime/string.c b/libgfortran/runtime/string.c
index 835027a7cd6..0ccd731852a 100644
--- a/libgfortran/runtime/string.c
+++ b/libgfortran/runtime/string.c
@@ -23,6 +23,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23<http://www.gnu.org/licenses/>. */ 23<http://www.gnu.org/licenses/>. */
24 24
25#include "libgfortran.h" 25#include "libgfortran.h"
26#include <assert.h>
26#include <string.h> 27#include <string.h>
27#include <strings.h> 28#include <strings.h>
28 29
@@ -169,6 +170,38 @@ find_option (st_parameter_common *cmp, const char *s1, gfc_charlen_type s1_len,
169} 170}
170 171
171 172
173/* Fast helper function for a positive value that fits in uint64_t. */
174
175static inline char *
176itoa64 (uint64_t n, char *p)
177{
178 while (n != 0)
179 {
180 *--p = '0' + (n % 10);
181 n /= 10;
182 }
183 return p;
184}
185
186
187#if defined(HAVE_GFC_INTEGER_16)
188# define TEN19 ((GFC_UINTEGER_LARGEST) 1000000 * (GFC_UINTEGER_LARGEST) 1000000 * (GFC_UINTEGER_LARGEST) 10000000)
189
190/* Same as itoa64(), with zero padding of 19 digits. */
191
192static inline char *
193itoa64_pad19 (uint64_t n, char *p)
194{
195 for (int k = 0; k < 19; k++)
196 {
197 *--p = '0' + (n % 10);
198 n /= 10;
199 }
200 return p;
201}
202#endif
203
204
172/* Integer to decimal conversion. 205/* Integer to decimal conversion.
173 206
174 This function is much more restricted than the widespread (but 207 This function is much more restricted than the widespread (but
@@ -195,11 +228,33 @@ gfc_itoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
195 p = buffer + GFC_ITOA_BUF_SIZE - 1; 228 p = buffer + GFC_ITOA_BUF_SIZE - 1;
196 *p = '\0'; 229 *p = '\0';
197 230
198 while (n != 0) 231#if defined(HAVE_GFC_INTEGER_16)
232 /* On targets that have a 128-bit integer type, division in that type
233 is slow, because it occurs through a function call. We avoid that. */
234
235 if (n <= UINT64_MAX)
236 /* If the value fits in uint64_t, use the fast function. */
237 return itoa64 (n, p);
238 else
199 { 239 {
200 *--p = '0' + (n % 10); 240 /* Otherwise, break down into smaller bits by division. Two calls to
201 n /= 10; 241 the uint64_t function are not sufficient for all 128-bit unsigned
242 integers (we would need three calls), but they do suffice for all
243 values up to 2^127, which is the largest that Fortran can produce
244 (-HUGE(0_16)-1) with its signed integer types. */
245 static_assert(sizeof(GFC_UINTEGER_LARGEST) <= 2 * sizeof(uint64_t));
246
247 GFC_UINTEGER_LARGEST r;
248 r = n % TEN19;
249 n = n / TEN19;
250 assert (r <= UINT64_MAX);
251 p = itoa64_pad19 (r, p);
252
253 assert(n <= UINT64_MAX);
254 return itoa64 (n, p);
202 } 255 }
203 256#else
204 return p; 257 /* On targets where the largest integer is 64-bit, just use that. */
258 return itoa64 (n, p);
259#endif
205} 260}