From patchwork Sat Sep 14 14:56:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 51603 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:9fc3:0:b0:48e:c0f8:d0de with SMTP id k3csp395254vqy; Sat, 14 Sep 2024 07:57:32 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUbPLI4JaB6kAAKd/kqpCxim0NDlWG4AmfOsUj2A8nOApJ+u8+m8LB4yLFO03K5HUPGyNUymgruxn9p6/y+swRx@gmail.com X-Google-Smtp-Source: AGHT+IEZjwpgZtyh87mz6GAvk66RyN2r9t+aQHic9DxPVVKCG4NEvIfFMdpY65CxbOs6gtKwLK+j X-Received: by 2002:a17:907:3eaa:b0:a8d:4410:3bd6 with SMTP id a640c23a62f3a-a902946b6e2mr462784266b.4.1726325852221; Sat, 14 Sep 2024 07:57:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1726325852; cv=none; d=google.com; s=arc-20240605; b=cPqBEVc87uy6Vinb08pWFIVmS0IMp3XMStEzYd35mXRQhd7OMYw/h+6suFgfo7mclc uuTif0wMe77j9JqloEG0AFe7dXZjn8PrQclpznBqD3FBWbW4tYhlNcsrmgwbivzQHAVP Aq+SFN8+VdyYNSZM+U2H4tHTfpBgknoOk0DG1fqdWzGQoti48GVAw82aZDTs9+3Htyh9 coiPsRiGvpvPAIKQrWbo5DpXtdNaxmNel/sjDXek3T6uStb6Nfo+YaawR3rzqLqBkazk rgakQm6pfgKo0A8QIH+Gt4lXcfAejuh5JcohUEuhx/X1vJedel0Xim5LhICk0YBMYN1p sjqg== 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=byFr9C9aaxFve8m4ghW034gfLFAE+Aynvcns4/qnbjM=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=S1N9jI1d7Co8QyS0nxpZN8OVUy8ZEsWyxFJu1mSH08ughfSARsJbXfPrMw5i0gcRAD PZS86jfVpD6w24QrPg+jiRfMmcgJZmXrlJw0o1M2vi4ikW+NAp/0G0euXhVB/5GS3Rim Pm85U0QShVM6jg0mn+JmPxmafG+Sy5sZGf6kHzQ5b7NE/U6jjH59NjbBVKdqafPz9bZR hk4jLn1W4WVSESv/P6NPasYe1ITAvaj6wBe4Rea929ozCG/pbIkWd1C8hst5wRIXFDv+ dN37SDeQgCjT70mZ/oyibX/fR3E283p5pMo/shaipfkw9VA9XaWbXSlQUHzOdOx8uIvx 7RQQ==; 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=LNX6MI7C; 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 a640c23a62f3a-a9061372348si105480966b.966.2024.09.14.07.57.31; Sat, 14 Sep 2024 07:57:32 -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=LNX6MI7C; 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 663E968DE44; Sat, 14 Sep 2024 17:57:28 +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 6D02568DDEC for ; Sat, 14 Sep 2024 17:57:21 +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=LNX6MI7C; dkim-atps=neutral Received: from localhost (mail1.khirnov.net [IPv6:::1]) by mail1.khirnov.net (Postfix) with ESMTP id 8C7594DE4 for ; Sat, 14 Sep 2024 16:57:20 +0200 (CEST) Received: from mail1.khirnov.net ([IPv6:::1]) by localhost (mail1.khirnov.net [IPv6:::1]) (amavis, port 10024) with ESMTP id LCmJ0lyVCd-l for ; Sat, 14 Sep 2024 16:57:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=khirnov.net; s=mail; t=1726325839; bh=BPL0vNXbo4ytRxCcBynQap9qR+I/905BczHnByH6nMQ=; h=From:To:Subject:Date:In-Reply-To:References:From; b=LNX6MI7C7eeJ9XsW9S+PQmybYvzWMccAZ8Hw1f6rXhHj9K1Ioq62OhapdC4W+up5M 46ZEdc/KSBFqJTfdNzbGVrp6T82Rzw6193mayavUYSV8HbC9DqTXt88Y3FdCKirvra W65566W8oHpcJru4hwMcMm14RDcjS3AANSHHq1BXuefkw8mz2rTvmVN8H55DZs/guC 2QpzDd6OWKMfwVEXDCJffOP2gpwLMGShfToGc5YMBCeSLUfncF0AiOv8lsbqKGaA1Z lKfFnSori+bsr6BpoD/ryuKhsRase5bmmrtBvwdnjoNeOjifeG2do+WwIjOna5HuYG pvP0ks2Xf8J0A== 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 8F8A64DDE for ; Sat, 14 Sep 2024 16:57:19 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 68EEF3A036E for ; Sat, 14 Sep 2024 16:57:19 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sat, 14 Sep 2024 16:56:58 +0200 Message-ID: <20240914145711.21340-1-anton@khirnov.net> X-Mailer: git-send-email 2.43.0 In-Reply-To: <646cc1ea-dcad-4609-a473-09093b11dec2@gmail.com> References: <646cc1ea-dcad-4609-a473-09093b11dec2@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 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: OrLY6agNg23L From: Rémi Denis-Courmont Header contents taken from VLC commit 7a970a33329c9836d169727ddbaf49a33240d587. Signed-off-by: Anton Khirnov --- compat/stdbit/stdbit.h | 596 +++++++++++++++++++++++++++++++++++++++++ configure | 4 + tests/ref/fate/source | 1 + 3 files changed, 601 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..b434fc2357 --- /dev/null +++ b/compat/stdbit/stdbit.h @@ -0,0 +1,596 @@ +/* + * 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 +#elif defined(_MSC_VER) +# define __STDC_ENDIAN_NATIVE__ __STDC_ENDIAN_LITTLE__ +#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: