Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp1660178pxv; Sat, 10 Jul 2021 10:57:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzbq8Qcq2BSbu75iq1qWLVV4WL8DvCuRFBf5ExjRWxD2PlncysWZgy3z3kMRicopdwxwlBl X-Received: by 2002:a17:906:2590:: with SMTP id m16mr44411576ejb.265.1625939837475; Sat, 10 Jul 2021 10:57:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625939837; cv=none; d=google.com; s=arc-20160816; b=XkQpC7fBSbNA1+Fkx1SJRKUuacdwPdwuuMOgLYo6KLbCU1AVns0qvNlU4J/jj3wliz P34K5WT4fHmKy4HAX6at6kf2JjJ/iBRGMtukosh6EGRNfZfJplafFqbtZVxoNyiNHI9I E3mrlmUwsFepB5yPqLTGaD2SpKyDGSbQK40Y93sWAic27uh2oqeCvoNFNOlmG49zv3qq +SqgeEeCOlxiAow4kr5gnyhBEtEvdHkfdapx10X1yiOo1niHbpUDKq9dIfgdRE4ETSeC YMIS8NLrIdpeoWrmhperBdxviQzVQldnrF2NcqkXjyl9Sy8gRymoGkmNNwvVC36RZqyp mjng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:references:subject:cc:to:from :message-id:date:content-transfer-encoding:mime-version :dkim-signature; bh=sW9hZja3xQYTQGPj+6uAtbSZMYkpOa8syLZTN1hyNWA=; b=Hfg585axcWHlBgoBGFLvawkvv+l2W0Gzc5KBLCXKepfk+j7Tffoxab5xCGRb9g5OyB p0Tr9TuTLg0r/uSR04WgHWSRWPIt+2e3io0BRLp+YcsR9BPaogfGjkrgxC9Dxyb4xqG1 w1qIkp7R0vsejSKx8ZAVF8fw5wCvwgm96RfbEcNUdw+WAaTkyQqVa1Qi6BF0fwjLhV3c nqdcF99m+o67EqGGAtW7qMA0RfB0CrRi3JnrbC4j9g6PCTFVcU36+iePapTy3Rtrn3OE 5wq9z6n+auAKo4YfA1O/+ixF13KqNuS8N+5NIC+hKQt4w5lFNYn6fhx7Nu2k/CB45E3h ZuFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=l+idCI0a; 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 ec19si11739425edb.518.2021.07.10.10.56.53; Sat, 10 Jul 2021 10:57:17 -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=l+idCI0a; 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 S231293AbhGJRsc (ORCPT + 99 others); Sat, 10 Jul 2021 13:48:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229846AbhGJRsb (ORCPT ); Sat, 10 Jul 2021 13:48:31 -0400 Received: from mail-qv1-xf34.google.com (mail-qv1-xf34.google.com [IPv6:2607:f8b0:4864:20::f34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 082DDC0613DD; Sat, 10 Jul 2021 10:45:44 -0700 (PDT) Received: by mail-qv1-xf34.google.com with SMTP id j14so6212095qvu.6; Sat, 10 Jul 2021 10:45:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:content-transfer-encoding:date:message-id:from:to:cc :subject:references:in-reply-to; bh=sW9hZja3xQYTQGPj+6uAtbSZMYkpOa8syLZTN1hyNWA=; b=l+idCI0aRPJOkC41EHdwZME3sL9B2PrRibbecoP3Ug1v4iyVplD6bvnPCWq+JGxJRI uN9gVu4ES2JgRqxg+ttzKCyt52xiUJIgHp+b0xfyllHDjB5GQoJydmmFjKRBEOl/sd7N NkjVkEaZW/c22jQC/vu/Vdso+3M7jxVF2rJmE6ve55NAaFG5bkPbe2dGjFJZkFfaEqkD hnE1PtnYLbv4TSERPW5MKeRhpdfSVJcfABgxQnH/i9IXA7eQDdeqR+m5egjnesr1B4GD uwHQ2KcVJ51Rn95rb66KSDYPoM/xAkyHQjf5TtelTaDSucrL7Qiwki6LQ8lTjC9y7gb6 gDaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:content-transfer-encoding:date :message-id:from:to:cc:subject:references:in-reply-to; bh=sW9hZja3xQYTQGPj+6uAtbSZMYkpOa8syLZTN1hyNWA=; b=Q8zZGfFQPzcrwjkCMaDg0JhnrvU52qPLQ79P7G0k8smsUU9dngr5BOG2G0sumno62V BjTwRYfvqpU+61Oz2Mjk0QJT3DLjOVM0ghE+YLyC7Bqwe7H9OUvF2HFa/4S9Xem2uaAG wR2Kk7Gy8ubLmBRU+jCC0RdExm4wXGDhiWQOLFa+Vg3kxZj6j3NXHpAWvK8fjd8LrTVo PLUHe7yaQRQFrS0ApCH8LtDUSYYasVUM5uyBdaxDP9mi/G+/DqoPROBeQi8GAfr/fa/w 2EvVETWSWTK33+FiMvSKw2XW+Jp3RpZzdbeuHMpAuKCt0905gRFmmDpkkQ7xFXgKvntE PXnw== X-Gm-Message-State: AOAM531QLNFlfU6RvZIPIDd/jUVqYD/Rdr83AYEXd0JLbK/a2Lh938S2 SE2wraS8IsO7bFY4vrzSkOrgL8O3LxnkyQ== X-Received: by 2002:a05:6214:172:: with SMTP id y18mr16067392qvs.14.1625939144124; Sat, 10 Jul 2021 10:45:44 -0700 (PDT) Received: from localhost (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id n64sm4167120qkd.79.2021.07.10.10.45.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 10 Jul 2021 10:45:43 -0700 (PDT) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Sat, 10 Jul 2021 13:45:42 -0400 Message-Id: From: "Liam Beguin" To: "Peter Rosin" , , , Cc: , , , Subject: Re: [PATCH v4 05/10] iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support References: <20210706160942.3181474-1-liambeguin@gmail.com> <20210706160942.3181474-6-liambeguin@gmail.com> <4be51a74-9913-291a-9dac-422ac23da3ea@axentia.se> <353ceae9-ad7a-3175-d764-a9e590d3e8d3@axentia.se> In-Reply-To: <353ceae9-ad7a-3175-d764-a9e590d3e8d3@axentia.se> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat Jul 10, 2021 at 4:14 AM EDT, Peter Rosin wrote: > > > On 2021-07-09 21:30, Liam Beguin wrote: > > On Fri Jul 9, 2021 at 12:29 PM EDT, Peter Rosin wrote: > >> > >> > >> On 2021-07-06 18:09, Liam Beguin wrote: > >>> From: Liam Beguin > >>> > >>> Add IIO_VAL_INT_PLUS_{NANO,MICRO} scaling support. > >>> Scale the integer part and the decimal parts individually and keep th= e > >>> original scaling type. > >>> > >>> Signed-off-by: Liam Beguin > >>> --- > >>> drivers/iio/afe/iio-rescale.c | 8 ++++++++ > >>> 1 file changed, 8 insertions(+) > >>> > >>> diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-resc= ale.c > >>> index ba3bdcc69b16..1d0e24145d87 100644 > >>> --- a/drivers/iio/afe/iio-rescale.c > >>> +++ b/drivers/iio/afe/iio-rescale.c > >>> @@ -89,7 +89,15 @@ static int rescale_read_raw(struct iio_dev *indio_= dev, > >>> do_div(tmp, 1000000000LL); > >>> *val =3D tmp; > >>> return ret; > >>> + case IIO_VAL_INT_PLUS_NANO: > >>> + case IIO_VAL_INT_PLUS_MICRO: > >>> + tmp =3D (s64)*val * rescale->numerator; > >>> + *val =3D div_s64(tmp, rescale->denominator); > >>> + tmp =3D (s64)*val2 * rescale->numerator; > >>> + *val2 =3D div_s64(tmp, rescale->denominator); > >> > >=20 > > Hi Peter, > >=20 > >> Hi! > >> > >> You are losing precision, and you are not mormalising after the > >> calculation. > >=20 > > Can you elaborate a little on what you mean here? > >=20 > > Do you mean that I should make sure that *val2, the PLUS_{NANO,MICRO} > > part, doesn't contain an integer part? And if so transfer that part bac= k > > to *val? Hi Peter, > > Yes. On 32-bit, you will easily wrap, especially for PLUS_NANO. You'd > only need a scale factor of 10 or so and a fractional part above .5 to > hit the roof (10 * 500000000 > 2^32). > Right, That makes sense! > But I also mean that you are losing precision when you are scaling > the integer part and the fractional part separately. That deserves > at least a comment, but ideally it should be handled correctly. > Oh got it! Apologies, How did I miss that... All things considered, it might make sense to also implement the test case Jonathan mentioned [1]. I'll look into it. [1] https://lore.kernel.org/linux-devicetree/20210704173639.622371bf@jic23-= huawei/ > >> I think it's better to not even attempt this given that the results ca= n > >> be > >> really poor. > >=20 > > Unfortunatelly, I'm kinda stuck with this as some of my ADC use these > > types. > > Ok. Crap. :-) Can't agree more :-) Thanks, Liam > > Cheers, > Peter