vector constructors

classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

vector constructors

Jochen Wilhelmy
Hi!

I'd like to do this myself but I have to dig into clang only for this
wich takes some
time therefore I'd like to ask if someone is already is working on it
(or wants to do it ;-) :

for c, the cast operator is extended to vectors (opencl):
(int)(1)
(int4)(1, 2, 3, 4)

for c++ the same could be done:
int(1)
int4(1, 2, 3, 4)

I use this variant since it can be emulated with c++ classes when no vector
extensions are available.

Will it be supported in the near future?

-Jochen

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: vector constructors

Chris Lattner

On Mar 16, 2010, at 11:39 AM, Jochen Wilhelmy wrote:

> Hi!
>
> I'd like to do this myself but I have to dig into clang only for this
> wich takes some
> time therefore I'd like to ask if someone is already is working on it
> (or wants to do it ;-) :
>
> for c, the cast operator is extended to vectors (opencl):
> (int)(1)
> (int4)(1, 2, 3, 4)
>
> for c++ the same could be done:
> int(1)
> int4(1, 2, 3, 4)
>
> I use this variant since it can be emulated with c++ classes when no vector
> extensions are available.
>
> Will it be supported in the near future?

This is already supported with -faltivec, you can enable the LangOptions flag to get this.

-Chris
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: vector constructors

Jochen Wilhelmy
Hi!


> This is already supported with -faltivec, you can enable the LangOptions flag to get this.
>    
Unfortunately I didn't get it to work.
I have langOptions.AltiVec = 1; and I tried
typedef __vector float float4;
and
typedef float __attribute__((ext_vector_type(4))) float4;

I get this error:

testc.cpp:51:23:{51:14-51:33}: error: function-style cast to a builtin
type can
       only take one argument
          float4 b = float4(1, 2, 3, 4);

- Jochen

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: vector constructors

Chris Lattner

On Mar 16, 2010, at 12:05 PM, Jochen Wilhelmy wrote:

> Hi!
>
>
>> This is already supported with -faltivec, you can enable the LangOptions flag to get this.
>>  
> Unfortunately I didn't get it to work.
> I have langOptions.AltiVec = 1; and I tried
> typedef __vector float float4;
> and
> typedef float __attribute__((ext_vector_type(4))) float4;
>
> I get this error:
>
> testc.cpp:51:23:{51:14-51:33}: error: function-style cast to a builtin type can
>      only take one argument
>         float4 b = float4(1, 2, 3, 4);

This works for me:

$ cat t.c
void foo() {
  vector float x;
  x = (vector float)(1,2,3,4);
}
$ clang -cc1 t.c -fsyntax-only -faltivec



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: vector constructors

Jochen Wilhelmy

> This works for me:
>
> $ cat t.c
> void foo() {
>   vector float x;
>   x = (vector float)(1,2,3,4);
> }
> $ clang -cc1 t.c -fsyntax-only -faltivec
>
>
>  
Yes I know that this works. It's the c-like extension of the
cast operator to vectors, but in contrast I'd like to have
a c++-like extension of the function style cast:

int(1)
int4(1, 2, 3, 4)


-Jochen

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: vector constructors

Douglas Gregor

On Mar 16, 2010, at 2:00 PM, Jochen Wilhelmy wrote:

>
>> This works for me:
>>
>> $ cat t.c
>> void foo() {
>>  vector float x;
>>  x = (vector float)(1,2,3,4);
>> }
>> $ clang -cc1 t.c -fsyntax-only -faltivec
>>
>>
>>
> Yes I know that this works. It's the c-like extension of the
> cast operator to vectors, but in contrast I'd like to have
> a c++-like extension of the function style cast:
>
> int(1)
> int4(1, 2, 3, 4)

Do other compilers support this syntax? (e.g., GCC)?

        - Doug
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: vector constructors

Jochen Wilhelmy

>>
>> int(1)
>> int4(1, 2, 3, 4)
>>    
>
> Do other compilers support this syntax? (e.g., GCC)?
>  

Cg, glsl and hlsl support it.

- Jochen

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: vector constructors

Chris Lattner

On Mar 16, 2010, at 2:22 PM, Jochen Wilhelmy wrote:

>
>>>
>>> int(1)
>>> int4(1, 2, 3, 4)
>>>    
>>
>> Do other compilers support this syntax? (e.g., GCC)?
>>  
>
> Cg, glsl and hlsl support it.

We do not aim to implement those languages in clang.

-Chris
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: vector constructors

Jochen Wilhelmy

>> Cg, glsl and hlsl support it.
>>    
>
> We do not aim to implement those languages in clang.
>
>  
I do not expect this, but will a patch for only the function style
constructors
be accepted into mainline?
I think it's a natural extension of c++ syntax to vectors.

