From patchwork Mon Apr 20 15:24:21 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 133399 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id B014E4C900D8 for ; Mon, 20 Apr 2026 15:25:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B014E4C900D8 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Z+rTJFUc X-Original-To: newlib@sourceware.org Delivered-To: newlib@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 787A94B35884 for ; Mon, 20 Apr 2026 15:25:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 787A94B35884 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 787A94B35884 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776698709; cv=none; b=gOYzQNdu95PvSNFNfXd2GKHjVBjDBrKLQyHCtoYxrtRV8B3dIkCz97WT3c3p2UNb7RT++1yk2i1tNYtq6yQ+fOmreCL/b65wv27VGEbSQVgjpeVloKv40FBs18j5qUkkNOw1YIpBpFgHtZJWs5unakP3H6ecUYho5dsevEsqKSY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776698709; c=relaxed/simple; bh=R0kQqNp+J/APkr79f6cBt0gpH7eZ8JdUmV5ImEA46Ko=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=f8ihrA64+uTfopipqrkEnwvCHyTM626fpmAMCjedyWpCdIy+dAsXMHUXcyQgZ+y4OU8BhR1cBjSMs2/+LrF9vLVVkqFWNdI5Q2T+m+6/SuHfhthA3LeKLPNQoFbX1MTXY78wYv4l6HvNKUVwpHGHDTazX0vod1r5pChPE8AUbWA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 787A94B35884 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1776698709; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=aWilTD7v3At+JsI88WHDuQ7dPGhHZx9ZlGuuLT3IacQ=; b=Z+rTJFUcNlcP8kDaxDCWp5RYtc0TqWkepI4JIWHggSThKOml1jWeq7mRgGnGd459rsSZag 8advc+MKESdJ9GZTeMqUscyF65y4xzRpJgtRDzjFNZs0zk608ZFIfGHCyyddmxuisV2etT hkwyen2e09jEp+y1FgTCXX6PsRsli04= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-537-lVo6CX9mNqeDECAPbYu06A-1; Mon, 20 Apr 2026 11:25:07 -0400 X-MC-Unique: lVo6CX9mNqeDECAPbYu06A-1 X-Mimecast-MFC-AGG-ID: lVo6CX9mNqeDECAPbYu06A_1776698706 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 66288195605F for ; Mon, 20 Apr 2026 15:25:06 +0000 (UTC) Received: from zen.kayari.org (unknown [10.44.50.46]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A05F61800352 for ; Mon, 20 Apr 2026 15:25:05 +0000 (UTC) From: Jonathan Wakely To: newlib@sourceware.org Subject: [PATCH] newlib/libc/stdbit: Fix static assertions for 16-bit int Date: Mon, 20 Apr 2026 16:24:21 +0100 Message-ID: <20260420152504.827904-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: cxxMQoPbvx_fXTv8NFhmCgBXT09LCAUUOCA3eQeh_4o_1776698706 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Newlib mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: newlib-bounces~patchwork=sourceware.org@sourceware.org All the USHRT_WIDTH < UINT_WIDTH assertions fail for 16-bit int targets, such as msp430-elf. Remove those static assertions and instead fix the stdc_xxx_us functions to work correctly when USHRT_WIDTH == UINT_WIDTH. Signed-off-by: Jonathan Wakely --- This is untested, except to check that it now compiles on msp430-elf and still compiles on arm-eabi. newlib/libc/stdbit/stdc_bit_ceil.c | 9 +++++---- newlib/libc/stdbit/stdc_leading_ones.c | 10 ++++++---- newlib/libc/stdbit/stdc_leading_zeros.c | 10 ++++++---- newlib/libc/stdbit/stdc_trailing_ones.c | 10 ++++++---- newlib/libc/stdbit/stdc_trailing_zeros.c | 11 +++++++---- 5 files changed, 30 insertions(+), 20 deletions(-) diff --git a/newlib/libc/stdbit/stdc_bit_ceil.c b/newlib/libc/stdbit/stdc_bit_ceil.c index f25d184af..7ffed05f4 100644 --- a/newlib/libc/stdbit/stdc_bit_ceil.c +++ b/newlib/libc/stdbit/stdc_bit_ceil.c @@ -23,16 +23,17 @@ stdc_bit_ceil_uc(unsigned char x) return (1U << (UINT_WIDTH - __builtin_clz(x - 1))); } -/* Ensure we don't shift 1U out of range. */ -_Static_assert(USHRT_WIDTH < UINT_WIDTH, - "stdc_bit_ceil_us needs USHRT_WIDTH < UINT_WIDTH"); - unsigned short stdc_bit_ceil_us(unsigned short x) { if (x <= 1) return (1); +#if USHRT_WIDTH == UINT_WIDTH + if (x > USHRT_MAX/2 + 1) + return (0); +#endif + return (1U << (UINT_WIDTH - __builtin_clz(x - 1))); } diff --git a/newlib/libc/stdbit/stdc_leading_ones.c b/newlib/libc/stdbit/stdc_leading_ones.c index c7ab75b98..48f61d71e 100644 --- a/newlib/libc/stdbit/stdc_leading_ones.c +++ b/newlib/libc/stdbit/stdc_leading_ones.c @@ -22,13 +22,15 @@ stdc_leading_ones_uc(unsigned char x) return (__builtin_clz(~(x << offset))); } -/* Avoid triggering undefined behavior if x == 0. */ -_Static_assert(USHRT_WIDTH < UINT_WIDTH, - "stdc_leading_ones_us needs USHRT_WIDTH < UINT_WIDTH"); - unsigned int stdc_leading_ones_us(unsigned short x) { +#if USHRT_WIDTH == UINT_WIDTH + /* Avoid triggering undefined behavior if x == 0. */ + if (x == ~0U) + return (USHRT_WIDTH); +#endif + const int offset = UINT_WIDTH - USHRT_WIDTH; return (__builtin_clz(~(x << offset))); diff --git a/newlib/libc/stdbit/stdc_leading_zeros.c b/newlib/libc/stdbit/stdc_leading_zeros.c index dba6a14e2..204f71e21 100644 --- a/newlib/libc/stdbit/stdc_leading_zeros.c +++ b/newlib/libc/stdbit/stdc_leading_zeros.c @@ -22,13 +22,15 @@ stdc_leading_zeros_uc(unsigned char x) return (__builtin_clz((x << offset) + (1U << (offset - 1)))); } -/* Offset must be greater than zero. */ -_Static_assert(USHRT_WIDTH < UINT_WIDTH, - "stdc_leading_zeros_us needs USHRT_WIDTH < UINT_WIDTH"); - unsigned int stdc_leading_zeros_us(unsigned short x) { +#if USHRT_WIDTH == UINT_WIDTH + /* Offset must be greater than zero. */ + if (x == 0) + return (USHRT_WIDTH); +#endif + const int offset = UINT_WIDTH - USHRT_WIDTH; return (__builtin_clz((x << offset) + (1U << (offset - 1)))); diff --git a/newlib/libc/stdbit/stdc_trailing_ones.c b/newlib/libc/stdbit/stdc_trailing_ones.c index 0f7ccb553..22f002878 100644 --- a/newlib/libc/stdbit/stdc_trailing_ones.c +++ b/newlib/libc/stdbit/stdc_trailing_ones.c @@ -20,13 +20,15 @@ stdc_trailing_ones_uc(unsigned char x) return (__builtin_ctz(~x)); } -/* Avoid triggering undefined behavior if x == ~0. */ -_Static_assert(USHRT_WIDTH < UINT_WIDTH, - "stdc_trailing_ones_uc needs USHRT_WIDTH < UINT_WIDTH"); - unsigned int stdc_trailing_ones_us(unsigned short x) { +#if USHRT_WIDTH == UINT_WIDTH + /* Avoid triggering undefined behavior if x == ~0. */ + if (x == ~0U) + return (USHRT_WIDTH); +#endif + return (__builtin_ctz(~x)); } diff --git a/newlib/libc/stdbit/stdc_trailing_zeros.c b/newlib/libc/stdbit/stdc_trailing_zeros.c index 396333e17..8526461bc 100644 --- a/newlib/libc/stdbit/stdc_trailing_zeros.c +++ b/newlib/libc/stdbit/stdc_trailing_zeros.c @@ -20,14 +20,17 @@ stdc_trailing_zeros_uc(unsigned char x) return (__builtin_ctz(x | 1U << UCHAR_WIDTH)); } -/* Ensure we do not shift 1U out of range. */ -_Static_assert(USHRT_WIDTH < UINT_WIDTH, - "stdc_trailing_zeros_uc needs USHRT_WIDTH < UINT_WIDTH"); - unsigned int stdc_trailing_zeros_us(unsigned short x) { +#if USHRT_WIDTH == UINT_WIDTH + /* Ensure we do not shift 1U out of range. */ + if (x == 0U) + return (USHRT_WIDTH); + return (__builtin_ctz(x)); +#else return (__builtin_ctz(x | 1U << USHRT_WIDTH)); +#endif } unsigned int