6

I'm trying to implement a support for double and float and corresponding basic arithmetic on a CPU without an FPU.

I know that it is possible on all AVR ATmega controllers. An ATmega also has no FPU. So here comes the question: How does it work? If there any suggestions for literature or links with explanations and examples?

At the best case I will provide a support for code like this:

double twice ( double x )
{
  return x*x;
}

Many thanks in advance, Alex

Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
Alex44
  • 3,597
  • 7
  • 39
  • 56

3 Answers3

4

Here are AVR related links with explanations and examples for implementing soft double:

  1. You will find one double floating point lib here.
  2. Another one can be found it in the last message here.
  3. Double is very slow, so if speed is in concern, you might opt for fixed point math. Just read my messages in this thread:
avra
  • 3,690
  • 19
  • 19
3

This post may be interesting for you: Floating point calculations in a processor with no FPU

As stated:

Your compiler may provide support, or you may need to roll your own. There are freely-available implementations, too.

If it's for an ATmega, you probably don't have to write anything yourself. All the already available libraries are probably already optimized much further than you possible can do yourself. If you need more performance, you could consider to convert the floating points to fixed points. You should consider this anyway. If you can get the job done in fixed point, you should stay away from floating point.

Community
  • 1
  • 1
Sander
  • 171
  • 1
  • 10
  • It's not for an ATmega. It's for an Linux kernel module. I need to do some fp math to setup an device correctly. But the kernel doesn't support FPU-operations. Your suggestion to fixed point could be a way too. If there some examples? – Alex44 Dec 12 '15 at 11:51
  • Kernel modding is not my expertise but this post may be interesting: http://stackoverflow.com/questions/13886338/use-of-floating-point-in-the-linux-kernel If you can do the job with fixed point, you sure should do so. I'll search for an example, one moment. – Sander Dec 12 '15 at 14:28
  • This post could help you with fixed point: http://stackoverflow.com/questions/79677/whats-the-best-way-to-do-fixed-point-math – Sander Dec 12 '15 at 14:33
  • I think you are right. It's very difficult to integrate a floating point implementation to a kernel module. So I try to get the job done in fixed point. – Alex44 Dec 13 '15 at 17:42
3

Avr uses AVR Libc, which you can download and examine.

There is a math library, but that is not what you are looking for. That contains the standard functions defined in math.h.

Instead, you need functions that perform multiplication and things like like that. These are also in Libc, under fplib and written in assembly language. But the user doesn't call them directly. Instead, when the compiler comes across a multiplication involving floats, the compiler will choose the correct function to insert.

You can browse through the AVR fplib to get an idea of what to do, but you are going to have to write your own assembly or bit-twiddling code for your processor.

You need to find out what standard your processor and language are using for floating point. IEEE, perhaps? And you'll also need to know if the system is little-endian or big-endian.

I am assuming you system doesn't have a C compiler already. Otherwise, all the floating point operations would already be implemented. That "twice()" function (actually square()) would work just fine as it is.

UncleO
  • 8,299
  • 21
  • 29