[FFmpeg-devel] [PATCH v2 1/2] random_seed: Reorder if clauses for gathering entropy

Michael Niedermayer michael at niedermayer.cc
Thu Feb 6 04:08:36 EET 2025


Hi

On Thu, Feb 06, 2025 at 12:18:08AM +0200, Martin Storsjö wrote:
> Make it easier to add more cases.
> 
> This should be a pure refactoring, with no functional changes.
> ---
>  libavutil/random_seed.c | 19 +++++++++++--------
>  1 file changed, 11 insertions(+), 8 deletions(-)
> 
> diff --git a/libavutil/random_seed.c b/libavutil/random_seed.c
> index 8a4e4f1fc0..ca084b40da 100644
> --- a/libavutil/random_seed.c
> +++ b/libavutil/random_seed.c
> @@ -98,17 +98,20 @@ static uint32_t get_generic_seed(void)
>  
>      for (;;) {
>          clock_t t = clock();
> -        if (last_t + 2*last_td + (CLOCKS_PER_SEC > 1000) >= t) {
> -            last_td = t - last_t;
> -            buffer[i & 511] = 1664525*buffer[i & 511] + 1013904223 + (last_td % 3294638521U);
> +        int incremented_i = 0;
> +        int cur_td = t - last_t;
> +        if (last_t + 2*last_td + (CLOCKS_PER_SEC > 1000) < t) {
> +            buffer[++i & 511] += cur_td % 3294638521U;
> +            incremented_i = 1;
>          } else {
> -            last_td = t - last_t;
> -            buffer[++i & 511] += last_td % 3294638521U;
> -            if ((t - init_t) >= CLOCKS_PER_SEC>>5)
> -                if (last_i && i - last_i > 4 || i - last_i > 64 || TEST && i - last_i > 8)
> -                    break;
> +            buffer[i & 511] = 1664525*buffer[i & 511] + 1013904223 + (cur_td % 3294638521U);
> +        }
> +        if (incremented_i && (t - init_t) >= CLOCKS_PER_SEC>>5) {
> +            if (last_i && i - last_i > 4 || i - last_i > 64 || TEST && i - last_i > 8)
> +                break;
>          }
>          last_t = t;
> +        last_td = cur_td;
>          if (!init_t)
>              init_t = t;
>      }

sure, if you prefer / if it makes chanegs easier

thx

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

I am the wisest man alive, for I know one thing, and that is that I know
nothing. -- Socrates
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 195 bytes
Desc: not available
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20250206/88dfaa60/attachment.sig>


More information about the ffmpeg-devel mailing list