master
ブランチ master
VulkanExample / dep / include / glm / simd / common.h
7.2 KB、249行 生 Blame 履歴
1/// @ref simd
2/// @file glm/simd/common.h
3
4#pragma once
5
6#include "platform.h"
7
8#if GLM_ARCH & GLM_ARCH_SSE2_BIT
9
10GLM_FUNC_QUALIFIER glm_f32vec4 glm_vec4_add(glm_f32vec4 a, glm_f32vec4 b)
11{
12 return _mm_add_ps(a, b);
13}
14
15GLM_FUNC_QUALIFIER glm_f32vec4 glm_vec1_add(glm_f32vec4 a, glm_f32vec4 b)
16{
17 return _mm_add_ss(a, b);
18}
19
20GLM_FUNC_QUALIFIER glm_f32vec4 glm_vec4_sub(glm_f32vec4 a, glm_f32vec4 b)
21{
22 return _mm_sub_ps(a, b);
23}
24
25GLM_FUNC_QUALIFIER glm_f32vec4 glm_vec1_sub(glm_f32vec4 a, glm_f32vec4 b)
26{
27 return _mm_sub_ss(a, b);
28}
29
30GLM_FUNC_QUALIFIER glm_f32vec4 glm_vec4_mul(glm_f32vec4 a, glm_f32vec4 b)
31{
32 return _mm_mul_ps(a, b);
33}
34
35GLM_FUNC_QUALIFIER glm_f32vec4 glm_vec1_mul(glm_f32vec4 a, glm_f32vec4 b)
36{
37 return _mm_mul_ss(a, b);
38}
39
40GLM_FUNC_QUALIFIER glm_f32vec4 glm_vec4_div(glm_f32vec4 a, glm_f32vec4 b)
41{
42 return _mm_div_ps(a, b);
43}
44
45GLM_FUNC_QUALIFIER glm_f32vec4 glm_vec1_div(glm_f32vec4 a, glm_f32vec4 b)
46{
47 return _mm_div_ss(a, b);
48}
49
50GLM_FUNC_QUALIFIER glm_f32vec4 glm_vec4_div_lowp(glm_f32vec4 a, glm_f32vec4 b)
51{
52 return glm_vec4_mul(a, _mm_rcp_ps(b));
53}
54
55GLM_FUNC_QUALIFIER glm_f32vec4 glm_vec4_swizzle_xyzw(glm_f32vec4 a)
56{
57# if GLM_ARCH & GLM_ARCH_AVX2_BIT
58 return _mm_permute_ps(a, _MM_SHUFFLE(3, 2, 1, 0));
59# else
60 return _mm_shuffle_ps(a, a, _MM_SHUFFLE(3, 2, 1, 0));
61# endif
62}
63
64GLM_FUNC_QUALIFIER glm_f32vec4 glm_vec1_fma(glm_f32vec4 a, glm_f32vec4 b, glm_f32vec4 c)
65{
66# ifdef GLM_FORCE_FMA
67 return _mm_fmadd_ss(a, b, c);
68# else
69 return _mm_add_ss(_mm_mul_ss(a, b), c);
70# endif
71}
72
73GLM_FUNC_QUALIFIER glm_f32vec4 glm_vec4_fma(glm_f32vec4 a, glm_f32vec4 b, glm_f32vec4 c)
74{
75# ifdef GLM_FORCE_FMA
76 return _mm_fmadd_ps(a, b, c);
77# else
78 return glm_vec4_add(glm_vec4_mul(a, b), c);
79# endif
80}
81
82GLM_FUNC_QUALIFIER glm_f32vec4 glm_vec4d_fma(glm_f32vec4 a, glm_f32vec4 b, glm_f32vec4 c)
83{
84# ifdef GLM_FORCE_FMA
85 return _mm_fmadd_ps(a, b, c);
86# else
87 return glm_vec4_add(glm_vec4_mul(a, b), c);
88# endif
89}
90
91GLM_FUNC_QUALIFIER glm_f32vec4 glm_vec4_abs(glm_f32vec4 x)
92{
93 return _mm_and_ps(x, _mm_castsi128_ps(_mm_set1_epi32(0x7FFFFFFF)));
94}
95
96GLM_FUNC_QUALIFIER glm_ivec4 glm_ivec4_abs(glm_ivec4 x)
97{
98# if GLM_ARCH & GLM_ARCH_SSSE3_BIT
99 return _mm_sign_epi32(x, x);
100# else
101 glm_ivec4 const sgn0 = _mm_srai_epi32(x, 31);
102 glm_ivec4 const inv0 = _mm_xor_si128(x, sgn0);
103 glm_ivec4 const sub0 = _mm_sub_epi32(inv0, sgn0);
104 return sub0;
105# endif
106}
107
108GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_sign(glm_vec4 x)
109{
110 glm_vec4 const zro0 = _mm_setzero_ps();
111 glm_vec4 const cmp0 = _mm_cmplt_ps(x, zro0);
112 glm_vec4 const cmp1 = _mm_cmpgt_ps(x, zro0);
113 glm_vec4 const and0 = _mm_and_ps(cmp0, _mm_set1_ps(-1.0f));
114 glm_vec4 const and1 = _mm_and_ps(cmp1, _mm_set1_ps(1.0f));
115 glm_vec4 const or0 = _mm_or_ps(and0, and1);
116 return or0;
117}
118
119GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_round(glm_vec4 x)
120{
121# if GLM_ARCH & GLM_ARCH_SSE41_BIT
122 return _mm_round_ps(x, _MM_FROUND_TO_NEAREST_INT);
123# else
124 glm_vec4 const sgn0 = _mm_castsi128_ps(_mm_set1_epi32(int(0x80000000)));
125 glm_vec4 const and0 = _mm_and_ps(sgn0, x);
126 glm_vec4 const or0 = _mm_or_ps(and0, _mm_set_ps1(8388608.0f));
127 glm_vec4 const add0 = glm_vec4_add(x, or0);
128 glm_vec4 const sub0 = glm_vec4_sub(add0, or0);
129 return sub0;
130# endif
131}
132
133GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_floor(glm_vec4 x)
134{
135# if GLM_ARCH & GLM_ARCH_SSE41_BIT
136 return _mm_floor_ps(x);
137# else
138 glm_vec4 const rnd0 = glm_vec4_round(x);
139 glm_vec4 const cmp0 = _mm_cmplt_ps(x, rnd0);
140 glm_vec4 const and0 = _mm_and_ps(cmp0, _mm_set1_ps(1.0f));
141 glm_vec4 const sub0 = glm_vec4_sub(rnd0, and0);
142 return sub0;
143# endif
144}
145
146/* trunc TODO
147GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_trunc(glm_vec4 x)
148{
149 return glm_vec4();
150}
151*/
152
153//roundEven
154GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_roundEven(glm_vec4 x)
155{
156 glm_vec4 const sgn0 = _mm_castsi128_ps(_mm_set1_epi32(int(0x80000000)));
157 glm_vec4 const and0 = _mm_and_ps(sgn0, x);
158 glm_vec4 const or0 = _mm_or_ps(and0, _mm_set_ps1(8388608.0f));
159 glm_vec4 const add0 = glm_vec4_add(x, or0);
160 glm_vec4 const sub0 = glm_vec4_sub(add0, or0);
161 return sub0;
162}
163
164GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_ceil(glm_vec4 x)
165{
166# if GLM_ARCH & GLM_ARCH_SSE41_BIT
167 return _mm_ceil_ps(x);
168# else
169 glm_vec4 const rnd0 = glm_vec4_round(x);
170 glm_vec4 const cmp0 = _mm_cmpgt_ps(x, rnd0);
171 glm_vec4 const and0 = _mm_and_ps(cmp0, _mm_set1_ps(1.0f));
172 glm_vec4 const add0 = glm_vec4_add(rnd0, and0);
173 return add0;
174# endif
175}
176
177GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_fract(glm_vec4 x)
178{
179 glm_vec4 const flr0 = glm_vec4_floor(x);
180 glm_vec4 const sub0 = glm_vec4_sub(x, flr0);
181 return sub0;
182}
183
184GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_mod(glm_vec4 x, glm_vec4 y)
185{
186 glm_vec4 const div0 = glm_vec4_div(x, y);
187 glm_vec4 const flr0 = glm_vec4_floor(div0);
188 glm_vec4 const mul0 = glm_vec4_mul(y, flr0);
189 glm_vec4 const sub0 = glm_vec4_sub(x, mul0);
190 return sub0;
191}
192
193GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_clamp(glm_vec4 v, glm_vec4 minVal, glm_vec4 maxVal)
194{
195 glm_vec4 const min0 = _mm_min_ps(v, maxVal);
196 glm_vec4 const max0 = _mm_max_ps(min0, minVal);
197 return max0;
198}
199
200GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_mix(glm_vec4 v1, glm_vec4 v2, glm_vec4 a)
201{
202 glm_vec4 const sub0 = glm_vec4_sub(_mm_set1_ps(1.0f), a);
203 glm_vec4 const mul0 = glm_vec4_mul(v1, sub0);
204 glm_vec4 const mad0 = glm_vec4_fma(v2, a, mul0);
205 return mad0;
206}
207
208GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_step(glm_vec4 edge, glm_vec4 x)
209{
210 glm_vec4 const cmp = _mm_cmple_ps(x, edge);
211 return _mm_movemask_ps(cmp) == 0 ? _mm_set1_ps(1.0f) : _mm_setzero_ps();
212}
213
214GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_smoothstep(glm_vec4 edge0, glm_vec4 edge1, glm_vec4 x)
215{
216 glm_vec4 const sub0 = glm_vec4_sub(x, edge0);
217 glm_vec4 const sub1 = glm_vec4_sub(edge1, edge0);
218 glm_vec4 const div0 = glm_vec4_div(sub0, sub1);
219 glm_vec4 const clp0 = glm_vec4_clamp(div0, _mm_setzero_ps(), _mm_set1_ps(1.0f));
220 glm_vec4 const mul0 = glm_vec4_mul(_mm_set1_ps(2.0f), clp0);
221 glm_vec4 const sub2 = glm_vec4_sub(_mm_set1_ps(3.0f), mul0);
222 glm_vec4 const mul1 = glm_vec4_mul(clp0, clp0);
223 glm_vec4 const mul2 = glm_vec4_mul(mul1, sub2);
224 return mul2;
225}
226
227// Agner Fog method
228GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_nan(glm_vec4 x)
229{
230 glm_ivec4 const t1 = _mm_castps_si128(x); // reinterpret as 32-bit integer
231 glm_ivec4 const t2 = _mm_sll_epi32(t1, _mm_cvtsi32_si128(1)); // shift out sign bit
232 glm_ivec4 const t3 = _mm_set1_epi32(int(0xFF000000)); // exponent mask
233 glm_ivec4 const t4 = _mm_and_si128(t2, t3); // exponent
234 glm_ivec4 const t5 = _mm_andnot_si128(t3, t2); // fraction
235 glm_ivec4 const Equal = _mm_cmpeq_epi32(t3, t4);
236 glm_ivec4 const Nequal = _mm_cmpeq_epi32(t5, _mm_setzero_si128());
237 glm_ivec4 const And = _mm_and_si128(Equal, Nequal);
238 return _mm_castsi128_ps(And); // exponent = all 1s and fraction != 0
239}
240
241// Agner Fog method
242GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_inf(glm_vec4 x)
243{
244 glm_ivec4 const t1 = _mm_castps_si128(x); // reinterpret as 32-bit integer
245 glm_ivec4 const t2 = _mm_sll_epi32(t1, _mm_cvtsi32_si128(1)); // shift out sign bit
246 return _mm_castsi128_ps(_mm_cmpeq_epi32(t2, _mm_set1_epi32(int(0xFF000000)))); // exponent is all 1s, fraction is 0
247}
248
249#endif//GLM_ARCH & GLM_ARCH_SSE2_BIT