DPC++ Runtime
Runtime libraries for oneAPI DPC++
item_base.hpp
Go to the documentation of this file.
1 //==----------- item_base.hpp --- SYCL iteration ItemBase ------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
11 #include <sycl/id.hpp> // for id
12 #include <sycl/range.hpp> // for range
13 
14 #include <stddef.h> // for size_t
15 
16 namespace sycl {
17 inline namespace _V1 {
18 template <int dimensions> class id;
19 template <int dimensions> class range;
20 
21 namespace detail {
22 template <int Dims, bool WithOffset> struct ItemBase;
23 
24 template <int Dims> struct ItemBase<Dims, true> {
25 
26  bool operator==(const ItemBase &Rhs) const {
27  return (Rhs.MIndex == MIndex) && (Rhs.MExtent == MExtent) &&
28  (Rhs.MOffset == MOffset);
29  }
30 
31  bool operator!=(const ItemBase &Rhs) const { return !((*this) == Rhs); }
32 
33  size_t get_linear_id() const {
34  if (1 == Dims) {
35  return MIndex[0] - MOffset[0];
36  }
37  if (2 == Dims) {
38  return (MIndex[0] - MOffset[0]) * MExtent[1] + (MIndex[1] - MOffset[1]);
39  }
40  return ((MIndex[0] - MOffset[0]) * MExtent[1] * MExtent[2]) +
41  ((MIndex[1] - MOffset[1]) * MExtent[2]) + (MIndex[2] - MOffset[2]);
42  }
43 
47 };
48 
49 template <int Dims> struct ItemBase<Dims, false> {
50 
51  bool operator==(const ItemBase &Rhs) const {
52  return (Rhs.MIndex == MIndex) && (Rhs.MExtent == MExtent);
53  }
54 
55  bool operator!=(const ItemBase &Rhs) const { return !((*this) == Rhs); }
56 
57  operator ItemBase<Dims, true>() const {
58  return ItemBase<Dims, true>(MExtent, MIndex, id<Dims>{});
59  }
60 
61  size_t get_linear_id() const {
62  if (1 == Dims) {
63  return MIndex[0];
64  }
65  if (2 == Dims) {
66  return MIndex[0] * MExtent[1] + MIndex[1];
67  }
68  return (MIndex[0] * MExtent[1] * MExtent[2]) + (MIndex[1] * MExtent[2]) +
69  MIndex[2];
70  }
71 
74 };
75 
76 } // namespace detail
77 } // namespace _V1
78 } // namespace sycl
Definition: access.hpp:18
bool operator==(const ItemBase &Rhs) const
Definition: item_base.hpp:51
bool operator!=(const ItemBase &Rhs) const
Definition: item_base.hpp:55
bool operator==(const ItemBase &Rhs) const
Definition: item_base.hpp:26
bool operator!=(const ItemBase &Rhs) const
Definition: item_base.hpp:31