summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Performance/CodeQuality/Bytemark/fourier.cs
blob: e2e0a5d6097c0c585324ae607211ca2fcba3b01b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/*
** Copyright (c) Microsoft. All rights reserved.
** Licensed under the MIT license. 
** See LICENSE file in the project root for full license information.
** 
** This program was translated to C# and adapted for xunit-performance.
** New variants of several tests were added to compare class versus 
** struct and to compare jagged arrays vs multi-dimensional arrays.
*/

/*
** BYTEmark (tm)
** BYTE Magazine's Native Mode benchmarks
** Rick Grehan, BYTE Magazine
**
** Create:
** Revision: 3/95
**
** DISCLAIMER
** The source, executable, and documentation files that comprise
** the BYTEmark benchmarks are made available on an "as is" basis.
** This means that we at BYTE Magazine have made every reasonable
** effort to verify that the there are no errors in the source and
** executable code.  We cannot, however, guarantee that the programs
** are error-free.  Consequently, McGraw-HIll and BYTE Magazine make
** no claims in regard to the fitness of the source code, executable
** code, and documentation of the BYTEmark.
** 
** Furthermore, BYTE Magazine, McGraw-Hill, and all employees
** of McGraw-Hill cannot be held responsible for any damages resulting
** from the use of this code or the results obtained from using
** this code.
*/

using System;

public class Fourier : FourierStruct
{
    public override string Name()
    {
        return "FOURIER";
    }

    /* 
    ** Perform the transcendental/trigonometric portion of the
    ** benchmark.  This benchmark calculates the first n
    ** fourier coefficients of the function (x+1)^x defined
    ** on the interval 0,2.
    */
    public override double Run()
    {
        double[] abase;         /* Base of A[] coefficients array */
        double[] bbase;         /* Base of B[] coefficients array */
        long accumtime;         /* Accumulated time in ticks */
        double iterations;      /* # of iterations */

        /*
        ** See if we need to do self-adjustment code.
        */
        if (this.adjust == 0)
        {
            this.arraysize = 100; /* Start at 100 elements */
            while (true)
            {
                abase = new double[this.arraysize];
                bbase = new double[this.arraysize];
                /*
                ** Do an iteration of the tests.  If the elapsed time is
                ** less than or equal to the permitted minimum, re-allocate
                ** larger arrays and try again.
                */
                if (DoFPUTransIteration(abase, bbase, this.arraysize) > global.min_ticks) break;
                this.arraysize += 50;
            }
        }
        else
        {
            /*
            ** Don't need self-adjustment.  Just allocate the
            ** arrays, and go.
            */
            abase = new double[this.arraysize];
            bbase = new double[this.arraysize];
        }

        accumtime = 0L;
        iterations = 0.0;
        do
        {
            accumtime += DoFPUTransIteration(abase, bbase, this.arraysize);
            iterations += (double)this.arraysize * (double)2.0 - (double)1.0;
        } while (ByteMark.TicksToSecs(accumtime) < this.request_secs);

        if (this.adjust == 0)
            this.adjust = 1;

        return iterations / (double)ByteMark.TicksToFracSecs(accumtime);
    }

    /*
    ** Perform an iteration of the FPU Transcendental/trigonometric
    ** benchmark.  Here, an iteration consists of calculating the
    ** first n fourier coefficients of the function (x+1)^x on
    ** the interval 0,2.  n is given by arraysize.
    ** NOTE: The # of integration steps is fixed at
    ** 200.
    */
    private static long DoFPUTransIteration(double[] abase, double[] bbase, int arraysize)
    {
        double omega;   /* Fundamental frequency */
        int i;      /* Index */
        long elapsed;   /* Elapsed time */

        elapsed = ByteMark.StartStopwatch();

        /*
        ** Calculate the fourier series.  Begin by
        ** calculating A[0].
        */

        abase[0] = TrapezoidIntegrate(0.0, 2.0, 200, 0.0, 0) / 2.0;

        /*
        ** Calculate the fundamental frequency.
        ** ( 2 * pi ) / period...and since the period
        ** is 2, omega is simply pi.
        */
        omega = 3.1415926535897921;

        for (i = 1; i < arraysize; i++)
        {
            /*
            ** Calculate A[i] terms.  Note, once again, that we
            ** can ignore the 2/period term outside the integral
            ** since the period is 2 and the term cancels itself
            ** out.
            */
            abase[i] = TrapezoidIntegrate(0.0, 2.0, 200, omega * (double)i, 1);

            bbase[i] = TrapezoidIntegrate(0.0, 2.0, 200, omega * (double)i, 2);
        }
        /*
        ** All done, stop the stopwatch
        */
        return (ByteMark.StopStopwatch(elapsed));
    }

    /*
    ** Perform a simple trapezoid integration on the
    ** function (x+1)**x.
    ** x0,x1 set the lower and upper bounds of the
    ** integration.
    ** nsteps indicates # of trapezoidal sections
    ** omegan is the fundamental frequency times
    **  the series member #
    ** select = 0 for the A[0] term, 1 for cosine terms, and
    **   2 for sine terms.
    ** Returns the value.
    */
    private static double TrapezoidIntegrate(double x0, double x1, int nsteps, double omegan, int select)
    {
        double x;       /* Independent variable */
        double dx;      /* Stepsize */
        double rvalue;          /* Return value */

        /*
        ** Initialize independent variable
        */
        x = x0;

        /*
        ** Calculate stepsize
        */
        dx = (x1 - x0) / (double)nsteps;

        /*
        ** Initialize the return value.
        */
        rvalue = thefunction(x0, omegan, select) / (double)2.0;

        /*
        ** Compute the other terms of the integral.
        */
        if (nsteps != 1)
        {
            --nsteps;               /* Already done 1 step */
            while (--nsteps != 0)
            {
                x += dx;
                rvalue += thefunction(x, omegan, select);
            }
        }
        /*
        ** Finish computation
        */
        rvalue = (rvalue + thefunction(x1, omegan, select) / (double)2.0) * dx;

        return (rvalue);
    }

    /*
    ** This routine selects the function to be used
    ** in the Trapezoid integration.
    ** x is the independent variable
    ** omegan is omega * n
    ** select chooses which of the sine/cosine functions
    **  are used.  note the special case for select=0.
    */
    private static double thefunction(double x, double omegan, int select)
    {
        /*
        ** Use select to pick which function we call.
        */
        switch (select)
        {
            case 0: return Math.Pow(x + (double)1.0, x);
            case 1: return Math.Pow(x + (double)1.0, x) * Math.Cos(omegan * x);
            case 2: return Math.Pow(x + (double)1.0, x) * Math.Sin(omegan * x);
        }

        /*
        ** We should never reach this point, but the following
        ** keeps compilers from issuing a warning message.
        */
        return (0.0);
    }
}