blob: fd2e0ae3785e9a2cb24e3e7466235d21cdfba2b1 [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
Sanghoon Lee24486d62017-09-04 15:51:21 +010026#include "tests/validation/Helpers.h"
Sanghoon Lee24486d62017-09-04 15:51:21 +010027
28#include "tests/Types.h"
29
30namespace arm_compute
31{
32namespace test
33{
34namespace validation
35{
36namespace reference
37{
Sanghoon Lee24486d62017-09-04 15:51:21 +010038template < 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 >
39SimpleTensor<T2> depth_convert(const SimpleTensor<T1> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift)
40{
41 SimpleTensor<T2> result(src.shape(), dt_out);
42
43 // Up-casting
44 if(src.data_type() <= dt_out)
45 {
46 for(int i = 0; i < src.num_elements(); ++i)
47 {
48 result[i] = src[i] << shift;
49 }
50 }
51 // Down-casting
52 else
53 {
54 for(int i = 0; i < src.num_elements(); ++i)
55 {
56 T1 val = src[i] >> shift;
57 result[i] = (policy == ConvertPolicy::SATURATE) ? saturate_cast<T2>(val) : static_cast<T2>(val);
58 }
59 }
60 return result;
61}
62
Michele Di Giorgio6b4e6042018-08-02 12:02:48 +010063template < typename T1, typename T2, typename std::enable_if < is_floating_point<T1>::value &&is_floating_point<T2>::value &&!std::is_same<T1, T2>::value, int >::type >
64SimpleTensor<T2> depth_convert(const SimpleTensor<T1> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift)
65{
66 SimpleTensor<T2> result(src.shape(), dt_out);
67
68 const uint32_t scale = 1 << shift;
69
70 // Up-casting
71 if(src.data_type() <= dt_out)
72 {
73 for(int i = 0; i < src.num_elements(); ++i)
74 {
75 result[i] = src[i] * static_cast<T2>(scale);
76 }
77 }
78 // Down-casting
79 else
80 {
81 for(int i = 0; i < src.num_elements(); ++i)
82 {
83 T1 val = src[i] / static_cast<T1>(scale);
84 result[i] = (policy == ConvertPolicy::SATURATE) ? saturate_cast<T2>(val) : static_cast<T2>(val);
85 }
86 }
87 return result;
88}
89
Sanghoon Lee24486d62017-09-04 15:51:21 +010090template SimpleTensor<uint16_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift);
91template SimpleTensor<int16_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift);
92template SimpleTensor<int32_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift);
93template SimpleTensor<uint8_t> depth_convert(const SimpleTensor<uint16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift);
94template SimpleTensor<uint32_t> depth_convert(const SimpleTensor<uint16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift);
95template SimpleTensor<uint8_t> depth_convert(const SimpleTensor<int16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift);
96template SimpleTensor<int32_t> depth_convert(const SimpleTensor<int16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift);
Michele Di Giorgio6b4e6042018-08-02 12:02:48 +010097template SimpleTensor<half> depth_convert(const SimpleTensor<float> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift);
98template SimpleTensor<float> depth_convert(const SimpleTensor<half> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift);
Sanghoon Lee24486d62017-09-04 15:51:21 +010099} // namespace reference
100} // namespace validation
101} // namespace test
102} // namespace arm_compute