public unsafe ReadOnlyProposal[] ForVectorized() { ProposalResult p = ProposalBuilder.GetSortedVectorizedInsurances(); var insuranceId = Vector256.Create(SearchedInsuranceId); fixed(int *iip = p.InsuranceIds) { int i = 0; int length = p.InsuranceIds.Length - Vector256 <int> .Count + 1; int mask = 8; while (mask == 8 && i < length) { mask = (int)Lzcnt.LeadingZeroCount( (uint)Avx2.MoveMask( Vector256.AsByte( Avx2.CompareEqual( Avx2.LoadVector256(iip + i), insuranceId ) ).Reverse() ) ) >> 2; i += Vector256 <int> .Count; } i -= Vector256 <int> .Count; int initial = i + mask; if (initial == p.InsuranceIds.Length) { return(Array.Empty <ReadOnlyProposal>()); } mask = 0; while (mask == 0 && i < length) { mask = (int)Lzcnt.LeadingZeroCount( (uint)Avx2.MoveMask( Vector256.AsByte( Avx2.CompareEqual( Avx2.LoadVector256(iip + i), insuranceId ) ) ) ) >> 2; i += Vector256 <int> .Count; } return(p.Proposals.AsSpan(initial, i - mask - initial).ToArray()); } }
// removed, not testing loop speed //[Benchmark] public unsafe ReadOnlyProposal[] PositionalSortedVectorized() { ProposalResult[] proposals = ProposalBuilder.GetPositionalSortedVectorizedInsurances(); ProposalResult p = proposals[SearchedInsuranceId]; var minPremium = Vector256.Create(decimal.ToOACurrency(SearchedNetPremium)); fixed(long *npp = p.NetPremiums) { int i = 0; int initial = 0; for (; i < p.NetPremiums.Length - Vector256 <long> .Count + 1; i += Vector256 <long> .Count) { int mask = (int)Lzcnt.LeadingZeroCount( (uint)Avx2.MoveMask( Vector256.AsByte( Avx2.CompareGreaterThan( Avx2.LoadVector256(npp + i), minPremium ) ).Reverse() ) ) >> 3; if (mask != 8) { initial = i + mask; break; } } for (; i < p.NetPremiums.Length - Vector256 <long> .Count + 1; i += Vector256 <long> .Count) { int mask = (int)Lzcnt.LeadingZeroCount( (uint)Avx2.MoveMask( Vector256.AsByte( Avx2.CompareGreaterThan( Avx2.LoadVector256(npp + i), minPremium ) ) ) ) >> 3; if (mask != 0) { int length = i + Vector256 <long> .Count - mask - initial; return(p.Proposals.AsSpan(initial, length).ToArray()); } } return(p.Proposals.AsSpan(initial).ToArray()); } }
private static ProposalResult[] CreatePositionalSortedVectorizedProposal() { var grouped = CreateProposal() .Select(p => new ReadOnlyProposal(p)) .GroupBy(p => p.InsuranceId) .ToList(); ProposalResult[] result = new ProposalResult[grouped.Max(p => p.Key) + 1]; foreach (var p in grouped) { result[p.Key] = new ProposalResult(p.OrderByDescending(ps => ps.NetPremium).ToArray()); } return(result); }