blob: 022007720a155002982ec11bded763aaa1c799eb [file] [log] [blame]
Sanghoon Lee24486d62017-09-04 15:51:21 +01001/*
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +01002 * Copyright (c) 2017-2018 ARM Limited.
Sanghoon Lee24486d62017-09-04 15:51:21 +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 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000024#include "DepthConvertLayer.h"
Sanghoon Lee24486d62017-09-04 15:51:21 +010025
26#include "tests/validation/FixedPoint.h"
27#include "tests/validation/Helpers.h"
Sanghoon Lee24486d62017-09-04 15:51:21 +010028
29#include "tests/Types.h"
30
31namespace arm_compute
32{
33namespace test
34{
35namespace validation
36{
37namespace reference
38{
Sanghoon Lee24486d62017-09-04 15:51:21 +010039template < typename T1, typename T2, typename std::enable_if < std::is_integral<T1>::value &&std::is_integral<T2>::value &&!std::is_same<T1, T2>::value, int >::type >
40SimpleTensor<T2> depth_convert(const SimpleTensor<T1> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift)
41{
42 SimpleTensor<T2> result(src.shape(), dt_out);
43
44 // Up-casting
45 if(src.data_type() <= dt_out)
46 {
47 for(int i = 0; i < src.num_elements(); ++i)
48 {
49 result[i] = src[i] << shift;
50 }
51 }
52 // Down-casting
53 else
54 {
55 for(int i = 0; i < src.num_elements(); ++i)
56 {
57 T1 val = src[i] >> shift;
58 result[i] = (policy == ConvertPolicy::SATURATE) ? saturate_cast<T2>(val) : static_cast<T2>(val);
59 }
60 }
61 return result;
62}
63
64template < typename T1, typename T2, typename std::enable_if < std::is_integral<T1>::value &&std::is_integral<T2>::value &&std::is_same<T1, T2>::value, int >::type >
65SimpleTensor<T2> depth_convert(const SimpleTensor<T1> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift)
66{
67 ARM_COMPUTE_UNUSED(policy);
68
69 using namespace fixed_point_arithmetic;
70
71 SimpleTensor<T2> result(src.shape(), dt_out);
72
73 bool is_in_place = (&src == &result);
74
75 const int fixed_point_position_in = src.fixed_point_position();
76 const int fixed_point_position_out = (is_in_place) ? static_cast<int>(shift) : result.fixed_point_position();
77
78 if(!is_in_place || (fixed_point_position_in != fixed_point_position_out))
79 {
80 for(int i = 0; i < src.num_elements(); ++i)
81 {
82 auto x = fixed_point<T2>(src[i], fixed_point_position_in, true);
83 x.resacle(fixed_point_position_out);
84 result[i] = x.raw();
85 }
86 }
87
88 return result;
89}
90
Sanghoon Lee24486d62017-09-04 15:51:21 +010091template SimpleTensor<uint16_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift);
92template SimpleTensor<int16_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift);
93template SimpleTensor<int32_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift);
94template SimpleTensor<uint8_t> depth_convert(const SimpleTensor<uint16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift);
95template SimpleTensor<uint32_t> depth_convert(const SimpleTensor<uint16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift);
96template SimpleTensor<uint8_t> depth_convert(const SimpleTensor<int16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift);
97template SimpleTensor<int32_t> depth_convert(const SimpleTensor<int16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift);
Sanghoon Lee24486d62017-09-04 15:51:21 +010098} // namespace reference
99} // namespace validation
100} // namespace test
101} // namespace arm_compute