John Richardson | dd715f2 | 2017-09-18 16:10:48 +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 | * dst OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "FixedPointPixelWiseMultiplication.h" |
| 25 | |
| 26 | #include "tests/validation/FixedPoint.h" |
| 27 | |
| 28 | namespace arm_compute |
| 29 | { |
| 30 | namespace test |
| 31 | { |
| 32 | namespace validation |
| 33 | { |
| 34 | namespace reference |
| 35 | { |
| 36 | template <typename T> |
| 37 | SimpleTensor<T> fixed_point_pixel_wise_multiplication(const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, float scale, ConvertPolicy convert_policy) |
| 38 | { |
| 39 | using namespace fixed_point_arithmetic; |
| 40 | |
| 41 | SimpleTensor<T> dst(src2.shape(), src2.data_type(), 1, src2.fixed_point_position()); |
| 42 | |
| 43 | const int fixed_point_position = src1.fixed_point_position(); |
| 44 | |
| 45 | ARM_COMPUTE_ERROR_ON_MSG(src1.data_type() != src2.data_type() || src1.data_type() != dst.data_type(), |
| 46 | "Tensors must all have the same DataType"); |
| 47 | ARM_COMPUTE_ERROR_ON_MSG(fixed_point_position != src2.fixed_point_position() || fixed_point_position != dst.fixed_point_position(), |
| 48 | "Fixed-point position must be the same for both inputs and outputs"); |
| 49 | |
| 50 | // Validate fixed_point_position |
| 51 | ARM_COMPUTE_ERROR_ON((src1.data_type() == DataType::QS8) && (fixed_point_position == 0 || fixed_point_position > 7)); |
| 52 | ARM_COMPUTE_ERROR_ON((src1.data_type() == DataType::QS16) && (fixed_point_position == 0 || fixed_point_position > 15)); |
| 53 | |
| 54 | const fixed_point<T> fp_scale(scale, fixed_point_position); |
| 55 | const bool is_sat = convert_policy == ConvertPolicy::SATURATE; |
| 56 | |
| 57 | for(int i = 0; i < src1.num_elements(); ++i) |
| 58 | { |
| 59 | const fixed_point<T> val1(src1[i], fixed_point_position, true); |
| 60 | fixed_point<T> res(src2[i], fixed_point_position, true); |
| 61 | if(is_sat) |
| 62 | { |
| 63 | res = mul(mul(res, val1), fp_scale); |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | res = mul<OverflowPolicy::WRAP>(mul<OverflowPolicy::WRAP>(res, val1), fp_scale); |
| 68 | } |
| 69 | dst[i] = res.raw(); |
| 70 | } |
| 71 | |
| 72 | return dst; |
| 73 | } |
| 74 | |
| 75 | // *INDENT-OFF* |
| 76 | // clang-format off |
| 77 | template SimpleTensor<qint8_t> fixed_point_pixel_wise_multiplication(const SimpleTensor<qint8_t> &src1, const SimpleTensor<qint8_t> &src2, float scale, ConvertPolicy convert_policy); |
| 78 | template SimpleTensor<qint16_t> fixed_point_pixel_wise_multiplication(const SimpleTensor<qint16_t> &src1, const SimpleTensor<qint16_t> &src2, float scale, ConvertPolicy convert_policy); |
| 79 | // *INDENT-ON* |
| 80 | // clang-format on |
| 81 | |
| 82 | } // namespace reference |
| 83 | } // namespace validation |
| 84 | } // namespace test |
| 85 | } // namespace arm_compute |