Skip to contentSkip to conversation

How to use floating point in a module?

DISCUSSION [email protected] 8 participants 8 messages May 31, 2004

lore
Share & tools
lore
#0 lore raw

Mans Rullgard replied to me:

Quoted 4 lines
Quoted 2 lines

A driver, implemented as a module, must do some floating-point computations including trig functions.

Sorry, floating point in the Linux kernel isn't allowed.

No. Use of floating point HARDWARE, and/or emulation which emulates troublesome features such as traps, isn't allowed. Guess why I posted in the first place, asking whether a certain combination of techniques might be feasible.

Quoted 4 lines
Quoted 1 line

Recompile GNU's libc with option "--without-fp".

Probably, but it doesn't matter, since the kernel doesn't link with libc.

This unfortunate answer probably does answer my question, thank you.

Quoted 5 lines
Quoted 3 lines

Compile the module's .c files with gcc's "-msoft-float" option and "-D__NO_MATH_INLINES". (Actually I think "-D__NO_MATH_INLINES" is probably unnecessary here.)

Using floating point emulation will be VERY slow.

No kidding. And if I write my own code to do floating point emulation, it will be even slower. But if we do none of the above, then we should say the result will be even slower because we will wait an infinite amount of time without getting results.

Quoted 5 lines
Quoted 3 lines

Link the module's .o files with the version of libc produced above, and try to get a loadable .ko from this... or a loadable .o since the target is still kernel 2.4.something.

As I said, the kernel doesn't link with libc.

Right, but is there a way to get a customized libc.a to link with a module's .o and produce a loadable .o without damaging the rest of the kernel. I don't quite know a way.

Quoted 2 lines

Floating point is forbidden in kernel code since the floating point registers (and other floating point context) is not saved/restored

No kidding, that's why use of floating point HARDWARE is prohibited.

Quoted 2 lines

might be possible to manually save the floating point context while doing some floating point operations.

Yes, my searching found a few people saying they had found tricks like this, but my impression is that it's very unreliable and they didn't reveal their entire trickery (probably unteachable as mentioned). I do think it is better to avoid the floating point hardware entirely.

Quoted 2 lines

What you should do is think again about why you need all this floating point in the kernel.

To control a device.

Quoted 1 line

Could it be moved to userspace somehow?

Yes, if we use a real-time Linux and make a daemon cooperate very closely with the driver.

Quoted 2 lines

Maybe you could use lookup tables instead of doing floating point arithmetic.

You might be right, if the device can only be controlled to position itself in say 1,000 different ways, then we could have lookup tables for 1,000 different intervals of (emulations of) floating-point numbers, that yield 1,000 different values of sin. Another table for cos, another for log10, etc. But I'd still have to write my own emulations for binary operators such as +, /, etc., since a 1,000*1,000 lookup table would be too big.

#1 lore raw
Quoted 6 lines · [email protected] wrote:

Mans Rullgard replied to me:

Quoted 2 lines

What you should do is think again about why you need all this floating point in the kernel.

To control a device.

Note that there is nothing stopping you from doing fixed-point math in the kernel. Depending on the application, this may be sufficient.

Chris

#2 lore raw
In reply to [email protected]
Quoted 7 lines · On Sun, 30 May 2004 [email protected] wrote:

Yes, if we use a real-time Linux and make a daemon cooperate very closely with the driver.

Quoted 2 lines

Maybe you could use lookup tables instead of doing floating point arithmetic.

Quoted 6 lines

You might be right, if the device can only be controlled to position itself in say 1,000 different ways, then we could have lookup tables for 1,000 different intervals of (emulations of) floating-point numbers, that yield 1,000 different values of sin. Another table for cos, another for log10, etc. But I'd still have to write my own emulations for binary operators such as +, /, etc., since a 1,000*1,000 lookup table would be too big.

Why not scaled longs (or bigger), scalled to number of significant digits. The Taylor series for the trig functions might be a painfull.

Ian

#3 lore raw
Quoted 18 lines · Ian Kent
Quoted 7 lines · On Sun, 30 May 2004 [email protected] wrote:

Yes, if we use a real-time Linux and make a daemon cooperate very closely with the driver.

Quoted 2 lines

Maybe you could use lookup tables instead of doing floating point arithmetic.

Quoted 6 lines

You might be right, if the device can only be controlled to position itself in say 1,000 different ways, then we could have lookup tables for 1,000 different intervals of (emulations of) floating-point numbers, that yield 1,000 different values of sin. Another table for cos, another for log10, etc. But I'd still have to write my own emulations for binary operators such as +, /, etc., since a 1,000*1,000 lookup table would be too big.

Quoted 1 line

