Re: [Outreachy kernel] Re: [PATCH v3 3/4] staging: iio: adc: ad7192: get_filter_freq code optimization

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2020-03-25 at 23:08 +0530, DEEPAK VARMA wrote:
> [External]
> 
> On Tue, Mar 24, 2020 at 08:06:34AM +0000, Ardelean, Alexandru wrote:
> > On Mon, 2020-03-23 at 23:22 +0530, DEEPAK VARMA wrote:
> > > [External]
> > > 
> > > On Mon, Mar 23, 2020 at 01:15:31PM +0100, Stefano Brivio wrote:
> > > > On Mon, 23 Mar 2020 11:28:52 +0200
> > > > Andy Shevchenko <andy.shevchenko@xxxxxxxxx> wrote:
> > > > 
> > > > > On Mon, Mar 23, 2020 at 2:49 AM Stefano Brivio <sbrivio@xxxxxxxxxx>
> > > > > wrote:
> > > > > > On Mon, 23 Mar 2020 01:44:20 +0200
> > > > > > Andy Shevchenko <andy.shevchenko@xxxxxxxxx> wrote:  
> > > > > > > On Sun, Mar 22, 2020 at 9:57 PM Deepak R Varma <
> > > > > > > mh12gx2825@xxxxxxxxx>
> > > > > > > wrote:  
> > > > > > > > Current implementation of the function
> > > > > > > > ad7192_get_available_filter_freq
> > > > > > > > repeats calculation of output data rate a few times. We can
> > > > > > > > simplify
> > > > > > > > these steps by refactoring out the calculation of fADC. This
> > > > > > > > would
> > > > > > > > also
> > > > > > > > addresses the checkpatch warning of line exceeding 80
> > > > > > > > character.  
> > > > > > > 
> > > > > > > I'm not sure you did an equivalent changes. I believe in the
> > > > > > > original
> > > > > > > code precision is better. Consider low clock frequencies when 10
> > > > > > > bit
> > > > > > > right shift may hide some bits of the division.  
> > > > > > 
> > > > > > Note that those bits are eventually "hidden" in the same way
> > > > > > later,  
> > > > > 
> > > > > Even if mathematically (arithmetically) evaluation is correct, we have
> > > > > to remember that computers are bad with floating point and especially
> > > > > kernel, which uses integer arithmetic. That said, it's easy to get
> > > > > off-by-one error (due to precision lost) if we do big division before
> > > > > (not so big) multiplication.
> > > > 
> > > > That's exactly the point I was trying to explain below: swapping steps
> > > > in a sequence of DIV_ROUND_CLOSEST() (*not* of arithmetic divisions),
> > > > *should* not affect quantisation ("off-by-one") error.
> > > > 
> > > > I'm not entirely sure in this case, so a quick "demonstration" in
> > > > Python or suchlike as you suggested would be nice to have, indeed.
> > > > 
> > > > > > despite the different sequence, due to DIV_ROUND_CLOSEST() being
> > > > > > used
> > > > > > at every step (both before and after the change) without other
> > > > > > operations occurring.  
> > > > > 
> > > > > By the way, where AD7192_SINC3_FILTER and AD7192_SINC4_FILTER
> > > > > multiplications disappear and why?
> > > > 
> > > > Those were in fact divisions (multiplications of the divisor). Overall,
> > > > these steps are now arranged in a way closer to how they are presented
> > > > in the datasheet mentioned here (up to "Chop Enabled" paragraph, page
> > > > 26).
> > > > 
> > > 
> > > Thank you Andy and Stefano for your comments. Its very thoughtful. I am
> > > not much familiar with Python so far, but thinking on evaluating your
> > > suggestion in a sample c program. I will share the outcome shortly.
> > 
> > +adding Mircea Caprioru
> > 
> > Umm, this math-cleanup looks pretty dangerous.
> > If possible, I wouldn't change it.
> > At least without some testing on HW.
> > 
> > Maybe doing math-simulations in Python scripts would also work, but not
> > sure.
> > 
> 
> Hello All,
> I further reviewed current and proposed implementation of the
> get_filter_freq() function[Thank you Stefano for your time]. We realised that
> I
> was wrong in swapping DIV_ROUND_CLOSEST calls with mixing
> multiplication in it. It is indeed incorrect to mix multiplication if we
> want to reorder the calls. Comparison of the results from current and
> proposed implementation proved it. In short, the patch I sent is wrong.
> My apologies for any trouble.
> 
> We have further improved the test program with a revised implementation
> [attached with this email] and found that this revision appears to
> provide more accurate results [I think].
> 
> May I please request you to review the attached test program, verify the
> results and share your feedback.
> 
> Thank you for your patience and the opportunity to learn a few new
> things!
> 

Hey,

Many thanks for the test program.
I admit, it is a good way for convincing me [and my paranoia about changing math
in the ADI drivers]. I don't want to say that the math we did is the best, but
since it was tested... ¯\_(ツ)_/¯
[ Also, there's plenty of ADI drivers that we have to look at, so that also
makes me paranoid ]

I took a look and ran your program.
I like the improved results.

Only one suggestion I have for it; maybe use an extra variable for part of the
divisor; see here:

void new_func_get_freq1(struct adc7192_state *st, int *freq)
{
        unsigned int div;

        /* Formulas for filter at page 25 of the datasheet */
        div = AD7192_MODE_RATE(st->mode) * 1024;
        freq[0] = DIV_ROUND_CLOSEST(st->fclk * 240, div * AD7192_SINC4_FILTER);
        freq[1] = DIV_ROUND_CLOSEST(st->fclk * 240, div * AD7192_SINC3_FILTER);

        freq[2] = DIV_ROUND_CLOSEST(st->fclk * 230, div);
        freq[3] = DIV_ROUND_CLOSEST(st->fclk * 272, div);
}

if you want to you can go extra-further a bit and re-add the fadc for the first
2 frequencies; so something like:


void new_func_get_freq2(struct adc7192_state *st, int *freq)
{
        unsigned int div, fadc;

        /* Formulas for filter at page 25 of the datasheet */
        fadc = st->fclk * 240;
        div = AD7192_MODE_RATE(st->mode) * 1024;
        freq[0] = DIV_ROUND_CLOSEST(fadc, div * AD7192_SINC4_FILTER);
        freq[1] = DIV_ROUND_CLOSEST(fadc, div * AD7192_SINC3_FILTER);

        freq[2] = DIV_ROUND_CLOSEST(st->fclk * 230, div);
        freq[3] = DIV_ROUND_CLOSEST(st->fclk * 272, div);
}

either version is fine from my side;

Thanks
Alex


> Deepak.
> 
> > > Deepak.
> > > 
> > > 
> > > > -- 
> > > > Stefano
> > > > 




[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Input]     [Linux Kernel]     [Linux SCSI]     [X.org]

  Powered by Linux