Support for gcc vector attributes, SIMD builtins

Daniel Gibson metalcaedes at gmail.com
Tue Feb 1 03:12:44 PST 2011


Am 01.02.2011 09:10, schrieb Mike Farnsworth:
> I built gdc from tip on Fedora 13 (x86-64) and started playing around
> with creating a vector struct (x,y,z,w) to see what kind of optimization
> the code generator did with it.  It was able to partially drop into SSE
> registers and instructions, but not as well as I had hoped from writing
> "regular" D code.
>
> I poked through the builtins that get pulled into d-builtins.c /
> d-builtins2.cc but I don't see anything that might be pulling in
> definitions such as __builtin_ia32_* for SSE, for example.
>
> How hard would it be to get some sort of vector attribute attached to a
> type (or just plain indroduce v4sf, __m128, or something like that) and
> get those SIMD builtins available?
>
> For the curious, here are how they are defined in, for example,
> xmmintrin.h for gcc:
>
> typedef float __m128 __attribute__ ((__vector_size__ (16), __may_alias__));
>
> typedef float __v4sf __attribute__ ((__vector_size__ (16)));
>
> extern __inline __m128 __attribute__((__gnu_inline__, __always_inline__,
> __artificial__))
> _mm_add_ps (__m128 __A, __m128 __B)
> {
>    return (__m128) __builtin_ia32_addps ((__v4sf)__A, (__v4sf)__B);
> }
>
> I'm game for making an attempt myself if someone can point me in the
> right direction.  I'm a hardcore ray tracing / rendering guy, and
> performance is of the utmost importance.  If I could write a ray tracer
> in D that matches my C++ tracer for performance, I'd be ecstatic.
>
> -Mike
>

I'm not sure if that'll help at all, but you may try something like
alias float[4] vec4; // or whatever type you're using
/Maybe/ SSE optimizations work better on arrays than on structs.
Of course, such a type isn't as handy because it'll be vec4[0] instead 
of vec4.x, but it may be worth a try..
If it helps (i.e. SSE is used better) you could go on trying to put that 
vector in a struct, have x, y, z, w as properties[1] that get/set the 
corresponding fields in the array and overload operators so they work 
directly on the array.

Cheers,
- Daniel

[1] http://digitalmars.com/d/2.0/property.html at the bottom of the 
page. I guess this will cause little/no overhead because of inlining.


More information about the D.gnu mailing list