Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / ie_preprocess_gapi_kernels_impl.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #ifdef HAVE_SSE
8   #define MANUAL_SIMD 1  // 1=call manually vectored code, 0=don't
9 #else
10   #define MANUAL_SIMD 0
11 #endif
12
13 #if MANUAL_SIMD
14   #define USE_CVKL 1     // 1=reuse CVKL code for Resize, 0=don't
15 #else
16   #define USE_CVKL 0
17 #endif
18
19 #include <climits>
20 #include <cstdint>
21
22 namespace InferenceEngine {
23 namespace gapi {
24 namespace kernels {
25
26 template<typename DST, typename SRC> static inline DST saturate_cast(SRC x);
27 template<> inline short saturate_cast(int x) { return (std::min)(SHRT_MAX, (std::max)(SHRT_MIN, x)); }
28 template<> inline short saturate_cast(float x) { return saturate_cast<short>(static_cast<int>(std::rint(x))); }
29 template<> inline float saturate_cast(float x) { return x; }
30 template<> inline short saturate_cast(short x) { return x; }
31 template<> inline uint16_t saturate_cast(int x) { return (std::min)(USHRT_MAX, (std::max)(0, x)); }
32
33 //------------------------------------------------------------------------------
34
35 constexpr static const int ONE = 1 << 15;
36
37 inline static uint8_t calc(short alpha0, uint8_t src0, short alpha1, uint8_t src1) {
38     constexpr static const int half = 1 << 14;
39     return (src0 * alpha0 + src1 * alpha1 + half) >> 15;
40 }
41
42 inline static float calc(float alpha0, float src0, float alpha1, float src1) {
43     return src0 * alpha0 + src1 * alpha1;
44 }
45
46 //------------------------------------------------------------------------------
47
48 // Variants:
49 // - F=float, I=int
50 // - F=short, I=short (e.g. F is Q1.7.8 encoded with short)
51 template<typename F, typename I>
52 struct MapperUnit {
53     F alpha0, alpha1;
54     I index0, index1;
55 };
56
57 //------------------------------------------------------------------------------
58
59 typedef uint16_t Q0_16;  // value [0..1)   with 16 fractional bits
60 typedef uint16_t Q8_8;   // value [0..255) with  8 fractional bits
61 typedef uint8_t  U8;     // value [0..255)
62
63 template<typename DST, typename SRC> static inline DST convert_cast(SRC x);
64 template<> inline uint8_t convert_cast(uint8_t x) { return x; }
65 template<> inline uint8_t convert_cast(float x) { return static_cast<uint8_t>(x); }
66 template<> inline float convert_cast(float  x) { return x; }
67 template<> inline float convert_cast(double x) { return static_cast<float>(x); }
68 template<> inline Q0_16 convert_cast(double x) {
69     int ix = static_cast<int>(std::rint(x * (1 << 16)));
70     return saturate_cast<Q0_16>(ix);
71 }
72 template<> inline Q8_8 convert_cast(uchar x) { return x << 8; }
73 template<> inline uchar convert_cast(Q8_8 x) { return x >> 8; }
74
75 template<typename DST, typename SRC> static inline DST checked_cast(SRC x) {
76     short dx = static_cast<DST>(x);
77     GAPI_Assert(x == dx);  // check
78     return dx;
79 }
80
81 static inline Q8_8 mulas(Q0_16 a, U8   s) { return static_cast<Q8_8>((a * s) >>  8); }
82 static inline Q8_8 mulaw(Q0_16 a, Q8_8 w) { return static_cast<Q8_8>((a * w) >> 16); }
83
84 static inline float mulas(float a, float s) { return a * s; }
85 static inline float mulaw(float a, float w) { return a * w; }
86
87 }  // namespace kernels
88 }  // namespace gapi
89 }  // namespace InferenceEngine