C 128 bit double type

Kendall Weihe picture Kendall Weihe · Nov 7, 2015 · Viewed 7.3k times · Source

I have the following code...

            ran_int = rand(); //check READ ME Citation B
            if (input == 32){
                //rand() with 2^31 as lower limit and 2^32 as its upper
                long long int upper_limit = 4294967295;
                long long int lower_limit = 2147483649
                ran_32_64 = (rand() * 2) % (upper_limit - lower_limit);
                ran_32_64 += lower_limit;
            }
            else if(input == 64){
                //rand() x 4 with 2^63 as lower limit and 2^64 as its upper
                unsigned long long int upper_limit = powl(2, 64);
                long long int lower_limit = powl(2, 63); 
                ran_32_64 = (rand() * 4) % (upper_limit - lower_limit); 
                ran_32_64 += lower_limit;

            }
            else if(input == 128){
                //rand() x 8 with 2^127 as lower limit
                unsigned _int128 upper_limit = 
            }

and for the last block I need to represent a 128 bit binary number. I know I can use the type _int128 for 128 bit values, but that's not totally precise.

So I'm thinking to be totally precise I could use type double, but I can't find any literature on 128 bit double types.

Any ideas?

Answer

Jeff Hammond picture Jeff Hammond · Nov 7, 2015

Compiler support

It's not a double if it is 128b, but both GNU and Intel C/C++ compilers support 128b floats via __float128 (older Intel C/C++ compilers had Quad) instead.

The GCC documentation is quite thorough.

Library support

There are library implementations including:

Modified code

It is hard to tell what your code is trying to do, but the 128-bit integer equivalent of what you wrote before is below.

I honestly don't know where you intend to use floating-point numbers of any width, so please provide a more appropriate example if you want details for 128b floats.

#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#include <quadmath.h>

void foo(int input) {
    int ran_int = rand(); //check READ ME Citation B
    if (input == 32){
        //rand() with 2^31 as lower limit and 2^32 as its upper
        uint32_t upper_limit = 4294967295;
        int32_t  lower_limit = 2147483649;
        uint32_t ran_32_64 = (rand() * 2) % (upper_limit - lower_limit);
        ran_32_64 += lower_limit;
    }
    /*
    else if(input == 64){
        //rand() x 4 with 2^63 as lower limit and 2^64 as its upper
        uint64_t upper_limit = powl(2L, 64);
        int64_t  lower_limit = powl(2L, 63);
        uint64_t ran_32_64 = (rand() * 4) % (upper_limit - lower_limit);
        ran_32_64 += lower_limit;

    }
    */
    else if(input == 128){
        //rand() x 8 with 2^127 as lower limit
        unsigned __int128 upper_limit = powq(2, 64);
        __int128  lower_limit = powq(2, 63);
        unsigned __int128 ran_32_64 = (rand() * 4) % (upper_limit - lower_limit);
        ran_32_64 += lower_limit;
    }
}