1/*******************************************************************************
2* Copyright 2016 Intel Corporation.
3*
4*
5* This software and the related documents are Intel copyrighted materials, and your use of them is governed by
6* the express license under which they were provided to you ('License'). Unless the License provides otherwise,
7* you may not use, modify, copy, publish, distribute, disclose or transmit this software or the related
8* documents without Intel's prior written permission.
9* This software and the related documents are provided as is, with no express or implied warranties, other than
10* those that are expressly stated in the License.
11*******************************************************************************/
12
13#if !defined( __IPP_IWPP_CORE__ )
14#define __IPP_IWPP_CORE__
15
16#include "iw/iw_core.h"
17
18#include <string>
19
20// IW++ interface configuration switches
21#ifndef IW_ENABLE_EXCEPTIONS
22#define IW_ENABLE_EXCEPTIONS 1 // IW++ can return all errors by exceptions or by classic return codes.
23 // Note that some errors cannot be returned without exceptions and may be lost.
24#endif
25
26
27#if IW_ENABLE_EXCEPTIONS == 0
28#define OWN_ERROR_THROW(IPP_STATUS) return (IPP_STATUS);
29#define OWN_ERROR_THROW_ONLY(IPP_STATUS) (void)(IPP_STATUS);
30#else
31#define OWN_ERROR_THROW(IPP_STATUS) throw IwException(IPP_STATUS);
32#define OWN_ERROR_THROW_ONLY(IPP_STATUS) OWN_ERROR_THROW(IPP_STATUS);
33#endif
34#define OWN_ERROR_CHECK_THROW_ONLY(IPP_STATUS)\
35{\
36 if((IPP_STATUS) < 0)\
37 {\
38 OWN_ERROR_THROW_ONLY(IPP_STATUS)\
39 }\
40}
41#define OWN_ERROR_CHECK(IPP_STATUS)\
42{\
43 if((IPP_STATUS) < 0)\
44 {\
45 OWN_ERROR_THROW(IPP_STATUS)\
46 }\
47}
48
49// Common IW++ API declaration macro
50#define IW_DECL_CPP(RET_TYPE) IW_INLINE RET_TYPE IPP_STDCALL
51
52// Base constructors set for auxiliary parameters
53#define IW_BASE_PARAMS_CONSTRUCTORS(NAME, FUN)\
54 NAME(const ::NAME *pParams)\
55 {\
56 if(pParams)\
57 *((::NAME*)this) = *pParams;\
58 else\
59 FUN(this);\
60 }\
61 NAME(const ::NAME &params)\
62 {\
63 *((::NAME*)this) = params;\
64 }\
65 NAME(const ipp::IwDefault &)\
66 {\
67 FUN(this);\
68 }
69
70namespace ipp
71{
72
73/* /////////////////////////////////////////////////////////////////////////////
74// Base IW++ definitions
75///////////////////////////////////////////////////////////////////////////// */
76
77using ::IppStatus;
78using ::IwSize;
79
80using ::Ipp8u;
81using ::Ipp16u;
82using ::Ipp32u;
83using ::Ipp8s;
84using ::Ipp16s;
85using ::Ipp32s;
86using ::Ipp32f;
87using ::Ipp64s;
88using ::Ipp64u;
89using ::Ipp64f;
90using ::Ipp16f;
91
92using ::IppDataType;
93using ::ipp1u;
94using ::ipp8u;
95using ::ipp8uc;
96using ::ipp8s;
97using ::ipp8sc;
98using ::ipp16u;
99using ::ipp16uc;
100using ::ipp16s;
101using ::ipp16sc;
102using ::ipp32u;
103using ::ipp32uc;
104using ::ipp32s;
105using ::ipp32sc;
106using ::ipp32f;
107using ::ipp32fc;
108using ::ipp64u;
109using ::ipp64uc;
110using ::ipp64s;
111using ::ipp64sc;
112using ::ipp64f;
113using ::ipp64fc;
114
115// Class to initialize default objects
116class IwDefault
117{
118public:
119 IwDefault() {}
120};
121
122#if IW_ENABLE_EXCEPTIONS
123// Stores an error code value for an exception thrown by the function
124class IwException
125{
126public:
127 // Constructor with status assignment
128 IwException(
129 IppStatus status // IppStatus value
130 )
131 {
132 m_status = status;
133 m_string = iwGetStatusString(status: m_status);
134 }
135
136 // Default destructor
137 ~IwException() {}
138
139 // IwException to IppStatus cast operator
140 inline operator IppStatus() const { return m_status;}
141
142 IppStatus m_status; // Stored IppStatus value
143 const char *m_string; // Stored status string
144};
145#endif
146
147// This class sets Intel IPP optimizations for the current region and restores previous optimizations at the region end
148class IwSetCpuFeaturesRegion
149{
150public:
151 // Default constructor. Saves current enabled CPU features.
152 IwSetCpuFeaturesRegion()
153 {
154 m_stored = ::ippGetEnabledCpuFeatures();
155 }
156
157 // Saves current enabled CPU features and sets new features mask.
158 IwSetCpuFeaturesRegion(Ipp64u featuresMask)
159 {
160 m_stored = ::ippGetEnabledCpuFeatures();
161 Set(featuresMask);
162 }
163
164 // Sets new features mask for the region.
165 IppStatus Set(Ipp64u featuresMask)
166 {
167 return ::ippSetCpuFeatures(cpuFeatures: featuresMask);
168 }
169
170 // Default destructor. Restores saved features mask.
171 ~IwSetCpuFeaturesRegion()
172 {
173 ::ippSetCpuFeatures(cpuFeatures: m_stored);
174 }
175
176private:
177 Ipp64u m_stored;
178};
179
180// Stores values for an array for array type casting
181template<typename DST>
182class IwValue
183{
184public:
185 // Default constructor. Sets array to zero
186 IwValue()
187 {
188 SetValue<Ipp8u>(0);
189 }
190
191 // Uniform template-based constructor. Sets channels to one value
192 template<typename SRC>
193 IwValue(SRC valUniform)
194 {
195 SetValue<SRC>(valUniform);
196 }
197
198 // 3 channels template-based constructor. Sets channels to individual values
199 template<typename SRC>
200 IwValue(SRC valC1, SRC valC2, SRC valC3)
201 {
202 SetValue<SRC>(valC1, valC2, valC3);
203 }
204
205 // 4 channels template-based constructor. Sets channels to individual values
206 template<typename SRC>
207 IwValue(SRC valC1, SRC valC2, SRC valC3, SRC valC4)
208 {
209 SetValue<SRC>(valC1, valC2, valC3, valC4);
210 }
211
212 // Buffer template-based constructor. Sets values from a buffer of specific type
213 template<typename SRC>
214 IwValue(SRC *pBuffer, int channels)
215 {
216 SetValue(pBuffer, channels);
217 }
218
219 // Buffer parameter-based constructor. Sets values from a buffer of specific type
220 IwValue(void *pBuffer, IppDataType type, int channels)
221 {
222 SetValue(pBuffer, type, channels);
223 }
224
225 // Uniform template setter. Sets channels to one value
226 template<typename SRC>
227 void SetValue(SRC valUniform)
228 {
229 m_values = 1;
230 m_val[0] = m_val[1] = m_val[2] = m_val[3] = (DST)valUniform;
231 }
232
233 // 3 channels template setter. Sets channels to individual values
234 template<typename SRC>
235 void SetValue(SRC valC1, SRC valC2, SRC valC3)
236 {
237 m_values = 3;
238 m_val[0] = (DST)valC1, m_val[1] = (DST)valC2, m_val[2] = (DST)valC3;
239 }
240
241 // 4 channels template setter. Sets channels to individual values
242 template<typename SRC>
243 void SetValue(SRC valC1, SRC valC2, SRC valC3, SRC valC4)
244 {
245 m_values = 4;
246 m_val[0] = (DST)valC1, m_val[1] = (DST)valC2, m_val[2] = (DST)valC3, m_val[3] = (DST)valC4;
247 }
248
249 // Buffer template-based setter. Sets values from a buffer of specific type
250 template<typename SRC>
251 void SetValue(SRC *pBuffer, int channels)
252 {
253 if(!pBuffer)
254 OWN_ERROR_THROW_ONLY(ippStsNullPtrErr);
255 if(channels > 4 || channels < 1)
256 OWN_ERROR_THROW_ONLY(ippStsNumChannelsErr);
257 m_values = channels;
258
259 for(int i = 0; i < channels; i++)
260 m_val[i] = (DST)(pBuffer[i]);
261 }
262
263 // Buffer parameter-based setter. Sets values from a buffer of specific type
264 void SetValue(void *pBuffer, IppDataType type, int channels)
265 {
266 switch(type)
267 {
268 case ipp8u:
269 SetValue((Ipp8u*)pBuffer, channels); break;
270 case ipp8s:
271 SetValue((Ipp8s*)pBuffer, channels); break;
272 case ipp16u:
273 SetValue((Ipp16u*)pBuffer, channels); break;
274 case ipp16s:
275 SetValue((Ipp16s*)pBuffer, channels); break;
276 case ipp32u:
277 SetValue((Ipp32u*)pBuffer, channels); break;
278 case ipp32s:
279 SetValue((Ipp32s*)pBuffer, channels); break;
280 case ipp32f:
281 SetValue((Ipp32f*)pBuffer, channels); break;
282 case ipp64u:
283 SetValue((Ipp64u*)pBuffer, channels); break;
284 case ipp64s:
285 SetValue((Ipp64s*)pBuffer, channels); break;
286 case ipp64f:
287 SetValue((Ipp64f*)pBuffer, channels); break;
288 default:
289 OWN_ERROR_THROW_ONLY(ippStsDataTypeErr);
290 }
291 }
292
293 // Returns number of initialized values
294 int ValuesNum() const { return m_values; }
295
296 // IwValue to Ipp64f cast operator
297 inline operator DST () const { return ((DST*)m_val)[0];}
298
299 // IwValue to Ipp64f* cast operator
300 inline operator DST* () const { return (DST*)m_val;}
301
302 // IwValue to const Ipp64f* cast operator
303 inline operator const DST* () const { return (const DST*)m_val;}
304
305 // Array subscript operator
306 inline DST& operator[](int channel) { return (channel>=m_values)?m_val[m_values-1]:m_val[channel]; }
307 inline const DST& operator[](int channel) const { return (channel>=m_values)?m_val[m_values-1]:m_val[channel]; }
308
309 // Compares values
310 bool operator==(const IwValue& rhs) const
311 {
312 if((*this)[0] == rhs[0] && (*this)[1] == rhs[1] && (*this)[2] == rhs[2] && (*this)[3] == rhs[3])
313 return true;
314 else
315 return false;
316 }
317 bool operator!=(const IwValue& rhs) const
318 {
319 return !(*this==rhs);
320 }
321
322private:
323 int m_values; // Number of initialized values
324 DST m_val[4]; // reserve 4 channels
325};
326
327typedef IwValue<Ipp64f> IwValueFloat;
328typedef IwValue<Ipp32s> IwValueInt;
329
330// Convert IppDataType to actual size in bytes
331// Returns:
332// Size of IppDataType in bytes
333IW_DECL_CPP(int) iwTypeToSize(
334 IppDataType type // Data type
335)
336{
337 return ::iwTypeToSize(type);
338}
339
340// Returns 1 if data type is of float type and 0 otherwise
341// Returns:
342// Absolute value
343IW_DECL_CPP(int) iwTypeIsFloat(
344 IppDataType type // Data type
345)
346{
347 return ::iwTypeIsFloat(type);
348}
349
350// Returns minimum possible value for specified data type
351// Returns:
352// Minimum value
353IW_DECL_CPP(double) iwTypeGetMin(
354 IppDataType type // Data type for min value
355)
356{
357 return ::iwTypeGetMin(type);
358}
359
360// Returns maximum possible value for specified data type
361// Returns:
362// Maximum value
363IW_DECL_CPP(double) iwTypeGetMax(
364 IppDataType type // Data type for max value
365)
366{
367 return ::iwTypeGetMax(type);
368}
369
370// Returns values range for specified data type
371// Returns:
372// Range value
373IW_DECL_CPP(double) iwTypeGetRange(
374 IppDataType type // Data type for range value
375)
376{
377 return ::iwTypeGetRange(type);
378}
379
380// Cast double value to input type with rounding and saturation
381// Returns:
382// Rounded and saturated value
383IW_DECL_CPP(double) iwValueSaturate(
384 double val, // Input value
385 IppDataType dstType // Data type for saturation range
386)
387{
388 return ::iwValueSaturate(val, dstType);
389}
390
391// Converts relative value in range of [0,1] to the absolute value according to specified type
392// Returns:
393// Absolute value
394IW_DECL_CPP(double) iwValueRelToAbs(
395 double val, // Relative value. From 0 to 1
396 IppDataType type // Data type for the absolute range
397)
398{
399 return ::iwValueRelToAbs(val, type);
400}
401
402/* /////////////////////////////////////////////////////////////////////////////
403// IW with Threading Layer control
404///////////////////////////////////////////////////////////////////////////// */
405
406// This function sets number of threads for IW functions with parallel execution support
407IW_DECL_CPP(void) iwSetThreadsNum(
408 int threads // Number of threads to use
409)
410{
411 ::iwSetThreadsNum(threads);
412}
413
414// This function returns number of threads used by IW functions with parallel execution support
415// Returns:
416// Number of threads or 0 if compiled without internal threading support
417IW_DECL_CPP(int) iwGetThreadsNum()
418{
419 return ::iwGetThreadsNum();
420}
421
422// This function returns initial number of threads used by IW functions with parallel execution support
423// Returns:
424// Default number of threads or 0 if compiled without internal threading support
425IW_DECL_CPP(int) iwGetThreadsNumDefault()
426{
427 return ::iwGetThreadsNumDefault();
428}
429
430
431/* /////////////////////////////////////////////////////////////////////////////
432// IwTls - TLS data storage interface
433///////////////////////////////////////////////////////////////////////////// */
434
435// Template-based TLS abstraction layer class.
436// This is an extension of C IwTls structure with automatic objects destruction
437template<class TYPE>
438class IwTls: private ::IwTls
439{
440public:
441 // Default constructor
442 IwTls()
443 {
444 IppStatus status = ::iwTls_Init(pTls: this, destructor: (IwTlsDestructor)(IwTls::TypeDestructor));
445 OWN_ERROR_CHECK_THROW_ONLY(status);
446 }
447
448 // Default destructor
449 ~IwTls()
450 {
451 ::iwTls_Release(pTls: this);
452 }
453
454 // Allocates object for current thread and returns pointer to it
455 TYPE* Create()
456 {
457 TYPE *pData = new TYPE;
458 if(!pData)
459 return NULL;
460 IppStatus status = ::iwTls_Set(pTls: this, pData);
461 if(status < 0)
462 {
463 delete pData;
464 OWN_ERROR_CHECK_THROW_ONLY(status);
465 return NULL;
466 }
467 return pData;
468 }
469
470 // Releases object for current thread
471 void Release()
472 {
473 IppStatus status = ::iwTls_Set(pTls: this, NULL);
474 if(status < 0)
475 OWN_ERROR_CHECK_THROW_ONLY(status);
476 }
477
478 // Releases objects for all threads
479 void ReleaseAll()
480 {
481 IppStatus status = ::iwTls_ReleaseData(pTls: this);
482 if(status < 0)
483 OWN_ERROR_CHECK_THROW_ONLY(status);
484 }
485
486 // Returns pointer to object for current thread
487 TYPE* Get() const
488 {
489 return (TYPE*)::iwTls_Get(pTls: this);
490 }
491
492private:
493 // Object destructor
494 static void IPP_STDCALL TypeDestructor(void *pData)
495 {
496 if(pData)
497 delete ((TYPE*)pData);
498 }
499};
500
501/* /////////////////////////////////////////////////////////////////////////////
502// IW version info
503///////////////////////////////////////////////////////////////////////////// */
504
505class IppVersion: private ::IwVersion
506{
507public:
508 IppVersion()
509 {
510 ::iwGetLibVersion(pVersion: this);
511 if(!this->m_pIppVersion)
512 OWN_ERROR_THROW_ONLY(ippStsNullPtrErr);
513 }
514
515 int getMajor()
516 {
517 return this->m_pIppVersion->major;
518 }
519
520 int getMinor()
521 {
522 return this->m_pIppVersion->minor;
523 }
524
525 int getUpdate()
526 {
527 return this->m_pIppVersion->majorBuild;
528 }
529
530 int getRevision()
531 {
532 return this->m_pIppVersion->build;
533 }
534
535 std::string getString()
536 {
537 return this->m_pIppVersion->Version;
538 }
539
540 std::string getLibraryName()
541 {
542 return this->m_pIppVersion->Name;
543 }
544
545 std::string getTargetCpu()
546 {
547 return this->m_pIppVersion->targetCpu;
548 }
549
550 std::string getBuildDate()
551 {
552 return this->m_pIppVersion->BuildDate;
553 }
554
555 std::string getInfoString()
556 {
557 return getLibraryName() + " " + getString() + ", " + getBuildDate();
558 }
559};
560
561class IwVersion: private ::IwVersion
562{
563public:
564 IwVersion()
565 {
566 ::iwGetLibVersion(pVersion: this);
567 if(!this->m_pIppVersion)
568 OWN_ERROR_THROW_ONLY(ippStsNullPtrErr);
569 }
570
571 int getMajor()
572 {
573 return this->m_major;
574 }
575
576 int getMinor()
577 {
578 return this->m_minor;
579 }
580
581 int getUpdate()
582 {
583 return this->m_update;
584 }
585
586 std::string getString()
587 {
588 return this->m_versionStr;
589 }
590
591 std::string getInfoString()
592 {
593 if(isReleaseBuild())
594 return getString() + ", release build";
595 else
596 return getString() + ", user build";
597 }
598
599 bool isReleaseBuild()
600 {
601 return !this->m_bUserBuild;
602 }
603};
604
605}
606
607#endif
608

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of opencv/build/3rdparty/ippicv/ippicv_lnx/iw/include/iw++/iw_core.hpp