porting variable with double data type to linux kernel 2.6.9

Submitted by jpos_2008
on May 28, 2009 - 6:10am

Hi All,

I am currently doing porting work from CAVIUM based code to linux 2.6 kernel. As per my requirement I have to port the below piece of code to linux 2.6.9

----------------------------------------

void NHNPGetNTPTime(struct timeval strTv, unsigned long *ntpLsw, unsigned long *ntpMsw)

{

#ifdef CAVIUM
double dLsw;

*ntpMsw = htonl (((unsigned long)strTv.tv_sec + (unsigned long)NHNP_RTP_NTPTIMEOFFSET)); //2208992400UL

dLsw = (double)strTv.tv_usec / 1000000 * 65536 * 65536;
*ntpLsw = htonl ((unsigned long) dLsw);

#else

// TBD

/* Here we have to write code that will work for Linux 2.6 kernel. */

#endif

}
----------------------------------------
My main objective is how to convert the line” dLsw = (double)strTv.tv_usec / 1000000 * 65536 * 65536” , so that I will get the accurate NTPTime.

I will appreciate if I get help on this regard. Please provide some info on it.

Regards,
Jitendra

other ntp code

strcmp
on
May 28, 2009 - 2:10pm

there is ntp code in the kernel and in various ntp daemons, maybe you can have a look there.

but all the code seems to do is rescaling from the interval [0..1000000) to [0..2^32), you could just multiply by 2^32 / 1000000 = 4294.967296 to get the same result. as that would be a floating point number as well, you could first multiply by 2^32 (i.e. << 32) and then divide, but that requires long long type for the intermediate result and therefore do_div() for the division. you could stay 32 bit without excessive rounding by chopping the constants into smaller parts, i.e. ((((tv_usec*4192)/1000)*1024)/1000)*1024. you have to check yourself that the roundoff error is small enough, maybe you have to add constants to turn cutoff into rounding (x+500)/1000 instead of x/1000. the original code ignores that, though, casting to unsigned long doesn't round properly.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.