www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Vector Swizzling in D

reply "Boscop" <nospam example.com> writes:
Hi everyone,

I wrote a blog post for people who know a bit of D and want to 
dig deeper, it shows different approaches to get vector swizzling 
syntax in D:

http://boscop.tk/blog/?p=1

There is nothing revolutionary involved but it might still be 
useful to someone.
Comments and criticism are welcome.
Mar 14 2012
next sibling parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 14.03.2012 18:57, Boscop wrote:
 Hi everyone,

 I wrote a blog post for people who know a bit of D and want to dig
 deeper, it shows different approaches to get vector swizzling syntax in D:

 http://boscop.tk/blog/?p=1

 There is nothing revolutionary involved but it might still be useful to
 someone.
 Comments and criticism are welcome.
Why I'm not seeing opDispatch anywhere ? Take it as criticism :) -- Dmitry Olshansky
Mar 14 2012
parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 14.03.2012 19:03, Dmitry Olshansky wrote:
 On 14.03.2012 18:57, Boscop wrote:
 Hi everyone,

 I wrote a blog post for people who know a bit of D and want to dig
 deeper, it shows different approaches to get vector swizzling syntax
 in D:

 http://boscop.tk/blog/?p=1

 There is nothing revolutionary involved but it might still be useful to
 someone.
 Comments and criticism are welcome.
Why I'm not seeing opDispatch anywhere ? Take it as criticism :)
Nevermind, my browser tricked me to believe it was a single page. -- Dmitry Olshansky
Mar 14 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Mar 14, 2012 at 03:57:02PM +0100, Boscop wrote:
 Hi everyone,
 
 I wrote a blog post for people who know a bit of D and want to dig
 deeper, it shows different approaches to get vector swizzling syntax
 in D:
 
 http://boscop.tk/blog/?p=1
 
 There is nothing revolutionary involved but it might still be useful
 to someone.
 Comments and criticism are welcome.
I like it. It's a good way to show off what's possible with opDispatch. :-) Reminds me of implementing Roman numerals with opDispatch, that was discussed recently. T -- Prosperity breeds contempt, and poverty breeds consent. -- Suck.com
Mar 14 2012
prev sibling parent reply Don Clugston <dac nospam.com> writes:
On 14/03/12 15:57, Boscop wrote:
 Hi everyone,

 I wrote a blog post for people who know a bit of D and want to dig
 deeper, it shows different approaches to get vector swizzling syntax in D:

 http://boscop.tk/blog/?p=1

 There is nothing revolutionary involved but it might still be useful to
 someone.
 Comments and criticism are welcome.
In the last bit of code, why not use CTFE for valid(string s) instead of templates? bool valid(string s) { foreach(c; s) { if (c < 'w' || c > 'z') return false; } return true; } In fact you can use CTFE for the other template functions as well.
Mar 14 2012
parent reply "Boscop" <nospam example.com> writes:
On Wednesday, 14 March 2012 at 17:35:06 UTC, Don Clugston wrote:
 In the last bit of code, why not use CTFE for valid(string s) 
 instead of templates?

 bool valid(string s)
 {
    foreach(c; s)
    {
      if (c < 'w' || c > 'z') return false;
    }
    return true;
 }

 In fact you can use CTFE for the other template functions as 
 well.
In the original version I actually did this, but even with -O -inline -release the opDispatchs call didn't get inlined. I thought it was caused by CTFE-code that prevented the inlining. FWIW, this was the original code using CTFE: --- import std.algorithm: reduce; struct Vec { double[4] v; property auto X() {return v[0];} property auto Y() {return v[1];} property auto Z() {return v[2];} property auto W() {return v[3];} this(double x, double y, double z, double w) {v = [x,y,z,w];} property auto opDispatch(string s)() if(s.length <= 4 && reduce!((s,c)=>s && 'w' <= c && c <= 'z')(true, s)) { char[] p = s.dup; foreach(i; s.length .. 4) p ~= p[$-1]; int i(char c) {return [3,0,1,2][c-'w'];} return Vec(v[i(p[0])], v[i(p[1])], v[i(p[2])], v[i(p[3])]); } } unittest { assert(Vec(5,6,7,8).zyzx == Vec(7, 6, 7, 5)); assert(Vec(5,6,7,8).zyx == Vec(7, 6, 5, 5)); assert(Vec(5,6,7,8).wy == Vec(8, 6, 6, 6)); assert(Vec(5,6,7,8).z == Vec(7, 7, 7, 7)); } --- (I was using reduce here only to demonstrate D's functional features and nice lambda syntax. Maybe that's what prevented inlining?)
Mar 14 2012
parent reply Don Clugston <dac nospam.com> writes:
On 14/03/12 18:46, Boscop wrote:
 On Wednesday, 14 March 2012 at 17:35:06 UTC, Don Clugston wrote:
 In the last bit of code, why not use CTFE for valid(string s) instead
 of templates?

 bool valid(string s)
 {
 foreach(c; s)
 {
 if (c < 'w' || c > 'z') return false;
 }
 return true;
 }

 In fact you can use CTFE for the other template functions as well.
In the original version I actually did this, but even with -O -inline -release the opDispatchs call didn't get inlined. I thought it was caused by CTFE-code that prevented the inlining. FWIW, this was the original code using CTFE: --- import std.algorithm: reduce; struct Vec { double[4] v; property auto X() {return v[0];} property auto Y() {return v[1];} property auto Z() {return v[2];} property auto W() {return v[3];} this(double x, double y, double z, double w) {v = [x,y,z,w];} property auto opDispatch(string s)() if(s.length <= 4 && reduce!((s,c)=>s && 'w' <= c && c <= 'z')(true, s)) { char[] p = s.dup;
This won't be CTFEd, because it's not forced to be a compile-time constant.
 foreach(i; s.length .. 4) p ~= p[$-1];
This too. But, you can do something like: enum p = extend(s); since p is an enum, it must use CTFE.
 int i(char c) {return [3,0,1,2][c-'w'];}
this isn't forced to be CTFE either.
 return Vec(v[i(p[0])], v[i(p[1])], v[i(p[2])], v[i(p[3])]);
 }
 ---
 (I was using reduce here only to demonstrate D's functional features and
 nice lambda syntax. Maybe that's what prevented inlining?)
Mar 15 2012
parent "Boscop" <nospam example.com> writes:
Thanks for the suggestions, I updated the article with an 
improved CTFE version at the end.
Mar 15 2012