aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFredrik Svedberg <fredrik.svedberg@arm.com>2022-09-29 13:29:37 +0200
committerFredrik Svedberg <fredrik.svedberg@arm.com>2022-10-03 14:15:55 +0200
commit6ef02301ce36a0d2b72171926376d62d42bd348a (patch)
tree56a8102d9dcd2b9e4bbc3f18268f855ce10b1c23
parent1e5456fcb153b310f6b74ccb2938667bc8b2b6dd (diff)
downloadethos-u-vela-6ef02301ce36a0d2b72171926376d62d42bd348a.tar.gz
MLBEDSW-2723 Handle int16 multiplier overflow test case
Added unit tests for scaling including saturated multiplier test. Change-Id: I87bb3a4bed8f62f5ef5cf3851b97f09ce42bf2b6 Signed-off-by: Fredrik Svedberg <fredrik.svedberg@arm.com>
-rw-r--r--ethosu/vela/test/test_scaling.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/ethosu/vela/test/test_scaling.py b/ethosu/vela/test/test_scaling.py
new file mode 100644
index 00000000..b4518302
--- /dev/null
+++ b/ethosu/vela/test/test_scaling.py
@@ -0,0 +1,50 @@
+# Copyright (C) 2022 Arm Limited or its affiliates. All rights reserved.
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed under the Apache License, Version 2.0 (the License); you may
+# not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an AS IS BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# Description:
+# Unit tests for scaling
+from ethosu.vela.scaling import quantise_scale
+from ethosu.vela.scaling import reduced_quantise_scale
+
+
+def test_scaling():
+ multiplier, shift = quantise_scale(1)
+ assert multiplier == 1073741824 and shift == 30
+ multiplier, shift = quantise_scale(0.5)
+ assert multiplier == 1073741824 and shift == 31
+ multiplier, shift = quantise_scale(0.001)
+ assert multiplier == 1099511628 and shift == 40
+ multiplier, shift = quantise_scale(0.008)
+ assert multiplier == 1099511628 and shift == 37
+ multiplier, shift = quantise_scale(0.00097652)
+ assert multiplier == 2147390190 and shift == 41
+ multiplier, shift = quantise_scale(0.0009765615959827986)
+ assert multiplier == 2147481660 and shift == 41
+
+
+def test_reduced_scaling():
+ multiplier, shift = reduced_quantise_scale(1)
+ assert multiplier == 16384 and shift == 14
+ multiplier, shift = reduced_quantise_scale(0.5)
+ assert multiplier == 16384 and shift == 15
+ multiplier, shift = reduced_quantise_scale(0.001)
+ assert multiplier == 16777 and shift == 24
+ multiplier, shift = reduced_quantise_scale(0.008)
+ assert multiplier == 16777 and shift == 21
+ multiplier, shift = reduced_quantise_scale(0.00097652)
+ assert multiplier == 32767 and shift == 25
+ # multiplier saturated
+ multiplier, shift = reduced_quantise_scale(0.0009765615959827986)
+ assert multiplier == 32767 and shift == 25