xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/reference/ArithmeticDivision.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
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"
27 #include "tests/validation/Helpers.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>
unrollarm_compute::test::validation::reference::__anon9efd43a20111::BroadcastUnroll43     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>
unrollarm_compute::test::validation::reference::__anon9efd43a20111::BroadcastUnroll70     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>
arithmetic_division(const SimpleTensor<T> & src1,const SimpleTensor<T> & src2,DataType data_type)79 SimpleTensor<T> arithmetic_division(const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, DataType data_type)
80 {
81     SimpleTensor<T> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), data_type);
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 
92 template SimpleTensor<half> arithmetic_division(const SimpleTensor<half> &src1, const SimpleTensor<half> &src2, DataType data_type);
93 template SimpleTensor<float> arithmetic_division(const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, DataType data_type);
94 } // namespace reference
95 } // namespace validation
96 } // namespace test
97 } // namespace arm_compute
98