Solved clang; use 128b number

Hi,

I'm testing this on FreeBSD 10.2 amd64.

Following is the simple program:

Code:
#include <stdio.h>

int main() {
  __int128_t bundle;
  fprintf (stdout, "bundle size: %ld\n", sizeof(bundle));

  return 0;
}
and the output is as expected 16. But when I try to assign a number:

Code:
  __int128_t bundle = 0x773002e636f6d6d656e7400000000000;
I got the error:
Code:
parseop.c:7:22: error: integer constant is larger than the largest unsigned integer type
  __int128_t bundle = 0x773002e636f6d6d656e7400000000000;
But how come ?
 
Look at the actual definition of these 128 bit integers (I found it in sdp.h). Since that's not a "native" integer type for the C compiler, you will have to use some other method to assign a value to it. I see a SDP_PUT128 macro that looks like just what you need.
 
You really have 128 bit numbers in your compiler? Maybe you need LLL or LLLL after the initializer.

Juha

Edit: stdint.h should provide a macro INT128_C(literal) so that the suffix can then be really foul looking or just a bit shameful so it can stay hidden
 
It came to my mind that I would need to make it out of two 64b integers. I'll be doing bitwise operations on it, that brings an overhead I'd like to avoid if possible.

I checked the stdint.h (and other headers it loads) but I was not able to find that macro (did the search on /usr/include and even on whole /usr/src).

I tried to append ULL but it didn't help. I'm curious how the __uint128_t got defined in the first place.

EDIT: from what I read it seems __int128_t is not an integer type. Its size is the same as long long, which is 8 on my architecture.
 
From what I found in the GCC documention:
Code:
There is no support in GCC for expressing an integer constant of type __int128 for targets with long long integer less than 128 bits wide.

So this works:
Code:
 bundle = (__int128_t)0x773002e636f6d6d6 << 64 | (__uint128_t)0x56e7400000000000;
 
I can't define the mask either as some of it would be more than 64b, but this does work:

Checking for bit 81:

Code:
  __uint128_t bundle = (__int128_t)0x773002e636f6d6d6 << 64 | (__uint128_t)0x56e7400000000000;

  if ( (bundle &  ( (__uint128_t)1 << 81) ) ) {
  fprintf (stdout, "bit selected\n");
  }
Thanks all for your help.
 
Back
Top