blob: 848ee566f095cf813d9f4944b454a233368a5bc3 [file] [log] [blame]
Chunosovd621bca2017-11-03 17:33:15 +07001/*
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 * 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
30using namespace arm_compute::quantization;
31
Chunosovf450caa2017-11-08 16:09:35 +070032constexpr int64_t fixed_point_one_Q0 = (1ll << 31);
33
Chunosovd621bca2017-11-03 17:33:15 +070034arm_compute::Error arm_compute::quantization::calculate_quantized_multiplier_less_than_one(double multiplier,
35 int *quant_multiplier,
36 int *right_shift)
37{
38 ARM_COMPUTE_RETURN_ERROR_ON(quant_multiplier == nullptr);
39 ARM_COMPUTE_RETURN_ERROR_ON(right_shift == nullptr);
40 ARM_COMPUTE_RETURN_ERROR_ON(multiplier < 0);
41 ARM_COMPUTE_RETURN_ERROR_ON(multiplier >= 1);
42 if(multiplier == 0)
43 {
44 *quant_multiplier = 0;
45 *right_shift = 0;
46 return arm_compute::Error{};
47 }
48 const double q = std::frexp(multiplier, right_shift);
49 *right_shift *= -1;
Chunosovf450caa2017-11-08 16:09:35 +070050 auto q_fixed = static_cast<int64_t>(round(q * fixed_point_one_Q0));
51 ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > fixed_point_one_Q0);
52 if(q_fixed == fixed_point_one_Q0)
Chunosovd621bca2017-11-03 17:33:15 +070053 {
54 q_fixed /= 2;
55 --*right_shift;
56 }
57 ARM_COMPUTE_RETURN_ERROR_ON(*right_shift < 0);
58 ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > std::numeric_limits<int32_t>::max());
Chunosovf450caa2017-11-08 16:09:35 +070059 *quant_multiplier = static_cast<int32_t>(q_fixed);
Chunosovd621bca2017-11-03 17:33:15 +070060
61 return arm_compute::Error{};
Chunosovf450caa2017-11-08 16:09:35 +070062}
63
64arm_compute::Error arm_compute::quantization::calculate_quantized_multiplier_greater_than_one(double multiplier,
65 int *quantized_multiplier,
66 int *left_shift)
67{
68 ARM_COMPUTE_RETURN_ERROR_ON(quantized_multiplier == nullptr);
69 ARM_COMPUTE_RETURN_ERROR_ON(left_shift == nullptr);
70 ARM_COMPUTE_RETURN_ERROR_ON(multiplier < 1.f);
71 const double q = std::frexp(multiplier, left_shift);
72 auto q_fixed = static_cast<int64_t>(round(q * fixed_point_one_Q0));
73 ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > fixed_point_one_Q0);
74 if(q_fixed == fixed_point_one_Q0)
75 {
76 q_fixed /= 2;
77 ++*left_shift;
78 }
79 ARM_COMPUTE_RETURN_ERROR_ON(*left_shift < 0);
80 ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > std::numeric_limits<int32_t>::max());
81 *quantized_multiplier = static_cast<int32_t>(q_fixed);
82
83 return arm_compute::Error{};
84}