blob: 29a2007bbd01d79e2568681c584ea6563ea01881 [file] [log] [blame]
Ioan-Cristian Szabo432a7d42017-10-12 09:25:19 +01001/*
2 * Copyright (c) 2017 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 "Accumulate.h"
25
26#include "arm_compute/core/Types.h"
27#include "tests/validation/FixedPoint.h"
28#include "tests/validation/Helpers.h"
29
30namespace arm_compute
31{
32namespace test
33{
34namespace validation
35{
36namespace reference
37{
38template <typename T1, typename T2>
39SimpleTensor<T2> accumulate(const SimpleTensor<T1> &src, DataType output_data_type)
40{
41 SimpleTensor<T2> dst{ src.shape(), output_data_type };
42
43 library->fill_tensor_uniform(dst, 1, static_cast<T2>(0), static_cast<T2>(std::numeric_limits<T1>::max()));
44
45 using intermediate_type = typename common_promoted_signed_type<T1, T2>::intermediate_type;
46
47 for(int i = 0; i < src.num_elements(); ++i)
48 {
49 intermediate_type val = static_cast<intermediate_type>(src[i]) + static_cast<intermediate_type>(dst[i]);
50 dst[i] = saturate_cast<T2>(val);
51 }
52
53 return dst;
54}
55
56template <typename T1, typename T2>
57SimpleTensor<T2> accumulate_weighted(const SimpleTensor<T1> &src, float alpha, DataType output_data_type)
58{
59 ARM_COMPUTE_ERROR_ON_MSG(alpha < 0.f || alpha > 1.f, "Weight (alpha) specified in accumulate_weighted must be within the range [0, 1]");
60
61 SimpleTensor<T2> dst{ src.shape(), output_data_type };
62
63 library->fill_tensor_uniform(dst, 1, static_cast<T2>(0), static_cast<T2>(std::numeric_limits<T1>::max()));
64
65 using intermediate_type = typename common_promoted_signed_type<T1, T2>::intermediate_type;
66
67 for(int i = 0; i < src.num_elements(); ++i)
68 {
69 double val = (1. - static_cast<double>(alpha)) * static_cast<intermediate_type>(dst[i]) + static_cast<double>(alpha) * static_cast<intermediate_type>(src[i]);
70 dst[i] = static_cast<T2>(val);
71 }
72
73 return dst;
74}
75
76template <typename T1, typename T2>
77SimpleTensor<T2> accumulate_squared(const SimpleTensor<T1> &src, uint32_t shift, DataType output_data_type)
78{
79 ARM_COMPUTE_ERROR_ON_MSG(shift > 15, "Shift in accumulate_squared must be within the range [0, 15]");
80
81 SimpleTensor<T2> dst{ src.shape(), output_data_type };
82
83 library->fill_tensor_uniform(dst, 1, static_cast<T2>(0), static_cast<T2>(std::numeric_limits<T1>::max()));
84
85 using intermediate_type = typename common_promoted_signed_type<T1, T2>::intermediate_type;
86 intermediate_type denom = 1 << shift;
87
88 for(int i = 0; i < src.num_elements(); ++i)
89 {
90 intermediate_type val = static_cast<intermediate_type>(dst[i]) + (static_cast<intermediate_type>(src[i]) * static_cast<intermediate_type>(src[i]) / denom);
91 dst[i] = saturate_cast<T2>(val);
92 }
93
94 return dst;
95}
96
97template SimpleTensor<int16_t> accumulate(const SimpleTensor<uint8_t> &src, DataType output_data_type);
98template SimpleTensor<uint8_t> accumulate_weighted(const SimpleTensor<uint8_t> &src, float alpha, DataType output_data_type);
99template SimpleTensor<int16_t> accumulate_squared(const SimpleTensor<uint8_t> &src, uint32_t shift, DataType output_data_type);
100} // namespace reference
101} // namespace validation
102} // namespace test
103} // namespace arm_compute