Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

arithmetic functions for linux driver

256 views
Skip to first unread message

John Bradford

unread,
Mar 17, 2004, 1:10:13 PM3/17/04
to VINOD GOPAL, linux-...@vger.kernel.org
> I need to use the arithmetic functions like sin, cos,
> exp, log, etc in linux device driver.
>
> On search read , not to use libm from kernel driver
> as it will harm the fpu registers.
>
> Do you have any insight how to support these
> functions in linux driver or any code that is
> available which I can make use of?

What exactly are you trying to do? Why not simply create a look up
table for the functions you need, if the expected range of input
values is small?

John.
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majo...@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Chris Friesen

unread,
Mar 17, 2004, 1:20:24 PM3/17/04
to VINOD GOPAL, linux-...@vger.kernel.org
VINOD GOPAL wrote:
> Hi,
> I am new to linux world and this mailing list.

>
> I need to use the arithmetic functions like sin, cos,
> exp, log, etc in linux device driver.
>
> On search read , not to use libm from kernel driver
> as it will harm the fpu registers.
>
> Do you have any insight how to support these
> functions in linux driver or any code that is
> available which I can make use of?

Best would be to try and have userspace do it for you.

If you really need to do it in the kernel on the fly, you'll need to either

1) save and restore the floating point context yourself
2) use fixed point calculations

As an optimization for 2, you could pre-calculate some lookup tables and
interpolate.


Chris


--
Chris Friesen | MailStop: 043/33/F10
Nortel Networks | work: (613) 765-0557
3500 Carling Avenue | fax: (613) 765-2986
Nepean, ON K2H 8E9 Canada | email: cfri...@nortelnetworks.com

Richard B. Johnson

unread,
Mar 17, 2004, 1:36:28 PM3/17/04
to VINOD GOPAL, linux-...@vger.kernel.org
On Wed, 17 Mar 2004, VINOD GOPAL wrote:

> Hi,
> I am new to linux world and this mailing list.
>
> I need to use the arithmetic functions like sin, cos,
> exp, log, etc in linux device driver.
>
> On search read , not to use libm from kernel driver
> as it will harm the fpu registers.
>
> Do you have any insight how to support these
> functions in linux driver or any code that is
> available which I can make use of?
>

> thanks
> vinod

There are at least three problems.
(1) The kernel does not save and restore FPU registers across
system calls, only context-switches. This means that you will
probably break some user-mode code that is using the FPU.
You could, of course, save then restore the FPU state in your
code. However, there are other problems, read on.

(2) Some versions of the GCC compiler rely on the floating-
point library and do not generate FPU instructions in-line.
You absolutely-positively cannot link your kernel code against
such a library. The shared libraries don't even exist from
within the kernel, and the static library assumes user-mode
code.

(3) The use of FP within the kernel means that you have
lost the reason for a kernel module. Any such code must
execute in user-mode. The Unix/Linux way involves a
task called a daemon. Such a task does all the user-mode
stuff like floating-point math and file-I/O. It interfaces
with your driver either through the POSIX API (open/close/read/write),
/proc, or sockets.

A Unix/Linux "driver" is NOT anything like a Windows "driver".
Most Windows drivers are not drivers at all, but Libraries!
Such Libraries run in user-mode. Making such a library
in Linux is easy, just compile your library-code and link
against it. You can use any resources other user-mode code
can use.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.24 on an i686 machine (797.90 BogoMips).
Note 96.31% of all statistics are fiction.

Richard B. Johnson

unread,
Mar 17, 2004, 1:50:04 PM3/17/04
to VINOD GOPAL, John Bradford, linux-...@vger.kernel.org
On Wed, 17 Mar 2004, VINOD GOPAL wrote:

> The range of input is varying.
> Iam looking for floating point arithmetic functions
> like log10, pow , sin ,exp , cos etc.
>

Like the range of input from an ADC? You get the data in
the kernel and you muck with it in user-mode after you
have it.

John Bradford

unread,
Mar 17, 2004, 3:27:24 PM3/17/04
to Richard B. Johnson, VINOD GOPAL, John Bradford, linux-...@vger.kernel.org
Quote from "Richard B. Johnson" <ro...@chaos.analogic.com>:

> On Wed, 17 Mar 2004, VINOD GOPAL wrote:
>
> > The range of input is varying.
> > Iam looking for floating point arithmetic functions
> > like log10, pow , sin ,exp , cos etc.
> >
>
> Like the range of input from an ADC? You get the data in
> the kernel and you muck with it in user-mode after you
> have it.

If the task in hand is anything much more complicated than getting the
input from an 8-bit ADC, and transforming it using a single function
such as sin, then yes, do it in userspace.

John.

Peter Williams

unread,
Mar 17, 2004, 6:24:42 PM3/17/04
to VINOD GOPAL, linux-...@vger.kernel.org
VINOD GOPAL wrote:
> Hi,
> I am new to linux world and this mailing list.
>
> I need to use the arithmetic functions like sin, cos,
> exp, log, etc in linux device driver.
>
> On search read , not to use libm from kernel driver
> as it will harm the fpu registers.
>
> Do you have any insight how to support these
> functions in linux driver or any code that is
> available which I can make use of?

I'd recommend using fixed denominator rational numbers to represent real
numbers and polynomial approximations for the transcendental functions.
We do this in our EBS scheduler patches and it seems to be successful.
The main downside to this approach is the limited range (and
precision) of values that can be represented so you need to have a clear
idea of the likely values at various stages of your calculations.

The "Handbook of Mathematical Functions" by Abramowitz and Stegun, Dover
Publications, New York ISBN 486-61272-4 (LCN 65-12253) is a good source
of methods for approximating functions.

If you'd like more detailed assistance don't hesitate to ask.

Peter
--
Dr Peter Williams, Chief Scientist pet...@aurema.com
Aurema Pty Limited Tel:+61 2 9698 2322
PO Box 305, Strawberry Hills NSW 2012, Australia Fax:+61 2 9699 9174
79 Myrtle Street, Chippendale NSW 2008, Australia http://www.aurema.com

0 new messages