zkf 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. zkf-0.1.0/LICENSE +201 -0
  2. zkf-0.1.0/PKG-INFO +357 -0
  3. zkf-0.1.0/README.md +332 -0
  4. zkf-0.1.0/pyproject.toml +69 -0
  5. zkf-0.1.0/setup.cfg +4 -0
  6. zkf-0.1.0/zkf/__init__.py +45 -0
  7. zkf-0.1.0/zkf/__main__.py +40 -0
  8. zkf-0.1.0/zkf/_core.py +1093 -0
  9. zkf-0.1.0/zkf/_tables/__init__.py +1 -0
  10. zkf-0.1.0/zkf/_tables/trans.py +69 -0
  11. zkf-0.1.0/zkf/_tables/trig.py +48 -0
  12. zkf-0.1.0/zkf/oracle.py +290 -0
  13. zkf-0.1.0/zkf/py.typed +0 -0
  14. zkf-0.1.0/zkf/rtl/_tables/_zkf_cordic_m16.v +105 -0
  15. zkf-0.1.0/zkf/rtl/_tables/_zkf_cordic_m18.v +106 -0
  16. zkf-0.1.0/zkf/rtl/_tables/_zkf_cordic_m24.v +109 -0
  17. zkf-0.1.0/zkf/rtl/_tables/_zkf_cordic_m27.v +111 -0
  18. zkf-0.1.0/zkf/rtl/_tables/_zkf_cordic_m32.v +113 -0
  19. zkf-0.1.0/zkf/rtl/_tables/_zkf_cordic_m36.v +115 -0
  20. zkf-0.1.0/zkf/rtl/_tables/_zkf_cordic_m48.v +121 -0
  21. zkf-0.1.0/zkf/rtl/_tables/_zkf_cordic_m53.v +124 -0
  22. zkf-0.1.0/zkf/rtl/_tables/_zkf_exp2_m16.v +161 -0
  23. zkf-0.1.0/zkf/rtl/_tables/_zkf_exp2_m18.v +161 -0
  24. zkf-0.1.0/zkf/rtl/_tables/_zkf_exp2_m24.v +353 -0
  25. zkf-0.1.0/zkf/rtl/_tables/_zkf_exp2_m27.v +161 -0
  26. zkf-0.1.0/zkf/rtl/_tables/_zkf_exp2_m32.v +225 -0
  27. zkf-0.1.0/zkf/rtl/_tables/_zkf_exp2_m36.v +353 -0
  28. zkf-0.1.0/zkf/rtl/_tables/_zkf_exp2_m48.v +161 -0
  29. zkf-0.1.0/zkf/rtl/_tables/_zkf_exp2_m53.v +225 -0
  30. zkf-0.1.0/zkf/rtl/_tables/_zkf_log2_m16.v +216 -0
  31. zkf-0.1.0/zkf/rtl/_tables/_zkf_log2_m18.v +216 -0
  32. zkf-0.1.0/zkf/rtl/_tables/_zkf_log2_m24.v +487 -0
  33. zkf-0.1.0/zkf/rtl/_tables/_zkf_log2_m27.v +216 -0
  34. zkf-0.1.0/zkf/rtl/_tables/_zkf_log2_m32.v +487 -0
  35. zkf-0.1.0/zkf/rtl/_tables/_zkf_log2_m36.v +849 -0
  36. zkf-0.1.0/zkf/rtl/_tables/_zkf_log2_m48.v +306 -0
  37. zkf-0.1.0/zkf/rtl/_tables/_zkf_log2_m53.v +487 -0
  38. zkf-0.1.0/zkf/rtl/_zkf_cordic.v +313 -0
  39. zkf-0.1.0/zkf/rtl/_zkf_div_core.v +283 -0
  40. zkf-0.1.0/zkf/rtl/_zkf_fixed_to_float.v +164 -0
  41. zkf-0.1.0/zkf/rtl/_zkf_horner.v +141 -0
  42. zkf-0.1.0/zkf/rtl/_zkf_normshift.v +281 -0
  43. zkf-0.1.0/zkf/rtl/_zkf_pack.v +226 -0
  44. zkf-0.1.0/zkf/rtl/_zkf_pmul.v +511 -0
  45. zkf-0.1.0/zkf/rtl/_zkf_rshift_sticky.v +197 -0
  46. zkf-0.1.0/zkf/rtl/_zkf_to_fixpoint.v +333 -0
  47. zkf-0.1.0/zkf/rtl/zkf_abs.v +12 -0
  48. zkf-0.1.0/zkf/rtl/zkf_add.v +526 -0
  49. zkf-0.1.0/zkf/rtl/zkf_addsub.v +51 -0
  50. zkf-0.1.0/zkf/rtl/zkf_atan2.v +892 -0
  51. zkf-0.1.0/zkf/rtl/zkf_cmp.v +67 -0
  52. zkf-0.1.0/zkf/rtl/zkf_cmp_comb.v +63 -0
  53. zkf-0.1.0/zkf/rtl/zkf_div.v +116 -0
  54. zkf-0.1.0/zkf/rtl/zkf_exp2.v +298 -0
  55. zkf-0.1.0/zkf/rtl/zkf_fma.v +566 -0
  56. zkf-0.1.0/zkf/rtl/zkf_from_int.v +121 -0
  57. zkf-0.1.0/zkf/rtl/zkf_is_finite.v +11 -0
  58. zkf-0.1.0/zkf/rtl/zkf_log2.v +432 -0
  59. zkf-0.1.0/zkf/rtl/zkf_mul.v +155 -0
  60. zkf-0.1.0/zkf/rtl/zkf_mul_ilog2.v +168 -0
  61. zkf-0.1.0/zkf/rtl/zkf_mul_ilog2_const.v +202 -0
  62. zkf-0.1.0/zkf/rtl/zkf_neg.v +14 -0
  63. zkf-0.1.0/zkf/rtl/zkf_pipe.v +54 -0
  64. zkf-0.1.0/zkf/rtl/zkf_resize.v +191 -0
  65. zkf-0.1.0/zkf/rtl/zkf_round.v +234 -0
  66. zkf-0.1.0/zkf/rtl/zkf_saturate.v +19 -0
  67. zkf-0.1.0/zkf/rtl/zkf_sincos.v +663 -0
  68. zkf-0.1.0/zkf/rtl/zkf_sort.v +65 -0
  69. zkf-0.1.0/zkf/rtl/zkf_to_int.v +123 -0
  70. zkf-0.1.0/zkf.egg-info/PKG-INFO +357 -0
  71. zkf-0.1.0/zkf.egg-info/SOURCES.txt +72 -0
  72. zkf-0.1.0/zkf.egg-info/dependency_links.txt +1 -0
  73. zkf-0.1.0/zkf.egg-info/requires.txt +10 -0
  74. zkf-0.1.0/zkf.egg-info/top_level.txt +1 -0
