Compute Library
 20.05
ArithmeticDivision.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020 ARM Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "ArithmeticDivision.h"
25 
26 #include "arm_compute/core/Types.h"
28 
29 namespace arm_compute
30 {
31 namespace test
32 {
33 namespace validation
34 {
35 namespace reference
36 {
37 namespace
38 {
39 template <size_t dim>
40 struct BroadcastUnroll
41 {
42  template <typename T>
43  static void unroll(const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst,
44  Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
45  {
46  const bool src1_is_broadcast = (src1.shape()[dim - 1] != dst.shape()[dim - 1]);
47  const bool src2_is_broadcast = (src2.shape()[dim - 1] != dst.shape()[dim - 1]);
48 
49  id_src1.set(dim - 1, 0);
50  id_src2.set(dim - 1, 0);
51  id_dst.set(dim - 1, 0);
52 #if defined(_OPENMP)
53  #pragma omp parallel for
54 #endif /* _OPENMP */
55  for(size_t i = 0; i < dst.shape()[dim - 1]; ++i)
56  {
57  BroadcastUnroll < dim - 1 >::unroll(src1, src2, dst, id_src1, id_src2, id_dst);
58 
59  id_src1[dim - 1] += !src1_is_broadcast;
60  id_src2[dim - 1] += !src2_is_broadcast;
61  ++id_dst[dim - 1];
62  }
63  }
64 };
65 
66 template <>
67 struct BroadcastUnroll<0>
68 {
69  template <typename T>
70  static void unroll(const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst,
71  Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
72  {
73  dst[coord2index(dst.shape(), id_dst)] = src1[coord2index(src1.shape(), id_src1)] / src2[coord2index(src2.shape(), id_src2)];
74  }
75 };
76 } // namespace
77 
78 template <typename T>
80 {
82 
83  Coordinates id_src1{};
84  Coordinates id_src2{};
85  Coordinates id_dst{};
86 
87  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(src1, src2, dst, id_src1, id_src2, id_dst);
88 
89  return dst;
90 }
91 
94 } // namespace reference
95 } // namespace validation
96 } // namespace test
97 } // namespace arm_compute
static TensorShape broadcast_shape(const Shapes &... shapes)
If shapes are broadcast compatible, return the broadcasted shape.
Definition: TensorShape.h:210
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:321
Copyright (c) 2017-2020 ARM Limited.
int coord2index(const TensorShape &shape, const Coordinates &coord)
Linearise the given coordinate.
Definition: Utils.h:489
Coordinates of an item.
Definition: Coordinates.h:37
SimpleTensor< T > arithmetic_division(const SimpleTensor< T > &src1, const SimpleTensor< T > &src2, DataType data_type)
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:59
DataType
Available data types.
Definition: Types.h:77