From patchwork Sun Jul 14 07:10:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Nazzareno Trimarchi X-Patchwork-Id: 3254 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-lf1-f69.google.com (mail-lf1-f69.google.com [209.85.167.69]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id 8EB244130C for ; Sun, 14 Jul 2024 09:10:58 +0200 (CEST) Received: by mail-lf1-f69.google.com with SMTP id 2adb3069b0e04-52e9763468asf3308000e87.1 for ; Sun, 14 Jul 2024 00:10:58 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1720941058; cv=pass; d=google.com; s=arc-20160816; b=VsK2s3GW+tSrHd5z54ttyS0Q+woaYoS2ThSHsazM6WBO7Ednbs/PzI8MZZkMbjegSb fBuNkDz1R7U7pCZZ5LNkkwV3jLPxUOFhww/ZFUrl4OmSDLdX5TPXrBTOD4jI4OkjqXBV TC3P2/CPqUNNhXKn5brvU9uHwqX64bDIsnny1H4rU43OYZj+yKFwnMICa23bxxAmvpI1 /Aoeq2CUY21z49pRQgtfSQAnDpGzjpT77WaZaKHYHMJHkVceLSyfhC2BKS4KxfzZSY/q 83rQ5D8na+caAyGxw+XoqYpAAPd9B+xXZHEywRfhS/z0hAYc9qPZMzUDDkcmk3xH04lq YmBw== 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=mVb1s4j4Jvx3naUM0bIGfqITSBerhM/XaSvHP7bjfqw=; b=Xh2ESrqQ6xbyrc87+MPCdhmCs99MdV4y8piMxw445gmX8bA02R1gZwFxzhfStZTyXT ILXi0zYbIrrgXoGCFvDh93eg07nWzlxYlMlePNF8xpuwIu518RqFoi8MxyxbEkMEBboU IErOva+LqnEOa3fB5SPplk3+6KKwpL0lzLWTJSI6zJ93f8ueX519g3DV3rh3rqJq1r9E grpgmlMlfWFbPVp1gvGRCcUUtEZSiTutlvxc5T+8HlCdd5NG3Mu5UjC0r0UnpR6YsDlA pyDoKxMePFhnPftSoqgO6uSYHB2NMq4YFCY/9KzaWU52ia60PRKwuYgxqAoifCORsDcg RYFQ==; darn=patchwork.amarulasolutions.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=IyOvyizb; 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=1720941058; x=1721545858; 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=gGvd4wXtnsyRWCd43Xk6J3eE6AQmUYB+dg8BCvqiDz43F4qY4KcIzeAxZ++HKYNM3Y yOym10ZMjZPwpI1VDn/hlf3QrPzKHD0LsmAhMP9MarM/aOFXcgJQ4/xnEkpPlPsWYQYx 9ChcuaC8hyl18ZvnNuX2WYwv0xVuwI2y+nx6o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720941058; x=1721545858; 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=sNGOvfHjgvnFpszFNg9fp3Siw8qHZkWfrs3R5/lbLiTSx7NqM32oSAMSILyxl8sSKJ jN6zQecV05KL19DanU+89rWwwKaS2lmBGXrgS5mPDFBPRmLmqW6/28Bm/hIV/CH48EKQ DhQPDYI+CXh8JZdUR2VMXYRqKe3zK+t6iPdZkaREoPvl8e/LW/0d9hY2yhd34vjWAB64 XmBL20Su6F0LIHbu5kjHW8epT7kJbQUMQl4yz7k9C5ezv2k7At4HPmbj0aVE56EwWT5K CfnPiKppeHCN5RBYg/+HqqprlkRVPLxb7WovMTwEt03Nx9+kcZXL77dRgbpry2RvRzWX fO6w== X-Forwarded-Encrypted: i=2; AJvYcCWo2hi74JHxiw6485yrA3qRNMQ9qa7U4J2LM5kzXJy2MfyT897f9B3la+PMLQcb/kY7/kVCfTaxmIqRmwHWDKpKFLyQC9GHeM3gd5VztwYShmzSeNxsTPK6PdizCA== X-Gm-Message-State: AOJu0YzZPFNbOTbDUKFDhalIy6q4w9E8QTDdGaokP6sPJ7TvnwfHfvtf 1ZBPvaTdrS00CRVl5Q5wdixFjZVIbSJznYT8XV5l2WT7CFticKYTEOG5wQY129XVTQ== X-Google-Smtp-Source: AGHT+IGkNp8/tYDDRKZpHO+Smmt4qaw6CMUcuozW0nvzXYFdctRzsyVhxAZkA9urzF+d3XTmN64uLQ== X-Received: by 2002:a05:6512:4016:b0:52e:d0f8:2d43 with SMTP id 2adb3069b0e04-52ed0f82f09mr3226686e87.17.1720941057884; Sun, 14 Jul 2024 00:10:57 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:ac2:51ae:0:b0:52e:9d2f:232 with SMTP id 2adb3069b0e04-52ec409caa0ls1647138e87.0.-pod-prod-05-eu; Sun, 14 Jul 2024 00:10:56 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVn09QMVSsIRZKhY7ATGn8pTViNLYrK7go/QqPHCK0efNu6SHe72OV7BAJzF1t+sb2Kv77Augv5/37HPDZQ/gS1R8Jnhsr+qHaGsu+X2bwnW4+0 X-Received: by 2002:a05:6512:3e0c:b0:529:b718:8d00 with SMTP id 2adb3069b0e04-52eb9991e93mr16547533e87.8.1720941055867; Sun, 14 Jul 2024 00:10:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1720941055; cv=none; d=google.com; s=arc-20160816; b=cmLK6Rr8DXEQBRM1adjkYSac/alL/cGw+VtJq54/k2NQpnoa7xkctXat/hVOMNdqet JZz8pXpgj95JlS7ZhC0XsrUOA1V6vnXGox5hv2SrNii7UtfIMB7Gbi4H/dzIM9eoYQR5 qcn8qO6kg6QHvCh9JUNJ3cZ9B3l/F1c2REWFnfQql8Bpxq6HcR40I3+GACHSvRjGIuCS kWWXK2ibltqtAvvIWw7u86AE6XQpz3naHC7wJcAKriPVPiSlEhINSnmoakL6kj+SfgQK sKrXgL9Jt/DGBk6NNixOpWx/FLUBA8+WjvjOEY6xXHKij2/mcrE5Z1QLJkVl+kBND16J VBNw== 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=GuF3ebhf0Y2ZZdJ44j86HegNCYzAKV36pAha8tA7EMM=; b=CwnO6BqO+E0DannV3zvfPL1yETCKZsWloe/t9k7J7IV2A1x1UVGsqj7UCYev6bbSPz E+dxoiogwBQNPwF/IUDRfEzTHEQCuhQjD2F/UDP1N8bAvMFE4kulgWxP2F5/QFRs/zlI RwAcjLC1KkW5NxOVksVkStYG/9y2/cb0jb5VtU7Hb+fc2uiegiGnH2/hmMLEDrf3a67X PdXHwyTluPim8JbhlB3D5tDJEz+ch6+yeAD57dZskCcL+oSf4uN2lTs6mqf2DlB+KJ5Z fkC9cDrvDNpTZk1ff1w+5QIjMYiie5HSzFi6vQLpPvllpE6uVJ9ov5jFf7o5Kh4D4J/q ULHw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=IyOvyizb; 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 2adb3069b0e04-52ed2542d64sor450756e87.3.2024.07.14.00.10.55 for (Google Transport Security); Sun, 14 Jul 2024 00:10:55 -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; AJvYcCXcT45zkmWw0yxLr+DQpHnpYF98b3qQRrMRSrkwfPGSmNvujJIgnN4VJL1sfriZZ4NxuR9Lh7eNse6YKWI/e0uP4i0vcVfEhLWmWI2/TWsHDa+q X-Received: by 2002:a05:6512:3ca5:b0:52b:c33a:aa7c with SMTP id 2adb3069b0e04-52eb99d652emr12711446e87.65.1720941055435; Sun, 14 Jul 2024 00:10:55 -0700 (PDT) Received: from panicking.. (93-35-128-17.ip55.fastwebnet.it. [93.35.128.17]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a79bc820d52sm104950266b.214.2024.07.14.00.10.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jul 2024 00:10:55 -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 09:10:20 +0200 Message-ID: <20240714071023.10417-22-michael@amarulasolutions.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240714071023.10417-1-michael@amarulasolutions.com> References: <20240714071023.10417-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=IyOvyizb; 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 */