From patchwork Fri Sep 13 09:55:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 3485 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id A8421412D2 for ; Fri, 13 Sep 2024 11:57:22 +0200 (CEST) Received: by mail-ej1-f71.google.com with SMTP id a640c23a62f3a-a8a8308cceasf297970666b.0 for ; Fri, 13 Sep 2024 02:57:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1726221442; cv=pass; d=google.com; s=arc-20240605; b=Cy75c9FlgWv5pIsDh9jfWp+yz2GT3XVPFnjqo2SjcHh7V+Y5acgMw7kDt8EiJFNH0W dVyAhmxrAuHGHD0g15yAl43d0NTLC2BJO7iYvBvkJsB886++8K/I43Bv9jwzNuEMBjNy lccni8k+VAfZc/i5+G/dAXEgzfX2m9Ueca6pmRwDFoyCrbH1ZCd6GO9ndT4c1SVGPihH UjOmBrCFsZs1QlPKuULjBBiqJURZo6lBqD/0ZM/kCWh5qrBAIPj4PZJAigy4nhNtyvCF mBxlY9b4AB+MdkGynL1jus2etATAvAKy4HPtiT59Qf4L0Wcm+gnGwmcTS5W1gEa8zOAp cYGA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=R4ZXZ97cT7BL3O7fWZjBbfjBa8+jsTaXXvNXmSOop5c=; fh=YKEyjnW9UZgrQOD5gGLkyScnrjVCaPnVSQys+JNiR4w=; b=NT77XN7x7g2C13bwp0U/IojHahpOdsS4y1TZ322CNKX6j82AayCZ1OW/mZM6QQTXfl QZ8CirZAfNoiUmjE7OsfVbCsDoAxeZNCWbn9QS/0qw4g8jYQSg8m2+BlP91Xzm3H4jPI BnOG0krZnr7+rERiGNC3FWdMAx/rgieRmxLcrOqcXQiAFR4JxOv8etvgOVoGQjSbIryW CUY6ocXw1w4anxcByAGuWERq7TqmU+4bgQknXYYLPk4kUBcx5ExLE5C1+AP0+urky1yH 2M7sKJdbGdskaS6HV5hDuMI3i5dB1VIxacI8eS8nOV28oQeEV/wKesQF4bhgKYwOPsMW z2QQ==; darn=patchwork.amarulasolutions.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b="S8/9bTFG"; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@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=1726221442; x=1726826242; 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=R4ZXZ97cT7BL3O7fWZjBbfjBa8+jsTaXXvNXmSOop5c=; b=XifkgMUjsClNRi+FT82YoadtirZWg/4Jy5idRuK5e4kfB3yaWfmbYMyGaGCSkXEbpp oig/ne6szN2Yzjm9BOHMKb5W8E2SZIVG6w38xJD87TSdJVEZ9KG4Z0mKejXaed/aZk8w HGOQlJbTIchfrYcYC+Ds3sZ/wQL3zeFwdxBxI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726221442; x=1726826242; 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=R4ZXZ97cT7BL3O7fWZjBbfjBa8+jsTaXXvNXmSOop5c=; b=qWbkV+MhodaOQGl1ffBE9lCNPERdFPq8lO+M1+1Zw6iI7eWQn3tHFsXM0R0sjPTohA FistOflUD/4cS92h0Sv0Z36lnHB+N7bf3G3NnMrCnbrdjGRmgI4qY1TzAvM5do6mcsgR L3ESd4VEi1BRoBFQASB5HUDJKGe1XXyWGk1kyss2nzBTOy39pTxWCJ6TsaUjrZRHiiju pcKWAm+1W2Rvr9Nc1JEmeyw2dfAtScdYzCPW5pdLzg+yf0SZs3gDfrt4K0gtoWU1VPsv MHo50V37+zPbBcKYLq7oTosdjizUwXdkCVmlmReqCk6oyIQY+yk658oI2eDDqBhPeY5C n/jw== X-Forwarded-Encrypted: i=2; AJvYcCU33jj+XGu92sG31kzRKsw3jHc0Z070/vKxZzbLfVw8/qkZSU8Q3JyoEdW3wKg0Rmw6hUUJbDdXwF6CnU2R@patchwork.amarulasolutions.com X-Gm-Message-State: AOJu0YzHBpcokDd/uhtrubmxQIgviJrVqcBeUovdSRoz3oF6ZB/lhVVs a/X3ybBVrwivEnm6cG4aoffy+6u6ZUP45j/fQQhpzlA2wA6Tet55RKqOruiA1L0Lhg== X-Google-Smtp-Source: AGHT+IHe8WhVfwZy14bOchBnHbUf9boSA8yyRy6FvblEqqNhcCwSnvoC5B/70xNXB5FVeCwSj9/9Vg== X-Received: by 2002:a05:6402:4144:b0:5c2:6f35:41af with SMTP id 4fb4d7f45d1cf-5c4018e5d05mr15887773a12.16.1726221442299; Fri, 13 Sep 2024 02:57:22 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a05:6402:3488:b0:5c2:1813:8ca8 with SMTP id 4fb4d7f45d1cf-5c414091549ls2898a12.2.-pod-prod-00-eu; Fri, 13 Sep 2024 02:57:21 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVhY324sA+KBiHRjbCYk9IZ0qjEpDmilVTW09M3h+ic8OKMbhJfonEyq01zlb/pWra9DFxoz9SjoVd6JXQk@amarulasolutions.com X-Received: by 2002:a17:907:1b20:b0:a90:1f60:7b2d with SMTP id a640c23a62f3a-a902a09a509mr587124666b.0.1726221440550; Fri, 13 Sep 2024 02:57:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1726221440; cv=none; d=google.com; s=arc-20160816; b=bgjUoZ67RL8CEKpfrsmLB/c4kmiT/pLYJK4Uxm+RGxksyPnQhugNKYILo5SozCc91B PpdZgg/D5dk5SxisUlPhsDgKDGSiYE89HGCv//ZmW0X2dgCclFc3yVWCDWM63C6zkSP+ RBzXsCSa1py9IE3RbOn1xpxV17qS1sE+ycDt0ib9XKgF68IaJRX5LKNY/VASFesrZ8K9 dUIUGe6KRt2I8pj0p3hKrujwQJck/Vmwof9EF1b5mhZz7lW88vXNQGIN21aMI30rtHxh Lk24Q38aqID5TTDrZBnuMWCuXzlM7a+v5tYnx87CXa/+3S3pJ5YmdliT0hMZcXIQ0p4J gIpg== 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=vW7qJEHzB7K7Vuq5gSZygAenGvtTILGG5LHRpzHYU+k=; fh=F0xh9+9UNQaRTOxSCAhdiXPHilWdCRXc/paLnYn7H8Y=; b=gQwEOWD46xVw3zKjahnueuBqjClFZ1rU5SdgvpHMoIZf7ltMLKPV7SpOEFVVzdjT4m 7wFMIMDpKvXxxyQ5likqtCCBysW1gzoKR/fAKI0k8+zWKx2qtrOvH1+cFZap/iWuYg8O YoTS7P81x+/Z+MKRhbPyawtMg/Z3r5Un4aNXxZh3Mp/3rsx2O0zYuF3eYNTBzDg8Xa/J +I/VrnN2fG4vGRlcVcP8c8fMQd4+bi05WJib2aiMWaNo96otc1Sdc5TJvMynpF8lNFV/ o/Fo4nFNLzAE3I1HYL4Fj7QIS5e+GCIsT9qV+HXC4YYm3u8vg8vDEznEqAhnG8QHih8M e/FQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b="S8/9bTFG"; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@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 a640c23a62f3a-a904760d336sor63341366b.1.2024.09.13.02.57.20 for (Google Transport Security); Fri, 13 Sep 2024 02:57:20 -0700 (PDT) Received-SPF: pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; X-Forwarded-Encrypted: i=1; AJvYcCXBRjKtyW2ogVwxzK+9exrxIVVJVnI0127l7bd8MPk7R0/uZ1LbTJLt7PrQGj/zNZRmRYGvQL6nzsBmFO3f@amarulasolutions.com X-Received: by 2002:a17:907:6e8f:b0:a7a:acae:340b with SMTP id a640c23a62f3a-a902a8f7d52mr575358666b.31.1726221439869; Fri, 13 Sep 2024 02:57:19 -0700 (PDT) Received: from dario-ThinkPad-T14s-Gen-2i.homenet.telecomitalia.it (host-87-4-102-18.retail.telecomitalia.it. [87.4.102.18]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a8d25cee72bsm863224766b.173.2024.09.13.02.57.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Sep 2024 02:57:19 -0700 (PDT) From: Dario Binacchi To: u-boot@lists.denx.de Cc: Fabio Estevam , linux-amarula@amarulasolutions.com, michael@amarulasolutions.com, Miquel Raynal , Dario Binacchi , Tom Rini Subject: [PATCH 17/26] lib: div64: sync with Linux Date: Fri, 13 Sep 2024 11:55:59 +0200 Message-ID: <20240913095622.72377-18-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240913095622.72377-1-dario.binacchi@amarulasolutions.com> References: <20240913095622.72377-1-dario.binacchi@amarulasolutions.com> MIME-Version: 1.0 X-Original-Sender: dario.binacchi@amarulasolutions.com X-Original-Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b="S8/9bTFG"; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@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: , From: Michael Trimarchi Sync with Linux commit fc4a0db4149af ("Merge tag '6.5'"). Signed-off-by: Michael Trimarchi Signed-off-by: Dario Binacchi --- include/linux/math64.h | 110 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/include/linux/math64.h b/include/linux/math64.h index eaa9fd5b9685..82bd7472a32c 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 */