From nobody Tue Dec 2 02:59:10 2025 Received: from bali.collaboradmins.com (bali.collaboradmins.com [148.251.105.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE1802D4814 for ; Mon, 17 Nov 2025 23:52:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.251.105.195 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763423546; cv=none; b=M/brNo6Gc00gQfkk5KfU8dLzxgwBEzeT+ww29g2mHA6rmHxZ3k+Xwh9TmV5itdtI1gieLqVi/90QDAonvstJcSoSCqkJh9SJecAbHornuDkbiuf32RrpeYYN9hGXp5GAiNJPgJmGRJU67+/fijkbujXrWg50udjHwmALq/ClpRQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763423546; c=relaxed/simple; bh=+lxsf3OSskd50eKCDyensIyhpCWxmlJP/CpAxjesv3E=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=EWPNX1gnB2EX4Dr34rSLSNVI6HnnY85ogk8NwYw/F3DujihqnSbvc4x2M3wv1oVj8wyoHIFuNui4m5W6L/xyUrVBnJ3tv3uhCyrLbHzpXFJrCBWSUqUt5gds3vy56RzYnhf/uNFYIv3844M/pZGPjptqvBb1+UGneyzZDuzBkq4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b=URzK9UyG; arc=none smtp.client-ip=148.251.105.195 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="URzK9UyG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1763423543; bh=+lxsf3OSskd50eKCDyensIyhpCWxmlJP/CpAxjesv3E=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=URzK9UyGZF6AiiIFblckM4+u31ZC9WG4xQg1l0Bh6kHlLBlS6vUAkXUzk4pCVTuVh WtMyWgujwPNcdNLvA3dkTHXtKcBW/IStI95wrI6fsVyerV+BnwFFbRv2qHteTYnaTT TDE7Bup+KICyEbdRWF9CLMPcwRzA4mfdEAYKRs7MtpARX3W6aDqnqGyeUI9J78nG+j swxw6CnAQQcRD99NsIO1aUCd2jAe8+j1XMzZZZ4WiT9QtTkBJf0vdwajPe7iErfuJJ epSrZSJHROAzVIokz8AHI9nGMHRDolTv2bV6OUb/jyq4SJsERWpifrlHDx8GN6Mu/Y /czw+VQF78qEw== Received: from localhost (unknown [82.79.138.145]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: cristicc) by bali.collaboradmins.com (Postfix) with ESMTPSA id 0EBAC17E110D; Tue, 18 Nov 2025 00:52:23 +0100 (CET) From: Cristian Ciocaltea Date: Tue, 18 Nov 2025 01:51:59 +0200 Subject: [PATCH v3 1/4] uapi: Provide DIV_ROUND_CLOSEST() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251118-rk3588-bgcolor-v3-1-a2cc909428ea@collabora.com> References: <20251118-rk3588-bgcolor-v3-0-a2cc909428ea@collabora.com> In-Reply-To: <20251118-rk3588-bgcolor-v3-0-a2cc909428ea@collabora.com> To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Sandy Huang , =?utf-8?q?Heiko_St=C3=BCbner?= , Andy Yan , Louis Chauvet , Haneen Mohammed , Melissa Wen Cc: Robert Mader , kernel@collabora.com, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org X-Mailer: b4 0.14.3 Currently DIV_ROUND_CLOSEST() is only available for the kernel via include/linux/math.h. Expose it to userland as well by adding __KERNEL_DIV_ROUND_CLOSEST() as a common definition in uapi. Additionally, ensure it allows building ISO C applications by switching from the 'typeof' GNU extension to the ISO-friendly __typeof__. Signed-off-by: Cristian Ciocaltea --- include/linux/math.h | 18 +----------------- include/uapi/linux/const.h | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/include/linux/math.h b/include/linux/math.h index 0198c92cbe3e..24bb868f971c 100644 --- a/include/linux/math.h +++ b/include/linux/math.h @@ -89,23 +89,7 @@ } \ ) =20 -/* - * Divide positive or negative dividend by positive or negative divisor - * and round to closest integer. Result is undefined for negative - * divisors if the dividend variable type is unsigned and for negative - * dividends if the divisor variable type is unsigned. - */ -#define DIV_ROUND_CLOSEST(x, divisor)( \ -{ \ - typeof(x) __x =3D x; \ - typeof(divisor) __d =3D divisor; \ - (((typeof(x))-1) > 0 || \ - ((typeof(divisor))-1) > 0 || \ - (((__x) > 0) =3D=3D ((__d) > 0))) ? \ - (((__x) + ((__d) / 2)) / (__d)) : \ - (((__x) - ((__d) / 2)) / (__d)); \ -} \ -) +#define DIV_ROUND_CLOSEST __KERNEL_DIV_ROUND_CLOSEST /* * Same as above but for u64 dividends. divisor must be a 32-bit * number. diff --git a/include/uapi/linux/const.h b/include/uapi/linux/const.h index b8f629ef135f..471877322f47 100644 --- a/include/uapi/linux/const.h +++ b/include/uapi/linux/const.h @@ -50,4 +50,21 @@ =20 #define __KERNEL_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) =20 +/* + * Divide positive or negative dividend by positive or negative divisor + * and round to closest integer. Result is undefined for negative + * divisors if the dividend variable type is unsigned and for negative + * dividends if the divisor variable type is unsigned. + */ +#define __KERNEL_DIV_ROUND_CLOSEST(x, divisor)( \ +{ \ + __typeof__(x) __x =3D x; \ + __typeof__(divisor) __d =3D divisor; \ + (((__typeof__(x))-1) > 0 || \ + ((__typeof__(divisor))-1) > 0 || \ + (((__x) > 0) =3D=3D ((__d) > 0))) ? \ + (((__x) + ((__d) / 2)) / (__d)) : \ + (((__x) - ((__d) / 2)) / (__d)); \ +} \ +) #endif /* _UAPI_LINUX_CONST_H */ --=20 2.51.2