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) 2018 Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef VKVG_VECTORS_H
#define VKVG_VECTORS_H
#include "vkvg_internal.h"
#include <immintrin.h>
typedef union {
struct {
float x;
float y;
};
}vec2;
#ifdef __SSE2__
typedef union {
__m128d raw;
struct {
double x;
double y;
};
}vec2d;
#else
typedef struct {
double x;
double y;
}vec2d;
#endif
typedef struct {
float x;
float y;
float z;
}vec3;
typedef struct {
union {
float x;
float r;
};
union {
float y;
float g;
};
union {
float z;
float width;
float b;
};
union {
float w;
float height;
float a;
};
}vec4;
typedef struct {
uint16_t x;
uint16_t y;
uint16_t z;
uint16_t w;
}vec4i16;
typedef struct {
int16_t x;
int16_t y;
}vec2i16;
// compute length of float vector 2d
vkvg_inline float vec2_length(vec2 v){
return sqrtf (v.x*v.x + v.y*v.y);
}
// compute normal direction vector from line defined by 2 points in double precision
vkvg_inline vec2d vec2d_line_norm(vec2d a, vec2d b)
{
vec2d d = {b.x - a.x, b.y - a.y};
double md = sqrt (d.x*d.x + d.y*d.y);
d.x/=md;
d.y/=md;
return d;
}
// compute normal direction vector from line defined by 2 points
vkvg_inline vec2 vec2_line_norm(vec2 a, vec2 b)
{
vec2 d = {b.x - a.x, b.y - a.y};
float md = sqrtf (d.x*d.x + d.y*d.y);
d.x/=md;
d.y/=md;
return d;
}
// compute length of double vector 2d
vkvg_inline double vec2d_length(vec2d v){
return sqrt (v.x*v.x + v.y*v.y);
}
// normalize float vector
vkvg_inline vec2 vec2_norm(vec2 a)
{
float m = sqrtf (a.x*a.x + a.y*a.y);
return (vec2){a.x/m, a.y/m};
}
// devide 2d vector by scalar
vkvg_inline vec2 vec2_div(vec2 a, float m){
return (vec2){a.x/m,a.y/m};
}
// multiply 2d vector by scalar
vkvg_inline vec2 vec2_mult(vec2 a, float m){
return (vec2){a.x*m,a.y*m};
}
// compute sum of two double precision vectors
vkvg_inline vec2d vec2d_add (vec2d a, vec2d b){
#ifdef __SSE2__
return (vec2d)_mm_add_pd (a.raw, b.raw);
#else
return (vec2d){a.x + b.x, a.y + b.y};
#endif
}
// compute subbstraction of two double precision vectors
vkvg_inline vec2d vec2d_sub (vec2d a, vec2d b){
#ifdef __SSE2__
return (vec2d)_mm_sub_pd (a.raw, b.raw);
#else
return (vec2d){a.x - b.x, a.y - b.y};
#endif
}
// multiply 2d vector by scalar
vkvg_inline vec2d vec2d_mult(vec2d a, double m){
#ifdef __SSE2__
return (vec2d)_mm_mul_pd (a.raw, _mm_set_pd1 (m));
#else
return (vec2d){a.x*m,a.y*m};
#endif
}
vkvg_inline vec2d vec2d_div(vec2d a, double m){
#ifdef __SSE2__
return (vec2d)_mm_div_pd (a.raw, _mm_set_pd1 (m));
#else
return (vec2d){a.x/m,a.y/m};
#endif
}
// normalize double vector
vkvg_inline vec2d vec2d_norm(vec2d a)
{
double m = sqrt (a.x*a.x + a.y*a.y);
return (vec2d)vec2d_div (a, m);
//return (vec2d){a.x/m, a.y/m};
}
// compute perpendicular vector
vkvg_inline vec2d vec2d_perp (vec2d a){
return (vec2d){a.y, -a.x};
}
// compute perpendicular vector
vkvg_inline vec2 vec2_perp (vec2 a){
return (vec2){a.y, -a.x};
}
// convert double precision vector to single precision
vkvg_inline vec2 vec2d_to_vec2(vec2d vd){
return (vec2){(float)vd.x,(float)vd.y};
}
// compute sum of two single precision vectors
vkvg_inline vec2 vec2_add (vec2 a, vec2 b){
return (vec2){a.x + b.x, a.y + b.y};
}
// compute subbstraction of two single precision vectors
vkvg_inline vec2 vec2_sub (vec2 a, vec2 b){
return (vec2){a.x - b.x, a.y - b.y};
}
// test equality of two single precision vectors
vkvg_inline bool vec2_equ (vec2 a, vec2 b){
return (EQUF(a.x,b.x)&EQUF(a.y,b.y));
}
vkvg_inline bool vec2d_equ (vec2d a, vec2d b){
return (EQU(a.x,b.x)&EQU(a.y,b.y));
}
// compute opposite of single precision vector
vkvg_inline void vec2_inv (vec2* v){
v->x = -v->x;
v->y = -v->y;
}
// test if one component of float vector is nan
vkvg_inline bool vec2_isnan (vec2 v){
return (bool)(isnanf (v.x) || isnanf (v.y));
}
// test if one component of double vector is nan
vkvg_inline bool vec2d_isnan (vec2d v){
return (bool)(isnan (v.x) || isnan (v.y));
}
vkvg_inline bool vec4_equ (vec4 a, vec4 b){
return (EQUF(a.x,b.x)&EQUF(a.y,b.y)&EQUF(a.z,b.z)&EQUF(a.w,b.w));
}
#endif
|