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