当我尝试在gcc 4.9.1中分配一个128位整数时,我得到一个warning: integer constant is too large for its type
。
int main(void) {
__uint128_t p = 47942806932686753431;
return 0;
}
我正在使用gcc -std=c11 -o test test.c
进行编译,我得到了:
test.c: In function ‘main’:
test.c:2:19: warning: integer constant is too large for its type
__uint128_t p = 47942806932686753431;
^
我做错了什么或者这是gcc中的错误?
答案 0 :(得分:28)
我做错了什么或者这是gcc中的错误?
问题出在47942806932686753431
部分,而不是__uint128_t p
。根据{{3}},没有办法声明128位常数:
对于长整数小于128位的目标,GCC不支持表达__int128类型的整数常量。
所以,看起来虽然你可以拥有128位变量,但你不能拥有128位常量,除非你的long long
是128位宽。< / p>
解决方法可能是从&#34;更窄的&#34;构建128位值。使用基本算术运算的积分常数,并希望编译器执行gcc docs。
答案 1 :(得分:1)
你试过这个吗?
__int128 p = *(__int128*) "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f";
编辑11月25日
很抱歉上一篇文章的说明不好。说真的,我没有把这个答案作为一个笑话发布。虽然GCC文档指出无法表达128位整数常量,但这篇文章只是为那些想要轻松地为__uint128_t变量赋值的人提供解决方法。
您可以尝试使用GCC(7.2.0)或Clang(5.0.0)编写下面的代码。它打印出所需的结果。
#include <stdint.h>
#include <stdio.h>
int main()
{
__uint128_t p = *(__int128*) "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f";
printf("HIGH %016llx\n", (uint64_t) (p >> 64));
printf("LOW %016llx\n", (uint64_t) p);
return 0;
}
标准输出:
HIGH 0f0e0d0c0b0a0908
LOW 0706050403020100
这只被视为解决方法,因为它通过放置&#34;值&#34;来指示指针。在.rodata部分(如果你objdump它),并且它不可移植(x86_64和aarch64很好但不是arm和x86)。我认为对桌面计算机上的编码来说已经足够了。
答案 2 :(得分:0)
我遇到了同样的问题,并使用用户定义的文字编写了解决方案。 实例化用户定义的文字_xxl的方法如下:
int main(int argc, char** argv) {
auto a = 0xF0000000000000000000000000000000LLU;
auto b = 0xF0000000000000000000000000000000_xxl;
printf("sizeof(a): %zu\n", sizeof(a));
printf("sizeof(b): %zu\n", sizeof(b));
printf("a == 0? %s\n", a==0 ? "true":"false");
printf("b == 0? %s\n", b==0 ? "true":"false");
printf("b >> 124 = %x\n", b >> 124);
return 0;
}
输出:
sizeof(a): 8
sizeof(b): 16
a == 0? true
b == 0? false
b >> 124 = f
这是用户定义文字_xxl的实现
#pragma once
#include <stdint.h>
#ifdef __SIZEOF_INT128__
using uint_xxl_t = __uint128_t;
using sint_xxl_t = __int128_t;
namespace detail_xxl
{
constexpr uint8_t hexval(char c)
{ return c>='a' ? (10+c-'a') : c>='A' ? (10+c-'A') : c-'0'; }
template <int BASE, uint_xxl_t V>
constexpr uint_xxl_t lit_eval() { return V; }
template <int BASE, uint_xxl_t V, char C, char... Cs>
constexpr uint_xxl_t lit_eval() {
static_assert( BASE!=16 || sizeof...(Cs) <= 32-1, "Literal too large for BASE=16");
static_assert( BASE!=10 || sizeof...(Cs) <= 39-1, "Literal too large for BASE=10");
static_assert( BASE!=8 || sizeof...(Cs) <= 44-1, "Literal too large for BASE=8");
static_assert( BASE!=2 || sizeof...(Cs) <= 128-1, "Literal too large for BASE=2");
return lit_eval<BASE, BASE*V + hexval(C), Cs...>();
}
template<char... Cs > struct LitEval
{static constexpr uint_xxl_t eval() {return lit_eval<10,0,Cs...>();} };
template<char... Cs> struct LitEval<'0','x',Cs...>
{static constexpr uint_xxl_t eval() {return lit_eval<16,0,Cs...>();} };
template<char... Cs> struct LitEval<'0','b',Cs...>
{static constexpr uint_xxl_t eval() {return lit_eval<2,0,Cs...>();} };
template<char... Cs> struct LitEval<'0',Cs...>
{static constexpr uint_xxl_t eval() {return lit_eval<8,0,Cs...>();} };
template<char... Cs>
constexpr uint_xxl_t operator "" _xxl() {return LitEval<Cs...>::eval();}
}
template<char... Cs>
constexpr uint_xxl_t operator "" _xxl() {return ::detail_xxl::operator "" _xxl<Cs...>();}
#endif // __SIZEOF_INT128__
它可以像普通的整数常量一样在constexpr中使用:
static_assert( 0_xxl == 0, "_xxl error" );
static_assert( 0b0_xxl == 0, "_xxl error" );
static_assert( 00_xxl == 0, "_xxl error" );
static_assert( 0x0_xxl == 0, "_xxl error" );
static_assert( 1_xxl == 1, "_xxl error" );
static_assert( 0b1_xxl == 1, "_xxl error" );
static_assert( 01_xxl == 1, "_xxl error" );
static_assert( 0x1_xxl == 1, "_xxl error" );
static_assert( 2_xxl == 2, "_xxl error" );
static_assert( 0b10_xxl == 2, "_xxl error" );
static_assert( 02_xxl == 2, "_xxl error" );
static_assert( 0x2_xxl == 2, "_xxl error" );
static_assert( 9_xxl == 9, "_xxl error" );
static_assert( 0b1001_xxl == 9, "_xxl error" );
static_assert( 011_xxl == 9, "_xxl error" );
static_assert( 0x9_xxl == 9, "_xxl error" );
static_assert( 10_xxl == 10, "_xxl error" );
static_assert( 0xa_xxl == 10, "_xxl error" );
static_assert( 0xA_xxl == 10, "_xxl error" );
static_assert( 0xABCDEF_xxl == 0xABCDEF, "_xxl error" );
static_assert( 1122334455667788_xxl == 1122334455667788LLu, "_xxl error" );
static_assert(0x80000000000000000000000000000000_xxl >> 126 == 0b10, "_xxl error");
static_assert(0x80000000000000000000000000000000_xxl >> 127 == 0b01, "_xxl error");
static_assert( 0xF000000000000000B000000000000000_xxl > 0xB000000000000000, "_xxl error" );