Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ShLib.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright (c) 2003 University of Waterloo Computer Graphics Laboratory
00004 // Project administrator: Michael D. McCool
00005 // Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa,
00006 //          Michael D. McCool
00007 // 
00008 // This software is provided 'as-is', without any express or implied
00009 // warranty. In no event will the authors be held liable for any damages
00010 // arising from the use of this software.
00011 // 
00012 // Permission is granted to anyone to use this software for any purpose,
00013 // including commercial applications, and to alter it and redistribute it
00014 // freely, subject to the following restrictions:
00015 // 
00016 // 1. The origin of this software must not be misrepresented; you must
00017 // not claim that you wrote the original software. If you use this
00018 // software in a product, an acknowledgment in the product documentation
00019 // would be appreciated but is not required.
00020 // 
00021 // 2. Altered source versions must be plainly marked as such, and must
00022 // not be misrepresented as being the original software.
00023 // 
00024 // 3. This notice may not be removed or altered from any source
00025 // distribution.
00027 #ifndef SHLIB_HPP
00028 #define SHLIB_HPP
00029 
00030 #include <cmath>
00031 #include <cassert>
00032 #include "ShVariable.hpp"
00033 #include "ShGeneric.hpp"
00034 #include "ShAttrib.hpp"
00035 #include "ShTypeInfo.hpp"
00036 
00040 #define CT1T2 typename ShCommonType<T1, T2>::type
00041 #define CT1T2T3 typename ShCommonType3<T1, T2, T3>::type
00042 #define CT1T2T3T4 typename ShCommonType4<T1, T2, T3, T4>::type
00043 
00044 #define SH_SHLIB_CONST_SCALAR_OP(operation) \
00045 template<typename T> \
00046 ShGeneric<1, T> \
00047 operation(const ShGeneric<1, T>& left, double right) \
00048 { \
00049   return operation(left, ShAttrib<1, SH_CONST, T>(right)); \
00050 } \
00051 template<typename T> \
00052 ShGeneric<1, T> \
00053 operation(double left, const ShGeneric<1, T>& right) \
00054 { \
00055   return operation(ShAttrib<1, SH_CONST, T>(left), right); \
00056 } 
00057 
00058 #define SH_SHLIB_CONST_SCALAR_OP_DECL(operation) \
00059 template<typename T> \
00060 ShGeneric<1, T> \
00061 operation(const ShGeneric<1, T>& left, double right); \
00062  \
00063 template<typename T> \
00064 ShGeneric<1, T> \
00065 operation(double left, const ShGeneric<1, T>& right);
00066 
00067 #define SH_SHLIB_CONST_N_OP_RETSIZE_LEFT(operation, retsize) \
00068 template<int N, typename T> \
00069 ShGeneric<retsize, T> \
00070 operation(const ShGeneric<N, T>& left, double right) \
00071 { \
00072   return operation(left, ShAttrib<1, SH_CONST, T>(right)); \
00073 } 
00074 
00075 #define SH_SHLIB_CONST_N_OP_RETSIZE_LEFT_DECL(operation, retsize) \
00076 template<int N, typename T> \
00077 ShGeneric<retsize, T> \
00078 operation(const ShGeneric<N, T>& left, double right);
00079 
00080 #define SH_SHLIB_CONST_N_OP_RETSIZE_RIGHT(operation, retsize) \
00081 template<int N, typename T> \
00082 ShGeneric<retsize, T> \
00083 operation(double left, const ShGeneric<N, T>& right) \
00084 { \
00085   return operation(ShAttrib<1, SH_CONST, T>(left), right); \
00086 } 
00087 
00088 #define SH_SHLIB_CONST_N_OP_RETSIZE_RIGHT_DECL(operation, retsize) \
00089 template<int N, typename T> \
00090 ShGeneric<retsize, T> \
00091 operation(double left, const ShGeneric<N, T>& right); 
00092 
00093 #define SH_SHLIB_CONST_N_OP_LEFT(operation) \
00094   SH_SHLIB_CONST_N_OP_RETSIZE_LEFT(operation, N);
00095 
00096 #define SH_SHLIB_CONST_N_OP_RIGHT(operation) \
00097   SH_SHLIB_CONST_N_OP_RETSIZE_RIGHT(operation, N);
00098 
00099 #define SH_SHLIB_CONST_N_OP_BOTH(operation) \
00100 SH_SHLIB_CONST_N_OP_LEFT(operation); \
00101 SH_SHLIB_CONST_N_OP_RIGHT(operation);
00102 
00103 #define SH_SHLIB_CONST_N_OP_RETSIZE_BOTH(operation, retsize) \
00104 SH_SHLIB_CONST_N_OP_RETSIZE_LEFT(operation, retsize); \
00105 SH_SHLIB_CONST_N_OP_RETSIZE_RIGHT(operation, retsize);
00106 
00107 #define SH_SHLIB_CONST_N_OP_LEFT_DECL(operation) \
00108   SH_SHLIB_CONST_N_OP_RETSIZE_LEFT_DECL(operation, N);
00109 
00110 #define SH_SHLIB_CONST_N_OP_RIGHT_DECL(operation) \
00111   SH_SHLIB_CONST_N_OP_RETSIZE_RIGHT_DECL(operation, N);
00112 
00113 #define SH_SHLIB_CONST_N_OP_BOTH_DECL(operation) \
00114 SH_SHLIB_CONST_N_OP_LEFT_DECL(operation); \
00115 SH_SHLIB_CONST_N_OP_RIGHT_DECL(operation);
00116 
00117 #define SH_SHLIB_CONST_N_OP_RETSIZE_BOTH_DECL(operation, retsize) \
00118 SH_SHLIB_CONST_N_OP_RETSIZE_LEFT_DECL(operation, retsize); \
00119 SH_SHLIB_CONST_N_OP_RETSIZE_RIGHT_DECL(operation, retsize);
00120 
00121 //@todo type fix scalar types here.  Should be arbitrary templated types instead of
00122 // just T , but that casues overload problems 
00123 #define SH_SHLIB_CONST_TRINARY_OP_011_RETSIZE(operation, retsize) \
00124 template<int N, typename T> \
00125 ShGeneric<retsize, T> \
00126 operation(const ShGeneric<N, T>& a, double b, double c) \
00127 { \
00128   return operation(a, ShAttrib<1, SH_CONST, T>(b), ShAttrib<1, SH_CONST, T>(c)); \
00129 } 
00130 
00131 #define SH_SHLIB_CONST_TRINARY_OP_011_RETSIZE_DECL(operation, retsize) \
00132 template<int N, typename T> \
00133 ShGeneric<retsize, T> \
00134 operation(const ShGeneric<N, T>& a, double b, double c); 
00135 
00136 #define SH_SHLIB_CONST_TRINARY_OP_011(operation) \
00137 SH_SHLIB_CONST_TRINARY_OP_011_RETSIZE(operation, N);
00138 
00139 #define SH_SHLIB_CONST_TRINARY_OP_011_DECL(operation) \
00140 SH_SHLIB_CONST_TRINARY_OP_011_RETSIZE_DECL(operation, N);
00141 
00142 #include "ShLibArith.hpp"
00143 #include "ShLibBoolean.hpp"
00144 #include "ShLibClamp.hpp"
00145 #include "ShLibGeometry.hpp"
00146 #include "ShLibMatrix.hpp"
00147 #include "ShLibMisc.hpp"
00148 #include "ShLibTrig.hpp"
00149 #include "ShLibDeriv.hpp"
00150 #include "ShLibInterval.hpp"
00151 
00152 #undef CT1T2 
00153 #undef CT1T2T3 
00154 #undef CT1T2T3T4 
00155 
00156 // Semantic stuff
00157 
00158 #define SH_SHLIB_UNARY_RETTYPE_OPERATION(libtype, libop, librettype, libretsize) \
00159 template<int N, ShBindingType K1, typename T, bool S1> \
00160 librettype<libretsize, SH_TEMP, T, false> \
00161 libop(const libtype<N, K1, T, S1>& var) \
00162 { \
00163   ShGeneric<libretsize, T> t = libop(static_cast< ShGeneric<N, T> >(var)); \
00164   return librettype<libretsize, SH_TEMP, T, false>(t.node(), t.swizzle(), t.neg()); \
00165 }
00166 
00167 #define SH_SHLIB_UNARY_OPERATION(libtype, libop, libretsize) \
00168   SH_SHLIB_UNARY_RETTYPE_OPERATION(libtype, libop, libtype, libretsize)
00169 
00170 #define SH_SHLIB_BINARY_RETTYPE_OPERATION(libtype, libop, librettype, libretsize) \
00171 template<int N, ShBindingType K1, ShBindingType K2, typename T1, \
00172   typename T2, bool S1, bool S2> \
00173 librettype<libretsize, SH_TEMP, typename ShCommonType<T1, T2>::type, false> \
00174 libop(const libtype<N, K1, T1, S1>& left, const libtype<N, K2, T2, S2>& right) \
00175 { \
00176   ShGeneric<libretsize, typename ShCommonType<T1, T2>::type> t = libop(\
00177             static_cast< ShGeneric<N, T1> >(left), \
00178             static_cast< ShGeneric<N, T2> >(right)); \
00179   return librettype<libretsize, SH_TEMP, typename ShCommonType<T1, T2>::type, false>(t.node(), \
00180       t.swizzle(), t.neg()); \
00181 }
00182 
00183 #define SH_SHLIB_BINARY_OPERATION(libtype, libop, libretsize) \
00184   SH_SHLIB_BINARY_RETTYPE_OPERATION(libtype, libop, libtype, libretsize)
00185 
00186 #define SH_SHLIB_UNEQ_BINARY_RETTYPE_OPERATION(libtype, libop, librettype, libretsize) \
00187 template<int N, int M, ShBindingType K1, ShBindingType K2, typename T1, \
00188   typename T2, bool S1, bool S2> \
00189 librettype<libretsize, SH_TEMP, typename ShCommonType<T1, T2>::type, false> \
00190 libop(const libtype<N, K1, T1, S1>& left, const libtype<M, K2, T2, S2>& right) \
00191 { \
00192   ShGeneric<libretsize, typename ShCommonType<T1, T2>::type> t = libop(\
00193       static_cast< ShGeneric<N, T1> >(left), \
00194       static_cast< ShGeneric<M, T2> >(right)); \
00195   return librettype<libretsize, SH_TEMP, typename ShCommonType<T1, T2>::type, false>(\
00196       t.node(), t.swizzle(), t.neg()); \
00197 }
00198 
00199 #define SH_SHLIB_UNEQ_BINARY_OPERATION(libtype, libop, libretsize) \
00200   SH_SHLIB_UNEQ_BINARY_RETTYPE_OPERATION(libtype, libop, libtype, libretsize)
00201 
00202 #define SH_SHLIB_LEFT_SCALAR_RETTYPE_OPERATION(libtype, libop, librettype) \
00203 template<int M, ShBindingType K1, ShBindingType K2, typename T1, \
00204   typename T2, bool S1, bool S2> \
00205 librettype<M, SH_TEMP, typename ShCommonType<T1, T2>::type, false> \
00206 libop(const libtype<1, K2, T1, S2>& left, const libtype<M, K1, T2, S1>& right) \
00207 { \
00208   ShGeneric<M, typename ShCommonType<T1, T2>::type> t = libop( \
00209       static_cast< ShGeneric<1, T1> >(left), \
00210       static_cast< ShGeneric<M, T2> >(right)); \
00211   return librettype<M, SH_TEMP, typename ShCommonType<T1, T2>::type, false>(\
00212       t.node(), t.swizzle(), t.neg()); \
00213 }
00214 
00215 #define SH_SHLIB_LEFT_SCALAR_OPERATION(libtype, libop) \
00216   SH_SHLIB_LEFT_SCALAR_RETTYPE_OPERATION(libtype, libop, libtype)
00217 
00218 #define SH_SHLIB_LEFT_MATRIX_RETTYPE_OPERATION(libtype, libop, librettype, libretsize) \
00219 template<int M, int N, ShBindingType K1, ShBindingType K2, typename T1, \
00220   typename T2, bool S1> \
00221 librettype<libretsize, SH_TEMP, typename ShCommonType<T1, T2>::type, false> \
00222 libop(const ShMatrix<M, N, K1, T1>& a, const libtype<N, K2, T2, S1>& b) \
00223 { \
00224   ShGeneric<libretsize, typename ShCommonType<T1, T2>::type> t = libop(a, \
00225                                        static_cast< ShGeneric<N, T2> >(b)); \
00226   return librettype<libretsize, K1, typename ShCommonType<T1, T2>::type, S1>(t.node(), t.swizzle(), t.neg()); \
00227 }
00228 
00229 #define SH_SHLIB_LEFT_MATRIX_OPERATION(libtype, libop, libretsize) \
00230   SH_SHLIB_LEFT_MATRIX_RETTYPE_OPERATION(libtype, libop, libtype, libretsize)
00231 
00232 // All the scalar constant stuff
00233 
00234 #define SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(libtype, libop, librettype, libretsize) \
00235 template<ShBindingType K, typename T, bool S> \
00236 librettype<libretsize, SH_TEMP, T, false> \
00237 libop(const libtype<1, K, T, S>& left, double right) \
00238 { \
00239   return libop(left, ShAttrib<1, SH_CONST, T>(right)); \
00240 } \
00241 template<ShBindingType K, typename T, bool S> \
00242 librettype<libretsize, SH_TEMP, T, false> \
00243 libop(double left, const libtype<1, K, T, S>& right) \
00244 { \
00245   return libop(ShAttrib<1, SH_CONST, T>(left), right); \
00246 } 
00247 
00248 #define SH_SHLIB_SPECIAL_CONST_SCALAR_OP(libtype, libop) \
00249   SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(libtype, libop, libtype, 1)
00250 
00251 #define SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_LEFT(libtype, libop, librettype, libretsize) \
00252 template<int N, ShBindingType K, typename T, bool S> \
00253 librettype<libretsize, SH_TEMP, T, false> \
00254 libop(const libtype<N, K, T, S>& left, double right) \
00255 { \
00256   return libop(left, ShAttrib<1, SH_CONST, T>(right)); \
00257 } 
00258 
00259 #define SH_SHLIB_SPECIAL_CONST_N_OP_LEFT(libtype, libop) \
00260   SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_LEFT(libtype, libop, libtype, N)
00261 
00262 #define SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_RIGHT(libtype, libop, librettype, libretsize) \
00263 template<int N, ShBindingType K, typename T, bool S> \
00264 librettype<libretsize, SH_TEMP, T, false> \
00265 libop(double left, const libtype<N, K, T, S>& right) \
00266 { \
00267   return libop(ShAttrib<1, SH_CONST, T>(left), right); \
00268 } 
00269 
00270 #define SH_SHLIB_SPECIAL_CONST_N_OP_RIGHT(libtype, libop) \
00271   SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_RIGHT(libtype, libop, libtype, N)
00272 
00273 #define SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_BOTH(libtype, operation, librettype, libretsize) \
00274 SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_LEFT(libtype, operation, librettype, libretsize); \
00275 SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_RIGHT(libtype, operation, librettype, libretsize);
00276 
00277 #define SH_SHLIB_SPECIAL_CONST_N_OP_BOTH(libtype, operation) \
00278 SH_SHLIB_SPECIAL_CONST_N_OP_LEFT(libtype, operation); \
00279 SH_SHLIB_SPECIAL_CONST_N_OP_RIGHT(libtype, operation);
00280 
00281 // Standard stuff
00282 
00283 #define SH_SHLIB_USUAL_OPERATIONS(type) \
00284   SH_SHLIB_USUAL_OPERATIONS_RETTYPE(type, type)
00285 
00286 #define SH_SHLIB_USUAL_OPERATIONS_RETTYPE(type, rettype) \
00287   SH_SHLIB_USUAL_NON_UNIT_OPS_RETTYPE(type, rettype)\
00288   SH_SHLIB_UNARY_RETTYPE_OPERATION(type, normalize, rettype, N);       \
00289   SH_SHLIB_UNARY_RETTYPE_OPERATION(type, abs, rettype, N);           
00290 
00291 #define SH_SHLIB_USUAL_NON_UNIT_OPS_RETTYPE(type, rettype) \
00292 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, operator+, rettype, N);      \
00293 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator+, rettype, 1);  \
00294                                                         \
00295 SH_SHLIB_UNEQ_BINARY_RETTYPE_OPERATION(type, operator*, rettype, N); \
00296 SH_SHLIB_LEFT_SCALAR_RETTYPE_OPERATION(type, operator*, rettype);    \
00297 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator*, rettype, 1);  \
00298 SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_BOTH(type, operator*, rettype, N);  \
00299                                                         \
00300 SH_SHLIB_UNEQ_BINARY_RETTYPE_OPERATION(type, operator/, rettype, N); \
00301 SH_SHLIB_LEFT_SCALAR_RETTYPE_OPERATION(type, operator/, rettype);    \
00302 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator/, rettype, 1);  \
00303 SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_BOTH(type, operator/, rettype, N);  \
00304                                                         \
00305 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, pow, rettype, N);            \
00306 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, pow, rettype, 1);        \
00307 SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_RIGHT(type, pow, rettype, N);       \
00308                                                         \
00309 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, operator<, rettype, N);      \
00310 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator<, rettype, 1);  \
00311 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, operator<=, rettype, N);     \
00312 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator<=, rettype, 1); \
00313 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, operator>, rettype, N);      \
00314 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator>, rettype, 1);  \
00315 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, operator>=, rettype, N);     \
00316 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator>=, rettype, 1); \
00317 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, operator==, rettype, N);     \
00318 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator==, rettype, 1); \
00319 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, operator!=, rettype, N);     \
00320 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator!=, rettype, 1); \
00321                                                         \
00322 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, acos, rettype, N);            \
00323 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, asin, rettype, N);            \
00324 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, cos, rettype, N);             \
00325 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, dot, rettype, 1);           \
00326 SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_BOTH(type, dot, rettype, 1);           \
00327 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, mod, rettype, N);           \
00328 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, mod, rettype, 1);       \
00329                                                         \
00330 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, frac, rettype, N);            \
00331 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, sin, rettype, N);             \
00332 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, sqrt, rettype, N);            \
00333                                                         \
00334 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, min, rettype, N);            \
00335 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, min, rettype, 1);        \
00336 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, pos, rettype, N);            \
00337 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, pos, rettype, 1);        \
00338 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, max, rettype, N);            \
00339 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, max, rettype, 1);
00340 
00341 // Points have different subtraction, so we don't include them in our "usuals"
00342 #define SH_SHLIB_USUAL_SUBTRACT(type) \
00343 SH_SHLIB_BINARY_OPERATION(type, operator-, N);      \
00344 SH_SHLIB_SPECIAL_CONST_SCALAR_OP(type, operator-);  \
00345 
00346 
00347 #endif

Generated on Mon Jan 24 18:36:32 2005 for Sh by  doxygen 1.4.1