Skip to content

Commit f3c2d1c

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/smskrev
PR-URL: #10924 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#194
1 parent 003cb6e commit f3c2d1c

33 files changed

Lines changed: 4332 additions & 0 deletions
Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,324 @@
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 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 valid and **included** in the reversal. If a `mask` array element is `1`, the corresponding element in `x` is considered invalid/missing and **excluded** from 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, -1.0, -3.0 ] );
63+
var mask = new Uint8Array( [ 0, 0, 0, 0 ] );
64+
65+
smskrev( 4, x, 2, mask, 1 );
66+
// x => <Float32Array>[ -1.0, 1.0, 4.0, -5.0, 3.0, 0.0, -2.0, -3.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, 1 );
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 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:
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, 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 `x` unchanged.
130+
- 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.
131+
132+
</section>
133+
134+
<!-- /.notes -->
135+
136+
<section class="examples">
137+
138+
## Examples
139+
140+
<!-- eslint no-undef: "error" -->
141+
142+
```javascript
143+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
144+
var Uint8Array = require( '@stdlib/array/uint8' );
145+
var smskrev = require( '@stdlib/blas/ext/base/smskrev' );
146+
147+
var x = discreteUniform( 10, -100, 100, {
148+
'dtype': 'float32'
149+
});
150+
console.log( x );
151+
152+
var mask = new Uint8Array( [ 0, 0, 0, 1, 0, 0, 1, 0, 0, 0 ] );
153+
console.log( mask );
154+
155+
smskrev( x.length, x, 1, mask, 1 );
156+
console.log( x );
157+
```
158+
159+
</section>
160+
161+
<!-- /.examples -->
162+
163+
<!-- C interface documentation. -->
164+
165+
* * *
166+
167+
<section class="c">
168+
169+
## C APIs
170+
171+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
172+
173+
<section class="intro">
174+
175+
</section>
176+
177+
<!-- /.intro -->
178+
179+
<!-- C usage documentation. -->
180+
181+
<section class="usage">
182+
183+
### Usage
184+
185+
```c
186+
#include "stdlib/blas/ext/base/smskrev.h"
187+
```
188+
189+
#### stdlib_strided_smskrev( N, \*X, strideX, \*Mask, strideMask )
190+
191+
Reverses a single-precision floating-point strided array in-place according to a mask.
192+
193+
```c
194+
#include <stdint.h>
195+
196+
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
197+
const uint8_t mask[] = { 0, 0, 0, 0 };
198+
199+
stdlib_strided_smskrev( 4, x, 1, mask, 1 );
200+
```
201+
202+
The function accepts the following arguments:
203+
204+
- **N**: `[in] CBLAS_INT` number of indexed elements.
205+
- **X**: `[inout] float*` input array.
206+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
207+
- **Mask**: `[in] uint8_t*` mask array.
208+
- **strideMask**: `[in] CBLAS_INT` stride length for `Mask`.
209+
210+
```c
211+
void stdlib_strided_smskrev( const CBLAS_INT N, float *X, const CBLAS_INT strideX, const uint8_t *Mask, const CBLAS_INT strideMask );
212+
```
213+
214+
<!-- lint disable maximum-heading-length -->
215+
216+
#### stdlib_strided_smskrev_ndarray( N, \*X, strideX, offsetX, \*Mask, strideMask, offsetMask )
217+
218+
<!-- lint enable maximum-heading-length -->
219+
220+
Reverses a single-precision floating-point strided array in-place according to a mask and using alternative indexing semantics.
221+
222+
```c
223+
#include <stdint.h>
224+
225+
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
226+
const uint8_t mask[] = { 0, 0, 0, 0 };
227+
228+
stdlib_strided_smskrev_ndarray( 4, x, 1, 0, mask, 1, 0 );
229+
```
230+
231+
The function accepts the following arguments:
232+
233+
- **N**: `[in] CBLAS_INT` number of indexed elements.
234+
- **X**: `[inout] float*` input array.
235+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
236+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
237+
- **Mask**: `[in] uint8_t*` mask array.
238+
- **strideMask**: `[in] CBLAS_INT` stride length for `Mask`.
239+
- **offsetMask**: `[in] CBLAS_INT` starting index for `Mask`.
240+
241+
```c
242+
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 );
243+
```
244+
245+
</section>
246+
247+
<!-- /.usage -->
248+
249+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
250+
251+
<section class="notes">
252+
253+
</section>
254+
255+
<!-- /.notes -->
256+
257+
<!-- C API usage examples. -->
258+
259+
<section class="examples">
260+
261+
### Examples
262+
263+
```c
264+
#include "stdlib/blas/ext/base/smskrev.h"
265+
#include <stdint.h>
266+
#include <stdio.h>
267+
268+
int main( void ) {
269+
// Create a strided array:
270+
float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f };
271+
272+
// Create a mask array:
273+
const uint8_t mask[] = { 0, 0, 0, 1, 0, 0, 0, 0 };
274+
275+
// Specify the number of elements:
276+
const int N = 8;
277+
278+
// Specify strides:
279+
const int strideX = 1;
280+
const int strideMask = 1;
281+
282+
// Reverse the array:
283+
stdlib_strided_smskrev( N, x, strideX, mask, strideMask );
284+
285+
// Print the result:
286+
for ( int i = 0; i < 8; i++ ) {
287+
printf( "x[ %i ] = %f\n", i, x[ i ] );
288+
}
289+
}
290+
```
291+
292+
</section>
293+
294+
<!-- /.examples -->
295+
296+
</section>
297+
298+
<!-- /.c -->
299+
300+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
301+
302+
<section class="related">
303+
304+
</section>
305+
306+
<!-- /.related -->
307+
308+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
309+
310+
<section class="links">
311+
312+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
313+
314+
[@stdlib/array/uint8]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/uint8
315+
316+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
317+
318+
<!-- <related-links> -->
319+
320+
<!-- </related-links> -->
321+
322+
</section>
323+
324+
<!-- /.links -->

0 commit comments

Comments
 (0)