Skip to content

Instantly share code, notes, and snippets.

@EgorBo
Created October 21, 2021 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EgorBo/7e8e7eca52a884e472713158e636740a to your computer and use it in GitHub Desktop.
Save EgorBo/7e8e7eca52a884e472713158e636740a to your computer and use it in GitHub Desktop.
vpopcnt.cs
static Vector128<uint> VPopcnt(Vector128<uint> dataArg)
{
var data = dataArg; // workaround for a codegen issue
data = Sse2.Subtract(data, Sse2.And(Sse2.ShiftRightLogical(data, 1), Vector128.Create((uint)0x55555555)));
data = Sse2.Add(Sse2.And(data, Vector128.Create((uint)0x33333333)),
Sse2.And(Sse2.ShiftRightLogical(data, 2), Vector128.Create((uint)0x33333333)));
data = Sse2.And(Sse2.Add(data, Sse2.ShiftRightLogical(data, 4)), Vector128.Create((uint)0x0F0F0F0F));
data = Sse2.Add(data, Sse2.ShiftRightLogical(data, 8));
data = Sse2.Add(data, Sse2.ShiftRightLogical(data, 16));
data = Sse2.And(data, Vector128.Create((uint)0x0000003F));
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment