summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Performance/CodeQuality/SIMD/ConsoleMandel/VectorDoubleStrict.cs
blob: d2d8068ceabb8fb04516a918e61be1cef4750b5d (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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System;
using System.Numerics;
using System.Threading.Tasks;

namespace Algorithms
{
    // This contains renderers that only use Vector<double>'s with no Vector<long> types. It's
    // primarily useful when targeting AVX (not AVX2), because AVX doesn't support 256 bits of
    // integer values, only floating point values.
    internal class VectorDoubleStrictRenderer : FractalRenderer
    {
        private const double limit = 4.0;

        private static Vector<double> s_dummy;

        static VectorDoubleStrictRenderer()
        {
            s_dummy = Vector<double>.One;
        }

        public VectorDoubleStrictRenderer(Action<int, int, int> dp, Func<bool> abortFunc)
            : base(dp, abortFunc)
        {
        }

        // Render the fractal on multiple threads using the ComplexVecDouble data type
        // For a well commented version, go see VectorFloatRenderer.RenderSingleThreadedWithADT in VectorFloat.cs
        public void RenderMultiThreadedWithADT(float xminf, float xmaxf, float yminf, float ymaxf, float stepf)
        {
            double xmin = (double)xminf;
            double xmax = (double)xmaxf;
            double ymin = (double)yminf;
            double ymax = (double)ymaxf;
            double step = (double)stepf;

            Vector<double> vmax_iters = new Vector<double>((double)max_iters);
            Vector<double> vlimit = new Vector<double>(limit);
            Vector<double> vstep = new Vector<double>(step);
            Vector<double> vinc = new Vector<double>((double)Vector<double>.Count * step);
            Vector<double> vxmax = new Vector<double>(xmax);
            Vector<double> vxmin = VectorHelper.Create(i => xmin + step * i);

            Parallel.For(0, (int)(((ymax - ymin) / step) + .5), (yp) =>
            {
                if (Abort)
                    return;

                Vector<double> vy = new Vector<double>(ymin + step * yp);
                int xp = 0;
                for (Vector<double> vx = vxmin; Vector.LessThanOrEqualAny(vx, vxmax); vx += vinc, xp += Vector<double>.Count)
                {
                    ComplexVecDouble num = new ComplexVecDouble(vx, vy);
                    ComplexVecDouble accum = num;

                    Vector<double> viters = Vector<double>.Zero;
                    Vector<double> increment = Vector<double>.One;
                    do
                    {
                        accum = accum.square() + num;
                        viters += increment;
                        Vector<double> vCond = Vector.LessThanOrEqual<double>(accum.sqabs(), vlimit) &
                            Vector.LessThanOrEqual<double>(viters, vmax_iters);
                        increment = increment & vCond;
                    } while (increment != Vector<double>.Zero);

                    viters.ForEach((iter, elemNum) => DrawPixel(xp + elemNum, yp, (int)iter));
                }
            });
        }

        // Render the fractal on multiple threads using raw Vector<double> data types
        // For a well commented version, go see VectorFloatRenderer.RenderSingleThreadedWithADT in VectorFloat.cs
        public void RenderMultiThreadedNoADT(float xminf, float xmaxf, float yminf, float ymaxf, float stepf)
        {
            double xmin = (double)xminf;
            double xmax = (double)xmaxf;
            double ymin = (double)yminf;
            double ymax = (double)ymaxf;
            double step = (double)stepf;

            Vector<double> vmax_iters = new Vector<double>((double)max_iters);
            Vector<double> vlimit = new Vector<double>(limit);
            Vector<double> vstep = new Vector<double>(step);
            Vector<double> vinc = new Vector<double>((double)Vector<double>.Count * step);
            Vector<double> vxmax = new Vector<double>(xmax);
            Vector<double> vxmin = VectorHelper.Create(i => xmin + step * i);

            Parallel.For(0, (int)(((ymax - ymin) / step) + .5), (yp) =>
            {
                if (Abort)
                    return;

                Vector<double> vy = new Vector<double>(ymin + step * yp);
                int xp = 0;
                for (Vector<double> vx = vxmin; Vector.LessThanOrEqualAny(vx, vxmax); vx += vinc, xp += Vector<double>.Count)
                {
                    Vector<double> accumx = vx;
                    Vector<double> accumy = vy;

                    Vector<double> viters = Vector<double>.Zero;
                    Vector<double> increment = Vector<double>.One;
                    do
                    {
                        Vector<double> naccumx = accumx * accumx - accumy * accumy;
                        Vector<double> naccumy = accumx * accumy + accumx * accumy;
                        accumx = naccumx + vx;
                        accumy = naccumy + vy;
                        viters += increment;
                        Vector<double> sqabs = accumx * accumx + accumy * accumy;
                        Vector<double> vCond = Vector.LessThanOrEqual<double>(sqabs, vlimit) &
                            Vector.LessThanOrEqual<double>(viters, vmax_iters);
                        increment = increment & vCond;
                    } while (increment != Vector<double>.Zero);

                    viters.ForEach((iter, elemNum) => DrawPixel(xp + elemNum, yp, (int)iter));
                }
            });
        }

        // Render the fractal on a single thread using the ComplexVecDouble data type
        // For a well commented version, go see VectorFloatRenderer.RenderSingleThreadedWithADT in VectorFloat.cs
        public void RenderSingleThreadedWithADT(float xminf, float xmaxf, float yminf, float ymaxf, float stepf)
        {
            double xmin = (double)xminf;
            double xmax = (double)xmaxf;
            double ymin = (double)yminf;
            double ymax = (double)ymaxf;
            double step = (double)stepf;

            Vector<double> vmax_iters = new Vector<double>((double)max_iters);
            Vector<double> vlimit = new Vector<double>(limit);
            Vector<double> vstep = new Vector<double>(step);
            Vector<double> vinc = new Vector<double>((double)Vector<double>.Count * step);
            Vector<double> vxmax = new Vector<double>(xmax);
            Vector<double> vxmin = VectorHelper.Create(i => xmin + step * i);

            double y = ymin;
            int yp = 0;
            for (Vector<double> vy = new Vector<double>(ymin); y <= ymax && !Abort; vy += vstep, y += step, yp++)
            {
                int xp = 0;
                for (Vector<double> vx = vxmin; Vector.LessThanOrEqualAny(vx, vxmax); vx += vinc, xp += Vector<double>.Count)
                {
                    ComplexVecDouble num = new ComplexVecDouble(vx, vy);
                    ComplexVecDouble accum = num;

                    Vector<double> viters = Vector<double>.Zero;
                    Vector<double> increment = Vector<double>.One;
                    do
                    {
                        accum = accum.square() + num;
                        viters += increment;
                        Vector<double> vCond = Vector.LessThanOrEqual<double>(accum.sqabs(), vlimit) &
                            Vector.LessThanOrEqual<double>(viters, vmax_iters);
                        increment = increment & vCond;
                    } while (increment != Vector<double>.Zero);

                    viters.ForEach((iter, elemNum) => DrawPixel(xp + elemNum, yp, (int)iter));
                }
            }
        }

        // Render the fractal on a single thread using raw Vector<double> data types
        // For a well commented version, go see VectorFloatRenderer.RenderSingleThreadedWithADT in VectorFloat.cs
        public void RenderSingleThreadedNoADT(float xminf, float xmaxf, float yminf, float ymaxf, float stepf)
        {
            double xmin = (double)xminf;
            double xmax = (double)xmaxf;
            double ymin = (double)yminf;
            double ymax = (double)ymaxf;
            double step = (double)stepf;

            Vector<double> vmax_iters = new Vector<double>((double)max_iters);
            Vector<double> vlimit = new Vector<double>(limit);
            Vector<double> vstep = new Vector<double>(step);
            Vector<double> vinc = new Vector<double>((double)Vector<double>.Count * step);
            Vector<double> vxmax = new Vector<double>(xmax);
            Vector<double> vxmin = VectorHelper.Create(i => xmin + step * i);

            double y = ymin;
            int yp = 0;
            for (Vector<double> vy = new Vector<double>(ymin); y <= ymax && !Abort; vy += vstep, y += step, yp++)
            {
                int xp = 0;
                for (Vector<double> vx = vxmin; Vector.LessThanOrEqualAny(vx, vxmax); vx += vinc, xp += Vector<double>.Count)
                {
                    Vector<double> accumx = vx;
                    Vector<double> accumy = vy;

                    Vector<double> viters = Vector<double>.Zero;
                    Vector<double> increment = Vector<double>.One;
                    do
                    {
                        Vector<double> naccumx = accumx * accumx - accumy * accumy;
                        Vector<double> naccumy = accumx * accumy + accumx * accumy;
                        accumx = naccumx + vx;
                        accumy = naccumy + vy;
                        viters += increment;
                        Vector<double> sqabs = accumx * accumx + accumy * accumy;
                        Vector<double> vCond = Vector.LessThanOrEqual<double>(sqabs, vlimit) &
                            Vector.LessThanOrEqual<double>(viters, vmax_iters);
                        increment = increment & vCond;
                    } while (increment != Vector<double>.Zero);

                    viters.ForEach((iter, elemNum) => DrawPixel(xp + elemNum, yp, (int)iter));
                }
            }
        }
    }
}