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 library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
00018 // MA  02110-1301, USA
00020 #ifndef SHLIB_HPP
00021 #define SHLIB_HPP
00022 
00023 #include <cmath>
00024 #include <cassert>
00025 #include "ShVariable.hpp"
00026 #include "ShGeneric.hpp"
00027 #include "ShAttrib.hpp"
00028 #include "ShTypeInfo.hpp"
00029 
00033 #define CT1T2 typename ShCommonType<T1, T2>::type
00034 #define CT1T2T3 typename ShCommonType3<T1, T2, T3>::type
00035 #define CT1T2T3T4 typename ShCommonType4<T1, T2, T3, T4>::type
00036 
00037 #define SH_SHLIB_CONST_SCALAR_OP(operation) \
00038 template<typename T> \
00039 ShGeneric<1, T> \
00040 operation(const ShGeneric<1, T>& left, double right) \
00041 { \
00042   return operation(left, ShAttrib<1, SH_CONST, T>(right)); \
00043 } \
00044 template<typename T> \
00045 ShGeneric<1, T> \
00046 operation(double left, const ShGeneric<1, T>& right) \
00047 { \
00048   return operation(ShAttrib<1, SH_CONST, T>(left), right); \
00049 } 
00050 
00051 #define SH_SHLIB_CONST_SCALAR_OP_DECL(operation) \
00052 template<typename T> \
00053 ShGeneric<1, T> \
00054 operation(const ShGeneric<1, T>& left, double right); \
00055  \
00056 template<typename T> \
00057 ShGeneric<1, T> \
00058 operation(double left, const ShGeneric<1, T>& right);
00059 
00060 #define SH_SHLIB_CONST_N_OP_RETSIZE_LEFT(operation, retsize) \
00061 template<int N, typename T> \
00062 ShGeneric<retsize, T> \
00063 operation(const ShGeneric<N, T>& left, double right) \
00064 { \
00065   return operation(left, ShAttrib<1, SH_CONST, T>(right)); \
00066 } 
00067 
00068 #define SH_SHLIB_CONST_N_OP_RETSIZE_LEFT_DECL(operation, retsize) \
00069 template<int N, typename T> \
00070 ShGeneric<retsize, T> \
00071 operation(const ShGeneric<N, T>& left, double right);
00072 
00073 #define SH_SHLIB_CONST_N_OP_RETSIZE_RIGHT(operation, retsize) \
00074 template<int N, typename T> \
00075 ShGeneric<retsize, T> \
00076 operation(double left, const ShGeneric<N, T>& right) \
00077 { \
00078   return operation(ShAttrib<1, SH_CONST, T>(left), right); \
00079 } 
00080 
00081 #define SH_SHLIB_CONST_N_OP_RETSIZE_RIGHT_DECL(operation, retsize) \
00082 template<int N, typename T> \
00083 ShGeneric<retsize, T> \
00084 operation(double left, const ShGeneric<N, T>& right); 
00085 
00086 #define SH_SHLIB_CONST_N_OP_LEFT(operation) \
00087   SH_SHLIB_CONST_N_OP_RETSIZE_LEFT(operation, N);
00088 
00089 #define SH_SHLIB_CONST_N_OP_RIGHT(operation) \
00090   SH_SHLIB_CONST_N_OP_RETSIZE_RIGHT(operation, N);
00091 
00092 #define SH_SHLIB_CONST_N_OP_BOTH(operation) \
00093 SH_SHLIB_CONST_N_OP_LEFT(operation); \
00094 SH_SHLIB_CONST_N_OP_RIGHT(operation);
00095 
00096 #define SH_SHLIB_CONST_N_OP_RETSIZE_BOTH(operation, retsize) \
00097 SH_SHLIB_CONST_N_OP_RETSIZE_LEFT(operation, retsize); \
00098 SH_SHLIB_CONST_N_OP_RETSIZE_RIGHT(operation, retsize);
00099 
00100 #define SH_SHLIB_CONST_N_OP_LEFT_DECL(operation) \
00101   SH_SHLIB_CONST_N_OP_RETSIZE_LEFT_DECL(operation, N);
00102 
00103 #define SH_SHLIB_CONST_N_OP_RIGHT_DECL(operation) \
00104   SH_SHLIB_CONST_N_OP_RETSIZE_RIGHT_DECL(operation, N);
00105 
00106 #define SH_SHLIB_CONST_N_OP_BOTH_DECL(operation) \
00107 SH_SHLIB_CONST_N_OP_LEFT_DECL(operation); \
00108 SH_SHLIB_CONST_N_OP_RIGHT_DECL(operation);
00109 
00110 #define SH_SHLIB_CONST_N_OP_RETSIZE_BOTH_DECL(operation, retsize) \
00111 SH_SHLIB_CONST_N_OP_RETSIZE_LEFT_DECL(operation, retsize); \
00112 SH_SHLIB_CONST_N_OP_RETSIZE_RIGHT_DECL(operation, retsize);
00113 
00114 //@todo type fix scalar types here.  Should be arbitrary templated types instead of
00115 // just T , but that casues overload problems 
00116 #define SH_SHLIB_CONST_TRINARY_OP_011_RETSIZE(operation, retsize) \
00117 template<int N, typename T> \
00118 ShGeneric<retsize, T> \
00119 operation(const ShGeneric<N, T>& a, double b, double c) \
00120 { \
00121   return operation(a, ShAttrib<1, SH_CONST, T>(b), ShAttrib<1, SH_CONST, T>(c)); \
00122 } 
00123 
00124 #define SH_SHLIB_CONST_TRINARY_OP_011_RETSIZE_DECL(operation, retsize) \
00125 template<int N, typename T> \
00126 ShGeneric<retsize, T> \
00127 operation(const ShGeneric<N, T>& a, double b, double c); 
00128 
00129 #define SH_SHLIB_CONST_TRINARY_OP_011(operation) \
00130 SH_SHLIB_CONST_TRINARY_OP_011_RETSIZE(operation, N);
00131 
00132 #define SH_SHLIB_CONST_TRINARY_OP_011_DECL(operation) \
00133 SH_SHLIB_CONST_TRINARY_OP_011_RETSIZE_DECL(operation, N);
00134 
00135 #include "ShLibArith.hpp"
00136 #include "ShLibBoolean.hpp"
00137 #include "ShLibClamp.hpp"
00138 #include "ShLibGeometry.hpp"
00139 #include "ShLibMatrix.hpp"
00140 #include "ShLibMisc.hpp"
00141 #include "ShLibTrig.hpp"
00142 #include "ShLibDeriv.hpp"
00143 #include "ShLibNoise.hpp"
00144 #include "ShLibWorley.hpp"
00145 // If you add anything to this list, make sure that ShLib.hpp is
00146 // included before the header guard since these libraries must always
00147 // be loaded by ShLib.hpp, no matter who includes them.  See
00148 // ShLibNoise.hpp for an example.
00149 
00150 #undef CT1T2 
00151 #undef CT1T2T3 
00152 #undef CT1T2T3T4 
00153 
00154 // Semantic stuff
00155 
00156 #define SH_SHLIB_UNARY_RETTYPE_OPERATION(libtype, libop, librettype, libretsize) \
00157 template<int N, ShBindingType K1, typename T, bool S1> \
00158 ShAttrib<libretsize, SH_TEMP, T, librettype, false> \
00159 libop(const ShAttrib<N, K1, T, libtype, S1>& var) \
00160 { \
00161   ShGeneric<libretsize, T> t = libop(static_cast< ShGeneric<N, T> >(var)); \
00162   return ShAttrib<libretsize, SH_TEMP, T, librettype, false>(t.node(), t.swizzle(), t.neg()); \
00163 }
00164 
00165 #define SH_SHLIB_UNARY_OPERATION(libtype, libop, libretsize) \
00166   SH_SHLIB_UNARY_RETTYPE_OPERATION(libtype, libop, libtype, libretsize)
00167 
00168 #define SH_SHLIB_BINARY_RETTYPE_OPERATION(libtype, libop, librettype, libretsize) \
00169 template<int N, ShBindingType K1, ShBindingType K2, typename T1, typename T2, bool S1, bool S2> \
00170 ShAttrib<libretsize, SH_TEMP, typename ShCommonType<T1, T2>::type, librettype, false> \
00171 libop(const ShAttrib<N, K1, T1, libtype, S1>& left, const ShAttrib<N, K2, T2, libtype, S2>& right) \
00172 { \
00173   ShGeneric<libretsize, typename ShCommonType<T1, T2>::type> t = libop(\
00174       static_cast< ShGeneric<N, T1> >(left), static_cast< ShGeneric<N, T2> >(right)); \
00175   return ShAttrib<libretsize, SH_TEMP, typename ShCommonType<T1, T2>::type, librettype, false>(t.node(), \
00176       t.swizzle(), t.neg()); \
00177 }
00178 
00179 #define SH_SHLIB_BINARY_OPERATION(libtype, libop, libretsize) \
00180   SH_SHLIB_BINARY_RETTYPE_OPERATION(libtype, libop, libtype, libretsize)
00181 
00182 #define SH_SHLIB_UNEQ_BINARY_RETTYPE_OPERATION(libtype, libop, librettype, libretsize) \
00183 template<int N, int M, ShBindingType K1, ShBindingType K2, typename T1, typename T2, bool S1, bool S2> \
00184 ShAttrib<libretsize, SH_TEMP, typename ShCommonType<T1, T2>::type, librettype, false> \
00185 libop(const ShAttrib<N, K1, T1, libtype, S1>& left, const ShAttrib<M, K2, T2, libtype, S2>& right) \
00186 { \
00187   ShGeneric<libretsize, typename ShCommonType<T1, T2>::type> t = libop(\
00188       static_cast< ShGeneric<N, T1> >(left), static_cast< ShGeneric<M, T2> >(right)); \
00189   return ShAttrib<libretsize, SH_TEMP, typename ShCommonType<T1, T2>::type, librettype, false>(\
00190       t.node(), t.swizzle(), t.neg()); \
00191 }
00192 
00193 #define SH_SHLIB_UNEQ_BINARY_OPERATION(libtype, libop, libretsize) \
00194   SH_SHLIB_UNEQ_BINARY_RETTYPE_OPERATION(libtype, libop, libtype, libretsize)
00195 
00196 #define SH_SHLIB_LEFT_SCALAR_RETTYPE_OPERATION(libtype, libop, librettype) \
00197 template<int M, ShBindingType K1, ShBindingType K2, typename T1, typename T2, bool S1, bool S2> \
00198 ShAttrib<M, SH_TEMP, typename ShCommonType<T1, T2>::type, librettype, false> \
00199 libop(const ShAttrib<1, K2, T1, libtype, S2>& left, const ShAttrib<M, K1, T2, libtype, S1>& right) \
00200 { \
00201   ShGeneric<M, typename ShCommonType<T1, T2>::type> t = libop( \
00202       static_cast< ShGeneric<1, T1> >(left), static_cast< ShGeneric<M, T2> >(right)); \
00203   return ShAttrib<M, SH_TEMP, typename ShCommonType<T1, T2>::type, librettype, false>(\
00204       t.node(), t.swizzle(), t.neg()); \
00205 }
00206 
00207 #define SH_SHLIB_LEFT_SCALAR_OPERATION(libtype, libop) \
00208   SH_SHLIB_LEFT_SCALAR_RETTYPE_OPERATION(libtype, libop, libtype)
00209 
00210 #define SH_SHLIB_LEFT_MATRIX_RETTYPE_OPERATION(libtype, libop, librettype, libretsize) \
00211 template<int M, int N, ShBindingType K1, ShBindingType K2, typename T1, typename T2, bool S1> \
00212 ShAttrib<libretsize, SH_TEMP, typename ShCommonType<T1, T2>::type, librettype, false> \
00213 libop(const ShMatrix<M, N, K1, T1>& a, const ShAttrib<N, K2, T2, libtype, S1>& b) \
00214 { \
00215   ShGeneric<libretsize, typename ShCommonType<T1, T2>::type> t = libop(a, \
00216       static_cast< ShGeneric<N, T2> >(b)); \
00217   return ShAttrib<libretsize, K1, typename ShCommonType<T1, T2>::type, librettype, S1>(\
00218       t.node(), t.swizzle(), t.neg()); \
00219 }
00220 
00221 #define SH_SHLIB_LEFT_MATRIX_OPERATION(libtype, libop, libretsize) \
00222   SH_SHLIB_LEFT_MATRIX_RETTYPE_OPERATION(libtype, libop, libtype, libretsize)
00223 
00224 // All the scalar constant stuff
00225 
00226 #define SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(libtype, libop, librettype, libretsize) \
00227 template<ShBindingType K, typename T, bool S> \
00228 ShAttrib<libretsize, SH_TEMP, T, librettype, false> \
00229 libop(const ShAttrib<1, K, T, libtype, S>& left, double right) \
00230 { \
00231   return libop(left, ShAttrib<1, SH_CONST, T>(right)); \
00232 } \
00233 template<ShBindingType K, typename T, bool S> \
00234 ShAttrib<libretsize, SH_TEMP, T, librettype, false> \
00235 libop(double left, const ShAttrib<1, K, T, libtype, S>& right) \
00236 { \
00237   return libop(ShAttrib<1, SH_CONST, T>(left), right); \
00238 } 
00239 
00240 #define SH_SHLIB_SPECIAL_CONST_SCALAR_OP(libtype, libop) \
00241   SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(libtype, libop, libtype, 1)
00242 
00243 #define SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_LEFT(libtype, libop, librettype, libretsize) \
00244 template<int N, ShBindingType K, typename T, bool S> \
00245 ShAttrib<libretsize, SH_TEMP, T, librettype, false> \
00246 libop(const ShAttrib<N, K, T, libtype, S>& left, double right) \
00247 { \
00248   return libop(left, ShAttrib<1, SH_CONST, T>(right)); \
00249 } 
00250 
00251 #define SH_SHLIB_SPECIAL_CONST_N_OP_LEFT(libtype, libop) \
00252   SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_LEFT(libtype, libop, libtype, N)
00253 
00254 #define SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_RIGHT(libtype, libop, librettype, libretsize) \
00255 template<int N, ShBindingType K, typename T, bool S> \
00256 ShAttrib<libretsize, SH_TEMP, T, librettype, false> \
00257 libop(double left, const ShAttrib<N, K, T, libtype, S>& right) \
00258 { \
00259   return libop(ShAttrib<1, SH_CONST, T>(left), right); \
00260 } 
00261 
00262 #define SH_SHLIB_SPECIAL_CONST_N_OP_RIGHT(libtype, libop) \
00263   SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_RIGHT(libtype, libop, libtype, N)
00264 
00265 #define SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_BOTH(libtype, operation, librettype, libretsize) \
00266 SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_LEFT(libtype, operation, librettype, libretsize); \
00267 SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_RIGHT(libtype, operation, librettype, libretsize);
00268 
00269 #define SH_SHLIB_SPECIAL_CONST_N_OP_BOTH(libtype, operation) \
00270 SH_SHLIB_SPECIAL_CONST_N_OP_LEFT(libtype, operation); \
00271 SH_SHLIB_SPECIAL_CONST_N_OP_RIGHT(libtype, operation);
00272 
00273 // Standard stuff
00274 
00275 #define SH_SHLIB_USUAL_OPERATIONS(type) \
00276   SH_SHLIB_USUAL_OPERATIONS_RETTYPE(type, type)
00277 
00278 #define SH_SHLIB_USUAL_OPERATIONS_RETTYPE(type, rettype) \
00279   SH_SHLIB_USUAL_NON_UNIT_OPS_RETTYPE(type, rettype)\
00280   SH_SHLIB_UNARY_RETTYPE_OPERATION(type, normalize, rettype, N);       \
00281   SH_SHLIB_UNARY_RETTYPE_OPERATION(type, abs, rettype, N);           
00282 
00283 #define SH_SHLIB_USUAL_NON_UNIT_OPS_RETTYPE(type, rettype) \
00284 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, operator+, rettype, N);      \
00285 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator+, rettype, 1);  \
00286                                                         \
00287 SH_SHLIB_UNEQ_BINARY_RETTYPE_OPERATION(type, operator*, rettype, N); \
00288 SH_SHLIB_LEFT_SCALAR_RETTYPE_OPERATION(type, operator*, rettype);    \
00289 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator*, rettype, 1);  \
00290 SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_BOTH(type, operator*, rettype, N);  \
00291                                                         \
00292 SH_SHLIB_UNEQ_BINARY_RETTYPE_OPERATION(type, operator/, rettype, N); \
00293 SH_SHLIB_LEFT_SCALAR_RETTYPE_OPERATION(type, operator/, rettype);    \
00294 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator/, rettype, 1);  \
00295 SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_BOTH(type, operator/, rettype, N);  \
00296                                                         \
00297 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, pow, rettype, N);            \
00298 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, pow, rettype, 1);        \
00299 SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_RIGHT(type, pow, rettype, N);       \
00300                                                         \
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 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, operator!=, rettype, N);     \
00312 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator!=, rettype, 1); \
00313                                                         \
00314 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, acos, rettype, N);            \
00315 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, asin, rettype, N);            \
00316 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, cos, rettype, N);             \
00317 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, cosh, rettype, N);             \
00318 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, dot, rettype, 1);           \
00319 SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_BOTH(type, dot, rettype, 1);           \
00320 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, mod, rettype, N);           \
00321 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, mod, rettype, 1);       \
00322                                                         \
00323 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, frac, rettype, N);            \
00324 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, sin, rettype, N);             \
00325 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, sinh, rettype, N);             \
00326 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, sqrt, rettype, N);            \
00327 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, tan, rettype, N);            \
00328 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, tanh, rettype, N);            \
00329                                                         \
00330 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, min, rettype, N);            \
00331 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, min, rettype, 1);        \
00332 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, pos, rettype, N);            \
00333 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, pos, rettype, 1);        \
00334 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, max, rettype, N);            \
00335 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, max, rettype, 1);
00336 
00337 // Points have different subtraction, so we don't include them in our "usuals"
00338 #define SH_SHLIB_USUAL_SUBTRACT(type) \
00339 SH_SHLIB_BINARY_OPERATION(type, operator-, N);      \
00340 SH_SHLIB_SPECIAL_CONST_SCALAR_OP(type, operator-);  \
00341 
00342 
00343 #endif

Generated on Thu Jul 28 17:33:02 2005 for Sh by  doxygen 1.4.3-20050530