From patchwork Sat Sep 14 10:45:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 51580 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:9fc3:0:b0:48e:c0f8:d0de with SMTP id k3csp300305vqy; Sat, 14 Sep 2024 04:11:15 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVCRj6gqg3jZLT2DsKafL8vMLWhRRr8SZlz3vvdg7tLiUlxUtc9Px/7ZRcopQuyRPoe8hZd54MbgJkmTPK0Yuf/@gmail.com X-Google-Smtp-Source: AGHT+IHANZCGDkjONQ8lhvSqBQSJf3dYTTYfmC4SzfZ8ooDhjvkYT+yC2QqCAFPUrgv3uhp2f7m2 X-Received: by 2002:a2e:9017:0:b0:2f5:c13:bd11 with SMTP id 38308e7fff4ca-2f787dcdaf0mr42455691fa.17.1726312274833; Sat, 14 Sep 2024 04:11:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1726312274; cv=none; d=google.com; s=arc-20240605; b=WxaoPXpEcKtm2XUvUfkPvxY3K1s/a5U51A9NV2rc+COr7M0B+b9jnQeU1JtnPv12VT Ff8F3gwvUqhUGRxbnGdiQFayf6FyXuLGpaTMNfO4aNubl4YDEp5aJli0Y6XWA1SjHeJH xPAomoeMxhKR7AX93JcNm8kJAqfhBsNwj+H/6BFPNv1PJ+GhepYYDceUMPLy5TBMaGLF VUdKAcDvDmxwg4dWbhaIM5ScEqMkkdYyzfyvrX+JwAx8GlCB3l2lwQfE6+NAcDVYWDeY XWizs1/8ThLHJXsQoHHmp7nJg/rGJYB/daM2oiQ6yheRG+hz2wjQkin1tQjL2EEVvmfZ R5hQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=UL+ogkclR+GoO4z2ZDp2bY/ytaRA3FZf+pRYHQp+iDQ=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=lndXgFH7E5yYW6v9lvo4AmUji7/aY9n8e8hSWcLKW+e3mAekqWcaHUvdAL3A1fOAoB LYEkM5N323eA4kAgnQFORVLZ5hRJiZZMNrW3kL0Qei2cSOOGaPn6hpvHlh4sK0oF3jOJ AzvUnLvbP26HmQR7gd2VCjNvwXG0eVQCoeHovEfKLkghgYWSxnYTHrYe98siFPLzlZyA eA1N0/RbuZU1SvrHw0r7WlCLv4HmtMmYdYtp1kblT1XxQ7b+E3coFptcvuEITfn+NIAf v5CUb4O2qjGvqB6+LGApdWo47h55H609CKDfs9fhOUwyK2lgG/Ye3eQqFcKFL+dFZKkl j13w==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@khirnov.net header.s=mail header.b=qBVVgbNa; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 38308e7fff4ca-2f79d488dd8si3899051fa.604.2024.09.14.04.11.14; Sat, 14 Sep 2024 04:11:14 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@khirnov.net header.s=mail header.b=qBVVgbNa; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A296368DC6D; Sat, 14 Sep 2024 14:10:55 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail1.khirnov.net (quelana.khirnov.net [94.230.150.81]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0566668DA8C for ; Sat, 14 Sep 2024 14:10:47 +0300 (EEST) Authentication-Results: mail1.khirnov.net; dkim=pass (2048-bit key; unprotected) header.d=khirnov.net header.i=@khirnov.net header.a=rsa-sha256 header.s=mail header.b=qBVVgbNa; dkim-atps=neutral Received: from localhost (mail1.khirnov.net [IPv6:::1]) by mail1.khirnov.net (Postfix) with ESMTP id 4D3B44E19 for ; Sat, 14 Sep 2024 13:10:47 +0200 (CEST) Received: from mail1.khirnov.net ([IPv6:::1]) by localhost (mail1.khirnov.net [IPv6:::1]) (amavis, port 10024) with ESMTP id GfFXR51kmqOb for ; Sat, 14 Sep 2024 13:10:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=khirnov.net; s=mail; t=1726312246; bh=3WKBBwQj5Lf2Vmi+HMnv/gRohFCjNuRZQU2lzJmVMl8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=qBVVgbNaN7Uv0wa7QJkdwEIXjW2eFwu6pW4VpKQDocl+1VC8NbmJn88WLBD+qZWKk 7h0v1TXai2xh+ExSH2/DXkauUAb46632lkldHkmWmsrbBndJD/7FlMJsNaDgrLiDLy gJJNoyTDDSoTw8suxkZdj4GICQsL20SbUyTWsU0cczG9HcYGNCFWGY824ptSgbN9zM 7PV/VekFPITrklFFAFVtgysP1vH/0ph47W5R4Yc+fZ69nBV+3mfBfcUGlfCy+UWrbp rFB5MujzYS4CB/8bp3I99jojkLUylY0LxYhAkomQtl5g37rpPm8pv7IKMsq/v5AB+X 4F5zRP4er77bw== Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail1.khirnov.net (Postfix) with ESMTPS id 377314DE6 for ; Sat, 14 Sep 2024 13:10:46 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id E5F8D3A0998 for ; Sat, 14 Sep 2024 13:10:40 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sat, 14 Sep 2024 12:45:27 +0200 Message-ID: <20240914111036.17164-3-anton@khirnov.net> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240914111036.17164-1-anton@khirnov.net> References: <20240914111036.17164-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 02/23] compat: add a fallback implementation of C23 stdbit.h X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: CW16OtkQ+y3+ From: Rémi Denis-Courmont Header contents taken from VLC commit 7a970a33329c9836d169727ddbaf49a33240d587. Signed-off-by: Anton Khirnov --- compat/stdbit/stdbit.h | 594 +++++++++++++++++++++++++++++++++++++++++ configure | 4 + tests/ref/fate/source | 1 + 3 files changed, 599 insertions(+) create mode 100644 compat/stdbit/stdbit.h diff --git a/compat/stdbit/stdbit.h b/compat/stdbit/stdbit.h new file mode 100644 index 0000000000..b6a2c7e945 --- /dev/null +++ b/compat/stdbit/stdbit.h @@ -0,0 +1,594 @@ +/* + * Copyright (C) 2023 Rémi Denis-Courmont + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + */ + +#ifndef __STDC_VERSION_STDBIT_H__ +#define __STDC_VERSION_STDBIT_H__ 202311L + +#include +#include /* CHAR_BIT */ + +#define __STDC_ENDIAN_LITTLE__ 1234 +#define __STDC_ENDIAN_BIG__ 4321 + +#ifdef __BYTE_ORDER__ +# if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) +# define __STDC_ENDIAN_NATIVE__ __STDC_ENDIAN_LITTLE__ +# elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +# define __STDC_ENDIAN_NATIVE__ __STDC_ENDIAN_BIG__ +# else +# define __STDC_ENDIAN_NATIVE__ 3412 +# endif +#else +# error Not implemented. +#endif + +#define __stdbit_generic_type_func(func, value) \ + _Generic (value, \ + unsigned long long: stdc_##func##_ull((unsigned long long)(value)), \ + unsigned long: stdc_##func##_ul((unsigned long)(value)), \ + unsigned int: stdc_##func##_ui((unsigned int)(value)), \ + unsigned short: stdc_##func##_us((unsigned short)(value)), \ + unsigned char: stdc_##func##_uc((unsigned char)(value))) + +#if defined (__GNUC__) || defined (__clang__) +static inline unsigned int stdc_leading_zeros_ull(unsigned long long value) +{ + return value ? __builtin_clzll(value) : (CHAR_BIT * sizeof (value)); +} + +static inline unsigned int stdc_leading_zeros_ul(unsigned long value) +{ + return value ? __builtin_clzl(value) : (CHAR_BIT * sizeof (value)); +} + +static inline unsigned int stdc_leading_zeros_ui(unsigned int value) +{ + return value ? __builtin_clz(value) : (CHAR_BIT * sizeof (value)); +} + +static inline unsigned int stdc_leading_zeros_us(unsigned short value) +{ + return stdc_leading_zeros_ui(value) + - CHAR_BIT * (sizeof (int) - sizeof (value)); +} + +static inline unsigned int stdc_leading_zeros_uc(unsigned char value) +{ + return stdc_leading_zeros_ui(value) - (CHAR_BIT * (sizeof (int) - 1)); +} +#else +static inline unsigned int __stdc_leading_zeros(unsigned long long value, + unsigned int size) +{ + unsigned int zeros = size * CHAR_BIT; + + while (value != 0) { + value >>= 1; + zeros--; + } + + return zeros; +} + +static inline unsigned int stdc_leading_zeros_ull(unsigned long long value) +{ + return __stdc_leading_zeros(value, sizeof (value)); +} + +static inline unsigned int stdc_leading_zeros_ul(unsigned long value) +{ + return __stdc_leading_zeros(value, sizeof (value)); +} + +static inline unsigned int stdc_leading_zeros_ui(unsigned int value) +{ + return __stdc_leading_zeros(value, sizeof (value)); +} + +static inline unsigned int stdc_leading_zeros_us(unsigned short value) +{ + return __stdc_leading_zeros(value, sizeof (value)); +} + +static inline unsigned int stdc_leading_zeros_uc(unsigned char value) +{ + return __stdc_leading_zeros(value, sizeof (value)); +} +#endif + +#define stdc_leading_zeros(value) \ + __stdbit_generic_type_func(leading_zeros, value) + +static inline unsigned int stdc_leading_ones_ull(unsigned long long value) +{ + return stdc_leading_zeros_ull(~value); +} + +static inline unsigned int stdc_leading_ones_ul(unsigned long value) +{ + return stdc_leading_zeros_ul(~value); +} + +static inline unsigned int stdc_leading_ones_ui(unsigned int value) +{ + return stdc_leading_zeros_ui(~value); +} + +static inline unsigned int stdc_leading_ones_us(unsigned short value) +{ + return stdc_leading_zeros_us(~value); +} + +static inline unsigned int stdc_leading_ones_uc(unsigned char value) +{ + return stdc_leading_zeros_uc(~value); +} + +#define stdc_leading_ones(value) \ + __stdbit_generic_type_func(leading_ones, value) + +#if defined (__GNUC__) || defined (__clang__) +static inline unsigned int stdc_trailing_zeros_ull(unsigned long long value) +{ + return value ? (unsigned int)__builtin_ctzll(value) + : (CHAR_BIT * sizeof (value)); +} + +static inline unsigned int stdc_trailing_zeros_ul(unsigned long value) +{ + return value ? (unsigned int)__builtin_ctzl(value) + : (CHAR_BIT * sizeof (value)); +} + +static inline unsigned int stdc_trailing_zeros_ui(unsigned int value) +{ + return value ? (unsigned int)__builtin_ctz(value) + : (CHAR_BIT * sizeof (value)); +} + +static inline unsigned int stdc_trailing_zeros_us(unsigned short value) +{ + return value ? (unsigned int)__builtin_ctz(value) + : (CHAR_BIT * sizeof (value)); +} + +static inline unsigned int stdc_trailing_zeros_uc(unsigned char value) +{ + return value ? (unsigned int)__builtin_ctz(value) + : (CHAR_BIT * sizeof (value)); +} +#else +static inline unsigned int __stdc_trailing_zeros(unsigned long long value, + unsigned int size) +{ + unsigned int zeros = size * CHAR_BIT; + + while (value != 0) { + value <<= 1; + zeros--; + } + + return zeros; +} + +static inline unsigned int stdc_trailing_zeros_ull(unsigned long long value) +{ + return __stdc_trailing_zeros(value, sizeof (value)); +} + +static inline unsigned int stdc_trailing_zeros_ul(unsigned long value) +{ + return __stdc_trailing_zeros(value, sizeof (value)); +} + +static inline unsigned int stdc_trailing_zeros_ui(unsigned int value) +{ + return __stdc_trailing_zeros(value, sizeof (value)); +} + +static inline unsigned int stdc_trailing_zeros_us(unsigned short value) +{ + return __stdc_trailing_zeros(value, sizeof (value)); +} + +static inline unsigned int stdc_trailing_zeros_uc(unsigned char value) +{ + return __stdc_trailing_zeros(value, sizeof (value)); +} +#endif + +#define stdc_trailing_zeros(value) \ + __stdbit_generic_type_func(trailing_zeros, value) + +static inline unsigned int stdc_trailing_ones_ull(unsigned long long value) +{ + return stdc_trailing_zeros_ull(~value); +} + +static inline unsigned int stdc_trailing_ones_ul(unsigned long value) +{ + return stdc_trailing_zeros_ul(~value); +} + +static inline unsigned int stdc_trailing_ones_ui(unsigned int value) +{ + return stdc_trailing_zeros_ui(~value); +} + +static inline unsigned int stdc_trailing_ones_us(unsigned short value) +{ + return stdc_trailing_zeros_us(~value); +} + +static inline unsigned int stdc_trailing_ones_uc(unsigned char value) +{ + return stdc_trailing_zeros_uc(~value); +} + +#define stdc_trailing_ones(value) \ + __stdbit_generic_type_func(trailing_ones, value) + +static inline unsigned int stdc_first_leading_one_ull(unsigned long long value) +{ + return value ? (stdc_leading_zeros_ull(value) + 1) : 0; +} + +static inline unsigned int stdc_first_leading_one_ul(unsigned long value) +{ + return value ? (stdc_leading_zeros_ul(value) + 1) : 0; +} + +static inline unsigned int stdc_first_leading_one_ui(unsigned int value) +{ + return value ? (stdc_leading_zeros_ui(value) + 1) : 0; +} + +static inline unsigned int stdc_first_leading_one_us(unsigned short value) +{ + return value ? (stdc_leading_zeros_us(value) + 1) : 0; +} + +static inline unsigned int stdc_first_leading_one_uc(unsigned char value) +{ + return value ? (stdc_leading_zeros_uc(value) + 1) : 0; +} + +#define stdc_first_leading_one(value) \ + __stdbit_generic_type_func(first_leading_one, value) + +static inline unsigned int stdc_first_leading_zero_ull(unsigned long long value) +{ + return stdc_leading_ones_ull(~value); +} + +static inline unsigned int stdc_first_leading_zero_ul(unsigned long value) +{ + return stdc_leading_ones_ul(~value); +} + +static inline unsigned int stdc_first_leading_zero_ui(unsigned int value) +{ + return stdc_leading_ones_ui(~value); +} + +static inline unsigned int stdc_first_leading_zero_us(unsigned short value) +{ + return stdc_leading_ones_us(~value); +} + +static inline unsigned int stdc_first_leading_zero_uc(unsigned char value) +{ + return stdc_leading_ones_uc(~value); +} + +#define stdc_first_leading_zero(value) \ + __stdbit_generic_type_func(first_leading_zero, value) + +#if defined (__GNUC__) || defined (__clang__) +static inline unsigned int stdc_first_trailing_one_ull(unsigned long long value) +{ + return __builtin_ffsll(value); +} + +static inline unsigned int stdc_first_trailing_one_ul(unsigned long value) +{ + return __builtin_ffsl(value); +} + +static inline unsigned int stdc_first_trailing_one_ui(unsigned int value) +{ + return __builtin_ffs(value); +} + +static inline unsigned int stdc_first_trailing_one_us(unsigned short value) +{ + return __builtin_ffs(value); +} + +static inline unsigned int stdc_first_trailing_one_uc(unsigned char value) +{ + return __builtin_ffs(value); +} +#else +static inline unsigned int stdc_first_trailing_one_ull(unsigned long long value) +{ + return value ? (1 + stdc_trailing_zeros_ull(value)) : 0; +} + +static inline unsigned int stdc_first_trailing_one_ul(unsigned long value) +{ + return value ? (1 + stdc_trailing_zeros_ul(value)) : 0; +} + +static inline unsigned int stdc_first_trailing_one_ui(unsigned int value) +{ + return value ? (1 + stdc_trailing_zeros_ui(value)) : 0; +} + +static inline unsigned int stdc_first_trailing_one_us(unsigned short value) +{ + return value ? (1 + stdc_trailing_zeros_us(value)) : 0; +} + +static inline unsigned int stdc_first_trailing_one_uc(unsigned char value) +{ + return value ? (1 + stdc_trailing_zeros_uc(value)) : 0; +} +#endif + +#define stdc_first_trailing_one(value) \ + __stdbit_generic_type_func(first_trailing_one, value) + +static inline unsigned int stdc_first_trailing_zero_ull(unsigned long long value) +{ + return stdc_first_trailing_one_ull(~value); +} + +static inline unsigned int stdc_first_trailing_zero_ul(unsigned long value) +{ + return stdc_first_trailing_one_ul(~value); +} + +static inline unsigned int stdc_first_trailing_zero_ui(unsigned int value) +{ + return stdc_first_trailing_one_ui(~value); +} + +static inline unsigned int stdc_first_trailing_zero_us(unsigned short value) +{ + return stdc_first_trailing_one_us(~value); +} + +static inline unsigned int stdc_first_trailing_zero_uc(unsigned char value) +{ + return stdc_first_trailing_one_uc(~value); +} + +#define stdc_first_trailing_zero(value) \ + __stdbit_generic_type_func(first_trailing_zero, value) + +#if defined (__GNUC__) || defined (__clang__) +static inline unsigned int stdc_count_ones_ull(unsigned long long value) +{ + return __builtin_popcountll(value); +} + +static inline unsigned int stdc_count_ones_ul(unsigned long value) +{ + return __builtin_popcountl(value); +} + +static inline unsigned int stdc_count_ones_ui(unsigned int value) +{ + return __builtin_popcount(value); +} + +static inline unsigned int stdc_count_ones_us(unsigned short value) +{ + return __builtin_popcount(value); +} + +static inline unsigned int stdc_count_ones_uc(unsigned char value) +{ + return __builtin_popcount(value); +} +#else +static inline unsigned int __stdc_count_ones(unsigned long long value, + unsigned int size) +{ + unsigned int ones = 0; + + for (unsigned int c = 0; c < (size * CHAR_BIT); c++) { + ones += value & 1; + value >>= 1; + } + + return ones; +} + +static inline unsigned int stdc_count_ones_ull(unsigned long long value) +{ + return __stdc_count_ones(value, sizeof (value)); +} + +static inline unsigned int stdc_count_ones_ul(unsigned long value) +{ + return __stdc_count_ones(value, sizeof (value)); +} + +static inline unsigned int stdc_count_ones_ui(unsigned int value) +{ + return __stdc_count_ones(value, sizeof (value)); +} + +static inline unsigned int stdc_count_ones_us(unsigned short value) +{ + return __stdc_count_ones(value, sizeof (value)); +} + +static inline unsigned int stdc_count_ones_uc(unsigned char value) +{ + return __stdc_count_ones(value, sizeof (value)); +} +#endif + +#define stdc_count_ones(value) \ + __stdbit_generic_type_func(count_ones, value) + +static inline unsigned int stdc_count_zeros_ull(unsigned long long value) +{ + return stdc_count_ones_ull(~value); +} + +static inline unsigned int stdc_count_zeros_ul(unsigned long value) +{ + return stdc_count_ones_ul(~value); +} + +static inline unsigned int stdc_count_zeros_ui(unsigned int value) +{ + return stdc_count_ones_ui(~value); +} + +static inline unsigned int stdc_count_zeros_us(unsigned short value) +{ + return stdc_count_ones_us(~value); +} + +static inline unsigned int stdc_count_zeros_uc(unsigned char value) +{ + return stdc_count_ones_uc(~value); +} + +#define stdc_count_zeros(value) \ + __stdbit_generic_type_func(count_zeros, value) + +static inline bool stdc_has_single_bit_ull(unsigned long long value) +{ + return value && (value & (value - 1)) == 0; +} + +static inline bool stdc_has_single_bit_ul(unsigned long value) +{ + return value && (value & (value - 1)) == 0; +} + +static inline bool stdc_has_single_bit_ui(unsigned int value) +{ + return value && (value & (value - 1)) == 0; +} + +static inline bool stdc_has_single_bit_us(unsigned short value) +{ + return value && (value & (value - 1)) == 0; +} + +static inline bool stdc_has_single_bit_uc(unsigned char value) +{ + return value && (value & (value - 1)) == 0; +} + +#define stdc_has_single_bit(value) \ + __stdbit_generic_type_func(has_single_bit, value) + +static inline unsigned int stdc_bit_width_ull(unsigned long long value) +{ + return (CHAR_BIT * sizeof (value)) - stdc_leading_zeros_ull(value); +} + +static inline unsigned int stdc_bit_width_ul(unsigned long value) +{ + return (CHAR_BIT * sizeof (value)) - stdc_leading_zeros_ul(value); +} + +static inline unsigned int stdc_bit_width_ui(unsigned int value) +{ + return (CHAR_BIT * sizeof (value)) - stdc_leading_zeros_ui(value); +} + +static inline unsigned int stdc_bit_width_us(unsigned short value) +{ + return (CHAR_BIT * sizeof (value)) - stdc_leading_zeros_us(value); +} + +static inline unsigned int stdc_bit_width_uc(unsigned char value) +{ + return (CHAR_BIT * sizeof (value)) - stdc_leading_zeros_uc(value); +} + +#define stdc_bit_width(value) \ + __stdbit_generic_type_func(bit_width, value) + +static inline unsigned long long stdc_bit_floor_ull(unsigned long long value) +{ + return value ? (1ULL << (stdc_bit_width_ull(value) - 1)) : 0ULL; +} + +static inline unsigned long stdc_bit_floor_ul(unsigned long value) +{ + return value ? (1UL << (stdc_bit_width_ul(value) - 1)) : 0UL; +} + +static inline unsigned int stdc_bit_floor_ui(unsigned int value) +{ + return value ? (1U << (stdc_bit_width_ui(value) - 1)) : 0U; +} + +static inline unsigned short stdc_bit_floor_us(unsigned short value) +{ + return value ? (1U << (stdc_bit_width_us(value) - 1)) : 0U; +} + +static inline unsigned int stdc_bit_floor_uc(unsigned char value) +{ + return value ? (1U << (stdc_bit_width_uc(value) - 1)) : 0U; +} + +#define stdc_bit_floor(value) \ + __stdbit_generic_type_func(bit_floor, value) + +/* NOTE: Bit ceiling undefines overflow. */ +static inline unsigned long long stdc_bit_ceil_ull(unsigned long long value) +{ + return 1ULL << (value ? stdc_bit_width_ull(value - 1) : 0); +} + +static inline unsigned long stdc_bit_ceil_ul(unsigned long value) +{ + return 1UL << (value ? stdc_bit_width_ul(value - 1) : 0); +} + +static inline unsigned int stdc_bit_ceil_ui(unsigned int value) +{ + return 1U << (value ? stdc_bit_width_ui(value - 1) : 0); +} + +static inline unsigned short stdc_bit_ceil_us(unsigned short value) +{ + return 1U << (value ? stdc_bit_width_us(value - 1) : 0); +} + +static inline unsigned int stdc_bit_ceil_uc(unsigned char value) +{ + return 1U << (value ? stdc_bit_width_uc(value - 1) : 0); +} + +#define stdc_bit_ceil(value) \ + __stdbit_generic_type_func(bit_ceil, value) + +#endif /* __STDC_VERSION_STDBIT_H__ */ diff --git a/configure b/configure index 337172a6c6..83c0d02f8c 100755 --- a/configure +++ b/configure @@ -7766,6 +7766,10 @@ if disabled stdatomic; then fi fi +check_builtin stdbit "stdbit.h assert.h" \ + 'static_assert(__STDC_VERSION_STDBIT_H__ >= 202311L, "Compiler lacks stdbit.h")' || \ + add_cppflags '-I\$(SRC_PATH)/compat/stdbit' + # Check if requested libraries were found. for lib in $AUTODETECT_LIBS; do requested $lib && ! enabled $lib && die "ERROR: $lib requested but not found"; diff --git a/tests/ref/fate/source b/tests/ref/fate/source index 1703b36c02..78d3a2e0fa 100644 --- a/tests/ref/fate/source +++ b/tests/ref/fate/source @@ -24,6 +24,7 @@ Headers without standard inclusion guards: compat/djgpp/math.h compat/float/float.h compat/float/limits.h +compat/stdbit/stdbit.h libavcodec/bitstream_template.h tools/decode_simple.h Use of av_clip() where av_clip_uintp2() could be used: