This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libvectors.h
146 lines (130 loc) · 3.55 KB
/
libvectors.h
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libvectors.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaluh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/05/18 12:16:52 by tmaluh #+# #+# */
/* Updated: 2019/10/03 18:14:34 by tmaluh ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBVECTORS_H
# define LIBVECTORS_H
# include <math.h>
# include <stdbool.h>
# include <stddef.h>
# include <sys/cdefs.h>
# ifndef __always_inline
# define __always_inline inline __attribute__((__always_inline__))
# endif
# ifndef __lvec_always_inline
# define __lvec_always_inline static __always_inline
# endif
# define V4DF typedef double t_v4df __attribute__((vector_size(32),aligned))
# define V2DF typedef double t_v2df __attribute__((vector_size(16),aligned))
# define V4DU typedef size_t t_v4du __attribute__((vector_size(32),aligned))
V4DF;
V2DF;
V4DU;
# define X(v) (v)[0]
# define Y(v) (v)[1]
# define Z(v) (v)[2]
# define W(v) (v)[3]
# define VDISC(a, b, c) ((b) * (b) - 4.0 * (a) * (c))
__lvec_always_inline double_t
v_dot(t_v4df const v1, t_v4df const v2)
{
return X(v1) * X(v2) + Y(v1) * Y(v2) + Z(v1) * Z(v2);
}
__lvec_always_inline double_t
v_len(t_v4df const v)
{
return sqrt(v_dot(v, v));
}
__lvec_always_inline t_v4df
v_norm(t_v4df const v)
{
double_t const vlen = v_len(v);
return (t_v4df){X(v) / vlen, Y(v) / vlen, Z(v) / vlen, 0.0};
}
__lvec_always_inline t_v4df
v_mul_d(t_v4df const v, double_t const d)
{
return (t_v4df){X(v) * d, Y(v) * d, Z(v) * d, 0.0};
}
__lvec_always_inline t_v4df
v_add_d(t_v4df const v, double_t const d)
{
return (t_v4df){X(v) + d, Y(v) + d, Z(v) + d, 0.0};
}
__lvec_always_inline t_v4df
v_sub_d(t_v4df const v, double_t const d)
{
return (t_v4df){X(v) - d, Y(v) - d, Z(v) - d, 0.0};
}
__lvec_always_inline t_v4df
v_div_d(t_v4df const v, double_t const d)
{
return (t_v4df){X(v) / d, Y(v) / d, Z(v) / d, 0.0};
}
__lvec_always_inline t_v4df
v_mul_v(t_v4df const a, t_v4df const b)
{
return a * b;
}
__lvec_always_inline t_v4df
v_add_v(t_v4df const a, t_v4df const b)
{
return a + b;
}
__lvec_always_inline t_v4df
v_sub_v(t_v4df const a, t_v4df const b)
{
return a - b;
}
__lvec_always_inline t_v4df
v_div_v(t_v4df const a, t_v4df const b)
{
return a / b;
}
__lvec_always_inline t_v4df
v_invert(t_v4df const v)
{
return -v;
}
/*
** u_ prefix is short name for utils.
*/
__lvec_always_inline bool
u_vec_range(t_v4df const v,
double_t const max_range,
double_t const min_range)
{
if (max_range < X(v) || max_range < Y(v) || max_range < Z(v))
return false;
if (min_range > X(v) || min_range > Y(v) || min_range > Z(v))
return false;
return true;
}
__lvec_always_inline bool
u_isd_range(double_t const x,
double_t const max_range,
double_t const min_range)
{
if (max_range < x || x < min_range)
return false;
return true;
}
__lvec_always_inline double_t
u_d_range(double_t const x,
double_t const max_range,
double_t const min_range)
{
if (max_range < x)
return max_range;
if (min_range > x)
return min_range;
return x;
}
#endif