# DIY FP - Implementing floating point math on a microcontroller without a hardware FPU

I'm working on a little personal project (targeting a Parallax Propeller, but this should be pretty generalizable) that needs floating point math.

The micro-controller I am working with does not have native floating point, or a stock floating-point library.

Are there any good resources or tutorials on implementing software floating-point math? Preferably on really limited resources (I don't even have a stack!).

I'd like to support double sized floats. The propeller is a 32 bit MCU, so I'll have to use multiple variables for each float.

I know there is one software-floating-point library for the propeller out there, but it only supports single sized floats.

No, I probably don't really need double sized floats, but this sounds like a really interesting project. Half the reason I want to do this is because I'll learn a lot along the way.

• what compiler? I may have missed it, but I do not see what language you are using. – Kortuk Jan 4 '12 at 8:53
• @Kortuk - I'm personally targeting a Parallax Propeller, so it'll either be Spin or asm. However, I think this shouldn't be too language specific. Assume I have the common operations (+-/* bit-shifting, bitwise and/or/etc..) . – Connor Wolf Jan 4 '12 at 9:27
• What operations do you need to implement ? If you just want add, subtract and multiply, and you're not too worried about edge cases, NaNs, etc, then it should be pretty trivial to implement this. I've done it for 8 bit micro-controllers in the past and it's not a mammoth task, even if you do it all in asm. – Paul R Jan 4 '12 at 11:47
• Well as you say, you'll learn a lot about implementing floating point operations along the way. – Paul R Jan 4 '12 at 11:53
• Maybe you don't need floating point. If you just need fractional numbers also fixed point arithmetic with decimal (actual binary) point shifted some places to the left can do. Can you tell more about your application? – Curd Jan 4 '12 at 13:16

If you want to do it yourself, I'd say just do it.

I guess you wont find too many resources or tutorial because there is not much to it.

Here is an outline:

if exponents differ too much (more than the mantissa has bits):
just return the value with larger exponent (if this is the subtrahend: negate)

if exponents are similar:
shift the mantissa of smaller value by difference of exponents and add to/subtract from other mantissa (using fixed point arithmetic)
if result is not 0: shift mantissa up until MSBit of result is 1 and decrement exponent the same amount

• multiplication/division:
multiply/divide mantissas (using fixed point arithmetic) and add/subtract exponents

You should be able to use this multiple-precision floating-point library on the Propeller, with either Catalina C or gcc. It might be too slow for many applications, however.

• Can C code on the propeller interoperate with Spin or prop-asm? If not, I might have a go at translating it to spin. Fortunately, I don't need much speed. Realistically, I need to do 7 operations, at 2 hz. – Connor Wolf Jan 4 '12 at 11:40
• Oh boy, the source alone is 1.1 MB, zipped. I think it's a bit overkill. Are there any simpler options? – Connor Wolf Jan 4 '12 at 11:42
• More importantly, this library (1) is about arbitrary-precision floating point (not native size like 32-bit and 64-bit) and (2) it targets Linux compilers on i386 and x64 Intel, AMD, and MIPS processors. There is a mention of 'arm' and 'generic' in the source folders, but I would not start here. – Kevin Vermeer Jan 4 '12 at 14:35

It looks like this question has taken on the slant of seeking understanding more than solving the problem, so this may not be the most useful answer, but just for sake of completeness