February 17, 2012

Floating point support released


New Red/System version 0.2.4 is out. The main new feature is floating point numbers support. This feature was not planned, but as users were strongly pushing for having it now, we have added partial support for floats. Users can now wrap and use libraries like OpenGL.

New floating point datatypes

Two new IEEE-754 datatypes were added:

  • float64! datatype implements double precision (64-bit) floating point numbers. It has an alias named float! which is the preferred name (both will be accepted by the compiler anyway).
They are first-class datatypes, so they can be used anywhere, as variables, arguments or returned values.

Float operations

Basic math and comparison operations are supported. Transcendental functions are not predefined yet in the runtime, they need to be imported first from an external library. For equality test, no rounding is applied, so only exact float numbers match will return `true`. Adding an "almost-equal" operator is planned, but not available yet.

Also, type casting is allowed between:

  • float! <=> float32!
  • float32! <=> integer!

Remember to type cast all float32! values to float! when passing them as argument to C variadic functions. For non-variadic C functions, Red/System's compiler will promote float32! to float! automatically.

Limitations

Not all of IEEE-754 standard was implemented yet. See the remaining features to add in this todo-list.

Implementation

At low-level, floating point support is architecture-specific:

  • IA-32: currently uses x87 FPU instructions for backward compatibility with older Intel CPU. The default implementation requires a PentiumPro or better. SSE unit support will be added when a proper optimizing compiler will be available. For older CPU support, a new compilation option has been added (`cpu-version`), which triggers the use of older opcodes. For example, in order to use floats on older than PentiumPro CPU, you need to add this line in the target definition:   cpu-version: 5.0
  • ARM: uses the VFP unit when available and was reported to run fine on emulated VFP. When calling some C functions passing or returning float numbers, some ABI mismatching were also reported during the tests, as ARM uses different ABI for passing float numbers as arguments. Our current implementation only use the passing-on-stack convention, adding support for other conventions is not planned, but will be done when really required.
Tests coverage extended

Several thousands new tests were added to the test suite, pushing the total number of unit tests to 11755. Thanks to Peter for taking care of that.

Decimal support

As IEEE-754 2008 revision introduced several new decimal floating point formats, we plan to support them later in Red/System.

Other feature released: enumeration support

A contributed feature is also part of this new version of Red/System: C-like enumeration support. This contribution was brought by the lead developer of Amanita Design, small independent game developing studio based in Czech Republic, which released the award-winning game Machinarium. A new compilation directive was added to declare enumerations: #enum.

Last but not least...

We got noticed by Ruby's author, Matz, he tweeted about Red! :-)
Fork me on GitHub