From patchwork Sun Jul 14 11:32:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Trimarchi X-Patchwork-Id: 3304 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id D66304130C for ; Sun, 14 Jul 2024 13:33:32 +0200 (CEST) Received: by mail-ed1-f71.google.com with SMTP id 4fb4d7f45d1cf-58c38bcd765sf2847755a12.1 for ; Sun, 14 Jul 2024 04:33:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1720956812; cv=pass; d=google.com; s=arc-20160816; b=0M8pY3+vvSiuvCEWBD+X7mts6LJ834wP575tG4vvI+XjNVljcWVXIzbiEzJldR4y0G Cwn1VTIjiVz/tgRMrbUsaOIoL9kMZe4QVqF+lQ4z36KvOHq9P+AeOS3sID7jgKn9D/mk q7IKFlV8vpVxOHGYDxhXXoFaiZdiY1UEJIeU+GhSnfUPK6++/ZDzU9eQzVJBGRVm4+kG HvfgHsJmDrwWa8dvRjNW88iXNnP781CeLbP45DElerd9HMcUFJCNnVUucv586/nqeDn3 RJx+OW6ti7wQCbtdY/A6dcG46pOu2xqWPp+ufPzk1pDOI7kMgcnf4kYaV/3CX7AkeeYx 22qQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=IX2bI+AYYcyF4AJ8YY33jhHNvGyHcGPs5OrGSUzBR5k=; fh=2W66GUJtd3bOqk0XmMI5t+sIlukCwDD6N6fplCtbQsk=; b=pnidpfc1s6oC1ju7w2ZQ2UzoIB6kC4281ZAojPNxwx/1SucWa0uf0OUELXuklifq4L MpP/f3Fw/623WYrKy5HhrzEu2/OQy5+dW/V0oJHl31zy11Fb8ZbQ02UffX8+JCQmw4NC 657VJpI8yv3YFiVHm36+YgFbBIVROoWWYMVkgyBxfyj6C/z0Kn7IivBCfS9GhVs1NOA9 +x5qK4EIbw6gFNoO0geH+4zulYPlPCgnyoxMhtyJd9ssBab1sEbANPdVT4GJhCVufYPo HjiNH9D2qhKGV87UTwUL0OUHW+ocS5DYsoun1qXJmGhEImy06kVbxUwZ69gkXq6U/Vi1 oLqQ==; darn=patchwork.amarulasolutions.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=EGAVzDCS; spf=pass (google.com: domain of michael@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=michael@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com; dara=pass header.i=@amarulasolutions.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; t=1720956812; x=1721561612; darn=patchwork.amarulasolutions.com; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:x-original-authentication-results :x-original-sender:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=IX2bI+AYYcyF4AJ8YY33jhHNvGyHcGPs5OrGSUzBR5k=; b=iupm24BbehmV+prWif57HOjgiuBahDpX/Q3IT/FNlOJCtB/lr4Jfktg1ZKTtOhi6qt yH5xXjsvIRzBceG/yGq+xgs+y3eNdQcJS1wRMc1Nnyr/kbBJGiY4m2aVuAZVosxLXOJg FPFstTZFa9sWUOcKpA6P0u9gCW22D3R3xbex8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720956812; x=1721561612; h=list-unsubscribe:list-archive:list-help:list-post :x-spam-checked-in-group:list-id:mailing-list:precedence :x-original-authentication-results:x-original-sender:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :x-beenthere:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=IX2bI+AYYcyF4AJ8YY33jhHNvGyHcGPs5OrGSUzBR5k=; b=c8DiI2Pq+O6aKZ1Oe1ytBFwpmRwD+2yViY6igaMackHwt6zNGCXKW01TMvMwO4t2SM CZ9dqMCBJhbW8OnIreaqBtSS14+i+J2Yd44vLkwg1fgFh8VZ8nS3JrpDwu0tmWRm5ZFD MfVxPhimtc+yfvJL85nf1hPywo+OGQ2vseu5fUvq7lDdSVfpBYulAFNUdAqujqqOwYLq AV4uogcJLDlNzR3WkjNlewXKtknJP7Ngs1wzG9rAKbNb5Rp+/2XvXxw6zwrjsfgdeqju s7uaHPetPf+8rUGpOj3sZYxOXaePxN3IgXlyZNF5Fj9rq91JXKrVKtk1goYPt72niqQD sJGg== X-Forwarded-Encrypted: i=2; AJvYcCUG4+2G8KWk8+rgNq7qyLgm2zgcNW3SEh1jgkuyOJXJDA7Pl1WcZtgiKLoNoX6KsXLhcNP33/OTS6KA5TgGighGHHDDwst6ZkmFSVJCqjiXoxrj/cnN0bLjOKs2iw== X-Gm-Message-State: AOJu0Ywv5bDKeFo6PHAL/ewXwCR7w9FbC4Apn+qXjkJi7W+pNer0ueYP hxUxGrIjC3gK6ib6axD9n5dU5XexezB+cakvmTXWDBfQO/7fOA4cUWQ2dw77CvPIiQ== X-Google-Smtp-Source: AGHT+IGlHWrZer2vAQWUc5dQlRtegAvDrQhQSWeQYaQ/MDt/T+B2cRsE99b2LpsxBXuZV+oJVTVPSw== X-Received: by 2002:a05:6402:350b:b0:58f:ebaa:64bf with SMTP id 4fb4d7f45d1cf-594ba9975bamr12405355a12.2.1720956812564; Sun, 14 Jul 2024 04:33:32 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:aa7:cc94:0:b0:57d:555d:8ddd with SMTP id 4fb4d7f45d1cf-59821cb87f6ls1726680a12.0.-pod-prod-02-eu; Sun, 14 Jul 2024 04:33:31 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWrLi47MqKZDcrOF3cE3a7OuFNfo3TWzX3kvMkleJ7ZGIADQTqiZbKSyfaI8fjDIXbCygo+eqiieeY9Io2XovxYP44Hznw55mzp0ugPEUrEZVde X-Received: by 2002:a50:9f85:0:b0:59a:a8cc:23a7 with SMTP id 4fb4d7f45d1cf-59aa8cc2436mr3229266a12.0.1720956810848; Sun, 14 Jul 2024 04:33:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1720956810; cv=none; d=google.com; s=arc-20160816; b=eRXKBd0qoVsSJaQO6R9pfH+nC1fEUBhN4+Xx0/fpxQjeHmeeL3QcUbtUxaAW4Mh8wc 6/Xv5AmkGyq3n7Tj65773kyduV6glDTJiAry4ttO+ijCdZ+spRlLISek6n2ZvJvKFhG0 ntrzFjMBhIDBgWsTKgdP2zrA7thOsNDxaJ1IhWqWWmbLfc5wvDQHIhdXBXWaSmaCz+u7 GwUitwLXqoA5qQBzyBkkzHYlrk++6iZK0V6zsO7SaCch7dNpOVc0zgStH+X8Afw55YjG gP5M8hocwyJO4EVMHR07g344K46s+0zB+Va1QZNg1vlFnglDe5K7yBGIqoBYA3QBaj6t fRdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=WD7ZmfhocDYBRfei8FW+9wSfSoFIOqwvjpGxjOjRVBs=; fh=tnLa1kZ5wWxJ67NS5WFaTn665pfka0o9vqrl4k9tyGo=; b=PVLL/56aYPvGPHsMOgmXrsbPVhwTcPp96JaBDb1UcnMFAS/6oGeW9mDFkuCQzC13Dv pdbszvpGEN9F7qqPXPUeyjTMkIRDPYB3SQ4kSraHnhiPFyUS2lYCgQEY9Cz9dCOTgFIQ INwPgD7QdmHxGFg5nuZBKANaXzBu5lJLSE1T7bgj5So1Lt4EHCVPj9k1GrIVM2YxjhJs rlan37LAOMcXWpK1LnngsYHdyfxFGaM/D74IYAQyr0dUPlEKk/Q99/v2cDdKWsKjzXsL ohUbLiOCWPkD3hJO1mkYDmkMZiDiw8C1wA/vN8LOF2tlpJF+bjO3ODz0/GdXmJYvSjBO 8Idw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=EGAVzDCS; spf=pass (google.com: domain of michael@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=michael@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com; dara=pass header.i=@amarulasolutions.com Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id 4fb4d7f45d1cf-59b26f64d55sor430495a12.10.2024.07.14.04.33.30 for (Google Transport Security); Sun, 14 Jul 2024 04:33:30 -0700 (PDT) Received-SPF: pass (google.com: domain of michael@amarulasolutions.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; X-Forwarded-Encrypted: i=1; AJvYcCVau7s0sj0KgmLH43IOMZvuF5sMItrMtGEbxWhlBPm7f4UtdZjvvhpB4qEETDKjtnvHNtYrF36vwAlKndNgEEm0SYBY4LA92gE8rsI4nyKcjfcm X-Received: by 2002:a05:6402:348d:b0:58d:81ac:ea90 with SMTP id 4fb4d7f45d1cf-594bcba77e6mr13155416a12.38.1720956810376; Sun, 14 Jul 2024 04:33:30 -0700 (PDT) Received: from panicking.. (mob-5-91-58-211.net.vodafone.it. [5.91.58.211]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-59b25528dc1sm1929469a12.52.2024.07.14.04.33.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jul 2024 04:33:30 -0700 (PDT) From: Michael Trimarchi To: Michael Trimarchi Cc: Dario Binacchi , Patrick Barsanti , linux-amarula@amarulasolutions.com Subject: [PATCH 22/25] lib: div64: sync with Linux Date: Sun, 14 Jul 2024 13:32:59 +0200 Message-ID: <20240714113302.133399-22-michael@amarulasolutions.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240714113302.133399-1-michael@amarulasolutions.com> References: <20240714113302.133399-1-michael@amarulasolutions.com> MIME-Version: 1.0 X-Original-Sender: michael@amarulasolutions.com X-Original-Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=EGAVzDCS; spf=pass (google.com: domain of michael@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=michael@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com; dara=pass header.i=@amarulasolutions.com Content-Type: text/plain; charset="UTF-8" Precedence: list Mailing-list: list linux-amarula@amarulasolutions.com; contact linux-amarula+owners@amarulasolutions.com List-ID: X-Spam-Checked-In-Group: linux-amarula@amarulasolutions.com X-Google-Group-Id: 476853432473 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Sync with Linux commit fc4a0db4149af ("Merge tag '6.5'"). Cc: Tom Rini Signed-off-by: Michael Trimarchi --- include/linux/math64.h | 110 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/include/linux/math64.h b/include/linux/math64.h index eaa9fd5b96..0e684bb2be 100644 --- a/include/linux/math64.h +++ b/include/linux/math64.h @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: GPL-2.0 */ #ifndef _LINUX_MATH64_H #define _LINUX_MATH64_H @@ -12,6 +13,11 @@ /** * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder + * @dividend: unsigned 64bit dividend + * @divisor: unsigned 32bit divisor + * @remainder: pointer to unsigned 32bit remainder + * + * Return: sets ``*remainder``, then returns dividend / divisor * * This is commonly provided by 32bit archs to provide an optimized 64bit * divide. @@ -24,6 +30,11 @@ static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder) /** * div_s64_rem - signed 64bit divide with 32bit divisor with remainder + * @dividend: signed 64bit dividend + * @divisor: signed 32bit divisor + * @remainder: pointer to signed 32bit remainder + * + * Return: sets ``*remainder``, then returns dividend / divisor */ static inline s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder) { @@ -33,6 +44,11 @@ static inline s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder) /** * div64_u64_rem - unsigned 64bit divide with 64bit divisor and remainder + * @dividend: unsigned 64bit dividend + * @divisor: unsigned 64bit divisor + * @remainder: pointer to unsigned 64bit remainder + * + * Return: sets ``*remainder``, then returns dividend / divisor */ static inline u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder) { @@ -42,6 +58,10 @@ static inline u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder) /** * div64_u64 - unsigned 64bit divide with 64bit divisor + * @dividend: unsigned 64bit dividend + * @divisor: unsigned 64bit divisor + * + * Return: dividend / divisor */ static inline u64 div64_u64(u64 dividend, u64 divisor) { @@ -53,6 +73,10 @@ static inline u64 div64_u64(u64 dividend, u64 divisor) /** * div64_s64 - signed 64bit divide with 64bit divisor + * @dividend: signed 64bit dividend + * @divisor: signed 64bit divisor + * + * Return: dividend / divisor */ static inline s64 div64_s64(s64 dividend, s64 divisor) { @@ -92,10 +116,14 @@ extern s64 div64_s64(s64 dividend, s64 divisor); /** * div_u64 - unsigned 64bit divide with 32bit divisor + * @dividend: unsigned 64bit dividend + * @divisor: unsigned 32bit divisor * * This is the most common 64bit divide and should be used if possible, * as many 32bit archs can optimize this variant better than a full 64bit * divide. + * + * Return: dividend / divisor */ #ifndef div_u64 static inline u64 div_u64(u64 dividend, u32 divisor) @@ -107,6 +135,10 @@ static inline u64 div_u64(u64 dividend, u32 divisor) /** * div_s64 - signed 64bit divide with 32bit divisor + * @dividend: signed 64bit dividend + * @divisor: signed 32bit divisor + * + * Return: dividend / divisor */ #ifndef div_s64 static inline s64 div_s64(s64 dividend, s32 divisor) @@ -228,6 +260,24 @@ static inline u64 mul_u64_u64_shr(u64 a, u64 b, unsigned int shift) #endif +#ifndef mul_s64_u64_shr +static inline u64 mul_s64_u64_shr(s64 a, u64 b, unsigned int shift) +{ + u64 ret; + + /* + * Extract the sign before the multiplication and put it back + * afterwards if needed. + */ + ret = mul_u64_u64_shr(abs(a), b, shift); + + if (a < 0) + ret = -((s64) ret); + + return ret; +} +#endif /* mul_s64_u64_shr */ + #ifndef mul_u64_u32_div static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 divisor) { @@ -257,4 +307,64 @@ static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 divisor) } #endif /* mul_u64_u32_div */ +u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div); + +/** + * DIV64_U64_ROUND_UP - unsigned 64bit divide with 64bit divisor rounded up + * @ll: unsigned 64bit dividend + * @d: unsigned 64bit divisor + * + * Divide unsigned 64bit dividend by unsigned 64bit divisor + * and round up. + * + * Return: dividend / divisor rounded up + */ +#define DIV64_U64_ROUND_UP(ll, d) \ + ({ u64 _tmp = (d); div64_u64((ll) + _tmp - 1, _tmp); }) + +/** + * DIV64_U64_ROUND_CLOSEST - unsigned 64bit divide with 64bit divisor rounded to nearest integer + * @dividend: unsigned 64bit dividend + * @divisor: unsigned 64bit divisor + * + * Divide unsigned 64bit dividend by unsigned 64bit divisor + * and round to closest integer. + * + * Return: dividend / divisor rounded to nearest integer + */ +#define DIV64_U64_ROUND_CLOSEST(dividend, divisor) \ + ({ u64 _tmp = (divisor); div64_u64((dividend) + _tmp / 2, _tmp); }) + +/** + * DIV_U64_ROUND_CLOSEST - unsigned 64bit divide with 32bit divisor rounded to nearest integer + * @dividend: unsigned 64bit dividend + * @divisor: unsigned 32bit divisor + * + * Divide unsigned 64bit dividend by unsigned 32bit divisor + * and round to closest integer. + * + * Return: dividend / divisor rounded to nearest integer + */ +#define DIV_U64_ROUND_CLOSEST(dividend, divisor) \ + ({ u32 _tmp = (divisor); div_u64((u64)(dividend) + _tmp / 2, _tmp); }) + +/** + * DIV_S64_ROUND_CLOSEST - signed 64bit divide with 32bit divisor rounded to nearest integer + * @dividend: signed 64bit dividend + * @divisor: signed 32bit divisor + * + * Divide signed 64bit dividend by signed 32bit divisor + * and round to closest integer. + * + * Return: dividend / divisor rounded to nearest integer + */ +#define DIV_S64_ROUND_CLOSEST(dividend, divisor)( \ +{ \ + s64 __x = (dividend); \ + s32 __d = (divisor); \ + ((__x > 0) == (__d > 0)) ? \ + div_s64((__x + (__d / 2)), __d) : \ + div_s64((__x - (__d / 2)), __d); \ +} \ +) #endif /* _LINUX_MATH64_H */