Compute Library
 21.02
Magnitude.cpp
Go to the documentation of this file.
1 /*
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 
26 namespace arm_compute
27 {
28 namespace test
29 {
30 namespace validation
31 {
32 namespace reference
33 {
34 template <typename T>
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 
62 template SimpleTensor<int16_t> magnitude(const SimpleTensor<int16_t> &gx, const SimpleTensor<int16_t> &gy, MagnitudeType magnitude_type);
63 template SimpleTensor<int32_t> magnitude(const SimpleTensor<int32_t> &gx, const SimpleTensor<int32_t> &gy, MagnitudeType magnitude_type);
65 } // namespace reference
66 } // namespace validation
67 } // namespace test
68 } // namespace arm_compute
typename traits::make_unsigned_conditional_t< promoted_type >::type intermediate_type
Intermediate type.
Definition: Utils.h:450
L1 normalization type.
DATA_TYPE sum(__global const DATA_TYPE *input)
Calculate sum of a vector.
DataType data_type() const override
Data type of the tensor.
Definition: SimpleTensor.h:357
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:320
Copyright (c) 2017-2021 Arm Limited.
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
T saturate_cast(T val)
Saturate a value of type T against the numeric limits of type U.
Definition: Utils.h:414
int num_elements() const override
Number of elements of the tensor.
Definition: SimpleTensor.h:406
MagnitudeType
Magnitude calculation type.
Definition: Types.h:422
SimpleTensor< T > magnitude(const SimpleTensor< T > &gx, const SimpleTensor< T > &gy, MagnitudeType magnitude_type)
Definition: Magnitude.cpp:35