blob: aba44e45930b8407449cc7df17a0ed54ade2e542 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5
6#include <armnn/Tensor.hpp>
7
8#include <boost/assert.hpp>
9
10namespace armnn
11{
12
telsoa01c577f2c2018-08-31 09:22:23 +010013// Utility class providing access to raw tensor memory based on indices along each dimension.
telsoa014fcda012018-03-09 14:13:49 +000014template <typename DataType>
15class TensorBufferArrayView
16{
17public:
James Conroy59540822018-10-11 12:39:05 +010018 TensorBufferArrayView(const TensorShape& shape, DataType* data, DataLayoutIndexed dataLayout = DataLayout::NCHW)
telsoa014fcda012018-03-09 14:13:49 +000019 : m_Shape(shape)
20 , m_Data(data)
James Conroy59540822018-10-11 12:39:05 +010021 , m_DataLayout(dataLayout)
telsoa014fcda012018-03-09 14:13:49 +000022 {
23 }
24
25 DataType& Get(unsigned int b, unsigned int c, unsigned int h, unsigned int w) const
26 {
James Conroy59540822018-10-11 12:39:05 +010027 BOOST_ASSERT( b < m_Shape[0] || ( m_Shape[0] == 0 && b == 0 ) );
28 BOOST_ASSERT( c < m_Shape[m_DataLayout.GetChannelsIndex()] ||
29 ( m_Shape[m_DataLayout.GetChannelsIndex()] == 0 && c == 0) );
30 BOOST_ASSERT( h < m_Shape[m_DataLayout.GetHeightIndex()] ||
31 ( m_Shape[m_DataLayout.GetHeightIndex()] == 0 && h == 0) );
32 BOOST_ASSERT( w < m_Shape[m_DataLayout.GetWidthIndex()] ||
33 ( m_Shape[m_DataLayout.GetWidthIndex()] == 0 && w == 0) );
telsoa014fcda012018-03-09 14:13:49 +000034
35 return m_Data[b * m_Shape[1] * m_Shape[2] * m_Shape[3]
James Conroy59540822018-10-11 12:39:05 +010036 + c * m_Shape[m_DataLayout.GetHeightIndex()] * m_Shape[m_DataLayout.GetWidthIndex()]
37 + h * m_Shape[m_DataLayout.GetWidthIndex()]
telsoa014fcda012018-03-09 14:13:49 +000038 + w];
39 }
40
41private:
42 const TensorShape m_Shape;
James Conroy59540822018-10-11 12:39:05 +010043 DataType* m_Data;
44 DataLayoutIndexed m_DataLayout;
telsoa014fcda012018-03-09 14:13:49 +000045};
46
47} //namespace armnn