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 (c) 2003 University of Waterloo Computer Graphics Laboratory
00004 // Project administrator: Michael D. McCool
00005 // Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa,
00006 //          Michael D. McCool
00007 // 
00008 // This software is provided 'as-is', without any express or implied
00009 // warranty. In no event will the authors be held liable for any damages
00010 // arising from the use of this software.
00011 // 
00012 // Permission is granted to anyone to use this software for any purpose,
00013 // including commercial applications, and to alter it and redistribute it
00014 // freely, subject to the following restrictions:
00015 // 
00016 // 1. The origin of this software must not be misrepresented; you must
00017 // not claim that you wrote the original software. If you use this
00018 // software in a product, an acknowledgment in the product documentation
00019 // would be appreciated but is not required.
00020 // 
00021 // 2. Altered source versions must be plainly marked as such, and must
00022 // not be misrepresented as being the original software.
00023 // 
00024 // 3. This notice may not be removed or altered from any source
00025 // distribution.
00027 #ifndef SHGENERICIMPL_HPP
00028 #define SHGENERICIMPL_HPP
00029 
00030 #include "ShGeneric.hpp"
00031 #include "ShAttrib.hpp"
00032 #include "ShLib.hpp"
00033 #include "ShInstructions.hpp"
00034 #include "ShDebug.hpp"
00035 #include "ShProgram.hpp"
00036 
00037 namespace SH {
00038 
00039 template<int N, typename T>
00040 inline
00041 ShGeneric<N, T>::ShGeneric(const ShVariableNodePtr& node)
00042   : ShVariable(node)
00043 {
00044   SH_DEBUG_ASSERT(node); // DEBUG
00045 }
00046 
00047 template<int N, typename T>
00048 inline
00049 ShGeneric<N, T>::ShGeneric(const ShVariableNodePtr& node, ShSwizzle swizzle, bool neg)
00050   : ShVariable(node)
00051 {
00052   m_swizzle = swizzle;
00053   m_neg = neg;
00054   SH_DEBUG_ASSERT(node); // DEBUG
00055 }
00056 
00057 template<int N, typename T>
00058 inline
00059 ShGeneric<N, T>::~ShGeneric()
00060 {
00061 }
00062 
00063 //template<int N, typename T>
00064 //ShGeneric<N, T>::ShGeneric(const ShGeneric<N, T>& other)
00065 //  : ShVariable(new ShVariableNode(SH_TEMP, N, T, 
00066 //        other.node()->specialType()))
00067 //{
00068 //  SH_DEBUG_ASSERT(other.node());
00069 //  SH_DEBUG_ASSERT(m_node);
00070 //  shASN(*this, other);
00071 //}
00072 
00073 template<int N, typename T>
00074 template<typename T2>
00075 inline
00076 ShGeneric<N, T>::ShGeneric(const ShGeneric<N, T2>& other)
00077   : ShVariable(new ShVariableNode(SH_TEMP, N, value_type, 
00078         other.node()->specialType()))
00079 {
00080   SH_DEBUG_ASSERT(other.node());
00081   SH_DEBUG_ASSERT(m_node);
00082   shASN(*this, other);
00083 }
00084 
00085 template<int N, typename T>
00086 inline
00087 ShGeneric<N, T>& ShGeneric<N, T>::operator=(const ShProgram& prg)
00088 {
00089   this->ShVariable::operator=(prg);
00090   return *this;
00091 }
00092 
00093 template<int N, typename T>
00094 inline
00095 ShGeneric<N, T>& ShGeneric<N, T>::operator=(const ShGeneric<N, T>& other)
00096 {
00097   shASN(*this, other);
00098   return *this;
00099 }
00100 
00101 template<int N, typename T>
00102 template<typename T2>
00103 ShGeneric<N, T>& ShGeneric<N, T>::operator=(const ShGeneric<N, T2>& other)
00104 {
00105   shASN(*this, other);
00106   return *this;
00107 }
00108 
00109 template<int N, typename T>
00110 template<typename T2>
00111 inline
00112 ShGeneric<N, T>& ShGeneric<N, T>::operator+=(const ShGeneric<N, T2>& right)
00113 {
00114   *this = *this + right;
00115   return *this;
00116 }
00117 
00118 template<int N, typename T>
00119 template<typename T2>
00120 inline
00121 ShGeneric<N, T>& ShGeneric<N, T>::operator-=(const ShGeneric<N, T2>& right)
00122 {
00123   *this = *this - right;
00124   return *this;
00125 }
00126 
00127 template<int N, typename T>
00128 template<typename T2>
00129 inline
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 inline
00139 ShGeneric<N, T>& ShGeneric<N, T>::operator/=(const ShGeneric<N, T2>& right)
00140 {
00141   *this = *this / right;
00142   return *this;
00143 }
00144 
00145 template<int N, typename T>
00146 template<typename T2>
00147 inline
00148 ShGeneric<N, T>& ShGeneric<N, T>::operator%=(const ShGeneric<N, T2>& right)
00149 {
00150   *this = *this % right;
00151   return *this;
00152 }
00153 
00154 template<int N, typename T>
00155 template<typename T2>
00156 inline
00157 ShGeneric<N, T>& ShGeneric<N, T>::operator+=(const ShGeneric<1, T2>& right)
00158 {
00159   *this = *this + right;
00160   return *this;
00161 }
00162 
00163 template<int N, typename T>
00164 template<typename T2>
00165 inline
00166 ShGeneric<N, T>& ShGeneric<N, T>::operator-=(const ShGeneric<1, T2>& right)
00167 {
00168   *this = *this - right;
00169   return *this;
00170 }
00171 
00172 template<int N, typename T>
00173 template<typename T2>
00174 inline
00175 ShGeneric<N, T>& ShGeneric<N, T>::operator*=(const ShGeneric<1, T2>& right)
00176 {
00177   *this = *this * right;
00178   return *this;
00179 }
00180 
00181 template<int N, typename T>
00182 template<typename T2>
00183 inline
00184 ShGeneric<N, T>& ShGeneric<N, T>::operator/=(const ShGeneric<1, T2>& right)
00185 {
00186   *this = *this / right;
00187   return *this;
00188 }
00189 
00190 template<int N, typename T>
00191 template<typename T2>
00192 inline
00193 ShGeneric<N, T>& ShGeneric<N, T>::operator%=(const ShGeneric<1, T2>& right)
00194 {
00195   *this = *this % right;
00196   return *this;
00197 }
00198 
00199 template<int N, typename T>
00200 inline
00201 ShGeneric<N, T>& ShGeneric<N, T>::operator+=(host_type right)
00202 {
00203   *this = *this + right;
00204   return *this;
00205 }
00206 
00207 template<int N, typename T>
00208 inline
00209 ShGeneric<N, T>& ShGeneric<N, T>::operator-=(host_type right)
00210 {
00211   *this = *this - right;
00212   return *this;
00213 }
00214 
00215 template<int N, typename T>
00216 inline
00217 ShGeneric<N, T>& ShGeneric<N, T>::operator*=(host_type right)
00218 {
00219   *this = *this * right;
00220   return *this;
00221 }
00222 
00223 template<int N, typename T>
00224 inline
00225 ShGeneric<N, T>& ShGeneric<N, T>::operator/=(host_type right)
00226 {
00227   *this = *this / right;
00228   return *this;
00229 }
00230 
00231 template<int N, typename T>
00232 inline
00233 ShGeneric<N, T>& ShGeneric<N, T>::operator%=(host_type right)
00234 {
00235   *this = *this % right;
00236   return *this;
00237 }
00238 
00239 template<int N, typename T>
00240 inline
00241 ShGeneric<N, T> ShGeneric<N, T>::operator-() const
00242 {
00243   return ShGeneric<N, T>(m_node, m_swizzle, !m_neg);
00244 }
00245 
00246 
00247 template<int N, typename T>
00248 inline
00249 ShGeneric<N, T> ShGeneric<N, T>::operator()() const
00250 {
00251   return ShGeneric<N, T>(m_node, m_swizzle, m_neg);
00252 }
00253 
00254 template<int N, typename T>
00255 inline
00256 ShGeneric<1, T> ShGeneric<N, T>::operator()(int i1) const
00257 {
00258   return ShGeneric<1, T>(m_node, m_swizzle * ShSwizzle(size(), i1), m_neg);
00259 }
00260 
00261 template<int N, typename T>
00262 inline
00263 ShGeneric<1, T> ShGeneric<N, T>::operator[](int i1) const
00264 {
00265   return ShGeneric<1, T>(m_node, m_swizzle * ShSwizzle(size(), i1), m_neg);
00266 }
00267 
00268 template<int N, typename T>
00269 inline
00270 ShGeneric<2, T> ShGeneric<N, T>::operator()(int i1, int i2) const
00271 {
00272   return ShGeneric<2, T>(m_node, m_swizzle * ShSwizzle(size(), i1, i2), m_neg);
00273 }
00274 
00275 template<int N, typename T>
00276 inline
00277 ShGeneric<3, T> ShGeneric<N, T>::operator()(int i1, int i2, int i3) const
00278 {
00279   return ShGeneric<3, T>(m_node, m_swizzle * ShSwizzle(size(), i1, i2, i3), m_neg);
00280 }
00281 
00282 template<int N, typename T>
00283 inline
00284 ShGeneric<4, T> ShGeneric<N, T>::operator()(int i1, int i2, int i3, int i4) const
00285 {
00286   return ShGeneric<4, T>(m_node, m_swizzle * ShSwizzle(size(), i1, i2, i3, i4), m_neg);
00287 }
00288 
00289 template<int N, typename T>
00290 void ShGeneric<N, T>::range(host_type low, host_type high) 
00291 {
00292   rangeVariant(new VariantType(1, low), new VariantType(1, high));
00293 }
00294 
00295 template<int N, typename T>
00296 typename ShGeneric<N, T>::VariantType ShGeneric<N, T>::lowBound() const
00297 {
00298   return (*variant_cast<T, SH_HOST>(lowBoundVariant()));
00299 }
00300 
00301 template<int N, typename T>
00302 typename ShGeneric<N, T>::host_type ShGeneric<N, T>::lowBound(int index) const
00303 {
00304   return (*variant_cast<T, SH_HOST>(lowBoundVariant()))[index];
00305 }
00306 
00307 template<int N, typename T>
00308 typename ShGeneric<N, T>::VariantType ShGeneric<N, T>::highBound() const
00309 {
00310   return (*variant_cast<T, SH_HOST>(highBoundVariant()));
00311 }
00312 
00313 template<int N, typename T>
00314 typename ShGeneric<N, T>::host_type ShGeneric<N, T>::highBound(int index) const
00315 {
00316   return (*variant_cast<T, SH_HOST>(highBoundVariant()))[index];
00317 }
00318   
00319 template<int N, typename T> 
00320 template<int N2>
00321 ShGeneric<N2, T> ShGeneric<N, T>::swiz(int indices[]) const
00322 {
00323   return ShGeneric<N2, T>(m_node, m_swizzle * ShSwizzle(N, N2, indices), m_neg);
00324 }
00325 
00326 template<int N, typename T>
00327 void ShGeneric<N, T>::getValues(host_type dest[]) const
00328 {
00329   VariantTypePtr c = variant_cast<T, SH_HOST>(getVariant()); 
00330   for(int i = 0; i < N; ++i) dest[i] = (*c)[i]; 
00331 }
00332 
00333 template<int N, typename T>
00334 typename ShGeneric<N, T>::host_type ShGeneric<N, T>::getValue(int index) const
00335 {
00336   VariantTypePtr c = variant_cast<T, SH_HOST>(getVariant(index)); 
00337   return (*c)[0];
00338 }
00339 
00340 template<int N, typename T>
00341 void ShGeneric<N, T>::setValue(int index, const host_type &variantValue) 
00342 {
00343   if(m_swizzle.identity() && !m_neg) {
00344     VariantTypePtr c = variant_cast<T, SH_HOST>(m_node->getVariant()); 
00345     (*c)[index] = variantValue;
00346   } else {
00347     VariantTypePtr variant(new VariantType(1, variantValue));
00348     setVariant(variant, false, ShSwizzle(N, index));
00349   }
00350 }
00351 
00352 template<int N, typename T>
00353 void ShGeneric<N, T>::setValues(const host_type variantValues[]) 
00354 {
00355   if(m_swizzle.identity() && !m_neg) {
00356     memcpy(m_node->getVariant()->array(), variantValues, N * sizeof(host_type));
00357   } else {
00358     VariantTypePtr variantPtr(new VariantType(N, variantValues, false));
00359     setVariant(variantPtr);
00360   }
00361 }
00362 
00363 template<typename T>
00364 inline
00365 ShGeneric<1, T>::ShGeneric(const ShVariableNodePtr& node)
00366   : ShVariable(node)
00367 {
00368   SH_DEBUG_ASSERT(node); // DEBUG
00369 }
00370 
00371 template<typename T>
00372 inline
00373 ShGeneric<1, T>::ShGeneric(const ShVariableNodePtr& node, ShSwizzle swizzle, bool neg)
00374   : ShVariable(node)
00375 {
00376   m_swizzle = swizzle;
00377   m_neg = neg;
00378   SH_DEBUG_ASSERT(node); // DEBUG
00379 }
00380 
00381 template<typename T>
00382 inline
00383 ShGeneric<1, T>::~ShGeneric()
00384 {
00385 }
00386 
00387 //template<typename T>
00388 //ShGeneric<1, T>::ShGeneric(const ShGeneric<1, T>& other)
00389 //  : ShVariable(new ShVariableNode(SH_TEMP, 1, T, 
00390 //        other.node()->specialType()))
00391 //{
00392 //  SH_DEBUG_ASSERT(other.node());
00393 //  SH_DEBUG_ASSERT(m_node);
00394 //  SH_DEBUG_PRINT(m_node->size() << " " << other.node()->size());
00395 //  shASN(*this, other);
00396 //}
00397 
00398 template<typename T>
00399 template<typename T2>
00400 inline
00401 ShGeneric<1, T>::ShGeneric(const ShGeneric<1, T2>& other)
00402   : ShVariable(new ShVariableNode(SH_TEMP, 1, value_type, other.node()->specialType()))
00403 {
00404   SH_DEBUG_ASSERT(other.node());
00405   SH_DEBUG_ASSERT(m_node);
00406   SH_DEBUG_PRINT(m_node->size() << " " << other.node()->size());
00407   shASN(*this, other);
00408 }
00409 
00410 template<typename T>
00411 inline
00412 ShGeneric<1, T>& ShGeneric<1, T>::operator=(const ShProgram& prg)
00413 {
00414   this->ShVariable::operator=(prg);
00415   return *this;
00416 }
00417 
00418 template<typename T>
00419 inline
00420 ShGeneric<1, T>& ShGeneric<1, T>::operator=(const ShGeneric<1, T>& other)
00421 {
00422   shASN(*this, other);
00423   return *this;
00424 }
00425 
00426 template<typename T>
00427 template<typename T2>
00428 inline
00429 ShGeneric<1, T>& ShGeneric<1, T>::operator=(const ShGeneric<1, T2>& other)
00430 {
00431   shASN(*this, other);
00432   return *this;
00433 }
00434 
00435 
00436 template<typename T>
00437 inline
00438 ShGeneric<1, T>& ShGeneric<1, T>::operator=(host_type other)
00439 {
00440   shASN(*this, ShAttrib<1, SH_CONST, T>(other));
00441   return *this;
00442 }
00443 
00444 template<typename T>
00445 template<typename T2>
00446 inline
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 template<typename T2>
00455 inline
00456 ShGeneric<1, T>& ShGeneric<1, T>::operator-=(const ShGeneric<1, T2>& right)
00457 {
00458   *this = *this - right;
00459   return *this;
00460 }
00461 
00462 template<typename T>
00463 template<typename T2>
00464 inline
00465 ShGeneric<1, T>& ShGeneric<1, T>::operator*=(const ShGeneric<1, T2>& right)
00466 {
00467   *this = *this * right;
00468   return *this;
00469 }
00470 
00471 template<typename T>
00472 template<typename T2>
00473 inline
00474 ShGeneric<1, T>& ShGeneric<1, T>::operator/=(const ShGeneric<1, T2>& right)
00475 {
00476   *this = *this / right;
00477   return *this;
00478 }
00479 
00480 template<typename T>
00481 template<typename T2>
00482 inline
00483 ShGeneric<1, T>& ShGeneric<1, T>::operator%=(const ShGeneric<1, T2>& right)
00484 {
00485   *this = *this % right;
00486   return *this;
00487 }
00488 
00489 template<typename T>
00490 inline
00491 ShGeneric<1, T>& ShGeneric<1, T>::operator*=(host_type right)
00492 {
00493   *this = *this * right;
00494   return *this;
00495 }
00496 
00497 template<typename T>
00498 inline
00499 ShGeneric<1, T>& ShGeneric<1, T>::operator/=(host_type right)
00500 {
00501   *this = *this / right;
00502   return *this;
00503 }
00504 
00505 template<typename T>
00506 inline
00507 ShGeneric<1, T>& ShGeneric<1, T>::operator%=(host_type right)
00508 {
00509   *this = *this % right;
00510   return *this;
00511 }
00512 
00513 template<typename T>
00514 inline
00515 ShGeneric<1, T>& ShGeneric<1, T>::operator+=(host_type right)
00516 {
00517   *this = *this + right;
00518   return *this;
00519 }
00520 
00521 template<typename T>
00522 inline
00523 ShGeneric<1, T>& ShGeneric<1, T>::operator-=(host_type right)
00524 {
00525   *this = *this - right;
00526   return *this;
00527 }
00528 
00529 template<typename T>
00530 inline
00531 ShGeneric<1, T> ShGeneric<1, T>::operator-() const
00532 {
00533   return ShGeneric<1, T>(m_node, m_swizzle, !m_neg);
00534 }
00535 
00536 
00537 template<typename T>
00538 inline
00539 ShGeneric<1, T> ShGeneric<1, T>::operator()() const
00540 {
00541   return ShGeneric<1, T>(m_node, m_swizzle, m_neg);
00542 }
00543 
00544 template<typename T>
00545 inline
00546 ShGeneric<1, T> ShGeneric<1, T>::operator()(int i1) const
00547 {
00548   return ShGeneric<1, T>(m_node, m_swizzle * ShSwizzle(size(), i1), m_neg);
00549 }
00550 
00551 template<typename T>
00552 inline
00553 ShGeneric<1, T> ShGeneric<1, T>::operator[](int i1) const
00554 {
00555   return ShGeneric<1, T>(m_node, m_swizzle * ShSwizzle(size(), i1), m_neg);
00556 }
00557 
00558 template<typename T>
00559 inline
00560 ShGeneric<2, T> ShGeneric<1, T>::operator()(int i1, int i2) const
00561 {
00562   return ShGeneric<2, T>(m_node, m_swizzle * ShSwizzle(size(), i1, i2), m_neg);
00563 }
00564 
00565 template<typename T>
00566 inline
00567 ShGeneric<3, T> ShGeneric<1, T>::operator()(int i1, int i2, int i3) const
00568 {
00569   return ShGeneric<3, T>(m_node, m_swizzle * ShSwizzle(size(), i1, i2, i3), m_neg);
00570 }
00571 
00572 template<typename T>
00573 inline
00574 ShGeneric<4, T> ShGeneric<1, T>::operator()(int i1, int i2, int i3, int i4) const
00575 {
00576   return ShGeneric<4, T>(m_node, m_swizzle * ShSwizzle(size(), i1, i2, i3, i4), m_neg);
00577 }
00578 
00579 template<typename T>
00580 void ShGeneric<1, T>::range(host_type low, host_type high) 
00581 {
00582   rangeVariant(new VariantType(1, low), new VariantType(1, high));
00583 }
00584 
00585 template<typename T>
00586 typename ShGeneric<1, T>::VariantType ShGeneric<1, T>::lowBound() const
00587 {
00588   return (*variant_cast<T, SH_HOST>(lowBoundVariant()));
00589 }
00590 
00591 template<typename T>
00592 typename ShGeneric<1, T>::host_type ShGeneric<1, T>::lowBound(int index) const
00593 {
00594   return (*variant_cast<T, SH_HOST>(lowBoundVariant()))[index];
00595 }
00596 
00597 template<typename T>
00598 typename ShGeneric<1, T>::VariantType ShGeneric<1, T>::highBound() const
00599 {
00600   return (*variant_cast<T, SH_HOST>(highBoundVariant()));
00601 }
00602 
00603 template<typename T>
00604 typename ShGeneric<1, T>::host_type ShGeneric<1, T>::highBound(int index) const
00605 {
00606   return (*variant_cast<T, SH_HOST>(highBoundVariant()))[index];
00607 }
00608   
00609 template<typename T> 
00610 template<int N2>
00611 ShGeneric<N2, T> ShGeneric<1, T>::swiz(int indices[]) const
00612 {
00613   return ShGeneric<N2, T>(m_node, m_swizzle * ShSwizzle(1, N2, indices), m_neg);
00614 }
00615 
00616 template<typename T>
00617 void ShGeneric<1, T>::getValues(host_type dest[]) const
00618 {
00619   VariantTypePtr c = variant_cast<T, SH_HOST>(getVariant()); 
00620   dest[0] = (*c)[0]; 
00621 }
00622 
00623 template<typename T>
00624 typename ShGeneric<1, T>::host_type ShGeneric<1, T>::getValue(int index) const
00625 {
00626   VariantTypePtr c = variant_cast<T, SH_HOST>(getVariant(index)); 
00627   return (*c)[0];
00628 }
00629 
00630 template<typename T>
00631 void ShGeneric<1, T>::setValue(int index, const host_type &variantValue) 
00632 {
00633   VariantTypePtr variant(new VariantType(1, variantValue));
00634   setVariant(variant, false, ShSwizzle(1, index));
00635 }
00636 
00637 template<typename T>
00638 void ShGeneric<1, T>::setValues(const host_type variantValues[]) 
00639 {
00640   setVariant(new VariantType(1, variantValues[0]));
00641 }
00642 
00643 }
00644 
00645 #endif

Generated on Mon Jan 24 18:36:32 2005 for Sh by  doxygen 1.4.1