Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp33982lqo; Thu, 9 May 2024 11:43:47 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWGdt7PrM9WhRquUn1Cu+JlE+uSUDxX0CnBUP1KPw+Vy4YRGm5PqTATR7kHDrtLMLmMZRfZo3tp/m9FmHXk7K4l124pwFyrPGrdVw1pdw== X-Google-Smtp-Source: AGHT+IF3HvcQKfb5WICFiKM0LQr1KHOW1FM4bcedBdU7NDezh4qrXDQ4eoxF2GrBCzV88lPKnJ6K X-Received: by 2002:a05:6a21:18d:b0:1af:37bf:d7de with SMTP id adf61e73a8af0-1afde0b7019mr842459637.7.1715280226799; Thu, 09 May 2024 11:43:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715280225; cv=pass; d=google.com; s=arc-20160816; b=v7jQV3dqt5x3Edek2Z+o/YfIJPTqzEsApiVI/QdDMVw2qxwWf+Dm1FCMeHJMx9XcZl wMOc727G9rQjSB1+u9kNrequd5qF5CyiVoImjzeBHKscoZHce008pgNnCSdpe/NDGSgH fiyjlGsn2vJOqr/xHpc85D4Hj8VfXfyRJO50qI3i1JrDMmgZHEaVgC4bYlyPVXoUQ3bo cQqzT981wqWa5EbAxCpAnNCT/rYpXWhjWzsZM7NyF0gwb5ZDw3+NxBCAWw1fvJ0JpYRv tzuym1c75r8EYoUyj1VlNwYMgL+f2OTMBqbj2F0GWzaG4P+ejBoZDSkiOiyUZIRHzLFk y0xA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=lrdTPlSx5uWnTmoVruaPcAiqQdb2LlFFka9SllqeKfQ=; fh=1xU0MNTD5WvowJkrsm3kv7tjx5Tpe/c751cc9/Gl3K4=; b=ziKazq1WoMJH7qZY8ELhhL/nCPNxbOWt4a4oVRlJWhjFdEXjMI+V3426BcQuTG0tRV /UYTWBAcu6ANHY92ZbPiEGGcGNfsWBdRq5DH1uKbQTkOACBa6DQMCKYzQ9kFQHsr75/h y+nkyf48QMES0LDxzHZ3geXUxffsE+CmFQKA6SfkIxLryz+CSacMqvniPf6tH7wX+qM1 dyHMw+oFcEzcXY18OaXcUV85w3UYLx8aczbJ1mD9XWAO+92lYK+k/sb0ikiH+mOnG1v3 gE9FPfLC7AHxRJKbznoZwHVet3t3BKTqJDWXpohYI3gGFhrClLqj9G/aRpmWLNONpZKh YvKg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=Y6l4wMuU; 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-174782-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-174782-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id d2e1a72fcca58-6f4d2b514d2si2112384b3a.386.2024.05.09.11.43.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 11:43:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-174782-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=Y6l4wMuU; 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-174782-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-174782-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 7113BB22FB7 for ; Thu, 9 May 2024 18:41:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 72F9680C14; Thu, 9 May 2024 18:40:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ti.com header.i=@ti.com header.b="Y6l4wMuU" 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 E566E7F470; Thu, 9 May 2024 18:40:09 +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=1715280011; cv=none; b=RWjbf0UgunkTq78VOkUH4myijUUy6xl8pRBhu+D2b4biOI2fEg6M/e8iXPXd94TQWkcaiu23WPDleEbfK1vjXX7CW+4lWs0liCZLLXpiZzbydLxkH/ZgEDfSbOAqWpjF0d7BqIA+1jKjwMB5yqstgvfWI9w7In+sl5HL2yTjCc8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715280011; c=relaxed/simple; bh=ho6y2eiGzKmKxS7SJvcX41zaBIRbDgw/6Tjm/gP5XA8=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=Qb1yTm8R+7izH+dzLpR6wlC2HTUWC0DFa5Y0cdHGefFApEO1MCWGRS6ytmCzCQbDx+RdERG5ofUvRcv4LW5Hzp3MiHlzeg17lBRJ4zpGEFxSNOUU7na+FQCEU3fgaDrNdk6nZW5pZ93mX6snuw5wCqNVa7CwpbMK42Cvv93njZc= 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=Y6l4wMuU; 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 fllv0035.itg.ti.com ([10.64.41.0]) by lelv0142.ext.ti.com (8.15.2/8.15.2) with ESMTP id 449Ids7A090810; Thu, 9 May 2024 13:39:54 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1715279994; bh=lrdTPlSx5uWnTmoVruaPcAiqQdb2LlFFka9SllqeKfQ=; h=From:To:CC:Subject:Date; b=Y6l4wMuU3sCR4/pnLAByHJRo+7spyl3CQdZm7bJXqaXDi1ofG08qVgz0FQKnTG0Hu qmlmptMVUmE2iB6iWIyRXzeeieINGClAv+9uCGAkJ57bhGflIj10+RICSo4kjYl/6N MU3rJySLo7EwN235+EacWkv1CBtLRqNqe5p0CMkw= Received: from DLEE112.ent.ti.com (dlee112.ent.ti.com [157.170.170.23]) by fllv0035.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 449Idsjr028411 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Thu, 9 May 2024 13:39:54 -0500 Received: from DLEE114.ent.ti.com (157.170.170.25) by DLEE112.ent.ti.com (157.170.170.23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23; Thu, 9 May 2024 13:39:54 -0500 Received: from lelvsmtp6.itg.ti.com (10.180.75.249) by DLEE114.ent.ti.com (157.170.170.25) 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; Thu, 9 May 2024 13:39:54 -0500 Received: from localhost (ti.dhcp.ti.com [172.24.227.95] (may be forged)) by lelvsmtp6.itg.ti.com (8.15.2/8.15.2) with ESMTP id 449IdrZA004938; Thu, 9 May 2024 13:39:53 -0500 From: Devarsh Thakkar To: , , , , , , , , , , , , , , CC: , , , , , , , , , , , , Subject: [PATCH v7 6/8] math.h Add macros to round to closest specified power of 2 Date: Fri, 10 May 2024 00:09:52 +0530 Message-ID: <20240509183952.4064331-1-devarsht@ti.com> X-Mailer: git-send-email 2.39.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Add macros to round to nearest specified power of 2. Two macros are added : round_closest_up and round_closest_down which round up to nearest multiple of 2 with a preference to round up or round down respectively if there are two possible nearest values to the given number. This patch is inspired from the Mentor Graphics IPU driver [1] which uses similar macro locally and which can be updated to use this generic macro instead along with other drivers having similar requirements. [1]: https://elixir.bootlin.com/linux/v6.8.9/source/drivers/gpu/ipu-v3/ipu-image-convert.c#L480 Signed-off-by: Devarsh Thakkar --- V1->V6 (No change, patch introduced in V7) --- include/linux/math.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/include/linux/math.h b/include/linux/math.h index dd4152711de7..82ee3265c910 100644 --- a/include/linux/math.h +++ b/include/linux/math.h @@ -34,6 +34,42 @@ */ #define round_down(x, y) ((x) & ~__round_mask(x, y)) +/** + * round_closest_up - round to nearest specified power of 2 with preference + * to rounding up + * @x: the value to round + * @y: multiple to round to (must be a power of 2) + * + * Rounds @x to nearest multiple of @y (which must be a power of 2). + * The rounded value can be greater than or less than @x depending + * upon it's nearness to @x. If there are two nearest possible values, + * then preference will be given to the greater value. + * + * Examples : + * round_closest_up(17, 4) = 16 + * round_closest_up(15, 4) = 16 + * round_closest_up(14, 4) = 16 + */ +#define round_closest_up(x, y) round_down((x) + (y) / 2, (y)) + +/** + * round_closest_down - round to nearest specified power of 2 with preference + * to rounding down + * @x: the value to round + * @y: multiple to round down to (must be a power of 2) + * + * Rounds @x to nearest multiple of @y (which must be a power of 2). + * The rounded value can be greater than or less than @x depending + * upon it's nearness to @x. If there are two nearest possible values, + * then preference will be given to the lesser value. + * + * Examples : + * round_closest_down(17, 4) = 16 + * round_closest_down(15, 4) = 16 + * round_closest_down(14, 4) = 12 + */ +#define round_closest_down(x, y) round_up((x) - (y) / 2, (y)) + #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP #define DIV_ROUND_DOWN_ULL(ll, d) \ -- 2.39.1