include/opencv2/gpu/device/vec_traits.hpp
Go to the documentation of this file.
00001 /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                           License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
00014 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
00015 // Third party copyrights are property of their respective owners.
00016 //
00017 // Redistribution and use in source and binary forms, with or without modification,
00018 // are permitted provided that the following conditions are met:
00019 //
00020 //   * Redistribution's of source code must retain the above copyright notice,
00021 //     this list of conditions and the following disclaimer.
00022 //
00023 //   * Redistribution's in binary form must reproduce the above copyright notice,
00024 //     this list of conditions and the following disclaimer in the documentation
00025 //     and/or other materials provided with the distribution.
00026 //
00027 //   * The name of the copyright holders may not be used to endorse or promote products
00028 //     derived from this software without specific prior written permission.
00029 //
00030 // This software is provided by the copyright holders and contributors "as is" and
00031 // any express or implied warranties, including, but not limited to, the implied
00032 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00033 // In no event shall the Intel Corporation or contributors be liable for any direct,
00034 // indirect, incidental, special, exemplary, or consequential damages
00035 // (including, but not limited to, procurement of substitute goods or services;
00036 // loss of use, data, or profits; or business interruption) however caused
00037 // and on any theory of liability, whether in contract, strict liability,
00038 // or tort (including negligence or otherwise) arising in any way out of
00039 // the use of this software, even if advised of the possibility of such damage.
00040 //
00041 //M*/
00042 
00043 #ifndef __OPENCV_GPU_VEC_TRAITS_HPP__
00044 #define __OPENCV_GPU_VEC_TRAITS_HPP__
00045 
00046 #include "common.hpp"
00047 
00048 namespace cv { namespace gpu { namespace device
00049 {
00050     template<typename T, int N> struct TypeVec;
00051 
00052     struct __align__(8) uchar8
00053     {
00054         uchar a0, a1, a2, a3, a4, a5, a6, a7;
00055     };
00056     static __host__ __device__ __forceinline__ uchar8 make_uchar8(uchar a0, uchar a1, uchar a2, uchar a3, uchar a4, uchar a5, uchar a6, uchar a7)
00057     {
00058         uchar8 val = {a0, a1, a2, a3, a4, a5, a6, a7};
00059         return val;
00060     }
00061     struct __align__(8) char8
00062     {
00063         schar a0, a1, a2, a3, a4, a5, a6, a7;
00064     };
00065     static __host__ __device__ __forceinline__ char8 make_char8(schar a0, schar a1, schar a2, schar a3, schar a4, schar a5, schar a6, schar a7)
00066     {
00067         char8 val = {a0, a1, a2, a3, a4, a5, a6, a7};
00068         return val;
00069     }
00070     struct __align__(16) ushort8
00071     {
00072         ushort a0, a1, a2, a3, a4, a5, a6, a7;
00073     };
00074     static __host__ __device__ __forceinline__ ushort8 make_ushort8(ushort a0, ushort a1, ushort a2, ushort a3, ushort a4, ushort a5, ushort a6, ushort a7)
00075     {
00076         ushort8 val = {a0, a1, a2, a3, a4, a5, a6, a7};
00077         return val;
00078     }
00079     struct __align__(16) short8
00080     {
00081         short a0, a1, a2, a3, a4, a5, a6, a7;
00082     };
00083     static __host__ __device__ __forceinline__ short8 make_short8(short a0, short a1, short a2, short a3, short a4, short a5, short a6, short a7)
00084     {
00085         short8 val = {a0, a1, a2, a3, a4, a5, a6, a7};
00086         return val;
00087     }
00088     struct __align__(32) uint8
00089     {
00090         uint a0, a1, a2, a3, a4, a5, a6, a7;
00091     };
00092     static __host__ __device__ __forceinline__ uint8 make_uint8(uint a0, uint a1, uint a2, uint a3, uint a4, uint a5, uint a6, uint a7)
00093     {
00094         uint8 val = {a0, a1, a2, a3, a4, a5, a6, a7};
00095         return val;
00096     }
00097     struct __align__(32) int8
00098     {
00099         int a0, a1, a2, a3, a4, a5, a6, a7;
00100     };
00101     static __host__ __device__ __forceinline__ int8 make_int8(int a0, int a1, int a2, int a3, int a4, int a5, int a6, int a7)
00102     {
00103         int8 val = {a0, a1, a2, a3, a4, a5, a6, a7};
00104         return val;
00105     }
00106     struct __align__(32) float8
00107     {
00108         float a0, a1, a2, a3, a4, a5, a6, a7;
00109     };
00110     static __host__ __device__ __forceinline__ float8 make_float8(float a0, float a1, float a2, float a3, float a4, float a5, float a6, float a7)
00111     {
00112         float8 val = {a0, a1, a2, a3, a4, a5, a6, a7};
00113         return val;
00114     }
00115     struct double8
00116     {
00117         double a0, a1, a2, a3, a4, a5, a6, a7;
00118     };
00119     static __host__ __device__ __forceinline__ double8 make_double8(double a0, double a1, double a2, double a3, double a4, double a5, double a6, double a7)
00120     {
00121         double8 val = {a0, a1, a2, a3, a4, a5, a6, a7};
00122         return val;
00123     }
00124 
00125 #define OPENCV_GPU_IMPLEMENT_TYPE_VEC(type) \
00126     template<> struct TypeVec<type, 1> { typedef type vec_type; }; \
00127     template<> struct TypeVec<type ## 1, 1> { typedef type ## 1 vec_type; }; \
00128     template<> struct TypeVec<type, 2> { typedef type ## 2 vec_type; }; \
00129     template<> struct TypeVec<type ## 2, 2> { typedef type ## 2 vec_type; }; \
00130     template<> struct TypeVec<type, 3> { typedef type ## 3 vec_type; }; \
00131     template<> struct TypeVec<type ## 3, 3> { typedef type ## 3 vec_type; }; \
00132     template<> struct TypeVec<type, 4> { typedef type ## 4 vec_type; }; \
00133     template<> struct TypeVec<type ## 4, 4> { typedef type ## 4 vec_type; }; \
00134     template<> struct TypeVec<type, 8> { typedef type ## 8 vec_type; }; \
00135     template<> struct TypeVec<type ## 8, 8> { typedef type ## 8 vec_type; };
00136 
00137     OPENCV_GPU_IMPLEMENT_TYPE_VEC(uchar)
00138     OPENCV_GPU_IMPLEMENT_TYPE_VEC(char)
00139     OPENCV_GPU_IMPLEMENT_TYPE_VEC(ushort)
00140     OPENCV_GPU_IMPLEMENT_TYPE_VEC(short)
00141     OPENCV_GPU_IMPLEMENT_TYPE_VEC(int)
00142     OPENCV_GPU_IMPLEMENT_TYPE_VEC(uint)
00143     OPENCV_GPU_IMPLEMENT_TYPE_VEC(float)
00144     OPENCV_GPU_IMPLEMENT_TYPE_VEC(double)
00145 
00146     #undef OPENCV_GPU_IMPLEMENT_TYPE_VEC
00147 
00148     template<> struct TypeVec<schar, 1> { typedef schar vec_type; };
00149     template<> struct TypeVec<schar, 2> { typedef char2 vec_type; };
00150     template<> struct TypeVec<schar, 3> { typedef char3 vec_type; };
00151     template<> struct TypeVec<schar, 4> { typedef char4 vec_type; };
00152     template<> struct TypeVec<schar, 8> { typedef char8 vec_type; };
00153 
00154     template<> struct TypeVec<bool, 1> { typedef uchar vec_type; };
00155     template<> struct TypeVec<bool, 2> { typedef uchar2 vec_type; };
00156     template<> struct TypeVec<bool, 3> { typedef uchar3 vec_type; };
00157     template<> struct TypeVec<bool, 4> { typedef uchar4 vec_type; };
00158     template<> struct TypeVec<bool, 8> { typedef uchar8 vec_type; };
00159 
00160     template<typename T> struct VecTraits;
00161 
00162 #define OPENCV_GPU_IMPLEMENT_VEC_TRAITS(type) \
00163     template<> struct VecTraits<type> \
00164     { \
00165         typedef type elem_type; \
00166         enum {cn=1}; \
00167         static __device__ __host__ __forceinline__ type all(type v) {return v;} \
00168         static __device__ __host__ __forceinline__ type make(type x) {return x;} \
00169         static __device__ __host__ __forceinline__ type make(const type* v) {return *v;} \
00170     }; \
00171     template<> struct VecTraits<type ## 1> \
00172     { \
00173         typedef type elem_type; \
00174         enum {cn=1}; \
00175         static __device__ __host__ __forceinline__ type ## 1 all(type v) {return make_ ## type ## 1(v);} \
00176         static __device__ __host__ __forceinline__ type ## 1 make(type x) {return make_ ## type ## 1(x);} \
00177         static __device__ __host__ __forceinline__ type ## 1 make(const type* v) {return make_ ## type ## 1(*v);} \
00178     }; \
00179     template<> struct VecTraits<type ## 2> \
00180     { \
00181         typedef type elem_type; \
00182         enum {cn=2}; \
00183         static __device__ __host__ __forceinline__ type ## 2 all(type v) {return make_ ## type ## 2(v, v);} \
00184         static __device__ __host__ __forceinline__ type ## 2 make(type x, type y) {return make_ ## type ## 2(x, y);} \
00185         static __device__ __host__ __forceinline__ type ## 2 make(const type* v) {return make_ ## type ## 2(v[0], v[1]);} \
00186     }; \
00187     template<> struct VecTraits<type ## 3> \
00188     { \
00189         typedef type elem_type; \
00190         enum {cn=3}; \
00191         static __device__ __host__ __forceinline__ type ## 3 all(type v) {return make_ ## type ## 3(v, v, v);} \
00192         static __device__ __host__ __forceinline__ type ## 3 make(type x, type y, type z) {return make_ ## type ## 3(x, y, z);} \
00193         static __device__ __host__ __forceinline__ type ## 3 make(const type* v) {return make_ ## type ## 3(v[0], v[1], v[2]);} \
00194     }; \
00195     template<> struct VecTraits<type ## 4> \
00196     { \
00197         typedef type elem_type; \
00198         enum {cn=4}; \
00199         static __device__ __host__ __forceinline__ type ## 4 all(type v) {return make_ ## type ## 4(v, v, v, v);} \
00200         static __device__ __host__ __forceinline__ type ## 4 make(type x, type y, type z, type w) {return make_ ## type ## 4(x, y, z, w);} \
00201         static __device__ __host__ __forceinline__ type ## 4 make(const type* v) {return make_ ## type ## 4(v[0], v[1], v[2], v[3]);} \
00202     }; \
00203     template<> struct VecTraits<type ## 8> \
00204     { \
00205         typedef type elem_type; \
00206         enum {cn=8}; \
00207         static __device__ __host__ __forceinline__ type ## 8 all(type v) {return make_ ## type ## 8(v, v, v, v, v, v, v, v);} \
00208         static __device__ __host__ __forceinline__ type ## 8 make(type a0, type a1, type a2, type a3, type a4, type a5, type a6, type a7) {return make_ ## type ## 8(a0, a1, a2, a3, a4, a5, a6, a7);} \
00209         static __device__ __host__ __forceinline__ type ## 8 make(const type* v) {return make_ ## type ## 8(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);} \
00210     };
00211 
00212     OPENCV_GPU_IMPLEMENT_VEC_TRAITS(uchar)
00213     OPENCV_GPU_IMPLEMENT_VEC_TRAITS(ushort)
00214     OPENCV_GPU_IMPLEMENT_VEC_TRAITS(short)
00215     OPENCV_GPU_IMPLEMENT_VEC_TRAITS(int)
00216     OPENCV_GPU_IMPLEMENT_VEC_TRAITS(uint)
00217     OPENCV_GPU_IMPLEMENT_VEC_TRAITS(float)
00218     OPENCV_GPU_IMPLEMENT_VEC_TRAITS(double)
00219 
00220     #undef OPENCV_GPU_IMPLEMENT_VEC_TRAITS
00221 
00222     template<> struct VecTraits<char>
00223     {
00224         typedef char elem_type;
00225         enum {cn=1};
00226         static __device__ __host__ __forceinline__ char all(char v) {return v;}
00227         static __device__ __host__ __forceinline__ char make(char x) {return x;}
00228         static __device__ __host__ __forceinline__ char make(const char* x) {return *x;}
00229     };
00230     template<> struct VecTraits<schar>
00231     {
00232         typedef schar elem_type;
00233         enum {cn=1};
00234         static __device__ __host__ __forceinline__ schar all(schar v) {return v;}
00235         static __device__ __host__ __forceinline__ schar make(schar x) {return x;}
00236         static __device__ __host__ __forceinline__ schar make(const schar* x) {return *x;}
00237     };
00238     template<> struct VecTraits<char1>
00239     {
00240         typedef schar elem_type;
00241         enum {cn=1};
00242         static __device__ __host__ __forceinline__ char1 all(schar v) {return make_char1(v);}
00243         static __device__ __host__ __forceinline__ char1 make(schar x) {return make_char1(x);}
00244         static __device__ __host__ __forceinline__ char1 make(const schar* v) {return make_char1(v[0]);}
00245     };
00246     template<> struct VecTraits<char2>
00247     {
00248         typedef schar elem_type;
00249         enum {cn=2};
00250         static __device__ __host__ __forceinline__ char2 all(schar v) {return make_char2(v, v);}
00251         static __device__ __host__ __forceinline__ char2 make(schar x, schar y) {return make_char2(x, y);}
00252         static __device__ __host__ __forceinline__ char2 make(const schar* v) {return make_char2(v[0], v[1]);}
00253     };
00254     template<> struct VecTraits<char3>
00255     {
00256         typedef schar elem_type;
00257         enum {cn=3};
00258         static __device__ __host__ __forceinline__ char3 all(schar v) {return make_char3(v, v, v);}
00259         static __device__ __host__ __forceinline__ char3 make(schar x, schar y, schar z) {return make_char3(x, y, z);}
00260         static __device__ __host__ __forceinline__ char3 make(const schar* v) {return make_char3(v[0], v[1], v[2]);}
00261     };
00262     template<> struct VecTraits<char4>
00263     {
00264         typedef schar elem_type;
00265         enum {cn=4};
00266         static __device__ __host__ __forceinline__ char4 all(schar v) {return make_char4(v, v, v, v);}
00267         static __device__ __host__ __forceinline__ char4 make(schar x, schar y, schar z, schar w) {return make_char4(x, y, z, w);}
00268         static __device__ __host__ __forceinline__ char4 make(const schar* v) {return make_char4(v[0], v[1], v[2], v[3]);}
00269     };
00270     template<> struct VecTraits<char8>
00271     {
00272         typedef schar elem_type;
00273         enum {cn=8};
00274         static __device__ __host__ __forceinline__ char8 all(schar v) {return make_char8(v, v, v, v, v, v, v, v);}
00275         static __device__ __host__ __forceinline__ char8 make(schar a0, schar a1, schar a2, schar a3, schar a4, schar a5, schar a6, schar a7) {return make_char8(a0, a1, a2, a3, a4, a5, a6, a7);}
00276         static __device__ __host__ __forceinline__ char8 make(const schar* v) {return make_char8(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);}
00277     };
00278 }}} // namespace cv { namespace gpu { namespace device
00279 
00280 #endif // __OPENCV_GPU_VEC_TRAITS_HPP__