Compute Library
 20.02.1
ArithmeticDivision.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2019 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 
53  for(size_t i = 0; i < dst.shape()[dim - 1]; ++i, ++id_dst[dim - 1])
54  {
55  BroadcastUnroll < dim - 1 >::unroll(src1, src2, dst, id_src1, id_src2, id_dst);
56 
57  id_src1[dim - 1] += !src1_is_broadcast;
58  id_src2[dim - 1] += !src2_is_broadcast;
59  }
60  }
61 };
62 
63 template <>
64 struct BroadcastUnroll<0>
65 {
66  template <typename T>
67  static void unroll(const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst,
68  Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
69  {
70  dst[coord2index(dst.shape(), id_dst)] = src1[coord2index(src1.shape(), id_src1)] / src2[coord2index(src2.shape(), id_src2)];
71  }
72 };
73 } // namespace
74 
75 template <typename T>
77 {
79 
80  Coordinates id_src1{};
81  Coordinates id_src2{};
82  Coordinates id_dst{};
83 
84  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(src1, src2, dst, id_src1, id_src2, id_dst);
85 
86  return dst;
87 }
88 
91 } // namespace reference
92 } // namespace validation
93 } // namespace test
94 } // 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:485
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:75