blob: 42bd84db47b1952349c0fca13d347f7b208be45d [file] [log] [blame]
Chunosovd621bca2017-11-03 17:33:15 +07001/*
Michalis Spyrou299fdd32019-05-01 13:03:59 +01002 * Copyright (c) 2017-2019 ARM Limited.
Chunosovd621bca2017-11-03 17:33:15 +07003 *
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
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010030namespace arm_compute
31{
32namespace quantization
33{
Michalis Spyrou299fdd32019-05-01 13:03:59 +010034constexpr int64_t fixed_point_one_Q0 = (1LL << 31);
Gian Marco Iodice3139f032018-11-05 14:26:32 +000035constexpr float epsilon = 0.00001f;
Chunosovf450caa2017-11-08 16:09:35 +070036
Manuel Bottini07263982019-10-17 18:37:26 +010037Status calculate_quantized_multiplier(float multiplier, int *quant_multiplier, int *shift)
38{
39 if(multiplier > 1.f)
40 {
41 Status status = calculate_quantized_multiplier_greater_than_one(multiplier, quant_multiplier, shift);
42 *shift *= -1;
43 return status;
44 }
45 else
46 {
47 return calculate_quantized_multiplier_less_than_one(multiplier, quant_multiplier, shift);
48 }
49}
50
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010051Status calculate_quantized_multiplier_less_than_one(float multiplier,
52 int *quant_multiplier,
53 int *right_shift)
Chunosovd621bca2017-11-03 17:33:15 +070054{
55 ARM_COMPUTE_RETURN_ERROR_ON(quant_multiplier == nullptr);
56 ARM_COMPUTE_RETURN_ERROR_ON(right_shift == nullptr);
Gian Marco Iodice3139f032018-11-05 14:26:32 +000057 ARM_COMPUTE_RETURN_ERROR_ON(multiplier < -epsilon);
58 ARM_COMPUTE_RETURN_ERROR_ON(multiplier > 1.0f + epsilon);
59 if(std::fabs(1.0f - multiplier) < epsilon)
60 {
61 *quant_multiplier = 1;
62 *right_shift = 0;
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010063 return Status{};
Gian Marco Iodice3139f032018-11-05 14:26:32 +000064 }
65
66 if(std::fabs(0.0f - multiplier) < epsilon)
Chunosovd621bca2017-11-03 17:33:15 +070067 {
68 *quant_multiplier = 0;
69 *right_shift = 0;
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010070 return Status{};
Chunosovd621bca2017-11-03 17:33:15 +070071 }
Gian Marco Iodice3139f032018-11-05 14:26:32 +000072
Chunosovd621bca2017-11-03 17:33:15 +070073 const double q = std::frexp(multiplier, right_shift);
74 *right_shift *= -1;
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010075 auto q_fixed = static_cast<int64_t>(support::cpp11::round(q * fixed_point_one_Q0));
Chunosovf450caa2017-11-08 16:09:35 +070076 ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > fixed_point_one_Q0);
77 if(q_fixed == fixed_point_one_Q0)
Chunosovd621bca2017-11-03 17:33:15 +070078 {
79 q_fixed /= 2;
80 --*right_shift;
81 }
82 ARM_COMPUTE_RETURN_ERROR_ON(*right_shift < 0);
83 ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > std::numeric_limits<int32_t>::max());
Chunosovf450caa2017-11-08 16:09:35 +070084 *quant_multiplier = static_cast<int32_t>(q_fixed);
Chunosovd621bca2017-11-03 17:33:15 +070085
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010086 return Status{};
Chunosovf450caa2017-11-08 16:09:35 +070087}
88
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010089Status calculate_quantized_multiplier_greater_than_one(float multiplier,
90 int *quantized_multiplier,
91 int *left_shift)
Chunosovf450caa2017-11-08 16:09:35 +070092{
93 ARM_COMPUTE_RETURN_ERROR_ON(quantized_multiplier == nullptr);
94 ARM_COMPUTE_RETURN_ERROR_ON(left_shift == nullptr);
95 ARM_COMPUTE_RETURN_ERROR_ON(multiplier < 1.f);
96 const double q = std::frexp(multiplier, left_shift);
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +010097 auto q_fixed = static_cast<int64_t>(support::cpp11::round(q * fixed_point_one_Q0));
Chunosovf450caa2017-11-08 16:09:35 +070098 ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > fixed_point_one_Q0);
99 if(q_fixed == fixed_point_one_Q0)
100 {
101 q_fixed /= 2;
102 ++*left_shift;
103 }
104 ARM_COMPUTE_RETURN_ERROR_ON(*left_shift < 0);
105 ARM_COMPUTE_RETURN_ERROR_ON(q_fixed > std::numeric_limits<int32_t>::max());
106 *quantized_multiplier = static_cast<int32_t>(q_fixed);
107
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +0100108 return Status{};
Chunosovf450caa2017-11-08 16:09:35 +0700109}
Michele Di Giorgiod87a7b22019-09-10 10:42:27 +0100110std::pair<int, int> get_min_max_values_from_quantized_data_type(DataType data_type)
111{
112 int min_quant_val = 0;
113 int max_quant_val = 0;
114 switch(data_type)
115 {
116 case DataType::QASYMM8:
117 min_quant_val = std::numeric_limits<uint8_t>::min();
118 max_quant_val = std::numeric_limits<uint8_t>::max();
119 break;
120 case DataType::QSYMM8:
121 min_quant_val = std::numeric_limits<int8_t>::min();
122 max_quant_val = std::numeric_limits<int8_t>::max();
123 break;
124 case DataType::QASYMM16:
125 min_quant_val = std::numeric_limits<uint16_t>::min();
126 max_quant_val = std::numeric_limits<uint16_t>::max();
127 break;
128 case DataType::QSYMM16:
129 min_quant_val = std::numeric_limits<int16_t>::min();
130 max_quant_val = std::numeric_limits<int16_t>::max();
131 break;
132 default:
133 ARM_COMPUTE_ERROR("Unsupported data type");
134 }
135 return std::make_pair(min_quant_val, max_quant_val);
136}
137} // quantization
138} // arm_compute