|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2026 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# smskrev |
| 22 | + |
| 23 | +> Reverse a single-precision floating-point strided array in-place according to a mask. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var smskrev = require( '@stdlib/blas/ext/base/smskrev' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### smskrev( N, x, strideX, mask, strideMask ) |
| 34 | + |
| 35 | +Reverses a single-precision floating-point strided array `x` in-place according to a `mask`. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 39 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 40 | + |
| 41 | +var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] ); |
| 42 | +var mask = new Uint8Array( [ 0, 0, 0, 1, 0, 0, 0, 0 ] ); |
| 43 | + |
| 44 | +smskrev( x.length, x, 1, mask, 1 ); |
| 45 | +// x => <Float32Array>[ -3.0, -1.0, 0.0, -5.0, 4.0, 3.0, 1.0, -2.0 ] |
| 46 | +``` |
| 47 | + |
| 48 | +The function has the following parameters: |
| 49 | + |
| 50 | +- **N**: number of indexed elements. |
| 51 | +- **x**: input [`Float32Array`][@stdlib/array/float32]. |
| 52 | +- **strideX**: stride length for `x`. |
| 53 | +- **mask**: mask [`Uint8Array`][@stdlib/array/uint8]. If a `mask` array element is `0`, the corresponding element in `x` is considered unmasked and will participate in the reversal. If a `mask` array element is non-zero, the corresponding element in `x` is considered masked and the element will not participate in the reversal. |
| 54 | +- **strideMask**: stride length for `mask`. |
| 55 | + |
| 56 | +The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to reverse every other element: |
| 57 | + |
| 58 | +```javascript |
| 59 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 60 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 61 | + |
| 62 | +var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0 ] ); |
| 63 | +var mask = new Uint8Array( [ 0, 0, 0, 0, 0, 0 ] ); |
| 64 | + |
| 65 | +smskrev( 3, x, 2, mask, 2 ); |
| 66 | +// x => <Float32Array>[ 4.0, 1.0, 3.0, -5.0, -2.0, 0.0 ] |
| 67 | +``` |
| 68 | + |
| 69 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 70 | + |
| 71 | +```javascript |
| 72 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 73 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 74 | + |
| 75 | +// Initial arrays... |
| 76 | +var x0 = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); |
| 77 | +var m0 = new Uint8Array( [ 0, 0, 0, 0, 0, 0 ] ); |
| 78 | + |
| 79 | +// Create offset views... |
| 80 | +var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 81 | +var m1 = new Uint8Array( m0.buffer, m0.BYTES_PER_ELEMENT*1 ); |
| 82 | + |
| 83 | +// Reverse every other element... |
| 84 | +smskrev( 3, x1, 2, m1, 2 ); |
| 85 | +// x0 => <Float32Array>[ 1.0, -6.0, 3.0, -4.0, 5.0, -2.0 ] |
| 86 | +``` |
| 87 | + |
| 88 | +#### smskrev.ndarray( N, x, strideX, offsetX, mask, strideMask, offsetMask ) |
| 89 | + |
| 90 | +Reverses a single-precision floating-point strided array `x` in-place according to a `mask` and using alternative indexing semantics. |
| 91 | + |
| 92 | +```javascript |
| 93 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 94 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 95 | + |
| 96 | +var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] ); |
| 97 | +var mask = new Uint8Array( [ 0, 0, 0, 1, 0, 0, 0, 0 ] ); |
| 98 | + |
| 99 | +smskrev.ndarray( x.length, x, 1, 0, mask, 1, 0 ); |
| 100 | +// x => <Float32Array>[ -3.0, -1.0, 0.0, -5.0, 4.0, 3.0, 1.0, -2.0 ] |
| 101 | +``` |
| 102 | + |
| 103 | +The function has the following additional parameters: |
| 104 | + |
| 105 | +- **offsetX**: starting index for `x`. |
| 106 | +- **offsetMask**: starting index for `mask`. |
| 107 | + |
| 108 | +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to access only the last three elements of the strided array: |
| 109 | + |
| 110 | +```javascript |
| 111 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 112 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 113 | + |
| 114 | +var x = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); |
| 115 | +var mask = new Uint8Array( [ 0, 0, 0, 0, 0, 0 ] ); |
| 116 | + |
| 117 | +smskrev.ndarray( 3, x, 1, x.length-3, mask, 1, mask.length-3 ); |
| 118 | +// x => <Float32Array>[ 1.0, -2.0, 3.0, -6.0, 5.0, -4.0 ] |
| 119 | +``` |
| 120 | + |
| 121 | +</section> |
| 122 | + |
| 123 | +<!-- /.usage --> |
| 124 | + |
| 125 | +<section class="notes"> |
| 126 | + |
| 127 | +## Notes |
| 128 | + |
| 129 | +- If `N <= 0`, both functions return the strided array unchanged. |
| 130 | +- A swap is only performed when **both** elements in a pair are unmasked (i.e., their corresponding mask values are `0`). If either element is masked, neither element is modified. |
| 131 | +- Where possible, one should "reverse" a strided array by negating its stride, which is an `O(1)` operation, in contrast to performing an in-place reversal, which is `O(N)`. However, in certain circumstances, this is not tenable, particularly when interfacing with libraries which assume and/or expect a specific memory layout (e.g., strided array elements arranged in memory in ascending order). In general, when working with strided arrays, only perform an in-place reversal when strictly necessary. |
| 132 | + |
| 133 | +</section> |
| 134 | + |
| 135 | +<!-- /.notes --> |
| 136 | + |
| 137 | +<section class="examples"> |
| 138 | + |
| 139 | +## Examples |
| 140 | + |
| 141 | +<!-- eslint no-undef: "error" --> |
| 142 | + |
| 143 | +```javascript |
| 144 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 145 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 146 | +var smskrev = require( '@stdlib/blas/ext/base/smskrev' ); |
| 147 | + |
| 148 | +var x = discreteUniform( 10, -100, 100, { |
| 149 | + 'dtype': 'float32' |
| 150 | +}); |
| 151 | +console.log( x ); |
| 152 | + |
| 153 | +var mask = new Uint8Array( [ 0, 0, 0, 1, 0, 0, 1, 0, 0, 0 ] ); |
| 154 | +console.log( mask ); |
| 155 | + |
| 156 | +smskrev( x.length, x, 1, mask, 1 ); |
| 157 | +console.log( x ); |
| 158 | +``` |
| 159 | + |
| 160 | +</section> |
| 161 | + |
| 162 | +<!-- /.examples --> |
| 163 | + |
| 164 | +<!-- C interface documentation. --> |
| 165 | + |
| 166 | +* * * |
| 167 | + |
| 168 | +<section class="c"> |
| 169 | + |
| 170 | +## C APIs |
| 171 | + |
| 172 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 173 | + |
| 174 | +<section class="intro"> |
| 175 | + |
| 176 | +</section> |
| 177 | + |
| 178 | +<!-- /.intro --> |
| 179 | + |
| 180 | +<!-- C usage documentation. --> |
| 181 | + |
| 182 | +<section class="usage"> |
| 183 | + |
| 184 | +### Usage |
| 185 | + |
| 186 | +```c |
| 187 | +#include "stdlib/blas/ext/base/smskrev.h" |
| 188 | +``` |
| 189 | + |
| 190 | +#### stdlib_strided_smskrev( N, \*X, strideX, \*Mask, strideMask ) |
| 191 | + |
| 192 | +Reverses a single-precision floating-point strided array `X` in-place according to a `Mask`. |
| 193 | + |
| 194 | +```c |
| 195 | +float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }; |
| 196 | +const uint8_t mask[] = { 0, 0, 0, 0 }; |
| 197 | + |
| 198 | +stdlib_strided_smskrev( 4, x, 1, mask, 1 ); |
| 199 | +``` |
| 200 | +
|
| 201 | +The function accepts the following arguments: |
| 202 | +
|
| 203 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 204 | +- **X**: `[inout] float*` input array. |
| 205 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 206 | +- **Mask**: `[in] uint8_t*` mask array. |
| 207 | +- **strideMask**: `[in] CBLAS_INT` stride length for `Mask`. |
| 208 | +
|
| 209 | +```c |
| 210 | +void stdlib_strided_smskrev( const CBLAS_INT N, float *X, const CBLAS_INT strideX, const uint8_t *Mask, const CBLAS_INT strideMask ); |
| 211 | +``` |
| 212 | + |
| 213 | +<!-- lint disable maximum-heading-length --> |
| 214 | + |
| 215 | +#### stdlib_strided_smskrev_ndarray( N, \*X, strideX, offsetX, \*Mask, strideMask, offsetMask ) |
| 216 | + |
| 217 | +<!-- lint enable maximum-heading-length --> |
| 218 | + |
| 219 | +Reverses a single-precision floating-point strided array `X` in-place according to a `Mask` and using alternative indexing semantics. |
| 220 | + |
| 221 | +```c |
| 222 | +float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }; |
| 223 | +const uint8_t mask[] = { 0, 0, 0, 0 }; |
| 224 | + |
| 225 | +stdlib_strided_smskrev_ndarray( 4, x, 1, 0, mask, 1, 0 ); |
| 226 | +``` |
| 227 | +
|
| 228 | +The function accepts the following arguments: |
| 229 | +
|
| 230 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 231 | +- **X**: `[inout] float*` input array. |
| 232 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 233 | +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. |
| 234 | +- **Mask**: `[in] uint8_t*` mask array. |
| 235 | +- **strideMask**: `[in] CBLAS_INT` stride length for `Mask`. |
| 236 | +- **offsetMask**: `[in] CBLAS_INT` starting index for `Mask`. |
| 237 | +
|
| 238 | +```c |
| 239 | +void stdlib_strided_smskrev_ndarray( const CBLAS_INT N, float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const uint8_t *Mask, const CBLAS_INT strideMask, const CBLAS_INT offsetMask ); |
| 240 | +``` |
| 241 | + |
| 242 | +</section> |
| 243 | + |
| 244 | +<!-- /.usage --> |
| 245 | + |
| 246 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 247 | + |
| 248 | +<section class="notes"> |
| 249 | + |
| 250 | +</section> |
| 251 | + |
| 252 | +<!-- /.notes --> |
| 253 | + |
| 254 | +<!-- C API usage examples. --> |
| 255 | + |
| 256 | +<section class="examples"> |
| 257 | + |
| 258 | +### Examples |
| 259 | + |
| 260 | +```c |
| 261 | +#include "stdlib/blas/ext/base/smskrev.h" |
| 262 | +#include <stdint.h> |
| 263 | +#include <stdio.h> |
| 264 | + |
| 265 | +int main( void ) { |
| 266 | + // Create a strided array: |
| 267 | + float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f }; |
| 268 | + |
| 269 | + // Create a mask array: |
| 270 | + const uint8_t mask[] = { 0, 0, 0, 1, 0, 0, 0, 0 }; |
| 271 | + |
| 272 | + // Specify the number of elements: |
| 273 | + const int N = 8; |
| 274 | + |
| 275 | + // Specify strides: |
| 276 | + const int strideX = 1; |
| 277 | + const int strideMask = 1; |
| 278 | + |
| 279 | + // Reverse the array: |
| 280 | + stdlib_strided_smskrev( N, x, strideX, mask, strideMask ); |
| 281 | + |
| 282 | + // Print the result: |
| 283 | + for ( int i = 0; i < 8; i++ ) { |
| 284 | + printf( "x[ %i ] = %f\n", i, x[ i ] ); |
| 285 | + } |
| 286 | +} |
| 287 | +``` |
| 288 | +
|
| 289 | +</section> |
| 290 | +
|
| 291 | +<!-- /.examples --> |
| 292 | +
|
| 293 | +</section> |
| 294 | +
|
| 295 | +<!-- /.c --> |
| 296 | +
|
| 297 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 298 | +
|
| 299 | +<section class="related"> |
| 300 | +
|
| 301 | +</section> |
| 302 | +
|
| 303 | +<!-- /.related --> |
| 304 | +
|
| 305 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 306 | +
|
| 307 | +<section class="links"> |
| 308 | +
|
| 309 | +[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32 |
| 310 | +
|
| 311 | +[@stdlib/array/uint8]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/uint8 |
| 312 | +
|
| 313 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 314 | +
|
| 315 | +<!-- <related-links> --> |
| 316 | +
|
| 317 | +<!-- </related-links> --> |
| 318 | +
|
| 319 | +</section> |
| 320 | +
|
| 321 | +<!-- /.links --> |
0 commit comments