Created
May 20, 2019 21:20
-
-
Save Mrnikbobjeff/d34845d810ae31f34eb5c095c1b2693d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public unsafe int FreeFasterSimplifiedAsmAlignedNonTemporalUnrolledUnsafePin(T obj) | |
{ | |
var items = _items; | |
var length = items.Length; | |
var nullVector = Vector256<long>.Zero; | |
fixed (long* addrPtr = &itemsRef[0]) | |
{ | |
var aligned = (long*)(((ulong)addrPtr + 31UL) & ~31UL); | |
var pos = (int)(aligned - addrPtr); | |
for (int w = 0; w < pos; w++) | |
if (addrPtr[w] == 0) | |
{ | |
items[w].Value = obj; | |
return w; | |
} | |
int i = 0; | |
const int vSizeLong = 4; | |
const int bSizeLong = 8; | |
var lengthWithoutBlockSize = length - bSizeLong; | |
for (; i <= lengthWithoutBlockSize; i += bSizeLong) | |
{ | |
var v2i = i + vSizeLong; | |
var v21 = Avx2.LoadAlignedVector256NonTemporal(aligned + i); | |
var v22 = Avx2.LoadAlignedVector256NonTemporal(aligned + v2i); | |
var temp1 = Avx2.CompareEqual(v21, nullVector).AsDouble(); | |
var m1 = Avx.MoveMask(temp1); | |
var temp2 = Avx2.CompareEqual(v22, nullVector).AsDouble(); | |
var m2 = Avx.MoveMask(temp2); | |
if (m1 + m2 == 0) | |
continue; | |
if (m1 == 0) | |
{ | |
i += vSizeLong; | |
} | |
goto nullOrFound; | |
} | |
nullOrFound: | |
for (int x = i + pos; x < length; x++) | |
{ | |
if (addrPtr[x] == 0) | |
{ | |
return x; | |
} | |
} | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment