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