Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 1 | /* |
Viet-Hoa Do | 9c7c2d2 | 2023-04-11 17:16:27 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2023 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" |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 25 | #include "arm_compute/core/Helpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 26 | #include "support/ToolchainSupport.h" |
Viet-Hoa Do | a62129a | 2023-04-26 15:38:45 +0100 | [diff] [blame] | 27 | #include "src/core/utils/quantization/AsymmHelpers.h" |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 28 | |
| 29 | #include <cmath> |
| 30 | #include <limits> |
| 31 | #include <numeric> |
| 32 | |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace quantization |
| 36 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 37 | constexpr int64_t fixed_point_one_Q0 = (1LL << 31); |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 38 | constexpr float epsilon = 0.00001f; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 39 | |
Sang-Hoon Park | 30b46a6 | 2020-04-18 01:40:57 +0100 | [diff] [blame] | 40 | Status calculate_quantized_multiplier(float multiplier, int32_t *quant_multiplier, int32_t *shift, bool ignore_epsilon) |
Manuel Bottini | 0726398 | 2019-10-17 18:37:26 +0100 | [diff] [blame] | 41 | { |
Michele Di Giorgio | 35c3794 | 2019-12-03 19:34:30 +0000 | [diff] [blame] | 42 | if(multiplier >= 1.f) |
Manuel Bottini | 0726398 | 2019-10-17 18:37:26 +0100 | [diff] [blame] | 43 | { |
| 44 | Status status = calculate_quantized_multiplier_greater_than_one(multiplier, quant_multiplier, shift); |
| 45 | *shift *= -1; |
| 46 | return status; |
| 47 | } |
| 48 | else |
| 49 | { |
Sang-Hoon Park | 30b46a6 | 2020-04-18 01:40:57 +0100 | [diff] [blame] | 50 | return calculate_quantized_multiplier_less_than_one(multiplier, quant_multiplier, shift, ignore_epsilon); |
Manuel Bottini | 0726398 | 2019-10-17 18:37:26 +0100 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 54 | Status calculate_quantized_multiplier_less_than_one(float multiplier, |
| 55 | int32_t *quant_multiplier, |
Sang-Hoon Park | 30b46a6 | 2020-04-18 01:40:57 +0100 | [diff] [blame] | 56 | int32_t *right_shift, |
| 57 | bool ignore_epsilon) |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 58 | { |
Sang-Hoon Park | 30b46a6 | 2020-04-18 01:40:57 +0100 | [diff] [blame] | 59 | const float internal_epsilon = ignore_epsilon ? 0.0f : epsilon; |
| 60 | |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON(quant_multiplier == nullptr); |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON(right_shift == nullptr); |
Sang-Hoon Park | 30b46a6 | 2020-04-18 01:40:57 +0100 | [diff] [blame] | 63 | ARM_COMPUTE_RETURN_ERROR_ON(multiplier < -internal_epsilon); |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON(multiplier > 1.0f + internal_epsilon); |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 65 | |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 66 | int shift_exp = 0; |
| 67 | const double q = std::frexp(multiplier, &shift_exp); |
| 68 | *right_shift = -1 * shift_exp; |
| 69 | auto q_fixed = static_cast<int64_t>(support::cpp11::round(q * fixed_point_one_Q0)); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 70 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > fixed_point_one_Q0); |
| 71 | if(q_fixed == fixed_point_one_Q0) |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 72 | { |
| 73 | q_fixed /= 2; |
| 74 | --*right_shift; |
| 75 | } |
Sang-Hoon Park | 30b46a6 | 2020-04-18 01:40:57 +0100 | [diff] [blame] | 76 | |
| 77 | if(ignore_epsilon && *right_shift > 31) |
| 78 | { |
| 79 | *right_shift = 0; |
| 80 | q_fixed = 0; |
| 81 | } |
| 82 | |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 83 | ARM_COMPUTE_RETURN_ERROR_ON(*right_shift < 0); |
| 84 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > std::numeric_limits<int32_t>::max()); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 85 | *quant_multiplier = static_cast<int32_t>(q_fixed); |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 86 | |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 87 | return Status{}; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 88 | } |
| 89 | |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 90 | Status calculate_quantized_multiplier_greater_than_one(float multiplier, |
| 91 | int32_t *quantized_multiplier, |
| 92 | int32_t *left_shift) |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 93 | { |
| 94 | ARM_COMPUTE_RETURN_ERROR_ON(quantized_multiplier == nullptr); |
| 95 | ARM_COMPUTE_RETURN_ERROR_ON(left_shift == nullptr); |
| 96 | ARM_COMPUTE_RETURN_ERROR_ON(multiplier < 1.f); |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 97 | |
| 98 | int shift_exp = 0; |
| 99 | const double q = std::frexp(multiplier, &shift_exp); |
| 100 | *left_shift = shift_exp; |
| 101 | auto q_fixed = static_cast<int64_t>(support::cpp11::round(q * fixed_point_one_Q0)); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 102 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > fixed_point_one_Q0); |
| 103 | if(q_fixed == fixed_point_one_Q0) |
| 104 | { |
| 105 | q_fixed /= 2; |
| 106 | ++*left_shift; |
| 107 | } |
| 108 | ARM_COMPUTE_RETURN_ERROR_ON(*left_shift < 0); |
| 109 | ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > std::numeric_limits<int32_t>::max()); |
| 110 | *quantized_multiplier = static_cast<int32_t>(q_fixed); |
| 111 | |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 112 | return Status{}; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 113 | } |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 114 | |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 115 | arm_compute::Status calculate_quantized_multipliers(const QuantizationInfo &iq_info, |
| 116 | const QuantizationInfo &wq_info, |
| 117 | const QuantizationInfo &oq_info, |
| 118 | GEMMLowpOutputStageInfo &stage_info) |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 119 | { |
| 120 | ARM_COMPUTE_RETURN_ERROR_ON(iq_info.scale().empty()); |
| 121 | ARM_COMPUTE_RETURN_ERROR_ON(wq_info.scale().empty()); |
| 122 | ARM_COMPUTE_RETURN_ERROR_ON(oq_info.scale().empty()); |
| 123 | |
| 124 | const unsigned int size = wq_info.scale().size(); |
| 125 | |
| 126 | auto &quant_multipliers = stage_info.gemmlowp_multipliers; |
| 127 | auto &quant_shifts = stage_info.gemmlowp_shifts; |
| 128 | quant_multipliers.resize(size); |
| 129 | quant_shifts.resize(size); |
| 130 | |
| 131 | const auto &w_scales = wq_info.scale(); |
| 132 | const float i_scale = iq_info.scale().at(0); |
| 133 | const float o_scale = oq_info.scale().at(0); |
| 134 | |
| 135 | for(unsigned int i = 0; i < size; ++i) |
| 136 | { |
| 137 | const float multiplier = i_scale * w_scales[i] / o_scale; |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 138 | int32_t quant_multiplier = 0; |
| 139 | int32_t quant_shift = 0; |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 140 | ARM_COMPUTE_RETURN_ON_ERROR(calculate_quantized_multiplier(multiplier, &quant_multiplier, &quant_shift)); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 141 | quant_multipliers[i] = quant_multiplier; |
| 142 | quant_shifts[i] = quant_shift; |
| 143 | } |
| 144 | |
| 145 | // Legacy part |
| 146 | stage_info.gemmlowp_shift = quant_shifts[0]; |
| 147 | stage_info.gemmlowp_multiplier = quant_multipliers[0]; |
| 148 | |
| 149 | return Status{}; |
| 150 | } |
| 151 | |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 152 | std::pair<int, int> get_min_max_values_from_quantized_data_type(DataType data_type) |
| 153 | { |
| 154 | int min_quant_val = 0; |
| 155 | int max_quant_val = 0; |
| 156 | switch(data_type) |
| 157 | { |
| 158 | case DataType::QASYMM8: |
| 159 | min_quant_val = std::numeric_limits<uint8_t>::min(); |
| 160 | max_quant_val = std::numeric_limits<uint8_t>::max(); |
| 161 | break; |
| 162 | case DataType::QSYMM8: |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 163 | case DataType::QASYMM8_SIGNED: |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 164 | min_quant_val = std::numeric_limits<int8_t>::min(); |
| 165 | max_quant_val = std::numeric_limits<int8_t>::max(); |
| 166 | break; |
| 167 | case DataType::QASYMM16: |
| 168 | min_quant_val = std::numeric_limits<uint16_t>::min(); |
| 169 | max_quant_val = std::numeric_limits<uint16_t>::max(); |
| 170 | break; |
| 171 | case DataType::QSYMM16: |
| 172 | min_quant_val = std::numeric_limits<int16_t>::min(); |
| 173 | max_quant_val = std::numeric_limits<int16_t>::max(); |
| 174 | break; |
| 175 | default: |
| 176 | ARM_COMPUTE_ERROR("Unsupported data type"); |
| 177 | } |
| 178 | return std::make_pair(min_quant_val, max_quant_val); |
| 179 | } |
Viet-Hoa Do | 9c7c2d2 | 2023-04-11 17:16:27 +0100 | [diff] [blame] | 180 | |
Viet-Hoa Do | a62129a | 2023-04-26 15:38:45 +0100 | [diff] [blame] | 181 | std::tuple<int32_t, int32_t> get_quantized_asymmetric_output_min_max(const QuantizationInfo &q_info, const ActivationLayerInfo &act_info, DataType data_type) |
Viet-Hoa Do | 9c7c2d2 | 2023-04-11 17:16:27 +0100 | [diff] [blame] | 182 | { |
Viet-Hoa Do | a62129a | 2023-04-26 15:38:45 +0100 | [diff] [blame] | 183 | ARM_COMPUTE_ERROR_ON(data_type != DataType::QASYMM8 && data_type != DataType::QASYMM8_SIGNED); |
| 184 | |
| 185 | const auto min_max = get_min_max(data_type); |
| 186 | |
| 187 | int32_t type_min = std::get<0>(min_max).get<int32_t>(); |
| 188 | int32_t type_max = std::get<1>(min_max).get<int32_t>(); |
| 189 | |
Viet-Hoa Do | 9c7c2d2 | 2023-04-11 17:16:27 +0100 | [diff] [blame] | 190 | const UniformQuantizationInfo q_unif = q_info.uniform(); |
| 191 | |
| 192 | if(act_info.enabled()) |
| 193 | { |
| 194 | switch(act_info.activation()) |
| 195 | { |
| 196 | case ActivationLayerInfo::ActivationFunction::RELU: |
Viet-Hoa Do | a62129a | 2023-04-26 15:38:45 +0100 | [diff] [blame] | 197 | type_min = q_unif.offset; |
Viet-Hoa Do | 9c7c2d2 | 2023-04-11 17:16:27 +0100 | [diff] [blame] | 198 | break; |
| 199 | case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU: |
Viet-Hoa Do | a62129a | 2023-04-26 15:38:45 +0100 | [diff] [blame] | 200 | type_min = q_unif.offset; |
| 201 | type_max = (data_type == DataType::QASYMM8) ? quantize_qasymm8(act_info.a(), q_info) : quantize_qasymm8_signed(act_info.a(), q_info); |
Viet-Hoa Do | 9c7c2d2 | 2023-04-11 17:16:27 +0100 | [diff] [blame] | 202 | break; |
| 203 | case ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU: |
Viet-Hoa Do | a62129a | 2023-04-26 15:38:45 +0100 | [diff] [blame] | 204 | type_min = (data_type == DataType::QASYMM8) ? quantize_qasymm8(act_info.b(), q_info) : quantize_qasymm8_signed(act_info.b(), q_info); |
| 205 | type_max = (data_type == DataType::QASYMM8) ? quantize_qasymm8(act_info.a(), q_info) : quantize_qasymm8_signed(act_info.a(), q_info); |
Viet-Hoa Do | 9c7c2d2 | 2023-04-11 17:16:27 +0100 | [diff] [blame] | 206 | break; |
| 207 | default: |
| 208 | ARM_COMPUTE_ERROR("Activation function not supported."); |
| 209 | break; |
| 210 | } |
| 211 | } |
| 212 | |
Viet-Hoa Do | a62129a | 2023-04-26 15:38:45 +0100 | [diff] [blame] | 213 | return std::make_tuple(type_min, type_max); |
Viet-Hoa Do | 9c7c2d2 | 2023-04-11 17:16:27 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 216 | void compute_quantized_multipliers_and_shifts(const ITensorInfo *input, |
| 217 | const ITensorInfo *weights, |
| 218 | const ITensorInfo *output, |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 219 | int32_t *output_multipliers_ptr, |
| 220 | int32_t *output_shifts_ptr) |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 221 | { |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 222 | const UniformQuantizationInfo iq_info = input->quantization_info().uniform(); |
| 223 | const QuantizationInfo wq_info = weights->quantization_info(); |
| 224 | const UniformQuantizationInfo oq_info = output->quantization_info().uniform(); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 225 | |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 226 | const unsigned int num_filters = wq_info.scale().size(); |
| 227 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 228 | for(unsigned int i = 0; i < num_filters; ++i) |
| 229 | { |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 230 | int32_t output_multiplier = 0; |
| 231 | int32_t output_shift = 0; |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 232 | const float multiplier = iq_info.scale * wq_info.scale()[i] / oq_info.scale; |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 233 | calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 234 | |
| 235 | output_multipliers_ptr[i] = output_multiplier; |
| 236 | output_shifts_ptr[i] = output_shift; |
| 237 | } |
| 238 | } |
Sang-Hoon Park | 396cb95 | 2020-03-26 14:02:37 +0000 | [diff] [blame] | 239 | |
| 240 | int32_t saturating_rounding_doubling_highmul(int32_t a, int32_t b) |
| 241 | { |
| 242 | bool overflow = a == b && a == std::numeric_limits<int32_t>::min(); |
| 243 | int64_t a_64(a); |
| 244 | int64_t b_64(b); |
Sang-Hoon Park | 0d008f7 | 2020-03-13 14:56:05 +0000 | [diff] [blame] | 245 | int64_t ab_64 = a_64 * b_64; |
Pablo Tello | 4e66d70 | 2022-03-07 18:20:12 +0000 | [diff] [blame] | 246 | const bool is_positive_or_zero = |
| 247 | a == 0 || b == 0 || |
| 248 | (std::signbit(static_cast<double>(a)) == std::signbit(static_cast<double>(b))); |
Sang-Hoon Park | 0d008f7 | 2020-03-13 14:56:05 +0000 | [diff] [blame] | 249 | int32_t nudge = is_positive_or_zero ? (1 << 30) : (1 - (1 << 30)); |
| 250 | int32_t ab_x2_high32 = static_cast<int32_t>((ab_64 + nudge) / (1ll << 31)); |
Sang-Hoon Park | 396cb95 | 2020-03-26 14:02:37 +0000 | [diff] [blame] | 251 | return overflow ? std::numeric_limits<int32_t>::max() : ab_x2_high32; |
| 252 | } |
| 253 | |
| 254 | inline int32_t rounding_divide_by_pow2(int32_t x, int exponent) |
| 255 | { |
| 256 | const int32_t mask = (1 << exponent) - 1; |
| 257 | const int32_t threshold = (mask >> 1) + (x < 0 ? 1 : 0); |
| 258 | return (x >> exponent) + ((x & mask) > threshold ? 1 : 0); |
| 259 | } |
| 260 | |
Sang-Hoon Park | 0d008f7 | 2020-03-13 14:56:05 +0000 | [diff] [blame] | 261 | int32_t multiply_by_quantized_multiplier(int32_t input, int32_t qmul, int32_t shift) |
Sang-Hoon Park | 396cb95 | 2020-03-26 14:02:37 +0000 | [diff] [blame] | 262 | { |
| 263 | const auto left_shift = shift > 0 ? shift : 0; |
| 264 | const auto right_shift = shift > 0 ? 0 : -shift; |
| 265 | return rounding_divide_by_pow2(saturating_rounding_doubling_highmul(input * (1 << left_shift), qmul), right_shift); |
| 266 | } |
| 267 | |
| 268 | int32_t saturating_rounding_multiply_by_pow2(int32_t exponent, int32_t v) |
| 269 | { |
| 270 | if(exponent == 0) |
| 271 | { |
| 272 | return v; |
| 273 | } |
| 274 | else if(exponent < 0) |
| 275 | { |
| 276 | return rounding_divide_by_pow2(v, -exponent); |
| 277 | } |
| 278 | else |
| 279 | { |
| 280 | constexpr auto min = std::numeric_limits<int32_t>::min(); |
| 281 | constexpr auto max = std::numeric_limits<int32_t>::max(); |
| 282 | const auto width = sizeof(int32_t) * 8; |
| 283 | |
| 284 | const int32_t threshold = ((1 << (width - 1 - exponent)) - 1); |
| 285 | bool pos_mask = v > threshold; |
| 286 | bool neg_mask = v < -threshold; |
| 287 | int32_t result = v << exponent; |
| 288 | result = pos_mask ? max : result; |
| 289 | result = neg_mask ? min : result; |
| 290 | return result; |
| 291 | } |
| 292 | } |
Sang-Hoon Park | 0d008f7 | 2020-03-13 14:56:05 +0000 | [diff] [blame] | 293 | |
| 294 | void get_invsqrt_quantized_multiplier_exp(int32_t input, int32_t reverse_shift, int32_t &output_inv_sqrt, int32_t &output_shift) |
| 295 | { |
| 296 | ARM_COMPUTE_ERROR_ON(input < 0); |
| 297 | |
| 298 | if(input <= 1) |
| 299 | { |
| 300 | // dealing the inputs (0 and 1) separately to avoid overflow |
| 301 | output_inv_sqrt = std::numeric_limits<std::int32_t>::max(); |
| 302 | output_shift = 0; |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | // prepare input for fixed point operation and compute shift value |
| 307 | output_shift = 11; |
| 308 | while(input >= (1 << 29)) |
| 309 | { |
| 310 | input /= 4; |
| 311 | ++output_shift; |
| 312 | } |
| 313 | |
| 314 | const uint32_t max_left_shift_bits = __builtin_clz(static_cast<uint32_t>(input)) - 1; |
| 315 | const uint32_t max_left_shift_bits_pairs = max_left_shift_bits / 2; |
| 316 | const uint32_t left_shift_bit_pairs = max_left_shift_bits_pairs - 1; |
| 317 | output_shift -= left_shift_bit_pairs; |
| 318 | input <<= 2 * left_shift_bit_pairs; |
| 319 | |
| 320 | // Calculation in fixed point domain with 3 integer bits. |
| 321 | using FixedPointRawType = int32_t; |
| 322 | constexpr uint32_t fixedpoint_position = 3; |
| 323 | constexpr uint32_t fixedpoint_int_position = sizeof(FixedPointRawType) * 8 - 1 - fixedpoint_position; |
| 324 | using FixedPoint3 = FixedPointRawType; |
| 325 | using FixedPoint0 = FixedPointRawType; |
| 326 | |
| 327 | // fixed point representation of input divided by 2 and 1.5 for Newton-Raphson iteration |
| 328 | const FixedPoint3 fixedpoint_input = (input >> 1); |
| 329 | const FixedPoint3 fixedpoint_half_input = rounding_divide_by_pow2(fixedpoint_input, 1); |
| 330 | const FixedPoint3 fixedpoint_half_three = (0x1 << fixedpoint_int_position) + (0x1 << (fixedpoint_int_position - 1)); |
| 331 | |
| 332 | // initial guess (1) in fixed point representation |
| 333 | FixedPoint3 x = 0x1 << fixedpoint_int_position; |
| 334 | |
| 335 | // multiplication of two fixed point numbers, defined for readability |
| 336 | auto fixed_point_mul = [](FixedPointRawType a, FixedPointRawType b) -> FixedPointRawType |
| 337 | { |
| 338 | return saturating_rounding_doubling_highmul(a, b); |
| 339 | }; |
| 340 | |
| 341 | // rescaling of fixed point to have dst_bit integer bits, defined for readability |
| 342 | auto fixed_point_rescale = [](FixedPointRawType a, uint32_t src_bit, uint32_t dst_bit) -> FixedPointRawType |
| 343 | { |
| 344 | const uint32_t exponent = src_bit - dst_bit; |
| 345 | return saturating_rounding_multiply_by_pow2(exponent, a); |
| 346 | }; |
| 347 | |
| 348 | // 5 iterations of Newton-Raphson method for inverse square root - 1.5 * x_n = input/2 * (x_n)^3 |
| 349 | constexpr int32_t num_iteration = 5; |
| 350 | for(int32_t i = 0; i < num_iteration; ++i) |
| 351 | { |
| 352 | const auto x3 = fixed_point_rescale(fixed_point_mul(fixed_point_mul(x, x), x), 9, fixedpoint_position); |
| 353 | x = fixed_point_rescale(fixed_point_mul(fixedpoint_half_three, x) - fixed_point_mul(fixedpoint_half_input, x3), 6, fixedpoint_position); |
| 354 | } |
| 355 | |
| 356 | // fixed point representation of sqrt(1/2) |
| 357 | const FixedPoint0 fixedpoint_half_sqrt_2 = 1518500250; |
| 358 | x = fixed_point_mul(fixedpoint_half_sqrt_2, x); |
| 359 | output_inv_sqrt = x; |
| 360 | if(output_shift < 0) |
| 361 | { |
| 362 | output_inv_sqrt <<= -output_shift; |
| 363 | output_shift = 0; |
| 364 | } |
| 365 | // convert right shift to left shift |
| 366 | output_shift *= reverse_shift; |
| 367 | } |
Michele Di Giorgio | d87a7b2 | 2019-09-10 10:42:27 +0100 | [diff] [blame] | 368 | } // quantization |
| 369 | } // arm_compute |