blob: 2515b771d4b26526a104c9fb5311c945db5edd37 [file] [log] [blame]
Fredrik Svedberg1575b942020-08-18 13:19:18 +02001# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
2#
3# Copyright 2015 The Gemmlowp Authors. All Rights Reserved.
4#
5# SPDX-License-Identifier: Apache-2.0
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19# Description:
20# Contains various fixed point math functions based on the gemmlowp fixed
21# point implementation.
22import numpy as np
23
Fredrik Svedberg2f6f3792020-09-10 16:12:33 +020024# Convert floating point to fixed point, default Q5.26
25def from_float(x, integer_bits=5):
26 i32info = np.iinfo(np.int32)
27 fractional_bits = i32info.bits - integer_bits - 1
28 return min(max(round(x * (1 << fractional_bits)), i32info.min), i32info.max)
29
30
31# Convert fixed point to floating point, default Q5.26
32def to_float(x, integer_bits=5):
33 fractional_bits = np.iinfo(np.int32).bits - integer_bits - 1
34 return x / (1 << fractional_bits)
35
Fredrik Svedberg1575b942020-08-18 13:19:18 +020036
37def saturating_rounding_mul(a, b):
38 assert np.int32(a) == a
39 assert np.int32(b) == b
40 if a == b and a == np.iinfo(np.int32).min:
41 return np.int32(np.iinfo(np.int32).max)
Fredrik Svedberg2f6f3792020-09-10 16:12:33 +020042 divider = 1 << 31
43 ab = a * b
44 if ab >= 0:
45 nudge = 1 << 30
46 return (ab + nudge) // divider
47 else:
48 nudge = 1 - (1 << 30)
49 ab_plus_nudge = ab + nudge
50 result = ab_plus_nudge // divider
51 # Python uses floor, the reference uses truncation
52 # so we need to compensate for that.
53 if result * divider < ab_plus_nudge:
54 result += 1
55 return result
Fredrik Svedberg1575b942020-08-18 13:19:18 +020056
57
58def shift_left(a, offset):
59 assert np.int32(a) == a
60 assert offset >= 0
Fredrik Svedberg2f6f3792020-09-10 16:12:33 +020061 i32_info = np.iinfo(np.int32)
Fredrik Svedberg1575b942020-08-18 13:19:18 +020062 shifted = a * (1 << offset)
Fredrik Svedberg2f6f3792020-09-10 16:12:33 +020063 if shifted < i32_info.min:
64 return np.int32(i32_info.min)
65 elif shifted > i32_info.max:
66 return np.int32(i32_info.max)
Fredrik Svedberg1575b942020-08-18 13:19:18 +020067 else:
68 return np.int32(shifted)
69
70
71def rounding_divide_by_pot(x, exponent):
72 assert np.int32(x) == x
73 assert np.int32(exponent) == exponent
74 mask = (1 << exponent) - 1
75 remainder = x & mask
76 threshold = mask >> 1
77 if x < 0:
78 threshold += 1
79 result = x >> exponent
80 if remainder > threshold:
81 result += 1
82 return result
83
84
Fredrik Svedberg2f6f3792020-09-10 16:12:33 +020085def saturating_rounding_multiply_by_pot(x, exponent):
Fredrik Svedberg1575b942020-08-18 13:19:18 +020086 assert np.int32(x) == x
87 assert np.int32(exponent) == exponent
88 threshold = (1 << (np.iinfo(np.int32).bits - 1 - exponent)) - 1
89 if x > threshold:
90 return np.iinfo(np.int32).max
91 elif x < -threshold:
92 return np.iinfo(np.int32).min
93 else:
94 return shift_left(x, exponent)
95
96
97def rescale(integer_bits_src, integer_bits_dst, x):
98 assert np.int32(integer_bits_src) == integer_bits_src
99 assert np.int32(integer_bits_dst) == integer_bits_dst
100 assert np.int32(x) == x
101 exponent = integer_bits_src - integer_bits_dst
Fredrik Svedberg2f6f3792020-09-10 16:12:33 +0200102 if exponent < 0:
103 result = rounding_divide_by_pot(x, -exponent)
104 else:
105 result = saturating_rounding_multiply_by_pot(x, exponent)
Fredrik Svedberg1575b942020-08-18 13:19:18 +0200106 return result
107
108
109# Input Q0.31
110def exp_on_interval_between_negative_one_quarter_and_0_excl(a):
111 assert np.int32(a) == a
112 assert -1 << (31 - 2) <= a < 0
113 offset = 28
114 constant_term = 1895147668
115 constant_1_over_3 = 715827883
116 x = a + (1 << offset)
117 x2 = saturating_rounding_mul(x, x)
118 x3 = saturating_rounding_mul(x2, x)
119 x4 = saturating_rounding_mul(x2, x2)
120 x4_over_4 = rounding_divide_by_pot(x4, 2)
121 x4_over_24_plus_x3_over_6_plus_x2_over_2 = rounding_divide_by_pot(
122 saturating_rounding_mul((x4_over_4 + x3), constant_1_over_3) + x2, 1
123 )
124
125 return np.int32(
126 constant_term + saturating_rounding_mul(constant_term, x + x4_over_24_plus_x3_over_6_plus_x2_over_2)
127 )
128
129
130# Input Q5.26
131def exp_on_negative_values(a):
132 assert np.int32(a) == a
133 assert a <= 0
134 one_quarter = np.int32(16777216)
135 mask = np.int32(16777215)
136 a_mod_quarter_minus_one_quarter = np.int32((a & mask) - one_quarter)
137
138 result = exp_on_interval_between_negative_one_quarter_and_0_excl(rescale(5, 0, a_mod_quarter_minus_one_quarter))
139 remainder = np.int32(a_mod_quarter_minus_one_quarter - a)
140
141 def exp_barrel_shifter(exponent, multiplier, result):
142 fractional_bits = 26
143 integer_bits = 5
144 shift = fractional_bits + exponent if integer_bits > exponent else 0
145 if remainder & (1 << shift):
146 return saturating_rounding_mul(result, multiplier)
147 else:
148 return result
149
150 result = exp_barrel_shifter(-2, 1672461947, result)
151 result = exp_barrel_shifter(-1, 1302514674, result)
152 result = exp_barrel_shifter(+0, 790015084, result)
153 result = exp_barrel_shifter(+1, 290630308, result)
154 result = exp_barrel_shifter(+2, 39332535, result)
155 result = exp_barrel_shifter(+3, 720401, result)
156 result = exp_barrel_shifter(+4, 242, result)
157
158 if a == 0:
159 return np.iinfo(np.int32).max
160 else:
161 return result
Louis Verhaardd7911c42020-08-25 13:36:41 +0200162
163
164def multiply_by_quantized_multiplier(x, scale, shift):
165 # Multiplies x (int32) by (scale, shift) which have obtained by a call to scaling.quantize_scale,
166 # returns rounded result
167 shift = 31 - shift
168 left_shift = shift if shift > 0 else 0
169 right_shift = -shift if shift < 0 else 0
170 mul = saturating_rounding_mul(x * (1 << left_shift), scale)
171 return rounding_divide_by_pot(mul, right_shift)