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