LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   delay calculation for LSB in timer (https://www.linuxquestions.org/questions/programming-9/delay-calculation-for-lsb-in-timer-830412/)

tkmsr 09-05-2010 01:13 AM

delay calculation for LSB in timer
 
On the following link

Following code is given
Code:

loopbit = loops_per_jiffy;
/* Gradually work on the lower-order bits */
while (lps_precision-- && (loopbit >>= 1)) {
loops_per_jiffy |= loopbit;
ticks = jiffies;
while (ticks == jiffies); /* Wait until the start
of the next jiffy */
ticks = jiffies;
/* Delay */
__delay(loops_per_jiffy);

if (jiffies != ticks)
/* longer than 1 tick */
loops_per_jiffy &= ~loopbit;
}

I am not clear with how
Code:

loops_per_jiffy |= loopbit;
OR operation above and
Code:

loops_per_jiffy &= ~loopbit;
AND with Inverse help to achieve the desired result.

Mara 09-05-2010 01:41 PM

The first one sets the bits in loopbit, the second operation clears them. The whole operation tries to set the delay, if the current configuration doesn't work, it doesn't use it anymore. Try to work it out using some example numbers.

tkmsr 09-05-2010 02:47 PM

Yea I was able to do it with example.I will mention my observation if some once come here by chance should help them.

lets assume

Code:

loops_per_jiffy = 1011 0010
Code:

loopbit = loops_per_jiffy;
So loopbit = 1011 0010

Code:

/* Gradually work on the lower-order bits */
 while (lps_precision-- && (loopbit >>= 1)) {

no idea of what lps_precision is may be some sort of check.

loopbit = loopbit/2 by above operation so new value of loopbit is
Code:

loopbit = 0101 1001
Code:

loops_per_jiffy |= loopbit;
Now due to above
Quote:

loops_per_jiffy = loops_per_jiffy | loopbit
so the result of an OR operation
Quote:

loops_per_jiffy = 1011 0010 OR 0101 1001 = 1111 1011
which is higher than the initial value of loops_per_jiffy

Code:

ticks = jiffies;
simple

Code:

while (ticks == jiffies); /* Wait until the start
is also simple if due to some calculation you are in mid of jiffy interval then
it will make sure that as
Code:

tick==jiffies
will come out of loop only
at the start of jiffy

Code:

of the next jiffy */
 ticks = jiffies;

simple

Code:

/* Delay */
 __delay(loops_per_jiffy);

Will delay for the value loops_per_jiffy

Code:

if (jiffies != ticks)
 /* longer than 1 tick */
 loops_per_jiffy &= ~loopbit;

here comes the magic if it is not satisfying the if condition then
will go back divide the loopbits by 2 as a result of >> (right shift)
and the OR operation will make sure that the value which is divided by
2 of loopbits gets added to loops_per_jiffy
will enter the delay loop and in this case if this time the "if"
condition is true then
the result of
Quote:

loops_per_jiffy &= ~loopbit
is
Quote:

loops_per_jiffy = loops_per_jiffy AND (~loopbit)
where taking complement of loopbit as above makes it negative (not
sure on this part)
and the result is the value is subtracted (as it was more than required value)

and then loopbit is again divided so added and this cycle will continue until
exact value of loops_per_jiffy is calculated.
Code:

}
but if there are more examples of such a calculation then let me know.
I just came across this one.


All times are GMT -5. The time now is 07:24 AM.