Re: 32-bit IEEE float multiplication
Andy wrote:[color=blue]
>
> Eric,
> My goal is to provide a generic timing device which would
> provide accuracy (although not exact) from days down to
> milliseconds. The idea is to have a free running 32-bit
> timer (tick) that all others compare for timing. I'm using
> multiplication because the idea is to not limit the
> free running tick counter to 1 frequence (as in my previous
> examples 4 milliseconds). Maybe a concrete example would help.
>
> typedef unsigned long GCLK_T;
> GCLK_T gzFreeTicks; /* this gets incremented in an ISR */
> GCLK_T GCLK(void); /* provides atomic read of gzFreeTicks */
>
> #define SECS_PER_TICK 0.004 /* seconds for each tick */
> #define MSECS_TO_TICKS( MSECS) xxxx /* converting milliseconds to ticks */
>
> GCLK_T ElapsedTicks(GC LK_T ticks) {
> return(GCLK() - ticks);
> }
>
> unsigned long ElapsedSecs(GCL K_T ticks) {
> return((float)E lapsedTicks(tic ks) * (float)(SECS_PE R_TICK));
> }[/color]
Since you only care about the whole seconds, why bother
with floating-point at all?
unsigned long ElapsedSecs(GCL C_T ticks) {
return ticks / TICKS_PER_SEC; // new constant
/* or, if you want rounding: */
return (ticks + TICKS_PER_SEC / 2) / TICKS_PER_SEC;
}
[color=blue]
> /* has a endless loop with one 100 millisecond task */
> void main(void) {[/color]
This is the first time I've seen `void main' used
properly.
--
Eric.Sosman@sun .com
Andy wrote:[color=blue]
>
> Eric,
> My goal is to provide a generic timing device which would
> provide accuracy (although not exact) from days down to
> milliseconds. The idea is to have a free running 32-bit
> timer (tick) that all others compare for timing. I'm using
> multiplication because the idea is to not limit the
> free running tick counter to 1 frequence (as in my previous
> examples 4 milliseconds). Maybe a concrete example would help.
>
> typedef unsigned long GCLK_T;
> GCLK_T gzFreeTicks; /* this gets incremented in an ISR */
> GCLK_T GCLK(void); /* provides atomic read of gzFreeTicks */
>
> #define SECS_PER_TICK 0.004 /* seconds for each tick */
> #define MSECS_TO_TICKS( MSECS) xxxx /* converting milliseconds to ticks */
>
> GCLK_T ElapsedTicks(GC LK_T ticks) {
> return(GCLK() - ticks);
> }
>
> unsigned long ElapsedSecs(GCL K_T ticks) {
> return((float)E lapsedTicks(tic ks) * (float)(SECS_PE R_TICK));
> }[/color]
Since you only care about the whole seconds, why bother
with floating-point at all?
unsigned long ElapsedSecs(GCL C_T ticks) {
return ticks / TICKS_PER_SEC; // new constant
/* or, if you want rounding: */
return (ticks + TICKS_PER_SEC / 2) / TICKS_PER_SEC;
}
[color=blue]
> /* has a endless loop with one 100 millisecond task */
> void main(void) {[/color]
This is the first time I've seen `void main' used
properly.
--
Eric.Sosman@sun .com
Comment