blob: f86ee5e5997ed1925f1dab8e4af51b6c37cb9466 [file] [log] [blame]
Michalis Spyrou0a887922018-06-11 16:30:23 +01001/*
Michalis Spyroud1d77222020-04-08 14:10:15 +01002 * Copyright (c) 2018-2020 ARM Limited.
Michalis Spyrou0a887922018-06-11 16:30:23 +01003 *
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"
Michalis Spyrou0a887922018-06-11 16:30:23 +010027#include "tests/validation/Helpers.h"
28
29namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
35namespace reference
36{
37namespace
38{
39template <size_t dim>
40struct 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);
Michalis Spyroud1d77222020-04-08 14:10:15 +010052#if defined(_OPENMP)
53 #pragma omp parallel for
54#endif /* _OPENMP */
55 for(size_t i = 0; i < dst.shape()[dim - 1]; ++i)
Michalis Spyrou0a887922018-06-11 16:30:23 +010056 {
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;
Michalis Spyroud1d77222020-04-08 14:10:15 +010061 ++id_dst[dim - 1];
Michalis Spyrou0a887922018-06-11 16:30:23 +010062 }
63 }
64};
65
66template <>
67struct 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
78template <typename T>
79SimpleTensor<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
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010083 Coordinates id_src1{};
84 Coordinates id_src2{};
85 Coordinates id_dst{};
Michalis Spyrou0a887922018-06-11 16:30:23 +010086
87 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(src1, src2, dst, id_src1, id_src2, id_dst);
88
89 return dst;
90}
91
92template SimpleTensor<half> arithmetic_division(const SimpleTensor<half> &src1, const SimpleTensor<half> &src2, DataType data_type);
93template 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