1 /* SPDX-License-Identifier: MIT
2  *
3  * Permission is hereby granted, free of charge, to any person
4  * obtaining a copy of this software and associated documentation
5  * files (the "Software"), to deal in the Software without
6  * restriction, including without limitation the rights to use, copy,
7  * modify, merge, publish, distribute, sublicense, and/or sell copies
8  * of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be
12  * included in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Copyright:
24  *   2020      Evan Nemerson <evan@nemerson.com>
25  *   2020      Hidayat Khan <huk2209@gmail.com>
26  */
27 
28 #if !defined(SIMDE_X86_AVX512_PACKS_H)
29 #define SIMDE_X86_AVX512_PACKS_H
30 
31 #include "types.h"
32 #include "../avx2.h"
33 #include "mov.h"
34 
35 HEDLEY_DIAGNOSTIC_PUSH
36 SIMDE_DISABLE_UNWANTED_DIAGNOSTICS
37 SIMDE_BEGIN_DECLS_
38 
39 SIMDE_FUNCTION_ATTRIBUTES
40 simde__m512i
simde_mm512_packs_epi16(simde__m512i a,simde__m512i b)41 simde_mm512_packs_epi16 (simde__m512i a, simde__m512i b) {
42   #if defined(SIMDE_X86_AVX512BW_NATIVE)
43     return _mm512_packs_epi16(a, b);
44   #else
45     simde__m512i_private
46       r_,
47       a_ = simde__m512i_to_private(a),
48       b_ = simde__m512i_to_private(b);
49 
50     #if SIMDE_NATURAL_VECTOR_SIZE_LE(256)
51       r_.m256i[0] = simde_mm256_packs_epi16(a_.m256i[0], b_.m256i[0]);
52       r_.m256i[1] = simde_mm256_packs_epi16(a_.m256i[1], b_.m256i[1]);
53     #else
54       const size_t halfway_point = (sizeof(r_.i8) / sizeof(r_.i8[0])) / 2;
55       const size_t quarter_point = (sizeof(r_.i8) / sizeof(r_.i8[0])) / 4;
56       const size_t octet_point = (sizeof(r_.i8) / sizeof(r_.i8[0])) / 8;
57 
58       SIMDE_VECTORIZE
59       for (size_t i = 0 ; i < octet_point ; i++) {
60         r_.i8[i] = (a_.i16[i] > INT8_MAX) ? INT8_MAX : ((a_.i16[i] < INT8_MIN) ? INT8_MIN : HEDLEY_STATIC_CAST(int8_t, a_.i16[i]));
61         r_.i8[i + octet_point] = (b_.i16[i] > INT8_MAX) ? INT8_MAX : ((b_.i16[i] < INT8_MIN) ? INT8_MIN : HEDLEY_STATIC_CAST(int8_t, b_.i16[i]));
62         r_.i8[quarter_point + i] = (a_.i16[octet_point + i] > INT8_MAX) ? INT8_MAX : ((a_.i16[octet_point + i] < INT8_MIN) ? INT8_MIN : HEDLEY_STATIC_CAST(int8_t, a_.i16[octet_point + i]));
63         r_.i8[quarter_point + i + octet_point] = (b_.i16[octet_point + i] > INT8_MAX) ? INT8_MAX : ((b_.i16[octet_point + i] < INT8_MIN) ? INT8_MIN : HEDLEY_STATIC_CAST(int8_t, b_.i16[octet_point + i]));
64         r_.i8[halfway_point + i] = (a_.i16[quarter_point + i] > INT8_MAX) ? INT8_MAX : ((a_.i16[quarter_point + i] < INT8_MIN) ? INT8_MIN : HEDLEY_STATIC_CAST(int8_t, a_.i16[quarter_point + i]));
65         r_.i8[halfway_point + i + octet_point] = (b_.i16[quarter_point + i] > INT8_MAX) ? INT8_MAX : ((b_.i16[quarter_point + i] < INT8_MIN) ? INT8_MIN : HEDLEY_STATIC_CAST(int8_t, b_.i16[quarter_point + i]));
66         r_.i8[halfway_point + quarter_point + i]     = (a_.i16[quarter_point + octet_point + i] > INT8_MAX) ? INT8_MAX : ((a_.i16[quarter_point + octet_point + i] < INT8_MIN) ? INT8_MIN : HEDLEY_STATIC_CAST(int8_t, a_.i16[quarter_point + octet_point + i]));
67         r_.i8[halfway_point + quarter_point + i + octet_point] = (b_.i16[quarter_point + octet_point + i] > INT8_MAX) ? INT8_MAX : ((b_.i16[quarter_point + octet_point + i] < INT8_MIN) ? INT8_MIN : HEDLEY_STATIC_CAST(int8_t, b_.i16[quarter_point + octet_point + i]));
68       }
69     #endif
70 
71     return simde__m512i_from_private(r_);
72   #endif
73 }
74 #if defined(SIMDE_X86_AVX512BW_ENABLE_NATIVE_ALIASES)
75   #undef _mm512_packs_epi16
76   #define _mm512_packs_epi16(a, b) simde_mm512_packs_epi16(a, b)
77 #endif
78 
79 SIMDE_FUNCTION_ATTRIBUTES
80 simde__m512i
simde_mm512_packs_epi32(simde__m512i a,simde__m512i b)81 simde_mm512_packs_epi32 (simde__m512i a, simde__m512i b) {
82   #if defined(SIMDE_X86_AVX512BW_NATIVE)
83     return _mm512_packs_epi32(a, b);
84   #else
85     simde__m512i_private
86       r_,
87       a_ = simde__m512i_to_private(a),
88       b_ = simde__m512i_to_private(b);
89 
90     #if SIMDE_NATURAL_VECTOR_SIZE_LE(256)
91       r_.m256i[0] = simde_mm256_packs_epi32(a_.m256i[0], b_.m256i[0]);
92       r_.m256i[1] = simde_mm256_packs_epi32(a_.m256i[1], b_.m256i[1]);
93     #else
94       const size_t halfway_point = (sizeof(r_.i16) / sizeof(r_.i16[0])) / 2;
95       const size_t quarter_point = (sizeof(r_.i16) / sizeof(r_.i16[0])) / 4;
96       const size_t octet_point = (sizeof(r_.i16) / sizeof(r_.i16[0])) / 8;
97 
98       SIMDE_VECTORIZE
99       for (size_t i = 0 ; i < octet_point ; i++) {
100         r_.i16[i] = (a_.i32[i] > INT16_MAX) ? INT16_MAX : ((a_.i32[i] < INT16_MIN) ? INT16_MIN : HEDLEY_STATIC_CAST(int16_t, a_.i32[i]));
101         r_.i16[i + octet_point] = (b_.i32[i] > INT16_MAX) ? INT16_MAX : ((b_.i32[i] < INT16_MIN) ? INT16_MIN : HEDLEY_STATIC_CAST(int16_t, b_.i32[i]));
102         r_.i16[quarter_point + i] = (a_.i32[octet_point + i] > INT16_MAX) ? INT16_MAX : ((a_.i32[octet_point + i] < INT16_MIN) ? INT16_MIN : HEDLEY_STATIC_CAST(int16_t, a_.i32[octet_point + i]));
103         r_.i16[quarter_point + i + octet_point] = (b_.i32[octet_point + i] > INT16_MAX) ? INT16_MAX : ((b_.i32[octet_point + i] < INT16_MIN) ? INT16_MIN : HEDLEY_STATIC_CAST(int16_t, b_.i32[octet_point + i]));
104         r_.i16[halfway_point + i] = (a_.i32[quarter_point + i] > INT16_MAX) ? INT16_MAX : ((a_.i32[quarter_point +i] < INT16_MIN) ? INT16_MIN : HEDLEY_STATIC_CAST(int16_t, a_.i32[quarter_point + i]));
105         r_.i16[halfway_point + i + octet_point] = (b_.i32[quarter_point + i] > INT16_MAX) ? INT16_MAX : ((b_.i32[quarter_point + i] < INT16_MIN) ? INT16_MIN : HEDLEY_STATIC_CAST(int16_t, b_.i32[quarter_point +i]));
106         r_.i16[halfway_point + quarter_point + i] = (a_.i32[quarter_point + octet_point + i] > INT16_MAX) ? INT16_MAX : ((a_.i32[quarter_point + octet_point + i] < INT16_MIN) ? INT16_MIN : HEDLEY_STATIC_CAST(int16_t, a_.i32[quarter_point + octet_point + i]));
107         r_.i16[halfway_point + quarter_point + i + octet_point] = (b_.i32[quarter_point + octet_point + i] > INT16_MAX) ? INT16_MAX : ((b_.i32[quarter_point + octet_point + i] < INT16_MIN) ? INT16_MIN : HEDLEY_STATIC_CAST(int16_t, b_.i32[quarter_point + octet_point + i]));
108       }
109     #endif
110 
111     return simde__m512i_from_private(r_);
112   #endif
113 }
114 #if defined(SIMDE_X86_AVX512BW_ENABLE_NATIVE_ALIASES)
115   #undef _mm512_packs_epi32
116   #define _mm512_packs_epi32(a, b) simde_mm512_packs_epi32(a, b)
117 #endif
118 
119 SIMDE_END_DECLS_
120 HEDLEY_DIAGNOSTIC_POP
121 
122 #endif /* !defined(SIMDE_X86_AVX512_PACKS_H) */
123