Click here to Skip to main content
15,914,795 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Glad About the Hype Pin
W Balboos, GHB15-Mar-17 6:41
W Balboos, GHB15-Mar-17 6:41 
GeneralXKCDOTD Pin
Mel Padden14-Mar-17 1:34
Mel Padden14-Mar-17 1:34 
GeneralRe: XKCDOTD Pin
CHill6014-Mar-17 1:45
mveCHill6014-Mar-17 1:45 
GeneralRe: XKCDOTD Pin
Eddy Vluggen14-Mar-17 2:47
professionalEddy Vluggen14-Mar-17 2:47 
GeneralRe: XKCDOTD Pin
Chris Quinn14-Mar-17 3:07
Chris Quinn14-Mar-17 3:07 
GeneralRe: XKCDOTD Pin
Mark_Wallace14-Mar-17 4:38
Mark_Wallace14-Mar-17 4:38 
GeneralRe: XKCDOTD Pin
dandy7215-Mar-17 6:32
dandy7215-Mar-17 6:32 
General"Fixing" SIMD in C#, preview PinPopular
harold aptroot14-Mar-17 0:52
harold aptroot14-Mar-17 0:52 
Instead of just complaining about it, I set out to fix it. Although maybe "fix" is a big word for what I'm doing since I'm not building it into the language, just adding it on as a class library. As a result it does not mix with normal code well.

Here is an example of what it would look like:
SIMDcode s = new SIMDcode(_ =>
{
    var ptr = _.arg(0);
    var size = _.arg(1);
    var constants = _.arg(2);
    var w = _._mm_loadu_si128(constants, 0);
    var shufmask = _._mm_loadu_si128(constants, 16);
    var alpha = _._mm_loadu_si128(constants, 32);
    _.repeat(size, 4);
    {
        var p = _._mm_load_si128(ptr, 0);
        p = _._mm_maddubs_epi16(p, w);
        p = _._mm_add_epi32(p, _._mm_srli_si128(p, 2));
        p = _._mm_srli_epi32(p, 6);
        p = _._mm_shuffle_epi8(p, shufmask);
        p = _._mm_or_si128(p, alpha);
        _._mm_store_si128(ptr, 0, p);
        _.add_r_i(ptr, 16);
    }
    _.endrepeat();
});
The "meat" looks just like "intrinsics" like you're used to (or not, if you've never worked with them), but in this case they're not proper intrinsic functions. When executed they build up some internal representation of the code, which is then compiled and written to unmanaged memory. That is also why it needs the "weird stuff" such as `arg` and `repeat` and `add_r_i` - normally normal code fills those rolls but here normal C# code will just execute once, it does not itself end up in the native code.

The native code generated by the example above is
ASM
    push   rbp
    mov    rbp,rsp
    vmovdqu xmm0,XMMWORD PTR [r8]
    vmovdqu xmm1,XMMWORD PTR [r8+0x10]
    vmovdqu xmm2,XMMWORD PTR [r8+0x20]
15: vmovdqa xmm3,XMMWORD PTR [rcx]
    vpmaddubsw xmm3,xmm3,xmm0
    vpsrldq xmm4,xmm3,0x2
    vpaddd xmm3,xmm3,xmm4
    vpsrld xmm3,xmm3,0x6
    vpshufb xmm3,xmm3,xmm1
    vpor   xmm3,xmm3,xmm2
    vmovdqa XMMWORD PTR [rcx],xmm3
    add    rcx,0x10
    sub    rdx,0x4
    jnz    0x15
    leave
    ret
To actually invoke the code you define a delegate and ask for a function pointer of that type:
delegate void ToGrayDelegate(IntPtr pixels, int length, byte[] constants);
...
var f = s.GetDelegate<ToGrayDelegate>(0);

