21 #include <type_traits>
201 #if (VTKM_SIZE_LONG_LONG == 8) || defined(VTKM_DOXYGEN_ONLY)
202 using Int64 =
signed long long;
208 #define VTKM_UNUSED_INT_TYPE long
209 #elif VTKM_SIZE_LONG == 8
210 using Int64 =
signed long;
215 using UInt64 =
unsigned long;
216 #define VTKM_UNUSED_INT_TYPE long long
218 #error Could not find a 64-bit integer.
226 #ifdef VTKM_USE_64BIT_IDS
233 #ifdef VTKM_USE_DOUBLE_PRECISION
256 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
257 #pragma GCC diagnostic push
258 #pragma GCC diagnostic ignored "-Wconversion"
259 #endif // gcc || clang
262 template <
typename T,
typename U>
271 template <
typename T>
273 typename std::enable_if<std::is_integral<T>::value &&
sizeof(T) <
sizeof(
int), T>::type
276 return static_cast<T
>(a + b);
282 template <
typename T,
typename U>
291 template <
typename T>
293 typename std::enable_if<std::is_integral<T>::value &&
sizeof(T) <
sizeof(
int), T>::type
296 return static_cast<T
>(a - b);
302 template <
typename T,
typename U>
311 template <
typename T>
313 typename std::enable_if<std::is_integral<T>::value &&
sizeof(T) <
sizeof(
int), T>::type
316 return static_cast<T
>(a * b);
322 template <
typename T,
typename U>
331 template <
typename T>
333 typename std::enable_if<std::is_integral<T>::value &&
sizeof(T) <
sizeof(
int), T>::type
336 return static_cast<T
>(a / b);
342 template <
typename T>
349 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
350 #pragma GCC diagnostic pop
351 #endif // gcc || clang
356 template <
typename T, vtkm::IdComponent Size>
359 template <
typename T>
362 template <
typename T>
380 #if (defined(VTKM_CUDA) && (__CUDACC_VER_MAJOR__ < 8))
381 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
382 #pragma GCC diagnostic push
383 #pragma GCC diagnostic ignored "-Wunknown-pragmas"
384 #pragma GCC diagnostic ignored "-Wpragmas"
385 #pragma GCC diagnostic ignored "-Wconversion"
386 #pragma GCC diagnostic ignored "-Wfloat-conversion"
387 #endif // gcc || clang
388 #endif // use cuda < 8
389 template <
typename T,
typename DerivedClass>
396 VecBaseCommon() =
default;
399 const DerivedClass& Derived()
const {
return *
static_cast<const DerivedClass*
>(
this); }
402 DerivedClass& Derived() {
return *
static_cast<DerivedClass*
>(
this); }
407 inline vtkm::IdComponent NumComponents()
const {
return this->Derived().GetNumberOfComponents(); }
411 inline const T& Component(
vtkm::IdComponent index)
const {
return this->Derived()[index]; }
418 template <vtkm::IdComponent OtherSize>
421 for (
vtkm::IdComponent index = 0; (index < this->NumComponents()) && (index < OtherSize);
424 dest[index] = this->Component(index);
429 template <
typename OtherVecType>
432 VTKM_ASSERT(this->NumComponents() == src.GetNumberOfComponents());
435 this->Component(i) = src[i];
437 return this->Derived();
441 bool operator==(
const DerivedClass& other)
const
446 equal = (this->Component(i) == other[i]);
452 bool operator<(
const DerivedClass& other)
const
457 if (this->Component(i) < other[i])
461 else if (other[i] < this->Component(i))
473 #if (!(defined(VTKM_CUDA) && (__CUDACC_VER_MAJOR__ < 8)))
474 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
475 #pragma GCC diagnostic push
476 #pragma GCC diagnostic ignored "-Wunknown-pragmas"
477 #pragma GCC diagnostic ignored "-Wpragmas"
478 #pragma GCC diagnostic ignored "-Wconversion"
479 #pragma GCC diagnostic ignored "-Wfloat-conversion"
480 #endif // gcc || clang
481 #endif // not using cuda < 8
483 template <vtkm::IdComponent Size>
491 result[i] = this->Component(i) + other[i];
496 template <
typename OtherClass>
497 inline VTKM_EXEC_CONT DerivedClass& operator+=(
const OtherClass& other)
499 VTKM_ASSERT(this->NumComponents() == other.GetNumberOfComponents());
502 this->Component(i) += other[i];
504 return this->Derived();
507 template <vtkm::IdComponent Size>
515 result[i] = this->Component(i) - other[i];
520 template <
typename OtherClass>
521 inline VTKM_EXEC_CONT DerivedClass& operator-=(
const OtherClass& other)
523 VTKM_ASSERT(this->NumComponents() == other.GetNumberOfComponents());
526 this->Component(i) -= other[i];
528 return this->Derived();
531 template <vtkm::IdComponent Size>
538 result[i] = this->Component(i) * other[i];
543 template <
typename OtherClass>
544 inline VTKM_EXEC_CONT DerivedClass& operator*=(
const OtherClass& other)
546 VTKM_ASSERT(this->NumComponents() == other.GetNumberOfComponents());
549 this->Component(i) *= other[i];
551 return this->Derived();
554 template <vtkm::IdComponent Size>
561 result[i] = this->Component(i) / other[i];
566 template <
typename OtherClass>
569 VTKM_ASSERT(this->NumComponents() == other.GetNumberOfComponents());
572 this->Component(i) /= other[i];
574 return this->Derived();
577 #if (!(defined(VTKM_CUDA) && (__CUDACC_VER_MAJOR__ < 8)))
578 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
579 #pragma GCC diagnostic pop
580 #endif // gcc || clang
581 #endif // not using cuda < 8
584 ComponentType* GetPointer() {
return &this->Component(0); }
587 const ComponentType* GetPointer()
const {
return &this->Component(0); }
593 template <
typename T, vtkm::IdComponent Size,
typename DerivedClass>
594 class VTKM_ALWAYS_EXPORT VecBase :
public vtkm::detail::VecBaseCommon<T, DerivedClass>
597 using ComponentType = T;
605 template <vtkm::IdComponent Size2 = Size, typename std::enable_if<Size2 != 1, int>::type = 0>
610 this->Components[i] = value;
615 template <
typename... Ts>
616 VTKM_EXEC_CONT constexpr VecBase(ComponentType value0, Ts&&... values)
617 : Components{ value0, values... }
624 VecBase(std::initializer_list<ComponentType> values)
626 ComponentType* dest = this->Components;
627 auto src = values.begin();
628 if (values.size() == 1)
632 this->Components[i] = *src;
639 "Vec object initialized wrong number of components.");
640 for (; src != values.end(); ++src)
648 #if (!(defined(VTKM_CUDA) && (__CUDACC_VER_MAJOR__ < 8)))
649 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
650 #pragma GCC diagnostic push
651 #pragma GCC diagnostic ignored "-Wunknown-pragmas"
652 #pragma GCC diagnostic ignored "-Wpragmas"
653 #pragma GCC diagnostic ignored "-Wconversion"
654 #pragma GCC diagnostic ignored "-Wfloat-conversion"
655 #endif // gcc || clang
656 #endif //not using cuda < 8
657 #if defined(VTKM_MSVC)
658 #pragma warning(push)
659 #pragma warning(disable : 4244)
663 template <
typename OtherValueType,
typename OtherDerivedType>
664 VTKM_EXEC_CONT explicit VecBase(
const VecBase<OtherValueType, Size, OtherDerivedType>& src)
670 this->Components[i] = src[i];
677 return NUM_COMPONENTS;
682 return this->Components[idx];
689 return this->Components[idx];
693 template <
typename OtherComponentType,
typename OtherClass>
695 operator+(
const VecBaseCommon<OtherComponentType, OtherClass>& other)
const
697 const OtherClass& other_derived =
static_cast<const OtherClass&
>(other);
698 VTKM_ASSERT(NUM_COMPONENTS == other_derived.GetNumberOfComponents());
703 result[i] = this->Components[i] +
static_cast<ComponentType
>(other_derived[i]);
709 template <
typename OtherComponentType,
typename OtherClass>
711 operator-(
const VecBaseCommon<OtherComponentType, OtherClass>& other)
const
713 const OtherClass& other_derived =
static_cast<const OtherClass&
>(other);
714 VTKM_ASSERT(NUM_COMPONENTS == other_derived.GetNumberOfComponents());
719 result[i] = this->Components[i] -
static_cast<ComponentType
>(other_derived[i]);
725 template <
typename OtherComponentType,
typename OtherClass>
727 operator*(
const VecBaseCommon<OtherComponentType, OtherClass>& other)
const
729 const OtherClass& other_derived =
static_cast<const OtherClass&
>(other);
730 VTKM_ASSERT(NUM_COMPONENTS == other_derived.GetNumberOfComponents());
735 result[i] = this->Components[i] *
static_cast<ComponentType
>(other_derived[i]);
741 template <
typename OtherComponentType,
typename OtherClass>
743 operator/(
const VecBaseCommon<OtherComponentType, OtherClass>& other)
const
745 const OtherClass& other_derived =
static_cast<const OtherClass&
>(other);
746 VTKM_ASSERT(NUM_COMPONENTS == other_derived.GetNumberOfComponents());
751 result[i] = this->Components[i] /
static_cast<ComponentType
>(other_derived[i]);
756 #if (!(defined(VTKM_CUDA) && (__CUDACC_VER_MAJOR__ < 8)))
757 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
758 #pragma GCC diagnostic pop
759 #endif // gcc || clang
760 #endif // not using cuda < 8
761 #if defined(VTKM_MSVC)
766 ComponentType Components[NUM_COMPONENTS];
769 #if (defined(VTKM_CUDA) && (__CUDACC_VER_MAJOR__ < 8))
770 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
771 #pragma GCC diagnostic pop
772 #endif // gcc || clang
773 #endif // use cuda < 8
777 template <
typename T,
typename DerivedClass>
778 class VTKM_ALWAYS_EXPORT VecCBase :
public vtkm::detail::VecBaseCommon<T, DerivedClass>
804 template <
typename T, vtkm::IdComponent Size>
810 #ifdef VTKM_DOXYGEN_ONLY
815 using Superclass::Superclass;
817 #if defined(_MSC_VER) && _MSC_VER < 1910
818 template <
typename... Ts>
819 constexpr
Vec(T value, Ts&&... values)
820 :
Superclass(value, std::forward<Ts>(values)...)
834 template <
typename T>
844 template <
typename OtherType>
857 return NUM_COMPONENTS;
874 template <
typename T>
886 template <
typename OtherType>
896 template <
typename T>
908 template <
typename OtherType>
915 constexpr
Vec(
const T& x,
const T& y)
982 #ifdef VTKM_USE_64BIT_IDS
1012 template <
typename T>
1024 template <
typename OtherType>
1031 constexpr
Vec(
const T& x,
const T& y,
const T& z)
1100 #ifdef VTKM_USE_64BIT_IDS
1130 template <
typename T>
1142 template <
typename OtherType>
1149 constexpr
Vec(
const T& x,
const T& y,
const T& z,
const T& w)
1216 #ifdef VTKM_USE_64BIT_IDS
1249 template <
typename T,
typename... Ts>
1274 template <
typename T>
1280 "You cannot use VecC with a const type as its template argument. "
1281 "Use either const VecC or VecCConst.");
1284 #ifdef VTKM_DOXYGEN_ONLY
1290 : Components(nullptr)
1291 , NumberOfComponents(0)
1298 , NumberOfComponents(size)
1302 template <vtkm::IdComponent Size>
1304 : Components(src.GetPointer())
1305 , NumberOfComponents(Size)
1312 , NumberOfComponents(1)
1318 : Components(src.Components)
1319 , NumberOfComponents(src.NumberOfComponents)
1327 return this->Components[index];
1334 return this->Components[index];
1339 return this->NumberOfComponents;
1348 (*this)[index] = src[index];
1368 template <
typename T>
1374 "You cannot use VecCConst with a const type as its template argument. "
1375 "Remove the const from the type.");
1378 #ifdef VTKM_DOXYGEN_ONLY
1384 : Components(nullptr)
1385 , NumberOfComponents(0)
1392 , NumberOfComponents(size)
1396 template <vtkm::IdComponent Size>
1398 : Components(src.GetPointer())
1399 , NumberOfComponents(Size)
1406 , NumberOfComponents(1)
1412 : Components(src.Components)
1413 , NumberOfComponents(src.NumberOfComponents)
1419 : Components(src.Components)
1420 , NumberOfComponents(src.NumberOfComponents)
1428 return this->Components[index];
1433 return this->NumberOfComponents;
1451 template <
typename T>
1459 template <
typename T>
1468 template <
typename T>
1469 static inline VTKM_EXEC_CONT auto vec_dot(
const T& a,
const T& b)
1471 auto result = a[0] * b[0];
1474 result = result + a[i] * b[i];
1478 template <
typename T, vtkm::IdComponent Size>
1481 auto result = a[0] * b[0];
1484 result = result + a[i] * b[i];
1491 template <
typename T>
1494 return detail::vec_dot(a, b);
1497 template <
typename T>
1500 return (a[0] * b[0]) + (a[1] * b[1]);
1502 template <
typename T>
1505 return (a[0] * b[0]) + (a[1] * b[1]) + (a[2] * b[2]);
1507 template <
typename T>
1510 return (a[0] * b[0]) + (a[1] * b[1]) + (a[2] * b[2]) + (a[3] * b[3]);
1514 #define VTK_M_SCALAR_DOT(stype) \
1515 static inline VTKM_EXEC_CONT auto dot(stype a, stype b) { return a * b; } \
1516 static inline VTKM_EXEC_CONT auto Dot(stype a, stype b) { return a * b; }
1529 template <
typename T>
1530 static inline VTKM_EXEC_CONT auto dot(
const T& a,
const T& b) -> decltype(detail::vec_dot(a, b))
1532 return vtkm::Dot(a, b);
1534 template <
typename T>
1537 return vtkm::Dot(a, b);
1539 template <
typename T>
1542 return vtkm::Dot(a, b);
1544 template <
typename T>
1547 return vtkm::Dot(a, b);
1551 template <
typename T, vtkm::IdComponent Size>
1562 template <
typename T>
1568 template <
typename T>
1571 return a[0] + a[1] + a[2];
1574 template <
typename T>
1577 return a[0] + a[1] + a[2] + a[3];
1580 template <
typename T, vtkm::IdComponent Size>
1591 template <
typename T>
1597 template <
typename T>
1600 return a[0] * a[1] * a[2];
1603 template <
typename T>
1606 return a[0] * a[1] * a[2] * a[3];
1611 template <
typename U,
typename V>
1616 template <
typename T, vtkm::IdComponent Size>
1622 stream << vec[component] <<
",";
1624 return stream << vec[Size - 1] <<
"]";
1629 template <
typename T,
typename U>
1632 return stream <<
"[" << vec.
first <<
"," << vec.
second <<
"]";
1639 #endif //vtk_m_Types_h