How does an AVR perform floating point Arithmetic

11,298

Solution 1

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:

Solution 2

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.

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

Share:
11,298
Alex44
Author by

Alex44

I studied aeronautics. And now I'm proud to be part of a company developing a really green general aviation aircraft.

Updated on June 20, 2022

Comments

  • Alex44
    Alex44 almost 2 years

    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

  • Alex44
    Alex44 over 8 years
    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?
  • Sander
    Sander over 8 years
    Kernel modding is not my expertise but this post may be interesting: stackoverflow.com/questions/13886338/… If you can do the job with fixed point, you sure should do so. I'll search for an example, one moment.
  • Sander
    Sander over 8 years
    This post could help you with fixed point: stackoverflow.com/questions/79677/…
  • Jens
    Jens over 8 years
    The OP used an exceptionally misleading name; twice() actually computes square().
  • Alex44
    Alex44 over 8 years
    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.