8

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.

Connor Wolf
  • 31,938
  • 6
  • 77
  • 137
  • what compiler? I may have missed it, but I do not see what language you are using. – Kortuk Jan 04 '12 at 08: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 04 '12 at 09: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 04 '12 at 11:47
  • @Paul R - Add, subtract, multiply, divide are all I need. I figure it's not that big a deal, I'm just on completely unfamiliar turf. I figure I might as well ask before just starting to try things. – Connor Wolf Jan 04 '12 at 11:48
  • I don't need much in the way of edge-case protection either. I can be pretty confident my input values are sane. – Connor Wolf Jan 04 '12 at 11:49
  • 1
    Well as you say, you'll learn a lot about implementing floating point operations along the way. – Paul R Jan 04 '12 at 11:53
  • @Paul R - Yeah. I'm mostly looking for documentation and examples. Most of the 'production' libraries out there are so complex (due to having to handle edge-cases, etc...) that they're nearly impossible to follow. – Connor Wolf Jan 04 '12 at 11:55
  • Yes - adopt the "KISS" principle - use naive algorithms and don't worry about edge cases or even accuracy too much - that should keep the task manageable and the resulting code will probably still be useful for 90% of applications that need floating point. – Paul R Jan 04 '12 at 11:57
  • @Paul R - that said, you have any pointers on where to start? – Connor Wolf Jan 04 '12 at 12:00
  • I would just start with the description of IEEE 754 floating point formats on Wikipedia and work it out from first principles. I expect the trickiest part will be implementing routines to manipulate the 52 bit mantissa - you'll need integer add, subtract, multiply, shift, etc for 52 (actually 53) bits. – Paul R Jan 04 '12 at 12:19
  • @Paul R - Well, I guess that's what we have bit shift operations, and the carry flag for. – Connor Wolf Jan 04 '12 at 12:23
  • 3
    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 04 '12 at 13:16
  • @Curd - Yep. Fixed point would probably work fine. But where's the fun in that? The application is largely irrelevant. I'm more interested in learning how to do floating point then solving the implementation details in this context as easily as possible. – Connor Wolf Jan 04 '12 at 13:21

3 Answers3

4

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:

  • adding/subtracting:
    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

Curd
  • 16,043
  • 34
  • 43
2

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.

Leon Heller
  • 38,774
  • 2
  • 60
  • 96
  • 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 04 '12 at 11:40
  • 2
    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 04 '12 at 11:42
  • 2
    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 04 '12 at 14:35
2

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

http://code.google.com/p/propgcc/wiki/PropGccCompileOptions

Implies that the propeller version of GCC has floating point support, including doubles.

GCC and its compiler libraries are of course open source, though there may be a real learning curve before you could start to see what the code is doing.

Chris Stratton
  • 33,282
  • 3
  • 43
  • 89