Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp1973248lqo; Mon, 13 May 2024 04:26:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVczvgFklpHn+YWTdiXn00LQZvaFmgjr9OFK3X4L9DxjEX/4jTcPl9burg7Q/XCQnJhauf7uBe/y60pvzlfUq7DBDwOOHPFz69Zm2xNVA== X-Google-Smtp-Source: AGHT+IGCWZPxP2abzj2fBAF0ywpHZcMUcjNOfqNdyHNHw/B/U1Eb4xe8yLQK/a18krKfMghMbFzd X-Received: by 2002:a05:6300:8085:b0:1af:48b0:a5c8 with SMTP id adf61e73a8af0-1afde20080fmr8271317637.57.1715599599626; Mon, 13 May 2024 04:26:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715599599; cv=pass; d=google.com; s=arc-20160816; b=u4fJ0iMHBg4KlY7WoKEtjH5K0Zl/6MGjpVR0kV4TjiTjHgGTl0jFQMMiLPi2h+AKfT iaoikOX8OyOLwrxaGdpWMyH9tAZORD3eQvm+9sLRd2LLu3F5Zj1rqHsGiNceQHP2ggu1 rwGUDkExPqMih8bwFwm/BRcg9qjwDlv+jWgZzMDF3GAjiHeL80Ku7fgp32YoDG0zlalb 15ps78SGUBN3e+6SjvU9xLu5Tr/KFmqnMRW1Rv/dYBkf0WFdHbfjT3lQ+sAzf7bTqvRm yLXiUaeZwCPsNTmj7FQw2+6GZ0l4KnwyrGIiGpGosiNSihRva/ghqsPJ5vqYX0g7tx/X DPbQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=eawYmx/H8bHfSzex1qDdS9vopfxAzhLAnG6LsubXIAc=; fh=KhZyWZsaxQE5Sy8uLko6qIgPJ+YDpsu+LnKeYbWyqVg=; b=f6q1PU/kS946eotCYm2nJ8xp3Yp5+pS8hFFqFRwG0UPqf9Qj5e8gRN7oJL0OlxwYbg CW49dFvBkeiqOTKTGG3ICYW5LgUnwYDVEg8FosyPpUD0hvemDJD8pshVpD25/lvyVV9m d9+TFCjaBB1+skQz3FjrNrFIc7yLznCxLdRYJUYPEOs/D1Q9DnmKWnChakgUigUlRPis kenak/vEMb+l2caNyPCePF/SaqyUhP/LH2rhMYGVZZQKphuCqywwJSb06Mdi2H6ihy3n zw4FTlpRgFMKGBp5kd6bAVdMGOqgbgzSUxch41WlvC4VXRw5vZk2PZBKa5Zrm2uKpp6J GVWw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=mTVa941V; arc=pass (i=1 spf=pass spfdomain=ti.com dkim=pass dkdomain=ti.com dmarc=pass fromdomain=ti.com); spf=pass (google.com: domain of linux-kernel+bounces-177502-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-177502-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d9443c01a7336-1ef0c133acdsi95212735ad.432.2024.05.13.04.26.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 04:26:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-177502-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=mTVa941V; arc=pass (i=1 spf=pass spfdomain=ti.com dkim=pass dkdomain=ti.com dmarc=pass fromdomain=ti.com); spf=pass (google.com: domain of linux-kernel+bounces-177502-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-177502-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 41F1A283022 for ; Mon, 13 May 2024 11:26:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BE49914D28C; Mon, 13 May 2024 11:26:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ti.com header.i=@ti.com header.b="mTVa941V" Received: from lelv0142.ext.ti.com (lelv0142.ext.ti.com [198.47.23.249]) (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 EAA9C146A8B; Mon, 13 May 2024 11:26:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.47.23.249 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715599589; cv=none; b=Vhg6JYUELYKs6Lzx4xVuh3pzpzqFycVg50Slbet5wpNmP1avdXDOcXeEyLMzHzlvSW54WM3V+unJBBBBbQmn+jCFAdoOIjRkMf4IHPfSXBX06rJRwYJxe3t5mgIbDqj6/5lY+ObkHFLQhI2lqthMXP8g8z5ssrLbDc0t7gl+rFA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715599589; c=relaxed/simple; bh=w6bXvTIiUpMD4wbwccXChfqc4hfumpC2Fp5Gf0Kx3Jw=; h=Message-ID:Date:MIME-Version:Subject:To:CC:References:From: In-Reply-To:Content-Type; b=TEroiBp62BSpSsif/2elBxKeYprLZTIlC6ZK3cbZvCSH/pcVDQAc2k3gDcgHB0nJBIG0JKU9QDBj9KPWV0bYmlzQwhX/nXC24/W2tIPPUTJdeLDG4pzrxd6nmNBxqiDAsvPU3R5ByGgjpmpreGpCdVSBSprYyMkki1XHmPgXCRo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ti.com; spf=pass smtp.mailfrom=ti.com; dkim=pass (1024-bit key) header.d=ti.com header.i=@ti.com header.b=mTVa941V; arc=none smtp.client-ip=198.47.23.249 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ti.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ti.com Received: from fllv0034.itg.ti.com ([10.64.40.246]) by lelv0142.ext.ti.com (8.15.2/8.15.2) with ESMTP id 44DBQ7LZ120561; Mon, 13 May 2024 06:26:07 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1715599567; bh=eawYmx/H8bHfSzex1qDdS9vopfxAzhLAnG6LsubXIAc=; h=Date:Subject:To:CC:References:From:In-Reply-To; b=mTVa941Vc+32qCU2A+G8gQGpQNJUVjif86p6HO5cP8Mjap69/1yYgiv2Hvg9CZwWI aKdBkNh7SOV+wtWX55ixUTiFFM1wj59EUiCSGs4JxanKU4ySbxUM7WGJAGLXRiK+0l sgqsnCoOrTwSe86OgZBWpV8Qt0XSJ18pD19cBJtA= Received: from DLEE106.ent.ti.com (dlee106.ent.ti.com [157.170.170.36]) by fllv0034.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 44DBQ7gs029885 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Mon, 13 May 2024 06:26:07 -0500 Received: from DLEE108.ent.ti.com (157.170.170.38) by DLEE106.ent.ti.com (157.170.170.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23; Mon, 13 May 2024 06:26:07 -0500 Received: from lelvsmtp6.itg.ti.com (10.180.75.249) by DLEE108.ent.ti.com (157.170.170.38) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23 via Frontend Transport; Mon, 13 May 2024 06:26:07 -0500 Received: from [172.24.227.193] (devarsht.dhcp.ti.com [172.24.227.193] (may be forged)) by lelvsmtp6.itg.ti.com (8.15.2/8.15.2) with ESMTP id 44DBPxsB051978; Mon, 13 May 2024 06:25:59 -0500 Message-ID: <6557050e-6b18-2628-cbab-1a811b2190ba@ti.com> Date: Mon, 13 May 2024 16:55:58 +0530 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: [PATCH v7 6/8] math.h Add macros to round to closest specified power of 2 Content-Language: en-US To: Andy Shevchenko CC: Jani Nikula , , , , , , , , , , , , , , , , , , , , , , , , , References: <20240509183952.4064331-1-devarsht@ti.com> <87fruphf55.fsf@intel.com> <5ebcf480-81c6-4c2d-96e8-727d44f21ca9@ti.com> From: Devarsh Thakkar In-Reply-To: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Hi Andy, On 13/05/24 14:29, Andy Shevchenko wrote: > On Sat, May 11, 2024 at 11:11:14PM +0530, Devarsh Thakkar wrote: >> On 10/05/24 20:45, Jani Nikula wrote: > > [...] > >>> Moreover, I think the naming of round_up() and round_down() should have >>> reflected the fact that they operate on powers of 2. It's unfortunate >>> that the difference to roundup() and rounddown() is just the underscore! >>> That's just a trap. >>> >>> So let's perhaps not repeat the same with round_closest_up() and >>> round_closest_down()? >> >> Yes the naming is inspired by existing macros i.e. round_up, round_down >> (which round up/down to next pow of 2) and DIV_ROUND_CLOSEST (which >> round the divided value to closest value) and there are already a lot of >> users for these API's : >> >> linux-next git:(heads/next-20240509) ✗ grep -nr round_up drivers | wc >> 730 4261 74775 >> >> linux-next git:(heads/next-20240509) ✗ grep -nr round_down drivers | wc >> 226 1293 22194 >> >> linux-next git:(heads/next-20240509) ✗ grep -nr DIV_ROUND_CLOSEST >> drivers | wc >> 1207 7461 111822 > > Side note, discover `git grep ...`: it's much much faster on Git index, > than classic one on a working copy. > >> so I thought to align with existing naming convention assuming >> developers are already familiar with this. >> >> But if a wider consensus is to go with a newer naming convention then I >> am open to it, although a challenge there would be to keep it short. For >> e.g. this one is already 3 words, if we go with more explicit >> "round_closest_up_pow_2" it looks quite long in my opinion :) . > > You need properly name the macros. Again, round_up() / roundup() and > roundup_pow_of_two() are three _different_ macros, and it's not clear > why you can't use one of them in your case. > I can't use any of these because these macros either round up or round down, whereas I want to round to closest value for the argument specified by the user, be it achieved either by rounding up or rounding down depending upon whichever makes the answer closer to the user-specified argument. To make it clear, I have already included the examples in the macro description [2], copying it here, maybe I can put the same examples in the commit message too to avoid confusions : round_closest_up(17, 4) = 16 round_closest_up(15, 4) = 16 round_closest_up(14, 4) = 16 round_closest_down(17, 4) = 16 round_closest_down(15, 4) = 16 round_closest_down(14, 4) = 12 Coming back to naming, this is as per existing convention used for naming round_up, round_down (notice the `_` being used for macros working with pow of 2) and DIV_ROUND_CLOSEST (notice the work `closest` used to specify the answer to be nearest to specified value). Naming is a bit subjective, but I personally don't think it is a good idea to go away with the existing naming convention or go with longer names. > The patch that changes those to a new one are doubtful to begin with. > I.e. need a careful review on the arithmetics side of the change > including HW capabilities of handling "closest" results. > This is already tested from my side, in-fact I have posted some of the results in cover-letter with these macros [1] : Regarding hardware capabilities, it uses existing round_up, round_down macros underneath which are optimized to handle pow of 2 after modifying the user provided argument using addition/subtraction and division, so I don't think it should generally a problem with the hardware. And I see other macros DIV_ROUND_CLOSEST [3] already using similar operations i.e. addition/subtraction and division so don't think it should be a problem to keep similar other macros in the same file. [1]: https://gist.github.com/devarsht/de6f5142f678bb1a5338abfd9f814abd#file-v7_jpeg_encoder_crop_validation-L204 [2]: https://lore.kernel.org/all/20240509183952.4064331-1-devarsht@ti.com/ [3]: https://elixir.bootlin.com/linux/latest/source/include/linux/math.h#L86 Regards Devarsh