zkf-0.1.0/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
zkf-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,357 @@
1
+ Metadata-Version: 2.4
2
+ Name: zkf
3
+ Version: 0.1.0
4
+ Summary: Efficient floating-point engine RTL for controls & embedded following IEEE 754 sans NaN and subnormals
5
+ Author-email: Zubax Robotics <pavel.kirienko@zubax.com>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Repository, https://github.com/Zubax/zkf
8
+ Keywords: fpga,hdl,verilog,floating point,rtl,dsp,hardware design
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: Intended Audience :: Science/Research
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Topic :: Scientific/Engineering :: Electronic Design Automation (EDA)
13
+ Requires-Python: >=3.11
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+ Provides-Extra: oracle
17
+ Requires-Dist: numpy~=2.4; extra == "oracle"
18
+ Requires-Dist: mpmath~=1.4; extra == "oracle"
19
+ Provides-Extra: test
20
+ Requires-Dist: zkf[oracle]; extra == "test"
21
+ Requires-Dist: cocotb~=2.0; extra == "test"
22
+ Requires-Dist: pytest>=8; extra == "test"
23
+ Requires-Dist: pytest-xdist>=3; extra == "test"
24
+ Dynamic: license-file
25
+
26
+ # Zubax Kulibin floating point
27
+
28
+ A small and FPGA-friendly floating point format that is similar to IEEE 754 but intentionally omits support for NaN,
29
+ subnormals, exceptions, and rounding modes other than round-to-nearest, ties-to-even (RNTE).
30
+ Only one canonical positive zero representation exists.
31
+
32
+ The bit layout is identical to IEEE 754: sign, exponent, and the significand with the MSb omitted.
33
+
34
+ See how ZKF beats other floating-point libraries in <https://zubax.github.io/fpga-floating-point-eval>.
35
+
36
+ ## Usage
37
+
38
+ `zkf` ships on PyPI and is equally usable by direct RTL copy-paste; both paths are first-class.
39
+
40
+ - **As a Python dependency** (the main use case for projects that generate RTL): `pip install zkf`, then
41
+ `zkf.get_rtl()` returns every Verilog module as a `{path: source}` mapping, keyed by path relative to `zkf/rtl/`
42
+ (e.g. `zkf_add.v`, `_tables/_zkf_exp2_m18.v`). `import zkf` is pure standard library; the bit-exact value model
43
+ (`ZkfFormat`, `Zkf`, …) is re-exported from the package root and equals the RTL output bit-for-bit, so it doubles
44
+ as a golden emulator. The optional oracles in `zkf.oracle` need `pip install zkf[oracle]` (numpy/mpmath).
45
+ - **By copy-paste / submodule**: the `zkf_*` modules under `zkf/rtl/` implement the operators; drop the directory
46
+ into your project tree. Private helpers are named `_zkf_*`.
47
+
48
+ Everything else in the repo is verification scaffolding and is not a shippable artifact.
49
+
50
+ Most modules are zero-bubble throughput-1 pipelines; only those that implement computationally heavy functions
51
+ are FSM-based and offer limited throughput.
52
+ The zero-bubble ones offer the conventional `in_valid`/`out_valid` interface;
53
+ those with limited throughput extend it with `in_ready`/`out_ready` handshake.
54
+
55
+ All modules have fixed data-independent latency known at elaboration time.
56
+
57
+ The two main parameters are WEXP and WMAN setting the bit width of the biased exponent and the significand;
58
+ the most significant bit of the significand is not stored, but there is a sign bit,
59
+ so the total bit width is simply WFULL=WEXP+WMAN.
60
+
61
+ The modules are entirely self-contained -- no external dependencies; simply drag-and-drop the directory into your project.
62
+ There are private helper modules named `_zkf_*`;
63
+ they are not supposed to be instantiated by the user but the public modules depend on them.
64
+ They do not offer any of the guarantees that are valid for the public modules.
65
+
66
+ Some of the simple combinational modules may produce non-canonical outputs; this does not affect compatibility with
67
+ other modules since they always canonicalize inputs, but it is worth noting.
68
+
69
+ ### Tuning knobs
70
+
71
+ Most modules provide pipelining knobs, like output register selection, internal registers, etc,
72
+ to enable tuning for the target chip. Common options seen in most modules are:
73
+ `STAGE_INPUT` -- latch inputs (no combinational paths at the input) plus optional dummy stages
74
+ (helps in routing-congested designs);
75
+ `STAGE_OUTPUT` -- registered outputs (no combinational paths at the output);
76
+ others control various computation stages.
77
+
78
+ Some modules offer to split long multiplication into several stages via `STAGE_PRODUCT`;
79
+ usually this only helps if the operands exceed the width of the chip's DSP tile inputs.
80
+ The value, as with any other STAGE knob, is the number of extra cycles in the multiplier and also a manual split knob:
81
+ 0 - no extra stages and no manual splitting, let the synthesizer arrange the circuit automatically in a single cycle.
82
+ 1 - same as above but adds an operand capture register stage before the multiplier, which allows the synthesizer
83
+ to place a latch immediately before the DSP tile (or several if auto-split is happening) and in some cases (e.g. Vivado)
84
+ retime multiplication across two stages.
85
+ 2/3 - manually split the product into a 2x2/3x3 grid of sub-products (all use the operand-capture stage), summing
86
+ the partial products in as many registered reduction stages.
87
+ 4 - same 3x3 grid as 3, but the final partial-product reduction is itself pipelined into two registered stages.
88
+
89
+ Some modules that use multiplication offer the optional `WMULTIPLIER` parameter that defaults to zero,
90
+ but it can be set by the user to the argument width of the DSP tile multipliers available on-chip.
91
+ This enables the library to optimally split wide multiplication (when it doesn't fit into a single DSP tile)
92
+ across several tiles; this is significant when wide operands over 2x the native argument width are involved.
93
+ If not specified, the library will split wide multiplication into equal-width operands, which is not always optimal.
94
+ Common multiplier operand widths frequently found in FPGAs are 16, 18, and 24 bits.
95
+
96
+ Every sequential module exposes a `LATENCY` parameter. It is not a tuning knob; changing it does not change the hardware.
97
+ Its purpose is to let a latency-sensitive consumer pin down the latency it relies on: compute the value locally and pass it in.
98
+ The module fails synthesis if the supplied value disagrees with its real stage count, so an internal change that shifts
99
+ the latency cannot slip through unnoticed -- the build breaks and points you at the stale constant.
100
+ Pair `LATENCY` with `zkf_pipe` to delay your own control or sideband signals so they land with the operator's output.
101
+ A zero `LATENCY` is a special value indicating that the latency should not be checked (the default).
102
+
103
+ The `LATENCY` value is a sum of some constant baseline number of stages,
104
+ plus optionally some WMAN-dependent stage count, plus the sum of all `STAGE_*` values (all zero by default).
105
+
106
+ Generated lookup table ROMs are plain initialized Verilog arrays. They expose `ZKF_ATTRIBUTE_ROM_PRE` and
107
+ `ZKF_ATTRIBUTE_ROM_POST` as optional hooks around the ROM declaration for tool-specific attributes.
108
+ They may require overriding to enable correct ROM inference depending on the target chip/flow.
109
+
110
+ ### Catalogue
111
+
112
+ Notation: ⇝ - combinational, ⇻ - sequential, (nothing) - can be either depending on the selected `STAGE_`s;
113
+ II - initiation interval (cycles between accepting new inputs, reciprocal of cycle throughput;
114
+ 1 for zero-bubble pipelined modules).
115
+
116
+ | Module | | II | Function | Remarks |
117
+ |-----------------------|---|---------|----------------------------------------------------------------|-----------------------------|
118
+ | `zkf_abs` | ⇝ | | Absolute value. | |
119
+ | `zkf_neg` | ⇝ | | Negation. | May produce -0 (non-canonical)|
120
+ | `zkf_is_finite` | ⇝ | | True iff `x` is finite. | |
121
+ | `zkf_saturate` | ⇝ | | Replace ±∞ with the nearest finite of the same sign. | Does not canonicalize |
122
+ | `zkf_cmp` | ⇻ | 1 | Compare two values. | |
123
+ | `zkf_sort` | ⇻ | 1 | Min and max of two values. | Does not canonicalize |
124
+ | `zkf_add` | ⇻ | 1 | `a + b`. | |
125
+ | `zkf_addsub` | ⇻ | 1 | `a + b` or `a − b` selected by `op_sub` (trivial wrapper). | |
126
+ | `zkf_mul` | ⇻ | 1 | `a⋅b`. | |
127
+ | `zkf_mul_ilog2` | ⇻ | 1 | `a⋅2^k` for signed integer k (ldexp/scalbn). | |
128
+ | `zkf_mul_ilog2_const` | ⇻ | 1 | `a⋅2^K` for an elaboration-time signed integer `K`. | Const wins some fabric area |
129
+ | `zkf_div` | ⇻ | 1 | `a ÷ b`; flags divide-by-zero. | |
130
+ | `zkf_fma` | ⇻ | 1 | `(a⋅b) + c` fused multiply-add, high precision, rounded once. | Larger than separate mul->add; non-finite handling follows mul->add.|
131
+ | `zkf_from_int` | ⇻ | 1 | Cast signed two's-complement integer to float. | |
132
+ | `zkf_to_int` | ⇻ | 1 | Cast float to signed two's-complement integer with saturation. | RNTE |
133
+ | `zkf_resize` | | 1 | Cast between different float formats. | |
134
+ | `zkf_round` | | 1 | Round to integer in same format: RNTE/floor/ceil/trunc. | Outputs float; also see `zkf_to_int`|
135
+ | `zkf_exp2` | ⇻ | 1 | `2^x` | Faithful rounding, see below|
136
+ | `zkf_log2` | ⇻ | 1 | `log2(x)`; `domain_error` if `x<0`, `pole` if `x=0`. | Faithful rounding, see below|
137
+ | `zkf_sincos` | ⇻ |latency+1| `sin(2π⋅x)`, `cos(2π⋅x)` for `x` in turns; exposes `quadrant`. | Faithful rounding, see below|
138
+ | `zkf_atan2` | ⇻ |latency+1| `atan2(y,x)` in turns ∈ (−0.5,0.5] and `hypot(y,x)`. | Faithful rounding, see below|
139
+ | `zkf_pipe` | | 1 | Delay line of N register stages, W bits each. | No-op |
140
+
141
+ #### Notably absent functions
142
+
143
+ The transcendental/trigonometric functions offer high accuracy ≤1 ULP. This is desirable for many applications,
144
+ but often one would accept a lower accuracy (common in control systems) to save fabric and/or cycle latency.
145
+ There is interest in extending the module set with approximate trans/trig functions built on a simple
146
+ piecewise function approximation kernel that offer II=1, low cycle latency, and low fabric usage:
147
+ `zkf_exp2_approx`, `zkf_sincos_approx`, etc.
148
+
149
+ ### Derived functions
150
+
151
+ The basic modules available enable simple computation of a huge variety of derived functions; examples follow.
152
+ Bare angle functions follow the usual radian convention; helpers suffixed `_turns` expose ZKF's native turn
153
+ representation.
154
+
155
+ sin_turns(x), cos_turns(x) = zkf_sincos(x) ; x in turns
156
+ atan2_turns(y,x) = zkf_atan2(y, x) ; angle in turns ∈ (−0.5,0.5]
157
+ atan_turns(x) = atan2_turns(x, 1)
158
+
159
+ normalize_angle(x) = x − 2π⋅floor((x + π) / (2π)) ; [-π,+π)
160
+ normalize_angle_turns(t) = t − floor(t + 0.5) ; [-0.5,+0.5)
161
+
162
+ INV_TAU = 1 / (2π)
163
+ radians_to_turns(x) = x⋅INV_TAU
164
+
165
+ sin(x), cos(x) = sin_turns(radians_to_turns(x)), cos_turns(radians_to_turns(x))
166
+ atan2(y,x) = 2π⋅atan2_turns(y, x)
167
+
168
+ exp(x) = exp2(x⋅log2(e))
169
+ ln(x) = log2(x) / log2(e)
170
+ log10(x) = log2(x) / log2(10)
171
+ log_b(x) = log2(x) / log2(b) ; x>0, b>0, b≠1
172
+ pow(a,b) = exp2(b⋅log2(a)) ; real-valued identity for a>0
173
+ recip(x) = 1 / x
174
+ sqrt(x) = exp2(log2(x)⋅2^-1) ; x≥0; see zkf_mul_ilog2_const
175
+ rsqrt(x) = exp2(log2(x)⋅-2^-1) ; x>0; avoids division
176
+ cbrt(x) = sign(x)⋅exp2(log2(abs(x)) / 3)
177
+
178
+ tan(x) = sin(x) / cos(x)
179
+ atan(x) = atan2(x, 1)
180
+ asin(x) = atan2(x, sqrt(1 − x⋅x)) ; x ∈ [-1,+1]
181
+ acos(x) = atan2(sqrt(1 − x⋅x), x) ; x ∈ [-1,+1]
182
+ h = max(abs(x), abs(y))
183
+ hypot(x,y) = h=0 ? 0 : !is_finite(h) ? +∞ : h⋅sqrt((x/h)⋅(x/h) + (y/h)⋅(y/h))
184
+
185
+ min(a,b), max(a,b) = sort(a,b)
186
+ clamp(x, lo, hi) = min(max(x, lo), hi)
187
+ lerp(a,b,t) = fma(t, b − a, a)
188
+ deadzone(x,d) = sign(x)⋅max(abs(x) − d, 0)
189
+ smoothstep(t) = t⋅t⋅(3 − 2⋅t); where t is clamped to [0,1]
190
+
191
+ dot(a,b) = sum_i a[i]⋅b[i] ; use zkf_fma chains
192
+ norm2(x) = dot(x, x)
193
+ norm(x) = sqrt(norm2(x))
194
+ normalize(x) = x⋅rsqrt(norm2(x) + ε)
195
+ distance(a,b) = norm(a − b)
196
+ distance_2d(a,b) = hypot(a.x − b.x, a.y − b.y)
197
+
198
+ db_power(x) = 10⋅log10(x) ; x>0
199
+ db_amplitude(x) = 20⋅log10(abs(x)) ; x≠0
200
+ power_from_db(x) = exp2(x⋅log2(10) / 10)
201
+ amp_from_db(x) = exp2(x⋅log2(10) / 20)
202
+
203
+ sinc(x) = sin(π⋅x) / (π⋅x) ; normalized, sinc(0)=1
204
+ rms(x) = sqrt(mean(x⋅x))
205
+ ema(y,x,a) = fma(a, x − y, y)
206
+
207
+ complex_abs(re,im) = hypot(re, im)
208
+ arg(re,im) = atan2(im, re)
209
+ arg_turns(re,im) = atan2_turns(im, re)
210
+ unit_complex(t) = (cos(t), sin(t)) ; principal counterpart of arg()
211
+ polar(r,t) = (r⋅cos(t), r⋅sin(t)) ; r≥0
212
+ complex_mul((ar,ai),(br,bi)) = (ar⋅br − ai⋅bi, ar⋅bi + ai⋅br)
213
+ rotate2(x,y,t) = (x⋅cos(t) − y⋅sin(t), x⋅sin(t) + y⋅cos(t))
214
+
215
+ relu(x) = max(x, 0)
216
+ leaky_relu(x) = x ≥ 0 ? x : α⋅x
217
+ hard_sigmoid(x) = clamp(α⋅x + β, 0, 1)
218
+ hard_swish(x) = x⋅hard_sigmoid(x)
219
+
220
+ sigmoid(x) = 1 / (1 + exp2(−x⋅log2(e)))
221
+ tanh(x) = 2⋅sigmoid(2⋅x) − 1
222
+ softplus(x) = max(x, 0) + log2(1 + exp2(−abs(x)⋅log2(e))) / log2(e)
223
+ silu(x) = x⋅sigmoid(x)
224
+
225
+ m = max_i x[i]
226
+ logsumexp(x[]) = m + log2(sum_i exp2((x[i] − m)⋅log2(e))) / log2(e)
227
+ softmax_i(x[]) = exp2((x[i] − m)⋅log2(e)) / sum_j exp2((x[j] − m)⋅log2(e))
228
+ layer_norm(x) = (x − mean(x))⋅rsqrt(var(x) + ε)
229
+
230
+ And so on.
231
+
232
+ Generic floating-point remainder/modulo computation is not included because the general solution requires iterative
233
+ range reduction which maps poorly onto fixed-latency FPGA cores; instead, one can build the iterative solver using
234
+ the existing basic operators: zkf_fma, zkf_div, etc.
235
+
236
+ ## Semantics
237
+
238
+ Differences from IEEE 754: no NaN, no subnormals (exponent 0 always encodes +0; finite magnitudes in `(0, min_normal/2)`
239
+ round to +0; magnitudes in `[min_normal/2, min_normal)` round to signed min_normal), no −0, no exceptions,
240
+ overflow produces ±∞.
241
+
242
+ Infinity cases that would be NaN in IEEE 754:
243
+
244
+ | Expression | Result |
245
+ |---------------------|--------------------------------|
246
+ | +∞ + −∞ | +0 |
247
+ | 0⋅±∞ | +0 |
248
+ | 0 ÷ 0 | +0 |
249
+ | ±∞ ÷ ±∞ | +0 |
250
+
251
+ Non-NaN infinity cases (same intent as IEEE 754):
252
+
253
+ | Expression | Result |
254
+ |---------------------|--------------------------------|
255
+ | finite≠0 ÷ 0 | ±∞ (sign = sign of dividend) |
256
+ | ±∞ ÷ 0 | ±∞ (sign = sign of dividend) |
257
+ | finite ÷ ±∞ | +0 |
258
+ | ±∞⋅±∞ | ±∞ (sign = signs XOR) |
259
+ | finite≠0⋅±∞ | ±∞ (sign = signs XOR) |
260
+
261
+ The subnormal round-to-nearest behavior is illustrated below, compared against the basic flush to zero for any value
262
+ below the min normal. The timing/area cost of both approaches is approximately equivalent while the rounding method
263
+ halves the worst-case error.
264
+
265
+ ZKF only has a single canonical zero representation -- the positive zero. However, it is not an error to pass a
266
+ negative zero as an operand; the sign bit of a zero operand is simply ignored. This relaxation enables simplification
267
+ of certain basic operators.
268
+
269
+ <img src="docs/zkf_underflow_rounding.svg">
270
+
271
+ ### Accuracy of the transcendental functions
272
+
273
+ The exp2 and log2 transcendentals deliver faithful rounding (≤1 ULP guaranteed) with a 0.5 ULP correctly-rounded target,
274
+ enforced by two paired headroom budgets.
275
+
276
+ The per-segment Chebyshev fit + truncating Horner is sized to clear `< 2^-(WMAN+ERR_GUARD)` relative error with
277
+ ERR_GUARD = 8, i.e. between `2^-(ERR_GUARD+1) = 1/512` and `2^-ERR_GUARD = 1/256` of an ULP absolute across the
278
+ helper's `[1, 2)` interval — small enough that the round bit is structurally trustworthy,
279
+ so faithful rounding is automatic.
280
+
281
+ A mis-round against round-to-nearest-ties-to-even is possible only when the true value lies within `≈2^-(ERR_GUARD-1)`
282
+ ULP of a midpoint between adjacent representable values, bounding the worst-case mis-round rate at `≈2^-7 ≈ 0.8%`;
283
+ raising ERR_GUARD by one bit halves that rate at the cost of bumping the polynomial degree (and a Horner stage)
284
+ at some WMAN, but the Table-Maker's Dilemma rules out correctly-rounded-everywhere at WMAN = 53 regardless of budget,
285
+ so the 0.5 ULP target is best-effort while the ≤ 1 ULP bound is the hard contract.
286
+
287
+ The fixed-point datapath then carries GUARD = ERR_GUARD + 4 = 12 extra fractional bits below the WMAN significand
288
+ — eight bits of polynomial-noise headroom plus four bits to host the guard/round/sticky positions and absorb the
289
+ truncating Horner's LSB noise — which is the smallest split that keeps the round bit clear of the noise floor under
290
+ truncating arithmetic; widening it further has no accuracy benefit and just pays in DSP/LUT/FF area.
291
+
292
+ The trigonometric modules (sincos, atan2) carry the same ≤1 ULP contract and are built on a shared CORDIC core instead
293
+ of polynomials, with post-refinement to achieve the accuracy target trading a few DSP tiles for a lower cycle latency.
294
+
295
+ <img src="docs/zkf_transcendental_accuracy.svg">
296
+
297
+ **ATTENTION:** To achieve good results, it is essential to ensure that the look-up tables used by the
298
+ transcendental/trigonometric operators are correctly mapped to ROM. If you see unreasonable fabric usage and bad
299
+ timings, check your synthesis settings first, and if necessary override `ZKF_ATTRIBUTE_ROM_PRE` and
300
+ `ZKF_ATTRIBUTE_ROM_POST`.
301
+
302
+ ## Sizing the exponent and the significand (WEXP/WMAN)
303
+
304
+ WEXP can be chosen freely depending on the required range, while WMAN is sensitive to the chip's DSP capabilities
305
+ and thus requires careful selection to achieve best resource utilization.
306
+
307
+ |WMAN |≈ε (interval)| Description |
308
+ |-----|-------------|---------------------------------------------------------------------------------------------|
309
+ | 16 | 3.052e-05 | DSP tiles in Lattice iCE40 and similar |
310
+ | 18 | 7.629e-06 | Classic FPGA DSP width, very common: ECP5, PolarFire, Trion, many Intel modes, etc. |
311
+ | 24 | 1.192e-07 | IEEE 754 binary32; also fits Versal DSP58's 27x24 asymmetric multiplier side |
312
+ | 27 | 1.490e-08 | Intel/Altera variable-precision DSPs |
313
+ | 32 | 4.657e-10 | 2x16 |
314
+ | 36 | 2.910e-11 | 2x18 (very common) or native Intel/Altera 36x36-style variable-precision mode |
315
+ | 48 | 7.105e-15 | 2x24 or 3x16; with an 8-bit exponent amounts to 7 bytes exactly |
316
+ | 53 | 2.220e-16 | IEEE 754 binary64 |
317
+
318
+ Narrower WMAN is rarely practical for computation due to low precision and fast error accumulation,
319
+ although they can still be useful for storage/exchange. One notable exception is neural networks though.
320
+
321
+ ### WMAN=18
322
+
323
+ An FPGA-friendly format because modern DSP-enabled FPGAs often implement 18x18 bit multipliers, which means that a
324
+ narrower mantissa is unlikely to save much resources or nontrivially improve timings as long as hardware multipliers
325
+ are used.
326
+
327
+ One can stay within 24 bits total by choosing WEXP=6:
328
+
329
+ WEXP=6 WMAN=18 WFRAC=17 WFULL=24 BIAS=31
330
+ lowest = 1/1073741824 ≈ 9.313e-10
331
+ max = 0xFFFF_C000 ≈ 4.295e+09
332
+ ε = 1/131072 ≈ 7.629e-06
333
+
334
+ ### WMAN=36
335
+
336
+ Similar to the above, WMAN=36 is efficient on common FPGAs because it maps multiplication to four 18x18 DSP slices.
337
+ This is often a better fit for intermediate result representation to avoid error accumulation --
338
+ the precision lands halfway between IEEE 754 binary64 and binary32.
339
+
340
+ Usually, on an 18x18 DSP chip, going even a single bit higher causes f_max to tank dramatically while area explodes.
341
+ Thus this is likely to be the optimal choice for a large number of applications.
342
+
343
+ Using binary32-compatible exponent WEXP=8, 44 bits total (5.5 bytes):
344
+
345
+ WEXP=8 WMAN=36 WFRAC=35 WFULL=44 BIAS=127
346
+ lowest = 1/85070591730234615865843651857942052864 ≈ 1.175e-38
347
+ max = 0xFFFFFFFF_F0000000_00000000_00000000 ≈ 3.403e+38
348
+ ε = 1/34359738368 ≈ 2.910e-11
349
+
350
+ ### IEEE 754-like
351
+
352
+ ZKF offers limited compatibility with IEEE 754 so while it can match the bit layout,
353
+ not all states are mappable between the formats.
354
+
355
+ - WEXP=5 WMAN=11: IEEE 754 binary16-like
356
+ - WEXP=8 WMAN=24: IEEE 754 binary32-like
357
+ - WEXP=11 WMAN=53: IEEE 754 binary64-like