blob: 4f6a525ed0ddaad9eb9e0f9d1b6320a18ba94f67 [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
Kevin Cheng3a478572021-01-22 17:21:02 -08002// Copyright (c) 2020-2021, ARM Limited.
Eric Kunzee5e26762020-10-13 16:11:07 -07003//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#ifndef TOSA_REFERENCE_QUANT_UTIL_H
17#define TOSA_REFERENCE_QUANT_UTIL_H
18
19#include "arith_util.h"
20#include "func_debug.h"
21#include "ops/template_types.h"
22#include "tosa_generated.h"
23
24using namespace tosa;
25
26namespace TosaReference
27{
28
Eric Kunzee5e26762020-10-13 16:11:07 -070029class QuantUtil
30{
31public:
Eric Kunzee5e26762020-10-13 16:11:07 -070032 static void reciprocal_scale(int32_t value,
33 // Output
34 int32_t& multiplier,
35 int32_t& shift)
36 {
Kevin Chengaee1fac2020-11-11 13:54:06 -080037 ASSERT_MSG(value > 0, "AvgPool2d reciprocal_scale() error: # of elements should be > 1 but is %d", value);
Eric Kunzee5e26762020-10-13 16:11:07 -070038 uint32_t value_u32 = (uint32_t)value;
39 int32_t k = 32 - LEADING_ZEROS_32(value_u32 - 1); // (1<<k)/2 < value <= (1<<k)
40 int64_t numerator = ((1L << 30) + 1) << k;
41 multiplier = numerator / value; // (1<<30) <= multiplier < (1<<31)
42 shift = 30 + k;
43 }
44
Kevin Cheng99bea142020-10-19 12:35:05 -070045 static int32_t apply_scale_32(int32_t value, int32_t multiplier, int32_t shift, bool double_round = true)
Eric Kunzee5e26762020-10-13 16:11:07 -070046 {
Kevin Chengacb550f2021-06-29 15:32:19 -070047 if (multiplier < 0)
48 {
49 std::string desc = "apply_scale_32() error: multiplier should >= 0 but is " + std::to_string(multiplier);
50 throw desc;
51 }
52 if (shift < 2 || shift > 62)
53 {
54 std::string desc =
55 "apply_scale_32(): shift value should stay within [2, 62] but is " + std::to_string(shift);
56 throw desc;
57 }
Kevin Cheng99bea142020-10-19 12:35:05 -070058 int64_t round = 1L << (shift - 1);
59 if (double_round)
Eric Kunzee5e26762020-10-13 16:11:07 -070060 {
Kevin Cheng99bea142020-10-19 12:35:05 -070061 if (shift > 31 && value >= 0)
62 round += (1L << 30);
63 if (shift > 31 && value < 0)
64 round -= (1L << 30);
Eric Kunzee5e26762020-10-13 16:11:07 -070065 }
66 int64_t result = (int64_t)value * multiplier + round;
67 result = result >> shift;
Kevin Chengacb550f2021-06-29 15:32:19 -070068 if (result < -(1L << 31) || result >= (1L << 31))
69 {
70 std::string desc = "apply_scale_32() error: scaled result exceeds int32 numeric range";
71 throw desc;
72 }
Eric Kunzee5e26762020-10-13 16:11:07 -070073 return static_cast<int32_t>(result);
74 }
Kevin Cheng0f87c952021-03-18 17:41:39 -070075
76 static int32_t apply_scale_16(int64_t value, int16_t multiplier, int32_t shift)
77 {
Kevin Chengacb550f2021-06-29 15:32:19 -070078 if (multiplier < 0)
79 {
80 std::string desc = "apply_scale_16() error: multiplier should >= 0 but is " + std::to_string(multiplier);
81 throw desc;
82 }
83 if (shift < 2 || shift > 62)
84 {
85 std::string desc =
86 "apply_scale_16(): shift value should stay within [2, 62] but is " + std::to_string(shift);
87 throw desc;
88 }
Kevin Cheng0f87c952021-03-18 17:41:39 -070089 int64_t round = 1L << (shift - 1);
90 int64_t result = value * (int64_t)multiplier + round;
91 result = result >> shift;
Kevin Chengacb550f2021-06-29 15:32:19 -070092 if (result < -(1L << 31) || result >= (1L << 31))
93 {
94 std::string desc = "apply_scale_16() error: scaled result exceeds int32 numeric range";
95 throw desc;
96 }
Kevin Cheng0f87c952021-03-18 17:41:39 -070097 return static_cast<int32_t>(result);
98 }
Eric Kunzee5e26762020-10-13 16:11:07 -070099};
100
101class TypeChecker
102{
103public:
104 static bool is_integer(DType dtype)
105 {
Kevin Cheng0f87c952021-03-18 17:41:39 -0700106 if (dtype == DType_INT4 || dtype == DType_INT8 || dtype == DType_UINT8 || dtype == DType_INT16 ||
107 dtype == DType_INT32 || dtype == DType_INT48)
Eric Kunzee5e26762020-10-13 16:11:07 -0700108 {
109 return true;
110 }
111 return false;
112 }
Eric Kunzee5e26762020-10-13 16:11:07 -0700113};
114}; // namespace TosaReference
115
116#endif