public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "fxcoudert at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libfortran/98076] Increase speed of integer I/O
Date: Thu, 16 Dec 2021 21:40:09 +0000	[thread overview]
Message-ID: <bug-98076-4-wONeHDvwH7@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-98076-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98076

--- Comment #5 from Francois-Xavier Coudert <fxcoudert at gcc dot gnu.org> ---
Consider this:

/* Fast helper function for a positive value that fits in uint64_t.  */
char *itoa64 (uint64_t n, char *p)
{
  while (n != 0)
    {
      *--p = '0' + (n % 10);
      n /= 10;
    }
  return p;
}


Then the logic inside gfc_itoa is reworked like this:

%%%%%%%%%%

  *p = '\0';

#if 1
  i (t <= UINT64_MAX) {
    p = toa64(t, p);
  } else {
    unsigned __int128 r;

#define TEN19 ((unsigned __int128) 1000000 * (unsigned __int128) 1000000 *
(unsigned __int128) 10000000)

    r = t % TEN19;
    t = t / TEN19;
    p = itoa64(r, p);

    assert(t <= UINT64_MAX);
    p = itoa64(t, p);
  }
#else
  while (t != 0)
    {
      *--p = '0' + (t % 10);
      t /= 10;
    }
#endif

  if (negative)
    *--p = '-';

  return p;

%%%%%%%%%%

where the "#if 1" branch is the new code, and the "#else" branch is the old
code. Unless I'm mistaken, two calls to itoa64() are sufficient to guarantee
that we can output all values up to INT128_MAX = 2^127 - 1. That's because
(2^127 - 1) / 10^19 is smaller than UINT64_MAX = 2^64 - 1.

We would need three calls if we wanted to output values up to UINT128_MAX =
2^128 - 1, but we don't: Fortran only deals with signed integers.



Benchmark of this code, for formatting of 10 millions numbers into a buffer:

- formatting 1042:       old code 0.31 seconds, new code 0.06 seconds
- formatting INT32_MAX:  old code 0.88 seconds, new code 0.07 seconds
- formatting INT64_MAX:  old code 1.77 seconds, new code 0.16 seconds
- formatting INT128_MAX: old code 3.74 seconds, new code 0.49 seconds


It remains to be seen how much of the actual Fortran I/O is bound by
gfc_itoa(). But the benchmarking is interesting enough that it ought to be
tried in a real example inside libgfortran, which is what I will do next.

  parent reply	other threads:[~2021-12-16 21:40 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-01  6:49 [Bug libfortran/98076] New: " tkoenig at gcc dot gnu.org
2020-12-01  6:50 ` [Bug libfortran/98076] " tkoenig at gcc dot gnu.org
2021-04-16 16:22 ` tkoenig at gcc dot gnu.org
2021-04-27 11:39 ` jakub at gcc dot gnu.org
2021-07-28  7:05 ` rguenth at gcc dot gnu.org
2021-07-29 19:18 ` anlauf at gcc dot gnu.org
2021-12-16 21:31 ` fxcoudert at gcc dot gnu.org
2021-12-16 21:40 ` fxcoudert at gcc dot gnu.org [this message]
2021-12-16 23:20 ` fxcoudert at gcc dot gnu.org
2021-12-17  9:05 ` fxcoudert at gcc dot gnu.org
2021-12-25 13:04 ` fxcoudert at gcc dot gnu.org
2021-12-26 11:14 ` fxcoudert at gcc dot gnu.org
2021-12-27 20:13 ` ro at gcc dot gnu.org
2021-12-27 20:25 ` fxcoudert at gcc dot gnu.org
2021-12-27 21:59 ` ro at CeBiTec dot Uni-Bielefeld.DE

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-98076-4-wONeHDvwH7@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).