Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp423727pxb; Tue, 1 Feb 2022 03:00:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJyDHLsdxHkcGgUWYfgZzejHw5pWRBS3Ztb+ayhObUo7wu3oYl6khK3wHhoHVLLc5kEZ2eFM X-Received: by 2002:a17:907:2ce3:: with SMTP id hz3mr20125506ejc.361.1643713247929; Tue, 01 Feb 2022 03:00:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643713247; cv=none; d=google.com; s=arc-20160816; b=nqIhkmF5pprKARTz6vVS/BHnps+gYyMXiLXfdFu3aAbh2UUN5ITJCqj3PnJnMieq46 /dU0KAmmQ9h9mxpBNdxknyYEd8OWTYcWYOMT8jWOVd6PGj5hfT7nN1q1vj+Sb8TIfFGl 6FVcBp2e1Zfg/BMTJwYFMII9+K6Kye2mz7S400JJjMUQ4WSpj8qnC+g1t3s6m+FH4/kz K6oHAvo9MZMKyHkolU/SQyYlQ6SxmbjcHA+59zj1KbQeF2SaDyhNVyQCGr5DeDSPrzfp wR1JECC50myH3+rnvKqpvs36hehv7/XNuCEPqeeO9FlQrAEagTanKhqRYPRsowBx3S6g 3EIA== 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=QupiRAY/OkgsAeqJhxYxj7p2ygKMQjMx9I3zAYF6M5LOW1QnWvjHESsFEy1h/LTFHq YHeSssVReIZMLCVUS6GEmH4m7v/cy3RPnGcoelgzvAP+5MHebbNd/Og936ov8DFGVU4U EFxZVQ0R5uD5V6yFdIkMJmU2d+2XX/o5TRk+pDf4YlXpzJ3p14oxTcoIpc7bvNQOyqGj Y9u3xF52w5a4ymX1Pi6tmZQa/d9trtyF+t5wXhRpC3IEYAlR5OHcz5nPGSSHmcP8ePfV Wr8aa8gTyLW8ngnsM/hx8P5GAqMBSR6SfJSK1IxtwKq3X/DLzpgLh3vyzvQic9awX68t zGnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=dpnZO3KT; 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 w12si10658439edj.235.2022.02.01.03.00.23; Tue, 01 Feb 2022 03:00:47 -0800 (PST) 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=20210112 header.b=dpnZO3KT; 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 S1355535AbiA3QLa (ORCPT + 99 others); Sun, 30 Jan 2022 11:11:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355486AbiA3QLW (ORCPT ); Sun, 30 Jan 2022 11:11:22 -0500 Received: from mail-qk1-x72c.google.com (mail-qk1-x72c.google.com [IPv6:2607:f8b0:4864:20::72c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 697ECC061749; Sun, 30 Jan 2022 08:11:22 -0800 (PST) Received: by mail-qk1-x72c.google.com with SMTP id o25so9939440qkj.7; Sun, 30 Jan 2022 08:11:22 -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=dpnZO3KT6kmPArsuYNYs+ZMNl7/HALPgkEoI+18SEIBTN3uAAqWXX62f5X/7pdv/wC PmDyi4G812jtgi5vjOloMi1WggowQ7qynEiOBPzqB3Hu6c1PCkDMQAnXzZxkllKy92rp 6wz2ADR1FfDF6M5U0VK9WJciSdnU6eH2B9CLmQtJaHOGlZu7SD0O2s5fcGL7rIhIRuY9 msKyDyUAnV7/9RkxElf6rdkRK62rzb+T4pYq0M/LnDPaEcaiDzRmsi2KI6NT7B5UpnaC 9r0rNu3ePOGAxm9jxXykx++b6miKyjcxvqLaZ3riMu6ZS0K6k8govYoOPNqEhk3nSdZK raUw== 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=POaraYi/0DNPHmGfV5NHI72WzQkszebqx1T7g9j7HMPrKg6H+XTpVQy+awG+8QvWrA szoKQfu7IcI16fwwAsSCBfvOD6PydBPHFGEZEz/MOGnBYtppU/Djf0vqlsAYaHYZ5Di1 eGz4cyrV9kE3aPcudglWqTGV3MpHFkC5EibQDJE5hPYTD2lBLQkkuyXXMDXLqCSNuYOW m590z5szJgrqD+jMuo8L4xYuoz7fc/AwND9Uh33YjgSuc+8gf7ui8KCEp9H2Rx/tVTKn Rb1sJK2NPN3iXfKHS5gD79Ea//YUP8boFYZZNpfx3Sis9j5bSZnZGhDTJD9QOvJtFnKu RCMA== X-Gm-Message-State: AOAM530rU1hAEylmY/AEQEqqsJky8aLifkbNp0FgsZzVX5bMp/krnR5g BpfzxhA+ORPViN3sgg+V8Ns= X-Received: by 2002:a05:620a:4404:: with SMTP id v4mr10779489qkp.250.1643559081627; Sun, 30 Jan 2022 08:11:21 -0800 (PST) Received: from shaak.. (modemcable055.92-163-184.mc.videotron.ca. [184.163.92.55]) by smtp.gmail.com with ESMTPSA id v73sm6906148qkb.51.2022.01.30.08.11.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Jan 2022 08:11:21 -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 v13 04/11] iio: afe: rescale: fix accuracy for small fractional scales Date: Sun, 30 Jan 2022 11:10:54 -0500 Message-Id: <20220130161101.1067691-5-liambeguin@gmail.com> X-Mailer: git-send-email 2.35.1.4.g5d01301f2b86 In-Reply-To: <20220130161101.1067691-1-liambeguin@gmail.com> References: <20220130161101.1067691-1-liambeguin@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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