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 | |
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 | |
| 28 | #include "tests/Types.h" |
| 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace test |
| 33 | { |
| 34 | namespace validation |
| 35 | { |
| 36 | namespace reference |
| 37 | { |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 38 | 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 > |
| 39 | SimpleTensor<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 Giorgio | 6b4e604 | 2018-08-02 12:02:48 +0100 | [diff] [blame] | 63 | template < 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 > |
| 64 | SimpleTensor<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 Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 90 | template SimpleTensor<uint16_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 91 | template SimpleTensor<int16_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 92 | template SimpleTensor<int32_t> depth_convert(const SimpleTensor<uint8_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 93 | template SimpleTensor<uint8_t> depth_convert(const SimpleTensor<uint16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 94 | template SimpleTensor<uint32_t> depth_convert(const SimpleTensor<uint16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 95 | template SimpleTensor<uint8_t> depth_convert(const SimpleTensor<int16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 96 | template SimpleTensor<int32_t> depth_convert(const SimpleTensor<int16_t> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
Michele Di Giorgio | 6b4e604 | 2018-08-02 12:02:48 +0100 | [diff] [blame] | 97 | template SimpleTensor<half> depth_convert(const SimpleTensor<float> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
| 98 | template SimpleTensor<float> depth_convert(const SimpleTensor<half> &src, DataType dt_out, ConvertPolicy policy, uint32_t shift); |
Sanghoon Lee | 24486d6 | 2017-09-04 15:51:21 +0100 | [diff] [blame] | 99 | } // namespace reference |
| 100 | } // namespace validation |
| 101 | } // namespace test |
| 102 | } // namespace arm_compute |