Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/core/src/convert.hpp
16337 views
1
// This file is part of OpenCV project.
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
// of this distribution and at http://opencv.org/license.html
4
5
6
#ifndef SRC_CONVERT_HPP
7
#define SRC_CONVERT_HPP
8
9
#include "opencv2/core/types.hpp"
10
11
namespace cv
12
{
13
14
#if CV_SIMD
15
16
static inline void vx_load_as(const uchar* ptr, v_float32& a)
17
{ a = v_cvt_f32(v_reinterpret_as_s32(vx_load_expand_q(ptr))); }
18
19
static inline void vx_load_as(const schar* ptr, v_float32& a)
20
{ a = v_cvt_f32(vx_load_expand_q(ptr)); }
21
22
static inline void vx_load_as(const ushort* ptr, v_float32& a)
23
{ a = v_cvt_f32(v_reinterpret_as_s32(vx_load_expand(ptr))); }
24
25
static inline void vx_load_as(const short* ptr, v_float32& a)
26
{ a = v_cvt_f32(v_reinterpret_as_s32(vx_load_expand(ptr))); }
27
28
static inline void vx_load_as(const int* ptr, v_float32& a)
29
{ a = v_cvt_f32(vx_load(ptr)); }
30
31
static inline void vx_load_as(const float* ptr, v_float32& a)
32
{ a = vx_load(ptr); }
33
34
static inline void vx_load_as(const float16_t* ptr, v_float32& a)
35
{ a = vx_load_expand(ptr); }
36
37
static inline void v_store_as(ushort* ptr, const v_float32& a)
38
{ v_pack_u_store(ptr, v_round(a)); }
39
40
static inline void v_store_as(short* ptr, const v_float32& a)
41
{ v_pack_store(ptr, v_round(a)); }
42
43
static inline void v_store_as(int* ptr, const v_float32& a)
44
{ v_store(ptr, v_round(a)); }
45
46
static inline void v_store_as(float* ptr, const v_float32& a)
47
{ v_store(ptr, a); }
48
49
static inline void v_store_as(float16_t* ptr, const v_float32& a)
50
{ v_pack_store(ptr, a); }
51
52
static inline void vx_load_pair_as(const uchar* ptr, v_uint16& a, v_uint16& b)
53
{ v_expand(vx_load(ptr), a, b); }
54
55
static inline void vx_load_pair_as(const schar* ptr, v_uint16& a, v_uint16& b)
56
{
57
const v_int8 z = vx_setzero_s8();
58
v_int16 sa, sb;
59
v_expand(v_max(vx_load(ptr), z), sa, sb);
60
a = v_reinterpret_as_u16(sa);
61
b = v_reinterpret_as_u16(sb);
62
}
63
64
static inline void vx_load_pair_as(const ushort* ptr, v_uint16& a, v_uint16& b)
65
{ a = vx_load(ptr); b = vx_load(ptr + v_uint16::nlanes); }
66
67
static inline void vx_load_pair_as(const uchar* ptr, v_int16& a, v_int16& b)
68
{
69
v_uint16 ua, ub;
70
v_expand(vx_load(ptr), ua, ub);
71
a = v_reinterpret_as_s16(ua);
72
b = v_reinterpret_as_s16(ub);
73
}
74
75
static inline void vx_load_pair_as(const schar* ptr, v_int16& a, v_int16& b)
76
{ v_expand(vx_load(ptr), a, b); }
77
78
static inline void vx_load_pair_as(const short* ptr, v_int16& a, v_int16& b)
79
{ a = vx_load(ptr); b = vx_load(ptr + v_uint16::nlanes); }
80
81
static inline void vx_load_pair_as(const uchar* ptr, v_int32& a, v_int32& b)
82
{
83
v_uint32 ua, ub;
84
v_expand(vx_load_expand(ptr), ua, ub);
85
a = v_reinterpret_as_s32(ua);
86
b = v_reinterpret_as_s32(ub);
87
}
88
89
static inline void vx_load_pair_as(const schar* ptr, v_int32& a, v_int32& b)
90
{ v_expand(vx_load_expand(ptr), a, b); }
91
92
static inline void vx_load_pair_as(const ushort* ptr, v_int32& a, v_int32& b)
93
{
94
v_uint32 ua, ub;
95
v_expand(vx_load(ptr), ua, ub);
96
a = v_reinterpret_as_s32(ua);
97
b = v_reinterpret_as_s32(ub);
98
}
99
100
static inline void vx_load_pair_as(const short* ptr, v_int32& a, v_int32& b)
101
{
102
v_expand(vx_load(ptr), a, b);
103
}
104
105
static inline void vx_load_pair_as(const int* ptr, v_int32& a, v_int32& b)
106
{
107
a = vx_load(ptr);
108
b = vx_load(ptr + v_int32::nlanes);
109
}
110
111
static inline void vx_load_pair_as(const uchar* ptr, v_float32& a, v_float32& b)
112
{
113
v_uint32 ua, ub;
114
v_expand(vx_load_expand(ptr), ua, ub);
115
a = v_cvt_f32(v_reinterpret_as_s32(ua));
116
b = v_cvt_f32(v_reinterpret_as_s32(ub));
117
}
118
119
static inline void vx_load_pair_as(const schar* ptr, v_float32& a, v_float32& b)
120
{
121
v_int32 ia, ib;
122
v_expand(vx_load_expand(ptr), ia, ib);
123
a = v_cvt_f32(ia);
124
b = v_cvt_f32(ib);
125
}
126
127
static inline void vx_load_pair_as(const ushort* ptr, v_float32& a, v_float32& b)
128
{
129
v_uint32 ua, ub;
130
v_expand(vx_load(ptr), ua, ub);
131
a = v_cvt_f32(v_reinterpret_as_s32(ua));
132
b = v_cvt_f32(v_reinterpret_as_s32(ub));
133
}
134
135
static inline void vx_load_pair_as(const short* ptr, v_float32& a, v_float32& b)
136
{
137
v_int32 ia, ib;
138
v_expand(vx_load(ptr), ia, ib);
139
a = v_cvt_f32(ia);
140
b = v_cvt_f32(ib);
141
}
142
143
static inline void vx_load_pair_as(const int* ptr, v_float32& a, v_float32& b)
144
{
145
v_int32 ia = vx_load(ptr), ib = vx_load(ptr + v_int32::nlanes);
146
a = v_cvt_f32(ia);
147
b = v_cvt_f32(ib);
148
}
149
150
static inline void vx_load_pair_as(const float* ptr, v_float32& a, v_float32& b)
151
{ a = vx_load(ptr); b = vx_load(ptr + v_float32::nlanes); }
152
153
static inline void vx_load_pair_as(const float16_t* ptr, v_float32& a, v_float32& b)
154
{
155
a = vx_load_expand(ptr);
156
b = vx_load_expand(ptr + v_float32::nlanes);
157
}
158
159
static inline void v_store_pair_as(uchar* ptr, const v_uint16& a, const v_uint16& b)
160
{
161
v_store(ptr, v_pack(a, b));
162
}
163
164
static inline void v_store_pair_as(schar* ptr, const v_uint16& a, const v_uint16& b)
165
{
166
const v_uint8 maxval = vx_setall_u8((uchar)std::numeric_limits<schar>::max());
167
v_uint8 v = v_pack(a, b);
168
v_store(ptr, v_reinterpret_as_s8(v_min(v, maxval)));
169
}
170
171
static inline void v_store_pair_as(ushort* ptr, const v_uint16& a, const v_uint16& b)
172
{ v_store(ptr, a); v_store(ptr + v_uint16::nlanes, b); }
173
174
static inline void v_store_pair_as(uchar* ptr, const v_int16& a, const v_int16& b)
175
{ v_store(ptr, v_pack_u(a, b)); }
176
177
static inline void v_store_pair_as(schar* ptr, const v_int16& a, const v_int16& b)
178
{ v_store(ptr, v_pack(a, b)); }
179
180
static inline void v_store_pair_as(short* ptr, const v_int16& a, const v_int16& b)
181
{ v_store(ptr, a); v_store(ptr + v_int16::nlanes, b); }
182
183
static inline void v_store_pair_as(uchar* ptr, const v_int32& a, const v_int32& b)
184
{ v_pack_u_store(ptr, v_pack(a, b)); }
185
186
static inline void v_store_pair_as(schar* ptr, const v_int32& a, const v_int32& b)
187
{ v_pack_store(ptr, v_pack(a, b)); }
188
189
static inline void v_store_pair_as(ushort* ptr, const v_int32& a, const v_int32& b)
190
{ v_store(ptr, v_pack_u(a, b)); }
191
192
static inline void v_store_pair_as(short* ptr, const v_int32& a, const v_int32& b)
193
{ v_store(ptr, v_pack(a, b)); }
194
195
static inline void v_store_pair_as(int* ptr, const v_int32& a, const v_int32& b)
196
{
197
v_store(ptr, a);
198
v_store(ptr + v_int32::nlanes, b);
199
}
200
201
static inline void v_store_pair_as(uchar* ptr, const v_float32& a, const v_float32& b)
202
{ v_pack_u_store(ptr, v_pack(v_round(a), v_round(b))); }
203
204
static inline void v_store_pair_as(schar* ptr, const v_float32& a, const v_float32& b)
205
{ v_pack_store(ptr, v_pack(v_round(a), v_round(b))); }
206
207
static inline void v_store_pair_as(ushort* ptr, const v_float32& a, const v_float32& b)
208
{ v_store(ptr, v_pack_u(v_round(a), v_round(b))); }
209
210
static inline void v_store_pair_as(short* ptr, const v_float32& a, const v_float32& b)
211
{ v_store(ptr, v_pack(v_round(a), v_round(b))); }
212
213
static inline void v_store_pair_as(int* ptr, const v_float32& a, const v_float32& b)
214
{
215
v_int32 ia = v_round(a), ib = v_round(b);
216
v_store(ptr, ia);
217
v_store(ptr + v_int32::nlanes, ib);
218
}
219
220
static inline void v_store_pair_as(float* ptr, const v_float32& a, const v_float32& b)
221
{ v_store(ptr, a); v_store(ptr + v_float32::nlanes, b); }
222
223
#if CV_SIMD_64F
224
225
static inline void vx_load_as(const double* ptr, v_float32& a)
226
{
227
v_float64 v0 = vx_load(ptr), v1 = vx_load(ptr + v_float64::nlanes);
228
a = v_cvt_f32(v0, v1);
229
}
230
231
static inline void vx_load_pair_as(const double* ptr, v_int32& a, v_int32& b)
232
{
233
v_float64 v0 = vx_load(ptr), v1 = vx_load(ptr + v_float64::nlanes);
234
v_float64 v2 = vx_load(ptr + v_float64::nlanes*2), v3 = vx_load(ptr + v_float64::nlanes*3);
235
v_int32 iv0 = v_round(v0), iv1 = v_round(v1);
236
v_int32 iv2 = v_round(v2), iv3 = v_round(v3);
237
a = v_combine_low(iv0, iv1);
238
b = v_combine_low(iv2, iv3);
239
}
240
241
static inline void vx_load_pair_as(const double* ptr, v_float32& a, v_float32& b)
242
{
243
v_float64 v0 = vx_load(ptr), v1 = vx_load(ptr + v_float64::nlanes);
244
v_float64 v2 = vx_load(ptr + v_float64::nlanes*2), v3 = vx_load(ptr + v_float64::nlanes*3);
245
a = v_cvt_f32(v0, v1);
246
b = v_cvt_f32(v2, v3);
247
}
248
249
static inline void vx_load_pair_as(const uchar* ptr, v_float64& a, v_float64& b)
250
{
251
v_int32 v0 = v_reinterpret_as_s32(vx_load_expand_q(ptr));
252
a = v_cvt_f64(v0);
253
b = v_cvt_f64_high(v0);
254
}
255
256
static inline void vx_load_pair_as(const schar* ptr, v_float64& a, v_float64& b)
257
{
258
v_int32 v0 = vx_load_expand_q(ptr);
259
a = v_cvt_f64(v0);
260
b = v_cvt_f64_high(v0);
261
}
262
263
static inline void vx_load_pair_as(const ushort* ptr, v_float64& a, v_float64& b)
264
{
265
v_int32 v0 = v_reinterpret_as_s32(vx_load_expand(ptr));
266
a = v_cvt_f64(v0);
267
b = v_cvt_f64_high(v0);
268
}
269
270
static inline void vx_load_pair_as(const short* ptr, v_float64& a, v_float64& b)
271
{
272
v_int32 v0 = vx_load_expand(ptr);
273
a = v_cvt_f64(v0);
274
b = v_cvt_f64_high(v0);
275
}
276
277
static inline void vx_load_pair_as(const int* ptr, v_float64& a, v_float64& b)
278
{
279
v_int32 v0 = vx_load(ptr);
280
a = v_cvt_f64(v0);
281
b = v_cvt_f64_high(v0);
282
}
283
284
static inline void vx_load_pair_as(const float* ptr, v_float64& a, v_float64& b)
285
{
286
v_float32 v0 = vx_load(ptr);
287
a = v_cvt_f64(v0);
288
b = v_cvt_f64_high(v0);
289
}
290
291
static inline void vx_load_pair_as(const double* ptr, v_float64& a, v_float64& b)
292
{
293
a = vx_load(ptr);
294
b = vx_load(ptr + v_float64::nlanes);
295
}
296
297
static inline void vx_load_pair_as(const float16_t* ptr, v_float64& a, v_float64& b)
298
{
299
v_float32 v0 = vx_load_expand(ptr);
300
a = v_cvt_f64(v0);
301
b = v_cvt_f64_high(v0);
302
}
303
304
static inline void v_store_as(double* ptr, const v_float32& a)
305
{
306
v_float64 fa0 = v_cvt_f64(a), fa1 = v_cvt_f64_high(a);
307
v_store(ptr, fa0);
308
v_store(ptr + v_float64::nlanes, fa1);
309
}
310
311
static inline void v_store_pair_as(double* ptr, const v_int32& a, const v_int32& b)
312
{
313
v_float64 fa0 = v_cvt_f64(a), fa1 = v_cvt_f64_high(a);
314
v_float64 fb0 = v_cvt_f64(b), fb1 = v_cvt_f64_high(b);
315
316
v_store(ptr, fa0);
317
v_store(ptr + v_float64::nlanes, fa1);
318
v_store(ptr + v_float64::nlanes*2, fb0);
319
v_store(ptr + v_float64::nlanes*3, fb1);
320
}
321
322
static inline void v_store_pair_as(double* ptr, const v_float32& a, const v_float32& b)
323
{
324
v_float64 fa0 = v_cvt_f64(a), fa1 = v_cvt_f64_high(a);
325
v_float64 fb0 = v_cvt_f64(b), fb1 = v_cvt_f64_high(b);
326
327
v_store(ptr, fa0);
328
v_store(ptr + v_float64::nlanes, fa1);
329
v_store(ptr + v_float64::nlanes*2, fb0);
330
v_store(ptr + v_float64::nlanes*3, fb1);
331
}
332
333
static inline void v_store_pair_as(double* ptr, const v_float64& a, const v_float64& b)
334
{
335
v_store(ptr, a);
336
v_store(ptr + v_float64::nlanes, b);
337
}
338
339
static inline void v_store_pair_as(int* ptr, const v_float64& a, const v_float64& b)
340
{
341
v_int32 ia = v_round(a), ib = v_round(b);
342
v_store(ptr, v_combine_low(ia, ib));
343
}
344
345
static inline void v_store_pair_as(float* ptr, const v_float64& a, const v_float64& b)
346
{
347
v_float32 v = v_cvt_f32(a, b);
348
v_store(ptr, v);
349
}
350
351
static inline void v_store_pair_as(float16_t* ptr, const v_float64& a, const v_float64& b)
352
{
353
v_float32 v = v_cvt_f32(a, b);
354
v_pack_store(ptr, v);
355
}
356
357
#else
358
359
static inline void vx_load_as(const double* ptr, v_float32& a)
360
{
361
const int VECSZ = v_float32::nlanes;
362
float buf[VECSZ*2];
363
364
for( int i = 0; i < VECSZ; i++ )
365
buf[i] = saturate_cast<float>(ptr[i]);
366
a = vx_load(buf);
367
}
368
369
template<typename _Tdvec>
370
static inline void vx_load_pair_as(const double* ptr, _Tdvec& a, _Tdvec& b)
371
{
372
const int VECSZ = _Tdvec::nlanes;
373
typename _Tdvec::lane_type buf[VECSZ*2];
374
375
for( int i = 0; i < VECSZ*2; i++ )
376
buf[i] = saturate_cast<typename _Tdvec::lane_type>(ptr[i]);
377
a = vx_load(buf);
378
b = vx_load(buf + VECSZ);
379
}
380
381
static inline void v_store_as(double* ptr, const v_float32& a)
382
{
383
const int VECSZ = v_float32::nlanes;
384
float buf[VECSZ];
385
386
v_store(buf, a);
387
for( int i = 0; i < VECSZ; i++ )
388
ptr[i] = (double)buf[i];
389
}
390
391
template<typename _Tsvec>
392
static inline void v_store_pair_as(double* ptr, const _Tsvec& a, const _Tsvec& b)
393
{
394
const int VECSZ = _Tsvec::nlanes;
395
typename _Tsvec::lane_type buf[VECSZ*2];
396
397
v_store(buf, a); v_store(buf + VECSZ, b);
398
for( int i = 0; i < VECSZ*2; i++ )
399
ptr[i] = (double)buf[i];
400
}
401
402
#endif /////////// CV_SIMD_64F
403
404
#endif /////////// CV_SIMD
405
406
}
407
408
#endif // SRC_CONVERT_HPP
409
410