blob: 0ced43940420625e55449146ec686d0bfd4a857a [file] [log] [blame]
Michalis Spyrou0a887922018-06-11 16:30:23 +01001/*
Michalis Spyroubcfd09a2019-05-01 13:03:59 +01002 * Copyright (c) 2018-2019 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);
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
63template <>
64struct 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
75template <typename T>
76SimpleTensor<T> arithmetic_division(const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, DataType data_type)
77{
78 SimpleTensor<T> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), data_type);
79
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010080 Coordinates id_src1{};
81 Coordinates id_src2{};
82 Coordinates id_dst{};
Michalis Spyrou0a887922018-06-11 16:30:23 +010083
84 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(src1, src2, dst, id_src1, id_src2, id_dst);
85
86 return dst;
87}
88
89template SimpleTensor<half> arithmetic_division(const SimpleTensor<half> &src1, const SimpleTensor<half> &src2, DataType data_type);
90template SimpleTensor<float> arithmetic_division(const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, DataType data_type);
91} // namespace reference
92} // namespace validation
93} // namespace test
94} // namespace arm_compute