public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: "H.J. Lu" <hjl.tools@gmail.com>
To: Florian Weimer <fweimer@redhat.com>
Cc: "H.J. Lu via Libc-alpha" <libc-alpha@sourceware.org>,
	 Tulio Magno Quites Machado Filho <tuliom@ascii.art.br>
Subject: Re: [PATCH] Correct timespec implementation [BZ #26232]
Date: Tue, 14 Jul 2020 06:12:23 -0700	[thread overview]
Message-ID: <CAMe9rOp6jg1VHJmq44ehtDuqa3DrR_pEprt8vsAzYxTxb7ncbg@mail.gmail.com> (raw)
In-Reply-To: <87sgdu8f3p.fsf@oldenburg2.str.redhat.com>

On Tue, Jul 14, 2020 at 5:18 AM Florian Weimer <fweimer@redhat.com> wrote:
>
> * H. J. Lu:
>
> > There are
> >
> > time_t
> > support_timespec_ns (struct timespec time)
> > {
> >   time_t time_ns;
> >   if (INT_MULTIPLY_WRAPV(time.tv_sec, TIMESPEC_HZ, &time_ns))
> >     return time.tv_sec < 0 ? TYPE_MINIMUM(time_t) : TYPE_MAXIMUM(time_t);
> >   if (INT_ADD_WRAPV(time_ns, time.tv_nsec, &time_ns))
> >     return time.tv_nsec < 0 ? TYPE_MINIMUM(time_t) : TYPE_MAXIMUM(time_t);
> >   return time_ns;
> > }
> >
> > Even if support_timespec_ns is changed to return long long, we still
> > may need to keep
> >
> >  time_t time_ns;
> >
> > for
> >
> >    if (INT_MULTIPLY_WRAPV(time.tv_sec, TIMESPEC_HZ, &time_ns))
> >
> > and
> >
> >   if (INT_ADD_WRAPV(time_ns, time.tv_nsec, &time_ns))
> >
> > It looks odd to return long long here.
>
> Why?
>
> You can use the GCC built-ins for checked arithmetic, they support mixed
> types.
>

This doesn't work on i686:

diff --git a/support/timespec.c b/support/timespec.c
index edbdb165ec..c35e8a8201 100644
--- a/support/timespec.c
+++ b/support/timespec.c
@@ -63,10 +63,10 @@ test_timespec_equal_or_after_impl (const char
*file, int line,
 /* Convert TIME to nanoseconds stored in a time_t.
    Returns time_t maximum or minimum if the conversion overflows
    or underflows, respectively.  */
-time_t
+long long
 support_timespec_ns (struct timespec time)
 {
-  time_t time_ns;
+  long long time_ns;
   if (INT_MULTIPLY_WRAPV(time.tv_sec, TIMESPEC_HZ, &time_ns))
     return time.tv_sec < 0 ? TYPE_MINIMUM(time_t) : TYPE_MAXIMUM(time_t);
   if (INT_ADD_WRAPV(time_ns, time.tv_nsec, &time_ns))
diff --git a/support/timespec.h b/support/timespec.h
index 1a6775a882..68055f42c3 100644
--- a/support/timespec.h
+++ b/support/timespec.h
@@ -48,7 +48,7 @@ void test_timespec_equal_or_after_impl (const char
*file, int line,
                                         const struct timespec left,
                                         const struct timespec right);

-time_t support_timespec_ns (struct timespec time);
+long long support_timespec_ns (struct timespec time);

 struct timespec support_timespec_normalize (struct timespec time);

This works:

diff --git a/support/timespec.c b/support/timespec.c
index edbdb165ec..b117e7c0d2 100644
--- a/support/timespec.c
+++ b/support/timespec.c
@@ -63,7 +63,7 @@ test_timespec_equal_or_after_impl (const char *file, int line,
 /* Convert TIME to nanoseconds stored in a time_t.
    Returns time_t maximum or minimum if the conversion overflows
    or underflows, respectively.  */
-time_t
+long long
 support_timespec_ns (struct timespec time)
 {
   time_t time_ns;
diff --git a/support/timespec.h b/support/timespec.h
index 1a6775a882..68055f42c3 100644
--- a/support/timespec.h
+++ b/support/timespec.h
@@ -48,7 +48,7 @@ void test_timespec_equal_or_after_impl (const char
*file, int line,
                                         const struct timespec left,
                                         const struct timespec right);

-time_t support_timespec_ns (struct timespec time);
+long long support_timespec_ns (struct timespec time);

 struct timespec support_timespec_normalize (struct timespec time);

What is the advantage of long long over time_t?

-- 
H.J.

  reply	other threads:[~2020-07-14 13:13 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-06 14:48 [PATCH v2] Fix time/tst-cpuclock1 intermitent failures Lucas A. M. Magalhaes
2020-02-17 16:44 ` Lucas A. M. Magalhaes
2020-02-18 12:44 ` Adhemerval Zanella
2020-02-19 16:42   ` Lucas A. M. Magalhaes
2020-02-19 18:51     ` Adhemerval Zanella
2020-02-20 18:17 ` [PATCH v3] " Lucas A. M. Magalhaes
2020-03-04 19:24   ` Matheus Castanho
2020-03-06 17:31     ` Lucas A. M. Magalhaes
2020-03-10 16:20   ` [PATCH v4] " Lucas A. M. Magalhaes
2020-03-10 16:30     ` Andreas Schwab
2020-03-10 17:45     ` Carlos O'Donell
2020-03-23 17:20     ` [PATCH v5] " Lucas A. M. Magalhaes
2020-03-23 21:06       ` Carlos O'Donell
2020-03-24 19:42         ` Lucas A. M. Magalhaes
2020-03-31 18:55           ` Carlos O'Donell
2020-03-31 11:34       ` [PATCH v6] " Lucas A. M. Magalhaes
2020-03-31 19:02         ` Carlos O'Donell
2020-04-03 19:24         ` [PATCH v7] " Lucas A. M. Magalhaes
2020-04-03 20:48           ` Carlos O'Donell
2020-04-07 13:59           ` [PATCH v8] " Lucas A. M. Magalhaes
2020-04-16 17:30             ` Lucas A. M. Magalhaes
2020-04-16 19:21             ` Carlos O'Donell
2020-04-21 17:44             ` [PATCH v9] " Lucas A. M. Magalhaes
2020-05-11 17:41               ` Lucas A. M. Magalhaes
2020-05-25 11:46                 ` Lucas A. M. Magalhaes
2020-06-08 13:58                   ` Lucas A. M. Magalhaes
2020-06-08 16:52               ` Carlos O'Donell
2020-06-12 15:28               ` [PATCH v10] " Lucas A. M. Magalhaes
2020-06-25 17:26                 ` Lucas A. M. Magalhaes
2020-07-06 14:15                   ` Lucas A. M. Magalhaes
2020-07-07 20:12                 ` Carlos O'Donell
2020-07-10 23:07                   ` Tulio Magno Quites Machado Filho
2020-07-11 14:45                     ` H.J. Lu
2020-07-11 16:31                       ` H.J. Lu
2020-07-13 23:30                         ` [PATCH] Correct timespec implementation [BZ #26232] H.J. Lu
2020-07-14  2:35                           ` Carlos O'Donell
2020-07-14 11:16                           ` Florian Weimer
2020-07-14 11:42                             ` H.J. Lu
2020-07-14 12:04                               ` H.J. Lu
2020-07-14 12:18                                 ` Florian Weimer
2020-07-14 13:12                                   ` H.J. Lu [this message]
2020-07-14 13:14                                     ` Florian Weimer
2020-07-14 13:17                                       ` H.J. Lu
2020-07-15 19:38                                         ` Paul Eggert
2020-07-15 19:44                                           ` H.J. Lu
2020-07-14 13:08                               ` Lucas A. M. Magalhaes

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=CAMe9rOp6jg1VHJmq44ehtDuqa3DrR_pEprt8vsAzYxTxb7ncbg@mail.gmail.com \
    --to=hjl.tools@gmail.com \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=tuliom@ascii.art.br \
    /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).