-Jochen

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: vector constructors

John McCall

On Mar 16, 2010, at 2:39 PM, Jochen Wilhelmy wrote:

>
>>> Cg, glsl and hlsl support it.
>>>
>>
>> We do not aim to implement those languages in clang.
>>
>>
> I do not expect this, but will a patch for only the function style
> constructors
> be accepted into mainline?
> I think it's a natural extension of c++ syntax to vectors.

If you're okay with accepting it only in C++, I think it's a reasonable extension.

John.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: vector constructors

Chris Lattner
In reply to this post by Jochen Wilhelmy

On Mar 16, 2010, at 2:39 PM, Jochen Wilhelmy wrote:

>
>>> Cg, glsl and hlsl support it.
>>>    
>>
>> We do not aim to implement those languages in clang.
>>
>>  
> I do not expect this, but will a patch for only the function style constructors
> be accepted into mainline?
> I think it's a natural extension of c++ syntax to vectors.

In my opinion, there is no reason to do this.  If you want C++ syntax to do this, define your_float4 as a struct with a constructor.  There is already too much needless diversity in vector initialization, and C++'0x may add a whole new world of pain here.

-Chris
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: vector constructors

Jochen Wilhelmy
In reply to this post by John McCall

> If you're okay with accepting it only in C++, I think it's a reasonable extension.
>
>  
of course only in c++ mode (like int(1) also only works in c++ mode)

- Jochen

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: vector constructors

Jochen Wilhelmy
In reply to this post by Chris Lattner

> In my opinion, there is no reason to do this.  If you want C++ syntax to do this, define your_float4 as a struct with a constructor.  There is already too much needless diversity in vector initialization, and C++'0x may add a whole new world of pain here.
>  
Then I can keep my current implementation that does not use vectors and
use an auto-vectorization
pass. By the way what is the current state of the art on this topic?

But maybe I add the function style constructor to my local copy since
not having it raises other
questions like default-initialization:

struct Foo
{
    Foo() : x(), y() {}

    int x;
    int4 y;
};

If I get it working I will ask again ;-)

- Jochen

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: vector constructors

Jochen Wilhelmy
In reply to this post by Chris Lattner
Hi!

just to capture the current state of c++ style vector support I made a
little test:

struct Foo
{
     Foo(int) : x() {} // works
     Foo(float f) : x(f) {} // works
     Foo(float4 x) : x(x) {} // works
     Foo() : x(1, 2, 3, 4) {} // error: excess elements in scalar
initializer

     float4 bar1() {return float4();} // works
     float4 bar2() {return float4(f);} // works
     float4 bar3() {return float4(x);} // works
     float4 bar4() {return float4(1, 2, 3, 4);} // error: function-style
cast to a builtin type can only take one argument

     float f;
     float4 x;
};

I only checked if the statements compile.

-Jochen

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: vector constructors

Chris Lattner
Looks right to me!

-Chris

On Mar 17, 2010, at 9:08 AM, Jochen Wilhelmy <[hidden email]>  
wrote:

> Hi!
>
> just to capture the current state of c++ style vector support I made a
> little test:
>
> struct Foo
> {
>     Foo(int) : x() {} // works
>     Foo(float f) : x(f) {} // works
>     Foo(float4 x) : x(x) {} // works
>     Foo() : x(1, 2, 3, 4) {} // error: excess elements in scalar
> initializer
>
>     float4 bar1() {return float4();} // works
>     float4 bar2() {return float4(f);} // works
>     float4 bar3() {return float4(x);} // works
>     float4 bar4() {return float4(1, 2, 3, 4);} // error: function-
> style
> cast to a builtin type can only take one argument
>
>     float f;
>     float4 x;
> };
>
> I only checked if the statements compile.
>
> -Jochen
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: vector constructors

Jochen Wilhelmy
while being allowed to write float4(1,2,3,4) would be deluxe-support that
I know from graphics-centric compilers I found another way: e.g.
float4 makeFloat4(float a, float b, float c, float d){return (float4)(a,
b, c, d);}

this can look the same from the outside if no vector support is
available and
float4 is a struct with no constructors.

-Jochen

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: vector constructors

Jochen Wilhelmy
In reply to this post by Chris Lattner
Hi!

while playing with vectors I get this error for int. it works for float:

typedef int __attribute__((ext_vector_type(4))) int4;

int4 foo = (int4)(1, 2, 3, 4);

testc.cpp:72:13:{72:13-72:19}: error: C-style cast from scalar 'int' to
vector
       'int4' of different size
          int4 foo = (int4)(1, 2, 3, 4);


-Jochen
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev