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 "ShLibSplines.hpp"
00145 #include "ShLibWorley.hpp"
00146 // If you add anything to this list, make sure that ShLib.hpp is
00147 // included before the header guard since these libraries must always
00148 // be loaded by ShLib.hpp, no matter who includes them.  See
00149 // ShLibNoise.hpp for an example.
00150 
00151 #undef CT1T2 
00152 #undef CT1T2T3 
00153 #undef CT1T2T3T4 
00154 
00155 // Semantic stuff
00156 
00157 #define SH_SHLIB_UNARY_RETTYPE_OPERATION(libtype, libop, librettype, libretsize) \
00158 template<int N, ShBindingType K1, typename T, bool S1> \
00159 ShAttrib<libretsize, SH_TEMP, T, librettype, false> \
00160 libop(const ShAttrib<N, K1, T, libtype, S1>& var) \
00161 { \
00162   ShGeneric<libretsize, T> t = libop(static_cast< ShGeneric<N, T> >(var)); \
00163   return ShAttrib<libretsize, SH_TEMP, T, librettype, false>(t.node(), t.swizzle(), t.neg()); \
00164 }
00165 
00166 #define SH_SHLIB_UNARY_OPERATION(libtype, libop, libretsize) \
00167   SH_SHLIB_UNARY_RETTYPE_OPERATION(libtype, libop, libtype, libretsize)
00168 
00169 #define SH_SHLIB_BINARY_RETTYPE_OPERATION(libtype, libop, librettype, libretsize) \
00170 template<int N, ShBindingType K1, ShBindingType K2, typename T1, typename T2, bool S1, bool S2> \
00171 ShAttrib<libretsize, SH_TEMP, typename ShCommonType<T1, T2>::type, librettype, false> \
00172 libop(const ShAttrib<N, K1, T1, libtype, S1>& left, const ShAttrib<N, K2, T2, libtype, S2>& right) \
00173 { \
00174   ShGeneric<libretsize, typename ShCommonType<T1, T2>::type> t = libop(\
00175       static_cast< ShGeneric<N, T1> >(left), static_cast< ShGeneric<N, T2> >(right)); \
00176   return ShAttrib<libretsize, SH_TEMP, typename ShCommonType<T1, T2>::type, librettype, false>(t.node(), \
00177       t.swizzle(), t.neg()); \
00178 }
00179 
00180 #define SH_SHLIB_BINARY_OPERATION(libtype, libop, libretsize) \
00181   SH_SHLIB_BINARY_RETTYPE_OPERATION(libtype, libop, libtype, libretsize)
00182 
00183 #define SH_SHLIB_UNEQ_BINARY_RETTYPE_OPERATION(libtype, libop, librettype, libretsize) \
00184 template<int N, int M, ShBindingType K1, ShBindingType K2, typename T1, typename T2, bool S1, bool S2> \
00185 ShAttrib<libretsize, SH_TEMP, typename ShCommonType<T1, T2>::type, librettype, false> \
00186 libop(const ShAttrib<N, K1, T1, libtype, S1>& left, const ShAttrib<M, K2, T2, libtype, S2>& right) \
00187 { \
00188   ShGeneric<libretsize, typename ShCommonType<T1, T2>::type> t = libop(\
00189       static_cast< ShGeneric<N, T1> >(left), static_cast< ShGeneric<M, T2> >(right)); \
00190   return ShAttrib<libretsize, SH_TEMP, typename ShCommonType<T1, T2>::type, librettype, false>(\
00191       t.node(), t.swizzle(), t.neg()); \
00192 }
00193 
00194 #define SH_SHLIB_UNEQ_BINARY_OPERATION(libtype, libop, libretsize) \
00195   SH_SHLIB_UNEQ_BINARY_RETTYPE_OPERATION(libtype, libop, libtype, libretsize)
00196 
00197 #define SH_SHLIB_LEFT_SCALAR_RETTYPE_OPERATION(libtype, libop, librettype) \
00198 template<int M, ShBindingType K1, ShBindingType K2, typename T1, typename T2, bool S1, bool S2> \
00199 ShAttrib<M, SH_TEMP, typename ShCommonType<T1, T2>::type, librettype, false> \
00200 libop(const ShAttrib<1, K2, T1, libtype, S2>& left, const ShAttrib<M, K1, T2, libtype, S1>& right) \
00201 { \
00202   ShGeneric<M, typename ShCommonType<T1, T2>::type> t = libop( \
00203       static_cast< ShGeneric<1, T1> >(left), static_cast< ShGeneric<M, T2> >(right)); \
00204   return ShAttrib<M, SH_TEMP, typename ShCommonType<T1, T2>::type, librettype, false>(\
00205       t.node(), t.swizzle(), t.neg()); \
00206 }
00207 
00208 #define SH_SHLIB_LEFT_SCALAR_OPERATION(libtype, libop) \
00209   SH_SHLIB_LEFT_SCALAR_RETTYPE_OPERATION(libtype, libop, libtype)
00210 
00211 #define SH_SHLIB_LEFT_MATRIX_RETTYPE_OPERATION(libtype, libop, librettype, libretsize) \
00212 template<int M, int N, ShBindingType K1, ShBindingType K2, typename T1, typename T2, bool S1> \
00213 ShAttrib<libretsize, SH_TEMP, typename ShCommonType<T1, T2>::type, librettype, false> \
00214 libop(const ShMatrix<M, N, K1, T1>& a, const ShAttrib<N, K2, T2, libtype, S1>& b) \
00215 { \
00216   ShGeneric<libretsize, typename ShCommonType<T1, T2>::type> t = libop(a, \
00217       static_cast< ShGeneric<N, T2> >(b)); \
00218   return ShAttrib<libretsize, K1, typename ShCommonType<T1, T2>::type, librettype, S1>(\
00219       t.node(), t.swizzle(), t.neg()); \
00220 }
00221 
00222 #define SH_SHLIB_LEFT_MATRIX_OPERATION(libtype, libop, libretsize) \
00223   SH_SHLIB_LEFT_MATRIX_RETTYPE_OPERATION(libtype, libop, libtype, libretsize)
00224 
00225 // All the scalar constant stuff
00226 
00227 #define SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(libtype, libop, librettype, libretsize) \
00228 template<ShBindingType K, typename T, bool S> \
00229 ShAttrib<libretsize, SH_TEMP, T, librettype, false> \
00230 libop(const ShAttrib<1, K, T, libtype, S>& left, double right) \
00231 { \
00232   return libop(left, ShAttrib<1, SH_CONST, T>(right)); \
00233 } \
00234 template<ShBindingType K, typename T, bool S> \
00235 ShAttrib<libretsize, SH_TEMP, T, librettype, false> \
00236 libop(double left, const ShAttrib<1, K, T, libtype, S>& right) \
00237 { \
00238   return libop(ShAttrib<1, SH_CONST, T>(left), right); \
00239 } 
00240 
00241 #define SH_SHLIB_SPECIAL_CONST_SCALAR_OP(libtype, libop) \
00242   SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(libtype, libop, libtype, 1)
00243 
00244 #define SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_LEFT(libtype, libop, librettype, libretsize) \
00245 template<int N, ShBindingType K, typename T, bool S> \
00246 ShAttrib<libretsize, SH_TEMP, T, librettype, false> \
00247 libop(const ShAttrib<N, K, T, libtype, S>& left, double right) \
00248 { \
00249   return libop(left, ShAttrib<1, SH_CONST, T>(right)); \
00250 } 
00251 
00252 #define SH_SHLIB_SPECIAL_CONST_N_OP_LEFT(libtype, libop) \
00253   SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_LEFT(libtype, libop, libtype, N)
00254 
00255 #define SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_RIGHT(libtype, libop, librettype, libretsize) \
00256 template<int N, ShBindingType K, typename T, bool S> \
00257 ShAttrib<libretsize, SH_TEMP, T, librettype, false> \
00258 libop(double left, const ShAttrib<N, K, T, libtype, S>& right) \
00259 { \
00260   return libop(ShAttrib<1, SH_CONST, T>(left), right); \
00261 } 
00262 
00263 #define SH_SHLIB_SPECIAL_CONST_N_OP_RIGHT(libtype, libop) \
00264   SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_RIGHT(libtype, libop, libtype, N)
00265 
00266 #define SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_BOTH(libtype, operation, librettype, libretsize) \
00267 SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_LEFT(libtype, operation, librettype, libretsize); \
00268 SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_RIGHT(libtype, operation, librettype, libretsize);
00269 
00270 #define SH_SHLIB_SPECIAL_CONST_N_OP_BOTH(libtype, operation) \
00271 SH_SHLIB_SPECIAL_CONST_N_OP_LEFT(libtype, operation); \
00272 SH_SHLIB_SPECIAL_CONST_N_OP_RIGHT(libtype, operation);
00273 
00274 // Standard stuff
00275 
00276 #define SH_SHLIB_USUAL_OPERATIONS(type) \
00277   SH_SHLIB_USUAL_OPERATIONS_RETTYPE(type, type)
00278 
00279 #define SH_SHLIB_USUAL_OPERATIONS_RETTYPE(type, rettype) \
00280   SH_SHLIB_USUAL_NON_UNIT_OPS_RETTYPE(type, rettype)\
00281   SH_SHLIB_UNARY_RETTYPE_OPERATION(type, normalize, rettype, N);       \
00282   SH_SHLIB_UNARY_RETTYPE_OPERATION(type, abs, rettype, N);           
00283 
00284 #define SH_SHLIB_USUAL_NON_UNIT_OPS_RETTYPE(type, rettype) \
00285 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, operator+, rettype, N);      \
00286 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator+, rettype, 1);  \
00287                                                         \
00288 SH_SHLIB_UNEQ_BINARY_RETTYPE_OPERATION(type, operator*, rettype, N); \
00289 SH_SHLIB_LEFT_SCALAR_RETTYPE_OPERATION(type, operator*, rettype);    \
00290 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator*, rettype, 1);  \
00291 SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_BOTH(type, operator*, rettype, N);  \
00292                                                         \
00293 SH_SHLIB_UNEQ_BINARY_RETTYPE_OPERATION(type, operator/, rettype, N); \
00294 SH_SHLIB_LEFT_SCALAR_RETTYPE_OPERATION(type, operator/, rettype);    \
00295 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator/, rettype, 1);  \
00296 SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_BOTH(type, operator/, rettype, N);  \
00297                                                         \
00298 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, pow, rettype, N);            \
00299 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, pow, rettype, 1);        \
00300 SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_RIGHT(type, pow, rettype, N);       \
00301                                                         \
00302 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, operator<, rettype, N);      \
00303 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator<, rettype, 1);  \
00304 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, operator<=, rettype, N);     \
00305 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator<=, rettype, 1); \
00306 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, operator>, rettype, N);      \
00307 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator>, rettype, 1);  \
00308 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, operator>=, rettype, N);     \
00309 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator>=, rettype, 1); \
00310 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, operator==, rettype, N);     \
00311 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator==, rettype, 1); \
00312 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, operator!=, rettype, N);     \
00313 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, operator!=, rettype, 1); \
00314                                                         \
00315 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, acos, rettype, N);            \
00316 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, asin, rettype, N);            \
00317 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, cos, rettype, N);             \
00318 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, cosh, rettype, N);             \
00319 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, dot, rettype, 1);           \
00320 SH_SHLIB_SPECIAL_RETTYPE_CONST_N_OP_BOTH(type, dot, rettype, 1);           \
00321 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, mod, rettype, N);           \
00322 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, mod, rettype, 1);       \
00323                                                         \
00324 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, frac, rettype, N);            \
00325 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, sin, rettype, N);             \
00326 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, sinh, rettype, N);             \
00327 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, sqrt, rettype, N);            \
00328 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, tan, rettype, N);            \
00329 SH_SHLIB_UNARY_RETTYPE_OPERATION(type, tanh, rettype, N);            \
00330                                                         \
00331 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, min, rettype, N);            \
00332 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, min, rettype, 1);        \
00333 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, pos, rettype, N);            \
00334 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, pos, rettype, 1);        \
00335 SH_SHLIB_BINARY_RETTYPE_OPERATION(type, max, rettype, N);            \
00336 SH_SHLIB_SPECIAL_RETTYPE_CONST_SCALAR_OP(type, max, rettype, 1);
00337 
00338 // Points have different subtraction, so we don't include them in our "usuals"
00339 #define SH_SHLIB_USUAL_SUBTRACT(type) \
00340 SH_SHLIB_BINARY_OPERATION(type, operator-, N);      \
00341 SH_SHLIB_SPECIAL_CONST_SCALAR_OP(type, operator-);  \
00342 
00343 
00344 #endif

Generated on Wed Nov 9 15:29:33 2005 for Sh by  doxygen 1.4.5