blob: d730d43b40a8c3d16fcee607ac800128cb927ebb [file] [log] [blame]
Rickard Bolinbc6ee582022-11-04 08:24:29 +00001# SPDX-FileCopyrightText: Copyright 2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
Fredrik Svedberg6ef02302022-09-29 13:29:37 +02002#
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the License); you may
6# not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an AS IS BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Rickard Bolinbc6ee582022-11-04 08:24:29 +000016#
Fredrik Svedberg6ef02302022-09-29 13:29:37 +020017# Description:
18# Unit tests for scaling
19from ethosu.vela.scaling import quantise_scale
20from ethosu.vela.scaling import reduced_quantise_scale
21
22
23def test_scaling():
24 multiplier, shift = quantise_scale(1)
25 assert multiplier == 1073741824 and shift == 30
26 multiplier, shift = quantise_scale(0.5)
27 assert multiplier == 1073741824 and shift == 31
28 multiplier, shift = quantise_scale(0.001)
29 assert multiplier == 1099511628 and shift == 40
30 multiplier, shift = quantise_scale(0.008)
31 assert multiplier == 1099511628 and shift == 37
32 multiplier, shift = quantise_scale(0.00097652)
33 assert multiplier == 2147390190 and shift == 41
34 multiplier, shift = quantise_scale(0.0009765615959827986)
35 assert multiplier == 2147481660 and shift == 41
36
37
38def test_reduced_scaling():
39 multiplier, shift = reduced_quantise_scale(1)
40 assert multiplier == 16384 and shift == 14
41 multiplier, shift = reduced_quantise_scale(0.5)
42 assert multiplier == 16384 and shift == 15
43 multiplier, shift = reduced_quantise_scale(0.001)
44 assert multiplier == 16777 and shift == 24
45 multiplier, shift = reduced_quantise_scale(0.008)
46 assert multiplier == 16777 and shift == 21
47 multiplier, shift = reduced_quantise_scale(0.00097652)
48 assert multiplier == 32767 and shift == 25
49 # multiplier saturated
50 multiplier, shift = reduced_quantise_scale(0.0009765615959827986)
51 assert multiplier == 32767 and shift == 25