Advertisement
Ameisen

Untitled

May 21st, 2022
1,201
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2. private static void ScrambleAccumulatorSse2(ulong* accumulator, byte* secret) {
  3.     byte* accumulatorBytes = (byte*)accumulator;
  4.     var prime32 = Vector128.Create(Prime32.Prime0);
  5.  
  6.     if (UnrollCount > 2) {
  7.         var accumulatorVec0 = Sse2.LoadVector128(accumulatorBytes + 0).AsUInt64();
  8.         var accumulatorVec1 = Sse2.LoadVector128(accumulatorBytes + 16).AsUInt64();
  9.         var accumulatorVec2 = Sse2.LoadVector128(accumulatorBytes + 32).AsUInt64();
  10.         var accumulatorVec3 = Sse2.LoadVector128(accumulatorBytes + 48).AsUInt64();
  11.         var shifted0 = Sse2.ShiftRightLogical(accumulatorVec0, 47);
  12.         var shifted1 = Sse2.ShiftRightLogical(accumulatorVec1, 47);
  13.         var shifted2 = Sse2.ShiftRightLogical(accumulatorVec2, 47);
  14.         var shifted3 = Sse2.ShiftRightLogical(accumulatorVec3, 47);
  15.         var dataVec0 = Sse2.Xor(accumulatorVec0, shifted0);
  16.         var dataVec1 = Sse2.Xor(accumulatorVec1, shifted1);
  17.         var dataVec2 = Sse2.Xor(accumulatorVec2, shifted2);
  18.         var dataVec3 = Sse2.Xor(accumulatorVec3, shifted3);
  19.  
  20.         var keyVec0 = Sse2.LoadVector128(secret + 0).AsUInt64();
  21.         var keyVec1 = Sse2.LoadVector128(secret + 16).AsUInt64();
  22.         var keyVec2 = Sse2.LoadVector128(secret + 32).AsUInt64();
  23.         var keyVec3 = Sse2.LoadVector128(secret + 48).AsUInt64();
  24.         var dataKey0 = Sse2.Xor(dataVec0, keyVec0.AsUInt64());
  25.         var dataKey1 = Sse2.Xor(dataVec1, keyVec1.AsUInt64());
  26.         var dataKey2 = Sse2.Xor(dataVec2, keyVec2.AsUInt64());
  27.         var dataKey3 = Sse2.Xor(dataVec3, keyVec3.AsUInt64());
  28.  
  29.         var dataKeyHi0 = Sse2.Shuffle(dataKey0.AsUInt32(), ShuffleDataKey);
  30.         var dataKeyHi1 = Sse2.Shuffle(dataKey1.AsUInt32(), ShuffleDataKey);
  31.         var dataKeyHi2 = Sse2.Shuffle(dataKey2.AsUInt32(), ShuffleDataKey);
  32.         var dataKeyHi3 = Sse2.Shuffle(dataKey3.AsUInt32(), ShuffleDataKey);
  33.         var productLo0 = Sse2.Multiply(dataKey0.AsUInt32(), prime32);
  34.         var productLo1 = Sse2.Multiply(dataKey1.AsUInt32(), prime32);
  35.         var productLo2 = Sse2.Multiply(dataKey2.AsUInt32(), prime32);
  36.         var productLo3 = Sse2.Multiply(dataKey3.AsUInt32(), prime32);
  37.         var productHi0 = Sse2.Multiply(dataKeyHi0.AsUInt32(), prime32);
  38.         var productHi1 = Sse2.Multiply(dataKeyHi1.AsUInt32(), prime32);
  39.         var productHi2 = Sse2.Multiply(dataKeyHi2.AsUInt32(), prime32);
  40.         var productHi3 = Sse2.Multiply(dataKeyHi3.AsUInt32(), prime32);
  41.  
  42.         productHi0 = Sse2.ShiftLeftLogical(productHi0, 32);
  43.         productHi1 = Sse2.ShiftLeftLogical(productHi1, 32);
  44.         productHi2 = Sse2.ShiftLeftLogical(productHi2, 32);
  45.         productHi3 = Sse2.ShiftLeftLogical(productHi3, 32);
  46.  
  47.         var sum0 = Sse2.Add(productLo0, productHi0);
  48.         var sum1 = Sse2.Add(productLo1, productHi1);
  49.         var sum2 = Sse2.Add(productLo2, productHi2);
  50.         var sum3 = Sse2.Add(productLo3, productHi3);
  51.  
  52.         Sse2.Store((ulong*)(accumulatorBytes + 0), sum0);
  53.         Sse2.Store((ulong*)(accumulatorBytes + 16), sum1);
  54.         Sse2.Store((ulong*)(accumulatorBytes + 32), sum2);
  55.         Sse2.Store((ulong*)(accumulatorBytes + 48), sum3);
  56.     }
  57.     else if (UnrollCount == 2) {
  58.         for (uint i = 0; i < StripeLength; i += 32) {
  59.             var accumulatorVec0 = Sse2.LoadVector128(accumulatorBytes + i + 0).AsUInt64();
  60.             var accumulatorVec1 = Sse2.LoadVector128(accumulatorBytes + i + 16).AsUInt64();
  61.             var shifted0 = Sse2.ShiftRightLogical(accumulatorVec0, 47);
  62.             var shifted1 = Sse2.ShiftRightLogical(accumulatorVec1, 47);
  63.             var dataVec0 = Sse2.Xor(accumulatorVec0, shifted0);
  64.             var dataVec1 = Sse2.Xor(accumulatorVec1, shifted1);
  65.  
  66.             var keyVec0 = Sse2.LoadVector128(secret + i + 0).AsUInt64();
  67.             var keyVec1 = Sse2.LoadVector128(secret + i + 16).AsUInt64();
  68.             var dataKey0 = Sse2.Xor(dataVec0, keyVec0.AsUInt64());
  69.             var dataKey1 = Sse2.Xor(dataVec1, keyVec1.AsUInt64());
  70.  
  71.             var dataKeyHi0 = Sse2.Shuffle(dataKey0.AsUInt32(), ShuffleDataKey);
  72.             var dataKeyHi1 = Sse2.Shuffle(dataKey1.AsUInt32(), ShuffleDataKey);
  73.             var productLo0 = Sse2.Multiply(dataKey0.AsUInt32(), prime32);
  74.             var productLo1 = Sse2.Multiply(dataKey1.AsUInt32(), prime32);
  75.             var productHi0 = Sse2.Multiply(dataKeyHi0.AsUInt32(), prime32);
  76.             var productHi1 = Sse2.Multiply(dataKeyHi1.AsUInt32(), prime32);
  77.  
  78.             productHi0 = Sse2.ShiftLeftLogical(productHi0, 32);
  79.             productHi1 = Sse2.ShiftLeftLogical(productHi1, 32);
  80.  
  81.             var sum0 = Sse2.Add(productLo0, productHi0);
  82.             var sum1 = Sse2.Add(productLo1, productHi1);
  83.  
  84.             Sse2.Store((ulong*)(accumulatorBytes + i + 0), sum0);
  85.             Sse2.Store((ulong*)(accumulatorBytes + i + 16), sum1);
  86.         }
  87.     }
  88.     else {
  89.         for (uint i = 0; i < StripeLength; i += 16) {
  90.             var accumulatorVec = Sse2.LoadVector128(accumulatorBytes + i).AsUInt64();
  91.             var shifted = Sse2.ShiftRightLogical(accumulatorVec, 47);
  92.             var dataVec = Sse2.Xor(accumulatorVec, shifted);
  93.  
  94.             var keyVec = Sse2.LoadVector128(secret + i).AsUInt64();
  95.             var dataKey = Sse2.Xor(dataVec, keyVec.AsUInt64());
  96.  
  97.             var dataKeyHi = Sse2.Shuffle(dataKey.AsUInt32(), ShuffleDataKey);
  98.             var productLo = Sse2.Multiply(dataKey.AsUInt32(), prime32);
  99.             var productHi = Sse2.Multiply(dataKeyHi.AsUInt32(), prime32);
  100.  
  101.             productHi = Sse2.ShiftLeftLogical(productHi, 32);
  102.  
  103.             var sum = Sse2.Add(productLo, productHi);
  104.  
  105.             Sse2.Store((ulong*)(accumulatorBytes + i), sum);
  106.         }
  107.     }
  108. }
Advertisement
RAW Paste Data Copied
Advertisement