17

I have a coprocessor attached to the main processor. Some floating point calculations needs to be done in the coprocessor, but it does not support hardware floating point instructions, and emulation is too slow.

Now one way is to have the main processor to scale the floating point values so that they can be represented as integers, send them to the co processor, who performs some calculations, and scale back those values on return. However, that wouldn't work most of the time, as the numbers would eventually become too big or small to be out of range of those integers. So my question is, what is the fastest way of doing this properly.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
  • 1
    Learn how floating point math works. It's easy. You should've gotten most of the necessary info at school. The rest can be found online (use Wikipedia, Google, etc). And implement floating point arithmetic routines. – Alexey Frunze Apr 09 '13 at 18:44
  • But that is a slow method. I want something efficient. – MetallicPriest Apr 09 '13 at 18:44
  • 1
    @AlexeyFrunze The OP has already said that emulation is too slow. This isn't an issue of implementing floating point. – sfstewman Apr 09 '13 at 18:44
  • 1
    Well, something efficient can be done when the problem is defined and is sufficiently narrow. The current question is too broad/general. – Alexey Frunze Apr 09 '13 at 18:46
  • 1
    `fastest` & `properly`... Give a specific problem. A formula you need to compute or something to that effect. How can we give you the `fastest` and `proper` solution to an undefined problem? – Alexey Frunze Apr 09 '13 at 18:52
  • A good solution in C# here, http://stackoverflow.com/questions/605124/fixed-point-math-in-c. – MetallicPriest Apr 09 '13 at 18:56
  • I always wondered what platform this is you were coding for... – Prof. Falken Dec 29 '20 at 19:37

2 Answers2

15

You are saying emulation is too slow. I guess you mean emulation of floating point. The only remaining alternative if scaled integers are not sufficient, is fixed point math but it's not exactly fast either, even though it's much faster than emulated float.

Also, you are never going to escape the fact that with both scaled integers, and fixed point math, you are going to get less dynamic range than with floating point.

However, if your range is known in advance, the fixed point math implementation can be tuned for the range you need.

Here is an article on fixed point. The gist of the trick is deciding how to split the variable, how many bits for the low and high part of the number.

A full implementation of fixed point for C can be found here. (BSD license.) There are others.

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
  • 1
    Which means, such optimizations should be done on a case by case basis. No silver bullet. – Alexey Frunze Apr 09 '13 at 18:48
  • @AlexeyFrunze, indeed. The OP case seems to be pretty special. – Prof. Falken Apr 09 '13 at 18:49
  • *Almost* +1. The links given are all pure `C` and one is Java. I like GnuPGP for multi-word arithmetic; *in-line* assembler is used to get faster multi-word operations when available. I think a *fixed point* 'C' library could also benefit from this. While fixed point has less *dynamic range*, it also has a lot less [land minds](http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/), especially given the *embedded* tag. – artless noise Apr 10 '13 at 18:42
  • @artlessnoise, sorry, I am not sure what you mean. – Prof. Falken Apr 11 '13 at 06:09
  • 1
    Which part all of it? *magicfixedpoint* appears to be Java. The 'C' libraries are only fixed point and don't include transcendental functions. You can also do **bignum** (or multi-word) type fixed point, which increased dynamic range. This can be done efficiently with `carry` hardware. Also floating point has *dynamic precision*, the larger the value the farther `eta` is. Fixed point does not have this issue; just to say your answer needs a little balance. Floating point is not a panacea. – artless noise Apr 11 '13 at 13:29
6

In addition to @Amigable Clark Kant's suggestion, Anthony Williams' fixed point math library provides a C++ fixed class that can be use almost interchangeably with float or double and on ARM gives a 5x performance improvement over software floating point. It includes a complete fixed point version of the standard math library including trig and log functions etc. using the CORDIC algorithm.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 1
    Another project of the same sort is on [google hosted project](http://code.google.com/p/fpmath/) and [code project link](http://www.codeproject.com/Articles/37636/Fixed-Point-Class), these seem to have more of a complete library including some transcendental functions. – artless noise Apr 10 '13 at 18:51