From patchwork Thu Jan 4 09:34:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 13510943 Received: from mail-lj1-f178.google.com (mail-lj1-f178.google.com [209.85.208.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A2729208D5; Thu, 4 Jan 2024 09:34:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MLr3iUu+" Received: by mail-lj1-f178.google.com with SMTP id 38308e7fff4ca-2cca5d81826so4127461fa.2; Thu, 04 Jan 2024 01:34:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704360888; x=1704965688; darn=vger.kernel.org; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=wwPJBvFUuJ82VNrrULxCE0KnzFUUxsKXN3QUl2P63Vk=; b=MLr3iUu+IgjnqFmbzwK5uEGRvNfetWt0CuYJr9CWkqdMqovJWeXUE+gtki7wX9BGPm acxrPA+PLfJ+av/T/IY7BU/2TSHq0Ow+z82JZWtDooN82A7weX0vywDqs7Y11BYMFQkh EjrTz6+dy95sC1uHbJibAKaJDCyWEmFQN0enauZVJEPCta8jLgtQDKRzKggTm+6DOPOD d7hNVnxe74jH5BhxYrH12atBMxzdGfusHfL7T4DlQvUxrOdE4Q+3YqwbKdts6GwwQp1q 7aGG9ps7iCQz8maXBQEFGDWarjMKBkXQTQhUgqA+iSWuRZORt0mkhbiQC8W+JhQjnFHW Ro0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704360888; x=1704965688; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wwPJBvFUuJ82VNrrULxCE0KnzFUUxsKXN3QUl2P63Vk=; b=A2HnVciNDJwywSQEbvcK2eiQ3UR4S8rTGhihCMP9hIB7DAWl9jy+RSiryd53iy3Akm EG6N4hCRkx4PcsTTt/r1J8Jb6EIYNu+87QMONHM8+ZcSRzUsIZP9sY3HnnqlpwMQbV5o DOMaQq53xuuAugAqZ7v3OjxMhCJqTXfmJ5q/J2cSIJbBXJKo7RAsCcQLyMRfaWmp4Y35 R9ttZVlsn9gG/BK7UqNCCBa/pvLD260iUObdjMzNM04HROzQsykl/5rgaYNDmFT6mlCq 3YsU0ZzAwXvnwxKF17MOYG6ojGogF8EkQC3+Htolytc6L9mjtKPTeOAg4TgWwQ8uIL0f EPGQ== X-Gm-Message-State: AOJu0YwtTUAcVbuUfeM2bLOvwZyhKE03fblodKbuGIVNaisJIxriBXHv Uy5Vo4MY7NS0PdSuhr4oFKM= X-Google-Smtp-Source: AGHT+IG+EjNeEQH8XAZ58l6P7Y1N2m9ZQ6s2V/eWis8olnOxfoxwbrN3hnVFPLdz1HHGTZ8CWByd7w== X-Received: by 2002:a2e:a405:0:b0:2cc:a253:a4a4 with SMTP id p5-20020a2ea405000000b002cca253a4a4mr201837ljn.21.1704360887340; Thu, 04 Jan 2024 01:34:47 -0800 (PST) Received: from dc78bmyyyyyyyyyyyyydt-3.rev.dnainternet.fi (dc78bmyyyyyyyyyyyyydt-3.rev.dnainternet.fi. [2001:14ba:16f8:1500::7]) by smtp.gmail.com with ESMTPSA id g14-20020a2e9cce000000b002ccbc2a0d43sm4998488ljj.47.2024.01.04.01.34.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 01:34:46 -0800 (PST) Date: Thu, 4 Jan 2024 11:34:28 +0200 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Jonathan Cameron , Lars-Peter Clausen , Matti Vaittinen , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] iio: gts-helper: Fix division loop Message-ID: Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline The loop based 64bit division may run for a long time when dividend is a lot bigger than the divider. Replace the division loop by the div64_u64() which implementation may be significantly faster. Signed-off-by: Matti Vaittinen Fixes: 38416c28e168 ("iio: light: Add gain-time-scale helpers") --- I've implemented also a fixup series for supporting rounding of gains/scales: https://lore.kernel.org/lkml/37d3aa193e69577353d314e94463a08d488ddd8d.1701780964.git.mazziesaccount@gmail.com/ That series does also remove the offending loop. We don't currently have any in-tree users of GTS helpers which would need the rounding support so pushing the rounding is not urgent (and I haven't heard of Subjahit whose driver required the rounding). Hence, we may want to only take this loop fix in for now (?) and reconsider rounding when someone need that. Jonathan, what's your take on this? drivers/iio/industrialio-gts-helper.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) base-commit: 2cc14f52aeb78ce3f29677c2de1f06c0e91471ab diff --git a/drivers/iio/industrialio-gts-helper.c b/drivers/iio/industrialio-gts-helper.c index 7653261d2dc2..abcab2d38589 100644 --- a/drivers/iio/industrialio-gts-helper.c +++ b/drivers/iio/industrialio-gts-helper.c @@ -34,7 +34,7 @@ static int iio_gts_get_gain(const u64 max, const u64 scale) { u64 full = max; - int tmp = 1; + int tmp = 0; if (scale > full || !scale) return -EINVAL; @@ -48,8 +48,7 @@ static int iio_gts_get_gain(const u64 max, const u64 scale) tmp++; } - while (full > scale * (u64)tmp) - tmp++; + tmp += div64_u64(full, scale); return tmp; }