7

The GCC docs describe limited decimal floating point support in recent GCCs.

But how do I actually use it?

For example on Fedora 18, GCC 4.7.2.

A simple C program like

int main()
{
    _Decimal64 x = 0.10dd;
    return 0;
}

compiles (when using -std=gnu99) - but how do I actually do other useful stuff - like printing _Decimal64 values or converting strings to _Decimal64 values?

The docs talk about 'a separate C library implementation' for (I assume) things like printf - which additional library do I have to use for - say - printing the result of a decimal floating point computation?

I've tried

printf("%Df\n", x);

which did not work - printf just produced: %Df.

maxschlepzig
  • 35,645
  • 14
  • 145
  • 182

1 Answers1

7

As the docs say, GCC doesn't provide I/O, because printf etc. are provided by libc not by GCC.

IBM contributed an extension to the GNU C library, libdfp, which adds printf hooks to make Decimal I/O work.

The README says:

When libdfp is loaded printf will recognize the following length modifiers:

        %H - for _Decimal32
        %D - for _Decimal64
        %DD - for _Decimal128

It will recognize the following conversion specifier 'spec' characters:

        e,E
        f,F
        g,G
        a,A  (as debuted in ISO/IEC TR 24732)

Therefore, any combination of DFP length modifiers and spec characters is
supported.

But as noted in the comments below, this library was originally only supported on s390 and powerpc hardware. It looks like support for aarch64 was added a few months ago (September 2022), and there are directories for x86 and x86_64 so maybe it's now possible to do I/O for these types.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • Ok, libdfp seems to be the way to go - unfortunately, it seems that it is not available via the Fedora repositories - compiling from source does not seem to be that straight forward, either - it complains about 'no configuration information is in libbid' - on the other hand gcc compiled code calls functions like `__bid_muldd3` (when doing operations on _Decimal64 values) - without link errors - thus, gcc seems to include libbid in its runtime. – maxschlepzig Feb 17 '13 at 21:49
  • Try the configure option `--with-backend=libdecnumber`, that allows `configure` to work for me on Fedora 17. The `configure --help` says that's the default, but apparently it's lying. I get a later compilation failure though – Jonathan Wakely Feb 17 '13 at 21:58
  • It seems the library can only be built on IBM s390 and powerpc systems – Jonathan Wakely Feb 17 '13 at 22:19