Thread: Reputable atod/dtoa code for IEEE 754 half/float/double/80bitExt/quadfloat?

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    23

    Reputable atod/dtoa code for IEEE 754 half/float/double/80bitExt/quadfloat?

    Hi,

    I'm writing a C library that supports performing calculations in several IEEE 754 floating point types (half, float, double, Intel 80bit extended, and quad floats as in __float128).

    My library uses the excellent SoftFloat lib by John Hauser so that calculations can be done with all of such types even when the host CPU hardware doesn't support them (letting also use the native host implementation for the types it supports). I chose SoftFloat because it seems to be the most reputable IEEE 754 software implementation, from what I've read.

    Unfortunately, SoftFloat doesn't provide functions for dtoa/atod (printf/scanf), and it doesn't recommend any code in particular either.

    Because not all host system C runtimes have printf/scanf implementations capable of dealing with all these formats, I need to provide my own functions for converting all the floats to/from strings.

    All the printf/scanf implementations for all current C runtimes are based in dtoa at netlib/fp Unfortunately, dtoa supports float and double only.

    Then, there's the more general gdtoa (also available in the same page) which DOES support all the types that I wish to support. However, I found two drawbacks that make me pause before I decide to use it:

    First, the build instructions tell you to provide thread lock functions if the code will be run in multiple threads (I want to believe the reason is not thread memory but some kind of sync required by the FP hardware in the CPU). No matter the reason for requiring you to provide such thread locks, it's an annoyance at this moment, because then I'd need to introduce in my library a choice for using pthreads, C threads, OpenMP, etc... and I don't like to do that just because the dtoa/atod code needs it.

    Second, I've read that libquadmath started using gdtoa in the beginning, but that they dropped it because the code was buggy for 128bit floats. I don't know the details, but this really makes me stop and think.

    So, to my question: Is there any reputable (I mean, reputable as the SoftFloat library) code for printing/scanning all these IEEE 754 types? License must be NOT copyleft (although LGPL with static linking exception might be acceptable).

    I'd like to find a good and simple solution (but yes, I know that float to decimal conversion is anything but simple).

    Any recommendation you can say?

  2. #2
    Registered User
    Join Date
    Aug 2022
    Posts
    1
    Dtoa, Grisu and Dragon tackles floating-point conversion, however they all rely on many external functions.
    I can write a sophisticated floating-point conversion without the use of external headers or external functions, with the exception of stdio for printf.

    Below you can see that grisu has terrible naming practice and relies on many external functions.

    Code:
    void grisu(double v, char* buffer) {
    diy fp w; uint32 t ps[3];
    int q = 64, alpha = 0, gamma = 3;
    w = normalize diy fp(double2diy fp(v));
    int mk = k comp(w.e + q, alpha, gamma);
    diy fp c mk = cached power(mk);
    diy fp D = multiply(w, c mk);
    cut(D, ps);
    sprintf(buffer, "%u%07u%07ue%d",
    ps[0], ps[1], ps[2], -mk);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 06-20-2012, 02:34 AM
  2. conversion to ieee from a float
    By srcsrc13 in forum C Programming
    Replies: 5
    Last Post: 10-04-2010, 04:41 PM
  3. Rounding a float to the nearest half
    By kzar in forum C Programming
    Replies: 4
    Last Post: 04-01-2005, 10:07 PM
  4. Conversion from hex to IEEE-754 float
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-04-2002, 01:41 AM

Tags for this Thread