Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: "Rémi Denis-Courmont" <remi@remlab.net>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Subject: Re: [FFmpeg-devel] [PATCH 4/5] lavu/intmath.h: Fix UB in ff_ctz_c() and ff_ctzll_c()
Date: Fri, 31 May 2024 08:48:21 +0300
Message-ID: <4CC644FF-BBE1-47DC-87DA-435BF7FE1FB8@remlab.net> (raw)
In-Reply-To: <20240531004140.GL2821752@pb2>



Le 31 mai 2024 03:41:40 GMT+03:00, Michael Niedermayer <michael@niedermayer.cc> a écrit :
>On Thu, May 30, 2024 at 10:54:46AM +0300, Rémi Denis-Courmont wrote:
>> Can't we just use the compiler built-ins here? AFAIK, they (GCC, LLVM) use the same algorithm if the CPU doesn't support native CTZ. And they will pick the right instruction if CPU does have CTZ.
>> 
>> I get it that maybe it wasn't working so well 20 years ago, but we've increased compiler version requirements since then.
>
>ffmpeg is written in C not GNU-C nor LLVM-C
>so we need to have non buggy C code

What does that mean here?

We can put compilers in 3 sets:
1) GCC+Clang
2) MSVC+ICC
3) others

Note that ICC and others are *not* tested (at least in FATE). MSVC and ICC are using intrinsics (in x86/intmath.h).

The problem is, GCC and Clang use intrinsics only if fast_clz is manually selected. This is silly. That's just requiring more platform-specific code added for no reasons.

If you want to test this code, DO write a test that calls the C version always. DO NOT force all unknown or unoptimised FFmpeg platforms to use the C just because we need to keep supporting hypothetical C11-conforming C compilers.

Note that I never said that we should remove the standard code.

>A modern compiler should turn a built-in into a efficient piece of code
>but so should it recognize that efficient piece of code and turn it into
>a single instruction if such exist.

I doubt any compiler will detect that the Debujin algorithm can be replaced by a CTZ.  The *official* standard solution is to use <stdbit.h> (C23-only unfortunately), so there are no reasons why compilers should even care to deal with this by now.

>In the end with a modern compiler it shouldnt matter how you write this
>a loop, some optimized standard implementation or a built in
>the disavantage of a builtin is that it requires #ifs and checks
>for each compiler.

A modern compiler supports STDC bit-wise functions from C23, and therefore doesn't care about this.

>So we should go with the simplest/cleanest that reliably produces optimal code
>across all supported platforms (and also consider potential future platforms
>so we dont have a maintaince nightmare but something that we can write once
>and expect it will be close to optimal forever)

So what do you do about the existing x86 special cases and the fast_clz case?

AFAICT, the only way to (arguably) do that is to switch to the C23 bit manipulation functions, and provide fallback for old compilers.

That's clearly out of the scope of this patch, and I bet some people would object anyway because they'll find the names of the new functions ugly and/or too long.
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

  reply	other threads:[~2024-05-31  5:48 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-29 22:13 [FFmpeg-devel] [PATCH 1/5] lavu/common.h: Fix UB in av_clipl_int32_c() Tomas Härdin
2024-05-29 22:13 ` [FFmpeg-devel] [PATCH 2/5] lavu/common.h: Fix UB in av_clip_intp2_c() Tomas Härdin
2024-05-29 22:24   ` Andreas Rheinhardt
2024-05-29 23:08     ` Tomas Härdin
2024-05-29 22:14 ` [FFmpeg-devel] [PATCH 3/5] lavu/common.h: Fix UB in av_clip_uintp2_c() Tomas Härdin
2024-05-31  0:27   ` Michael Niedermayer
2024-05-29 22:14 ` [FFmpeg-devel] [PATCH 4/5] lavu/intmath.h: Fix UB in ff_ctz_c() and ff_ctzll_c() Tomas Härdin
2024-05-30  7:54   ` Rémi Denis-Courmont
2024-05-30  9:50     ` Tomas Härdin
2024-05-30 12:29       ` Hendrik Leppkes
2024-05-30 13:06       ` Rémi Denis-Courmont
2024-05-30 14:03         ` Tomas Härdin
2024-05-30 14:32           ` Rémi Denis-Courmont
2024-05-31  0:43           ` Ronald S. Bultje
2024-05-31  0:41     ` Michael Niedermayer
2024-05-31  5:48       ` Rémi Denis-Courmont [this message]
2024-05-31  0:31   ` Michael Niedermayer
2024-05-29 22:15 ` [FFmpeg-devel] [PATCH 5/5] lavu/mathematics: Return early if either a or b is zero Tomas Härdin
2024-05-31  0:22   ` Michael Niedermayer
2024-05-31 15:21     ` Tomas Härdin
2024-05-29 22:31 ` [FFmpeg-devel] [PATCH 1/5] lavu/common.h: Fix UB in av_clipl_int32_c() Andreas Rheinhardt
2024-05-30  9:48   ` Tomas Härdin
2024-05-30  6:41 ` Rémi Denis-Courmont
2024-05-30  9:40   ` Tomas Härdin
2024-05-30 11:50     ` Rémi Denis-Courmont
2024-05-30 14:07       ` Tomas Härdin
2024-05-30 14:28         ` Rémi Denis-Courmont
2024-05-30 15:32           ` Tomas Härdin
2024-05-30 15:38             ` Rémi Denis-Courmont
2024-05-31  1:03               ` Michael Niedermayer
2024-06-03  7:32                 ` Rémi Denis-Courmont
2024-06-03 21:21                   ` Michael Niedermayer
2024-05-30 15:42             ` James Almer
2024-05-30 16:48               ` Tomas Härdin
2024-05-30 17:49                 ` Rémi Denis-Courmont
2024-05-30 19:07                   ` Michael Niedermayer
2024-05-30 19:20                     ` Rémi Denis-Courmont
2024-05-31 15:23                   ` Tomas Härdin
2024-06-14 12:31 ` Tomas Härdin

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=4CC644FF-BBE1-47DC-87DA-435BF7FE1FB8@remlab.net \
    --to=remi@remlab.net \
    --cc=ffmpeg-devel@ffmpeg.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

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git