Update CoreClr, PgoData to preview1-26004-01, master-20171204-0047, respectively...
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchI / Array1 / Array1.cs
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 //
5 // The sorting benchmark calls a random number generator the number
6 // of times specified by Maxnum to create an array of int integers,
7 // then does a quicksort on the array of ints. Random numbers
8 // are produced using a multiplicative modulus method with known
9 // seed, so that the generated array is constant across compilers.
10 //
11 // This is adapted from a benchmark in BYTE Magazine, August 1984.
12
13 using Microsoft.Xunit.Performance;
14 using System;
15 using System.Runtime.CompilerServices;
16 using Xunit;
17
18 [assembly: OptimizeForBenchmarks]
19
20 namespace Benchstone.BenchI
21 {
22 public static class Array1
23 {
24 #if DEBUG
25     private const int Iterations = 1;
26     private const int Maxnum = 100;
27 #else
28     private const int Iterations = 125;
29     private const int Maxnum = 1000;
30 #endif
31
32     private const int Modulus = ((int)0x20000);
33     private const int C = 13849;
34     private const int A = 25173;
35     static int s_seed = 7;
36
37     private static void Quick(int lo, int hi, int[] input)
38     {
39         int i, j;
40         int pivot, temp;
41
42         if (lo < hi)
43         {
44             // 0 <= lo < hi
45             for (i = lo, j = (hi + 1), pivot = input[lo]; ;)
46             {
47                 do
48                 {
49                     ++i;
50                 } while (input[i] < pivot);
51
52                 do
53                 {
54                     --j;
55                     // Accessing upto hi
56                 } while (input[j] > pivot);
57
58                 if (i < j)
59                 {
60                     temp = input[i];
61                     input[i] = input[j];
62                     input[j] = temp;
63                 }
64                 else
65                 {
66                     break;
67                 }
68             }
69             temp = input[j];
70             input[j] = input[lo];
71             input[lo] = temp;
72             Quick(lo, j - 1, input);
73             Quick(j + 1, hi, input);
74         }
75     }
76
77     private static int Random(int size)
78     {
79         unchecked
80         {
81             s_seed = s_seed * A + C;
82         }
83
84         return (s_seed % size);
85     }
86
87     private static bool VerifySort(int[] buffer)
88     {
89         for (int y = 0; y < Maxnum - 2; y++)
90         {
91             if (buffer[y] > buffer[y + 1])
92             {
93                 return false;
94             }
95         }
96         return true;
97     }
98
99     [MethodImpl(MethodImplOptions.NoInlining)]
100     private static bool Bench()
101     {
102         int[] buffer = new int[Maxnum + 1];
103
104         for (int i = 0; i < Iterations; ++i)
105         {
106             for (int j = 0; j < Maxnum; ++j)
107             {
108                 int temp = Random(Modulus);
109                 if (temp < 0L)
110                 {
111                     temp = (-temp);
112                 }
113                 buffer[j] = temp;
114             }
115             buffer[Maxnum] = Modulus;
116
117             Quick(0, Maxnum - 1, buffer);
118         }
119
120         bool result = VerifySort(buffer);
121
122         return result;
123     }
124
125     [Benchmark]
126     public static void Test()
127     {
128         foreach (var iteration in Benchmark.Iterations)
129         {
130             using (iteration.StartMeasurement())
131             {
132                 for (int i = 0; i < Iterations; i++)
133                 {
134                     Bench();
135                 }
136             }
137         }
138     }
139
140     private static bool TestBase()
141     {
142         bool result = true;
143         for (int i = 0; i < Iterations; i++)
144         {
145             result &= Bench();
146         }
147         return result;
148     }
149
150     public static int Main()
151     {
152         bool result = TestBase();
153         return (result ? 100 : -1);
154     }
155 }
156 }