├── .gitignore ├── Cargo.toml ├── LICENSE.Apache-2.0 ├── LICENSE.MIT ├── README.md └── src ├── const_shape.rs ├── lib.rs └── runtime_shape.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "ndshape" 3 | description = "Simple, fast linearization of N-dimensional array indices" 4 | version = "0.3.0" 5 | edition = "2021" 6 | license = "MIT OR Apache-2.0" 7 | repository = "https://github.com/bonsairobo/ndshape-rs" 8 | keywords = ["array", "multidimensional"] 9 | 10 | [dependencies] 11 | static_assertions = "1.1" 12 | -------------------------------------------------------------------------------- /LICENSE.Apache-2.0: -------------------------------------------------------------------------------- 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 2021 bonsairobo 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 | MIT License 2 | 3 | Copyright (c) 2021 bonsairobo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ndshape 2 | 3 | Simple, fast linearization of 2D, 3D, and 4D coordinates. 4 | 5 | The canonical choice of linearization function is row-major, i.e. stepping linearly through an N dimensional array would 6 | step by X first, then Y, then Z, etc, assuming that `[T; N]` coordinates are provided as `[X, Y, Z, ...]`. More explicitly: 7 | 8 | ``` 9 | linearize([x, y, z, ...]) = x + X_SIZE * y + X_SIZE * Y_SIZE * z + ... 10 | ``` 11 | 12 | To achieve a different layout, one only needs to choose a different permutation of coordinates. For example, column-major 13 | layout would require coordinates specified as `[..., Z, Y, X]`. For a 3D layout where each Y level set is contiguous in 14 | memory, either layout `[X, Z, Y]` or `[Z, X, Y]` would work. 15 | 16 | ## Example: Indexing Multidimensional Arrays 17 | 18 | ```rust 19 | use ndshape::{Shape, ConstShape3u32, ConstShape4u32, ConstPow2Shape3u32, RuntimeShape}; 20 | 21 | // An arbitrary shape. 22 | let shape = ConstShape3u32::<5, 6, 7>; 23 | let index = shape.linearize([1, 2, 3]); 24 | assert_eq!(index, 101); 25 | assert_eq!(shape.delinearize(index), [1, 2, 3]); 26 | 27 | // A shape with power-of-two dimensions 28 | // This allows us to use bit shifting and masking for linearization. 29 | let shape = ConstPow2Shape3u32::<1, 2, 3>; // These are number of bits per dimension. 30 | let index = shape.linearize([1, 2, 3]); 31 | assert_eq!(index, 0b011_10_1); 32 | assert_eq!(shape.delinearize(index), [1, 2, 3]); 33 | 34 | // A runtime shape. 35 | let shape = RuntimeShape::::new([5, 6, 7]); 36 | let index = shape.linearize([1, 2, 3]); 37 | assert_eq!(index, 101); 38 | assert_eq!(shape.delinearize(index), [1, 2, 3]); 39 | 40 | // Use a shape for indexing an array in 4D. 41 | // Step X, then Y, then Z, since that results in monotonic increasing indices. 42 | // (Believe it or not, Rust's N-dimensional array (e.g. `[[T; N]; M]`) 43 | // indexing is significantly slower than this). 44 | let shape = ConstShape4u32::<5, 6, 7, 8>; 45 | let data = [0; 5 * 6 * 7 * 8]; 46 | for w in 0..8 { 47 | for z in 0..7 { 48 | for y in 0..6 { 49 | for x in 0..5 { 50 | let i = shape.linearize([x, y, z, w]); 51 | assert_eq!(0, data[i as usize]); 52 | } 53 | } 54 | } 55 | } 56 | ``` 57 | 58 | ## Example: Negative Strides with Modular Arithmetic 59 | 60 | It is often beneficial to linearize a negative vector that results in a negative linear "stride." But when using unsigned 61 | linear indices, a negative stride would require a modular arithmetic representation, where e.g. `-1` maps to `u32::MAX`. 62 | This works fine with any [`Shape`](crate::Shape). You just need to be sure to use modular arithmetic with the resulting 63 | linear strides, e.g. [`u32::wrapping_add`](u32::wrapping_add) and [`u32::wrapping_mul`](u32::wrapping_mul). Also, it is not 64 | possible to delinearize a negative stride with modular arithmetic. For that, you must use signed integer coordinates. 65 | 66 | ```rust 67 | use ndshape::{Shape, ConstShape3u32, ConstShape3i32}; 68 | 69 | let shape = ConstShape3u32::<10, 10, 10>; 70 | let stride = shape.linearize([0, -1i32 as u32, 0]); 71 | assert_eq!(stride, -10i32 as u32); 72 | 73 | // Delinearize does not work with unsigned coordinates! 74 | assert_ne!(shape.delinearize(stride), [0, -1i32 as u32, 0]); 75 | assert_eq!(shape.delinearize(stride), [6, 8, 42949672]); 76 | 77 | let shape = ConstShape3i32::<10, 10, 10>; 78 | let stride = shape.linearize([0, -1, 0]); 79 | assert_eq!(stride, -10); 80 | 81 | // Delinearize works with signed coordinates. 82 | assert_eq!(shape.delinearize(stride), [0, -1, 0]); 83 | ``` 84 | 85 | License: MIT OR Apache-2.0 86 | -------------------------------------------------------------------------------- /src/const_shape.rs: -------------------------------------------------------------------------------- 1 | use crate::{AbstractShape, ConstShape, Shape}; 2 | 3 | use static_assertions::assert_impl_all; 4 | 5 | macro_rules! impl_const_shape2 { 6 | ($name:ident, $scalar:ty) => { 7 | #[derive(Clone, Debug, Copy, Eq, PartialEq)] 8 | pub struct $name; 9 | 10 | impl $name { 11 | pub const STRIDES: [$scalar; 2] = [1, X]; 12 | } 13 | 14 | impl ConstShape<2> for $name { 15 | type Coord = $scalar; 16 | 17 | const ARRAY: [$scalar; 2] = [X, Y]; 18 | const SIZE: $scalar = X * Y; 19 | const USIZE: usize = Self::SIZE as usize; 20 | 21 | #[inline] 22 | fn linearize(p: [$scalar; 2]) -> $scalar { 23 | p[0] + Self::STRIDES[1].wrapping_mul(p[1]) 24 | } 25 | 26 | #[inline] 27 | fn delinearize(i: $scalar) -> [$scalar; 2] { 28 | let y = i / Self::STRIDES[1]; 29 | let x = i % Self::STRIDES[1]; 30 | [x, y] 31 | } 32 | } 33 | 34 | assert_impl_all!($name<1, 1>: AbstractShape<$scalar, [$scalar; 2]>); 35 | assert_impl_all!($name<1, 1>: Shape<2>); 36 | }; 37 | } 38 | 39 | impl_const_shape2!(ConstShape2u8, u8); 40 | impl_const_shape2!(ConstShape2u16, u16); 41 | impl_const_shape2!(ConstShape2u32, u32); 42 | impl_const_shape2!(ConstShape2u64, u64); 43 | impl_const_shape2!(ConstShape2usize, usize); 44 | 45 | impl_const_shape2!(ConstShape2i8, i8); 46 | impl_const_shape2!(ConstShape2i16, i16); 47 | impl_const_shape2!(ConstShape2i32, i32); 48 | impl_const_shape2!(ConstShape2i64, i64); 49 | 50 | macro_rules! impl_const_shape3 { 51 | ($name:ident, $scalar:ty) => { 52 | #[derive(Clone, Debug, Copy, Eq, PartialEq)] 53 | pub struct $name; 54 | 55 | impl $name { 56 | pub const STRIDES: [$scalar; 3] = [1, X, X * Y]; 57 | } 58 | 59 | impl ConstShape<3> 60 | for $name 61 | { 62 | type Coord = $scalar; 63 | 64 | const ARRAY: [$scalar; 3] = [X, Y, Z]; 65 | const SIZE: $scalar = X * Y * Z; 66 | const USIZE: usize = Self::SIZE as usize; 67 | 68 | #[inline] 69 | fn linearize(p: [$scalar; 3]) -> $scalar { 70 | p[0] + Self::STRIDES[1].wrapping_mul(p[1]) + Self::STRIDES[2].wrapping_mul(p[2]) 71 | } 72 | 73 | #[inline] 74 | fn delinearize(mut i: $scalar) -> [$scalar; 3] { 75 | let z = i / Self::STRIDES[2]; 76 | i -= z * Self::STRIDES[2]; 77 | let y = i / Self::STRIDES[1]; 78 | let x = i % Self::STRIDES[1]; 79 | [x, y, z] 80 | } 81 | } 82 | 83 | assert_impl_all!($name<1, 1, 1>: AbstractShape<$scalar, [$scalar; 3]>); 84 | assert_impl_all!($name<1, 1, 1>: Shape<3>); 85 | }; 86 | } 87 | 88 | impl_const_shape3!(ConstShape3u8, u8); 89 | impl_const_shape3!(ConstShape3u16, u16); 90 | impl_const_shape3!(ConstShape3u32, u32); 91 | impl_const_shape3!(ConstShape3u64, u64); 92 | impl_const_shape3!(ConstShape3usize, usize); 93 | 94 | impl_const_shape3!(ConstShape3i8, i8); 95 | impl_const_shape3!(ConstShape3i16, i16); 96 | impl_const_shape3!(ConstShape3i32, i32); 97 | impl_const_shape3!(ConstShape3i64, i64); 98 | 99 | macro_rules! impl_const_shape4 { 100 | ($name:ident, $scalar:ty) => { 101 | #[derive(Clone, Debug, Copy, Eq, PartialEq)] 102 | pub struct $name; 103 | 104 | impl 105 | $name 106 | { 107 | pub const STRIDES: [$scalar; 4] = [1, X, X * Y, X * Y * Z]; 108 | } 109 | 110 | impl 111 | ConstShape<4> for $name 112 | { 113 | type Coord = $scalar; 114 | 115 | const ARRAY: [$scalar; 4] = [X, Y, Z, W]; 116 | const SIZE: $scalar = X * Y * Z * W; 117 | const USIZE: usize = Self::SIZE as usize; 118 | 119 | #[inline] 120 | fn linearize(p: [$scalar; 4]) -> $scalar { 121 | p[0] + 122 | Self::STRIDES[1].wrapping_mul(p[1]) + 123 | Self::STRIDES[2].wrapping_mul(p[2]) + 124 | Self::STRIDES[3].wrapping_mul(p[3]) 125 | } 126 | 127 | #[inline] 128 | fn delinearize(mut i: $scalar) -> [$scalar; 4] { 129 | let w = i / Self::STRIDES[3]; 130 | i -= w * Self::STRIDES[3]; 131 | let z = i / Self::STRIDES[2]; 132 | i -= z * Self::STRIDES[2]; 133 | let y = i / Self::STRIDES[1]; 134 | let x = i % Self::STRIDES[1]; 135 | [x, y, z, w] 136 | } 137 | } 138 | 139 | assert_impl_all!($name<1, 1, 1, 1>: AbstractShape<$scalar, [$scalar; 4]>); 140 | assert_impl_all!($name<1, 1, 1, 1>: Shape<4>); 141 | }; 142 | } 143 | 144 | impl_const_shape4!(ConstShape4u8, u8); 145 | impl_const_shape4!(ConstShape4u16, u16); 146 | impl_const_shape4!(ConstShape4u32, u32); 147 | impl_const_shape4!(ConstShape4u64, u64); 148 | impl_const_shape4!(ConstShape4usize, usize); 149 | 150 | impl_const_shape4!(ConstShape4i8, i8); 151 | impl_const_shape4!(ConstShape4i16, i16); 152 | impl_const_shape4!(ConstShape4i32, i32); 153 | impl_const_shape4!(ConstShape4i64, i64); 154 | 155 | macro_rules! impl_const_pow2_shape2 { 156 | ($name:ident, $scalar:ty) => { 157 | #[derive(Clone, Debug, Copy, Eq, PartialEq)] 158 | pub struct $name; 159 | 160 | impl $name { 161 | pub const SHIFTS: [$scalar; 2] = [0, X]; 162 | 163 | pub const MASKS: [$scalar; 2] = [ 164 | !(!0 << X), 165 | !(!0 << Y) << Self::SHIFTS[1] 166 | ]; 167 | } 168 | 169 | impl ConstShape<2> for $name { 170 | type Coord = $scalar; 171 | 172 | const ARRAY: [$scalar; 2] = [1 << X, 1 << Y]; 173 | const SIZE: $scalar = 1 << (X + Y); 174 | const USIZE: usize = Self::SIZE as usize; 175 | 176 | #[inline] 177 | fn linearize(p: [$scalar; 2]) -> $scalar { 178 | (p[1] << Self::SHIFTS[1]) | p[0] 179 | } 180 | 181 | #[inline] 182 | fn delinearize(i: $scalar) -> [$scalar; 2] { 183 | [(i & Self::MASKS[0]), ((i & Self::MASKS[1]) >> Self::SHIFTS[1])] 184 | } 185 | } 186 | 187 | assert_impl_all!($name<1, 1>: AbstractShape<$scalar, [$scalar; 2]>); 188 | assert_impl_all!($name<1, 1>: Shape<2>); 189 | }; 190 | } 191 | 192 | impl_const_pow2_shape2!(ConstPow2Shape2u8, u8); 193 | impl_const_pow2_shape2!(ConstPow2Shape2u16, u16); 194 | impl_const_pow2_shape2!(ConstPow2Shape2u32, u32); 195 | impl_const_pow2_shape2!(ConstPow2Shape2u64, u64); 196 | impl_const_pow2_shape2!(ConstPow2Shape2usize, usize); 197 | 198 | impl_const_pow2_shape2!(ConstPow2Shape2i8, i8); 199 | impl_const_pow2_shape2!(ConstPow2Shape2i16, i16); 200 | impl_const_pow2_shape2!(ConstPow2Shape2i32, i32); 201 | impl_const_pow2_shape2!(ConstPow2Shape2i64, i64); 202 | 203 | macro_rules! impl_const_pow2_shape3 { 204 | ($name:ident, $scalar:ty) => { 205 | #[derive(Clone, Debug, Copy, Eq, PartialEq)] 206 | pub struct $name; 207 | 208 | impl $name { 209 | pub const SHIFTS: [$scalar; 3] = [0, X, X + Y]; 210 | 211 | pub const MASKS: [$scalar; 3] = [ 212 | !(!0 << X), 213 | !(!0 << Y) << Self::SHIFTS[1], 214 | !(!0 << Z) << Self::SHIFTS[2], 215 | ]; 216 | } 217 | 218 | impl ConstShape<3> 219 | for $name 220 | { 221 | type Coord = $scalar; 222 | 223 | const ARRAY: [$scalar; 3] = [1 << X, 1 << Y, 1 << Z]; 224 | const SIZE: $scalar = 1 << (X + Y + Z); 225 | const USIZE: usize = Self::SIZE as usize; 226 | 227 | #[inline] 228 | fn linearize(p: [$scalar; 3]) -> $scalar { 229 | (p[2] << Self::SHIFTS[2]) | (p[1] << Self::SHIFTS[1]) | p[0] 230 | } 231 | 232 | #[inline] 233 | fn delinearize(i: $scalar) -> [$scalar; 3] { 234 | [ 235 | (i & Self::MASKS[0]), 236 | ((i & Self::MASKS[1]) >> Self::SHIFTS[1]), 237 | ((i & Self::MASKS[2]) >> Self::SHIFTS[2]), 238 | ] 239 | } 240 | } 241 | 242 | assert_impl_all!($name<1, 1, 1>: AbstractShape<$scalar, [$scalar; 3]>); 243 | assert_impl_all!($name<1, 1, 1>: Shape<3>); 244 | }; 245 | } 246 | 247 | impl_const_pow2_shape3!(ConstPow2Shape3u8, u8); 248 | impl_const_pow2_shape3!(ConstPow2Shape3u16, u16); 249 | impl_const_pow2_shape3!(ConstPow2Shape3u32, u32); 250 | impl_const_pow2_shape3!(ConstPow2Shape3u64, u64); 251 | impl_const_pow2_shape3!(ConstPow2Shape3usize, usize); 252 | 253 | impl_const_pow2_shape3!(ConstPow2Shape3i8, i8); 254 | impl_const_pow2_shape3!(ConstPow2Shape3i16, i16); 255 | impl_const_pow2_shape3!(ConstPow2Shape3i32, i32); 256 | impl_const_pow2_shape3!(ConstPow2Shape3i64, i64); 257 | 258 | macro_rules! impl_const_pow2_shape4 { 259 | ($name:ident, $scalar:ty) => { 260 | #[derive(Clone, Debug, Copy, Eq, PartialEq)] 261 | pub struct $name; 262 | 263 | impl 264 | $name 265 | { 266 | pub const SHIFTS: [$scalar; 4] = [0, X, X + Y, X + Y + Z]; 267 | 268 | pub const MASKS: [$scalar; 4] = [ 269 | !(!0 << X), 270 | !(!0 << Y) << Self::SHIFTS[1], 271 | !(!0 << Z) << Self::SHIFTS[2], 272 | !(!0 << W) << Self::SHIFTS[3], 273 | ]; 274 | } 275 | 276 | impl 277 | ConstShape<4> for $name 278 | { 279 | type Coord = $scalar; 280 | 281 | const ARRAY: [$scalar; 4] = [1 << X, 1 << Y, 1 << Z, 1 << W]; 282 | const SIZE: $scalar = 1 << (X + Y + Z + W); 283 | const USIZE: usize = Self::SIZE as usize; 284 | 285 | #[inline] 286 | fn linearize(p: [$scalar; 4]) -> $scalar { 287 | (p[3] << Self::SHIFTS[3]) | (p[2] << Self::SHIFTS[2]) | (p[1] << Self::SHIFTS[1]) | p[0] 288 | } 289 | 290 | #[inline] 291 | fn delinearize(i: $scalar) -> [$scalar; 4] { 292 | [ 293 | (i & Self::MASKS[0]), 294 | ((i & Self::MASKS[1]) >> Self::SHIFTS[1]), 295 | ((i & Self::MASKS[2]) >> Self::SHIFTS[2]), 296 | ((i & Self::MASKS[3]) >> Self::SHIFTS[3]), 297 | ] 298 | } 299 | } 300 | 301 | assert_impl_all!($name<1, 1, 1, 1>: AbstractShape<$scalar, [$scalar; 4]>); 302 | assert_impl_all!($name<1, 1, 1, 1>: Shape<4>); 303 | }; 304 | } 305 | 306 | impl_const_pow2_shape4!(ConstPow2Shape4u8, u8); 307 | impl_const_pow2_shape4!(ConstPow2Shape4u16, u16); 308 | impl_const_pow2_shape4!(ConstPow2Shape4u32, u32); 309 | impl_const_pow2_shape4!(ConstPow2Shape4u64, u64); 310 | impl_const_pow2_shape4!(ConstPow2Shape4usize, usize); 311 | 312 | impl_const_pow2_shape4!(ConstPow2Shape4i8, i8); 313 | impl_const_pow2_shape4!(ConstPow2Shape4i16, i16); 314 | impl_const_pow2_shape4!(ConstPow2Shape4i32, i32); 315 | impl_const_pow2_shape4!(ConstPow2Shape4i64, i64); 316 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Simple, fast linearization of 2D, 3D, and 4D coordinates. 2 | //! 3 | //! The canonical choice of linearization function is row-major, i.e. stepping linearly through an N dimensional array would 4 | //! step by X first, then Y, then Z, etc, assuming that `[T; N]` coordinates are provided as `[X, Y, Z, ...]`. More explicitly: 5 | //! 6 | //! ```text 7 | //! linearize([x, y, z, ...]) = x + X_SIZE * y + X_SIZE * Y_SIZE * z + ... 8 | //! ``` 9 | //! 10 | //! To achieve a different layout, one only needs to choose a different permutation of coordinates. For example, column-major 11 | //! layout would require coordinates specified as `[..., Z, Y, X]`. For a 3D layout where each Y level set is contiguous in 12 | //! memory, either layout `[X, Z, Y]` or `[Z, X, Y]` would work. 13 | //! 14 | //! # Example: Indexing Multidimensional Arrays 15 | //! 16 | //! ``` 17 | //! use ndshape::{Shape, ConstShape3u32, ConstShape4u32, ConstPow2Shape3u32, RuntimeShape}; 18 | //! 19 | //! // An arbitrary shape. 20 | //! let shape = ConstShape3u32::<5, 6, 7>; 21 | //! let index = shape.linearize([1, 2, 3]); 22 | //! assert_eq!(index, 101); 23 | //! assert_eq!(shape.delinearize(index), [1, 2, 3]); 24 | //! 25 | //! // A shape with power-of-two dimensions 26 | //! // This allows us to use bit shifting and masking for linearization. 27 | //! let shape = ConstPow2Shape3u32::<1, 2, 3>; // These are number of bits per dimension. 28 | //! let index = shape.linearize([1, 2, 3]); 29 | //! assert_eq!(index, 0b011_10_1); 30 | //! assert_eq!(shape.delinearize(index), [1, 2, 3]); 31 | //! 32 | //! // A runtime shape. 33 | //! let shape = RuntimeShape::::new([5, 6, 7]); 34 | //! let index = shape.linearize([1, 2, 3]); 35 | //! assert_eq!(index, 101); 36 | //! assert_eq!(shape.delinearize(index), [1, 2, 3]); 37 | //! 38 | //! // Use a shape for indexing an array in 4D. 39 | //! // Step X, then Y, then Z, since that results in monotonic increasing indices. 40 | //! // (Believe it or not, Rust's N-dimensional array (e.g. `[[T; N]; M]`) 41 | //! // indexing is significantly slower than this). 42 | //! let shape = ConstShape4u32::<5, 6, 7, 8>; 43 | //! let data = [0; 5 * 6 * 7 * 8]; 44 | //! for w in 0..8 { 45 | //! for z in 0..7 { 46 | //! for y in 0..6 { 47 | //! for x in 0..5 { 48 | //! let i = shape.linearize([x, y, z, w]); 49 | //! assert_eq!(0, data[i as usize]); 50 | //! } 51 | //! } 52 | //! } 53 | //! } 54 | //! ``` 55 | //! 56 | //! # Example: Negative Strides with Modular Arithmetic 57 | //! 58 | //! It is often beneficial to linearize a negative vector that results in a negative linear "stride." But when using unsigned 59 | //! linear indices, a negative stride would require a modular arithmetic representation, where e.g. `-1` maps to `u32::MAX`. 60 | //! This works fine with any [`Shape`](crate::Shape). You just need to be sure to use modular arithmetic with the resulting 61 | //! linear strides, e.g. [`u32::wrapping_add`](u32::wrapping_add) and [`u32::wrapping_mul`](u32::wrapping_mul). Also, it is not 62 | //! possible to delinearize a negative stride with modular arithmetic. For that, you must use signed integer coordinates. 63 | //! 64 | //! ``` 65 | //! use ndshape::{Shape, ConstShape3u32, ConstShape3i32}; 66 | //! 67 | //! let shape = ConstShape3u32::<10, 10, 10>; 68 | //! let stride = shape.linearize([0, -1i32 as u32, 0]); 69 | //! assert_eq!(stride, -10i32 as u32); 70 | //! 71 | //! // Delinearize does not work with unsigned coordinates! 72 | //! assert_ne!(shape.delinearize(stride), [0, -1i32 as u32, 0]); 73 | //! assert_eq!(shape.delinearize(stride), [6, 8, 42949672]); 74 | //! 75 | //! let shape = ConstShape3i32::<10, 10, 10>; 76 | //! let stride = shape.linearize([0, -1, 0]); 77 | //! assert_eq!(stride, -10); 78 | //! 79 | //! // Delinearize works with signed coordinates. 80 | //! assert_eq!(shape.delinearize(stride), [0, -1, 0]); 81 | //! ``` 82 | 83 | mod const_shape; 84 | mod runtime_shape; 85 | 86 | pub use const_shape::*; 87 | pub use runtime_shape::*; 88 | 89 | /// The shape of an array with unspecified dimensionality. 90 | pub trait AbstractShape { 91 | /// The number of elements in an array with this shape. 92 | fn size(&self) -> Coord; 93 | /// Translates a vector `V` (with an unspecified number of dimensions) into a single number `T` that can be used for 94 | /// linear indexing. 95 | fn linearize(&self, p: Vector) -> Coord; 96 | /// The inverse of `linearize`. 97 | fn delinearize(&self, i: Coord) -> Vector; 98 | } 99 | 100 | /// The shape of an `N`-dimensional array. 101 | pub trait Shape { 102 | type Coord; 103 | 104 | /// The number of elements in an array with this shape. 105 | fn size(&self) -> Self::Coord; 106 | /// The same as `self.size() as usize`. 107 | fn usize(&self) -> usize; 108 | /// The dimensions of the shape. 109 | fn as_array(&self) -> [Self::Coord; N]; 110 | /// Translate an `N`-dimensional vector into a single number `T` that can be used for linear indexing. 111 | fn linearize(&self, p: [Self::Coord; N]) -> Self::Coord; 112 | /// The inverse of `linearize`. 113 | fn delinearize(&self, i: Self::Coord) -> [Self::Coord; N]; 114 | } 115 | 116 | /// A constant shape of an `N`-dimensional array. 117 | pub trait ConstShape { 118 | type Coord; 119 | 120 | /// The number of elements in an array with this shape. 121 | const SIZE: Self::Coord; 122 | /// Same as `Self::SIZE as usize`. 123 | const USIZE: usize; 124 | /// The dimensions of the shape. 125 | const ARRAY: [Self::Coord; N]; 126 | /// Translate an `N`-dimensional vector into a single number `T` that can be used for linear indexing. 127 | fn linearize(p: [Self::Coord; N]) -> Self::Coord; 128 | /// The inverse of `linearize`. 129 | fn delinearize(i: Self::Coord) -> [Self::Coord; N]; 130 | } 131 | 132 | impl AbstractShape for S 133 | where 134 | S: Shape, 135 | { 136 | #[inline] 137 | fn size(&self) -> S::Coord { 138 | self.size() 139 | } 140 | #[inline] 141 | fn linearize(&self, p: [S::Coord; N]) -> S::Coord { 142 | self.linearize(p) 143 | } 144 | #[inline] 145 | fn delinearize(&self, i: S::Coord) -> [S::Coord; N] { 146 | self.delinearize(i) 147 | } 148 | } 149 | 150 | impl Shape for S 151 | where 152 | S: ConstShape, 153 | { 154 | type Coord = S::Coord; 155 | 156 | #[inline] 157 | fn size(&self) -> Self::Coord { 158 | S::SIZE 159 | } 160 | #[inline] 161 | fn usize(&self) -> usize { 162 | S::USIZE 163 | } 164 | #[inline] 165 | fn as_array(&self) -> [Self::Coord; N] { 166 | S::ARRAY 167 | } 168 | #[inline] 169 | fn linearize(&self, p: [Self::Coord; N]) -> Self::Coord { 170 | S::linearize(p) 171 | } 172 | #[inline] 173 | fn delinearize(&self, i: Self::Coord) -> [Self::Coord; N] { 174 | S::delinearize(i) 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /src/runtime_shape.rs: -------------------------------------------------------------------------------- 1 | use crate::Shape; 2 | 3 | #[derive(Clone)] 4 | pub struct RuntimeShape { 5 | array: [C; N], 6 | strides: [C; N], 7 | size: C, 8 | } 9 | 10 | macro_rules! impl_shape2 { 11 | ($scalar:ident) => { 12 | impl RuntimeShape<$scalar, 2> { 13 | pub fn new([x, y]: [$scalar; 2]) -> Self { 14 | Self { 15 | array: [x, y], 16 | strides: [1, x], 17 | size: x * y, 18 | } 19 | } 20 | } 21 | 22 | impl Shape<2> for RuntimeShape<$scalar, 2> { 23 | type Coord = $scalar; 24 | 25 | #[inline] 26 | fn as_array(&self) -> [$scalar; 2] { 27 | self.array 28 | } 29 | 30 | #[inline] 31 | fn size(&self) -> $scalar { 32 | self.size 33 | } 34 | 35 | #[inline] 36 | fn usize(&self) -> usize { 37 | self.size as usize 38 | } 39 | 40 | #[inline] 41 | fn linearize(&self, p: [$scalar; 2]) -> $scalar { 42 | p[0] + self.strides[1].wrapping_mul(p[1]) 43 | } 44 | 45 | #[inline] 46 | fn delinearize(&self, i: $scalar) -> [$scalar; 2] { 47 | let y = i / self.strides[1]; 48 | let x = i % self.strides[1]; 49 | [x, y] 50 | } 51 | } 52 | }; 53 | } 54 | 55 | impl_shape2!(u8); 56 | impl_shape2!(u16); 57 | impl_shape2!(u32); 58 | impl_shape2!(u64); 59 | impl_shape2!(usize); 60 | 61 | impl_shape2!(i8); 62 | impl_shape2!(i16); 63 | impl_shape2!(i32); 64 | impl_shape2!(i64); 65 | 66 | macro_rules! impl_shape3 { 67 | ($scalar:ident) => { 68 | impl RuntimeShape<$scalar, 3> { 69 | pub fn new([x, y, z]: [$scalar; 3]) -> Self { 70 | Self { 71 | array: [x, y, z], 72 | strides: [1, x, x * y], 73 | size: x * y * z, 74 | } 75 | } 76 | } 77 | 78 | impl Shape<3> for RuntimeShape<$scalar, 3> { 79 | type Coord = $scalar; 80 | 81 | #[inline] 82 | fn as_array(&self) -> [$scalar; 3] { 83 | self.array 84 | } 85 | 86 | #[inline] 87 | fn size(&self) -> $scalar { 88 | self.size 89 | } 90 | 91 | #[inline] 92 | fn usize(&self) -> usize { 93 | self.size as usize 94 | } 95 | 96 | #[inline] 97 | fn linearize(&self, p: [$scalar; 3]) -> $scalar { 98 | p[0] + self.strides[1].wrapping_mul(p[1]) + self.strides[2].wrapping_mul(p[2]) 99 | } 100 | 101 | #[inline] 102 | fn delinearize(&self, mut i: $scalar) -> [$scalar; 3] { 103 | let z = i / self.strides[2]; 104 | i -= z * self.strides[2]; 105 | let y = i / self.strides[1]; 106 | let x = i % self.strides[1]; 107 | [x, y, z] 108 | } 109 | } 110 | }; 111 | } 112 | 113 | impl_shape3!(u8); 114 | impl_shape3!(u16); 115 | impl_shape3!(u32); 116 | impl_shape3!(u64); 117 | impl_shape3!(usize); 118 | 119 | impl_shape3!(i8); 120 | impl_shape3!(i16); 121 | impl_shape3!(i32); 122 | impl_shape3!(i64); 123 | 124 | macro_rules! impl_shape4 { 125 | ($scalar:ident) => { 126 | impl RuntimeShape<$scalar, 4> { 127 | pub fn new([x, y, z, w]: [$scalar; 4]) -> Self { 128 | Self { 129 | array: [x, y, z, w], 130 | strides: [1, x, x * y, x * y * z], 131 | size: x * y * z * w, 132 | } 133 | } 134 | } 135 | 136 | impl Shape<4> for RuntimeShape<$scalar, 4> { 137 | type Coord = $scalar; 138 | 139 | #[inline] 140 | fn as_array(&self) -> [$scalar; 4] { 141 | self.array 142 | } 143 | 144 | #[inline] 145 | fn size(&self) -> $scalar { 146 | self.size 147 | } 148 | 149 | #[inline] 150 | fn usize(&self) -> usize { 151 | self.size as usize 152 | } 153 | 154 | #[inline] 155 | fn linearize(&self, p: [$scalar; 4]) -> $scalar { 156 | p[0] + self.strides[1].wrapping_mul(p[1]) 157 | + self.strides[2].wrapping_mul(p[2]) 158 | + self.strides[3].wrapping_mul(p[3]) 159 | } 160 | 161 | #[inline] 162 | fn delinearize(&self, mut i: $scalar) -> [$scalar; 4] { 163 | let w = i / self.strides[3]; 164 | i -= w * self.strides[3]; 165 | let z = i / self.strides[2]; 166 | i -= z * self.strides[2]; 167 | let y = i / self.strides[1]; 168 | let x = i % self.strides[1]; 169 | [x, y, z, w] 170 | } 171 | } 172 | }; 173 | } 174 | 175 | impl_shape4!(u8); 176 | impl_shape4!(u16); 177 | impl_shape4!(u32); 178 | impl_shape4!(u64); 179 | impl_shape4!(usize); 180 | 181 | impl_shape4!(i8); 182 | impl_shape4!(i16); 183 | impl_shape4!(i32); 184 | impl_shape4!(i64); 185 | 186 | #[derive(Clone)] 187 | pub struct RuntimePow2Shape { 188 | array: [C; N], 189 | shifts: [C; N], 190 | masks: [C; N], 191 | size: C, 192 | } 193 | 194 | macro_rules! impl_pow2_shape2 { 195 | ($scalar:ty) => { 196 | impl RuntimePow2Shape<$scalar, 2> { 197 | pub fn new([x, y]: [$scalar; 2]) -> Self { 198 | let y_shift = x; 199 | Self { 200 | array: [1 << x, 1 << y], 201 | shifts: [0, y_shift], 202 | size: 1 << x + y, 203 | masks: [!(!0 << x), !(!0 << y) << y_shift], 204 | } 205 | } 206 | } 207 | 208 | impl Shape<2> for RuntimePow2Shape<$scalar, 2> { 209 | type Coord = $scalar; 210 | 211 | #[inline] 212 | fn as_array(&self) -> [$scalar; 2] { 213 | self.array 214 | } 215 | 216 | #[inline] 217 | fn size(&self) -> $scalar { 218 | self.size 219 | } 220 | 221 | #[inline] 222 | fn usize(&self) -> usize { 223 | self.size as usize 224 | } 225 | 226 | #[inline] 227 | fn linearize(&self, p: [$scalar; 2]) -> $scalar { 228 | (p[1] << self.shifts[1]) | p[0] 229 | } 230 | 231 | #[inline] 232 | fn delinearize(&self, i: $scalar) -> [$scalar; 2] { 233 | [i & self.masks[0], (i & self.masks[1]) >> self.shifts[1]] 234 | } 235 | } 236 | }; 237 | } 238 | 239 | impl_pow2_shape2!(u8); 240 | impl_pow2_shape2!(u16); 241 | impl_pow2_shape2!(u32); 242 | impl_pow2_shape2!(u64); 243 | impl_pow2_shape2!(usize); 244 | 245 | impl_pow2_shape2!(i8); 246 | impl_pow2_shape2!(i16); 247 | impl_pow2_shape2!(i32); 248 | impl_pow2_shape2!(i64); 249 | 250 | macro_rules! impl_pow2_shape3 { 251 | ($scalar:ty) => { 252 | impl RuntimePow2Shape<$scalar, 3> { 253 | pub fn new([x, y, z]: [$scalar; 3]) -> Self { 254 | let y_shift = x; 255 | let z_shift = x + y; 256 | Self { 257 | array: [1 << x, 1 << y, 1 << z], 258 | shifts: [0, y_shift, z_shift], 259 | masks: [!(!0 << x), !(!0 << y) << y_shift, !(!0 << z) << z_shift], 260 | size: 1 << x + y + z, 261 | } 262 | } 263 | } 264 | 265 | impl Shape<3> for RuntimePow2Shape<$scalar, 3> { 266 | type Coord = $scalar; 267 | 268 | #[inline] 269 | fn as_array(&self) -> [$scalar; 3] { 270 | self.array 271 | } 272 | 273 | #[inline] 274 | fn size(&self) -> $scalar { 275 | self.size 276 | } 277 | 278 | #[inline] 279 | fn usize(&self) -> usize { 280 | self.size as usize 281 | } 282 | 283 | #[inline] 284 | fn linearize(&self, p: [$scalar; 3]) -> $scalar { 285 | (p[2] << self.shifts[2]) | (p[1] << self.shifts[1]) | p[0] 286 | } 287 | 288 | #[inline] 289 | fn delinearize(&self, i: $scalar) -> [$scalar; 3] { 290 | [ 291 | i & self.masks[0], 292 | (i & self.masks[1]) >> self.shifts[1], 293 | (i & self.masks[2]) >> self.shifts[2], 294 | ] 295 | } 296 | } 297 | }; 298 | } 299 | 300 | impl_pow2_shape3!(u8); 301 | impl_pow2_shape3!(u16); 302 | impl_pow2_shape3!(u32); 303 | impl_pow2_shape3!(u64); 304 | impl_pow2_shape3!(usize); 305 | 306 | impl_pow2_shape3!(i8); 307 | impl_pow2_shape3!(i16); 308 | impl_pow2_shape3!(i32); 309 | impl_pow2_shape3!(i64); 310 | 311 | macro_rules! impl_pow2_shape4 { 312 | ($scalar:ty) => { 313 | impl RuntimePow2Shape<$scalar, 4> { 314 | pub fn new([x, y, z, w]: [$scalar; 4]) -> Self { 315 | let y_shift = x; 316 | let z_shift = x + y; 317 | let w_shift = x + y + z; 318 | Self { 319 | array: [1 << x, 1 << y, 1 << z, 1 << w], 320 | size: 1 << (x + y + z + w), 321 | shifts: [0, y_shift, z_shift, w_shift], 322 | masks: [ 323 | !(!0 << x), 324 | !(!0 << y) << y_shift, 325 | !(!0 << z) << z_shift, 326 | !(!0 << w) << w_shift, 327 | ], 328 | } 329 | } 330 | } 331 | 332 | impl Shape<4> for RuntimePow2Shape<$scalar, 4> { 333 | type Coord = $scalar; 334 | 335 | #[inline] 336 | fn as_array(&self) -> [$scalar; 4] { 337 | self.array 338 | } 339 | 340 | #[inline] 341 | fn size(&self) -> $scalar { 342 | self.size 343 | } 344 | 345 | #[inline] 346 | fn usize(&self) -> usize { 347 | self.size as usize 348 | } 349 | 350 | #[inline] 351 | fn linearize(&self, p: [$scalar; 4]) -> $scalar { 352 | (p[2] << self.shifts[2]) | (p[1] << self.shifts[1]) | p[0] 353 | } 354 | 355 | #[inline] 356 | fn delinearize(&self, i: $scalar) -> [$scalar; 4] { 357 | [ 358 | i & self.masks[0], 359 | (i & self.masks[1]) >> self.shifts[1], 360 | (i & self.masks[2]) >> self.shifts[2], 361 | (i & self.masks[3]) >> self.shifts[3], 362 | ] 363 | } 364 | } 365 | }; 366 | } 367 | 368 | impl_pow2_shape4!(u8); 369 | impl_pow2_shape4!(u16); 370 | impl_pow2_shape4!(u32); 371 | impl_pow2_shape4!(u64); 372 | impl_pow2_shape4!(usize); 373 | 374 | impl_pow2_shape4!(i8); 375 | impl_pow2_shape4!(i16); 376 | impl_pow2_shape4!(i32); 377 | impl_pow2_shape4!(i64); 378 | --------------------------------------------------------------------------------