2008-10-28 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mcs / class / Mono.Simd / Mono.Simd / Vector2l.cs
1 // Vector2l.cs
2 //
3 // Author:
4 //   Rodrigo Kumpera (rkumpera@novell.com)
5 //
6 // (C) 2008 Novell, Inc. (http://www.novell.com)
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27 using System;\r
28 using System.Runtime.InteropServices;\r
29 \r
30 namespace Mono.Simd\r
31 {\r
32         [StructLayout(LayoutKind.Sequential, Pack = 0, Size = 16)]\r
33         public struct Vector2l\r
34         {\r
35                 private long x;\r
36                 private long y;\r
37 \r
38                 public long X { get { return x; } set { x = value; } }\r
39                 public long Y { get { return y; } set { y = value; } }\r
40 \r
41                 public Vector2l (long x, long y)\r
42                 {\r
43                         this.x = x;\r
44                         this.y = y;\r
45                 }
46
47                 //PADDQ
48                 public static Vector2l operator + (Vector2l v1, Vector2l v2)\r
49                 {\r
50                         return new Vector2l (v1.x + v2.x, v1.y + v2.y);\r
51                 }
52
53                 //PSUBQ
54                 public static Vector2l operator - (Vector2l v1, Vector2l v2)\r
55                 {\r
56                         return new Vector2l (v1.x - v2.x, v1.y - v2.y);\r
57                 }
58
59                 public static unsafe Vector2l operator << (Vector2l v1, int amount)\r
60                 {\r
61                         return new Vector2l (v1.x << amount, v1.y << amount);\r
62                 }
63
64                 public static Vector2l operator & (Vector2l v1, Vector2l v2)\r
65                 {\r
66                         return new Vector2l (v1.x & v2.x, v1.y & v2.y);\r
67                 }
68
69                 public static Vector2l operator | (Vector2l v1, Vector2l v2)\r
70                 {\r
71                         return new Vector2l (v1.x | v2.x, v1.y | v2.y);\r
72                 }\r
73 \r
74                 public static Vector2l operator ^ (Vector2l v1, Vector2l v2)\r
75                 {\r
76                         return new Vector2l (v1.x ^ v2.x, v1.y ^ v2.y);\r
77                 }\r
78
79                 public static Vector2l UnpackLow (Vector2l v1, Vector2l v2)
80                 {
81                         return new Vector2l (v1.x, v2.x);
82                 }
83
84                 public static Vector2l UnpackHigh (Vector2l v1, Vector2l v2)
85                 {
86                         return new Vector2l (v1.y, v2.y);
87                 }
88
89                 public static unsafe Vector2l ShiftRightLogic (Vector2l v1, int amount)\r
90                 {\r
91                         return new Vector2l ((long)((ulong)(v1.x) >> amount), (long)((ulong)(v1.y) >> amount));\r
92                 }
93
94                 public static unsafe long ExtractByteMask (Vector2l va) {
95                         int res = 0;
96                         byte *a = (byte*)&va;
97                         for (int i = 0; i < 16; ++i)
98                                 res |= (*a++ & 0x80) >> 7 << i;
99                         return res;
100                 }
101
102                 /*Requires SSE 4.1*/
103                 public static Vector2l CompareEqual (Vector2l v1, Vector2l v2)
104                 {
105                         return new Vector2l ((long)(v1.x ==  v2.x ? -1 : 0), (long)(v1.y ==  v2.y ? -1 : 0));
106                 }
107
108                 /*Requires SSE 4.1*/
109                 public static Vector2l CompareGreaterThan (Vector2l v1, Vector2l v2)
110                 {
111                         return new Vector2l ((long)(v1.x > v2.x ? -1 : 0), (long)(v1.y >  v2.y ? -1 : 0));
112                 }
113
114                 public static unsafe explicit operator Vector4f (Vector2l v1)\r
115                 {\r
116                         Vector4f* p = (Vector4f*)&v1;\r
117                         return *p;\r
118                 }
119
120                 [CLSCompliant(false)]\r
121                 public static unsafe explicit operator Vector8us (Vector2l v1)\r
122                 {\r
123                         Vector8us* p = (Vector8us*)&v1;\r
124                         return *p;\r
125                 }
126
127                 [CLSCompliant(false)]\r
128                 public static unsafe explicit operator Vector16b (Vector2l v1)\r
129                 {\r
130                         Vector16b* p = (Vector16b*)&v1;\r
131                         return *p;\r
132                 }
133
134                 public static Vector2l LoadAligned (ref Vector2l v)\r
135                 {\r
136                         return v;\r
137                 }\r
138 \r
139                 public static void StoreAligned (ref Vector2l res, Vector2l val)\r
140                 {\r
141                         res = val;\r
142                 }
143
144                 [CLSCompliant(false)]\r
145                 public static unsafe Vector2l LoadAligned (Vector2l *v)\r
146                 {\r
147                         return *v;\r
148                 }\r
149 \r
150                 [CLSCompliant(false)]\r
151                 public static unsafe void StoreAligned (Vector2l *res, Vector2l val)\r
152                 {\r
153                         *res = val;\r
154                 }
155         }\r
156 }\r