blob: 3b58b66dca536084612fd42b7f7201f0afd60e3f [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
2// Copyright (c) 2020, ARM Limited.
3//
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 Cheng99bea142020-10-19 12:35:05 -070037 ASSERT_MSG(value > 0,
38 "AvgPool2d reciprocal_scale() error: # of elements should be > 1 but is %d", value);
Eric Kunzee5e26762020-10-13 16:11:07 -070039 uint32_t value_u32 = (uint32_t)value;
40 int32_t k = 32 - LEADING_ZEROS_32(value_u32 - 1); // (1<<k)/2 < value <= (1<<k)
41 int64_t numerator = ((1L << 30) + 1) << k;
42 multiplier = numerator / value; // (1<<30) <= multiplier < (1<<31)
43 shift = 30 + k;
44 }
45
Kevin Cheng99bea142020-10-19 12:35:05 -070046 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 -070047 {
Kevin Cheng99bea142020-10-19 12:35:05 -070048 ASSERT_MSG(multiplier >= 0, "apply_scale_32() error: multiplier should >= 0 but is %d", multiplier);
49 ASSERT_MSG(shift >= 2 && shift <= 62, "apply_scale_32() error: shift should be within [2, 62] but is %d",
50 shift);
51 int64_t round = 1L << (shift - 1);
52 if (double_round)
Eric Kunzee5e26762020-10-13 16:11:07 -070053 {
Kevin Cheng99bea142020-10-19 12:35:05 -070054 if (shift > 31 && value >= 0)
55 round += (1L << 30);
56 if (shift > 31 && value < 0)
57 round -= (1L << 30);
Eric Kunzee5e26762020-10-13 16:11:07 -070058 }
59 int64_t result = (int64_t)value * multiplier + round;
60 result = result >> shift;
61 ASSERT_MSG(result >= -(1L << 31) && result < (1L << 31),
Kevin Cheng99bea142020-10-19 12:35:05 -070062 "apply_scale_32() error: scaled result exceed int32 numeric range");
Eric Kunzee5e26762020-10-13 16:11:07 -070063 return static_cast<int32_t>(result);
64 }
65};
66
67class TypeChecker
68{
69public:
70 static bool is_integer(DType dtype)
71 {
72 if (dtype == DType_INT4 || dtype == DType_INT8 || dtype == DType_AINT8 || dtype == DType_UINT8 ||
73 dtype == DType_INT16 || dtype == DType_INT32 || dtype == DType_INT48)
74 {
75 return true;
76 }
77 return false;
78 }
79 static bool is_symmetric(DType dtype)
80 {
81 if (dtype == DType_INT4 || dtype == DType_INT8 || dtype == DType_INT16 || dtype == DType_INT32 ||
82 dtype == DType_INT48)
83 {
84 return true;
85 }
86 return false;
87 }
88};
89}; // namespace TosaReference
90
91#endif