summaryrefslogtreecommitdiff
path: root/tests/src/JIT/SIMD/VectorReturn.cs
blob: d1785af45036f51c95f2270eb76339e681b62992 (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
// 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.
//

using System;
using System.Numerics;
using System.Runtime.CompilerServices;

internal partial class VectorTest
{
    private const int Pass = 100;
    private const int Fail = -1;
    private static Vector2[] s_A;
    private static Vector2 s_p0;
    private static Vector2 s_p1;
    private static Vector2 s_p2;
    private static Vector2 s_p3;

    [MethodImplAttribute(MethodImplOptions.NoInlining)]
    public static void init()
    {
        s_A = new Vector2[10];
        Random random = new Random(100);
        for (int i = 0; i < 10; i++)
        {
            s_A[i] = new Vector2(random.Next(100));
        }
        s_p0 = new Vector2(random.Next(100));
        s_p1 = new Vector2(random.Next(100));
        s_p2 = new Vector2(random.Next(100));
        s_p3 = new Vector2(random.Next(100));
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static Vector2 F1(float t)
    {
        float ti = 1 - t;
        float t0 = ti * ti * ti;
        float t1 = 3 * ti * ti * t;
        float t2 = 3 * ti * t * t;
        float t3 = t * t * t;
        return (t0 * s_p0) + (t1 * s_p1) + (t2 * s_p2) + (t3 * s_p3);
    }

    [MethodImplAttribute(MethodImplOptions.NoInlining)]
    public static Vector2 F2(float u)
    {
        if (u < 0)
            return s_A[0];
        if (u >= 1)
            return s_A[1];
        if (u < 0.1)
            return s_A[2];
        if (u > 0.9)
            return s_A[3];
        return F1(u);
    }

    [MethodImplAttribute(MethodImplOptions.NoInlining)]
    public static Vector<T> VectorOne<T>() where T: struct
    {
        return Vector<T>.One;
    }

    [MethodImplAttribute(MethodImplOptions.NoInlining)]
    public static Vector<T> VectorPlusOne<T>(Vector<T> v1) where T : struct
    {
        Vector<T> v2 = VectorOne<T>();
        return v1 + v2;
    }

    public static int VectorTReturnTest()
    {
        Vector<float> v1 = new Vector<float>(2.0f);
        Vector<float> result1 = VectorPlusOne<float>(v1);
        for (int i=0; i < Vector<float>.Count; ++i)
        {
            if (!CheckValue<float>(result1[i], 3.0f))
            {
                Console.WriteLine("Expected result is " + 3.0f);
                Console.WriteLine("Instead got " + result1[i]);
                Console.WriteLine("FAILED");
                return Fail;
            }
        }

        Vector<int> v2 = new Vector<int>(5);
        Vector<int> result2 = VectorPlusOne<int>(v2);
        for (int i = 0; i < Vector<int>.Count; ++i)
        {
            if (!CheckValue<int>(result2[i], 6))
            {
                Console.WriteLine("Expected result is " + 6);
                Console.WriteLine("Instead got " + result2[i]);
                Console.WriteLine("FAILED");
                return Fail;
            }
        }

        return Pass;
    }

    [MethodImplAttribute(MethodImplOptions.NoInlining)]
    public static Vector3 GetVector3One()
    {
        return new Vector3(1.0f);
    }

    [MethodImplAttribute(MethodImplOptions.NoInlining)]
    public static Vector3 GetVector3PlusOne(Vector3 v1)
    {
        Vector3 v2 = GetVector3One();
        return v1 + v2;
    }

    public static int Vector3ReturnTest()
    {
        Vector3 v1 = new Vector3(3.0f, 4.0f, 5.0f);
        Vector3 result = GetVector3PlusOne(v1);

        if (!CheckValue<float>(result.X, 4.0f) ||
            !CheckValue<float>(result.Y, 5.0f) ||
            !CheckValue<float>(result.Z, 6.0f))
        {
            Console.WriteLine("Vector3ReturnTest did not return expected value");
            return Fail;
        }

        return Pass;
    }

    public static int Main()
    {
        init();
        Vector2 result = F2(0.5F);
        Vector2 expectedResult = F1(0.5F);
        Console.WriteLine("Result is " + result.ToString());
        if (!CheckValue<float>(result.X, expectedResult.X) || !CheckValue<float>(result.Y, expectedResult.Y))
        {
            Console.WriteLine("Expected result is " + expectedResult.ToString());
            Console.WriteLine("FAILED");
            return Fail;
        }

        if (VectorTReturnTest() != Pass)
        {
            Console.WriteLine("FAILED");
            return Fail;
        }

        if (Vector3ReturnTest() != Pass)
        {
            Console.WriteLine("FAILED");
            return Fail;
        }

        Console.WriteLine("PASSED");
        return Pass;
    }
}