Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 1 | /* |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 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 | */ |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 24 | #include "DepthConvertLayer.h" |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 25 | |
| 26 | #include "tests/validation/FixedPoint.h" |
| 27 | #include "tests/validation/Helpers.h" |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 28 | |
| 29 | #include "tests/Types.h" |
| 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | namespace test |
| 34 | { |
| 35 | namespace validation |
| 36 | { |
| 37 | namespace reference |
| 38 | { |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 39 | template < 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 > |
| 40 | SimpleTensor<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 | |
| 64 | template < 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 > |
| 65 | SimpleTensor<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 Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 91 | template SimpleTensor<uint16_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 92 | template SimpleTensor<int16_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 93 | template SimpleTensor<int32_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 94 | template SimpleTensor<uint8_t> depth_convert(const SimpleTensor<uint16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 95 | template SimpleTensor<uint32_t> depth_convert(const SimpleTensor<uint16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 96 | template SimpleTensor<uint8_t> depth_convert(const SimpleTensor<int16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 97 | template SimpleTensor<int32_t> depth_convert(const SimpleTensor<int16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 98 | } // namespace reference |
| 99 | } // namespace validation |
| 100 | } // namespace test |
| 101 | } // namespace arm_compute |