This is the mail archive of the
ecos-discuss@sources.redhat.com
mailing list for the eCos project.
Re: clock() problem?
- To: ecos-discuss at sourceware dot cygnus dot com
- Subject: Re: [ECOS] clock() problem?
- From: Hugo Tyson <hmt at redhat dot com>
- Date: 15 Nov 2000 13:42:31 +0000
- Newsgroups: cygnus.sourceware.ecos.d
- Organization: Red Hat UK
- References: <00111508233800.28506@chouston.logikos.com>
Chris Houston <chouston@logikos.com> writes:
> Has anyone else encountered strange behaviour when using the "clock()"
> function? When I use it, the result seems to roll over int 16 bits.
>
> In packages/language/c/libc/time/v1_x_x/src/clock.cxx, in the clock()
> function, if I change
>
> clocks = ((clock_t)curr_clock * resolution.dividend) /
> (resolution.divisor * (1000000000 / CLOCKS_PER_SEC));
>
> to
>
> clocks = (clock_t)((curr_clock * resolution.dividend) /
> (resolution.divisor * (1000000000 / CLOCKS_PER_SEC)));
>
> then the function seems to behave correctly. It seems that in
> the first case, the multiplication in the numerator gets truncated to
> 32 bits.
Overflow bites, I guess. Or maybe a more liberal sprinkling of casts is
needed to make all the maths be 64 bit.
This really needs rewriting to use the C++ clock conversion facility in the
kernel; look for the word "converter" in clock.hxx and also see the
clockcnv.cxx kernel testcase. Clock converters split
foo = bar * A / (B * (10^9/CLOCKS_PER_SEC))
into
foo = bar * P / Q * R / (S * (10^9/CLOCKS_PER_SEC))
where PR==A and QS==B so as to retain accuracy whilst avoiding overflow.
Contributions welcome ;-)
- Huge