IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit
SaturatedCastReg.h
Go to the documentation of this file.
1 // SaturatedCastReg.h:
3 // ------------
4 //
14 
15 #ifndef __IPSDKUTIL_INSTRUCTIONSET_DETAIL_STD_SATURATEDCASTREG_H__
16 #define __IPSDKUTIL_INSTRUCTIONSET_DETAIL_STD_SATURATEDCASTREG_H__
17 
23 
24 #include <boost/type_traits/is_same.hpp>
25 #include <boost/type_traits/is_signed.hpp>
26 
27 namespace ipsdk {
28 namespace simd {
29 namespace detail {
30 
33 
34 template <typename TIn, typename TOut>
36  typename boost::enable_if_c<
37  boost::is_same<TIn, TOut>::value
38  >::type
39 >
40 {
41  static IPSDK_FORCEINLINE
42  TOut
43  act(const TIn& in)
44  {
45  TOut out = static_cast<TOut>(in);
46  return static_cast<TOut>(in);
47  }
48 
49  static IPSDK_FORCEINLINE
50  void
51  act(const TIn& in, TOut& out)
52  {
53  out = static_cast<TOut>(in);
54  }
55 };
56 
57 template <typename TIn, typename TOut>
59  typename boost::enable_if_c<
60  boost::is_integral<TIn>::value &&
61  boost::is_integral<TOut>::value &&
62  boost::is_signed<TIn>::value==boost::is_signed<TOut>::value &&
63  sizeof(TIn)<sizeof(TOut)
64  >::type
65 >
66 {
67  static IPSDK_FORCEINLINE
68  TOut
69  act(const TIn& in)
70  {
71  return static_cast<TOut>(in);
72  }
73 
74  static IPSDK_FORCEINLINE
75  void
76  act(const TIn& in, TOut& out)
77  {
78  out = static_cast<TOut>(in);
79  }
80 };
81 
82 template <typename TIn, typename TOut>
84  typename boost::enable_if_c<
85  boost::is_integral<TIn>::value &&
86  boost::is_integral<TOut>::value &&
87  boost::is_signed<TIn>::value &&
88  !boost::is_signed<TOut>::value &&
89  sizeof(TOut)>=sizeof(TIn)
90  >::type
91 >
92 {
93  static IPSDK_FORCEINLINE
94  TOut
95  act(const TIn& in)
96  {
97  const TIn clampedIn = in >= 0 ? in : 0;
98  return static_cast<TOut>(clampedIn);
99  }
100 
101  static IPSDK_FORCEINLINE
102  void
103  act(const TIn& in, TOut& out)
104  {
105  const TIn clampedIn = in >= 0 ? in : 0;
106  out = static_cast<TOut>(clampedIn);
107  }
108 };
109 
110 template <typename TIn, typename TOut>
111 struct SaturatedCastReg<eInstructionSet::eIS_Standard, TIn, TOut,
112  typename boost::enable_if_c<
113  boost::is_integral<TIn>::value &&
114  boost::is_integral<TOut>::value &&
115  !boost::is_signed<TIn>::value &&
116  boost::is_signed<TOut>::value &&
117  sizeof(TOut)<=sizeof(TIn)
118  >::type
119 >
120 {
121  static IPSDK_FORCEINLINE
122  TOut
123  act(const TIn& in)
124  {
125  const TIn nMax = static_cast<TIn>(NumericLimits<TOut>::max());
126  const TIn clampedIn = (in >= nMax ? nMax : in);
127  return static_cast<TOut>(clampedIn);
128  }
129 
130  static IPSDK_FORCEINLINE
131  void
132  act(const TIn& in, TOut& out)
133  {
134  const TIn nMax = static_cast<TIn>(NumericLimits<TOut>::max());
135  const TIn clampedIn = (in >= nMax ? nMax : in);
136  out = static_cast<TOut>(clampedIn);
137  }
138 };
139 
140 template <typename TIn, typename TOut>
141 struct SaturatedCastReg<eInstructionSet::eIS_Standard, TIn, TOut,
142  typename boost::enable_if_c<
143  boost::is_integral<TIn>::value &&
144  boost::is_integral<TOut>::value &&
145  !boost::is_signed<TIn>::value &&
146  boost::is_signed<TOut>::value &&
147  (sizeof(TOut) > sizeof(TIn))
148  >::type
149 >
150 {
151  static IPSDK_FORCEINLINE
152  TOut
153  act(const TIn& in)
154  {
155  return static_cast<TOut>(in);
156  }
157 
158  static IPSDK_FORCEINLINE
159  void
160  act(const TIn& in, TOut& out)
161  {
162  out = static_cast<TOut>(in);
163  }
164 };
165 
166 template <typename TIn, typename TOut>
167 struct SaturatedCastReg<eInstructionSet::eIS_Standard, TIn, TOut,
168  typename boost::enable_if_c<
169  boost::is_integral<TIn>::value &&
170  boost::is_integral<TOut>::value &&
171  !boost::is_signed<TIn>::value &&
172  !boost::is_signed<TOut>::value &&
173  sizeof(TOut)<sizeof(TIn)
174  >::type
175 >
176 {
177  static IPSDK_FORCEINLINE
178  TOut
179  act(const TIn& in)
180  {
181  const TIn nMax = static_cast<TIn>(NumericLimits<TOut>::max());
182  const TIn clampedIn = (in >= nMax ? nMax : in);
183  return static_cast<TOut>(clampedIn);
184  }
185 
186  static IPSDK_FORCEINLINE
187  void
188  act(const TIn& in, TOut& out)
189  {
190  const TIn nMax = static_cast<TIn>(NumericLimits<TOut>::max());
191  const TIn clampedIn = (in >= nMax ? nMax : in);
192  out = static_cast<TOut>(clampedIn);
193  }
194 };
195 
196 template <typename TIn, typename TOut>
197 struct SaturatedCastReg<eInstructionSet::eIS_Standard, TIn, TOut,
198  typename boost::enable_if_c<
199  boost::is_integral<TIn>::value &&
200  boost::is_integral<TOut>::value &&
201  boost::is_signed<TIn>::value &&
202  sizeof(TOut)<sizeof(TIn)
203  >::type
204 >
205 {
206  static IPSDK_FORCEINLINE
207  TOut
208  act(const TIn& in)
209  {
210  const TIn nMin = static_cast<TIn>(NumericLimits<TOut>::min());
211  const TIn nMax = static_cast<TIn>(NumericLimits<TOut>::max());
212  TIn clampedIn = (in > nMax ? nMax : in);
213  clampedIn = (clampedIn < nMin ? nMin : clampedIn);
214  return static_cast<TOut>(clampedIn);
215  }
216 
217  static IPSDK_FORCEINLINE
218  void
219  act(const TIn& in, TOut& out)
220  {
221  const TIn nMin = static_cast<TIn>(NumericLimits<TOut>::min());
222  const TIn nMax = static_cast<TIn>(NumericLimits<TOut>::max());
223  TIn clampedIn = (in > nMax ? nMax : in);
224  clampedIn = (clampedIn < nMin ? nMin : clampedIn);
225  out = static_cast<TOut>(clampedIn);
226  }
227 };
228 
229 template <typename TIn>
230 struct SaturatedCastReg<eInstructionSet::eIS_Standard, TIn, ipReal32,
231  typename boost::enable_if_c<boost::is_integral<TIn>::value>::type>
232 {
233  static IPSDK_FORCEINLINE
234  ipReal32
235  act(const TIn& in)
236  {
237  ipReal32 out;
238  return static_cast<ipReal32>(in);
239  }
240 
241  static IPSDK_FORCEINLINE
242  void
243  act(const TIn& in, ipReal32& out)
244  {
245  out = static_cast<ipReal32>(in);
246  }
247 };
248 
249 template <typename TOut>
250 struct SaturatedCastReg<eInstructionSet::eIS_Standard, ipReal32, TOut,
251  typename boost::enable_if_c<boost::is_integral<TOut>::value>::type>
252 {
253  static IPSDK_FORCEINLINE
254  TOut
255  act(const ipReal32& in)
256  {
257  const ipReal64 fMin = static_cast<ipReal64>(NumericLimits<TOut>::min());
258  const ipReal64 fMax = static_cast<ipReal64>(NumericLimits<TOut>::max());
259  ipReal64 clampedIn = in;
260  clampedIn = (clampedIn > fMax ? fMax : clampedIn);
261  clampedIn = (clampedIn < fMin ? fMin : clampedIn);
262  return static_cast<TOut>(clampedIn);
263  }
264 
265  static IPSDK_FORCEINLINE
266  void
267  act(const ipReal32& in, TOut& out)
268  {
269  const ipReal64 fMin = static_cast<ipReal64>(NumericLimits<TOut>::min());
270  const ipReal64 fMax = static_cast<ipReal64>(NumericLimits<TOut>::max());
271  ipReal64 clampedIn = in;
272  clampedIn = (clampedIn > fMax ? fMax : clampedIn);
273  clampedIn = (clampedIn < fMin ? fMin : clampedIn);
274  out = static_cast<TOut>(clampedIn);
275  }
276 };
277 
278 template <typename TIn>
279 struct SaturatedCastReg<eInstructionSet::eIS_Standard, TIn, ipReal64,
280  typename boost::enable_if_c<
281  !boost::is_same<TIn, ipReal64>::value
282  >::type
283 >
284 {
285  static IPSDK_FORCEINLINE
286  ipReal64
287  act(const TIn& in)
288  {
289  return static_cast<ipReal64>(in);
290  }
291 
292  static IPSDK_FORCEINLINE
293  void
294  act(const TIn& in, ipReal64& out)
295  {
296  out = static_cast<ipReal64>(in);
297  }
298 };
299 
300 template <typename TOut>
301 struct SaturatedCastReg<eInstructionSet::eIS_Standard, ipReal64, TOut,
302  typename boost::enable_if_c<
303  !boost::is_same<ipReal64, TOut>::value
304  >::type
305 >
306 {
307  static IPSDK_FORCEINLINE
308  TOut
309  act(const ipReal64& in)
310  {
311  const ipReal64 fMin = static_cast<ipReal64>(NumericLimits<TOut>::min());
312  const ipReal64 fMax = static_cast<ipReal64>(NumericLimits<TOut>::max());
313  ipReal64 clampedIn = (in > fMax ? fMax : in);
314  clampedIn = (clampedIn < fMin ? fMin : clampedIn);
315  return static_cast<TOut>(clampedIn);
316  }
317 
318  static IPSDK_FORCEINLINE
319  void
320  act(const ipReal64& in, TOut& out)
321  {
322  const ipReal64 fMin = static_cast<ipReal64>(NumericLimits<TOut>::min());
323  const ipReal64 fMax = static_cast<ipReal64>(NumericLimits<TOut>::max());
324  ipReal64 clampedIn = (in > fMax ? fMax : in);
325  clampedIn = (clampedIn < fMin ? fMin : clampedIn);
326  out = static_cast<TOut>(clampedIn);
327  }
328 };
329 
332 
333 } // end of namespace detail
334 } // end of namespace simd
335 } // end of namespace ipsdk
336 
337 #endif // __IPSDKUTIL_INSTRUCTIONSET_DETAIL_STD_SATURATEDCASTREG_H__
Defines the IPSDK_FORCEINLINE.
Main namespace for IPSDK library.
Definition: AlgorithmFunctionEfficiency.h:22
Definition: SaturatedCastReg.h:33
double ipReal64
Base types definition.
Definition: BaseTypes.h:57
eInstructionSet
Enumerate for processor instruction set description.
Definition: InstructionSetTypes.h:31
Definition of import/export macro for library.
compiler optimisations only
Definition: InstructionSetTypes.h:34
float ipReal32
Base types definition.
Definition: BaseTypes.h:56