private sbyte[] Calculate(sbyte[] inputSignal, sbyte[] pattern, int digitsToSkip, sbyte[] output = null) { output ??= new sbyte[inputSignal.Length]; //for (int i = 0; i < inputSignal.Length; i++) Parallel.For(0, inputSignal.Length, i => { var iterationPattern = new RepeatingDigits(pattern, i + 1); int sum = 0; for (int j = 0; j < inputSignal.Length; j++) { var a = inputSignal[j]; var b = iterationPattern[j + digitsToSkip]; sum += a * b; } var onesDigit = (sbyte)new IndexableNumber(sum)[0]; // only keep the position in the 1s place output[i] = onesDigit; } ); //Console.WriteLine(); //Console.WriteLine(string.Join("", output)); return(output); }
// same function as the old one above, but uses iterators, no arrays, and no linq private sbyte[] Calculate(sbyte[] inputSignal, sbyte[] pattern, int digitsToSkip, sbyte[] output = null) { output ??= new sbyte[inputSignal.Length]; //for (int i = 0; i < inputSignal.Length; i++) Parallel.For(0, inputSignal.Length, i => { var iterationPattern = new RepeatingDigits(pattern, i + 1); //int sum = 0; //for (int j = 0; j < inputSignal.Length; j++) //{ // var a = inputSignal[j]; // var b = iterationPattern[j + digitsToSkip]; // sum += a * b; //} //var onesDigita = (sbyte)new IndexableNumber(sum)[0]; // only keep the position in the 1s place // simd var offset = Vector <sbyte> .Count; var muls = new sbyte[offset]; var sumd = Vector <sbyte> .Zero; int k = 0; if (offset < inputSignal.Length) { for (; k < inputSignal.Length - offset; k += offset) { // populate the muls for (int m = 0; m < offset; m++) { muls[m] = iterationPattern[k + m + digitsToSkip]; } var v1 = new Vector <sbyte>(inputSignal, k); var v2 = new Vector <sbyte>(muls); sumd += v1 * v2; } } int total = 0; for (int t = 0; t < offset; t++) { total += sumd[t]; } for (; k < inputSignal.Length; k++) { total += (inputSignal[k] * iterationPattern[k + digitsToSkip]); } var onesDigit = (sbyte)new IndexableNumber(total)[0]; // only keep the position in the 1s place output[i] = onesDigit; //Debug.Assert(onesDigita == onesDigit); } ); //Console.WriteLine(); //Console.WriteLine(string.Join("", output)); return(output); }