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

Generated on Wed Jun 15 18:12:39 2005 for Sh by  doxygen 1.4.3-20050530