Why not scaled longs (or bigger), scalled to number of significant digits. The Taylor series for the trig functions might be a painfull.

See the "Handbook of Mathematical Functions" by Abromawitz and Stegun, Dover Publications (ISBN 486-61272-2, Library of Congress number 65-12253) which has some small but accurate polynomial approximations for many functions. I have used these successfully with fixed point rational numbers (FDRN) (which are probably the same as your scaled longs) using 64 bit integers and the results were generally accurate to the least significant bit when compared to values calculated using the normal maths library and converted to FDRN representation.

Of course, the available range of values is smaller than for floats or double and you have to be careful w.r.t. overflow etc.

Peter

Signature · 4 lines
Dr Peter Williams                                [email protected]

"Learning, n. The kind of ignorance distinguishing the studious."
 -- Ambrose Bierce
#4 lore raw
In reply to Ian Kent

Ian Kent <[email protected]> said:

[...]

Quoted 2 lines

Why not scaled longs (or bigger), scalled to number of significant digits. The Taylor series for the trig functions might be a painfull.

Trascendental functions are _not_ computed by series in practice, rational approximations (polinomial / polinomial) are used instead. Or interpolate in a smallish table.

Signature · 4 lines
Dr. Horst H. von Brand                   User #22616 counter.li.org
Departamento de Informatica                     Fono: +56 32 654431
Universidad Tecnica Federico Santa Maria              +56 32 654239
Casilla 110-V, Valparaiso, Chile                Fax:  +56 32 797513
#5 lore raw
In reply to Ian Kent
Quoted 3 lines · Ian Kent

Why not scaled longs (or bigger), scalled to number of significant digits. The Taylor series for the trig functions might be a painfull.

Taylor series usually are painful for anything you want to calculate by any practical means. Slow convergence but, for a change, quickly growing roundup errors and things like that. An importance and uses of Taylor series lie elsewhere.

OTOH polynomial approximations, or rational ones (after all division is quite quick on modern processors), can be fast and surprisingly precise; especially if you know bounds for your arguments and that that range is not too wide. Of course when doing that in a fixed point one needs to pay attention to possible overflows and structuring calculations to guard against a loss of precision is always a good idea.

My guess is that finding some fixed-point libraries, at least to use as a starting point, should be not too hard.

Michal

#6 lore raw
Quoted 10 lines · Michal Jaegermann
Quoted 3 lines · Ian Kent

Why not scaled longs (or bigger), scalled to number of significant digits. The Taylor series for the trig functions might be a painfull.

Taylor series usually are painful for anything you want to calculate by any practical means. Slow convergence but, for a change, quickly growing roundup errors and things like that. An importance and uses of Taylor series lie elsewhere.

Quoted 7 lines

OTOH polynomial approximations, or rational ones (after all division is quite quick on modern processors), can be fast and surprisingly precise; especially if you know bounds for your arguments and that that range is not too wide. Of course when doing that in a fixed point one needs to pay attention to possible overflows and structuring calculations to guard against a loss of precision is always a good idea.

It's also worth pointing out that there are other, much
faster-converging, polynomial series that can be used to approximate trig functions. Chebyshev polynomials are, I believe, pretty well-behaved. IIRC, they are what the Sinclair Spectrum used to use.

Hugo.

Signature · 3 lines
=== Hugo Mills: hugo@... carfax.org.uk | darksatanic.net | lug.org.uk ===
  PGP key: 1C335860 from wwwkeys.eu.pgp.net or http://www.carfax.org.uk
   --- Hey, Virtual Memory! Now I can have a *really big* ramdisk! ---   
#7 lore raw
In reply to Michal Jaegermann
Quoted 18 lines · Michal Jaegermann
Quoted 1 line · Ian Kent

Why not scaled longs (or bigger), scalled to number of significant digits. The Taylor series for the trig functions might be a painfull.

Taylor series usually are painful for anything you want to calculate by any practical means. Slow convergence but, for a change, quickly growing roundup errors and things like that. An importance and uses of Taylor series lie elsewhere.

OTOH polynomial approximations, or rational ones (after all division is quite quick on modern processors), can be fast and surprisingly precise; especially if you know bounds for your arguments and that that range is not too wide. Of course when doing that in a fixed point one needs to pay attention to possible overflows and structuring calculations to guard against a loss of precision is always a good idea.

Quoted 2 lines

My guess is that finding some fixed-point libraries, at least to use as a starting point, should be not too hard.

See the "Handbook of Mathematical Functions" by Abromawitz and Stegun, Dover Publications (ISBN 486-61272-2, Library of Congress number 65-12253) which has some small but accurate polynomial approximations for many functions.

Signature · 4 lines
Dr Peter Williams, Chief Scientist                [email protected]
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