[cfe-dev] Complex values handling in Clang

Stephen Canon scanon at apple.com
Fri Feb 1 07:32:00 PST 2013

_Complex integer types are a GCC extension (the standard specifies only float _Complex, double _Complex and long double _Complex.  Apparently clang's support for this GCC extension doesn't perfectly match GCC.  I suspect that clang and GCC differ in arithmetic promotion rank here; clang "promotes" the unsigned long long literals to int _Complex (which truncates them), whereas GCC promotes both to unsigned long long _Complex.  I haven't actually dug into the issue at all, however.

A couple of work-arounds seem to be available:

	unsigned long long int _Complex lli_val = (unsigned long long int _Complex){5668231682443686620ULL, 7618395480654047911ULL};

	unsigned long long int _Complex lli_val;
	__real__ lli_val = 5668231682443686620ULL;
	__imag__ lli_val = 7618395480654047911ULL;

I believe that both of these will work with recent GCCs as well.

- Steve

On Feb 1, 2013, at 12:06 AM, rajesh viswabramana <viswabramana.rajesh at gmail.com> wrote:

> Hi all,
> Could someone comment about it.
> Thanks,
> Rajesh
> On Mon, Jan 28, 2013 at 7:16 PM, rajesh viswabramana <viswabramana.rajesh at gmail.com> wrote:
> Hi All,
> Complex values stored in variable value mismatches if program compiled clang and gcc.
> Below is the sample program printing byte by byte stored "complex long long int" value,
> int main()
> {
>  _Complex unsigned long long int lli_val= (5668231682443686620ULL + 7618395480654047911ULL*__extension__ 1i);
> // _Complex long double ld_val = (5668231682443686620ULL + 7618395480654047911ULL*__extension__ 1i);
>  int size = sizeof(lli_val);
>  int index = 0;
>  printf("long long int val:\n");
>  while (size != index)
>  { 
>     unsigned char cval = (unsigned char)*(((unsigned char*)&lli_val) + index);
>     printf("%x", cval);
>     index++;
>  }
> }
> Outputs of clang compiled, gcc compiled values are:
> For Clang,
> long long int val:
> dcb2ab00000a76ad35e0000
> For GCC,
> long long int val:
> dcb2ab0af9aa94ea76ad35e53fab969
> Similar behaviour observed for "_Complex float, _Complex double, _Complex long double" types.
> Complex values in clang are not handled in similar to GCC, Could someone comment about it.

More information about the cfe-dev mailing list