Just invoke that as normal. (this runs in 1 cycle per pixel on Haswell, the best I could do in pure C# was 6 cycles per pixel)

This is just a preview, and maybe that's all it will ever be. A bunch of things work, a lot of things do not work yet, because I just threw this whole together in a day.

Anyway I'm not sure how useful this actually is. On the one hand, you wouldn't need a separate C++ project that has to be kept in sync with the main project (but that's not even a real problem). On the other hand, it's more annoying to write code like in the example than to just write C++ with intrinsics. But this way the code can be tuned with run-time parameters (but is that even useful? and you can't do it too often since compiling more code obviously costs time). Theoretically this can easily support x86 and x64 simultaneously (that's a bit of a hassle with native dlls), but choosing one of them at compile time isn't really a problem anyway.
Compared to System.Numerics.Vectors I'd say this is a lot better - it actually allows you to get stuff done, there are all the usual things that you need such as shuffles and shifts and oddball instructions like vpmaddubsw. But compared to a C++ dll it doesn't seem all that useful.
So what do you think. Useful? Interesting? Total waste of time?

modified 14-Mar-17 8:17am.

GeneralRe: "Fixing" SIMD in C#, preview PinPopular
Pete O'Hanlon14-Mar-17 1:17
mvePete O'Hanlon14-Mar-17 1:17 
GeneralRe: "Fixing" SIMD in C#, preview Pin
Slacker00714-Mar-17 1:48
professionalSlacker00714-Mar-17 1:48 
GeneralRe: "Fixing" SIMD in C#, preview Pin
Nish Nishant14-Mar-17 2:23
sitebuilderNish Nishant14-Mar-17 2:23 
GeneralRe: "Fixing" SIMD in C#, preview Pin
Daniel Pfeffer14-Mar-17 1:29
professionalDaniel Pfeffer14-Mar-17 1:29 
PraiseRe: "Fixing" SIMD in C#, preview Pin
den2k8814-Mar-17 2:36
professionalden2k8814-Mar-17 2:36 
GeneralCareer advice of the day: Advocate for yourself PinPopular
Slacker00714-Mar-17 0:17
professionalSlacker00714-Mar-17 0:17 
GeneralRe: Career advice of the day: Advocate for yourself Pin
Mark_Wallace14-Mar-17 0:36
Mark_Wallace14-Mar-17 0:36 
GeneralRe: Career advice of the day: Advocate for yourself Pin
Slacker00714-Mar-17 0:42
professionalSlacker00714-Mar-17 0:42 
GeneralRe: Career advice of the day: Advocate for yourself Pin
Pom Pey314-Mar-17 1:35
Pom Pey314-Mar-17 1:35 
GeneralRe: Career advice of the day: Advocate for yourself Pin
Slacker00714-Mar-17 1:46
professionalSlacker00714-Mar-17 1:46 
GeneralRe: Career advice of the day: Advocate for yourself Pin
Mark_Wallace14-Mar-17 2:50
Mark_Wallace14-Mar-17 2:50 
GeneralRe: Career advice of the day: Advocate for yourself Pin
Nemanja Trifunovic14-Mar-17 3:17
Nemanja Trifunovic14-Mar-17 3:17 
GeneralRe: Career advice of the day: Advocate for yourself Pin
Richard Andrew x6414-Mar-17 6:38
professionalRichard Andrew x6414-Mar-17 6:38 
GeneralRe: Career advice of the day: Advocate for yourself Pin
KC@CahabaGBA15-Mar-17 3:17
KC@CahabaGBA15-Mar-17 3:17 
GeneralRe: Career advice of the day: Advocate for yourself Pin
Mark Starr15-Mar-17 3:30
professionalMark Starr15-Mar-17 3:30 
GeneralRe: Career advice of the day: Advocate for yourself Pin
F-ES Sitecore14-Mar-17 1:13
professionalF-ES Sitecore14-Mar-17 1:13 
GeneralRe: Career advice of the day: Advocate for yourself Pin
Hooga Booga15-Mar-17 3:02
Hooga Booga15-Mar-17 3:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.