mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] selftests: timers: use 'llabs()' over 'abs()'
@ 2021-11-05 16:34 Anders Roxell
  2021-11-05 20:44 ` Nick Desaulniers
  0 siblings, 1 reply; 3+ messages in thread
From: Anders Roxell @ 2021-11-05 16:34 UTC (permalink / raw)
  To: shuah, tglx, john.stultz
  Cc: nathan, ndesaulniers, linux-kernel, linux-kselftest, llvm, Anders Roxell

When building selftests/timers with clang, the compiler warn about the
function abs() see below:

posix_timers.c:69:6: warning: absolute value function 'abs' given an argument of type 'long long' but has parameter of type 'int' which may cause truncation of value [-Wabsolute-value]
        if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
            ^
posix_timers.c:69:6: note: use function 'llabs' instead
        if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
            ^~~
            llabs

The note indicates what to do, Rework to use the function 'llabs()'.

Signed-off-by: Anders Roxell <anders.roxell@linaro.org>
---
 tools/testing/selftests/timers/posix_timers.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/timers/posix_timers.c b/tools/testing/selftests/timers/posix_timers.c
index 337424c5c987..73fb27901a1d 100644
--- a/tools/testing/selftests/timers/posix_timers.c
+++ b/tools/testing/selftests/timers/posix_timers.c
@@ -66,7 +66,7 @@ static int check_diff(struct timeval start, struct timeval end)
 	diff = end.tv_usec - start.tv_usec;
 	diff += (end.tv_sec - start.tv_sec) * USECS_PER_SEC;
 
-	if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
+	if (llabs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
 		printf("Diff too high: %lld..", diff);
 		return -1;
 	}
-- 
2.33.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] selftests: timers: use 'llabs()' over 'abs()'
  2021-11-05 16:34 [PATCH] selftests: timers: use 'llabs()' over 'abs()' Anders Roxell
@ 2021-11-05 20:44 ` Nick Desaulniers
  2021-11-20  0:07   ` Shuah Khan
  0 siblings, 1 reply; 3+ messages in thread
From: Nick Desaulniers @ 2021-11-05 20:44 UTC (permalink / raw)
  To: Anders Roxell
  Cc: shuah, tglx, john.stultz, nathan, linux-kernel, linux-kselftest, llvm

On Fri, Nov 5, 2021 at 9:34 AM Anders Roxell <anders.roxell@linaro.org> wrote:
>
> When building selftests/timers with clang, the compiler warn about the
> function abs() see below:
>
> posix_timers.c:69:6: warning: absolute value function 'abs' given an argument of type 'long long' but has parameter of type 'int' which may cause truncation of value [-Wabsolute-value]
>         if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
>             ^
> posix_timers.c:69:6: note: use function 'llabs' instead
>         if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
>             ^~~
>             llabs
>
> The note indicates what to do, Rework to use the function 'llabs()'.
>
> Signed-off-by: Anders Roxell <anders.roxell@linaro.org>

Thanks for the patch!
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

I wonder why tools/testing/selftests/timers/adjtick.c redefines llabs
when it already includes stdlib.h, and how that doesn't result in some
kind of compiler diagnostic.

> ---
>  tools/testing/selftests/timers/posix_timers.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/timers/posix_timers.c b/tools/testing/selftests/timers/posix_timers.c
> index 337424c5c987..73fb27901a1d 100644
> --- a/tools/testing/selftests/timers/posix_timers.c
> +++ b/tools/testing/selftests/timers/posix_timers.c
> @@ -66,7 +66,7 @@ static int check_diff(struct timeval start, struct timeval end)
>         diff = end.tv_usec - start.tv_usec;
>         diff += (end.tv_sec - start.tv_sec) * USECS_PER_SEC;
>
> -       if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
> +       if (llabs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
>                 printf("Diff too high: %lld..", diff);
>                 return -1;
>         }
> --
> 2.33.0
>


-- 
Thanks,
~Nick Desaulniers

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] selftests: timers: use 'llabs()' over 'abs()'
  2021-11-05 20:44 ` Nick Desaulniers
@ 2021-11-20  0:07   ` Shuah Khan
  0 siblings, 0 replies; 3+ messages in thread
From: Shuah Khan @ 2021-11-20  0:07 UTC (permalink / raw)
  To: Nick Desaulniers, Anders Roxell
  Cc: shuah, tglx, john.stultz, nathan, linux-kernel, linux-kselftest,
	llvm, Shuah Khan

On 11/5/21 2:44 PM, Nick Desaulniers wrote:
> On Fri, Nov 5, 2021 at 9:34 AM Anders Roxell <anders.roxell@linaro.org> wrote:
>>
>> When building selftests/timers with clang, the compiler warn about the
>> function abs() see below:
>>
>> posix_timers.c:69:6: warning: absolute value function 'abs' given an argument of type 'long long' but has parameter of type 'int' which may cause truncation of value [-Wabsolute-value]
>>          if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
>>              ^
>> posix_timers.c:69:6: note: use function 'llabs' instead
>>          if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
>>              ^~~
>>              llabs
>>
>> The note indicates what to do, Rework to use the function 'llabs()'.
>>
>> Signed-off-by: Anders Roxell <anders.roxell@linaro.org>
> 
> Thanks for the patch!
> Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
> 
> I wonder why tools/testing/selftests/timers/adjtick.c redefines llabs
> when it already includes stdlib.h, and how that doesn't result in some
> kind of compiler diagnostic.
> 

Hmm. The define in /usr/include/stdlib.h is under #ifdef __USE_ISOC99
Maybe be that explains the reason for the definitions in the other two
timers file?

Anders, I would like to understand this before I take this patch. I see
you sent several patches to other tests as well. Would be useful at
least understand why these defines exist.

thanks,
-- Shuah



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-11-20  0:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-05 16:34 [PATCH] selftests: timers: use 'llabs()' over 'abs()' Anders Roxell
2021-11-05 20:44 ` Nick Desaulniers
2021-11-20  0:07   ` Shuah Khan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®