aboutsummaryrefslogtreecommitdiff
path: root/pseudocode/library/numeric_accuracy_helpers.tosac
diff options
context:
space:
mode:
authorKevin Petit <kevin.petit@arm.com>2024-01-31 10:24:14 +0000
committerEric Kunze <eric.kunze@arm.com>2024-02-06 21:31:27 +0000
commitc65cd9ccff6000be01ee0742319009f0061879ce (patch)
tree726ac4d8e85ad8e46e5240ff0f026eb99589dbd5 /pseudocode/library/numeric_accuracy_helpers.tosac
parent6bf50e8706a6969e6a25be6b2638e81ad21489e7 (diff)
downloadspecification-c65cd9ccff6000be01ee0742319009f0061879ce.tar.gz
pseudocode: move all helpers to separate files
Change-Id: I75ea94d42c63e862af8e492da7f47ecd6caa1055 Signed-off-by: Kevin Petit <kevin.petit@arm.com>
Diffstat (limited to 'pseudocode/library/numeric_accuracy_helpers.tosac')
-rw-r--r--pseudocode/library/numeric_accuracy_helpers.tosac52
1 files changed, 52 insertions, 0 deletions
diff --git a/pseudocode/library/numeric_accuracy_helpers.tosac b/pseudocode/library/numeric_accuracy_helpers.tosac
new file mode 100644
index 0000000..4a2b111
--- /dev/null
+++ b/pseudocode/library/numeric_accuracy_helpers.tosac
@@ -0,0 +1,52 @@
+//
+// This confidential and proprietary software may be used only as
+// authorised by a licensing agreement from ARM Limited
+// (C) COPYRIGHT 2020-2024 ARM Limited
+// ALL RIGHTS RESERVED
+// The entire notice above must be reproduced on all authorised
+// copies and copies may only be made to the extent permitted
+// by a licensing agreement from ARM Limited.
+
+fp64_t exp2(int n) {
+ if (n < -1075) {
+ return 0.0; // smaller than smallest denormal
+ }
+ REQUIRE(n <= 1023);
+ fp64_t v = 1.0;
+ while (n > 0) { v = v*2.0; n--; }
+ while (n < 0) { v = v/2.0; n++; }
+ return v;
+}
+
+int ilog2(fp64_t v) {
+ REQURE(0 < v && v < infinity);
+ int n = 0;
+ while (v >= 2.0) { v = v/2.0; n++; }
+ while (v < 1.0) { v = v*2.0; n--; }
+ return n;
+}
+
+fp64_t normal_min<in_t>() {
+ switch (in_t) {
+ case fp32_t: return exp2(-126);
+ case bf16_t: return exp2(-126);
+ case fp16_t: return exp2( -14);
+ }
+}
+
+fp64_t normal_max<in_t>() {
+ switch (in_t) {
+ case fp32_t: return exp2(128) - exp2(127-23);
+ case bf16_t: return exp2(128) - exp2(127- 7);
+ case fp16_t: return exp2( 16) - exp2( 15-10);
+ }
+}
+
+// Number of fractional (mantissa bits)
+int normal_frac<in_t> () {
+ switch (in_t) {
+ case fp32_t: return 23;
+ case fp16_t: return 10;
+ case bf16_t: return 7;
+ }
+}