Re: How to print out double precision number in the kernel using the printk

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

 



On Wed, Oct 31, 2001 at 09:41:04AM +0800, wanghg wrote:

> Hi, everyone:
> 
> How to print out double precision number like 1.0005 in the kernel using 
> the printk.
> 
> I am sorry ask such a simple question?

Just to clear up the confusion here:

You can't use floating point in the kernel.

Why? Because (on x86) there is an 'external' (nowadays it's on the same die as
the cpu though) FPU. This takes a while to do floating-point maths operations,
when you use 'float' or 'double' or a non-integral number in C, gcc produces
code to compute the values using the FPU. (You can usually tell an FPU assembler
instruction as they're prefixed by a 'f', fadd, fsub, etc).

Anyway, the FPU has several registers, and if a user-space process uses the FPU,
the kernel realizes and when the process is switched, the fpu's registers are
saved and loaded for the next process. The kernel doesn't bother to do this for
itself, as not many things that need floating point really need to be in the
kernel. If you use floating-point instructions in the kernel, you will 'corrupt'
the FPU registers for a user-space program.

There are several ways to work around this problem, firstly, as was suggested on
the earlier thread, you can just print out the integers and get a user-space
daemon to work out the results.

Another approach would be something like:

printk("%lu.%2u\n", jiffies / HZ, jiffies % HZ);

Which would print out the number of seconds the kernel had been up, on x86.
(Sorry, this will only work if HZ == 100).

You could also use 2 values to store the real bit and the decimal bit, and write
the code to increment, this would make division etc virtually impossible.

So, if you want floating-point, either go and buy a calculator or do it in
user-space.

-- 

Mark Zealey (aka JALH on irc.openprojects.net: #zealos and many more)
mark@zealos.org
mark@itsolve.co.uk

UL++++>$ G!>(GCM/GCS/GS/GM) dpu? s:-@ a16! C++++>$ P++++>+++++$ L+++>+++++$
!E---? W+++>$ N- !o? !w--- O? !M? !V? !PS !PE--@ PGP+? r++ !t---?@ !X---?
!R- b+ !tv b+ DI+ D+? G+++ e>+++++ !h++* r!-- y--

(www.geekcode.com)
-
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
IRC Channel:   irc.openprojects.net / #kernelnewbies
Web Page:      http://www.kernelnewbies.org/


[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