Debian Codegraph @

third-party/simde/arm/neon/cnt.h

raw
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 <[email protected]>
25 */
26
27 #if !defined(SIMDE_ARM_NEON_CNT_H)
28 #define SIMDE_ARM_NEON_CNT_H
29
30 #include "types.h"
31 #include "reinterpret.h"
32 #include <limits.h>
33
34 HEDLEY_DIAGNOSTIC_PUSH
35 SIMDE_DISABLE_UNWANTED_DIAGNOSTICS
36 SIMDE_BEGIN_DECLS_
37
38 SIMDE_FUNCTION_ATTRIBUTES
39 uint8_t
40 simde_x_arm_neon_cntb(uint8_t v) {
41 v = v - ((v >> 1) & (85));
42 v = (v & (51)) + ((v >> (2)) & (51));
43 v = (v + (v >> (4))) & (15);
44 return HEDLEY_STATIC_CAST(uint8_t, v) >> (sizeof(uint8_t) - 1) * CHAR_BIT;
45 }
46
47 SIMDE_FUNCTION_ATTRIBUTES
48 simde_int8x8_t
49 simde_vcnt_s8(simde_int8x8_t a) {
50 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
51 return vcnt_s8(a);
52 #else
53 simde_int8x8_private
54 r_,
55 a_ = simde_int8x8_to_private(a);
56
57 SIMDE_VECTORIZE
58 for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
59 r_.values[i] = HEDLEY_STATIC_CAST(int8_t, simde_x_arm_neon_cntb(HEDLEY_STATIC_CAST(uint8_t, a_.values[i])));
60 }
61
62 return simde_int8x8_from_private(r_);
63 #endif
64 }
65 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
66 #undef vcnt_s8
67 #define vcnt_s8(a) simde_vcnt_s8((a))
68 #endif
69
70 SIMDE_FUNCTION_ATTRIBUTES
71 simde_uint8x8_t
72 simde_vcnt_u8(simde_uint8x8_t a) {
73 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
74 return vcnt_u8(a);
75 #else
76 simde_uint8x8_private
77 r_,
78 a_ = simde_uint8x8_to_private(a);
79
80 SIMDE_VECTORIZE
81 for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
82 r_.values[i] = simde_x_arm_neon_cntb(a_.values[i]);
83 }
84
85 return simde_uint8x8_from_private(r_);
86 #endif
87 }
88 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
89 #undef vcnt_u8
90 #define vcnt_u8(a) simde_vcnt_u8((a))
91 #endif
92
93 /* The x86 implementations are stolen from
94 * https://github.com/WebAssembly/simd/pull/379. They could be cleaned
95 * up a bit if someone is bored; they're mostly just direct
96 * translations from the assembly. */
97
98 SIMDE_FUNCTION_ATTRIBUTES
99 simde_int8x16_t
100 simde_vcntq_s8(simde_int8x16_t a) {
101 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
102 return vcntq_s8(a);
103 #elif defined(SIMDE_POWER_ALTIVEC_P8_NATIVE)
104 return HEDLEY_REINTERPRET_CAST(SIMDE_POWER_ALTIVEC_VECTOR(signed char), vec_popcnt(HEDLEY_REINTERPRET_CAST(SIMDE_POWER_ALTIVEC_VECTOR(unsigned char), a)));
105 #else
106 simde_int8x16_private
107 r_,
108 a_ = simde_int8x16_to_private(a);
109
110 #if defined(SIMDE_X86_AVX512VL_NATIVE) && defined(SIMDE_X86_AVX512BITALG_NATIVE)
111 r_.m128i = _mm_popcnt_epi8(a_.m128i);
112 #elif defined(SIMDE_X86_AVX2_NATIVE)
113 __m128i tmp0 = _mm_set1_epi8(0x0f);
114 __m128i tmp1 = _mm_andnot_si128(tmp0, a_.m128i);
115 __m128i y = _mm_and_si128(tmp0, a_.m128i);
116 tmp0 = _mm_set_epi8(4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0);
117 tmp1 = _mm_srli_epi16(tmp1, 4);
118 y = _mm_shuffle_epi8(tmp0, y);
119 tmp1 = _mm_shuffle_epi8(tmp0, tmp1);
120 r_.m128i = _mm_add_epi8(y, tmp1);
121 #elif defined(SIMDE_X86_SSSE3_NATIVE)
122 __m128i tmp0 = _mm_set1_epi8(0x0f);
123 __m128i tmp1 = a_.m128i;
124 tmp1 = _mm_and_si128(tmp1, tmp0);
125 tmp0 = _mm_andnot_si128(tmp0, a_.m128i);
126 __m128i y = _mm_set_epi8(4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0);
127 tmp0 = _mm_srli_epi16(tmp0, 4);
128 y = _mm_shuffle_epi8(y, tmp1);
129 tmp1 = _mm_set_epi8(4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0);
130 tmp1 = _mm_shuffle_epi8(tmp1, tmp0);
131 r_.m128i = _mm_add_epi8(y, tmp1);
132 #elif defined(SIMDE_X86_SSE2_NATIVE)
133 __m128i tmp = _mm_and_si128(_mm_srli_epi16(a_.m128i, 1), _mm_set1_epi8(0x55));
134 a_.m128i = _mm_sub_epi8(a_.m128i, tmp);
135 tmp = a_.m128i;
136 a_.m128i = _mm_and_si128(a_.m128i, _mm_set1_epi8(0x33));
137 tmp = _mm_and_si128(_mm_srli_epi16(tmp, 2), _mm_set1_epi8(0x33));
138 a_.m128i = _mm_add_epi8(a_.m128i, tmp);
139 tmp = _mm_srli_epi16(a_.m128i, 4);
140 a_.m128i = _mm_add_epi8(a_.m128i, tmp);
141 r_.m128i = _mm_and_si128(a_.m128i, _mm_set1_epi8(0x0f));
142 #else
143 SIMDE_VECTORIZE
144 for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
145 r_.values[i] = HEDLEY_STATIC_CAST(int8_t, simde_x_arm_neon_cntb(HEDLEY_STATIC_CAST(uint8_t, a_.values[i])));
146 }
147 #endif
148
149 return simde_int8x16_from_private(r_);
150 #endif
151 }
152 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
153 #undef vcntq_s8
154 #define vcntq_s8(a) simde_vcntq_s8((a))
155 #endif
156
157 SIMDE_FUNCTION_ATTRIBUTES
158 simde_uint8x16_t
159 simde_vcntq_u8(simde_uint8x16_t a) {
160 return simde_vreinterpretq_u8_s8(simde_vcntq_s8(simde_vreinterpretq_s8_u8(a)));
161 }
162 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
163 #undef vcntq_u8
164 #define vcntq_u8(a) simde_vcntq_u8((a))
165 #endif
166
167 SIMDE_END_DECLS_
168 HEDLEY_DIAGNOSTIC_POP
169
170 #endif /* !defined(SIMDE_ARM_NEON_CNT_H) */