Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 180 lines (161 sloc) 4.123 kB
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
1 /*
8a472bf @fador tiny changes..
fador authored
2 Copyright (c) 2011, The Mineserver Project
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
3 All rights reserved.
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
4
080bd08 @deoxxa fixed formatting of license, it was annoying me to no end
deoxxa authored
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the The Mineserver Project nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
15
080bd08 @deoxxa fixed formatting of license, it was annoying me to no end
deoxxa authored
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
26 */
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
27
28 #ifndef _VEC_H
29 #define _VEC_H
30
31 #if defined(__clang__) || defined(__gcc__)
32 #define VEC_VECTORISED
33 #endif
34
35 class vec
36 {
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
37 private:
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
38 #ifdef VEC_VECTORISED
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
39 typedef int ivec __attribute__((vector_size(16)));
40 union
41 {
42 int arrayValue[4];
43 ivec vectorValue;
44 } data;
45 vec(ivec vector)
46 {
47 data.vectorValue = vector;
48 }
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
49 #else
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
50 union
51 {
52 int arrayValue[3];
53 } data;
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
54 #endif
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
55 public:
56 vec(int x, int y, int z)
57 {
58 data.arrayValue[0] = x;
59 data.arrayValue[1] = y;
60 data.arrayValue[2] = z;
61 }
62 vec(const int values[3])
63 {
64 data.arrayValue[0] = values[0];
65 data.arrayValue[1] = values[1];
66 data.arrayValue[2] = values[2];
67 }
68 vec()
69 {
70 data.arrayValue[0] = 0;
71 data.arrayValue[1] = 0;
72 data.arrayValue[2] = 0;
73 }
74 vec(const vec &ov)
75 {
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
76 #ifdef VEC_VECTORISED
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
77 data.vectorValue = ov.data.vectorValue;
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
78 #else
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
79 data.arrayValue[0] = ov.data.arrayValue[0];
80 data.arrayValue[1] = ov.data.arrayValue[1];
81 data.arrayValue[2] = ov.data.arrayValue[2];
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
82 #endif
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
83 }
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
84
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
85 vec& operator=(const vec &ov)
86 {
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
87 #ifdef VEC_VECTORISED
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
88 data.vectorValue = ov.data.vectorValue;
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
89 #else
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
90 data.arrayValue[0] = ov.data.arrayValue[0];
91 data.arrayValue[1] = ov.data.arrayValue[1];
92 data.arrayValue[2] = ov.data.arrayValue[2];
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
93 #endif
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
94 return *this;
95 }
96
97 int &x()
98 {
99 return data.arrayValue[0];
100 }
101 int &y()
102 {
103 return data.arrayValue[1];
104 }
105 int &z()
106 {
107 return data.arrayValue[2];
108 }
109 const int &x() const
110 {
111 return data.arrayValue[0];
112 }
113 const int &y() const
114 {
115 return data.arrayValue[1];
116 }
117 const int &z() const
118 {
119 return data.arrayValue[2];
120 }
121
122 vec operator+(const vec &ov) const
123 {
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
124 #ifdef VEC_VECTORISED
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
125 return vec(data.vectorValue + ov.data.vectorValue);
126
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
127 #else
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
128 return vec(x() + ov.x(),
129 y() + ov.y(),
130 z() + ov.z());
131
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
132 #endif
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
133 }
134 vec operator-(const vec &ov) const
135 {
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
136 #ifdef VEC_VECTORISED
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
137 return vec(data.vectorValue - ov.data.vectorValue);
138
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
139 #else
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
140 return vec(x() - ov.x(),
141 y() - ov.y(),
142 z() - ov.z());
143
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
144 #endif
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
145 }
146 vec operator-() const
147 {
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
148 #ifdef VEC_VECTORISED
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
149 return vec(-data.vectorValue);
150
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
151 #else
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
152 return vec(-x(),
153 -y(),
154 -z());
155
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
156 #endif
de8e38c @prophile Heavily cleaned up the source formatting.
prophile authored
157 }
158
159 vec& operator+=(const vec &ov)
160 {
161 return *this = *this + ov;
162 }
163 vec& operator-=(const vec &ov)
164 {
165 return *this = *this - ov;
166 }
167
168 int squareLength() const
169 {
170 return x()*x() + y()*y() + z()*z();
171 }
172
173 static int squareDistance(const vec &x, const vec &y)
174 {
175 return (x - y).squareLength();
176 }
ccc5fee @prophile Added a fast integer vector class, and integrated it.
prophile authored
177 };
178
179 #endif
Something went wrong with that request. Please try again.