public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Willy Tarreau <w@1wt.eu>
To: "Thomas Weißschuh" <thomas.weissschuh@linutronix.de>
Cc: "Thomas Weißschuh" <linux@weissschuh.net>,
	"Shuah Khan" <shuah@kernel.org>,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org
Subject: Re: [PATCH 11/15] tools/nolibc: add difftime()
Date: Sat, 26 Apr 2025 12:45:20 +0200	[thread overview]
Message-ID: <20250426104520.GD17549@1wt.eu> (raw)
In-Reply-To: <20250423-nolibc-misc-v1-11-a925bf40297b@linutronix.de>

On Wed, Apr 23, 2025 at 05:01:41PM +0200, Thomas Weißschuh wrote:
> This is used in various selftests and will be handy when integrating
> those with nolibc.
> 
> Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
> ---
>  tools/include/nolibc/time.h                  | 7 +++++++
>  tools/testing/selftests/nolibc/nolibc-test.c | 1 +
>  2 files changed, 8 insertions(+)
> 
> diff --git a/tools/include/nolibc/time.h b/tools/include/nolibc/time.h
> index 28a1549adb14e2087fa8fbdb7e9c35e1c3f22c2a..760133c574ece97165e3bba5616a387deaf07aff 100644
> --- a/tools/include/nolibc/time.h
> +++ b/tools/include/nolibc/time.h
> @@ -105,6 +105,13 @@ int clock_settime(clockid_t clockid, struct timespec *tp)
>  }
>  
>  
> +static __inline__
> +double difftime(time_t time1, time_t time2)
> +{
> +	return time1 - time2;
> +}

Thanks for making me discover difftime(). I've never heard about it, and
seeing it return a double looks totally weird, but that's how it is indeed.

In case time_t would be unsigned, this would return a large unsigned
value. I think it could be more robust to explicitly cast the two
inputs to long:

	return (long)time1 - (long)time2;

> diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> index aed71de4b4f3dd1f183c7fc25e5a5cee466600ed..fd8bab42e75157967658690005bc9142360fc135 100644
> --- a/tools/testing/selftests/nolibc/nolibc-test.c
> +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> @@ -1423,6 +1423,7 @@ int run_stdlib(int min, int max)
>  		CASE_TEST(toupper_noop);            EXPECT_EQ(1, toupper('A'), 'A'); break;
>  		CASE_TEST(abs);                     EXPECT_EQ(1, abs(-10), 10); break;
>  		CASE_TEST(abs_noop);                EXPECT_EQ(1, abs(10), 10); break;
> +		CASE_TEST(difftime);                EXPECT_EQ(1, difftime(200., 100.), 100.); break;

Then here maybe test it in reverse to make sure the types don't cause trouble:

		CASE_TEST(difftime);                EXPECT_EQ(1, difftime(100., 200.), -100.); break;

Willy

  reply	other threads:[~2025-04-26 10:45 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-23 15:01 [PATCH 00/15] tools/nolibc: various new functions Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 01/15] tools/nolibc: add strstr() Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 02/15] tools/nolibc: add %m printf format Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 03/15] tools/nolibc: add more stat() variants Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 04/15] tools/nolibc: add mremap() Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 05/15] tools/nolibc: add getrandom() Thomas Weißschuh
2025-04-26 10:31   ` Willy Tarreau
2025-04-28 10:37     ` Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 06/15] tools/nolibc: add abs() and friends Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 07/15] tools/nolibc: add support for access() and faccessat() Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 08/15] tools/nolibc: add clock_getres(), clock_gettime() and clock_settime() Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 09/15] tools/nolibc: add timer functions Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 10/15] tools/nolibc: add timerfd functionality Thomas Weißschuh
2025-04-26 10:33   ` Willy Tarreau
2025-04-23 15:01 ` [PATCH 11/15] tools/nolibc: add difftime() Thomas Weißschuh
2025-04-26 10:45   ` Willy Tarreau [this message]
2025-04-28 10:41     ` Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 12/15] tools/nolibc: add namespace functionality Thomas Weißschuh
2025-04-26 10:48   ` Willy Tarreau
2025-04-23 15:01 ` [PATCH 13/15] tools/nolibc: add fopen() Thomas Weißschuh
2025-04-26 10:58   ` Willy Tarreau
2025-04-23 15:01 ` [PATCH 14/15] tools/nolibc: implement fall back to sys_clock_gettime() in gettimeofday() Thomas Weißschuh
2025-04-23 15:01 ` [PATCH 15/15] tools/nolibc: implement wait() in terms of waitpid() Thomas Weißschuh
2025-04-26 11:01 ` [PATCH 00/15] tools/nolibc: various new functions Willy Tarreau

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=20250426104520.GD17549@1wt.eu \
    --to=w@1wt.eu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=shuah@kernel.org \
    --cc=thomas.weissschuh@linutronix.de \
    /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