C 128位双类型

时间:2015-11-07 15:45:07

标签: c types binary double 128-bit

我有以下代码......

            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 = 
            }

对于最后一个块,我需要表示一个128位的二进制数。我知道我可以将类型_int128用于128位值,但这并不完全准确。

所以我想要完全准确我可以使用double类型,但我找不到任何有关128位双重类型的文献。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

编译器支持

如果它是128b,它不是双倍的,但GNU和英特尔C / C ++编译器都通过__float128支持128b浮点数(旧的英特尔C / C ++编译器有Quad)。

GCC documentation非常彻底。

图书馆支持

有库实现,包括:

修改后的代码

很难说你的代码试图做什么,但是你之前写的128位整数等价于下面。

老实说,我不知道你打算使用任何宽度的浮点数,所以如果你想要128b浮点数的详细信息,请提供一个更合适的例子。

#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;
    }
}