/src/Simd/src/Simd/SimdLog.h
Line | Count | Source |
1 | | /* |
2 | | * Simd Library (http://ermig1979.github.io/Simd). |
3 | | * |
4 | | * Copyright (c) 2011-2026 Yermalayeu Ihar. |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | | * of this software and associated documentation files (the "Software"), to deal |
8 | | * in the Software without restriction, including without limitation the rights |
9 | | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10 | | * copies of the Software, and to permit persons to whom the Software is |
11 | | * furnished to do so, subject to the following conditions: |
12 | | * |
13 | | * The above copyright notice and this permission notice shall be included in |
14 | | * all copies or substantial portions of the Software. |
15 | | * |
16 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21 | | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22 | | * SOFTWARE. |
23 | | */ |
24 | | #ifndef __SimdLog_h__ |
25 | | #define __SimdLog_h__ |
26 | | |
27 | | #include "Simd/SimdConst.h" |
28 | | #include "Simd/SimdArray.h" |
29 | | |
30 | | #ifdef SIMD_LOG_ENABLE |
31 | | #include <iostream> |
32 | | #include <iomanip> |
33 | | |
34 | | namespace Simd |
35 | | { |
36 | | template<class T> SIMD_INLINE void Log(const T * data, size_t size, const std::string & name, int prec = 3) |
37 | | { |
38 | | std::cout << name.c_str() << " = { "; |
39 | | for (size_t i = 0; i < size; i++) |
40 | | { |
41 | | std::cout << int(data[i]) << " "; |
42 | | } |
43 | | std::cout << "} " << std::endl << std::flush; |
44 | | } |
45 | | |
46 | | template<> SIMD_INLINE void Log<float>(const float * data, size_t size, const std::string & name, int prec) |
47 | 0 | { |
48 | 0 | std::cout << name.c_str() << " = { " << std::setprecision(prec) << std::fixed; |
49 | 0 | for (size_t i = 0; i < size; i++) |
50 | 0 | { |
51 | 0 | std::cout << data[i] << " "; |
52 | 0 | } |
53 | 0 | std::cout << "} " << std::endl << std::flush; |
54 | 0 | } |
55 | | |
56 | | template<class T> SIMD_INLINE void Log(const Array<T> & array, const std::string & name, int prec = 3) |
57 | | { |
58 | | Log<T>(array.data, array.size, name); |
59 | | } |
60 | | |
61 | | #ifdef SIMD_SSE41_ENABLE |
62 | | namespace Sse41 |
63 | | { |
64 | | SIMD_INLINE void Log(const __m128 & value, const std::string & name) |
65 | 0 | { |
66 | 0 | float buffer[F]; |
67 | 0 | _mm_storeu_ps(buffer, value); |
68 | 0 | Simd::Log<float>(buffer, F, name); |
69 | 0 | } |
70 | | |
71 | | template<class T> SIMD_INLINE void Log(const __m128i & value, const std::string & name) |
72 | | { |
73 | | const size_t n = sizeof(__m128i) / sizeof(T); |
74 | | T buffer[n]; |
75 | | _mm_storeu_si128((__m128i*)buffer, value); |
76 | | Simd::Log<T>(buffer, n, name); |
77 | | } |
78 | | } |
79 | | #endif |
80 | | |
81 | | #ifdef SIMD_AVX2_ENABLE |
82 | | namespace Avx2 |
83 | | { |
84 | | SIMD_INLINE void Log(const __m256 & value, const std::string & name) |
85 | 0 | { |
86 | 0 | float buffer[F]; |
87 | 0 | _mm256_storeu_ps(buffer, value); |
88 | 0 | Simd::Log<float>(buffer, F, name); |
89 | 0 | } |
90 | | |
91 | | template<class T> SIMD_INLINE void Log(const __m256i & value, const std::string & name) |
92 | | { |
93 | | const size_t n = sizeof(__m256i) / sizeof(T); |
94 | | T buffer[n]; |
95 | | _mm256_storeu_si256((__m256i*)buffer, value); |
96 | | Simd::Log<T>(buffer, n, name); |
97 | | } |
98 | | } |
99 | | #endif |
100 | | |
101 | | #ifdef SIMD_AVX512BW_ENABLE |
102 | | namespace Avx512bw |
103 | | { |
104 | | SIMD_INLINE void Log(const __m512 & value, const std::string & name) |
105 | 0 | { |
106 | 0 | float buffer[F]; |
107 | 0 | _mm512_storeu_ps(buffer, value); |
108 | 0 | Simd::Log<float>(buffer, F, name); |
109 | 0 | } |
110 | | |
111 | | template<class T> SIMD_INLINE void Log(const __m512i& value, const std::string& name) |
112 | | { |
113 | | const size_t n = sizeof(__m512i) / sizeof(T); |
114 | | T buffer[n]; |
115 | | _mm512_storeu_si512((__m512i*)buffer, value); |
116 | | Simd::Log<T>(buffer, n, name); |
117 | | } |
118 | | } |
119 | | #endif |
120 | | |
121 | | #ifdef SIMD_AMXBF16_ENABLE |
122 | | namespace AmxBf16 |
123 | | { |
124 | | using Avx512bw::Log; |
125 | | } |
126 | | #endif |
127 | | |
128 | | #ifdef SIMD_NEON_ENABLE |
129 | | namespace Neon |
130 | | { |
131 | | SIMD_INLINE void Log8u(const uint8x16_t & value, const std::string & name) |
132 | | { |
133 | | uint8_t buffer[16]; |
134 | | vst1q_u8(buffer, value); |
135 | | Simd::Log(buffer, 16, name); |
136 | | } |
137 | | |
138 | | SIMD_INLINE void Log8u(const uint8x8_t& value, const std::string& name) |
139 | | { |
140 | | uint8_t buffer[8]; |
141 | | vst1_u8(buffer, value); |
142 | | Simd::Log(buffer, 8, name); |
143 | | } |
144 | | |
145 | | SIMD_INLINE void Log16u(const uint16x8_t & value, const std::string & name) |
146 | | { |
147 | | uint16_t buffer[8]; |
148 | | vst1q_u16(buffer, value); |
149 | | Simd::Log(buffer, 8, name); |
150 | | } |
151 | | |
152 | | SIMD_INLINE void Log16u(const uint16x4_t& value, const std::string& name) |
153 | | { |
154 | | uint16_t buffer[4]; |
155 | | vst1_u16(buffer, value); |
156 | | Simd::Log(buffer, 4, name); |
157 | | } |
158 | | |
159 | | SIMD_INLINE void Log16i(const int16x8_t & value, const std::string & name) |
160 | | { |
161 | | int16_t buffer[8]; |
162 | | vst1q_s16(buffer, value); |
163 | | Simd::Log(buffer, 8, name); |
164 | | } |
165 | | |
166 | | SIMD_INLINE void Log16i(const int16x4_t& value, const std::string& name) |
167 | | { |
168 | | int16_t buffer[4]; |
169 | | vst1_s16(buffer, value); |
170 | | Simd::Log(buffer, 4, name); |
171 | | } |
172 | | |
173 | | SIMD_INLINE void Log32u(const uint32x4_t & value, const std::string & name) |
174 | | { |
175 | | uint32_t buffer[4]; |
176 | | vst1q_u32(buffer, value); |
177 | | Simd::Log(buffer, 4, name); |
178 | | } |
179 | | |
180 | | SIMD_INLINE void Log32u(const uint32x2_t& value, const std::string& name) |
181 | | { |
182 | | uint32_t buffer[2]; |
183 | | vst1_u32(buffer, value); |
184 | | Simd::Log(buffer, 2, name); |
185 | | } |
186 | | |
187 | | SIMD_INLINE void Log32i(const int32x4_t & value, const std::string & name) |
188 | | { |
189 | | int32_t buffer[4]; |
190 | | vst1q_s32(buffer, value); |
191 | | Simd::Log(buffer, 4, name); |
192 | | } |
193 | | |
194 | | SIMD_INLINE void Log32i(const int32x2_t& value, const std::string& name) |
195 | | { |
196 | | int32_t buffer[2]; |
197 | | vst1_s32(buffer, value); |
198 | | Simd::Log(buffer, 2, name); |
199 | | } |
200 | | |
201 | | SIMD_INLINE void Log32f(const float32x4_t & value, const std::string & name) |
202 | | { |
203 | | float buffer[4]; |
204 | | vst1q_f32(buffer, value); |
205 | | std::cout << name << " = { "; |
206 | | for (int i = 0; i < 4; i++) |
207 | | std::cout << buffer[i] << " "; |
208 | | std::cout << "} " << std::endl; |
209 | | } |
210 | | |
211 | | SIMD_INLINE void Log32f(const float32x2_t& value, const std::string& name) |
212 | | { |
213 | | float buffer[2]; |
214 | | vst1_f32(buffer, value); |
215 | | std::cout << name << " = { "; |
216 | | for (int i = 0; i < 2; i++) |
217 | | std::cout << buffer[i] << " "; |
218 | | std::cout << "} " << std::endl; |
219 | | } |
220 | | } |
221 | | #endif |
222 | | } |
223 | | |
224 | | #define SIMD_LOG(value) Log(value, #value) |
225 | | #define SIMD_LOG1(value) Log<uint8_t>(value, #value) |
226 | | #define SIMD_LOG2(value) Log<int16_t>(value, #value) |
227 | | #define SIMD_LOG4(value) Log<int32_t>(value, #value) |
228 | | |
229 | | #define SIMD_LOG_ERROR(message) \ |
230 | | { \ |
231 | | std::stringstream ss; \ |
232 | | ss << std::endl << " In function " << SIMD_FUNCTION << ":" << std::endl; \ |
233 | | ss << " In file " << __FILE__ << ":" << __LINE__ << ":" << std::endl; \ |
234 | | ss << " Error: " << message << std::endl << std::endl; \ |
235 | | std::cerr << ss.str() << std::flush; \ |
236 | | } |
237 | | |
238 | | #else |
239 | | |
240 | | #define SIMD_LOG(value) |
241 | | #define SIMD_LOG1(value) |
242 | | #define SIMD_LOG2(value) |
243 | | #define SIMD_LOG4(value) |
244 | | |
245 | | #define SIMD_LOG_ERROR(message) |
246 | | |
247 | | #endif |
248 | | |
249 | | #endif |