├── .gitignore ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── README.tpl ├── doc └── genlut.md ├── examples └── multithreaded.rs ├── src ├── emu.rs ├── genlut.rs ├── lib.rs ├── load_store.rs ├── nativectx.rs ├── nativeops.rs ├── ops.rs └── regs.rs └── tests ├── genlut.rs ├── load_store.rs └── outer_prod.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "amx" 3 | version = "0.0.0" 4 | authors = ["yvt "] 5 | edition = "2018" 6 | license = "MIT/Apache-2.0" 7 | 8 | [features] 9 | default = ["either", "doc_cfg"] 10 | doc_cfg = [] 11 | 12 | [dependencies] 13 | either = { version = "1.6.1", optional = true } 14 | cfg-if = "1" 15 | 16 | [dev-dependencies] 17 | quickcheck_macros = "0.9.1" 18 | aligned_box = "0.2.0" 19 | env_logger = "0.7" 20 | quickcheck = "0.9.2" 21 | itertools = "0.10.0" 22 | either = "1.6.1" 23 | clap = { version = "4.4.8", features = ["derive"] } 24 | log = "0.4.11" 25 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 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. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright 2021 yvt 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # `amx` 2 | 3 | [docs.rs](https://docs.rs/amx/) 4 | 5 | Rust wrapper for Apple Matrix Coprocessor (AMX) instructions 6 | 7 | This crate provides wrapper functions for the undocumented AMX instructions, 8 | which are found in Apple Silicon processors. 9 | 10 | ## Resources 11 | 12 | - 13 | - 14 | 15 | ## Example 16 | 17 | ```rust 18 | use amx::{Amx, XRow, YRow, XBytes, YBytes, ZRow}; 19 | let mut ctx = amx::AmxCtx::new().unwrap(); 20 | let x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 21 | 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32i16]; 22 | let y = [51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 23 | 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82i16]; 24 | unsafe { ctx.load512(x.as_ptr(), XRow(0)) }; 25 | unsafe { ctx.load512(y.as_ptr(), YRow(0)) }; 26 | ctx.outer_product_i16_xy_to_z( 27 | Some(XBytes(0)), // input from X starting from byte offset 0 28 | Some(YBytes(0)), // input from Y starting from byte offset 0 29 | ZRow(0), // output to Z starting from row offset 0 30 | false, // don't accumulate 31 | ); 32 | let z: [[i16; 32]; 64] = unsafe { std::mem::transmute(ctx.read_z()) }; 33 | for (x_i, &x) in x.iter().enumerate() { 34 | for (y_i, &y) in y.iter().enumerate() { 35 | assert_eq!(z[y_i * 2][x_i], x * y); 36 | } 37 | } 38 | ``` 39 | 40 | ## Registers 41 | 42 | ```rust 43 | struct AmxState { 44 | /// "8 64-byte registers" 45 | x: [[u8; 64]; 8], 46 | /// "8 64-byte registers" 47 | y: [[u8; 64]; 8], 48 | /// "64 64-byte registers in an M-by-N matrix" 49 | z: [[u8; 64]; 64], 50 | } 51 | ``` 52 | 53 | License: MIT/Apache-2.0 54 | -------------------------------------------------------------------------------- /README.tpl: -------------------------------------------------------------------------------- 1 | # `{{crate}}` 2 | 3 | [docs.rs](https://docs.rs/amx/) 4 | 5 | {{readme}} 6 | 7 | License: {{license}} 8 | -------------------------------------------------------------------------------- /doc/genlut.md: -------------------------------------------------------------------------------- 1 | # ` ` Reverse, 4-bit indices, 32-bit floats 2 | 3 | the first 8 bytes are set 4 | 5 | got = [13, ff, 1f, f1, ff, ff, ff, ff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 6 | values = [36, 51, 41, 11, 3c, 9, 52, 1c, 36, 5a, 5, 33, 2f, 6, 2d, 2d, 51, 21, d, 42, 5c, b, 43, 14, 3d, 2a, 52, 20, 5c, 33, 47, 37, 49, 3, 45, 42, 42, 54, 32, 39, 17, 55, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 7 | indices = [3c, 55, 24, 3e, 4b, 52, 3f, 24, 5b, 4, 29, 5c, 45, 14, 19, 1, 34, 3, 5e, 4e, 47, 9, 0, 1d, 30, 55, 29, 24, 58, 40, 35, b] 8 | 9 | got = [5c, ff, 35, ff, ff, ff, ff, ff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 10 | values = [0, 0, 2, 3, 5, 6, 8, b, d, e, f, 11, 15, 16, 17, 1b, 1d, 20, 22, 24, 25, 27, 29, 2b, 2e, 2f, 2f, 31, 31, 34, 35, 3a, 3e, 3f, 41, 42, 42, 44, 44, 44, 45, 47, 49, 4b, 4c, 4c, 4d, 4e, 4f, 4f, 4f, 50, 52, 53, 53, 56, 59, 5b, 5c, 60, 60, 61, 62, 62] 11 | indices = [1, 21, 61, 55, 4f, 46, 2d, 2c, 0, 0, 0, 0, 0, 0, 0, 0, 13, 4, 5f, 30, 3b, 53, 28, 1e, 0, 0, 0, 0, 0, 0, 0, 0] 12 | 13 | values = [ 14 | 04010100, 0c0b0807, 0f0f0e0e, 16141110, 15 | 0 16 | 1d1d1b16, 2221211d, 2b262523, 2f2e2d2c, 17 | 4 18 | 3c3c3231, 433f3e3c, 47464545, 4b4a4a47, 19 | 8 20 | 534f4e4c, 59585755, 5e5c5a5a, 63605f5e 21 | c d e f 22 | ]: [u32] (pre-sorted) 23 | indices = [0, 25000000, 0, 31000000, 0, 2d000000, 0, 08000000, 0, 32000000, 0, 5f000000, 0, 4e000000, 0, 0]: [u32] 24 | got = [5f, 7f, 6f, f, 7f, ef, bf, ff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 25 | 26 | values = [ 27 | 08080705, 0c0b0b09, 0f0f0d0c, 1a191812, 1d1d1b1b, 2722221e, 2e2e2b28, 31313030, 28 | 36363533, 39383836, 403c3b3b, 44434140, 49484745, 514f4d4b, 5b585753, 62615d5b]: [u32] (pre-sorted) 29 | indices = [0, 5f000000, 0, 4b000000, 0, 52000000, 0, 32000000, 0, f000000, 0, 5c000000, 0, 34000000, 0, 0]: [u32] 30 | got = [ef, cf, df, 7f, 1f, ef, 7f, ff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 31 | 32 | When the values (LUT) are unsorted, it returns "the minimum index whose element is greater than the given value" minus 1 (wrapping around to `0xf` on overflow). 33 | 34 | values = [ 35 | 161d0127, 13225d55, 55503e43, c400755, 3c03093d, 585a5959, 421d543c, 5428191c, 36 | 611b4a5e, 424d6124, 0f40013b, 0601221, 00000000, 00000000, 00000000, 00000000]: [u32] 37 | indices = [ 38 | 0, 09000000, 0, 08000000, 0, 4c000000, 0, 37000000, 0, 5c000000, 0, 61000000, 0, 0, 0, 0]: [u32] 39 | f f f f f 1 f 1 f 7 f f 40 | got = [ff, ff, 1f, 1f, 7f, 7f, ff, ff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 41 | 42 | values = [ 43 | 14000000, 0e000000, 2b000000, 34000000, 32000000, 1d000000, 5e000000, 47000000, 44 | 0 45 | 2e000000, 46000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000]: [u32] 46 | 8 47 | indices = [ 48 | 19000000, 31000000, 4e000000, 46000000, 5a000000, 18000000, 10000000, 0, 49 | 1 2 5 5 5 1 f f 50 | 0, 0, 0, 0, 0, 0, 0, 0]: [u32] 51 | got = [21, 55, 15, ff, ff, ff, ff, ff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 52 | 53 | # ` [53]` Reverse, 5-bit indices, 16-bit floats 54 | 55 | the first 20 bytes are set 56 | 57 | got = [0, 80, f, 0, f8, e0, 3, 4, 0, 0, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 58 | values = [30, 1a, 19, 5d, 1c, 47, 1b, 27, 5c, 49, 40, 30, 10, 3d, 4f, a, c, 45, 2a, 62, 49, e, 29, c, 15, 2, 1c, 33, 32, 1a, 23, 55, 5a, 45, c, 4d, 12, 38, c, 41, 29, 39, 16, 57, 4f, 27, 3c, 8, 5c, 10, 4d, 61, 2a, 1b, 5b, 21, 1c, 60, 4f, 16, 26, 3a, 2d, 28] 59 | indices = [5a, 55, 57, 41, 33, 59, 3, 6, 5a, 3e, 19, 22, 2, 1f, c, 9, 58, 22, 11, 19, 27, 46, 16, 60, 59, 1b, 4c, 32, 60, 34, 40, 34] 60 | 61 | # ` [54] ` Reverse, 4-bit indices, 64-bit floats 62 | 63 | # ` [54][53]` Reverse, 4-bit indices, 32-bit signed integers 64 | 65 | the first 8 bytes are set 66 | 67 | got = [f3, ff, ff, 3f, ff, ff, ff, ff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 68 | 11001111 11111111 11111111 11001111 11111111 11111111 11111111 69 | values = [6, 51, 16, 32, 1b, 4c, 2d, 44, 38, 4e, 52, 34, 60, 39, 49, 14, 57, f, 4a, 62, 2d, 4b, 6, 2a, 19, 10, 61, 1f, 12, 3e, 3d, 9, 9, 56, 5a, 2a, 57, 3a, 51, 5c, 10, d, f, 42, 3, 0, 41, 2d, 38, 63, 4c, 23, 3b, 5, 24, 3d, 22, 50, 5, 4a, 3e, 3c, 42, 24] 70 | indices = [49, a, 57, 55, 39, 11, 61, 22, 4f, 24, 2f, d, 4a, 2a, 49, 1b, 36, 27, 60, 4, 34, 21, 2c, 5, 42, b, 54, 24, 4d, a, 56, 4c] 71 | 72 | # ` [55] ` Reverse, 5-bit indices, 16-bit signed integers 73 | 74 | the first 20 bytes are set 75 | 76 | got = [3f, fc, 1f, 42, f8, e1, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 77 | 1111 1100 0011 1111 1111 1000 0100 0010 0010 1111 1000 0111 1111 1111 1111 1111 1111 1111 ... 78 | values = [56, 2f, 1f, 2c, 5, 3c, 5d, 12, 34, 11, 18, 41, 57, 8, 30, 30, 5d, 45, 29, 4b, 53, 3a, 49, 37, 3f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 79 | indices = [4e, 4f, 13, 35, 8, f, 25, 5, 2b, 31, 26, 35, 52, 39, 23, 4e, 3d, 3a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 80 | 81 | # ` [55] [53]` Reverse, 4-bit indices, 32-bit something ??? 82 | 83 | the first 8 bytes are set 84 | 85 | got = [f3, 3f, ff, 63, ff, ff, ff, ff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 86 | values = [2a, 2b, 2f, 2f, 5f, 6, 10, 4, 51, 43, 32, 9, 45, 28, 22, 1b, 4f, 5a, 45, 4e, e, 9, 2e, b, 35, 52, 58, 4c, 42, 12, 46, 63, 18, 11, 50, 1, 48, 3, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 87 | indices = [42, 5b, 1a, 3e, 3d, 53, 3, 21, 61, f, 9, 27, 28, 3e, 1, 33, 36, 37, 4f, 12, 52, 5e, 10, a, 3, 14, 5a, 45, 27, 41, 52, 59] 88 | 89 | 90 | 91 | # ` [55][54] ` Reverse, 5-bit indices, 16-bit unsigned integers 92 | 93 | the first 20 bytes are set 94 | 95 | got = [61, 8, f1, c7, 18, 41, 88, 20, 44, 8, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 96 | values = [3f, d, 36, b, 31, 28, f, 49, 1a, 59, 10, 6, 36, c, 4a, 39, 32, 5c, 7, 36, e, 60, 45, 29, 43, 5e, 49, 10, 46, 45, 63, 42, 21, 38, 51, 1f, 37, 36, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 97 | indices = [29, 20, 63, 50, 62, 33, 12, 29, 34, 3, 4f, 4c, 44, 50, 55, 51, 49, 1e, 1, 30, 58, 33, 17, 23, 61, 48, 8, 2e, 48, 18, 40, e] 98 | 0100 1001 0000 0100 1100 0110 0000 1010 0100 0110 99 | 100 | got = [e2, 8b, ff, ff, ff, ff, ff, ff, cb, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 101 | values = [46, 34, 61, 2a, 26, 13, 36, 41, 1f, b, 1c, 45, 41, 45, 17, 21, d, 22, 3f, a, 22, 3c, 1d, 3d, 23, 20, 60, 1e, 63, 4e, 63, 6, 59, 7, b, 1b, 1c, 42, 3b, 37, 55, 47, 1e, 13, f, 15, d, 32, 4e, 58, 5b, 19, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 102 | indices = [18, 41, 5b, 1c, 21, 38, 5d, d, 4f, 59, 57, 61, 31, 2, 53, 2d, 19, 34, 40, 9, 28, 5d, 3d, a, 15, e, 28, 45, d, 1e, 4a, 1d] 103 | 104 | 105 | # ` [55][54][53]` 2-bit indices, 32-bit values 106 | 107 | got = [54, 25, 2, 6, -- 0 108 | 20, 34, 8, 1, -- 1 109 | 20, 34, 8, 1, -- 1 110 | 54, 25, 2, 6, -- 0 111 | 3b, 41, d, 17, -- 3 112 | 54, 25, 2, 6, -- 0 113 | 54, 25, 2, 6, -- 0 114 | 54, 25, 2, 6, -- 2 115 | 1b, 3a, 25, 5e, 54, 25, 2, 6, 54, 25, 2, 6, 20, 34, 8, 1, 1b, 3a, 25, 5e, 54, 25, 2, 6, 20, 34, 8, 1, 20, 34, 8, 1] 116 | idx: 0 117 | values = [54, 25, 2, 6, 20, 34, 8, 1, 1b, 3a, 25, 5e, 3b, 41, d, 17, 4c, 1d, 4a, 2e, 50, 46, 2c, 15, 61, 62, 46, 11, 29, 37, 59, 37, 59, 33, 4f, 40, 20, 54, 38, 18, 40, 54, 2, 19, 4b, 4c, 53, 3c, 11, f, 1e, 22, b, 28, 5b, 44, b, 3d, 58, 2b, 41, 2c, 8, 10] 118 | indices = [14, 3, 42, 52, 11, 4d, 4d, 37, 62, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 119 | 120 | # `[56] ` 2-bit indices, 16-bit values, 121 | 122 | values = [2f, b, 32, 8, 40, 55, 23, 61, 34, 18, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 123 | indices = [52, 26, c, 3d, a, 30, 4e, d, 49, 1f, e, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 124 | 0100 1010 0100 0110 0101 0000 0011 1101 125 | got = [40, 55, 2f, b, 32, 8, 32, 8, 40, 55, 32, 8, 40, 55, 2f, b, 2f, b, 23, 61, 2f, b, 2f, b, 32, 8, 23, 61, 23, 61, 2f, b, 40, 55, 40, 55, 2f, b, 2f, b, 2f, b, 2f, b, 23, 61, 2f, b, 40, 55, 23, 61, 2f, b, 32, 8, 32, 8, 23, 61, 2f, b, 2f, b] 126 | idx: 2 0 1 1 127 | 01001010 128 | 129 | # `[56] [53]` 2-bit indices, 8-bit values 130 | 131 | values = [5b, 1, 3d, b, 57, 20, 4c, 4a, 30, 53, 63, 21, 4f, 42, 8, 55, 60, 5, c, 4c, 2a, 62, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 132 | indices = [32, e, 42, 17, 10, 25, 5c, 5b, 6, 30, 0, 13, 15, 37, 22, 13, 1e, 48, 2d, 1, 19, 42, 18, e, 17, 33, 5, 58, 4b, 19, 54, 37] 133 | 01001100 134 | got = [3d, 5b, b, 5b, 3d, b, 5b, 5b, 3d, 5b, 5b, 1, b, 1, 1, 5b, 5b, 5b, 1, 5b, 1, 1, 3d, 5b, 5b, b, 1, 1, b, 3d, 1, 1, 3d, 1, 5b, 5b, 5b, 5b, b, 5b, 5b, 5b, 5b, 5b, b, 5b, 1, 5b, 1, 1, 1, 5b, b, 1, b, 5b, 3d, 5b, 3d, 5b, b, 5b, 1, 5b] 135 | 2 0 3 0 136 | 137 | # `[56] [54] ` 3-bit indices (with 1-bit padding), 64-bit values 138 | 139 | 140 | values = [3, 38, 2, 50, 46, 9, 1d, 2e, 3d, 5e, 23, 43, 5e, 5b, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 141 | indices = [1a, 18, 35, 27, a, 55, 16, 3d, 4e, 3c, 2f, 57, 21, 35, b, 5b, 36, 39, 16, 42, 19, b, 1c, 35, 40, 4a, 29, 17, 58, 4e, 31, 1f] 142 | got = [0, 0, 0, 0, 0, 0, 0, 0, 3d, 5e, 23, 43, 5e, 5b, 50, 0, 3, 38, 2, 50, 46, 9, 1d, 2e, 3d, 5e, 23, 43, 5e, 5b, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 143 | idx 8 9/c a 144 | 145 | values = [24, 13, 46, 2f, 45, 2b, 49, 62, 45, 44, 5f, 5b, b, 3c, 48, b, 5b, 2a, 20, 2, 18, c, 4f, 13, 35, 25, 3b, 4f, 46, 2b, 1a, 1f, 54, 26, 4b, f, 56, 45, 21, 57, 59, 9, 22, 35, 49, 56, 47, a, 5e, 63, 0, 1b, 3d, d, 7, 4f, 12, 61, 0, 0, 0, 0, 0, 0] 146 | indices = [49, 3a, 52, 55, 47, 3c, 47, 36, 3c, 10, 51, 3b, a, 4d, 18, 3c, 46, 8, 46, 3f, 1f, 2d, 46, a, 2c, 1d, 1, a, 30, 36, 52, 34] 147 | 1001 0010 0101 1100 0100 1010 1010 1010 148 | got = [45, 44, 5f, 5b, b, 3c, 48, b, 149 | 54, 26, 4b, f, 56, 45, 21, 57, 150 | 5b, 2a, 20, 2, 18, c, 4f, 13, 151 | 35, 25, 3b, 4f, 46, 2b, 1a, 1f, 152 | 5b, 2a, 20, 2, 18, c, 4f, 13, 153 | 59, 9, 22, 35, 49, 56, 47, a, 154 | 59, 9, 22, 35, 49, 56, 47, a, 155 | 59, 9, 22, 35, 49, 56, 47, a] 156 | idx: 1 4 2 3 2 5 5 5 157 | 100 001 010 110 010 158 | 159 | # `[56] [54][53]` 4-bit indices, 32-bit values 160 | 161 | got = [28, 5e, 32, 58, 9, 2c, 15, 1a, 2f, 16, 5d, 60, 2a, 48, 19, 47, 36, 1f, 3a, 33, 2a, 48, 19, 47, 4e, 54, 4a, 56, 2a, 48, 19, 47, 28, 1, 13, 54, 42, 18, 40, 3a, 28, 5e, 32, 58, 9, 2c, 15, 1a, 4e, 54, 4a, 56, 4e, 54, 4a, 56, 2f, 16, 5d, 60, 28, 5e, 32, 58] 162 | idx: 2 5 d 163 | values = [2a, 48, 19, 47, 42, 18, 40, 3a, 28, 5e, 32, 58, 4e, 54, 4a, 56, 36, 1f, 3a, 33, 9, 2c, 15, 1a, 17, 52, 5, 4b, 46, 10, 4c, 42, 63, 3b, 0, 3, 54, 5c, 2f, 1a, 46, 34, 49, 4a, 26, 22, 21, 20, 28, 1, 13, 54, 2f, 16, 5d, 60, 22, 52, 3c, 20, 2e, f, 3e, 5f] 164 | indices = [52, d, 4, 3, 1c, 52, 33, 2d, 34, 29, 59, 9, 19, 58, 7, 6, 5a, 58, 29, 2, 20, 22, 10, 27, 46, 39, 23, 43, 37, 54, 4f, 63] 165 | 166 | # `[56][55] ` 4-bit indices, 16-bit values 167 | 168 | got = [11, 3b, d, 51, f, 13, d, 51, f, 13, d, 51, 11, 3b, 2a, 37, 3e, 25, 11, 3b, 2a, 37, 2a, 37, 1f, 57, 4d, 4c, 4e, 55, 41, 51, 1f, 169 | idx: 5 0 e 0 e 170 | values = [d, 51, 1f, 57, 2a, 37, 4d, 4c, 41, 51, 11, 3b, 5d, 0, 3e, 5b, 4e, 55, 41, 62, b, 4, 2f, 8, 9, 52, 3e, 25, f, 13, 52, 4c, 57, c, 4a, 2e, 50, 3b, 1f, 2d, 4a, 1e, c, 34, 19, 41, 3a, 2c, a, 3b, 2d, 3e, 1b, 12, 24, 3e, 5a, 4f, 1f, 28, 1f, 2c, 51, 14] 171 | indices = [5, e, e, 25, 5d, 22, 31, 48, 61, 22, 2e, a, 16, 5c, 4b, 3f, 46, 16, 45, 1a, 2b, 2c, 62, 60, 1e, 19, 47, 0, 0, 0, 0, 0] 172 | 173 | # `[56][55] [53]` 4-bit indices, 8-bit values 174 | 175 | # `[56][55] [53][25]` 4-bit indices, 8-bit values, output to Y 176 | 177 | 178 | # `[56][55] [53][26]` 4-bit indices, 8-bit values, output to Z 179 | 180 | The result goes to Z instead 181 | 182 | # `[56][55] [53][25][26]` 4-bit indices, 8-bit values, output to Z 183 | 184 | The result goes to Z instead, the output row incremented by `32` 185 | 186 | z = [ 187 | [0, 0, 0, 0, 0, 0, 0, 0], 188 | [0, 0, 0, 0, 0, 0, 0, 0], 189 | [0, 0, 0, 0, 0, 0, 0, 0], 190 | [0, 0, 0, 0, 0, 0, 0, 0], 191 | [0, 0, 0, 0, 0, 0, 0, 0], 192 | [0, 0, 0, 0, 0, 0, 0, 0], 193 | [0, 0, 0, 0, 0, 0, 0, 0], 194 | [0, 0, 0, 0, 0, 0, 0, 0], 195 | [0, 0, 0, 0, 0, 0, 0, 0], 196 | [0, 0, 0, 0, 0, 0, 0, 0], 197 | [0, 0, 0, 0, 0, 0, 0, 0], 198 | [0, 0, 0, 0, 0, 0, 0, 0], 199 | [0, 0, 0, 0, 0, 0, 0, 0], 200 | [0, 0, 0, 0, 0, 0, 0, 0], 201 | [0, 0, 0, 0, 0, 0, 0, 0], 202 | [0, 0, 0, 0, 0, 0, 0, 0], 203 | [0, 0, 0, 0, 0, 0, 0, 0], 204 | [0, 0, 0, 0, 0, 0, 0, 0], 205 | [0, 0, 0, 0, 0, 0, 0, 0], 206 | [0, 0, 0, 0, 0, 0, 0, 0], 207 | [0, 0, 0, 0, 0, 0, 0, 0], 208 | [0, 0, 0, 0, 0, 0, 0, 0], 209 | [0, 0, 0, 0, 0, 0, 0, 0], 210 | [0, 0, 0, 0, 0, 0, 0, 0], 211 | [0, 0, 0, 0, 0, 0, 0, 0], 212 | [0, 0, 0, 0, 0, 0, 0, 0], 213 | [0, 0, 0, 0, 0, 0, 0, 0], 214 | [0, 0, 0, 0, 0, 0, 0, 0], 215 | [0, 0, 0, 0, 0, 0, 0, 0], 216 | [0, 0, 0, 0, 0, 0, 0, 0], 217 | [0, 0, 0, 0, 0, 0, 0, 0], 218 | [0, 0, 0, 0, 0, 0, 0, 0], 219 | [0, 0, 1, 0, 2, 0, 3, 0], 220 | [0, 0, 0, 0, 0, 0, 0, 0], 221 | [0, 0, 0, 0, 0, 0, 0, 0], 222 | [0, 0, 0, 0, 0, 0, 0, 0], 223 | [0, 0, 0, 0, 0, 0, 0, 0], 224 | [0, 0, 0, 0, 0, 0, 0, 0], 225 | [0, 0, 0, 0, 0, 0, 0, 0], 226 | [0, 0, 0, 0, 0, 0, 0, 0], 227 | [0, 0, 0, 0, 0, 0, 0, 0], 228 | [0, 0, 0, 0, 0, 0, 0, 0], 229 | [0, 0, 0, 0, 0, 0, 0, 0], 230 | [0, 0, 0, 0, 0, 0, 0, 0], 231 | [0, 0, 0, 0, 0, 0, 0, 0], 232 | [0, 0, 0, 0, 0, 0, 0, 0], 233 | [0, 0, 0, 0, 0, 0, 0, 0], 234 | [0, 0, 0, 0, 0, 0, 0, 0], 235 | [0, 0, 0, 0, 0, 0, 0, 0], 236 | [0, 0, 0, 0, 0, 0, 0, 0], 237 | [0, 0, 0, 0, 0, 0, 0, 0], 238 | [0, 0, 0, 0, 0, 0, 0, 0], 239 | [0, 0, 0, 0, 0, 0, 0, 0], 240 | [0, 0, 0, 0, 0, 0, 0, 0], 241 | [0, 0, 0, 0, 0, 0, 0, 0], 242 | [0, 0, 0, 0, 0, 0, 0, 0], 243 | [0, 0, 0, 0, 0, 0, 0, 0], 244 | [0, 0, 0, 0, 0, 0, 0, 0], 245 | [0, 0, 0, 0, 0, 0, 0, 0], 246 | [0, 0, 0, 0, 0, 0, 0, 0], 247 | [0, 0, 0, 0, 0, 0, 0, 0], 248 | [0, 0, 0, 0, 0, 0, 0, 0], 249 | [0, 0, 0, 0, 0, 0, 0, 0], 250 | [0, 0, 0, 0, 0, 0, 0, 0]] 251 | 252 | # `[54][55][54] ` 5-bit indices, 16-bit values 253 | 254 | got = [2c, 3f; 55, 43; 31, 3c; 3d, 5c; 3d, 5c; 46, 61; 3d, 5c; 19, 4e; 43, 10; 0, 0; 1c, 47, 0, 0, 30, 50, 33, 56, 15, f, 31, 3c, 33, 56, 23, 41, 15, f, 30, 50, 54, 13, 2c, 3f, 54, 13, 2e, e, 0, 0, 0, 0, 1c, 57, 2e, e, 60, 4e, 37, 2e, 2c, 3f, 31, 3c] 255 | idx: 5 1 a 0 0 e 0 3 6 ? d 256 | 101 1 0101 0 0 0111 0 11 011 257 | values = [3d, 5c, 55, 43, 23, 41, 19, 4e, 2e, e, 2c, 3f, 43, 10, 4e, 3c, 37, 2e, 1c, 57, 31, 3c, 5d, 1e, 42, 25, 1c, 47, 46, 61, 13, 49, 15, f, 11, 53, 30, 50, 60, 4e, 22, 50, 54, 13, 39, 5d, 33, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 258 | indices = [25, 28, 0, 1c, 18, 26, 37, 2d, 2f, 54, 57, 40, 59, 4b, 25, 19, 27, 32, 51, 51, 16, 1d, 4e, 5a, 19, 1, 59, 0, 0, 0, 0, 0] 259 | 1010 0100 0001 0100 0000 0000 0011 1000 0001 1000 0110 0100 260 | ^ ^ ^ ^ ^ ^ ^ ^ ^ 261 | 5 1 a 0 0 e 0 3 6 262 | 263 | # `[56][55][54][53]` 5-bit indices, 8-bit values 264 | 265 | got = [54, 1c, 34, 9, 0, 36, 25, f, 25, 25, 4e, 16, 1b, 4f, 25, 59, 4f, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d] 266 | idx: 9 18 a 4 ? 1c d/10 267 | 1001 00011 0101 001 ? 00111 1011/0101 268 | values = [4d, 37, f, 18, 9, 61, 4e, 59, 4f, 54, 34, 21, 2d, 25, 16, 35, 25, 0, 2f, 45, 1b, 3, 37, 18, 1c, a, 54, 5e, 36, 1e, 51, 2b, 20, 62, 5, 21, 37, 7, 38, 27, 3c, 4f, 2, b, 5f, 39, 3, 10, 41, 63, 3, 4b, 41, c, 39, 63, 51, 50, 29, 32, 15, 0, 3, 9] 269 | indices = [9, 2b, 12, 39, 14, 10, 1a, 47, 51, 3b, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 270 | 1001 0000 1101 0100 0100 1000 1001 1100 271 | 9 18 a 4 11 1c 272 | -------------------------------------------------------------------------------- /examples/multithreaded.rs: -------------------------------------------------------------------------------- 1 | use amx::{prelude::*, XBytes, YBytes, ZRow}; 2 | use clap::Parser; 3 | use std::time::Instant; 4 | 5 | #[derive(Debug, Parser)] 6 | #[command(author, version, about, long_about = None)] 7 | struct Opts { 8 | /// Number of threads to launch 9 | #[arg(short, long)] 10 | num_threads: usize, 11 | } 12 | 13 | fn main() { 14 | let opts = Opts::parse(); 15 | println!("Launching {} threads with AMX enabled", opts.num_threads); 16 | 17 | for i in 1..opts.num_threads { 18 | std::thread::spawn(move || stress_loop(i)); 19 | } 20 | stress_loop(0); 21 | } 22 | 23 | #[inline(never)] 24 | fn stress_loop(tid: usize) { 25 | let mut ctx = amx::AmxCtx::new().unwrap(); 26 | 27 | loop { 28 | let start = Instant::now(); 29 | let count = 10_000_000; 30 | for _ in 0..count / 16 { 31 | ctx.outer_product_i16_xy_to_z(Some(XBytes(0)), Some(YBytes(0)), ZRow(0), true); 32 | ctx.outer_product_i16_xy_to_z(Some(XBytes(0)), Some(YBytes(0)), ZRow(1), true); 33 | ctx.outer_product_i16_xy_to_z(Some(XBytes(0)), Some(YBytes(0)), ZRow(0), true); 34 | ctx.outer_product_i16_xy_to_z(Some(XBytes(0)), Some(YBytes(0)), ZRow(1), true); 35 | ctx.outer_product_i16_xy_to_z(Some(XBytes(0)), Some(YBytes(0)), ZRow(0), true); 36 | ctx.outer_product_i16_xy_to_z(Some(XBytes(0)), Some(YBytes(0)), ZRow(1), true); 37 | ctx.outer_product_i16_xy_to_z(Some(XBytes(0)), Some(YBytes(0)), ZRow(0), true); 38 | ctx.outer_product_i16_xy_to_z(Some(XBytes(0)), Some(YBytes(0)), ZRow(1), true); 39 | 40 | ctx.outer_product_i16_xy_to_z(Some(XBytes(0)), Some(YBytes(0)), ZRow(0), true); 41 | ctx.outer_product_i16_xy_to_z(Some(XBytes(0)), Some(YBytes(0)), ZRow(1), true); 42 | ctx.outer_product_i16_xy_to_z(Some(XBytes(0)), Some(YBytes(0)), ZRow(0), true); 43 | ctx.outer_product_i16_xy_to_z(Some(XBytes(0)), Some(YBytes(0)), ZRow(1), true); 44 | ctx.outer_product_i16_xy_to_z(Some(XBytes(0)), Some(YBytes(0)), ZRow(0), true); 45 | ctx.outer_product_i16_xy_to_z(Some(XBytes(0)), Some(YBytes(0)), ZRow(1), true); 46 | ctx.outer_product_i16_xy_to_z(Some(XBytes(0)), Some(YBytes(0)), ZRow(0), true); 47 | ctx.outer_product_i16_xy_to_z(Some(XBytes(0)), Some(YBytes(0)), ZRow(1), true); 48 | } 49 | let rate = count as f64 / start.elapsed().as_secs_f64(); 50 | println!("[{:3}] {:2} amxmac16s per second", tid, rate); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/emu.rs: -------------------------------------------------------------------------------- 1 | //! AMX emulation 2 | use crate::ops::AmxOps; 3 | 4 | /// An emulated AMX context. 5 | #[derive(Default, Debug, Copy, Clone)] 6 | pub struct AmxEmuCtx { 7 | st: AmxSt, 8 | } 9 | 10 | impl AmxEmuCtx { 11 | /// Construct a brand new `AmxEmuCtx` initialized with a default state. 12 | pub fn new() -> Self { 13 | Self::default() 14 | } 15 | } 16 | 17 | #[derive(Debug, Copy, Clone)] 18 | struct AmxSt { 19 | /// "8 64-byte registers" 20 | x: [u8; 512], 21 | /// "8 64-byte registers" 22 | y: [u8; 512], 23 | /// "64 64-byte registers in an M-by-N matrix" 24 | z: [u8; 4096], 25 | } 26 | 27 | // FIXME: Large arrays do not implement `Default` yet (This is complicated 28 | // because the current `Default` impl for `[T; 0]` doesn't have a `T: 29 | // Default` bound, so simply replacing it with const generics would be a 30 | // breaking change: 31 | // ) 32 | impl Default for AmxSt { 33 | fn default() -> Self { 34 | Self { 35 | x: [0; 512], 36 | y: [0; 512], 37 | z: [0; 4096], 38 | } 39 | } 40 | } 41 | 42 | unsafe impl AmxOps for AmxEmuCtx { 43 | unsafe fn ldx(&mut self, x: u64, ptr: *mut ()) { 44 | todo!() 45 | } 46 | 47 | unsafe fn ldy(&mut self, x: u64, ptr: *mut ()) { 48 | todo!() 49 | } 50 | 51 | unsafe fn stx(&mut self, x: u64, ptr: *mut ()) { 52 | todo!() 53 | } 54 | 55 | unsafe fn sty(&mut self, x: u64, ptr: *mut ()) { 56 | todo!() 57 | } 58 | 59 | unsafe fn ldz(&mut self, x: u64, ptr: *mut ()) { 60 | todo!() 61 | } 62 | 63 | unsafe fn stz(&mut self, x: u64, ptr: *mut ()) { 64 | todo!() 65 | } 66 | 67 | unsafe fn ldzi(&mut self, x: u64, ptr: *mut ()) { 68 | todo!() 69 | } 70 | 71 | unsafe fn stzi(&mut self, x: u64, ptr: *mut ()) { 72 | todo!() 73 | } 74 | 75 | fn extrx(&mut self, x: u64) { 76 | todo!() 77 | } 78 | 79 | fn extry(&mut self, x: u64) { 80 | todo!() 81 | } 82 | 83 | fn fma64(&mut self, x: u64) { 84 | todo!() 85 | } 86 | 87 | fn fms64(&mut self, x: u64) { 88 | todo!() 89 | } 90 | 91 | fn fma32(&mut self, x: u64) { 92 | todo!() 93 | } 94 | 95 | fn fms32(&mut self, x: u64) { 96 | todo!() 97 | } 98 | 99 | fn mac16(&mut self, x: u64) { 100 | todo!() 101 | } 102 | 103 | fn fma16(&mut self, x: u64) { 104 | todo!() 105 | } 106 | 107 | fn fms16(&mut self, x: u64) { 108 | todo!() 109 | } 110 | 111 | fn vecint(&mut self, x: u64) { 112 | todo!() 113 | } 114 | 115 | fn vecfp(&mut self, x: u64) { 116 | todo!() 117 | } 118 | 119 | fn matint(&mut self, x: u64) { 120 | todo!() 121 | } 122 | 123 | fn matfp(&mut self, x: u64) { 124 | todo!() 125 | } 126 | 127 | fn genlut(&mut self, x: u64) { 128 | todo!() 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /src/genlut.rs: -------------------------------------------------------------------------------- 1 | //! Wrapper for the `genlut` instruction 2 | use crate::{ 3 | regs::{XBytes, XRow, YBytes, YRow, ZRow}, 4 | AmxOps, 5 | }; 6 | 7 | /// The trait for marker types specifying `genlut` instruction's LUT type. 8 | /// 9 | /// The types implementing this trait are in the following form: 10 | /// `(direction, index, value)`. 11 | /// 12 | /// - `direction` is either [`Normal`] or [`Reverse`]. 13 | /// - `index` is the data type for indices. 14 | /// - `value` is the data type for looked-up values. 15 | /// 16 | pub trait LutTy { 17 | /// The raw LUT mode number for `genlut` instruction. 18 | fn genlut_mode(&self) -> u64; 19 | } 20 | 21 | /// Specifies the normal application of a look-up table. 22 | pub struct Normal; 23 | /// Specifies the reversed application of a look-up table. 24 | pub struct Reverse; 25 | 26 | /// Two-bit LUT indices. 27 | pub struct Index2; 28 | 29 | /// Four-bit LUT indices. 30 | pub struct Index4; 31 | 32 | /// Five-bit (tightly-packed) LUT indices. 33 | pub struct Index5; 34 | 35 | /// A tag for the 16-bit floating-point number data type. 36 | pub struct F16; 37 | 38 | /// A tag for the 32-bit floating-point number data type. 39 | pub struct F32; 40 | 41 | /// A tag for the 64-bit floating-point number data type. 42 | pub struct F64; 43 | 44 | /// A tag for the 16-bit unsigned integer type. 45 | pub struct U16; 46 | 47 | /// A tag for the 32-bit unsigned integer type. 48 | pub struct U32; 49 | 50 | /// A tag for the 64-bit unsigned integer type. 51 | pub struct U64; 52 | 53 | /// A tag for the 16-bit signed integer type. 54 | pub struct I16; 55 | 56 | /// A tag for the 32-bit signed integer type. 57 | pub struct I32; 58 | 59 | /// A tag for the 64-bit signed integer type. 60 | pub struct I64; 61 | 62 | /// A tag for an 8-bit type. 63 | pub struct X8; 64 | 65 | /// A tag for a 16-bit type. 66 | pub struct X16; 67 | 68 | /// A tag for a 32-bit type. 69 | pub struct X32; 70 | 71 | /// A tag for a 64-bit type. 72 | pub struct X64; 73 | 74 | macro_rules! define_lut_ty { 75 | ($( 76 | $ty:ty => $val:expr 77 | ),*$(,)*) => {$( 78 | impl LutTy for $ty { 79 | #[inline(always)] 80 | fn genlut_mode(&self) -> u64 { 81 | $val 82 | } 83 | } 84 | )*}; 85 | } 86 | 87 | define_lut_ty! { 88 | (Reverse, Index4, F32) => 0, 89 | (Reverse, Index5, F16) => 1, 90 | (Reverse, Index4, F64) => 2, 91 | (Reverse, Index4, I32) => 3, 92 | (Reverse, Index5, I16) => 4, 93 | (Reverse, Index4, U32) => 5, 94 | (Reverse, Index5, U16) => 6, 95 | (Normal, Index2, X32) => 7, 96 | (Normal, Index2, X16) => 8, 97 | (Normal, Index2, X8) => 9, 98 | (Normal, Index4, X64) => 10, 99 | (Normal, Index4, X32) => 11, 100 | (Normal, Index4, X16) => 12, 101 | (Normal, Index4, X8) => 13, 102 | (Normal, Index5, X16) => 14, 103 | (Normal, Index5, X8) => 15, 104 | } 105 | 106 | #[cfg(feature = "either")] 107 | impl LutTy for either::Either { 108 | #[inline] 109 | fn genlut_mode(&self) -> u64 { 110 | match self { 111 | either::Left(x) => x.genlut_mode(), 112 | either::Right(x) => x.genlut_mode(), 113 | } 114 | } 115 | } 116 | 117 | /// The trait representing `genlut` instruction's input, which can be either 118 | /// [`XBytes`] or [`YBytes`]. 119 | pub trait LutIn { 120 | fn as_genlut_input_param(&self) -> u64; 121 | } 122 | 123 | impl LutIn for XBytes { 124 | #[inline(always)] 125 | fn as_genlut_input_param(&self) -> u64 { 126 | debug_assert!(self.0 < 512); 127 | self.0 as u64 128 | } 129 | } 130 | 131 | impl LutIn for YBytes { 132 | #[inline(always)] 133 | fn as_genlut_input_param(&self) -> u64 { 134 | debug_assert!(self.0 < 512); 135 | self.0 as u64 | (1u64 << 10) // "input is in Y" 136 | } 137 | } 138 | 139 | #[cfg(feature = "either")] 140 | impl LutIn for either::Either { 141 | #[inline] 142 | fn as_genlut_input_param(&self) -> u64 { 143 | match self { 144 | either::Left(x) => x.as_genlut_input_param(), 145 | either::Right(x) => x.as_genlut_input_param(), 146 | } 147 | } 148 | } 149 | 150 | /// The trait representing `genlut` instruction's output, which can be either 151 | /// [`XRow`], [`YRow`], or [`ZRow`]. 152 | pub trait LutOut { 153 | fn as_genlut_output_param(&self) -> u64; 154 | } 155 | 156 | impl LutOut for XRow { 157 | #[inline(always)] 158 | fn as_genlut_output_param(&self) -> u64 { 159 | debug_assert!(self.0 < 8); 160 | (self.0 as u64) << 20 161 | } 162 | } 163 | 164 | impl LutOut for YRow { 165 | #[inline(always)] 166 | fn as_genlut_output_param(&self) -> u64 { 167 | debug_assert!(self.0 < 8); 168 | ((self.0 as u64) << 20) | (1u64 << 25) // "input is in Y" 169 | } 170 | } 171 | 172 | impl LutOut for ZRow { 173 | #[inline(always)] 174 | fn as_genlut_output_param(&self) -> u64 { 175 | debug_assert!(self.0 < 64); 176 | ((self.0 as u64) << 20) | (1u64 << 26) 177 | } 178 | } 179 | 180 | #[cfg(feature = "either")] 181 | impl LutOut for either::Either { 182 | #[inline] 183 | fn as_genlut_output_param(&self) -> u64 { 184 | match self { 185 | either::Left(x) => x.as_genlut_output_param(), 186 | either::Right(x) => x.as_genlut_output_param(), 187 | } 188 | } 189 | } 190 | 191 | #[inline(always)] 192 | pub(crate) fn lut( 193 | ops: &mut (impl AmxOps + ?Sized), 194 | input: impl LutIn, 195 | XRow(table_row): XRow, 196 | output: impl LutOut, 197 | mode: impl LutTy, 198 | ) { 199 | ops.genlut( 200 | input.as_genlut_input_param() 201 | | output.as_genlut_output_param() 202 | | (mode.genlut_mode() << 53) 203 | | ((table_row as u64) << 60), 204 | ); 205 | } 206 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Rust wrapper for Apple Matrix Coprocessor (AMX) instructions 2 | //! 3 | //! This crate provides wrapper functions for the undocumented AMX instructions, 4 | //! which are found in Apple Silicon processors. 5 | //! 6 | //! # Resources 7 | //! 8 | //! - 9 | //! - 10 | //! 11 | //! # Example 12 | //! 13 | //! ```rust 14 | //! use amx::{Amx, XRow, YRow, XBytes, YBytes, ZRow}; 15 | //! let mut ctx = amx::AmxCtx::new().unwrap(); 16 | //! let x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 | //! 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32i16]; 18 | //! let y = [51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 19 | //! 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82i16]; 20 | //! unsafe { ctx.load512(x.as_ptr(), XRow(0)) }; 21 | //! unsafe { ctx.load512(y.as_ptr(), YRow(0)) }; 22 | //! ctx.outer_product_i16_xy_to_z( 23 | //! Some(XBytes(0)), // input from X starting from byte offset 0 24 | //! Some(YBytes(0)), // input from Y starting from byte offset 0 25 | //! ZRow(0), // output to Z starting from row offset 0 26 | //! false, // don't accumulate 27 | //! ); 28 | //! let z: [[i16; 32]; 64] = unsafe { std::mem::transmute(ctx.read_z()) }; 29 | //! for (x_i, &x) in x.iter().enumerate() { 30 | //! for (y_i, &y) in y.iter().enumerate() { 31 | //! assert_eq!(z[y_i * 2][x_i], x * y); 32 | //! } 33 | //! } 34 | //! ``` 35 | //! 36 | //! # Registers 37 | //! 38 | //! ```rust 39 | //! struct AmxState { 40 | //! /// "8 64-byte registers" 41 | //! x: [[u8; 64]; 8], 42 | //! /// "8 64-byte registers" 43 | //! y: [[u8; 64]; 8], 44 | //! /// "64 64-byte registers in an M-by-N matrix" 45 | //! z: [[u8; 64]; 64], 46 | //! } 47 | //! ``` 48 | #![feature(asm_const)] 49 | #![cfg_attr(feature = "doc_cfg", feature(doc_cfg))] 50 | 51 | mod emu; 52 | mod genlut; 53 | mod load_store; 54 | mod ops; 55 | mod regs; 56 | pub use crate::{emu::*, genlut::*, load_store::*, ops::AmxOps, regs::*}; 57 | 58 | cfg_if::cfg_if! { 59 | if #[cfg(any(doc, target_arch = "aarch64"))] { 60 | #[cfg_attr(feature = "doc_cfg", doc(cfg(target_arch = "aarch64")))] 61 | mod nativectx; 62 | #[cfg_attr(feature = "doc_cfg", doc(cfg(target_arch = "aarch64")))] 63 | pub mod nativeops; 64 | pub use crate::nativectx::{AmxCtx, NewAmxCtxError}; 65 | } 66 | } 67 | 68 | /// The prelude. 69 | pub mod prelude { 70 | #[doc(no_inline)] 71 | pub use crate::{ops::AmxOps as _, Amx as _}; 72 | } 73 | 74 | /// A high-level wrapper for AMX instructions. 75 | /// 76 | /// An application can use this wrapper in one of the following ways: 77 | /// 78 | /// - On a supported system, construct an [`AmxCtx`] by calling 79 | /// [`AmxCtx::new`]. `AmxCtx` derefs to [`amx::nativeops::AmxOps`], which 80 | /// implements [`AmxOps`]​ (the low-level wrapper), which has a 81 | /// blanket impl of `Amx`. 82 | /// 83 | /// - On a supported system, construct [`amx::nativeops::AmxOps`] directly by 84 | /// calling [`amx::nativeops::AmxOps::new`]. This is unsafe because it does 85 | /// not enable the current thread's AMX context automatically or check if AMX 86 | /// is really available. 87 | /// 88 | /// - On any system, construct [`AmxEmuCtx`] by calling [`AmxEmuCtx::default`]. 89 | /// It implements [`AmxOps`], which has a blanket impl of `Amx`. 90 | /// 91 | /// [`amx::nativeops::AmxOps`]: crate::nativeops::AmxOps 92 | /// [`amx::nativeops::AmxOps::new`]: crate::nativeops::AmxOps::new 93 | pub trait Amx: crate::ops::AmxOps { 94 | /// Load 512 bits (64 bytes) from memory to the specified register row. 95 | #[inline(always)] 96 | #[track_caller] 97 | unsafe fn load512(&mut self, ptr: *const T, row: impl LoadStore) { 98 | row.load512(self, ptr); 99 | } 100 | 101 | /// Load 1024 bits (128 bytes) from memory to the specified register row 102 | /// and the subsequent one. The specified pointer must be aligned to 103 | /// 128-byte boundaries. 104 | #[inline(always)] 105 | #[track_caller] 106 | unsafe fn load1024_aligned(&mut self, ptr: *const T, row: impl LoadStore) { 107 | row.load1024_aligned(self, ptr); 108 | } 109 | 110 | /// Store 512 bits (64 bytes) the specified register row's contents to 111 | /// memory. 112 | #[inline(always)] 113 | #[track_caller] 114 | unsafe fn store512(&mut self, ptr: *mut T, row: impl LoadStore) { 115 | row.store512(self, ptr); 116 | } 117 | 118 | /// Store 1024 bits (128 bytes) the specified register row and the 119 | /// subsequent one's contents to memory. The specified pointer must be 120 | /// aligned to 128-byte boundaries. 121 | #[inline(always)] 122 | #[track_caller] 123 | unsafe fn store1024_aligned(&mut self, ptr: *mut T, row: impl LoadStore) { 124 | row.store1024_aligned(self, ptr); 125 | } 126 | 127 | /// Load 512 bits (64 bytes) from memory to `z[index][0..64]` with interleaving. 128 | /// 129 | /// `index` must be in range `0..64`. 130 | #[inline(always)] 131 | #[track_caller] 132 | unsafe fn load512_interleaved(&mut self, ptr: *const T, row: ZRow) { 133 | load512_z_interleaved(self, ptr, row); 134 | } 135 | 136 | /// Store 512 bits (64 bytes) `z[index][0..64]` to memory with interleaving. 137 | /// 138 | /// `index` must be in range `0..64`. 139 | #[inline(always)] 140 | #[track_caller] 141 | unsafe fn store512_interleaved(&mut self, ptr: *mut T, row: ZRow) { 142 | store512_z_interleaved(self, ptr, row); 143 | } 144 | 145 | /// Read the whole contents of `x`. 146 | fn read_x(&mut self) -> [u8; 512] { 147 | let mut ret = std::mem::MaybeUninit::uninit(); 148 | for i in 0..8 { 149 | // Safety: Writing in a memory region within `ret` 150 | unsafe { 151 | self.store512( 152 | (ret.as_mut_ptr() as *mut u8).offset(i as isize * 64), 153 | XRow(i), 154 | ) 155 | }; 156 | } 157 | // Safety: All elements are initialized 158 | unsafe { ret.assume_init() } 159 | } 160 | 161 | /// Read the whole contents of `y`. 162 | fn read_y(&mut self) -> [u8; 512] { 163 | let mut ret = std::mem::MaybeUninit::uninit(); 164 | for i in 0..8 { 165 | // Safety: Writing in a memory region within `ret` 166 | unsafe { 167 | self.store512( 168 | (ret.as_mut_ptr() as *mut u8).offset(i as isize * 64), 169 | YRow(i), 170 | ) 171 | }; 172 | } 173 | // Safety: All elements are initialized 174 | unsafe { ret.assume_init() } 175 | } 176 | 177 | /// Read the whole contents of `z`. 178 | fn read_z(&mut self) -> [u8; 4096] { 179 | let mut ret = std::mem::MaybeUninit::uninit(); 180 | for i in 0..64 { 181 | // Safety: Writing in a memory region within `ret` 182 | unsafe { 183 | self.store512( 184 | (ret.as_mut_ptr() as *mut u8).offset(i as isize * 64), 185 | ZRow(i), 186 | ) 187 | }; 188 | } 189 | // Safety: All elements are initialized 190 | unsafe { ret.assume_init() } 191 | } 192 | 193 | /// Calculate the outer product of `x: [i16; 32]` and `y: [i16; 32]` and write 194 | /// the output to every second row of `z: [[i16; 32]; 64]`. 195 | /// 196 | /// If `x_offset_bytes` and/or `y_offset_bytes` are `None`, the respective 197 | /// registers will be excluded from the operation (not performing 198 | /// multiplication). 199 | /// 200 | /// `z_index` must be in range `0..64`. Only the least significant bit of 201 | /// `z_index` will be taken into consideration. 202 | #[inline(always)] 203 | fn outer_product_i16_xy_to_z( 204 | &mut self, 205 | x_offset_bytes: Option, 206 | y_offset_bytes: Option, 207 | z_index: ZRow, 208 | accumulate: bool, 209 | ) { 210 | // FIXME: rustfmt doesn't like patterns in provided trait methods 211 | let z_index = z_index.0; 212 | debug_assert!(x_offset_bytes.unwrap_or_default().0 < 0x200); 213 | debug_assert!(y_offset_bytes.unwrap_or_default().0 < 0x200); 214 | debug_assert!(z_index < 64); 215 | // TODO: widening (i32 output) 216 | // TODO: vector output (reducing) 217 | self.mac16( 218 | (y_offset_bytes.unwrap_or_default().0 219 | | (x_offset_bytes.unwrap_or_default().0 << 10) 220 | | (z_index << 20) 221 | | (((!accumulate) as usize) << 27) 222 | | ((x_offset_bytes.is_none() as usize) << 28) 223 | | ((y_offset_bytes.is_none() as usize) << 29)) as u64, 224 | ); 225 | } 226 | 227 | /// Perform (reverse) table lookup. 228 | #[inline(always)] 229 | fn lut(&mut self, input: impl LutIn, table: XRow, output: impl LutOut, ty: impl LutTy) { 230 | genlut::lut(self, input, table, output, ty); 231 | } 232 | } 233 | 234 | impl Amx for T {} 235 | -------------------------------------------------------------------------------- /src/load_store.rs: -------------------------------------------------------------------------------- 1 | use crate::{ 2 | regs::{XRow, YRow, ZRow}, 3 | AmxOps, 4 | }; 5 | 6 | /// The parameters of AMX's load and store instructions. 7 | #[derive(Copy, Clone)] 8 | struct MemArgs { 9 | /// 6-bit register offset (in units of `0x40`) in range `0..64` 10 | reg_offset: u64, 11 | size: MemSize, 12 | } 13 | 14 | impl MemArgs { 15 | #[inline] 16 | fn encode(self) -> u64 { 17 | debug_assert!(self.reg_offset < 64); 18 | 19 | // The pointer is passed by a separate parameter when using `AmxOps` 20 | (self.reg_offset << 56) 21 | // [61] - ? 22 | | ((self.size as u64) << 62) 23 | // [63] - ? 24 | } 25 | } 26 | 27 | #[derive(Copy, Clone)] 28 | #[repr(u8)] 29 | enum MemSize { 30 | /// 64 bytes 31 | _64 = 0, 32 | /// 128 bytes 33 | _128 = 1, 34 | } 35 | 36 | /// Register row types supporting 512-bit and 1024-bit operations. 37 | /// 38 | /// This trait is not meant to be used directly. Please use [`Amx`]'s methods 39 | /// instead. 40 | /// 41 | /// [`Amx`]: crate::Amx 42 | pub trait LoadStore { 43 | /// Load 512 bits (64 bytes) from memory to the register. 44 | unsafe fn load512(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *const T); 45 | /// Store 512 bits (64 bytes) to memory from the register. 46 | unsafe fn store512(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *mut T); 47 | 48 | /// Load 1024 bits (128 bytes) from memory to the register. 49 | /// 50 | /// `ptr` must be aligned to 128-byte boundaries. 51 | unsafe fn load1024_aligned(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *const T); 52 | /// Store 1024 bits (128 bytes) to memory from the register. 53 | /// 54 | /// `ptr` must be aligned to 128-byte boundaries. 55 | unsafe fn store1024_aligned(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *mut T); 56 | } 57 | 58 | #[cfg(feature = "either")] 59 | impl LoadStore for either::Either { 60 | #[inline] 61 | unsafe fn load512(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *const T) { 62 | match self { 63 | either::Left(x) => x.load512(ops, ptr), 64 | either::Right(x) => x.load512(ops, ptr), 65 | } 66 | } 67 | 68 | #[inline] 69 | unsafe fn store512(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *mut T) { 70 | match self { 71 | either::Left(x) => x.store512(ops, ptr), 72 | either::Right(x) => x.store512(ops, ptr), 73 | } 74 | } 75 | 76 | #[inline] 77 | unsafe fn load1024_aligned(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *const T) { 78 | match self { 79 | either::Left(x) => x.load1024_aligned(ops, ptr), 80 | either::Right(x) => x.load1024_aligned(ops, ptr), 81 | } 82 | } 83 | 84 | #[inline] 85 | unsafe fn store1024_aligned(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *mut T) { 86 | match self { 87 | either::Left(x) => x.store1024_aligned(ops, ptr), 88 | either::Right(x) => x.store1024_aligned(ops, ptr), 89 | } 90 | } 91 | } 92 | 93 | impl LoadStore for XRow { 94 | #[inline(always)] 95 | #[track_caller] 96 | unsafe fn load512(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *const T) { 97 | let index = self.0; 98 | assert!(index < 8); 99 | ops.ldx( 100 | MemArgs { 101 | reg_offset: index as u64, 102 | size: MemSize::_64, 103 | } 104 | .encode(), 105 | ptr as *mut (), 106 | ); 107 | } 108 | 109 | #[inline(always)] 110 | #[track_caller] 111 | unsafe fn store512(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *mut T) { 112 | let index = self.0; 113 | assert!(index < 8); 114 | ops.stx( 115 | MemArgs { 116 | reg_offset: index as u64, 117 | size: MemSize::_64, 118 | } 119 | .encode(), 120 | ptr as *mut (), 121 | ); 122 | } 123 | 124 | #[inline(always)] 125 | #[track_caller] 126 | unsafe fn load1024_aligned(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *const T) { 127 | let index = self.0; 128 | assert!(index < 8); 129 | ops.ldx( 130 | MemArgs { 131 | reg_offset: index as u64, 132 | size: MemSize::_128, 133 | } 134 | .encode(), 135 | ptr as *mut (), 136 | ); 137 | } 138 | 139 | #[inline(always)] 140 | #[track_caller] 141 | unsafe fn store1024_aligned(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *mut T) { 142 | let index = self.0; 143 | assert!(index < 8); 144 | ops.stx( 145 | MemArgs { 146 | reg_offset: index as u64, 147 | size: MemSize::_128, 148 | } 149 | .encode(), 150 | ptr as *mut (), 151 | ); 152 | } 153 | } 154 | 155 | impl LoadStore for YRow { 156 | #[inline(always)] 157 | #[track_caller] 158 | unsafe fn load512(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *const T) { 159 | let index = self.0; 160 | assert!(index < 8); 161 | ops.ldy( 162 | MemArgs { 163 | reg_offset: index as u64, 164 | size: MemSize::_64, 165 | } 166 | .encode(), 167 | ptr as *mut (), 168 | ); 169 | } 170 | 171 | #[inline(always)] 172 | #[track_caller] 173 | unsafe fn store512(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *mut T) { 174 | let index = self.0; 175 | assert!(index < 8); 176 | ops.sty( 177 | MemArgs { 178 | reg_offset: index as u64, 179 | size: MemSize::_64, 180 | } 181 | .encode(), 182 | ptr as *mut (), 183 | ); 184 | } 185 | 186 | #[inline(always)] 187 | #[track_caller] 188 | unsafe fn load1024_aligned(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *const T) { 189 | let index = self.0; 190 | assert!(index < 8); 191 | ops.ldy( 192 | MemArgs { 193 | reg_offset: index as u64, 194 | size: MemSize::_128, 195 | } 196 | .encode(), 197 | ptr as *mut (), 198 | ); 199 | } 200 | 201 | #[inline(always)] 202 | #[track_caller] 203 | unsafe fn store1024_aligned(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *mut T) { 204 | let index = self.0; 205 | assert!(index < 8); 206 | ops.sty( 207 | MemArgs { 208 | reg_offset: index as u64, 209 | size: MemSize::_128, 210 | } 211 | .encode(), 212 | ptr as *mut (), 213 | ); 214 | } 215 | } 216 | 217 | impl LoadStore for ZRow { 218 | #[inline(always)] 219 | #[track_caller] 220 | unsafe fn load512(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *const T) { 221 | let index = self.0; 222 | assert!(index < 64); 223 | ops.ldz( 224 | MemArgs { 225 | reg_offset: index as u64, 226 | size: MemSize::_64, 227 | } 228 | .encode(), 229 | ptr as *mut (), 230 | ); 231 | } 232 | 233 | #[inline(always)] 234 | #[track_caller] 235 | unsafe fn store512(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *mut T) { 236 | let index = self.0; 237 | assert!(index < 64); 238 | ops.stz( 239 | MemArgs { 240 | reg_offset: index as u64, 241 | size: MemSize::_64, 242 | } 243 | .encode(), 244 | ptr as *mut (), 245 | ); 246 | } 247 | 248 | #[inline(always)] 249 | #[track_caller] 250 | unsafe fn load1024_aligned(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *const T) { 251 | let index = self.0; 252 | assert!(index < 64); 253 | ops.ldz( 254 | MemArgs { 255 | reg_offset: index as u64, 256 | size: MemSize::_128, 257 | } 258 | .encode(), 259 | ptr as *mut (), 260 | ); 261 | } 262 | 263 | #[inline(always)] 264 | #[track_caller] 265 | unsafe fn store1024_aligned(&self, ops: &mut (impl AmxOps + ?Sized), ptr: *mut T) { 266 | let index = self.0; 267 | assert!(index < 64); 268 | ops.stz( 269 | MemArgs { 270 | reg_offset: index as u64, 271 | size: MemSize::_128, 272 | } 273 | .encode(), 274 | ptr as *mut (), 275 | ); 276 | } 277 | } 278 | 279 | /// Load 512 bits (64 bytes) from memory to `z[index][0..64]` with interleaving. 280 | /// 281 | /// `index` must be in range `0..64`. 282 | #[inline(always)] 283 | #[track_caller] 284 | pub(crate) unsafe fn load512_z_interleaved( 285 | ops: &mut (impl AmxOps + ?Sized), 286 | ptr: *const T, 287 | ZRow(index): ZRow, 288 | ) { 289 | assert!(index < 64); 290 | ops.ldzi( 291 | MemArgs { 292 | reg_offset: index as u64, 293 | size: MemSize::_64, 294 | } 295 | .encode(), 296 | ptr as *mut (), 297 | ); 298 | } 299 | 300 | /// Store 512 bits (64 bytes) `z[index][0..64]` to memory with interleaving. 301 | /// 302 | /// `index` must be in range `0..64`. 303 | #[inline(always)] 304 | #[track_caller] 305 | pub(crate) unsafe fn store512_z_interleaved( 306 | ops: &mut (impl AmxOps + ?Sized), 307 | ptr: *mut T, 308 | ZRow(index): ZRow, 309 | ) { 310 | assert!(index < 64); 311 | ops.stzi( 312 | MemArgs { 313 | reg_offset: index as u64, 314 | size: MemSize::_64, 315 | } 316 | .encode(), 317 | ptr as *mut (), 318 | ); 319 | } 320 | -------------------------------------------------------------------------------- /src/nativectx.rs: -------------------------------------------------------------------------------- 1 | //! Safely manages AMX's actiavtion state. 2 | use std::{ 3 | cell::Cell, 4 | ops::{Deref, DerefMut}, 5 | }; 6 | 7 | use crate::nativeops::AmxOps; 8 | 9 | /// Represents the current thread's AMX context. 10 | pub struct AmxCtx { 11 | ops: AmxOps<'static>, 12 | } 13 | 14 | /// The error type for [`AmxCtx::new`] 15 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] 16 | pub enum NewAmxCtxError { 17 | /// The current thread already has an active `AmxCtx`. 18 | AlreadyActive, 19 | /// AMX is not supported by the target system. 20 | /// 21 | /// TODO: Report this error if it's unsupported 22 | Unsupported, 23 | } 24 | 25 | thread_local! { 26 | static CTX_ACTIVE: Cell = Cell::new(false); 27 | } 28 | 29 | impl AmxCtx { 30 | /// Construct a brand new instance of `AmxCtx` by enabling AMX for the 31 | /// current thread. 32 | pub fn new() -> Result { 33 | if CTX_ACTIVE.with(|x| x.get()) { 34 | Err(NewAmxCtxError::AlreadyActive) 35 | } else { 36 | // TODO: Don't assume AMX is always supported 37 | 38 | // Enable AMX for the current thread 39 | // Safety: AMX is supported 40 | unsafe { crate::nativeops::set() }; 41 | 42 | Ok(Self { 43 | // Safety: AMX is supported 44 | ops: unsafe { AmxOps::new() }, 45 | }) 46 | } 47 | } 48 | } 49 | 50 | impl Drop for AmxCtx { 51 | fn drop(&mut self) { 52 | // Disable AMX for the current thread 53 | // Safety: AMX is supported 54 | unsafe { crate::nativeops::clr() }; 55 | 56 | CTX_ACTIVE.with(|x| x.set(false)); 57 | } 58 | } 59 | 60 | impl Deref for AmxCtx { 61 | type Target = AmxOps<'static>; 62 | 63 | fn deref(&self) -> &Self::Target { 64 | &self.ops 65 | } 66 | } 67 | 68 | impl DerefMut for AmxCtx { 69 | fn deref_mut(&mut self) -> &mut Self::Target { 70 | &mut self.ops 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/nativeops.rs: -------------------------------------------------------------------------------- 1 | //! Low-level operations (modeled after [Apple compiler intrinsics]) 2 | //! 3 | //! [Apple compiler intrinsics]: https://www.realworldtech.com/forum/?threadid=187087&curpostid=187120 4 | use std::{arch::asm, marker::PhantomData}; 5 | 6 | /// Emit an AMX instruction with an input register. 7 | #[inline(always)] 8 | pub unsafe fn op_in(operand: u64) { 9 | asm!( 10 | // Most AMX instructions take a 64-bit register number (e.g., `x25`) as 11 | // the operand. The problem is how to encode the register number in the 12 | // `.word` directive. We could use a fixed register number, but then we 13 | // would have to move a value into that register, which is utterly 14 | // inefficient. 15 | // 16 | // The trick used here is to prepend `0` to the register name to make it 17 | // look like a hexadecimal number (e.g., `0x25`). The encoding is still 18 | // wrong at this point because the register number is a decimal number, 19 | // but it's being interpreted as a hexadecimal number (`0x25 = 37`). So 20 | // we have to split it into digits (`2` and `5`) and reconstitute the 21 | // intended register number (`2 * 10 + 5`). 22 | ".word 0x00201000 + ({op} << 5) + (0{operand} & 0xf) + (0{operand} >> 4) * 10", 23 | op = const OP, 24 | operand = in(reg) operand, 25 | options(nostack, preserves_flags), 26 | ); 27 | } 28 | 29 | /// Emit an AMX instruction with a 5-bit immediate. 30 | #[inline(always)] 31 | pub unsafe fn op_imm() { 32 | asm!( 33 | ".word 0x00201000 + ({op} << 5) + {operand}", 34 | op = const OP, 35 | operand = const OPERAND, 36 | options(nostack, preserves_flags), 37 | ); 38 | } 39 | 40 | #[inline(always)] 41 | pub unsafe fn ldx(x: u64) { 42 | op_in::<0>(x); 43 | } 44 | 45 | #[inline(always)] 46 | pub unsafe fn ldy(x: u64) { 47 | op_in::<1>(x); 48 | } 49 | 50 | #[inline(always)] 51 | pub unsafe fn stx(x: u64) { 52 | op_in::<2>(x); 53 | } 54 | 55 | #[inline(always)] 56 | pub unsafe fn sty(x: u64) { 57 | op_in::<3>(x); 58 | } 59 | 60 | #[inline(always)] 61 | pub unsafe fn ldz(x: u64) { 62 | op_in::<4>(x); 63 | } 64 | 65 | #[inline(always)] 66 | pub unsafe fn stz(x: u64) { 67 | op_in::<5>(x); 68 | } 69 | 70 | #[inline(always)] 71 | pub unsafe fn ldzi(x: u64) { 72 | op_in::<6>(x); 73 | } 74 | 75 | #[inline(always)] 76 | pub unsafe fn stzi(x: u64) { 77 | op_in::<7>(x); 78 | } 79 | 80 | #[inline(always)] 81 | pub unsafe fn extrx(x: u64) { 82 | op_in::<8>(x); 83 | } 84 | 85 | #[inline(always)] 86 | pub unsafe fn extry(x: u64) { 87 | op_in::<9>(x); 88 | } 89 | 90 | #[inline(always)] 91 | pub unsafe fn fma64(x: u64) { 92 | op_in::<10>(x); 93 | } 94 | 95 | #[inline(always)] 96 | pub unsafe fn fms64(x: u64) { 97 | op_in::<11>(x); 98 | } 99 | 100 | #[inline(always)] 101 | pub unsafe fn fma32(x: u64) { 102 | op_in::<12>(x); 103 | } 104 | 105 | #[inline(always)] 106 | pub unsafe fn fms32(x: u64) { 107 | op_in::<13>(x); 108 | } 109 | 110 | #[inline(always)] 111 | pub unsafe fn mac16(x: u64) { 112 | op_in::<14>(x); 113 | } 114 | 115 | #[inline(always)] 116 | pub unsafe fn fma16(x: u64) { 117 | op_in::<15>(x); 118 | } 119 | 120 | #[inline(always)] 121 | pub unsafe fn fms16(x: u64) { 122 | op_in::<16>(x); 123 | } 124 | 125 | #[inline(always)] 126 | pub unsafe fn set() { 127 | op_imm::<17, 0>(); 128 | } 129 | 130 | #[inline(always)] 131 | pub unsafe fn clr() { 132 | op_imm::<17, 1>(); 133 | } 134 | 135 | #[inline(always)] 136 | pub unsafe fn vecint(x: u64) { 137 | op_in::<18>(x); 138 | } 139 | 140 | #[inline(always)] 141 | pub unsafe fn vecfp(x: u64) { 142 | op_in::<19>(x); 143 | } 144 | 145 | #[inline(always)] 146 | pub unsafe fn matint(x: u64) { 147 | op_in::<20>(x); 148 | } 149 | 150 | #[inline(always)] 151 | pub unsafe fn matfp(x: u64) { 152 | op_in::<21>(x); 153 | } 154 | 155 | #[inline(always)] 156 | pub unsafe fn genlut(x: u64) { 157 | op_in::<22>(x); 158 | } 159 | 160 | /// Exposes the target processor's AMX support by implementing [`AmxOps`] trait. 161 | /// 162 | /// [`AmxOps`]: crate::ops::AmxOps 163 | /// 164 | /// The lifetime parameter can be used to restrict the use of `&AmxOps` to 165 | /// a scope where AMX is enabled. To make this technique effective, this type 166 | /// does not implement `Clone`. (GAT would make this more effective.) 167 | /// 168 | /// This type is not `Send`-able because AMX states, including whether it's 169 | /// enabled, are thread-local. 170 | pub struct AmxOps<'a>(PhantomData<(&'a mut (), *mut ())>); 171 | 172 | impl<'a> AmxOps<'a> { 173 | /// Construct `Self`. 174 | /// 175 | /// # Safety 176 | /// 177 | /// The following conditions must be satisfied on any use of this type's 178 | /// `AmxOps` methods: 179 | /// 180 | /// - The target processor must actually support AMX. 181 | /// 182 | /// (Calling the methods while AMX being disabled is no unsafer than calling 183 | /// `abort`, I think.) 184 | #[inline] 185 | pub unsafe fn new() -> Self { 186 | Self(PhantomData) 187 | } 188 | 189 | /// Reborrow `self`, constructing a new `AmxOps` with a narrower lifetime. 190 | #[inline] 191 | pub fn borrow_mut(&mut self) -> AmxOps<'_> { 192 | Self(PhantomData) 193 | } 194 | } 195 | 196 | unsafe impl crate::ops::AmxOps for AmxOps<'_> { 197 | #[inline(always)] 198 | unsafe fn ldx(&mut self, x: u64, ptr: *mut ()) { 199 | ldx(x | (ptr as u64 & 0x00ff_ffff_ffff_ffff)); 200 | } 201 | #[inline(always)] 202 | unsafe fn ldy(&mut self, x: u64, ptr: *mut ()) { 203 | ldy(x | (ptr as u64 & 0x00ff_ffff_ffff_ffff)); 204 | } 205 | #[inline(always)] 206 | unsafe fn stx(&mut self, x: u64, ptr: *mut ()) { 207 | stx(x | (ptr as u64 & 0x00ff_ffff_ffff_ffff)); 208 | } 209 | #[inline(always)] 210 | unsafe fn sty(&mut self, x: u64, ptr: *mut ()) { 211 | sty(x | (ptr as u64 & 0x00ff_ffff_ffff_ffff)); 212 | } 213 | #[inline(always)] 214 | unsafe fn ldz(&mut self, x: u64, ptr: *mut ()) { 215 | ldz(x | (ptr as u64 & 0x00ff_ffff_ffff_ffff)); 216 | } 217 | #[inline(always)] 218 | unsafe fn stz(&mut self, x: u64, ptr: *mut ()) { 219 | stz(x | (ptr as u64 & 0x00ff_ffff_ffff_ffff)); 220 | } 221 | #[inline(always)] 222 | unsafe fn ldzi(&mut self, x: u64, ptr: *mut ()) { 223 | ldzi(x | (ptr as u64 & 0x00ff_ffff_ffff_ffff)); 224 | } 225 | #[inline(always)] 226 | unsafe fn stzi(&mut self, x: u64, ptr: *mut ()) { 227 | stzi(x | (ptr as u64 & 0x00ff_ffff_ffff_ffff)); 228 | } 229 | #[inline(always)] 230 | fn extrx(&mut self, x: u64) { 231 | unsafe { extrx(x) }; 232 | } 233 | #[inline(always)] 234 | fn extry(&mut self, x: u64) { 235 | unsafe { extry(x) }; 236 | } 237 | #[inline(always)] 238 | fn fma64(&mut self, x: u64) { 239 | unsafe { fma64(x) }; 240 | } 241 | #[inline(always)] 242 | fn fms64(&mut self, x: u64) { 243 | unsafe { fms64(x) }; 244 | } 245 | #[inline(always)] 246 | fn fma32(&mut self, x: u64) { 247 | unsafe { fma32(x) }; 248 | } 249 | #[inline(always)] 250 | fn fms32(&mut self, x: u64) { 251 | unsafe { fms32(x) }; 252 | } 253 | #[inline(always)] 254 | fn mac16(&mut self, x: u64) { 255 | unsafe { mac16(x) }; 256 | } 257 | #[inline(always)] 258 | fn fma16(&mut self, x: u64) { 259 | unsafe { fma16(x) }; 260 | } 261 | #[inline(always)] 262 | fn fms16(&mut self, x: u64) { 263 | unsafe { fms16(x) }; 264 | } 265 | #[inline(always)] 266 | fn vecint(&mut self, x: u64) { 267 | unsafe { vecint(x) }; 268 | } 269 | #[inline(always)] 270 | fn vecfp(&mut self, x: u64) { 271 | unsafe { vecfp(x) }; 272 | } 273 | #[inline(always)] 274 | fn matint(&mut self, x: u64) { 275 | unsafe { matint(x) }; 276 | } 277 | #[inline(always)] 278 | fn matfp(&mut self, x: u64) { 279 | unsafe { matfp(x) }; 280 | } 281 | #[inline(always)] 282 | fn genlut(&mut self, x: u64) { 283 | unsafe { genlut(x) }; 284 | } 285 | } 286 | -------------------------------------------------------------------------------- /src/ops.rs: -------------------------------------------------------------------------------- 1 | /// Exposes all AMX instructions except `set` and `clr` as trait methods. 2 | /// 3 | /// Load and store operations receive a pointer by the additional parameter to 4 | /// allow emulation on a system with a different pointer size. 5 | pub unsafe trait AmxOps { 6 | unsafe fn ldx(&mut self, x: u64, ptr: *mut ()); 7 | unsafe fn ldy(&mut self, x: u64, ptr: *mut ()); 8 | unsafe fn stx(&mut self, x: u64, ptr: *mut ()); 9 | unsafe fn sty(&mut self, x: u64, ptr: *mut ()); 10 | unsafe fn ldz(&mut self, x: u64, ptr: *mut ()); 11 | unsafe fn stz(&mut self, x: u64, ptr: *mut ()); 12 | unsafe fn ldzi(&mut self, x: u64, ptr: *mut ()); 13 | unsafe fn stzi(&mut self, x: u64, ptr: *mut ()); 14 | fn extrx(&mut self, x: u64); 15 | fn extry(&mut self, x: u64); 16 | fn fma64(&mut self, x: u64); 17 | fn fms64(&mut self, x: u64); 18 | fn fma32(&mut self, x: u64); 19 | fn fms32(&mut self, x: u64); 20 | fn mac16(&mut self, x: u64); 21 | fn fma16(&mut self, x: u64); 22 | fn fms16(&mut self, x: u64); 23 | fn vecint(&mut self, x: u64); 24 | fn vecfp(&mut self, x: u64); 25 | fn matint(&mut self, x: u64); 26 | fn matfp(&mut self, x: u64); 27 | fn genlut(&mut self, x: u64); 28 | } 29 | 30 | // Safety: Just forwarding the calls 31 | unsafe impl AmxOps for &'_ mut T { 32 | unsafe fn ldx(&mut self, x: u64, ptr: *mut ()) { 33 | (**self).ldx(x, ptr) 34 | } 35 | unsafe fn ldy(&mut self, x: u64, ptr: *mut ()) { 36 | (**self).ldy(x, ptr) 37 | } 38 | unsafe fn stx(&mut self, x: u64, ptr: *mut ()) { 39 | (**self).stx(x, ptr) 40 | } 41 | unsafe fn sty(&mut self, x: u64, ptr: *mut ()) { 42 | (**self).sty(x, ptr) 43 | } 44 | unsafe fn ldz(&mut self, x: u64, ptr: *mut ()) { 45 | (**self).ldz(x, ptr) 46 | } 47 | unsafe fn stz(&mut self, x: u64, ptr: *mut ()) { 48 | (**self).stz(x, ptr) 49 | } 50 | unsafe fn ldzi(&mut self, x: u64, ptr: *mut ()) { 51 | (**self).ldzi(x, ptr) 52 | } 53 | unsafe fn stzi(&mut self, x: u64, ptr: *mut ()) { 54 | (**self).stzi(x, ptr) 55 | } 56 | fn extrx(&mut self, x: u64) { 57 | (**self).extrx(x) 58 | } 59 | fn extry(&mut self, x: u64) { 60 | (**self).extry(x) 61 | } 62 | fn fma64(&mut self, x: u64) { 63 | (**self).fma64(x) 64 | } 65 | fn fms64(&mut self, x: u64) { 66 | (**self).fms64(x) 67 | } 68 | fn fma32(&mut self, x: u64) { 69 | (**self).fma32(x) 70 | } 71 | fn fms32(&mut self, x: u64) { 72 | (**self).fms32(x) 73 | } 74 | fn mac16(&mut self, x: u64) { 75 | (**self).mac16(x) 76 | } 77 | fn fma16(&mut self, x: u64) { 78 | (**self).fma16(x) 79 | } 80 | fn fms16(&mut self, x: u64) { 81 | (**self).fms16(x) 82 | } 83 | fn vecint(&mut self, x: u64) { 84 | (**self).vecint(x) 85 | } 86 | fn vecfp(&mut self, x: u64) { 87 | (**self).vecfp(x) 88 | } 89 | fn matint(&mut self, x: u64) { 90 | (**self).matint(x) 91 | } 92 | fn matfp(&mut self, x: u64) { 93 | (**self).matfp(x) 94 | } 95 | fn genlut(&mut self, x: u64) { 96 | (**self).genlut(x) 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/regs.rs: -------------------------------------------------------------------------------- 1 | //! AMX registers 2 | 3 | /// Refers to a row (register) in the `x` register set. 4 | /// 5 | /// The row index must be in range `0..8`. 6 | #[derive(Default, Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] 7 | pub struct XRow(pub usize); 8 | 9 | /// Refers to a row (register) in the `y` register set. 10 | /// 11 | /// The row index must be in range `0..8`. 12 | #[derive(Default, Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] 13 | pub struct YRow(pub usize); 14 | 15 | /// Refers to a row (register) in the `z` register set. 16 | /// 17 | /// The row index must be in range `0..64`. 18 | #[derive(Default, Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] 19 | pub struct ZRow(pub usize); 20 | 21 | /// A byte offset in `x` register set. 22 | /// 23 | /// The byte offset must be in range `0..512`. 24 | #[derive(Default, Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] 25 | pub struct XBytes(pub usize); 26 | 27 | /// A byte offset in `y` register set. 28 | /// 29 | /// The byte offset must be in range `0..512`. 30 | #[derive(Default, Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] 31 | pub struct YBytes(pub usize); 32 | -------------------------------------------------------------------------------- /tests/genlut.rs: -------------------------------------------------------------------------------- 1 | use amx::{prelude::*, Index4, Normal, XBytes, XRow, YBytes, YRow, X8}; 2 | use either::{Left, Right}; 3 | use quickcheck::TestResult; 4 | 5 | fn init() { 6 | let _ = env_logger::builder().is_test(true).try_init(); 7 | } 8 | 9 | fn overlaps(x: std::ops::Range, y: std::ops::Range) -> bool { 10 | x.start < y.end && x.end > y.start 11 | } 12 | 13 | #[quickcheck_macros::quickcheck] 14 | fn qc_genlut_lut8x16( 15 | table_row: usize, 16 | index_offset: usize, 17 | indices_in_y: bool, 18 | out_row: usize, 19 | mut indices: Vec, 20 | mut values: Vec, 21 | ) -> TestResult { 22 | values.resize_with(64, u8::default); 23 | indices.resize_with(32, u8::default); 24 | let out_row = out_row % 8; 25 | let table_row = table_row % 8; 26 | let index_offset = index_offset % 512; 27 | if overlaps( 28 | index_offset..index_offset + 64, 29 | table_row * 64..table_row * 64 + 64, 30 | ) || overlaps( 31 | index_offset..index_offset + 64, 32 | table_row * 64 + 512..table_row * 64 + 64 + 512, 33 | ) { 34 | return TestResult::discard(); 35 | } 36 | 37 | log::debug!("values = {:x?}", values); 38 | log::debug!("indices = {:x?}", indices); 39 | log::debug!("table_row = {:x?}", table_row); 40 | log::debug!("index_offset = {:x?}", index_offset); 41 | log::debug!("out_row = {:x?}", out_row); 42 | log::debug!("indices_in_y = {:x?}", indices_in_y); 43 | 44 | let mut got = [0u8; 64]; 45 | let mut ctx = amx::AmxCtx::new().unwrap(); 46 | unsafe { 47 | indices.resize_with(64, u8::default); 48 | 49 | // Load `indices` at byte offset `index_offset` 50 | let mut index_row_1 = [0u8; 64]; 51 | let mut index_row_2 = [0u8; 64]; 52 | let sub = index_offset % 64; 53 | index_row_1[sub..].copy_from_slice(&indices[..64 - sub]); 54 | index_row_2[..sub].copy_from_slice(&indices[64 - sub..]); 55 | if indices_in_y { 56 | ctx.load512(index_row_1.as_ptr(), YRow(index_offset / 64)); 57 | ctx.load512(index_row_2.as_ptr(), YRow(index_offset / 64 + 1)); 58 | } else { 59 | ctx.load512(index_row_1.as_ptr(), XRow(index_offset / 64)); 60 | ctx.load512(index_row_2.as_ptr(), XRow(index_offset / 64 + 1)); 61 | } 62 | 63 | // Load `values` at the row `table_row` 64 | ctx.load512(values.as_ptr(), XRow(table_row)); 65 | } 66 | 67 | // Perform table lookup 68 | ctx.lut( 69 | if indices_in_y { 70 | Left(YBytes(index_offset)) 71 | } else { 72 | Right(XBytes(index_offset)) 73 | }, 74 | XRow(table_row), 75 | XRow(out_row), 76 | (Normal, Index4, X8), 77 | ); 78 | 79 | // Read the result 80 | unsafe { ctx.store512(got.as_mut_ptr(), XRow(out_row)) }; 81 | let all_x = unsafe { std::mem::transmute::<_, [[u64; 8]; 8]>(ctx.read_x()) }; 82 | 83 | let expected: Vec = (0..64) 84 | .map(|i| { 85 | let idx = (indices[i / 2] >> (i % 2 * 4)) as usize & 0xf; 86 | values[idx] 87 | }) 88 | .collect(); 89 | 90 | log::debug!("got = {:x?}", got); 91 | log::debug!("expected = {:x?}", expected); 92 | log::debug!("all_x = {:x?}", all_x); 93 | 94 | assert_eq!( 95 | got[..], 96 | expected[..], 97 | "got = {:?}, expected = {:?}", 98 | got, 99 | expected, 100 | ); 101 | 102 | TestResult::passed() 103 | } 104 | -------------------------------------------------------------------------------- /tests/load_store.rs: -------------------------------------------------------------------------------- 1 | #![feature(array_chunks)] 2 | use aligned_box::AlignedBox; 3 | use amx::{prelude::*, AmxOps, XRow, YRow, ZRow}; 4 | use itertools::iproduct; 5 | 6 | fn init() { 7 | let _ = env_logger::builder().is_test(true).try_init(); 8 | } 9 | 10 | #[derive(Debug, Copy, Clone, PartialEq, Eq)] 11 | #[repr(u8)] 12 | pub enum MemSize { 13 | /// 64 bytes 14 | _64 = 0, 15 | /// 128 bytes 16 | _128 = 1, 17 | } 18 | 19 | impl MemSize { 20 | pub fn num_bytes(self) -> usize { 21 | match self { 22 | Self::_64 => 64, 23 | Self::_128 => 128, 24 | } 25 | } 26 | } 27 | 28 | unsafe fn load_generic( 29 | ctx: &mut impl AmxOps, 30 | ptr: *const T, 31 | index: usize, 32 | size: MemSize, 33 | reg: u8, 34 | interleaved: bool, 35 | ) { 36 | match (size, reg, interleaved) { 37 | (MemSize::_64, 0, false) => { 38 | ctx.load512(ptr, XRow(index)); 39 | } 40 | (MemSize::_64, 1, false) => { 41 | ctx.load512(ptr, YRow(index)); 42 | } 43 | (MemSize::_64, 2, false) => { 44 | ctx.load512(ptr, ZRow(index)); 45 | } 46 | (MemSize::_64, 2, true) => { 47 | ctx.load512_interleaved(ptr, ZRow(index)); 48 | } 49 | (MemSize::_128, 0, false) => { 50 | ctx.load1024_aligned(ptr, XRow(index)); 51 | } 52 | (MemSize::_128, 1, false) => { 53 | ctx.load1024_aligned(ptr, YRow(index)); 54 | } 55 | (MemSize::_128, 2, false) => { 56 | ctx.load1024_aligned(ptr, ZRow(index)); 57 | } 58 | _ => unreachable!(), 59 | } 60 | } 61 | 62 | unsafe fn store_generic( 63 | ctx: &mut impl AmxOps, 64 | ptr: *mut T, 65 | index: usize, 66 | size: MemSize, 67 | reg: u8, 68 | interleaved: bool, 69 | ) { 70 | match (size, reg, interleaved) { 71 | (MemSize::_64, 0, false) => { 72 | ctx.store512(ptr, XRow(index)); 73 | } 74 | (MemSize::_64, 1, false) => { 75 | ctx.store512(ptr, YRow(index)); 76 | } 77 | (MemSize::_64, 2, false) => { 78 | ctx.store512(ptr, ZRow(index)); 79 | } 80 | (MemSize::_64, 2, true) => { 81 | ctx.store512_interleaved(ptr, ZRow(index)); 82 | } 83 | (MemSize::_128, 0, false) => { 84 | ctx.store1024_aligned(ptr, XRow(index)); 85 | } 86 | (MemSize::_128, 1, false) => { 87 | ctx.store1024_aligned(ptr, YRow(index)); 88 | } 89 | (MemSize::_128, 2, false) => { 90 | ctx.store1024_aligned(ptr, ZRow(index)); 91 | } 92 | _ => unreachable!(), 93 | } 94 | } 95 | 96 | #[test] 97 | fn copy_and_check_memory() { 98 | init(); 99 | let mut ctx = amx::AmxCtx::new().unwrap(); 100 | 101 | let mut src: AlignedBox<[u16]> = AlignedBox::slice_from_default(0x80, 4096).unwrap(); 102 | for (i, src) in src.iter_mut().enumerate() { 103 | *src = i as _; 104 | } 105 | 106 | for (&size, ®, reg_offset, &interleaved) in iproduct!( 107 | &[MemSize::_64, MemSize::_128], 108 | &[0, 1, 2], 109 | 0..64, 110 | &[false, true] 111 | ) { 112 | if interleaved && (reg != 2 || size != MemSize::_64) { 113 | continue; 114 | } 115 | if reg != 2 && reg_offset >= 8 { 116 | continue; 117 | } 118 | 119 | log::debug!( 120 | "size = {:?}, reg = amx{}, reg_offset = {}, interleaved = {}", 121 | size, 122 | reg, 123 | reg_offset, 124 | interleaved 125 | ); 126 | 127 | let mut got: AlignedBox<[u16]> = AlignedBox::slice_from_value(0x80, 4096, 0xbeef).unwrap(); 128 | let expected: Vec = (0..4096) 129 | .map(|i| { 130 | if i as usize * 2 < size.num_bytes() { 131 | i 132 | } else { 133 | 0xbeef 134 | } 135 | }) 136 | .collect(); 137 | 138 | unsafe { 139 | load_generic(&mut *ctx, src.as_ptr(), reg_offset, size, reg, interleaved); 140 | store_generic( 141 | &mut *ctx, 142 | got.as_mut_ptr(), 143 | reg_offset, 144 | size, 145 | reg, 146 | interleaved, 147 | ); 148 | } 149 | 150 | assert_eq!(*got, *expected); 151 | } 152 | } 153 | 154 | #[test] 155 | fn load_and_check_register() { 156 | init(); 157 | let mut ctx = amx::AmxCtx::new().unwrap(); 158 | 159 | let mut pat1: AlignedBox<[u64]> = AlignedBox::slice_from_default(0x80, 16).unwrap(); 160 | for (i, pat1) in pat1.iter_mut().enumerate() { 161 | *pat1 = i as u64 + (75 - i as u64) * 0x100000000; 162 | } 163 | 164 | let pat2: Vec = vec![0x2222_2222_2222_2222; 512]; 165 | 166 | for (&size, ®, reg_offset, &interleaved) in iproduct!( 167 | &[MemSize::_64, MemSize::_128], 168 | &[0, 1, 2], 169 | 0..64, 170 | &[false, true] 171 | ) { 172 | if interleaved && (reg != 2 || size != MemSize::_64) { 173 | continue; 174 | } 175 | if reg != 2 && reg_offset >= 8 { 176 | continue; 177 | } 178 | 179 | log::debug!( 180 | "size = {:?}, reg = amx{}, reg_offset = {}, interleaved = {}", 181 | size, 182 | reg, 183 | reg_offset, 184 | interleaved 185 | ); 186 | 187 | // Number of `u64`s in the register set 188 | let reg_size = [64, 64, 512][reg as usize]; 189 | 190 | // Fill the register set with `pat2`. 191 | for i in 0..reg_size / 8 { 192 | unsafe { 193 | load_generic( 194 | &mut *ctx, 195 | pat2[i * 8..].as_ptr() as *mut (), 196 | i, 197 | MemSize::_64, 198 | reg, 199 | false, 200 | ); 201 | } 202 | } 203 | 204 | // Load `pat1` to somewhere in the register 205 | unsafe { 206 | load_generic(&mut *ctx, pat1.as_ptr(), reg_offset, size, reg, interleaved); 207 | } 208 | 209 | // Read the whole register set 210 | let got = match reg { 211 | 0 => ctx.read_x().to_vec(), 212 | 1 => ctx.read_y().to_vec(), 213 | 2 => ctx.read_z().to_vec(), 214 | _ => unreachable!(), 215 | }; 216 | let got: Vec = got 217 | .array_chunks::<8>() 218 | .map(|x| u64::from_le_bytes(*x)) 219 | .collect(); 220 | 221 | // Calculate the expected result 222 | let mut expected: Vec = pat2[0..reg_size].to_owned(); 223 | if interleaved { 224 | // Assume the structure `z: [[u8; 64]; 64]` 225 | // 226 | // reg_offset is split into two parts: 227 | // 228 | // - `reg_index = reg_offset / 2 * 2` 229 | // - `second_half = reg_offset % 2`. 230 | // 231 | // Each input 64-bit value is split into low and high parts, and 232 | // the resultant low parts go to 233 | // `z[reg_index][second_half * 4..][..4]`. The high parts go to 234 | // `z[reg_index + 1][second_half * 4..][..4]` 235 | let reg_start = (reg_offset as usize % 2) * 4 + (reg_offset as usize / 2) * 16; 236 | for i in (0..size.num_bytes() / 8).step_by(2) { 237 | let low1 = pat1[i] & 0xffff_ffff; 238 | let low2 = pat1[i + 1] & 0xffff_ffff; 239 | let high1 = pat1[i] >> 32; 240 | let high2 = pat1[i + 1] >> 32; 241 | expected[(reg_start + i / 2) % reg_size] = low1 | (low2 << 32); 242 | expected[(reg_start + 8 + i / 2) % reg_size] = high1 | (high2 << 32); 243 | } 244 | } else { 245 | // Simple copy with register index wrap-around 246 | for i in 0..size.num_bytes() / 8 { 247 | expected[(reg_offset as usize * 8 + i) % reg_size] = pat1[i]; 248 | } 249 | } 250 | 251 | assert_eq!( 252 | got, expected, 253 | "\ngot = {:x?}\nexpected = {:x?}", 254 | got, expected 255 | ); 256 | } 257 | } 258 | -------------------------------------------------------------------------------- /tests/outer_prod.rs: -------------------------------------------------------------------------------- 1 | use amx::{prelude::*, XBytes, XRow, YBytes, YRow, ZRow}; 2 | use itertools::iproduct; 3 | 4 | fn init() { 5 | let _ = env_logger::builder().is_test(true).try_init(); 6 | } 7 | 8 | struct Xorshift32(u32); 9 | 10 | impl Xorshift32 { 11 | fn next(&mut self) -> u32 { 12 | self.0 ^= self.0 << 13; 13 | self.0 ^= self.0 >> 17; 14 | self.0 ^= self.0 << 5; 15 | self.0 16 | } 17 | } 18 | 19 | fn read_array_wrapping(a: &[T], i: usize) -> [T; N] { 20 | use std::mem::MaybeUninit; 21 | let mut out = [MaybeUninit::::uninit(); N]; 22 | for j in 0..N { 23 | out[j] = MaybeUninit::new(a[i.wrapping_add(j) % a.len()]); 24 | } 25 | unsafe { std::mem::transmute_copy(&out) } 26 | } 27 | 28 | #[test] 29 | fn outer_product_i16_xy_to_z() { 30 | init(); 31 | unsafe { 32 | let mut ctx = amx::AmxCtx::new().unwrap(); 33 | 34 | let mut rng = Xorshift32(0x114514); 35 | let in_x: Vec = (0..512).map(|_| rng.next() as u8).collect(); 36 | let in_y: Vec = (0..512).map(|_| rng.next() as u8).collect(); 37 | let mut expected_z = ctx.read_z(); 38 | 39 | for i in 0..8 { 40 | ctx.load512(&in_x[i * 64], XRow(i)); 41 | ctx.load512(&in_y[i * 64], YRow(i)); 42 | } 43 | 44 | log::info!("x = {:?}", *(in_x.as_ptr() as *const [[u16; 32]; 8])); 45 | log::info!("y = {:?}", *(in_y.as_ptr() as *const [[u16; 32]; 8])); 46 | 47 | for (x_offset, y_offset, &z_index) in iproduct!( 48 | (0..0x200).step_by(31), 49 | (0..0x200).step_by(47), 50 | &[0, 1, 50, 63] 51 | ) { 52 | log::debug!( 53 | "(x_offset, y_offset, z_index) = {:?}", 54 | (x_offset, y_offset, z_index) 55 | ); 56 | 57 | ctx.outer_product_i16_xy_to_z( 58 | Some(XBytes(x_offset)), 59 | Some(YBytes(y_offset)), 60 | ZRow(z_index), 61 | false, // don't accumulate 62 | ); 63 | 64 | // Calculate the expected answer 65 | for x_i in (0..64usize).step_by(2) { 66 | for y_i in (0..64usize).step_by(2) { 67 | let x = 68 | i16::from_le_bytes(read_array_wrapping(&in_x, x_i.wrapping_add(x_offset))); 69 | let y = 70 | i16::from_le_bytes(read_array_wrapping(&in_y, y_i.wrapping_add(y_offset))); 71 | let prod = x.wrapping_mul(y).to_le_bytes(); 72 | let out_row = (z_index % 2 + y_i) % 64; 73 | expected_z[out_row * 64 + x_i..][..2].copy_from_slice(&prod); 74 | } 75 | } 76 | 77 | // Get the actual answer 78 | let got_z = ctx.read_z(); 79 | 80 | assert_eq!( 81 | std::mem::transmute::<_, [[u16; 32]; 64]>(got_z), 82 | std::mem::transmute::<_, [[u16; 32]; 64]>(expected_z) 83 | ); 84 | } 85 | } 86 | } 87 | --------------------------------------------------------------------------------