blob: f0002bfb33e8dd0c767b1ab62c9e65d2182623a2 [file] [log] [blame]
John Richardson3c5f9492017-10-04 15:27:37 +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 "Magnitude.h"
25
26namespace arm_compute
27{
28namespace test
29{
30namespace validation
31{
32namespace reference
33{
34template <typename T>
35SimpleTensor<T> magnitude(const SimpleTensor<T> &gx, const SimpleTensor<T> &gy, MagnitudeType magnitude_type)
36{
37 SimpleTensor<T> mag(gx.shape(), gx.data_type());
38
39 using intermediate_type = typename common_promoted_unsigned_type<T>::intermediate_type;
40
41 for(int i = 0; i < gx.num_elements(); ++i)
42 {
43 double val = 0.f;
44
45 if(magnitude_type == MagnitudeType::L1NORM)
46 {
47 val = static_cast<intermediate_type>(std::abs(gx[i])) + static_cast<intermediate_type>(std::abs(gy[i]));
48 }
49 else // MagnitudeType::L2NORM
50 {
51 // Note: kernel saturates to uint32_t instead of intermediate_type for S32 format
52 auto sum = static_cast<uint32_t>(gx[i] * gx[i]) + static_cast<uint32_t>(gy[i] * gy[i]);
53 val = std::sqrt(sum) + 0.5f;
54 }
55
56 mag[i] = saturate_cast<T>(val);
57 }
58
59 return mag;
60}
61
62template SimpleTensor<int16_t> magnitude(const SimpleTensor<int16_t> &gx, const SimpleTensor<int16_t> &gy, MagnitudeType magnitude_type);
63template SimpleTensor<int32_t> magnitude(const SimpleTensor<int32_t> &gx, const SimpleTensor<int32_t> &gy, MagnitudeType magnitude_type);
64template SimpleTensor<half_float::half> magnitude(const SimpleTensor<half_float::half> &gx, const SimpleTensor<half_float::half> &gy, MagnitudeType magnitude_type);
65} // namespace reference
66} // namespace validation
67} // namespace test
68} // namespace arm_compute