Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [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 | */ |
| 24 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
| 25 | |
| 26 | #include <cmath> |
| 27 | #include <limits> |
| 28 | #include <numeric> |
| 29 | |
| 30 | using namespace arm_compute::quantization; |
| 31 | |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 32 | constexpr int64_t fixed_point_one_Q0 = (1ll << 31); |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 33 | constexpr float epsilon = 0.00001f; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 34 | |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 35 | arm_compute::Status arm_compute::quantization::calculate_quantized_multiplier_less_than_one(float multiplier, |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 36 | int *quant_multiplier, |
| 37 | int *right_shift) |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 38 | { |
| 39 | ARM_COMPUTE_RETURN_ERROR_ON(quant_multiplier == nullptr); |
| 40 | ARM_COMPUTE_RETURN_ERROR_ON(right_shift == nullptr); |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 41 | ARM_COMPUTE_RETURN_ERROR_ON(multiplier < -epsilon); |
| 42 | ARM_COMPUTE_RETURN_ERROR_ON(multiplier > 1.0f + epsilon); |
| 43 | if(std::fabs(1.0f - multiplier) < epsilon) |
| 44 | { |
| 45 | *quant_multiplier = 1; |
| 46 | *right_shift = 0; |
| 47 | return arm_compute::Status{}; |
| 48 | } |
| 49 | |
| 50 | if(std::fabs(0.0f - multiplier) < epsilon) |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 51 | { |
| 52 | *quant_multiplier = 0; |
| 53 | *right_shift = 0; |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 54 | return arm_compute::Status{}; |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 55 | } |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 56 | |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 57 | const double q = std::frexp(multiplier, right_shift); |
| 58 | *right_shift *= -1; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 59 | auto q_fixed = static_cast<int64_t>(round(q * fixed_point_one_Q0)); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > fixed_point_one_Q0); |
| 61 | if(q_fixed == fixed_point_one_Q0) |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 62 | { |
| 63 | q_fixed /= 2; |
| 64 | --*right_shift; |
| 65 | } |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON(*right_shift < 0); |
| 67 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > std::numeric_limits<int32_t>::max()); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 68 | *quant_multiplier = static_cast<int32_t>(q_fixed); |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 69 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 70 | return arm_compute::Status{}; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 71 | } |
| 72 | |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 73 | arm_compute::Status arm_compute::quantization::calculate_quantized_multiplier_greater_than_one(float multiplier, |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 74 | int *quantized_multiplier, |
| 75 | int *left_shift) |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 76 | { |
| 77 | ARM_COMPUTE_RETURN_ERROR_ON(quantized_multiplier == nullptr); |
| 78 | ARM_COMPUTE_RETURN_ERROR_ON(left_shift == nullptr); |
| 79 | ARM_COMPUTE_RETURN_ERROR_ON(multiplier < 1.f); |
| 80 | const double q = std::frexp(multiplier, left_shift); |
| 81 | auto q_fixed = static_cast<int64_t>(round(q * fixed_point_one_Q0)); |
| 82 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > fixed_point_one_Q0); |
| 83 | if(q_fixed == fixed_point_one_Q0) |
| 84 | { |
| 85 | q_fixed /= 2; |
| 86 | ++*left_shift; |
| 87 | } |
| 88 | ARM_COMPUTE_RETURN_ERROR_ON(*left_shift < 0); |
| 89 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > std::numeric_limits<int32_t>::max()); |
| 90 | *quantized_multiplier = static_cast<int32_t>(q_fixed); |
| 91 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 92 | return arm_compute::Status{}; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 93 | } |