Next: , Previous: , Up: Grade modifiers   [Contents][Index]


3.4.8 Grade modifiers for single precision floats

Many imperative languages have at least two floating point types, one single precision and one double precision, which usually means they are 32 bits and 64 bits in size respectively. Mercury has only one floating point type, float, which by default is implemented as an IEEE 754 double-precision 64-bit floating point number.

When targeting Java or C#, this default cannot be overridden: values of the Mercury type float are always represented by values of type double (which exists in both those languages). When targeting C, the default can be overridden by using the following grade modifier.

.spf

Compiling programs in a grade that includes the .spf grade modifier (which stands for “single-precision float”) generates executables that represent values of the Mercury float type as 32-bit values of C’s ‘float’ type.

This grade modifier was intended to be used on 32-bit platforms. On those platforms, 64-bit floats are twice the size of a machine word, which means that they cannot be stored the same way as all other values, and must instead be stored on the heap. This means that every operation that generates a new 64-bit floating point value must allocate a heap cell for it, and write those 64 bits to that cell. (This is usually called boxing the 64-bit value.) Representing Mercury floats as 32-bit single-precision values avoids this overhead, improving both memory consumption and speed. This made this grade modifier useful for programs that did not require the extra precision (or range) offered using 64-bits.

.spf grades provide no performance advantage at all 64-bit platforms, because on those platforms, 64-bit values do not require boxing. On the other hand, they may simplify the use of C APIs that exclusively use single precision floats.

This grade modifier is relevant only for base grades that target C, and is compatible only with them.


Next: , Previous: , Up: Grade modifiers   [Contents][Index]