ShGeneric.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2006 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 SHGENERIC_HPP
00021 #define SHGENERIC_HPP
00022 
00023 #include "ShVariableType.hpp"
00024 #include "ShDataType.hpp"
00025 #include "ShVariable.hpp"
00026 #include "ShVariant.hpp"
00027 
00028 namespace SH {
00029 
00030 class ShProgram;
00031 
00056 template<int N, typename T>
00057 class ShGeneric : public ShVariable 
00058 {
00059 public:
00060   typedef T storage_type;
00061   static const ShValueType value_type = ShStorageTypeInfo<T>::value_type;
00062   typedef typename ShHostType<T>::type host_type; 
00063   typedef typename ShMemType<T>::type mem_type; 
00064   static const int typesize = N;
00065 
00066   typedef ShDataVariant<T, SH_HOST> VariantType; 
00067   typedef ShPointer<VariantType> VariantTypePtr;
00068   typedef ShPointer<const VariantType> VariantTypeCPtr;
00069 
00070 
00071   ShGeneric(const ShVariableNodePtr& node, ShSwizzle swizzle, bool neg);
00072   ~ShGeneric();
00073 
00074   // Copy constructor 
00075   // This should only be used internally.
00076   // It generates a ShVariableNode of type SH_TEMP 
00077   // @{
00078 
00079   // @todo type get rid of this. default should be okay for 
00080   // internal usage
00081   // ShGeneric(const ShGeneric<N, T> &other);
00082 
00083   template<typename T2>
00084   ShGeneric(const ShGeneric<N, T2> &other);
00085   // @}
00086 
00087   // This is needed because the templated assignment op is 
00088   // non-default, hence C++ spec 12.8.10 says the default
00089   // is implicitly defined.  Here that doesn't work so well. 
00090   ShGeneric& operator=(const ShGeneric& other);
00091 
00092   template<typename T2>
00093   ShGeneric& operator=(const ShGeneric<N, T2>& other);
00094 
00095   ShGeneric& operator=(const ShProgram& other);
00096 
00097   ShGeneric& operator++();
00098   ShGeneric& operator--();
00099   
00100   template<typename T2>
00101   ShGeneric& operator+=(const ShGeneric<N, T2>& right);
00102 
00103   template<typename T2>
00104   ShGeneric& operator-=(const ShGeneric<N, T2>& right);
00105 
00106   template<typename T2>
00107   ShGeneric& operator*=(const ShGeneric<N, T2>& right);
00108 
00109   template<typename T2>
00110   ShGeneric& operator/=(const ShGeneric<N, T2>& right);
00111 
00112   template<typename T2>
00113   ShGeneric& operator%=(const ShGeneric<N, T2>& right);
00114 
00115   template<typename T2>
00116   ShGeneric& operator+=(const ShGeneric<1, T2>& right);
00117 
00118   template<typename T2>
00119   ShGeneric& operator-=(const ShGeneric<1, T2>& right);
00120 
00121   template<typename T2>
00122   ShGeneric& operator*=(const ShGeneric<1, T2>& right);
00123 
00124   template<typename T2>
00125   ShGeneric& operator/=(const ShGeneric<1, T2>& right);
00126 
00127   template<typename T2>
00128   ShGeneric& operator%=(const ShGeneric<1, T2>& right);
00129 
00130   ShGeneric& operator+=(host_type);
00131   ShGeneric& operator-=(host_type);
00132   ShGeneric& operator*=(host_type);
00133   ShGeneric& operator/=(host_type);
00134   ShGeneric& operator%=(host_type);
00135 
00136   ShGeneric operator-() const;
00137 
00138   ShGeneric operator()() const; 
00139   ShGeneric<1, T> operator()(int) const;
00140   ShGeneric<1, T> operator[](int) const;
00141   ShGeneric<2, T> operator()(int, int) const;
00142   ShGeneric<3, T> operator()(int, int, int) const;
00143   ShGeneric<4, T> operator()(int, int, int, int) const;
00144 
00146   void range(host_type low, host_type high);
00147 
00148   VariantType lowBound() const; 
00149   host_type lowBound(int index) const;
00150 
00151   VariantType highBound() const;
00152   host_type highBound(int index) const;
00153   
00154   // Arbitrary Swizzle
00155   template<int N2>
00156   ShGeneric<N2, T> swiz(int indices[]) const;
00157 
00159   void getValues(host_type dest[]) const;
00160   host_type getValue(int index) const;
00161   
00164   void setValue(int index, const host_type &value); 
00165   void setValues(const host_type values[]);
00166 
00167 
00168 protected:
00169   ShGeneric(const ShVariableNodePtr& node);
00170 
00171 };
00172 
00173 template<typename T>
00174 class ShGeneric<1, T> : public ShVariable 
00175 {
00176 public:
00177   typedef T storage_type;
00178   static const ShValueType value_type = ShStorageTypeInfo<T>::value_type;
00179   typedef typename ShHostType<T>::type host_type; 
00180   typedef typename ShMemType<T>::type mem_type; 
00181   static const int typesize = 1;
00182 
00183   typedef ShDataVariant<T, SH_HOST> VariantType; 
00184   typedef ShPointer<VariantType> VariantTypePtr;
00185   typedef ShPointer<const VariantType> VariantTypeCPtr;
00186 
00187 
00188   ShGeneric(const ShVariableNodePtr& node, ShSwizzle swizzle, bool neg);
00189   ~ShGeneric();
00190 
00191   // Copy constructor 
00192   // This should only be used internally.  It generates a SH_TEMP, 
00193   // SH_ATTRIB, with the only characteristic copied from other being
00194   // the storage type.
00195   // @{
00196 
00197   // @todo type get rid of this
00198   // ShGeneric(const ShGeneric<1, T> &other);
00199 
00200   template<typename T2>
00201   ShGeneric(const ShGeneric<1, T2> &other);
00202   // @}
00203 
00204   ShGeneric& operator=(const ShGeneric<1, T>& other);
00205 
00206   template<typename T2>
00207   ShGeneric& operator=(const ShGeneric<1, T2>& other);
00208 
00209   ShGeneric& operator=(host_type);
00210   ShGeneric& operator=(const ShProgram& other);
00211   
00212   ShGeneric& operator++();
00213   ShGeneric& operator--();
00214 
00215   template<typename T2>
00216   ShGeneric& operator+=(const ShGeneric<1, T2>& right);
00217 
00218   template<typename T2>
00219   ShGeneric& operator-=(const ShGeneric<1, T2>& right);
00220 
00221   template<typename T2>
00222   ShGeneric& operator*=(const ShGeneric<1, T2>& right);
00223 
00224   template<typename T2>
00225   ShGeneric& operator/=(const ShGeneric<1, T2>& right);
00226 
00227   template<typename T2>
00228   ShGeneric& operator%=(const ShGeneric<1, T2>& right);
00229 
00230   ShGeneric& operator+=(host_type);
00231   ShGeneric& operator-=(host_type);
00232   ShGeneric& operator*=(host_type);
00233   ShGeneric& operator/=(host_type);
00234   ShGeneric& operator%=(host_type);
00235 
00236   ShGeneric operator-() const;
00237 
00238   ShGeneric operator()() const; 
00239   ShGeneric<1, T> operator()(int) const;
00240   ShGeneric<1, T> operator[](int) const;
00241   ShGeneric<2, T> operator()(int, int) const;
00242   ShGeneric<3, T> operator()(int, int, int) const;
00243   ShGeneric<4, T> operator()(int, int, int, int) const;
00244 
00246   void range(host_type low, host_type high);
00247 
00248   VariantType lowBound() const; 
00249   host_type lowBound(int index) const;
00250 
00251   VariantType highBound() const;
00252   host_type highBound(int index) const;
00253   
00254   // Arbitrary Swizzle
00255   template<int N2>
00256   ShGeneric<N2, T> swiz(int indices[]) const;
00257 
00259   void getValues(host_type dest[]) const;
00260   host_type getValue(int index) const;
00261   
00264   void setValue(int index, const host_type &value); 
00265   void setValues(const host_type values[]);
00266 
00267 protected:
00268   ShGeneric(const ShVariableNodePtr& node);
00269 
00270 };
00271 
00272 typedef ShGeneric<1, ShFracUShort> ShGeneric1fus;
00273 typedef ShGeneric<2, ShFracUShort> ShGeneric2fus;
00274 typedef ShGeneric<3, ShFracUShort> ShGeneric3fus;
00275 typedef ShGeneric<4, ShFracUShort> ShGeneric4fus;
00276 
00277 typedef ShGeneric<1, short> ShGeneric1s;
00278 typedef ShGeneric<2, short> ShGeneric2s;
00279 typedef ShGeneric<3, short> ShGeneric3s;
00280 typedef ShGeneric<4, short> ShGeneric4s;
00281 
00282 typedef ShGeneric<1, ShFracUInt> ShGeneric1fui;
00283 typedef ShGeneric<2, ShFracUInt> ShGeneric2fui;
00284 typedef ShGeneric<3, ShFracUInt> ShGeneric3fui;
00285 typedef ShGeneric<4, ShFracUInt> ShGeneric4fui;
00286 
00287 typedef ShGeneric<1, ShFracByte> ShGeneric1fb;
00288 typedef ShGeneric<2, ShFracByte> ShGeneric2fb;
00289 typedef ShGeneric<3, ShFracByte> ShGeneric3fb;
00290 typedef ShGeneric<4, ShFracByte> ShGeneric4fb;
00291 
00292 typedef ShGeneric<1, int> ShGeneric1i;
00293 typedef ShGeneric<2, int> ShGeneric2i;
00294 typedef ShGeneric<3, int> ShGeneric3i;
00295 typedef ShGeneric<4, int> ShGeneric4i;
00296 
00297 typedef ShGeneric<1, double> ShGeneric1d;
00298 typedef ShGeneric<2, double> ShGeneric2d;
00299 typedef ShGeneric<3, double> ShGeneric3d;
00300 typedef ShGeneric<4, double> ShGeneric4d;
00301 
00302 typedef ShGeneric<1, unsigned char> ShGeneric1ub;
00303 typedef ShGeneric<2, unsigned char> ShGeneric2ub;
00304 typedef ShGeneric<3, unsigned char> ShGeneric3ub;
00305 typedef ShGeneric<4, unsigned char> ShGeneric4ub;
00306 
00307 typedef ShGeneric<1, float> ShGeneric1f;
00308 typedef ShGeneric<2, float> ShGeneric2f;
00309 typedef ShGeneric<3, float> ShGeneric3f;
00310 typedef ShGeneric<4, float> ShGeneric4f;
00311 
00312 typedef ShGeneric<1, char> ShGeneric1b;
00313 typedef ShGeneric<2, char> ShGeneric2b;
00314 typedef ShGeneric<3, char> ShGeneric3b;
00315 typedef ShGeneric<4, char> ShGeneric4b;
00316 
00317 typedef ShGeneric<1, unsigned short> ShGeneric1us;
00318 typedef ShGeneric<2, unsigned short> ShGeneric2us;
00319 typedef ShGeneric<3, unsigned short> ShGeneric3us;
00320 typedef ShGeneric<4, unsigned short> ShGeneric4us;
00321 
00322 typedef ShGeneric<1, ShFracUByte> ShGeneric1fub;
00323 typedef ShGeneric<2, ShFracUByte> ShGeneric2fub;
00324 typedef ShGeneric<3, ShFracUByte> ShGeneric3fub;
00325 typedef ShGeneric<4, ShFracUByte> ShGeneric4fub;
00326 
00327 typedef ShGeneric<1, ShHalf> ShGeneric1h;
00328 typedef ShGeneric<2, ShHalf> ShGeneric2h;
00329 typedef ShGeneric<3, ShHalf> ShGeneric3h;
00330 typedef ShGeneric<4, ShHalf> ShGeneric4h;
00331 
00332 typedef ShGeneric<1, ShFracShort> ShGeneric1fs;
00333 typedef ShGeneric<2, ShFracShort> ShGeneric2fs;
00334 typedef ShGeneric<3, ShFracShort> ShGeneric3fs;
00335 typedef ShGeneric<4, ShFracShort> ShGeneric4fs;
00336 
00337 typedef ShGeneric<1, ShFracInt> ShGeneric1fi;
00338 typedef ShGeneric<2, ShFracInt> ShGeneric2fi;
00339 typedef ShGeneric<3, ShFracInt> ShGeneric3fi;
00340 typedef ShGeneric<4, ShFracInt> ShGeneric4fi;
00341 
00342 typedef ShGeneric<1, unsigned int> ShGeneric1ui;
00343 typedef ShGeneric<2, unsigned int> ShGeneric2ui;
00344 typedef ShGeneric<3, unsigned int> ShGeneric3ui;
00345 typedef ShGeneric<4, unsigned int> ShGeneric4ui;
00346 
00347 }
00348 
00349 // This is a hack for ShAttrib.hpp in particular.
00350 // A little dirty, but it works well.
00351 #ifndef SH_DO_NOT_INCLUDE_GENERIC_IMPL
00352 #include "ShGenericImpl.hpp"
00353 #endif
00354 
00355 #endif

Generated on Thu Feb 16 14:51:32 2006 for Sh by  doxygen 1.4.6