1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #ifndef TENSORFLOW_LITE_MICRO_KERNELS_XTENSA_HIFIMINI_FIXEDPOINT_UTILS_H_
17 #define TENSORFLOW_LITE_MICRO_KERNELS_XTENSA_HIFIMINI_FIXEDPOINT_UTILS_H_
18
19 #include <algorithm>
20 #include <cmath>
21 #include <cstdint>
22
23 #include "tensorflow/lite/kernels/internal/compatibility.h"
24 #include "tensorflow/lite/micro/kernels/xtensa/xtensa.h"
25
26 namespace tflite {
27
28 #if defined(HIFIMINI)
29
30 // INT24 MIN/MAX
31 #define INT24_MIN -8388608
32 #define INT24_MAX 8388607
33
34 // Multiply 24bit value by a quantized multiplier (w/ shift) and returns a 48bit
35 // aligned value in the QR register.
MultiplyByQuantizedMultiplier(ae_p24x2s x_24x2,int32_t quantized_multiplier,int shift)36 inline ae_q56s MultiplyByQuantizedMultiplier(ae_p24x2s x_24x2,
37 int32_t quantized_multiplier,
38 int shift) {
39 // A value with 1 sign bit, N integer bits and M fractional bits is
40 // represented as QN+1.M since the sign bit is included in the integer bits.
41 //
42 // The Q notation in this method explains the values represented in each
43 // variable, along with an implicit division since the quantized_multiplier
44 // represents a value between 0.5 and 1.0 (Q1.X-1 where X is the bit precision
45 // of the type).
46 //
47 // Load the quantized multiplier into the PR register.
48 // NOTE: This method assumes that this param has been calculated for 24bit
49 // space - not 32bits.
50 // Q32.0 / 2^23 -> Q24.0 / 2^23 representing a Q1.23 multiplier.
51 ae_p24x2s quantized_multiplier_24x2 = AE_MOVPA24(quantized_multiplier);
52 // Shift right by 23 - 16 bits minus the specified shift. This is because we
53 // keep 16 fractional bits until the end to perform rounding. Subtract shift
54 // since shift is a left shift, and the 23-16 is a right shift.
55 int shift_amount = 7 - shift;
56
57 // Find the product of x and the quantized_multiplier.
58 // Q24.0 / 2^23 * Q24.0 = Q48.0 / 2^23
59 // Q48.0 / 2^23 >> 7 = Q48.0 / 2^16
60 ae_q56s result_56 = AE_MULP24S_HH(x_24x2, quantized_multiplier_24x2);
61
62 // Shift right if shift amount is positive, left if shift amount is negative.
63 if (shift_amount >= 0) {
64 result_56 = AE_Q56S_SRA(result_56, shift_amount);
65 } else {
66 result_56 = AE_Q56S_SLA(result_56, -shift_amount);
67 }
68
69 // Round off the bottom 16 bits.
70 // Q48.0 / 2^16 -> Q32.0 aligned to 48 bits.
71 result_56 = AE_ROUNDSQ32SYM(result_56);
72 return result_56;
73 }
74
75 // Multiply 32bit value by a quantized multiplier (w/ shift) and returns a 48bit
76 // aligned value in the QR register.
MultiplyByQuantizedMultiplierResult48Bit(int32_t x,int32_t quantized_multiplier,int shift)77 inline ae_q56s MultiplyByQuantizedMultiplierResult48Bit(
78 int32_t x, int32_t quantized_multiplier, int shift) {
79 // Convert x into a 2x24bit PR register file. If x is outside the numerical
80 // limits of a 24bit integer, the "fractional" or lower 8bits are discarded.
81 // If x is within the range of a 24 bit integer, the "signed" or upper 8bits
82 // are discarded.
83 ae_p24x2s x_24x2;
84 if (x > INT24_MIN && x < INT24_MAX) {
85 x_24x2 = AE_MOVPA24(x);
86 } else {
87 x_24x2 = static_cast<ae_p24s>(*reinterpret_cast<ae_p24f*>(&x));
88 shift += 8;
89 }
90
91 return MultiplyByQuantizedMultiplier(x_24x2, quantized_multiplier, shift);
92 }
93
94 // Calculate quantization params for 24bit runtimes.
QuantizeMultiplierForInt24(float multiplier,int32_t * quantized_multiplier,int * shift)95 inline void QuantizeMultiplierForInt24(float multiplier,
96 int32_t* quantized_multiplier,
97 int* shift) {
98 if (multiplier == 0.0f) {
99 *quantized_multiplier = 0;
100 *shift = 0;
101 return;
102 }
103
104 // Special cased to 24bit:
105 const float q = std::frexp(multiplier, shift);
106 auto q_fixed = static_cast<int64_t>(std::round(q * (1 << 23)));
107
108 TFLITE_CHECK(q_fixed <= (1 << 23));
109 if (q_fixed == (1 << 23)) {
110 q_fixed /= 2;
111 ++*shift;
112 }
113 TFLITE_CHECK_LE(q_fixed, INT24_MAX);
114
115 // Ensure shift does not exceed 24-bit range.
116 TFLITE_CHECK_LE(*shift, 23);
117 if (*shift < -23) {
118 *shift = 0;
119 q_fixed = 0;
120 }
121 *quantized_multiplier = static_cast<int32_t>(q_fixed);
122 }
123
124 // Convert a floating point number to a Q representation for 24 bit integers.
CreateQConstantForInt24(int integer_bits,float f)125 inline int CreateQConstantForInt24(int integer_bits, float f) {
126 const float min_bounds = static_cast<float>(INT24_MIN);
127 const float max_bounds = static_cast<float>(INT24_MAX);
128
129 int fractional_bits = 23 - integer_bits;
130 float raw = std::round(f * static_cast<float>(1 << fractional_bits));
131 raw = std::max(raw, min_bounds);
132 raw = std::min(raw, max_bounds);
133 return static_cast<int>(raw);
134 }
135
136 #endif // defined(HIFIMINI)
137
138 } // namespace tflite
139
140 #endif // TENSORFLOW_LITE_MICRO_KERNELS_XTENSA_HIFIMINI_FIXEDPOINT_UTILS_H_
141