1 // SPDX-License-Identifier: Apache-2.0
2 // ----------------------------------------------------------------------------
3 // Copyright 2019-2021 Arm Limited
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
6 // use this file except in compliance with the License. You may obtain a copy
7 // of the License at:
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 // License for the specific language governing permissions and limitations
15 // under the License.
16 // ----------------------------------------------------------------------------
17
18 /**
19 * @brief 4x32-bit vectors, implemented using SSE.
20 *
21 * This module implements 4-wide 32-bit float, int, and mask vectors for x86
22 * SSE. The implementation requires at least SSE2, but higher levels of SSE can
23 * be selected at compile time to improve performance.
24 *
25 * There is a baseline level of functionality provided by all vector widths and
26 * implementations. This is implemented using identical function signatures,
27 * modulo data type, so we can use them as substitutable implementations in VLA
28 * code.
29 *
30 * The 4-wide vectors are also used as a fixed-width type, and significantly
31 * extend the functionality above that available to VLA code.
32 */
33
34 #ifndef ASTC_VECMATHLIB_SSE_4_H_INCLUDED
35 #define ASTC_VECMATHLIB_SSE_4_H_INCLUDED
36
37 #ifndef ASTCENC_SIMD_INLINE
38 #error "Include astcenc_vecmathlib.h, do not include directly"
39 #endif
40
41 #include <cstdio>
42
43 // ============================================================================
44 // vfloat4 data type
45 // ============================================================================
46
47 /**
48 * @brief Data type for 4-wide floats.
49 */
50 struct vfloat4
51 {
52 /**
53 * @brief Construct from zero-initialized value.
54 */
55 ASTCENC_SIMD_INLINE vfloat4() = default;
56
57 /**
58 * @brief Construct from 4 values loaded from an unaligned address.
59 *
60 * Consider using loada() which is better with vectors if data is aligned
61 * to vector length.
62 */
vfloat4vfloat463 ASTCENC_SIMD_INLINE explicit vfloat4(const float *p)
64 {
65 m = _mm_loadu_ps(p);
66 }
67
68 /**
69 * @brief Construct from 1 scalar value replicated across all lanes.
70 *
71 * Consider using zero() for constexpr zeros.
72 */
vfloat4vfloat473 ASTCENC_SIMD_INLINE explicit vfloat4(float a)
74 {
75 m = _mm_set1_ps(a);
76 }
77
78 /**
79 * @brief Construct from 4 scalar values.
80 *
81 * The value of @c a is stored to lane 0 (LSB) in the SIMD register.
82 */
vfloat4vfloat483 ASTCENC_SIMD_INLINE explicit vfloat4(float a, float b, float c, float d)
84 {
85 m = _mm_set_ps(d, c, b, a);
86 }
87
88 /**
89 * @brief Construct from an existing SIMD register.
90 */
vfloat4vfloat491 ASTCENC_SIMD_INLINE explicit vfloat4(__m128 a)
92 {
93 m = a;
94 }
95
96 /**
97 * @brief Get the scalar value of a single lane.
98 */
lanevfloat499 template <int l> ASTCENC_SIMD_INLINE float lane() const
100 {
101 return _mm_cvtss_f32(_mm_shuffle_ps(m, m, l));
102 }
103
104 /**
105 * @brief Set the scalar value of a single lane.
106 */
set_lanevfloat4107 template <int l> ASTCENC_SIMD_INLINE void set_lane(float a)
108 {
109 #if ASTCENC_SSE >= 41
110 __m128 v = _mm_set1_ps(a);
111 m = _mm_insert_ps(m, v, l << 6 | l << 4);
112 #else
113 alignas(16) float idx[4];
114 _mm_store_ps(idx, m);
115 idx[l] = a;
116 m = _mm_load_ps(idx);
117 #endif
118 }
119
120 /**
121 * @brief Factory that returns a vector of zeros.
122 */
zerovfloat4123 static ASTCENC_SIMD_INLINE vfloat4 zero()
124 {
125 return vfloat4(_mm_setzero_ps());
126 }
127
128 /**
129 * @brief Factory that returns a replicated scalar loaded from memory.
130 */
load1vfloat4131 static ASTCENC_SIMD_INLINE vfloat4 load1(const float* p)
132 {
133 return vfloat4(_mm_load_ps1(p));
134 }
135
136 /**
137 * @brief Factory that returns a vector loaded from 16B aligned memory.
138 */
loadavfloat4139 static ASTCENC_SIMD_INLINE vfloat4 loada(const float* p)
140 {
141 return vfloat4(_mm_load_ps(p));
142 }
143
144 /**
145 * @brief Factory that returns a vector containing the lane IDs.
146 */
lane_idvfloat4147 static ASTCENC_SIMD_INLINE vfloat4 lane_id()
148 {
149 return vfloat4(_mm_set_ps(3, 2, 1, 0));
150 }
151
152 /**
153 * @brief Return a swizzled float 2.
154 */
swzvfloat4155 template <int l0, int l1> ASTCENC_SIMD_INLINE vfloat4 swz() const
156 {
157 vfloat4 result(_mm_shuffle_ps(m, m, l0 | l1 << 2));
158 result.set_lane<2>(0.0f);
159 result.set_lane<3>(0.0f);
160 return result;
161 }
162
163 /**
164 * @brief Return a swizzled float 3.
165 */
swzvfloat4166 template <int l0, int l1, int l2> ASTCENC_SIMD_INLINE vfloat4 swz() const
167 {
168 vfloat4 result(_mm_shuffle_ps(m, m, l0 | l1 << 2 | l2 << 4));
169 result.set_lane<3>(0.0f);
170 return result;
171 }
172
173 /**
174 * @brief Return a swizzled float 4.
175 */
swzvfloat4176 template <int l0, int l1, int l2, int l3> ASTCENC_SIMD_INLINE vfloat4 swz() const
177 {
178 return vfloat4(_mm_shuffle_ps(m, m, l0 | l1 << 2 | l2 << 4 | l3 << 6));
179 }
180
181 /**
182 * @brief The vector ...
183 */
184 __m128 m;
185 };
186
187 // ============================================================================
188 // vint4 data type
189 // ============================================================================
190
191 /**
192 * @brief Data type for 4-wide ints.
193 */
194 struct vint4
195 {
196 /**
197 * @brief Construct from zero-initialized value.
198 */
199 ASTCENC_SIMD_INLINE vint4() = default;
200
201 /**
202 * @brief Construct from 4 values loaded from an unaligned address.
203 *
204 * Consider using loada() which is better with vectors if data is aligned
205 * to vector length.
206 */
vint4vint4207 ASTCENC_SIMD_INLINE explicit vint4(const int *p)
208 {
209 m = _mm_loadu_si128(reinterpret_cast<const __m128i*>(p));
210 }
211
212 /**
213 * @brief Construct from 4 uint8_t loaded from an unaligned address.
214 */
vint4vint4215 ASTCENC_SIMD_INLINE explicit vint4(const uint8_t *p)
216 {
217 // _mm_loadu_si32 would be nicer syntax, but missing on older GCC
218 __m128i t = _mm_cvtsi32_si128(*reinterpret_cast<const int*>(p));
219
220 #if ASTCENC_SSE >= 41
221 m = _mm_cvtepu8_epi32(t);
222 #else
223 t = _mm_unpacklo_epi8(t, _mm_setzero_si128());
224 m = _mm_unpacklo_epi16(t, _mm_setzero_si128());
225 #endif
226 }
227
228 /**
229 * @brief Construct from 1 scalar value replicated across all lanes.
230 *
231 * Consider using vfloat4::zero() for constexpr zeros.
232 */
vint4vint4233 ASTCENC_SIMD_INLINE explicit vint4(int a)
234 {
235 m = _mm_set1_epi32(a);
236 }
237
238 /**
239 * @brief Construct from 4 scalar values.
240 *
241 * The value of @c a is stored to lane 0 (LSB) in the SIMD register.
242 */
vint4vint4243 ASTCENC_SIMD_INLINE explicit vint4(int a, int b, int c, int d)
244 {
245 m = _mm_set_epi32(d, c, b, a);
246 }
247
248 /**
249 * @brief Construct from an existing SIMD register.
250 */
vint4vint4251 ASTCENC_SIMD_INLINE explicit vint4(__m128i a)
252 {
253 m = a;
254 }
255
256 /**
257 * @brief Get the scalar from a single lane.
258 */
lanevint4259 template <int l> ASTCENC_SIMD_INLINE int lane() const
260 {
261 return _mm_cvtsi128_si32(_mm_shuffle_epi32(m, l));
262 }
263
264 /**
265 * @brief Set the scalar value of a single lane.
266 */
set_lanevint4267 template <int l> ASTCENC_SIMD_INLINE void set_lane(int a)
268 {
269 #if ASTCENC_SSE >= 41
270 m = _mm_insert_epi32(m, a, l);
271 #else
272 alignas(16) int idx[4];
273 _mm_store_si128(reinterpret_cast<__m128i*>(idx), m);
274 idx[l] = a;
275 m = _mm_load_si128(reinterpret_cast<const __m128i*>(idx));
276 #endif
277 }
278
279 /**
280 * @brief Factory that returns a vector of zeros.
281 */
zerovint4282 static ASTCENC_SIMD_INLINE vint4 zero()
283 {
284 return vint4(_mm_setzero_si128());
285 }
286
287 /**
288 * @brief Factory that returns a replicated scalar loaded from memory.
289 */
load1vint4290 static ASTCENC_SIMD_INLINE vint4 load1(const int* p)
291 {
292 return vint4(*p);
293 }
294
295 /**
296 * @brief Factory that returns a vector loaded from 16B aligned memory.
297 */
loadavint4298 static ASTCENC_SIMD_INLINE vint4 loada(const int* p)
299 {
300 return vint4(_mm_load_si128(reinterpret_cast<const __m128i*>(p)));
301 }
302
303 /**
304 * @brief Factory that returns a vector containing the lane IDs.
305 */
lane_idvint4306 static ASTCENC_SIMD_INLINE vint4 lane_id()
307 {
308 return vint4(_mm_set_epi32(3, 2, 1, 0));
309 }
310
311 /**
312 * @brief The vector ...
313 */
314 __m128i m;
315 };
316
317 // ============================================================================
318 // vmask4 data type
319 // ============================================================================
320
321 /**
322 * @brief Data type for 4-wide control plane masks.
323 */
324 struct vmask4
325 {
326 /**
327 * @brief Construct from an existing SIMD register.
328 */
vmask4vmask4329 ASTCENC_SIMD_INLINE explicit vmask4(__m128 a)
330 {
331 m = a;
332 }
333
334 /**
335 * @brief Construct from an existing SIMD register.
336 */
vmask4vmask4337 ASTCENC_SIMD_INLINE explicit vmask4(__m128i a)
338 {
339 m = _mm_castsi128_ps(a);
340 }
341
342 /**
343 * @brief Construct from 1 scalar value.
344 */
vmask4vmask4345 ASTCENC_SIMD_INLINE explicit vmask4(bool a)
346 {
347 vint4 mask(a == false ? 0 : -1);
348 m = _mm_castsi128_ps(mask.m);
349 }
350
351 /**
352 * @brief Construct from 4 scalar values.
353 *
354 * The value of @c a is stored to lane 0 (LSB) in the SIMD register.
355 */
vmask4vmask4356 ASTCENC_SIMD_INLINE explicit vmask4(bool a, bool b, bool c, bool d)
357 {
358 vint4 mask(a == false ? 0 : -1,
359 b == false ? 0 : -1,
360 c == false ? 0 : -1,
361 d == false ? 0 : -1);
362
363 m = _mm_castsi128_ps(mask.m);
364 }
365
366 /**
367 * @brief The vector ...
368 */
369 __m128 m;
370 };
371
372 // ============================================================================
373 // vmask4 operators and functions
374 // ============================================================================
375
376 /**
377 * @brief Overload: mask union (or).
378 */
379 ASTCENC_SIMD_INLINE vmask4 operator|(vmask4 a, vmask4 b)
380 {
381 return vmask4(_mm_or_ps(a.m, b.m));
382 }
383
384 /**
385 * @brief Overload: mask intersect (and).
386 */
387 ASTCENC_SIMD_INLINE vmask4 operator&(vmask4 a, vmask4 b)
388 {
389 return vmask4(_mm_and_ps(a.m, b.m));
390 }
391
392 /**
393 * @brief Overload: mask difference (xor).
394 */
395 ASTCENC_SIMD_INLINE vmask4 operator^(vmask4 a, vmask4 b)
396 {
397 return vmask4(_mm_xor_ps(a.m, b.m));
398 }
399
400 /**
401 * @brief Overload: mask invert (not).
402 */
403 ASTCENC_SIMD_INLINE vmask4 operator~(vmask4 a)
404 {
405 return vmask4(_mm_xor_si128(_mm_castps_si128(a.m), _mm_set1_epi32(-1)));
406 }
407
408 /**
409 * @brief Return a 4-bit mask code indicating mask status.
410 *
411 * bit0 = lane 0
412 */
mask(vmask4 a)413 ASTCENC_SIMD_INLINE unsigned int mask(vmask4 a)
414 {
415 return _mm_movemask_ps(a.m);
416 }
417
418 // ============================================================================
419 // vint4 operators and functions
420 // ============================================================================
421
422 /**
423 * @brief Overload: vector by vector addition.
424 */
425 ASTCENC_SIMD_INLINE vint4 operator+(vint4 a, vint4 b)
426 {
427 return vint4(_mm_add_epi32(a.m, b.m));
428 }
429
430 /**
431 * @brief Overload: vector by vector subtraction.
432 */
433 ASTCENC_SIMD_INLINE vint4 operator-(vint4 a, vint4 b)
434 {
435 return vint4(_mm_sub_epi32(a.m, b.m));
436 }
437
438 /**
439 * @brief Overload: vector by vector multiplication.
440 */
441 ASTCENC_SIMD_INLINE vint4 operator*(vint4 a, vint4 b)
442 {
443 #if ASTCENC_SSE >= 41
444 return vint4(_mm_mullo_epi32 (a.m, b.m));
445 #else
446 __m128i t1 = _mm_mul_epu32(a.m, b.m);
447 __m128i t2 = _mm_mul_epu32(
448 _mm_srli_si128(a.m, 4),
449 _mm_srli_si128(b.m, 4));
450 __m128i r = _mm_unpacklo_epi32(
451 _mm_shuffle_epi32(t1, _MM_SHUFFLE (0, 0, 2, 0)),
452 _mm_shuffle_epi32(t2, _MM_SHUFFLE (0, 0, 2, 0)));
453 return vint4(r);
454 #endif
455 }
456
457 /**
458 * @brief Overload: vector bit invert.
459 */
460 ASTCENC_SIMD_INLINE vint4 operator~(vint4 a)
461 {
462 return vint4(_mm_xor_si128(a.m, _mm_set1_epi32(-1)));
463 }
464
465 /**
466 * @brief Overload: vector by vector bitwise or.
467 */
468 ASTCENC_SIMD_INLINE vint4 operator|(vint4 a, vint4 b)
469 {
470 return vint4(_mm_or_si128(a.m, b.m));
471 }
472
473 /**
474 * @brief Overload: vector by vector bitwise and.
475 */
476 ASTCENC_SIMD_INLINE vint4 operator&(vint4 a, vint4 b)
477 {
478 return vint4(_mm_and_si128(a.m, b.m));
479 }
480
481 /**
482 * @brief Overload: vector by vector bitwise xor.
483 */
484 ASTCENC_SIMD_INLINE vint4 operator^(vint4 a, vint4 b)
485 {
486 return vint4(_mm_xor_si128(a.m, b.m));
487 }
488
489 /**
490 * @brief Overload: vector by vector equality.
491 */
492 ASTCENC_SIMD_INLINE vmask4 operator==(vint4 a, vint4 b)
493 {
494 return vmask4(_mm_cmpeq_epi32(a.m, b.m));
495 }
496
497 /**
498 * @brief Overload: vector by vector inequality.
499 */
500 ASTCENC_SIMD_INLINE vmask4 operator!=(vint4 a, vint4 b)
501 {
502 return ~vmask4(_mm_cmpeq_epi32(a.m, b.m));
503 }
504
505 /**
506 * @brief Overload: vector by vector less than.
507 */
508 ASTCENC_SIMD_INLINE vmask4 operator<(vint4 a, vint4 b)
509 {
510 return vmask4(_mm_cmplt_epi32(a.m, b.m));
511 }
512
513 /**
514 * @brief Overload: vector by vector greater than.
515 */
516 ASTCENC_SIMD_INLINE vmask4 operator>(vint4 a, vint4 b)
517 {
518 return vmask4(_mm_cmpgt_epi32(a.m, b.m));
519 }
520
521 /**
522 * @brief Logical shift left.
523 */
lsl(vint4 a)524 template <int s> ASTCENC_SIMD_INLINE vint4 lsl(vint4 a)
525 {
526 return vint4(_mm_slli_epi32(a.m, s));
527 }
528
529 /**
530 * @brief Logical shift right.
531 */
lsr(vint4 a)532 template <int s> ASTCENC_SIMD_INLINE vint4 lsr(vint4 a)
533 {
534 return vint4(_mm_srli_epi32(a.m, s));
535 }
536
537 /**
538 * @brief Arithmetic shift right.
539 */
asr(vint4 a)540 template <int s> ASTCENC_SIMD_INLINE vint4 asr(vint4 a)
541 {
542 return vint4(_mm_srai_epi32(a.m, s));
543 }
544
545 /**
546 * @brief Return the min vector of two vectors.
547 */
min(vint4 a,vint4 b)548 ASTCENC_SIMD_INLINE vint4 min(vint4 a, vint4 b)
549 {
550 #if ASTCENC_SSE >= 41
551 return vint4(_mm_min_epi32(a.m, b.m));
552 #else
553 vmask4 d = a < b;
554 __m128i ap = _mm_and_si128(_mm_castps_si128(d.m), a.m);
555 __m128i bp = _mm_andnot_si128(_mm_castps_si128(d.m), b.m);
556 return vint4(_mm_or_si128(ap,bp));
557 #endif
558 }
559
560 /**
561 * @brief Return the max vector of two vectors.
562 */
max(vint4 a,vint4 b)563 ASTCENC_SIMD_INLINE vint4 max(vint4 a, vint4 b)
564 {
565 #if ASTCENC_SSE >= 41
566 return vint4(_mm_max_epi32(a.m, b.m));
567 #else
568 vmask4 d = a > b;
569 __m128i ap = _mm_and_si128(_mm_castps_si128(d.m), a.m);
570 __m128i bp = _mm_andnot_si128(_mm_castps_si128(d.m), b.m);
571 return vint4(_mm_or_si128(ap,bp));
572 #endif
573 }
574
575 /**
576 * @brief Return the horizontal minimum of a vector.
577 */
hmin(vint4 a)578 ASTCENC_SIMD_INLINE vint4 hmin(vint4 a)
579 {
580 a = min(a, vint4(_mm_shuffle_epi32(a.m, _MM_SHUFFLE(0, 0, 3, 2))));
581 a = min(a, vint4(_mm_shuffle_epi32(a.m, _MM_SHUFFLE(0, 0, 0, 1))));
582 return vint4(_mm_shuffle_epi32(a.m, _MM_SHUFFLE(0, 0, 0, 0)));
583 }
584
585 /*
586 * @brief Return the horizontal maximum of a vector.
587 */
hmax(vint4 a)588 ASTCENC_SIMD_INLINE vint4 hmax(vint4 a)
589 {
590 a = max(a, vint4(_mm_shuffle_epi32(a.m, _MM_SHUFFLE(0, 0, 3, 2))));
591 a = max(a, vint4(_mm_shuffle_epi32(a.m, _MM_SHUFFLE(0, 0, 0, 1))));
592 return vint4(_mm_shuffle_epi32(a.m, _MM_SHUFFLE(0, 0, 0, 0)));
593 }
594
595 /**
596 * @brief Return the horizontal sum of a vector as a scalar.
597 */
hadd_s(vint4 a)598 ASTCENC_SIMD_INLINE int hadd_s(vint4 a)
599 {
600 // Add top and bottom halves, lane 1/0
601 __m128i fold = _mm_castps_si128(_mm_movehl_ps(_mm_castsi128_ps(a.m),
602 _mm_castsi128_ps(a.m)));
603 __m128i t = _mm_add_epi32(a.m, fold);
604
605 // Add top and bottom halves, lane 0 (_mm_hadd_ps exists but slow)
606 t = _mm_add_epi32(t, _mm_shuffle_epi32(t, 0x55));
607
608 return _mm_cvtsi128_si32(t);
609 }
610
611 /**
612 * @brief Store a vector to a 16B aligned memory address.
613 */
storea(vint4 a,int * p)614 ASTCENC_SIMD_INLINE void storea(vint4 a, int* p)
615 {
616 _mm_store_si128(reinterpret_cast<__m128i*>(p), a.m);
617 }
618
619 /**
620 * @brief Store a vector to an unaligned memory address.
621 */
store(vint4 a,int * p)622 ASTCENC_SIMD_INLINE void store(vint4 a, int* p)
623 {
624 // Cast due to missing intrinsics
625 _mm_storeu_ps(reinterpret_cast<float*>(p), _mm_castsi128_ps(a.m));
626 }
627
628 /**
629 * @brief Store lowest N (vector width) bytes into an unaligned address.
630 */
store_nbytes(vint4 a,uint8_t * p)631 ASTCENC_SIMD_INLINE void store_nbytes(vint4 a, uint8_t* p)
632 {
633 // Cast due to missing intrinsics
634 _mm_store_ss(reinterpret_cast<float*>(p), _mm_castsi128_ps(a.m));
635 }
636
637 /**
638 * @brief Gather N (vector width) indices from the array.
639 */
gatheri(const int * base,vint4 indices)640 ASTCENC_SIMD_INLINE vint4 gatheri(const int* base, vint4 indices)
641 {
642 #if ASTCENC_AVX >= 2
643 return vint4(_mm_i32gather_epi32(base, indices.m, 4));
644 #else
645 alignas(16) int idx[4];
646 storea(indices, idx);
647 return vint4(base[idx[0]], base[idx[1]], base[idx[2]], base[idx[3]]);
648 #endif
649 }
650
651 /**
652 * @brief Pack low 8 bits of N (vector width) lanes into bottom of vector.
653 */
pack_low_bytes(vint4 a)654 ASTCENC_SIMD_INLINE vint4 pack_low_bytes(vint4 a)
655 {
656 #if ASTCENC_SSE >= 41
657 __m128i shuf = _mm_set_epi8(0,0,0,0, 0,0,0,0, 0,0,0,0, 12,8,4,0);
658 return vint4(_mm_shuffle_epi8(a.m, shuf));
659 #else
660 __m128i va = _mm_unpacklo_epi8(a.m, _mm_shuffle_epi32(a.m, _MM_SHUFFLE(1,1,1,1)));
661 __m128i vb = _mm_unpackhi_epi8(a.m, _mm_shuffle_epi32(a.m, _MM_SHUFFLE(3,3,3,3)));
662 return vint4(_mm_unpacklo_epi16(va, vb));
663 #endif
664 }
665
666 /**
667 * @brief Return lanes from @c b if @c cond is set, else @c a.
668 */
select(vint4 a,vint4 b,vmask4 cond)669 ASTCENC_SIMD_INLINE vint4 select(vint4 a, vint4 b, vmask4 cond)
670 {
671 __m128i condi = _mm_castps_si128(cond.m);
672
673 #if ASTCENC_SSE >= 41
674 return vint4(_mm_blendv_epi8(a.m, b.m, condi));
675 #else
676 return vint4(_mm_or_si128(_mm_and_si128(condi, b.m), _mm_andnot_si128(condi, a.m)));
677 #endif
678 }
679
680 // ============================================================================
681 // vfloat4 operators and functions
682 // ============================================================================
683
684 /**
685 * @brief Overload: vector by vector addition.
686 */
687 ASTCENC_SIMD_INLINE vfloat4 operator+(vfloat4 a, vfloat4 b)
688 {
689 return vfloat4(_mm_add_ps(a.m, b.m));
690 }
691
692 /**
693 * @brief Overload: vector by vector subtraction.
694 */
695 ASTCENC_SIMD_INLINE vfloat4 operator-(vfloat4 a, vfloat4 b)
696 {
697 return vfloat4(_mm_sub_ps(a.m, b.m));
698 }
699
700 /**
701 * @brief Overload: vector by vector multiplication.
702 */
703 ASTCENC_SIMD_INLINE vfloat4 operator*(vfloat4 a, vfloat4 b)
704 {
705 return vfloat4(_mm_mul_ps(a.m, b.m));
706 }
707
708 /**
709 * @brief Overload: vector by vector division.
710 */
711 ASTCENC_SIMD_INLINE vfloat4 operator/(vfloat4 a, vfloat4 b)
712 {
713 return vfloat4(_mm_div_ps(a.m, b.m));
714 }
715
716 /**
717 * @brief Overload: vector by vector equality.
718 */
719 ASTCENC_SIMD_INLINE vmask4 operator==(vfloat4 a, vfloat4 b)
720 {
721 return vmask4(_mm_cmpeq_ps(a.m, b.m));
722 }
723
724 /**
725 * @brief Overload: vector by vector inequality.
726 */
727 ASTCENC_SIMD_INLINE vmask4 operator!=(vfloat4 a, vfloat4 b)
728 {
729 return vmask4(_mm_cmpneq_ps(a.m, b.m));
730 }
731
732 /**
733 * @brief Overload: vector by vector less than.
734 */
735 ASTCENC_SIMD_INLINE vmask4 operator<(vfloat4 a, vfloat4 b)
736 {
737 return vmask4(_mm_cmplt_ps(a.m, b.m));
738 }
739
740 /**
741 * @brief Overload: vector by vector greater than.
742 */
743 ASTCENC_SIMD_INLINE vmask4 operator>(vfloat4 a, vfloat4 b)
744 {
745 return vmask4(_mm_cmpgt_ps(a.m, b.m));
746 }
747
748 /**
749 * @brief Overload: vector by vector less than or equal.
750 */
751 ASTCENC_SIMD_INLINE vmask4 operator<=(vfloat4 a, vfloat4 b)
752 {
753 return vmask4(_mm_cmple_ps(a.m, b.m));
754 }
755
756 /**
757 * @brief Overload: vector by vector greater than or equal.
758 */
759 ASTCENC_SIMD_INLINE vmask4 operator>=(vfloat4 a, vfloat4 b)
760 {
761 return vmask4(_mm_cmpge_ps(a.m, b.m));
762 }
763
764 /**
765 * @brief Return the min vector of two vectors.
766 *
767 * If either lane value is NaN, @c b will be returned for that lane.
768 */
min(vfloat4 a,vfloat4 b)769 ASTCENC_SIMD_INLINE vfloat4 min(vfloat4 a, vfloat4 b)
770 {
771 // Do not reorder - second operand will return if either is NaN
772 return vfloat4(_mm_min_ps(a.m, b.m));
773 }
774
775 /**
776 * @brief Return the max vector of two vectors.
777 *
778 * If either lane value is NaN, @c b will be returned for that lane.
779 */
max(vfloat4 a,vfloat4 b)780 ASTCENC_SIMD_INLINE vfloat4 max(vfloat4 a, vfloat4 b)
781 {
782 // Do not reorder - second operand will return if either is NaN
783 return vfloat4(_mm_max_ps(a.m, b.m));
784 }
785
786 /**
787 * @brief Return the absolute value of the float vector.
788 */
abs(vfloat4 a)789 ASTCENC_SIMD_INLINE vfloat4 abs(vfloat4 a)
790 {
791 return vfloat4(_mm_max_ps(_mm_sub_ps(_mm_setzero_ps(), a.m), a.m));
792 }
793
794 /**
795 * @brief Return a float rounded to the nearest integer value.
796 */
round(vfloat4 a)797 ASTCENC_SIMD_INLINE vfloat4 round(vfloat4 a)
798 {
799 #if ASTCENC_SSE >= 41
800 constexpr int flags = _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC;
801 return vfloat4(_mm_round_ps(a.m, flags));
802 #else
803 __m128 v = a.m;
804 __m128 neg_zero = _mm_castsi128_ps(_mm_set1_epi32(0x80000000));
805 __m128 no_fraction = _mm_set1_ps(8388608.0f);
806 __m128 abs_mask = _mm_castsi128_ps(_mm_set1_epi32(0x7FFFFFFF));
807 __m128 sign = _mm_and_ps(v, neg_zero);
808 __m128 s_magic = _mm_or_ps(no_fraction, sign);
809 __m128 r1 = _mm_add_ps(v, s_magic);
810 r1 = _mm_sub_ps(r1, s_magic);
811 __m128 r2 = _mm_and_ps(v, abs_mask);
812 __m128 mask = _mm_cmple_ps(r2, no_fraction);
813 r2 = _mm_andnot_ps(mask, v);
814 r1 = _mm_and_ps(r1, mask);
815 return vfloat4(_mm_xor_ps(r1, r2));
816 #endif
817 }
818
819 /**
820 * @brief Return the horizontal minimum of a vector.
821 */
hmin(vfloat4 a)822 ASTCENC_SIMD_INLINE vfloat4 hmin(vfloat4 a)
823 {
824 a = min(a, vfloat4(_mm_shuffle_ps(a.m, a.m, _MM_SHUFFLE(0, 0, 3, 2))));
825 a = min(a, vfloat4(_mm_shuffle_ps(a.m, a.m, _MM_SHUFFLE(0, 0, 0, 1))));
826 return vfloat4(_mm_shuffle_ps(a.m, a.m, _MM_SHUFFLE(0, 0, 0, 0)));
827 }
828
829 /**
830 * @brief Return the horizontal maximum of a vector.
831 */
hmax(vfloat4 a)832 ASTCENC_SIMD_INLINE vfloat4 hmax(vfloat4 a)
833 {
834 a = max(a, vfloat4(_mm_shuffle_ps(a.m, a.m, _MM_SHUFFLE(0, 0, 3, 2))));
835 a = max(a, vfloat4(_mm_shuffle_ps(a.m, a.m, _MM_SHUFFLE(0, 0, 0, 1))));
836 return vfloat4(_mm_shuffle_ps(a.m, a.m, _MM_SHUFFLE(0, 0, 0, 0)));
837 }
838
839 /**
840 * @brief Return the horizontal sum of a vector as a scalar.
841 */
hadd_s(vfloat4 a)842 ASTCENC_SIMD_INLINE float hadd_s(vfloat4 a)
843 {
844 // Add top and bottom halves, lane 1/0
845 __m128 t = _mm_add_ps(a.m, _mm_movehl_ps(a.m, a.m));
846
847 // Add top and bottom halves, lane 0 (_mm_hadd_ps exists but slow)
848 t = _mm_add_ss(t, _mm_shuffle_ps(t, t, 0x55));
849
850 return _mm_cvtss_f32(t);
851 }
852
853 /**
854 * @brief Return the sqrt of the lanes in the vector.
855 */
sqrt(vfloat4 a)856 ASTCENC_SIMD_INLINE vfloat4 sqrt(vfloat4 a)
857 {
858 return vfloat4(_mm_sqrt_ps(a.m));
859 }
860
861 /**
862 * @brief Return lanes from @c b if @c cond is set, else @c a.
863 */
select(vfloat4 a,vfloat4 b,vmask4 cond)864 ASTCENC_SIMD_INLINE vfloat4 select(vfloat4 a, vfloat4 b, vmask4 cond)
865 {
866 #if ASTCENC_SSE >= 41
867 return vfloat4(_mm_blendv_ps(a.m, b.m, cond.m));
868 #else
869 return vfloat4(_mm_or_ps(_mm_and_ps(cond.m, b.m), _mm_andnot_ps(cond.m, a.m)));
870 #endif
871 }
872
873 /**
874 * @brief Return lanes from @c b if MSB of @c cond is set, else @c a.
875 */
select_msb(vfloat4 a,vfloat4 b,vmask4 cond)876 ASTCENC_SIMD_INLINE vfloat4 select_msb(vfloat4 a, vfloat4 b, vmask4 cond)
877 {
878 #if ASTCENC_SSE >= 41
879 return vfloat4(_mm_blendv_ps(a.m, b.m, cond.m));
880 #else
881 __m128 d = _mm_castsi128_ps(_mm_srai_epi32(_mm_castps_si128(cond.m), 31));
882 return vfloat4(_mm_or_ps(_mm_and_ps(d, b.m), _mm_andnot_ps(d, a.m)));
883 #endif
884 }
885
886 /**
887 * @brief Load a vector of gathered results from an array;
888 */
gatherf(const float * base,vint4 indices)889 ASTCENC_SIMD_INLINE vfloat4 gatherf(const float* base, vint4 indices)
890 {
891 #if ASTCENC_AVX >= 2
892 return vfloat4(_mm_i32gather_ps(base, indices.m, 4));
893 #else
894 alignas(16) int idx[4];
895 storea(indices, idx);
896 return vfloat4(base[idx[0]], base[idx[1]], base[idx[2]], base[idx[3]]);
897 #endif
898 }
899
900 /**
901 * @brief Store a vector to an unaligned memory address.
902 */
store(vfloat4 a,float * p)903 ASTCENC_SIMD_INLINE void store(vfloat4 a, float* p)
904 {
905 _mm_storeu_ps(p, a.m);
906 }
907
908 /**
909 * @brief Store a vector to a 16B aligned memory address.
910 */
storea(vfloat4 a,float * p)911 ASTCENC_SIMD_INLINE void storea(vfloat4 a, float* p)
912 {
913 _mm_store_ps(p, a.m);
914 }
915
916 /**
917 * @brief Return a integer value for a float vector, using truncation.
918 */
float_to_int(vfloat4 a)919 ASTCENC_SIMD_INLINE vint4 float_to_int(vfloat4 a)
920 {
921 return vint4(_mm_cvttps_epi32(a.m));
922 }
923
924 /**
925 * @brief Return a integer value for a float vector, using round-to-nearest.
926 */
float_to_int_rtn(vfloat4 a)927 ASTCENC_SIMD_INLINE vint4 float_to_int_rtn(vfloat4 a)
928 {
929 a = round(a);
930 return vint4(_mm_cvttps_epi32(a.m));
931 }
932
933 /**
934 * @brief Return a float value for an integer vector.
935 */
int_to_float(vint4 a)936 ASTCENC_SIMD_INLINE vfloat4 int_to_float(vint4 a)
937 {
938 return vfloat4(_mm_cvtepi32_ps(a.m));
939 }
940
941 /**
942 * @brief Return a float16 value for a float vector, using round-to-nearest.
943 */
float_to_float16(vfloat4 a)944 ASTCENC_SIMD_INLINE vint4 float_to_float16(vfloat4 a)
945 {
946 #if ASTCENC_F16C >= 1
947 __m128i packedf16 = _mm_cvtps_ph(a.m, 0);
948 __m128i f16 = _mm_cvtepu16_epi32(packedf16);
949 return vint4(f16);
950 #else
951 return vint4(
952 float_to_sf16(a.lane<0>()),
953 float_to_sf16(a.lane<1>()),
954 float_to_sf16(a.lane<2>()),
955 float_to_sf16(a.lane<3>()));
956 #endif
957 }
958
959 /**
960 * @brief Return a float16 value for a float scalar, using round-to-nearest.
961 */
float_to_float16(float a)962 static inline uint16_t float_to_float16(float a)
963 {
964 #if ASTCENC_F16C >= 1
965 __m128i f16 = _mm_cvtps_ph(_mm_set1_ps(a), 0);
966 return static_cast<uint16_t>(_mm_cvtsi128_si32(f16));
967 #else
968 return float_to_sf16(a);
969 #endif
970 }
971
972 /**
973 * @brief Return a float value for a float16 vector.
974 */
float16_to_float(vint4 a)975 ASTCENC_SIMD_INLINE vfloat4 float16_to_float(vint4 a)
976 {
977 #if ASTCENC_F16C >= 1
978 __m128i packed = _mm_packs_epi32(a.m, a.m);
979 __m128 f32 = _mm_cvtph_ps(packed);
980 return vfloat4(f32);
981 #else
982 return vfloat4(
983 sf16_to_float(a.lane<0>()),
984 sf16_to_float(a.lane<1>()),
985 sf16_to_float(a.lane<2>()),
986 sf16_to_float(a.lane<3>()));
987 #endif
988 }
989
990 /**
991 * @brief Return a float value for a float16 scalar.
992 */
float16_to_float(uint16_t a)993 ASTCENC_SIMD_INLINE float float16_to_float(uint16_t a)
994 {
995 #if ASTCENC_F16C >= 1
996 __m128i packed = _mm_set1_epi16(a);
997 __m128 f32 = _mm_cvtph_ps(packed);
998 return _mm_cvtss_f32(f32);
999 #else
1000 return sf16_to_float(a);
1001 #endif
1002 }
1003
1004 /**
1005 * @brief Return a float value as an integer bit pattern (i.e. no conversion).
1006 *
1007 * It is a common trick to convert floats into integer bit patterns, perform
1008 * some bit hackery based on knowledge they are IEEE 754 layout, and then
1009 * convert them back again. This is the first half of that flip.
1010 */
float_as_int(vfloat4 a)1011 ASTCENC_SIMD_INLINE vint4 float_as_int(vfloat4 a)
1012 {
1013 return vint4(_mm_castps_si128(a.m));
1014 }
1015
1016 /**
1017 * @brief Return a integer value as a float bit pattern (i.e. no conversion).
1018 *
1019 * It is a common trick to convert floats into integer bit patterns, perform
1020 * some bit hackery based on knowledge they are IEEE 754 layout, and then
1021 * convert them back again. This is the second half of that flip.
1022 */
int_as_float(vint4 v)1023 ASTCENC_SIMD_INLINE vfloat4 int_as_float(vint4 v)
1024 {
1025 return vfloat4(_mm_castsi128_ps(v.m));
1026 }
1027
1028 #if defined(ASTCENC_NO_INVARIANCE) && (ASTCENC_SSE >= 41)
1029
1030 #define ASTCENC_USE_NATIVE_DOT_PRODUCT 1
1031
1032 /**
1033 * @brief Return the dot product for the full 4 lanes, returning scalar.
1034 */
dot_s(vfloat4 a,vfloat4 b)1035 ASTCENC_SIMD_INLINE float dot_s(vfloat4 a, vfloat4 b)
1036 {
1037 return _mm_cvtss_f32(_mm_dp_ps(a.m, b.m, 0xFF));
1038 }
1039
1040 /**
1041 * @brief Return the dot product for the full 4 lanes, returning vector.
1042 */
dot(vfloat4 a,vfloat4 b)1043 ASTCENC_SIMD_INLINE vfloat4 dot(vfloat4 a, vfloat4 b)
1044 {
1045 return vfloat4(_mm_dp_ps(a.m, b.m, 0xFF));
1046 }
1047
1048 /**
1049 * @brief Return the dot product for the bottom 3 lanes, returning scalar.
1050 */
dot3_s(vfloat4 a,vfloat4 b)1051 ASTCENC_SIMD_INLINE float dot3_s(vfloat4 a, vfloat4 b)
1052 {
1053 return _mm_cvtss_f32(_mm_dp_ps(a.m, b.m, 0x77));
1054 }
1055
1056 /**
1057 * @brief Return the dot product for the bottom 3 lanes, returning vector.
1058 */
dot3(vfloat4 a,vfloat4 b)1059 ASTCENC_SIMD_INLINE vfloat4 dot3(vfloat4 a, vfloat4 b)
1060 {
1061 return vfloat4(_mm_dp_ps(a.m, b.m, 0x77));
1062 }
1063
1064 #endif // #if defined(ASTCENC_NO_INVARIANCE) && (ASTCENC_SSE >= 41)
1065
1066 #if ASTCENC_POPCNT >= 1
1067
1068 #define ASTCENC_USE_NATIVE_POPCOUNT 1
1069
1070 /**
1071 * @brief Population bit count.
1072 *
1073 * @param v The value to population count.
1074 *
1075 * @return The number of 1 bits.
1076 */
popcount(uint64_t v)1077 ASTCENC_SIMD_INLINE int popcount(uint64_t v)
1078 {
1079 return static_cast<int>(_mm_popcnt_u64(v));
1080 }
1081
1082 #endif // ASTCENC_POPCNT >= 1
1083
1084 #endif // #ifndef ASTC_VECMATHLIB_SSE_4_H_INCLUDED
1085