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

ShGenericImpl.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 SHGENERICIMPL_HPP
00025 #define SHGENERICIMPL_HPP
00026 
00027 #include "ShGeneric.hpp"
00028 #include "ShAttrib.hpp"
00029 #include "ShLib.hpp"
00030 #include "ShInstructions.hpp"
00031 #include "ShDebug.hpp"
00032 #include "ShProgram.hpp"
00033 
00034 namespace SH {
00035 
00036 template<int N, typename T>
00037 ShGeneric<N, T>::ShGeneric(const ShVariableNodePtr& node)
00038   : ShVariable(node)
00039 {
00040   SH_DEBUG_ASSERT(node); // DEBUG
00041 }
00042 
00043 template<int N, typename T>
00044 ShGeneric<N, T>::ShGeneric(const ShVariableNodePtr& node, ShSwizzle swizzle, bool neg)
00045   : ShVariable(node)
00046 {
00047   m_swizzle = swizzle;
00048   m_neg = neg;
00049   SH_DEBUG_ASSERT(node); // DEBUG
00050 }
00051 
00052 template<int N, typename T>
00053 ShGeneric<N, T>::~ShGeneric()
00054 {
00055 }
00056 
00057 template<int N, typename T>
00058 template<typename T2>
00059 ShGeneric<N, T>::ShGeneric(const ShGeneric<N, T2>& other)
00060   : ShVariable(new ShVariableNode(SH_TEMP, N, value_type, 
00061         other.node()->specialType()))
00062 {
00063   SH_DEBUG_ASSERT(other.node());
00064   SH_DEBUG_ASSERT(m_node);
00065   shASN(*this, other);
00066 }
00067 
00068 template<int N, typename T>
00069 ShGeneric<N, T>& ShGeneric<N, T>::operator=(const ShProgram& prg)
00070 {
00071   this->ShVariable::operator=(prg);
00072   return *this;
00073 }
00074 
00075 template<int N, typename T>
00076 ShGeneric<N, T>& ShGeneric<N, T>::operator=(const ShGeneric<N, T>& other)
00077 {
00078   shASN(*this, other);
00079   return *this;
00080 }
00081 
00082 template<int N, typename T>
00083 template<typename T2>
00084 ShGeneric<N, T>& ShGeneric<N, T>::operator=(const ShGeneric<N, T2>& other)
00085 {
00086   shASN(*this, other);
00087   return *this;
00088 }
00089 
00090 template<int N, typename T>
00091 ShGeneric<N, T>& ShGeneric<N, T>::operator++()
00092 {
00093   *this += 1;
00094   return *this;
00095 }
00096 
00097 template<int N, typename T>
00098 ShGeneric<N, T>& ShGeneric<N, T>::operator--()
00099 {
00100   *this -= 1;
00101   return *this;
00102 }
00103 
00104 template<int N, typename T>
00105 template<typename T2>
00106 ShGeneric<N, T>& ShGeneric<N, T>::operator+=(const ShGeneric<N, T2>& right)
00107 {
00108   *this = *this + right;
00109   return *this;
00110 }
00111 
00112 template<int N, typename T>
00113 template<typename T2>
00114 ShGeneric<N, T>& ShGeneric<N, T>::operator-=(const ShGeneric<N, T2>& right)
00115 {
00116   *this = *this - right;
00117   return *this;
00118 }
00119 
00120 template<int N, typename T>
00121 template<typename T2>
00122 ShGeneric<N, T>& ShGeneric<N, T>::operator*=(const ShGeneric<N, T2>& right)
00123 {
00124   *this = *this * right;
00125   return *this;
00126 }
00127 
00128 template<int N, typename T>
00129 template<typename T2>
00130 ShGeneric<N, T>& ShGeneric<N, T>::operator/=(const ShGeneric<N, T2>& right)
00131 {
00132   *this = *this / right;
00133   return *this;
00134 }
00135 
00136 template<int N, typename T>
00137 template<typename T2>
00138 ShGeneric<N, T>& ShGeneric<N, T>::operator%=(const ShGeneric<N, T2>& right)
00139 {
00140   *this = *this % right;
00141   return *this;
00142 }
00143 
00144 template<int N, typename T>
00145 template<typename T2>
00146 ShGeneric<N, T>& ShGeneric<N, T>::operator+=(const ShGeneric<1, T2>& right)
00147 {
00148   *this = *this + right;
00149   return *this;
00150 }
00151 
00152 template<int N, typename T>
00153 template<typename T2>
00154 ShGeneric<N, T>& ShGeneric<N, T>::operator-=(const ShGeneric<1, T2>& right)
00155 {
00156   *this = *this - right;
00157   return *this;
00158 }
00159 
00160 template<int N, typename T>
00161 template<typename T2>
00162 ShGeneric<N, T>& ShGeneric<N, T>::operator*=(const ShGeneric<1, T2>& right)
00163 {
00164   *this = *this * right;
00165   return *this;
00166 }
00167 
00168 template<int N, typename T>
00169 template<typename T2>
00170 ShGeneric<N, T>& ShGeneric<N, T>::operator/=(const ShGeneric<1, T2>& right)
00171 {
00172   *this = *this / right;
00173   return *this;
00174 }
00175 
00176 template<int N, typename T>
00177 template<typename T2>
00178 ShGeneric<N, T>& ShGeneric<N, T>::operator%=(const ShGeneric<1, T2>& right)
00179 {
00180   *this = *this % right;
00181   return *this;
00182 }
00183 
00184 template<int N, typename T>
00185 ShGeneric<N, T>& ShGeneric<N, T>::operator+=(host_type right)
00186 {
00187   *this = *this + right;
00188   return *this;
00189 }
00190 
00191 template<int N, typename T>
00192 ShGeneric<N, T>& ShGeneric<N, T>::operator-=(host_type right)
00193 {
00194   *this = *this - right;
00195   return *this;
00196 }
00197 
00198 template<int N, typename T>
00199 ShGeneric<N, T>& ShGeneric<N, T>::operator*=(host_type right)
00200 {
00201   *this = *this * right;
00202   return *this;
00203 }
00204 
00205 template<int N, typename T>
00206 ShGeneric<N, T>& ShGeneric<N, T>::operator/=(host_type right)
00207 {
00208   *this = *this / right;
00209   return *this;
00210 }
00211 
00212 template<int N, typename T>
00213 ShGeneric<N, T>& ShGeneric<N, T>::operator%=(host_type right)
00214 {
00215   *this = *this % right;
00216   return *this;
00217 }
00218 
00219 template<int N, typename T>
00220 ShGeneric<N, T> ShGeneric<N, T>::operator-() const
00221 {
00222   return ShGeneric<N, T>(m_node, m_swizzle, !m_neg);
00223 }
00224 
00225 
00226 template<int N, typename T>
00227 ShGeneric<N, T> ShGeneric<N, T>::operator()() const
00228 {
00229   return ShGeneric<N, T>(m_node, m_swizzle, m_neg);
00230 }
00231 
00232 template<int N, typename T>
00233 ShGeneric<1, T> ShGeneric<N, T>::operator()(int i1) const
00234 {
00235   return ShGeneric<1, T>(m_node, m_swizzle * ShSwizzle(size(), i1), m_neg);
00236 }
00237 
00238 template<int N, typename T>
00239 ShGeneric<1, T> ShGeneric<N, T>::operator[](int i1) const
00240 {
00241   return ShGeneric<1, T>(m_node, m_swizzle * ShSwizzle(size(), i1), m_neg);
00242 }
00243 
00244 template<int N, typename T>
00245 ShGeneric<2, T> ShGeneric<N, T>::operator()(int i1, int i2) const
00246 {
00247   return ShGeneric<2, T>(m_node, m_swizzle * ShSwizzle(size(), i1, i2), m_neg);
00248 }
00249 
00250 template<int N, typename T>
00251 ShGeneric<3, T> ShGeneric<N, T>::operator()(int i1, int i2, int i3) const
00252 {
00253   return ShGeneric<3, T>(m_node, m_swizzle * ShSwizzle(size(), i1, i2, i3), m_neg);
00254 }
00255 
00256 template<int N, typename T>
00257 ShGeneric<4, T> ShGeneric<N, T>::operator()(int i1, int i2, int i3, int i4) const
00258 {
00259   return ShGeneric<4, T>(m_node, m_swizzle * ShSwizzle(size(), i1, i2, i3, i4), m_neg);
00260 }
00261 
00262 template<int N, typename T>
00263 void ShGeneric<N, T>::range(host_type low, host_type high) 
00264 {
00265   rangeVariant(new VariantType(1, low), new VariantType(1, high));
00266 }
00267 
00268 template<int N, typename T>
00269 typename ShGeneric<N, T>::VariantType ShGeneric<N, T>::lowBound() const
00270 {
00271   return (*variant_cast<T, SH_HOST>(lowBoundVariant()));
00272 }
00273 
00274 template<int N, typename T>
00275 typename ShGeneric<N, T>::host_type ShGeneric<N, T>::lowBound(int index) const
00276 {
00277   return (*variant_cast<T, SH_HOST>(lowBoundVariant()))[index];
00278 }
00279 
00280 template<int N, typename T>
00281 typename ShGeneric<N, T>::VariantType ShGeneric<N, T>::highBound() const
00282 {
00283   return (*variant_cast<T, SH_HOST>(highBoundVariant()));
00284 }
00285 
00286 template<int N, typename T>
00287 typename ShGeneric<N, T>::host_type ShGeneric<N, T>::highBound(int index) const
00288 {
00289   return (*variant_cast<T, SH_HOST>(highBoundVariant()))[index];
00290 }
00291   
00292 template<int N, typename T> 
00293 template<int N2>
00294 ShGeneric<N2, T> ShGeneric<N, T>::swiz(int indices[]) const
00295 {
00296   return ShGeneric<N2, T>(m_node, m_swizzle * ShSwizzle(N, N2, indices), m_neg);
00297 }
00298 
00299 template<int N, typename T>
00300 void ShGeneric<N, T>::getValues(host_type dest[]) const
00301 {
00302   VariantTypePtr c = variant_cast<T, SH_HOST>(getVariant()); 
00303   for(int i = 0; i < N; ++i) dest[i] = (*c)[i]; 
00304 }
00305 
00306 template<int N, typename T>
00307 typename ShGeneric<N, T>::host_type ShGeneric<N, T>::getValue(int index) const
00308 {
00309   VariantTypePtr c = variant_cast<T, SH_HOST>(getVariant(index)); 
00310   return (*c)[0];
00311 }
00312 
00313 template<int N, typename T>
00314 void ShGeneric<N, T>::setValue(int index, const host_type &variantValue) 
00315 {
00316   if(m_swizzle.identity() && !m_neg) {
00317     VariantTypePtr c = variant_cast<T, SH_HOST>(m_node->getVariant()); 
00318     (*c)[index] = variantValue;
00319   } else {
00320     VariantTypePtr variant(new VariantType(1, variantValue));
00321     setVariant(variant, false, ShSwizzle(N, index));
00322   }
00323 }
00324 
00325 template<int N, typename T>
00326 void ShGeneric<N, T>::setValues(const host_type variantValues[]) 
00327 {
00328   if(m_swizzle.identity() && !m_neg) {
00329     VariantTypePtr variantPtr(variant_cast<T, SH_HOST>(m_node->getVariant()));
00330     std::copy(variantValues, variantValues + N, variantPtr->begin()); 
00331   } else {
00332     VariantTypePtr variantPtr(new VariantType(N, variantValues, false));
00333     setVariant(variantPtr);
00334   }
00335 }
00336 
00337 template<typename T>
00338 ShGeneric<1, T>::ShGeneric(const ShVariableNodePtr& node)
00339   : ShVariable(node)
00340 {
00341   SH_DEBUG_ASSERT(node); // DEBUG
00342 }
00343 
00344 template<typename T>
00345 ShGeneric<1, T>::ShGeneric(const ShVariableNodePtr& node, ShSwizzle swizzle, bool neg)
00346   : ShVariable(node)
00347 {
00348   m_swizzle = swizzle;
00349   m_neg = neg;
00350   SH_DEBUG_ASSERT(node); // DEBUG
00351 }
00352 
00353 template<typename T>
00354 ShGeneric<1, T>::~ShGeneric()
00355 {
00356 }
00357 
00358 template<typename T>
00359 template<typename T2>
00360 ShGeneric<1, T>::ShGeneric(const ShGeneric<1, T2>& other)
00361   : ShVariable(new ShVariableNode(SH_TEMP, 1, value_type, other.node()->specialType()))
00362 {
00363   SH_DEBUG_ASSERT(other.node());
00364   SH_DEBUG_ASSERT(m_node);
00365   SH_DEBUG_PRINT(m_node->size() << " " << other.node()->size());
00366   shASN(*this, other);
00367 }
00368 
00369 template<typename T>
00370 ShGeneric<1, T>& ShGeneric<1, T>::operator=(const ShProgram& prg)
00371 {
00372   this->ShVariable::operator=(prg);
00373   return *this;
00374 }
00375 
00376 template<typename T>
00377 ShGeneric<1, T>& ShGeneric<1, T>::operator=(const ShGeneric<1, T>& other)
00378 {
00379   shASN(*this, other);
00380   return *this;
00381 }
00382 
00383 template<typename T>
00384 template<typename T2>
00385 ShGeneric<1, T>& ShGeneric<1, T>::operator=(const ShGeneric<1, T2>& other)
00386 {
00387   shASN(*this, other);
00388   return *this;
00389 }
00390 
00391 
00392 template<typename T>
00393 ShGeneric<1, T>& ShGeneric<1, T>::operator=(host_type other)
00394 {
00395   shASN(*this, ShAttrib<1, SH_CONST, T>(other));
00396   return *this;
00397 }
00398 
00399 template<typename T>
00400 ShGeneric<1, T>& ShGeneric<1, T>::operator++()
00401 {
00402   *this += 1;
00403   return *this;
00404 }
00405 
00406 template<typename T>
00407 ShGeneric<1, T>& ShGeneric<1, T>::operator--()
00408 {
00409   *this -= 1;
00410   return *this;
00411 }
00412 
00413 template<typename T>
00414 template<typename T2>
00415 ShGeneric<1, T>& ShGeneric<1, T>::operator+=(const ShGeneric<1, T2>& right)
00416 {
00417   *this = *this + right;
00418   return *this;
00419 }
00420 
00421 template<typename T>
00422 template<typename T2>
00423 ShGeneric<1, T>& ShGeneric<1, T>::operator-=(const ShGeneric<1, T2>& right)
00424 {
00425   *this = *this - right;
00426   return *this;
00427 }
00428 
00429 template<typename T>
00430 template<typename T2>
00431 ShGeneric<1, T>& ShGeneric<1, T>::operator*=(const ShGeneric<1, T2>& right)
00432 {
00433   *this = *this * right;
00434   return *this;
00435 }
00436 
00437 template<typename T>
00438 template<typename T2>
00439 ShGeneric<1, T>& ShGeneric<1, T>::operator/=(const ShGeneric<1, T2>& right)
00440 {
00441   *this = *this / right;
00442   return *this;
00443 }
00444 
00445 template<typename T>
00446 template<typename T2>
00447 ShGeneric<1, T>& ShGeneric<1, T>::operator%=(const ShGeneric<1, T2>& right)
00448 {
00449   *this = *this % right;
00450   return *this;
00451 }
00452 
00453 template<typename T>
00454 ShGeneric<1, T>& ShGeneric<1, T>::operator*=(host_type right)
00455 {
00456   *this = *this * right;
00457   return *this;
00458 }
00459 
00460 template<typename T>
00461 ShGeneric<1, T>& ShGeneric<1, T>::operator/=(host_type right)
00462 {
00463   *this = *this / right;
00464   return *this;
00465 }
00466 
00467 template<typename T>
00468 ShGeneric<1, T>& ShGeneric<1, T>::operator%=(host_type right)
00469 {
00470   *this = *this % right;
00471   return *this;
00472 }
00473 
00474 template<typename T>
00475 ShGeneric<1, T>& ShGeneric<1, T>::operator+=(host_type right)
00476 {
00477   *this = *this + right;
00478   return *this;
00479 }
00480 
00481 template<typename T>
00482 ShGeneric<1, T>& ShGeneric<1, T>::operator-=(host_type right)
00483 {
00484   *this = *this - right;
00485   return *this;
00486 }
00487 
00488 template<typename T>
00489 ShGeneric<1, T> ShGeneric<1, T>::operator-() const
00490 {
00491   return ShGeneric<1, T>(m_node, m_swizzle, !m_neg);
00492 }
00493 
00494 
00495 template<typename T>
00496 ShGeneric<1, T> ShGeneric<1, T>::operator()() const
00497 {
00498   return ShGeneric<1, T>(m_node, m_swizzle, m_neg);
00499 }
00500 
00501 template<typename T>
00502 ShGeneric<1, T> ShGeneric<1, T>::operator()(int i1) const
00503 {
00504   return ShGeneric<1, T>(m_node, m_swizzle * ShSwizzle(size(), i1), m_neg);
00505 }
00506 
00507 template<typename T>
00508 ShGeneric<1, T> ShGeneric<1, T>::operator[](int i1) const
00509 {
00510   return ShGeneric<1, T>(m_node, m_swizzle * ShSwizzle(size(), i1), m_neg);
00511 }
00512 
00513 template<typename T>
00514 ShGeneric<2, T> ShGeneric<1, T>::operator()(int i1, int i2) const
00515 {
00516   return ShGeneric<2, T>(m_node, m_swizzle * ShSwizzle(size(), i1, i2), m_neg);
00517 }
00518 
00519 template<typename T>
00520 ShGeneric<3, T> ShGeneric<1, T>::operator()(int i1, int i2, int i3) const
00521 {
00522   return ShGeneric<3, T>(m_node, m_swizzle * ShSwizzle(size(), i1, i2, i3), m_neg);
00523 }
00524 
00525 template<typename T>
00526 ShGeneric<4, T> ShGeneric<1, T>::operator()(int i1, int i2, int i3, int i4) const
00527 {
00528   return ShGeneric<4, T>(m_node, m_swizzle * ShSwizzle(size(), i1, i2, i3, i4), m_neg);
00529 }
00530 
00531 template<typename T>
00532 void ShGeneric<1, T>::range(host_type low, host_type high) 
00533 {
00534   rangeVariant(new VariantType(1, low), new VariantType(1, high));
00535 }
00536 
00537 template<typename T>
00538 typename ShGeneric<1, T>::VariantType ShGeneric<1, T>::lowBound() const
00539 {
00540   return (*variant_cast<T, SH_HOST>(lowBoundVariant()));
00541 }
00542 
00543 template<typename T>
00544 typename ShGeneric<1, T>::host_type ShGeneric<1, T>::lowBound(int index) const
00545 {
00546   return (*variant_cast<T, SH_HOST>(lowBoundVariant()))[index];
00547 }
00548 
00549 template<typename T>
00550 typename ShGeneric<1, T>::VariantType ShGeneric<1, T>::highBound() const
00551 {
00552   return (*variant_cast<T, SH_HOST>(highBoundVariant()));
00553 }
00554 
00555 template<typename T>
00556 typename ShGeneric<1, T>::host_type ShGeneric<1, T>::highBound(int index) const
00557 {
00558   return (*variant_cast<T, SH_HOST>(highBoundVariant()))[index];
00559 }
00560   
00561 template<typename T> 
00562 template<int N2>
00563 ShGeneric<N2, T> ShGeneric<1, T>::swiz(int indices[]) const
00564 {
00565   return ShGeneric<N2, T>(m_node, m_swizzle * ShSwizzle(1, N2, indices), m_neg);
00566 }
00567 
00568 template<typename T>
00569 void ShGeneric<1, T>::getValues(host_type dest[]) const
00570 {
00571   VariantTypePtr c = variant_cast<T, SH_HOST>(getVariant()); 
00572   dest[0] = (*c)[0]; 
00573 }
00574 
00575 template<typename T>
00576 typename ShGeneric<1, T>::host_type ShGeneric<1, T>::getValue(int index) const
00577 {
00578   VariantTypePtr c = variant_cast<T, SH_HOST>(getVariant(index)); 
00579   return (*c)[0];
00580 }
00581 
00582 template<typename T>
00583 void ShGeneric<1, T>::setValue(int index, const host_type &variantValue) 
00584 {
00585   VariantTypePtr variant(new VariantType(1, variantValue));
00586   setVariant(variant, false, ShSwizzle(1, index));
00587 }
00588 
00589 template<typename T>
00590 void ShGeneric<1, T>::setValues(const host_type variantValues[]) 
00591 {
00592   setVariant(new VariantType(1, variantValues[0]));
00593 }
00594 
00595 }
00596 
00597 #endif

Generated on Thu Apr 21 17:32:47 2005 for Sh by  doxygen 1.4.2