Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 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 | |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 26 | #include "tests/validation/Helpers.h" |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 27 | |
Georgios Pinitas | 303f0db | 2018-11-19 11:56:51 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/utils/misc/Rounding.h" |
| 29 | #include "arm_compute/core/utils/misc/SaturateCast.h" |
| 30 | |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 31 | #include "tests/Types.h" |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace test |
| 36 | { |
| 37 | namespace validation |
| 38 | { |
| 39 | namespace reference |
| 40 | { |
Georgios Pinitas | 303f0db | 2018-11-19 11:56:51 +0000 | [diff] [blame] | 41 | template < typename T1, typename T2, typename std::enable_if < std::is_integral<T1>::value &&!std::is_same<T1, T2>::value, int >::type > |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 42 | SimpleTensor<T2> depth_convert(const SimpleTensor<T1> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift) |
| 43 | { |
| 44 | SimpleTensor<T2> result(src.shape(), dt_out); |
| 45 | |
| 46 | // Up-casting |
Georgios Pinitas | 303f0db | 2018-11-19 11:56:51 +0000 | [diff] [blame] | 47 | if(element_size_from_data_type(src.data_type()) < element_size_from_data_type(dt_out)) |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 48 | { |
| 49 | for(int i = 0; i < src.num_elements(); ++i) |
| 50 | { |
Usama Arif | 9e631c2 | 2019-05-14 17:10:40 +0100 | [diff] [blame] | 51 | result[i] = src[i] << shift; |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | // Down-casting |
| 55 | else |
| 56 | { |
| 57 | for(int i = 0; i < src.num_elements(); ++i) |
| 58 | { |
| 59 | T1 val = src[i] >> shift; |
Georgios Pinitas | 303f0db | 2018-11-19 11:56:51 +0000 | [diff] [blame] | 60 | result[i] = (policy == ConvertPolicy::SATURATE) ? utils::cast::saturate_cast<T2>(val) : static_cast<T2>(val); |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | return result; |
| 64 | } |
| 65 | |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 66 | template < typename T1, typename T2, typename std::enable_if < is_floating_point<T1>::value &&(!std::is_same<T1, T2>::value &&!std::is_same<T2, bfloat16>::value), int >::type > |
Michele Di Giorgio | 6b4e604 | 2018-08-02 12:02:48 +0100 | [diff] [blame] | 67 | SimpleTensor<T2> depth_convert(const SimpleTensor<T1> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift) |
| 68 | { |
| 69 | SimpleTensor<T2> result(src.shape(), dt_out); |
Georgios Pinitas | 303f0db | 2018-11-19 11:56:51 +0000 | [diff] [blame] | 70 | ARM_COMPUTE_ERROR_ON(shift != 0); |
| 71 | ARM_COMPUTE_UNUSED(policy, shift); |
Michele Di Giorgio | 6b4e604 | 2018-08-02 12:02:48 +0100 | [diff] [blame] | 72 | |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 73 | if(!std::is_same<T2, bfloat16>::value && !is_floating_point<T2>::value) |
Michele Di Giorgio | 6b4e604 | 2018-08-02 12:02:48 +0100 | [diff] [blame] | 74 | { |
Georgios Pinitas | 6a468ef | 2018-12-04 13:46:48 +0000 | [diff] [blame] | 75 | // Always saturate on floats |
| 76 | for(int i = 0; i < src.num_elements(); ++i) |
| 77 | { |
Usama Arif | 9e631c2 | 2019-05-14 17:10:40 +0100 | [diff] [blame] | 78 | T1 val = utils::rounding::round_half_away_from_zero(src[i]); |
| 79 | result[i] = utils::cast::saturate_cast<T2>(val); |
Georgios Pinitas | 6a468ef | 2018-12-04 13:46:48 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | for(int i = 0; i < src.num_elements(); ++i) |
| 85 | { |
| 86 | result[i] = static_cast<T2>(src[i]); |
| 87 | } |
Michele Di Giorgio | 6b4e604 | 2018-08-02 12:02:48 +0100 | [diff] [blame] | 88 | } |
| 89 | return result; |
| 90 | } |
| 91 | |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 92 | template < typename T1, typename T2, typename std::enable_if < std::is_same<T1, bfloat16>::value || std::is_same<T2, bfloat16>::value, int >::type > |
| 93 | SimpleTensor<T2> depth_convert(const SimpleTensor<T1> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift) |
| 94 | { |
| 95 | SimpleTensor<T2> result(src.shape(), dt_out); |
| 96 | ARM_COMPUTE_ERROR_ON(shift != 0); |
| 97 | ARM_COMPUTE_UNUSED(policy, shift); |
| 98 | |
| 99 | for(int i = 0; i < src.num_elements(); ++i) |
| 100 | { |
| 101 | result[i] = static_cast<T2>(src[i]); |
| 102 | } |
| 103 | |
| 104 | return result; |
| 105 | } |
| 106 | |
Georgios Pinitas | 303f0db | 2018-11-19 11:56:51 +0000 | [diff] [blame] | 107 | // U8 |
| 108 | template SimpleTensor<int8_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 109 | template SimpleTensor<uint16_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 110 | template SimpleTensor<int16_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
Georgios Pinitas | 303f0db | 2018-11-19 11:56:51 +0000 | [diff] [blame] | 111 | template SimpleTensor<uint32_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 112 | template SimpleTensor<int32_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
Georgios Pinitas | 303f0db | 2018-11-19 11:56:51 +0000 | [diff] [blame] | 113 | template SimpleTensor<half> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 114 | template SimpleTensor<float> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 115 | |
| 116 | // S8 |
| 117 | template SimpleTensor<uint8_t> depth_convert(const SimpleTensor<int8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 118 | template SimpleTensor<uint16_t> depth_convert(const SimpleTensor<int8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 119 | template SimpleTensor<int16_t> depth_convert(const SimpleTensor<int8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 120 | template SimpleTensor<uint32_t> depth_convert(const SimpleTensor<int8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 121 | template SimpleTensor<int32_t> depth_convert(const SimpleTensor<int8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 122 | template SimpleTensor<half> depth_convert(const SimpleTensor<int8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 123 | template SimpleTensor<float> depth_convert(const SimpleTensor<int8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 124 | |
| 125 | // U16 |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 126 | template SimpleTensor<uint8_t> depth_convert(const SimpleTensor<uint16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
Georgios Pinitas | 303f0db | 2018-11-19 11:56:51 +0000 | [diff] [blame] | 127 | template SimpleTensor<int8_t> depth_convert(const SimpleTensor<uint16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 128 | template SimpleTensor<int16_t> depth_convert(const SimpleTensor<uint16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 129 | template SimpleTensor<uint32_t> depth_convert(const SimpleTensor<uint16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
Georgios Pinitas | 303f0db | 2018-11-19 11:56:51 +0000 | [diff] [blame] | 130 | template SimpleTensor<int32_t> depth_convert(const SimpleTensor<uint16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 131 | template SimpleTensor<half> depth_convert(const SimpleTensor<uint16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 132 | template SimpleTensor<float> depth_convert(const SimpleTensor<uint16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 133 | |
| 134 | // S16 |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 135 | template SimpleTensor<uint8_t> depth_convert(const SimpleTensor<int16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
Georgios Pinitas | 303f0db | 2018-11-19 11:56:51 +0000 | [diff] [blame] | 136 | template SimpleTensor<int8_t> depth_convert(const SimpleTensor<int16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 137 | template SimpleTensor<uint16_t> depth_convert(const SimpleTensor<int16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 138 | template SimpleTensor<uint32_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] | 139 | template SimpleTensor<int32_t> depth_convert(const SimpleTensor<int16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
Georgios Pinitas | 303f0db | 2018-11-19 11:56:51 +0000 | [diff] [blame] | 140 | template SimpleTensor<half> depth_convert(const SimpleTensor<int16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 141 | template SimpleTensor<float> depth_convert(const SimpleTensor<int16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 142 | |
| 143 | // U32 |
| 144 | template SimpleTensor<uint8_t> depth_convert(const SimpleTensor<uint32_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 145 | template SimpleTensor<int8_t> depth_convert(const SimpleTensor<uint32_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 146 | template SimpleTensor<uint16_t> depth_convert(const SimpleTensor<uint32_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 147 | template SimpleTensor<int16_t> depth_convert(const SimpleTensor<uint32_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 148 | template SimpleTensor<int32_t> depth_convert(const SimpleTensor<uint32_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 149 | template SimpleTensor<half> depth_convert(const SimpleTensor<uint32_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 150 | template SimpleTensor<float> depth_convert(const SimpleTensor<uint32_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 151 | |
| 152 | // S32 |
| 153 | template SimpleTensor<uint8_t> depth_convert(const SimpleTensor<int32_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 154 | template SimpleTensor<int8_t> depth_convert(const SimpleTensor<int32_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 155 | template SimpleTensor<uint16_t> depth_convert(const SimpleTensor<int32_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 156 | template SimpleTensor<int16_t> depth_convert(const SimpleTensor<int32_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 157 | template SimpleTensor<uint32_t> depth_convert(const SimpleTensor<int32_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 158 | template SimpleTensor<half> depth_convert(const SimpleTensor<int32_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 159 | template SimpleTensor<float> depth_convert(const SimpleTensor<int32_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 160 | |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 161 | // BFLOAT16 |
| 162 | template SimpleTensor<float> depth_convert(const SimpleTensor<bfloat16> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 163 | |
Georgios Pinitas | 303f0db | 2018-11-19 11:56:51 +0000 | [diff] [blame] | 164 | // F16 |
| 165 | template SimpleTensor<uint8_t> depth_convert(const SimpleTensor<half> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 166 | template SimpleTensor<int8_t> depth_convert(const SimpleTensor<half> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 167 | template SimpleTensor<uint16_t> depth_convert(const SimpleTensor<half> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 168 | template SimpleTensor<int16_t> depth_convert(const SimpleTensor<half> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 169 | template SimpleTensor<uint32_t> depth_convert(const SimpleTensor<half> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 170 | template SimpleTensor<int32_t> depth_convert(const SimpleTensor<half> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
Michele Di Giorgio | 6b4e604 | 2018-08-02 12:02:48 +0100 | [diff] [blame] | 171 | template SimpleTensor<float> depth_convert(const SimpleTensor<half> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
Georgios Pinitas | 303f0db | 2018-11-19 11:56:51 +0000 | [diff] [blame] | 172 | |
| 173 | // F32 |
| 174 | template SimpleTensor<uint8_t> depth_convert(const SimpleTensor<float> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 175 | template SimpleTensor<int8_t> depth_convert(const SimpleTensor<float> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 176 | template SimpleTensor<uint16_t> depth_convert(const SimpleTensor<float> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 177 | template SimpleTensor<int16_t> depth_convert(const SimpleTensor<float> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 178 | template SimpleTensor<uint32_t> depth_convert(const SimpleTensor<float> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 179 | template SimpleTensor<int32_t> depth_convert(const SimpleTensor<float> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 180 | template SimpleTensor<half> depth_convert(const SimpleTensor<float> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 181 | template SimpleTensor<bfloat16> depth_convert(const SimpleTensor<float> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
Georgios Pinitas | 303f0db | 2018-11-19 11:56:51 +0000 | [diff] [blame] | 182 | |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 183 | } // namespace reference |
| 184 | } // namespace validation |
| 185 | } // namespace test |
| 186 | } // namespace arm_compute |