1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
8 #define MANUAL_SIMD 1 // 1=call manually vectored code, 0=don't
14 #define USE_CVKL 1 // 1=reuse CVKL code for Resize, 0=don't
22 namespace InferenceEngine {
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)); }
33 //------------------------------------------------------------------------------
35 constexpr static const int ONE = 1 << 15;
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;
42 inline static float calc(float alpha0, float src0, float alpha1, float src1) {
43 return src0 * alpha0 + src1 * alpha1;
46 //------------------------------------------------------------------------------
50 // - F=short, I=short (e.g. F is Q1.7.8 encoded with short)
51 template<typename F, typename I>
57 //------------------------------------------------------------------------------
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)
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);
72 template<> inline Q8_8 convert_cast(uchar x) { return x << 8; }
73 template<> inline uchar convert_cast(Q8_8 x) { return x >> 8; }
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
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); }
84 static inline float mulas(float a, float s) { return a * s; }
85 static inline float mulaw(float a, float w) { return a * w; }
87 } // namespace kernels
89 } // namespace InferenceEngine