Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1101963pxy; Sun, 1 Aug 2021 12:41:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwh6t57/YkqUxbIX7egwt9I6/8ZqZOHrJXhLw2HVv8lx+ZNuhd1Tb5FJBsPvIC4bQ+AnwV8 X-Received: by 2002:a92:dad0:: with SMTP id o16mr2437618ilq.65.1627846896820; Sun, 01 Aug 2021 12:41:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627846896; cv=none; d=google.com; s=arc-20160816; b=v39FKmw1z7YO3etCLy+xiwaWf6JjwqojkvGpeK6U/PttoswFfRetaadfFmI7YoLlkO /8affea6Sy6+K8yxlFgg2e2VKyeCNcOA4UxVIOnTvmgX7ywUDk9+bYI4CRnM8yy7BPBO HcMouf+ogdNaoKi0TzVl+CVfvTzKWmOcT1t7ciELa8aKfe0TbZcT6XTnLhTTcCz0E1e9 N6YNeFP4qb4+Sg/auZ4vjaMT0VhfLrA3jphT2en76hXe0ql1Cla8Oex2CzlKK6AO3/EQ ebM1NxV4RnmwXkvdcf1u/D+3tjyF6KR+M+6ElofH5M9kXQNWVI5nPmy5vedL3+gdcj1n enTA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=r9noamx1Lea6uM4ckRlZuCQifseImk8SgpqIftuzmWw=; b=cbdSy7qGI6KnNciYsiXZbNpN1+Ky0G6XikV3rNkAYhZe1hfX5Bqt7UF73eVtOdrURd cehAvCaoofGO2npqcsEAyQQEzq/ZItpALEqQjkNaBILI0RkVon/X0tvEYx9jMItl6GQR UzYAhaDdjKZQgmJO/IwiUu9FUmg9Z5PmIH7fxsVI8jj5V6heU1EHa11/jNb8K+41G2rX WicPWtLgiS0W4Y/W359hu8MHYSfGK4t29LGevAY46jH2dVsbm93DrLznCo1gdEb7po2/ XCPxT9CruFtzfMnfWxWUTW6UurqYLAYGP/JrtTq6LzSYNm+GBWpL+1eMtERBReRZsYoK e3bA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=rU7aL0sX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q8si11373505ilo.95.2021.08.01.12.41.25; Sun, 01 Aug 2021 12:41:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=rU7aL0sX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232156AbhHATkm (ORCPT + 99 others); Sun, 1 Aug 2021 15:40:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231250AbhHATk1 (ORCPT ); Sun, 1 Aug 2021 15:40:27 -0400 Received: from mail-qv1-xf2c.google.com (mail-qv1-xf2c.google.com [IPv6:2607:f8b0:4864:20::f2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50F87C0613D3; Sun, 1 Aug 2021 12:40:19 -0700 (PDT) Received: by mail-qv1-xf2c.google.com with SMTP id d3so7920605qvq.6; Sun, 01 Aug 2021 12:40:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=r9noamx1Lea6uM4ckRlZuCQifseImk8SgpqIftuzmWw=; b=rU7aL0sXno4InxJ3qI7n0CaL6/QT223xMb5SMKSH1a/08QZWB6INzjeZq4RfQhJygg Agqkmj7nUPOL1+qwyrNvFPIWqfMhuyqhFs957ufw9ZzSYIhLWRXvHPYVCbSQaPsu3Csp Mg+kowc75abuhjnQI1PHaKcJg5XCwF0jIUCohL1gY/8N7jW7A6jzHc11ShTSoZo2DnNo 7GHDVmh8JaSO9AMpyNa3LLZyYHNdHUfsS4X4L0mSSQSMfvBnQqoEXHSVTVdUMllz1ijQ W7sYMzifcT9iM5kBzgZgMh7rGQ6BK82/Id2YrTXZcw8meXaTiidGqyThDHZDSk4amg4I htYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=r9noamx1Lea6uM4ckRlZuCQifseImk8SgpqIftuzmWw=; b=IoHGGryTgmWmvWZ8bjSc4F3KRSGWKZhWaiBSopiUe+Es29O0v/v08RhnKWAT0IBSgA f71v/dgKIKCdxLz0nkeJ/Krr/Iw4CuT1kWjOQmLsvAu9TnSfy/qvcz4rbKODdebhjycn 1IMAy7EhSQQ8WGsTbbxuI4UeWdFDPnhpMWeeJsQ3ysyO5m4t1azl+Ap+4+C4/dMbGNHX nSEK+lUS7xyI4Yy4ucxfWNLQWjG4ZoRyH3bbVteHj/1OoZpoPAfohCvQ+btX8dVQsQtk yoDoObBSGf5rLtT+L63gt+5Fs5UwUpQtxluahluLG2VxaKHC51yaMnJxDYQQ+G5MtIEL /NJg== X-Gm-Message-State: AOAM533f2r8Io+yxUHG/MamLk6XTx9NplBAIp1hLaQW4fvdKud5x0T/x Ka35h2OxY9woofWrbPSMNNQ= X-Received: by 2002:a05:6214:3001:: with SMTP id ke1mr13377381qvb.52.1627846818542; Sun, 01 Aug 2021 12:40:18 -0700 (PDT) Received: from shaak.. (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id f10sm3621606qto.31.2021.08.01.12.40.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 12:40:18 -0700 (PDT) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v7 08/13] iio: afe: rescale: fix precision on fractional log scale Date: Sun, 1 Aug 2021 15:39:55 -0400 Message-Id: <20210801194000.3646303-9-liambeguin@gmail.com> X-Mailer: git-send-email 2.30.1.489.g328c10930387 In-Reply-To: <20210801194000.3646303-1-liambeguin@gmail.com> References: <20210801194000.3646303-1-liambeguin@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Liam Beguin The IIO_VAL_FRACTIONAL_LOG2 scale type doesn't return the expected scale. Update the case so that the rescaler returns a fractional type and a more precise scale. Signed-off-by: Liam Beguin --- drivers/iio/afe/iio-rescale.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index abd7ad73d1ce..e37a9766080c 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -47,12 +47,17 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, *val2 = rescale->denominator; return IIO_VAL_FRACTIONAL; case IIO_VAL_FRACTIONAL_LOG2: - tmp = *val * 1000000000LL; - do_div(tmp, rescale->denominator); - tmp *= rescale->numerator; - do_div(tmp, 1000000000LL); + if (check_mul_overflow(*val, rescale->numerator, (s32 *)&tmp) || + check_mul_overflow(rescale->denominator, (1 << *val2), (s32 *)&tmp2)) { + tmp = (s64)*val * rescale->numerator; + tmp2 = (s64)rescale->denominator * (1 << *val2); + factor = gcd(abs(tmp), abs(tmp2)); + tmp = div_s64(tmp, factor); + tmp2 = div_s64(tmp2, factor); + } *val = tmp; - return scale_type; + *val2 = tmp2; + return IIO_VAL_FRACTIONAL; case IIO_VAL_INT_PLUS_NANO: case IIO_VAL_INT_PLUS_MICRO: if (scale_type == IIO_VAL_INT_PLUS_NANO) -- 2.30.1.489.g328c10930387