Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1130433pxb; Tue, 8 Feb 2022 09:55:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJyIFgI5Qh9zen7xTa/Q25f5gIqwoJ4KXY4IYnofWJ4dswMWC5RCB8qMC4npjLHFHM6IX/9s X-Received: by 2002:a05:6402:2789:: with SMTP id b9mr5821922ede.358.1644342942463; Tue, 08 Feb 2022 09:55:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644342942; cv=none; d=google.com; s=arc-20160816; b=EvSUYC9gBagrTsbUhsal85kyNtk40liGh/aW8ikmVQS2Z8/0guCRS6q92FASZotd7P mf1ZgRYsAUZJ7CoOtL31vFLT//TRoYywzp7QXgNROwNOOgR1+Gp9N/yEZtQIN4e7in2W Zv//FqHa0ctiu97fRf7Z6yygHh3sCF1Uhae5WCj/86x+Xkz+GhrszmxxJRyU97VMnoWV VLa003PcVRwbwOdZy4A9Xq3iWmri8ZsaHKKlg7IlBj7Pl7oDDe01KZyP6jbAmVLx4TPf IxAHN9+CsvwybzP3KraBi6ryAp+E38w7/ij4YSdEjB4oc61g4J1BPOzXutW5ALAtJCxq IaAg== 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=9LZuh2BJBRJTUtNvsar+NW2892hNjIAPPNUSFSiWG50=; b=mSApNOcHFEFQbYpI5Vs3YC1CxZIvzqjYKGEQdmNt4JVI3w5Ar7XjI8uV7V66XwLsd5 8Dlqz5mCCyDhvtQyV1eNTf2NeE3Nz5XSmrVqkohpzCAPO51cFukRMh6v0jyAj6HxpYWe kMKB6X3+08BpMzmc3fUakelhBEDXaqeLlmq3ujs4lK7UJu11zjQYK/q+w8Z6NWWOMHTA qK78jPsix20VVN48oGpIJlqyOYmecQcNTsWWobuYT4zPYD4swg7kxRxgIncbmWWlnw+G YnC+Jm9Ow45afBZzSNcH7jFX5VdiJzuwunBZh0RKB1FXZ4do3VIkFJvmq7GhKaTnwG+P L1gw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=SvlM8ahO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d8si9149194edt.642.2022.02.08.09.55.17; Tue, 08 Feb 2022 09:55:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=SvlM8ahO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S238187AbiBHCJS (ORCPT + 99 others); Mon, 7 Feb 2022 21:09:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345422AbiBHCEx (ORCPT ); Mon, 7 Feb 2022 21:04:53 -0500 Received: from mail-qv1-xf32.google.com (mail-qv1-xf32.google.com [IPv6:2607:f8b0:4864:20::f32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25B06C061355; Mon, 7 Feb 2022 18:04:53 -0800 (PST) Received: by mail-qv1-xf32.google.com with SMTP id g3so8328651qvb.8; Mon, 07 Feb 2022 18:04:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9LZuh2BJBRJTUtNvsar+NW2892hNjIAPPNUSFSiWG50=; b=SvlM8ahOM9nO7PoAOfmwocpFkfx5v04cPdoCLLTqiwy7Pnj/xmMBw8GzJjDEn3MxBr /nTL3iNY2F5fQyA1Ot38HdqJth6eXJfzTl0ab9rpzukqcg3yMfIQvcqJim06lQ03UAk1 yUnshwQOa3HRZxwkgR3Bx/vvqe6POOp/SaPLFGWGKnu3bH5a6vU9rjKsd/4bAI90vcAa l51wLrPuLQ7DgzReQzngZXM98X8+ll7r1aoBP7Qbmb0sNWa9dcvkMDPacFssx4eM6I88 K9PJ04+wc9/fjtnkkzybJQP3PBKqV3EEXpmuUtlHVow9hHBiXHCCnBlJIRPxrEURuA8o pu6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9LZuh2BJBRJTUtNvsar+NW2892hNjIAPPNUSFSiWG50=; b=l7D4ubDQ77a1sqHatYO8DWzJVbqyIrlPGN4U9vlvRJhDJe5wpYCQdMp9OccVFr8D0Z Hyvy9+sRELveNSV3aLGEte8QsplbEB5AOoBQ0sG499BgWHnFYa9a7pNELAANFuKrz6qh 9rhAMiZQKwGMs5QgYqjgePXJ6niNzUcWk/irVZBGI6l1ZeR9x4JZEowDQ2S79ENvGIzh 4WenTHWyxfy3LBGnU80NW2SwHnrp3ULKIXVeTGNcvvYZpPDg3Je06uZtW2vHjbvm1xn1 9BkSWD7PcmeotzfdPi6AHgLiIVQoKxYRXpc5EeJjUY1GiPN7bu8WIOXO08YGfOkN79Vl 2PtQ== X-Gm-Message-State: AOAM5315ZapTG1lEm2pfU+W80teO9ROlgI4TPhSJoUAjWDnrpBoLIAvA hCMnRpBdmshA9tc0SI2T0aY= X-Received: by 2002:a05:6214:27e9:: with SMTP id jt9mr1606975qvb.65.1644285892345; Mon, 07 Feb 2022 18:04:52 -0800 (PST) Received: from shaak.. (modemcable055.92-163-184.mc.videotron.ca. [184.163.92.55]) by smtp.gmail.com with ESMTPSA id u9sm1416055qko.130.2022.02.07.18.04.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 18:04:51 -0800 (PST) From: Liam Beguin To: liambeguin@gmail.com, peda@axentia.se, jic23@kernel.org, andy.shevchenko@gmail.com, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org Subject: [PATCH v14 04/11] iio: afe: rescale: fix accuracy for small fractional scales Date: Mon, 7 Feb 2022 21:04:34 -0500 Message-Id: <20220208020441.3081162-5-liambeguin@gmail.com> X-Mailer: git-send-email 2.35.1.4.g5d01301f2b86 In-Reply-To: <20220208020441.3081162-1-liambeguin@gmail.com> References: <20220208020441.3081162-1-liambeguin@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The approximation caused by integer divisions can be costly on smaller scale values since the decimal part is significant compared to the integer part. Switch to an IIO_VAL_INT_PLUS_NANO scale type in such cases to maintain accuracy. Signed-off-by: Liam Beguin Reviewed-by: Peter Rosin --- drivers/iio/afe/iio-rescale.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 63035b4bce5e..468e6c345bd1 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -25,7 +25,7 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, int *val, int *val2) { s64 tmp; - s32 rem; + s32 rem, rem2; u32 mult; u32 neg; @@ -44,9 +44,23 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, tmp = (s64)*val * 1000000000LL; tmp = div_s64(tmp, rescale->denominator); tmp *= rescale->numerator; - tmp = div_s64(tmp, 1000000000LL); + + tmp = div_s64_rem(tmp, 1000000000LL, &rem); *val = tmp; - return scale_type; + + if (!rem) + return scale_type; + + tmp = 1 << *val2; + + rem2 = *val % (int)tmp; + *val = *val / (int)tmp; + + *val2 = rem / (int)tmp; + if (rem2) + *val2 += div_s64((s64)rem2 * 1000000000LL, tmp); + + return IIO_VAL_INT_PLUS_NANO; case IIO_VAL_INT_PLUS_NANO: case IIO_VAL_INT_PLUS_MICRO: mult = scale_type == IIO_VAL_INT_PLUS_NANO ? GIGA : MEGA; -- 2.35.1.4.g5d01301f2b86