I am looking for a reference implementation of IEEE-754 operations. Is there such a thing?
-
3Do you mean "reference implementation" in the sense of "something I can copy and be confident it'll be right", or in the sense of "something provided by IEEE as a non-normative description of the IEEE-754 standard in the form of C++ code"? For the former, you're looking for "software floating point emulation", for example here's a list of GCC's functions: http://gcc.gnu.org/onlinedocs/gccint/Soft-float-library-routines.html – Steve Jessop Feb 02 '10 at 19:13
-
I should add that being GCC, I'd expect there to be a pure C implementation of those functions which back-end writers can replace with platform-specific code. But I haven't actually checked. – Steve Jessop Feb 02 '10 at 19:30
-
https://github.com/bobbl/float754 is another project. – masterxilo Nov 01 '17 at 12:54
4 Answers
I believe the C libraries SoftFloat and fdlibm are suitable for what you are looking for. Others include Linux (GNU libc, glibc) or *BSD libc's math functions. Finally, CRlibm should also be of interest to you.
Ulrich Drepper has a interesting look at different math libraries, that might be also worth reading through.

- 5,159
- 20
- 32
-
1SoftFloat is from John R. Hauser who wrote an article about the use of IEEE exceptions in the article TOPLAS. I wasn't aware that Hauser did write an own implementation, so it's likely that he in fact has written a reference implementation. – Thorsten S. Feb 02 '10 at 20:03
-
Thanks. SoftFloat is definitely what i was looking for. I will take a look at the others too. – zr. Feb 02 '10 at 22:40
I must disappoint you: There is practically none.
While technically there are IEEE-754 compliant systems because they do not implement non-required features described in the standard, a reference implementation allowing
- access to all rounding modes
- support signalling NaNs
- support trapping of all five traps
does not exist in the standard languages. This causes recurring woes of William Kahan, the main force behind the standard and its adaption on the Intel processors.
I don't know if there are some esoteric languages which do support them, but I can rule out Java, C#, C++, Fortran.
EDIT: While there is a lack of compiler support, I advise Hausers SoftFloat implementation given by mctylr. Hauser knows what he is doing.

- 4,144
- 27
- 41
-
"some esoteric languages which do support them". I'd think the case is more that all languages support these, but that underlying differences in the hardware or incomplete hardware compliance is more the issue. Especially when you consider that the languages are usually defined in architecture-independent manners and then compiled by programs which run on many architectures. A programming language which offloads floating-point math to hardware cannot make IEEE754 guarantees. – Richard Jun 11 '12 at 22:03
A rather confusing question; in C++ it is assumed this sort of detail is taken care of by the hardware or the compiler. So, in C++ floating-point addition would be
float a = 1.0;
float b = 2.0;
float c = a + b;
I'm sure this is not what you actually meant; perhaps you'd benefit from this page which tries to emulate IEEE-754-compliant hardware in javascript?

- 84,206
- 33
- 197
- 283
-
And no, looking at the compiler's source would not be helpful, since you'd be looking at the C code to generate the IEEE-754-compliant assembly-code, rather than C code that does IEEE-754 computations. – BlueRaja - Danny Pflughoeft Feb 02 '10 at 19:15
-
1Old ARM chips (and probably some new ones) don't have an FPU, so looking at the source code for a compiler which targets ARM would be helpful, since it has to be capable of performing float and double arithmetic "in software". Of course the implementations will be ferociously optimised, and might use CPU ops that aren't entirely trivial in C. – Steve Jessop Feb 02 '10 at 19:28
one work around for you could python. python has a function that can convert IEEE-754 32/64 bit precision HEX float point
import struct
struct.unpack('!f', '41973333'.decode('hex'))[0]
You can either write you app in python or just create a python function and call it in C/C++
I'm unsure how to call python from C/C++ but it is possible. Calling Python functions from C++

- 1
- 1