Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does an AVR perform floating point Arithmetic

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

like image 377
Alex44 Avatar asked Dec 12 '15 11:12

Alex44


People also ask

How do you perform arithmetic with floating-point numbers?

Arithmetic operations on floating point numbers consist of addition, subtraction, multiplication and division. The operations are done with algorithms similar to those used on sign magnitude integers (because of the similarity of representation) — example, only add numbers of the same sign.

Does AVR have FPU?

Microcontroller and touch solutions leader Atmel Corporation (www.atmel.com) has unveiled the first 32-bit AVR microcontrollers (MCUs) that feature a floating point unit (FPU).

What is floating point arithmetic in coding?

In computing, floating-point arithmetic (FP) is arithmetic that represents real numbers approximately, using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base.


2 Answers

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:
like image 107
avra Avatar answered Sep 22 '22 03:09

avra


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.

like image 24
Sander Avatar answered Sep 19 '22 03:09

Sander