Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 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 | */ |
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 | { |
| 39 | template < typename T1, typename T2, typename std::enable_if < std::is_integral<T1>::value &&std::is_floating_point<T2>::value, int >::type > |
| 40 | SimpleTensor<T2> depth_convert(const SimpleTensor<T1> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift) |
| 41 | { |
| 42 | ARM_COMPUTE_UNUSED(policy); |
| 43 | ARM_COMPUTE_UNUSED(shift); |
| 44 | |
| 45 | using namespace fixed_point_arithmetic; |
| 46 | SimpleTensor<T2> result(src.shape(), dt_out); |
| 47 | |
| 48 | const int fixed_point_position = src.fixed_point_position(); |
| 49 | |
| 50 | for(int i = 0; i < src.num_elements(); ++i) |
| 51 | { |
| 52 | result[i] = static_cast<float>(fixed_point<T1>(src[i], fixed_point_position, true)); |
| 53 | } |
| 54 | |
| 55 | return result; |
| 56 | } |
| 57 | |
| 58 | template < typename T1, typename T2, typename std::enable_if < std::is_floating_point<T1>::value &&std::is_integral<T2>::value, int >::type > |
| 59 | SimpleTensor<T2> depth_convert(const SimpleTensor<T1> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift) |
| 60 | { |
| 61 | ARM_COMPUTE_UNUSED(policy); |
| 62 | ARM_COMPUTE_UNUSED(shift); |
| 63 | |
| 64 | using namespace fixed_point_arithmetic; |
| 65 | SimpleTensor<T2> result(src.shape(), dt_out, 1, src.fixed_point_position()); |
| 66 | |
| 67 | const int fixed_point_position = result.fixed_point_position(); |
| 68 | |
| 69 | for(int i = 0; i < src.num_elements(); ++i) |
| 70 | { |
| 71 | result[i] = fixed_point<T2>(src[i], fixed_point_position).raw(); |
| 72 | } |
| 73 | |
| 74 | return result; |
| 75 | } |
| 76 | |
| 77 | 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 > |
| 78 | SimpleTensor<T2> depth_convert(const SimpleTensor<T1> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift) |
| 79 | { |
| 80 | SimpleTensor<T2> result(src.shape(), dt_out); |
| 81 | |
| 82 | // Up-casting |
| 83 | if(src.data_type() <= dt_out) |
| 84 | { |
| 85 | for(int i = 0; i < src.num_elements(); ++i) |
| 86 | { |
| 87 | result[i] = src[i] << shift; |
| 88 | } |
| 89 | } |
| 90 | // Down-casting |
| 91 | else |
| 92 | { |
| 93 | for(int i = 0; i < src.num_elements(); ++i) |
| 94 | { |
| 95 | T1 val = src[i] >> shift; |
| 96 | result[i] = (policy == ConvertPolicy::SATURATE) ? saturate_cast<T2>(val) : static_cast<T2>(val); |
| 97 | } |
| 98 | } |
| 99 | return result; |
| 100 | } |
| 101 | |
| 102 | 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 > |
| 103 | SimpleTensor<T2> depth_convert(const SimpleTensor<T1> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift) |
| 104 | { |
| 105 | ARM_COMPUTE_UNUSED(policy); |
| 106 | |
| 107 | using namespace fixed_point_arithmetic; |
| 108 | |
| 109 | SimpleTensor<T2> result(src.shape(), dt_out); |
| 110 | |
| 111 | bool is_in_place = (&src == &result); |
| 112 | |
| 113 | const int fixed_point_position_in = src.fixed_point_position(); |
| 114 | const int fixed_point_position_out = (is_in_place) ? static_cast<int>(shift) : result.fixed_point_position(); |
| 115 | |
| 116 | if(!is_in_place || (fixed_point_position_in != fixed_point_position_out)) |
| 117 | { |
| 118 | for(int i = 0; i < src.num_elements(); ++i) |
| 119 | { |
| 120 | auto x = fixed_point<T2>(src[i], fixed_point_position_in, true); |
| 121 | x.resacle(fixed_point_position_out); |
| 122 | result[i] = x.raw(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return result; |
| 127 | } |
| 128 | |
| 129 | template < typename T1, typename T2, typename std::enable_if < std::is_floating_point<T1>::value &&is_floating_point<T2>::value, int >::type > |
| 130 | SimpleTensor<T2> depth_convert(const SimpleTensor<T1> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift) |
| 131 | { |
| 132 | ARM_COMPUTE_UNUSED(policy); |
| 133 | ARM_COMPUTE_UNUSED(shift); |
| 134 | |
| 135 | SimpleTensor<T2> result(src.shape(), dt_out); |
| 136 | |
| 137 | for(int i = 0; i < src.num_elements(); ++i) |
| 138 | { |
| 139 | result[i] = static_cast<T2>(src[i]); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | template SimpleTensor<uint16_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 144 | template SimpleTensor<int16_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 145 | template SimpleTensor<int32_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 146 | template SimpleTensor<uint8_t> depth_convert(const SimpleTensor<uint16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 147 | template SimpleTensor<uint32_t> depth_convert(const SimpleTensor<uint16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 148 | template SimpleTensor<uint8_t> depth_convert(const SimpleTensor<int16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 149 | template SimpleTensor<int32_t> depth_convert(const SimpleTensor<int16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 150 | template SimpleTensor<float> depth_convert(const SimpleTensor<int8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 151 | template SimpleTensor<float> depth_convert(const SimpleTensor<int16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 152 | template SimpleTensor<int8_t> depth_convert(const SimpleTensor<float> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 153 | template SimpleTensor<int16_t> depth_convert(const SimpleTensor<float> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 154 | } // namespace reference |
| 155 | } // namespace validation |
| 156 | } // namespace test |
| 157 | } // namespace arm_compute |