00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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);
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);
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 variantPtr(new VariantType(1, variantValue));
00321 setVariant(variantPtr, 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 c(variant_cast<T, SH_HOST>(m_node->getVariant()));
00330 for (int i=0; i < N; i++) {
00331 (*c)[i] = variantValues[i];
00332 }
00333 } else {
00334 host_type* data = const_cast<host_type*>(variantValues);
00335 VariantTypePtr variantPtr(new VariantType(N, data, false));
00336 setVariant(variantPtr);
00337 }
00338 }
00339
00340 template<typename T>
00341 ShGeneric<1, T>::ShGeneric(const ShVariableNodePtr& node)
00342 : ShVariable(node)
00343 {
00344 SH_DEBUG_ASSERT(node);
00345 }
00346
00347 template<typename T>
00348 ShGeneric<1, T>::ShGeneric(const ShVariableNodePtr& node, ShSwizzle swizzle, bool neg)
00349 : ShVariable(node)
00350 {
00351 m_swizzle = swizzle;
00352 m_neg = neg;
00353 SH_DEBUG_ASSERT(node);
00354 }
00355
00356 template<typename T>
00357 ShGeneric<1, T>::~ShGeneric()
00358 {
00359 }
00360
00361 template<typename T>
00362 template<typename T2>
00363 ShGeneric<1, T>::ShGeneric(const ShGeneric<1, T2>& other)
00364 : ShVariable(new ShVariableNode(SH_TEMP, 1, value_type, other.node()->specialType()))
00365 {
00366 SH_DEBUG_ASSERT(other.node());
00367 SH_DEBUG_ASSERT(m_node);
00368 shASN(*this, other);
00369 }
00370
00371 template<typename T>
00372 ShGeneric<1, T>& ShGeneric<1, T>::operator=(const ShProgram& prg)
00373 {
00374 this->ShVariable::operator=(prg);
00375 return *this;
00376 }
00377
00378 template<typename T>
00379 ShGeneric<1, T>& ShGeneric<1, T>::operator=(const ShGeneric<1, T>& other)
00380 {
00381 shASN(*this, other);
00382 return *this;
00383 }
00384
00385 template<typename T>
00386 template<typename T2>
00387 ShGeneric<1, T>& ShGeneric<1, T>::operator=(const ShGeneric<1, T2>& other)
00388 {
00389 shASN(*this, other);
00390 return *this;
00391 }
00392
00393
00394 template<typename T>
00395 ShGeneric<1, T>& ShGeneric<1, T>::operator=(host_type other)
00396 {
00397 shASN(*this, ShAttrib<1, SH_CONST, T>(other));
00398 return *this;
00399 }
00400
00401 template<typename T>
00402 ShGeneric<1, T>& ShGeneric<1, T>::operator++()
00403 {
00404 *this += 1;
00405 return *this;
00406 }
00407
00408 template<typename T>
00409 ShGeneric<1, T>& ShGeneric<1, T>::operator--()
00410 {
00411 *this -= 1;
00412 return *this;
00413 }
00414
00415 template<typename T>
00416 template<typename T2>
00417 ShGeneric<1, T>& ShGeneric<1, T>::operator+=(const ShGeneric<1, T2>& right)
00418 {
00419 *this = *this + right;
00420 return *this;
00421 }
00422
00423 template<typename T>
00424 template<typename T2>
00425 ShGeneric<1, T>& ShGeneric<1, T>::operator-=(const ShGeneric<1, T2>& right)
00426 {
00427 *this = *this - right;
00428 return *this;
00429 }
00430
00431 template<typename T>
00432 template<typename T2>
00433 ShGeneric<1, T>& ShGeneric<1, T>::operator*=(const ShGeneric<1, T2>& right)
00434 {
00435 *this = *this * right;
00436 return *this;
00437 }
00438
00439 template<typename T>
00440 template<typename T2>
00441 ShGeneric<1, T>& ShGeneric<1, T>::operator/=(const ShGeneric<1, T2>& right)
00442 {
00443 *this = *this / right;
00444 return *this;
00445 }
00446
00447 template<typename T>
00448 template<typename T2>
00449 ShGeneric<1, T>& ShGeneric<1, T>::operator%=(const ShGeneric<1, T2>& right)
00450 {
00451 *this = *this % right;
00452 return *this;
00453 }
00454
00455 template<typename T>
00456 ShGeneric<1, T>& ShGeneric<1, T>::operator*=(host_type right)
00457 {
00458 *this = *this * right;
00459 return *this;
00460 }
00461
00462 template<typename T>
00463 ShGeneric<1, T>& ShGeneric<1, T>::operator/=(host_type right)
00464 {
00465 *this = *this / right;
00466 return *this;
00467 }
00468
00469 template<typename T>
00470 ShGeneric<1, T>& ShGeneric<1, T>::operator%=(host_type right)
00471 {
00472 *this = *this % right;
00473 return *this;
00474 }
00475
00476 template<typename T>
00477 ShGeneric<1, T>& ShGeneric<1, T>::operator+=(host_type right)
00478 {
00479 *this = *this + right;
00480 return *this;
00481 }
00482
00483 template<typename T>
00484 ShGeneric<1, T>& ShGeneric<1, T>::operator-=(host_type right)
00485 {
00486 *this = *this - right;
00487 return *this;
00488 }
00489
00490 template<typename T>
00491 ShGeneric<1, T> ShGeneric<1, T>::operator-() const
00492 {
00493 return ShGeneric<1, T>(m_node, m_swizzle, !m_neg);
00494 }
00495
00496
00497 template<typename T>
00498 ShGeneric<1, T> ShGeneric<1, T>::operator()() const
00499 {
00500 return ShGeneric<1, T>(m_node, m_swizzle, m_neg);
00501 }
00502
00503 template<typename T>
00504 ShGeneric<1, T> ShGeneric<1, T>::operator()(int i1) const
00505 {
00506 return ShGeneric<1, T>(m_node, m_swizzle * ShSwizzle(size(), i1), m_neg);
00507 }
00508
00509 template<typename T>
00510 ShGeneric<1, T> ShGeneric<1, T>::operator[](int i1) const
00511 {
00512 return ShGeneric<1, T>(m_node, m_swizzle * ShSwizzle(size(), i1), m_neg);
00513 }
00514
00515 template<typename T>
00516 ShGeneric<2, T> ShGeneric<1, T>::operator()(int i1, int i2) const
00517 {
00518 return ShGeneric<2, T>(m_node, m_swizzle * ShSwizzle(size(), i1, i2), m_neg);
00519 }
00520
00521 template<typename T>
00522 ShGeneric<3, T> ShGeneric<1, T>::operator()(int i1, int i2, int i3) const
00523 {
00524 return ShGeneric<3, T>(m_node, m_swizzle * ShSwizzle(size(), i1, i2, i3), m_neg);
00525 }
00526
00527 template<typename T>
00528 ShGeneric<4, T> ShGeneric<1, T>::operator()(int i1, int i2, int i3, int i4) const
00529 {
00530 return ShGeneric<4, T>(m_node, m_swizzle * ShSwizzle(size(), i1, i2, i3, i4), m_neg);
00531 }
00532
00533 template<typename T>
00534 void ShGeneric<1, T>::range(host_type low, host_type high)
00535 {
00536 rangeVariant(new VariantType(1, low), new VariantType(1, high));
00537 }
00538
00539 template<typename T>
00540 typename ShGeneric<1, T>::VariantType ShGeneric<1, T>::lowBound() const
00541 {
00542 return (*variant_cast<T, SH_HOST>(lowBoundVariant()));
00543 }
00544
00545 template<typename T>
00546 typename ShGeneric<1, T>::host_type ShGeneric<1, T>::lowBound(int index) const
00547 {
00548 return (*variant_cast<T, SH_HOST>(lowBoundVariant()))[index];
00549 }
00550
00551 template<typename T>
00552 typename ShGeneric<1, T>::VariantType ShGeneric<1, T>::highBound() const
00553 {
00554 return (*variant_cast<T, SH_HOST>(highBoundVariant()));
00555 }
00556
00557 template<typename T>
00558 typename ShGeneric<1, T>::host_type ShGeneric<1, T>::highBound(int index) const
00559 {
00560 return (*variant_cast<T, SH_HOST>(highBoundVariant()))[index];
00561 }
00562
00563 template<typename T>
00564 template<int N2>
00565 ShGeneric<N2, T> ShGeneric<1, T>::swiz(int indices[]) const
00566 {
00567 return ShGeneric<N2, T>(m_node, m_swizzle * ShSwizzle(1, N2, indices), m_neg);
00568 }
00569
00570 template<typename T>
00571 void ShGeneric<1, T>::getValues(host_type dest[]) const
00572 {
00573 VariantTypePtr c = variant_cast<T, SH_HOST>(getVariant());
00574 dest[0] = (*c)[0];
00575 }
00576
00577 template<typename T>
00578 typename ShGeneric<1, T>::host_type ShGeneric<1, T>::getValue(int index) const
00579 {
00580 VariantTypePtr c = variant_cast<T, SH_HOST>(getVariant(index));
00581 return (*c)[0];
00582 }
00583
00584 template<typename T>
00585 void ShGeneric<1, T>::setValue(int index, const host_type &variantValue)
00586 {
00587 VariantTypePtr variant(new VariantType(1, variantValue));
00588 setVariant(variant, false, ShSwizzle(1, index));
00589 }
00590
00591 template<typename T>
00592 void ShGeneric<1, T>::setValues(const host_type variantValues[])
00593 {
00594 setVariant(new VariantType(1, variantValues[0]));
00595 }
00596
00597 }
00598
00599 #endif