Re: Porting float and double variable in linux kernel

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

 



On Thu, 28 May 2009 18:26:07 +0530
"Jitendra Pradhan" <jitendra.pradhan@xxxxxxxxxxxxxxxx> wrote:
> I am currently doing porting work from CAVIUM based code to linux 2.6
> kernel.

What is cavium?

> As per my requirement I have to port the below piece of code
> to linux 2.6.9

Ah, you must be an archeologist, linux-2.6.9 is dated October 18, 2004.
I guess it's an early bronze age kernel. I suggest to use a more recent
kernel, like 2.6.29.4.

[snip C code]

> 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.

Simple: you can't do floating point in the kernel. Period.

Given your calculation, you can do the same thing using integer math
anyway if you do it in the right order (remember multiplication and
division are assosiative operations):

#ifndef CAVIUM
# include <asm/div64.h>
#endif

long get_usec(struct timeval t)
{
  unisgned long long result;

  result = (unsigned long long)t.tv_usec * 65536ULL * 65536ULL;
#ifdef CAVIUM
  result /= 1000000;
#else
  /* not all architectures can do direct 64 bit divisions */
  do_div(result, 1000000);
#endif
 
  /* in theory this can overflow, but in practice tv_usec is between 0
     and 1000000 so it won't. the cast to long is safe */
  return (long)result; 
}

As a bonus, this code gets rid of floating point and probably gets less
rounding errors.

Now the only questions remains... Why do you want to do such NTP
calculations in kernel when it can (and already is) been done in
userland?


Regards,

Erik

-- 
Erik Mouw -- mouw@xxxxxxxxxxxx
GPG key fingerprint: D6AC 7F15 A26E C5C4 62E0  4A58 FCF9 551C 9B48 B68D

Attachment: signature.asc
Description: PGP signature


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux