From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 230783858D28; Thu, 16 Dec 2021 23:20:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 230783858D28 From: "fxcoudert at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libfortran/98076] Increase speed of integer I/O Date: Thu, 16 Dec 2021 23:20:55 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libfortran X-Bugzilla-Version: 11.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: enhancement X-Bugzilla-Who: fxcoudert at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 16 Dec 2021 23:20:55 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D98076 --- Comment #6 from Francois-Xavier Coudert = --- Integrating this quick patch into libgfortran, here are the timings to make= a formatted write of 10 million integers into a string. - very small value (1), negligible speedup (2.273s to 2.248s) - small value (1042), speedup of 28% (3.224s to 2.350s) - huge(0_8), speed up of 50% (5.914s to 2.560s) - huge(0_16), speed up of 83% (19.46s to 3.31s) Conclusion: this looks quite interesting! diff --git a/libgfortran/runtime/string.c b/libgfortran/runtime/string.c index 536a9cd3f2b..844ff6e65ce 100644 --- a/libgfortran/runtime/string.c +++ b/libgfortran/runtime/string.c @@ -25,6 +25,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively. = If not, see #include "libgfortran.h" #include #include +#include /* Given a fortran string, return its length exclusive of the trailing @@ -169,6 +170,19 @@ find_option (st_parameter_common *cmp, const char *s1, gfc_charlen_type s1_len, } +/* Fast helper function for a positive value that fits in uint64_t. */ +static char * +itoa64 (uint64_t n, char *p) +{ + while (n !=3D 0) + { + *--p =3D '0' + (n % 10); + n /=3D 10; + } + return p; +} + + /* gfc_itoa()-- Integer to decimal conversion. The itoa function is a widespread non-standard extension to standard C, often declared in . Even though the itoa @@ -202,11 +216,24 @@ gfc_itoa (GFC_INTEGER_LARGEST n, char *buffer, size_t len) p =3D buffer + GFC_ITOA_BUF_SIZE - 1; *p =3D '\0'; - while (t !=3D 0) - { - *--p =3D '0' + (t % 10); - t /=3D 10; - } + if (t <=3D UINT64_MAX) { + /* If the value fits in uint64_t, use the fast function. */ + p =3D itoa64(t, p); + } else { + /* Otherwise, break down into smaller bits by division. Two calls to t= he + uint64_t function are sufficient for all 128-bit signed integer + values, up to 2^127 - 1. */ +#define TEN19 ((GFC_UINTEGER_LARGEST) 1000000 * (GFC_UINTEGER_LARGEST) 100= 0000 * (GFC_UINTEGER_LARGEST) 10000000) + static_assert(sizeof(GFC_UINTEGER_LARGEST) <=3D 2 * sizeof(uint64_t)); + + GFC_UINTEGER_LARGEST r; + r =3D t % TEN19; + t =3D t / TEN19; + p =3D itoa64(r, p); + + assert(t <=3D UINT64_MAX); + p =3D itoa64(t, p); + } if (negative) *--p =3D '-';=