numr 0.5.2

High-performance numerical computing with multi-backend GPU acceleration (CPU/CUDA/WebGPU)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! SIMD-optimized index operations dispatch
//!
//! Provides AVX2/AVX-512 accelerated masked_fill and masked_select operations.

#[cfg(target_arch = "x86_64")]
mod avx2;
#[cfg(target_arch = "x86_64")]
mod avx512;

#[cfg(target_arch = "aarch64")]
mod aarch64;

use super::{SimdLevel, detect_simd};
use crate::dtype::Element;

/// Minimum elements to justify SIMD overhead
const SIMD_THRESHOLD: usize = 32;

// ============================================================================
// Masked Fill - SIMD Dispatch
// ============================================================================

/// SIMD-optimized masked fill for f32.
///
/// Fills output with `value` where mask is true, otherwise copies from input.
///
/// # Safety
/// - All pointers must be valid for `len` elements
pub unsafe fn masked_fill_f32(
    input: *const f32,
    mask: *const u8,
    output: *mut f32,
    len: usize,
    value: f32,
) {
    let level = detect_simd();

    if len < SIMD_THRESHOLD || level == SimdLevel::Scalar {
        masked_fill_scalar_f32(input, mask, output, len, value);
        return;
    }

    #[cfg(target_arch = "x86_64")]
    match level {
        SimdLevel::Avx512 => avx512::masked_fill_f32(input, mask, output, len, value),
        SimdLevel::Avx2Fma => avx2::masked_fill_f32(input, mask, output, len, value),
        _ => masked_fill_scalar_f32(input, mask, output, len, value),
    }

    #[cfg(target_arch = "aarch64")]
    match level {
        SimdLevel::Neon | SimdLevel::NeonFp16 => {
            aarch64::neon::masked_fill_f32(input, mask, output, len, value)
        }
        _ => masked_fill_scalar_f32(input, mask, output, len, value),
    }

    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
    masked_fill_scalar_f32(input, mask, output, len, value);
}

/// SIMD-optimized masked fill for f64.
pub unsafe fn masked_fill_f64(
    input: *const f64,
    mask: *const u8,
    output: *mut f64,
    len: usize,
    value: f64,
) {
    let level = detect_simd();

    if len < SIMD_THRESHOLD || level == SimdLevel::Scalar {
        masked_fill_scalar_f64(input, mask, output, len, value);
        return;
    }

    #[cfg(target_arch = "x86_64")]
    match level {
        SimdLevel::Avx512 => avx512::masked_fill_f64(input, mask, output, len, value),
        SimdLevel::Avx2Fma => avx2::masked_fill_f64(input, mask, output, len, value),
        _ => masked_fill_scalar_f64(input, mask, output, len, value),
    }

    #[cfg(target_arch = "aarch64")]
    match level {
        SimdLevel::Neon | SimdLevel::NeonFp16 => {
            aarch64::neon::masked_fill_f64(input, mask, output, len, value)
        }
        _ => masked_fill_scalar_f64(input, mask, output, len, value),
    }

    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
    masked_fill_scalar_f64(input, mask, output, len, value);
}

// ============================================================================
// Masked Select - SIMD Dispatch
// ============================================================================

/// SIMD-optimized masked select for f32.
///
/// Selects elements where mask is true into a contiguous output.
///
/// # Safety
/// - All pointers must be valid
/// - `output` must have space for at least `len` elements (worst case all selected)
pub unsafe fn masked_select_f32(
    input: *const f32,
    mask: *const u8,
    output: *mut f32,
    len: usize,
) -> usize {
    let level = detect_simd();

    if len < SIMD_THRESHOLD || level == SimdLevel::Scalar {
        return masked_select_scalar_f32(input, mask, output, len);
    }

    #[cfg(target_arch = "x86_64")]
    match level {
        SimdLevel::Avx512 => return avx512::masked_select_f32(input, mask, output, len),
        SimdLevel::Avx2Fma => return avx2::masked_select_f32(input, mask, output, len),
        _ => return masked_select_scalar_f32(input, mask, output, len),
    }

    #[cfg(target_arch = "aarch64")]
    match level {
        SimdLevel::Neon | SimdLevel::NeonFp16 => {
            return aarch64::neon::masked_select_f32(input, mask, output, len);
        }
        _ => return masked_select_scalar_f32(input, mask, output, len),
    }

    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
    masked_select_scalar_f32(input, mask, output, len)
}

/// SIMD-optimized masked select for f64.
pub unsafe fn masked_select_f64(
    input: *const f64,
    mask: *const u8,
    output: *mut f64,
    len: usize,
) -> usize {
    let level = detect_simd();

    if len < SIMD_THRESHOLD || level == SimdLevel::Scalar {
        return masked_select_scalar_f64(input, mask, output, len);
    }

    #[cfg(target_arch = "x86_64")]
    match level {
        SimdLevel::Avx512 => return avx512::masked_select_f64(input, mask, output, len),
        SimdLevel::Avx2Fma => return avx2::masked_select_f64(input, mask, output, len),
        _ => return masked_select_scalar_f64(input, mask, output, len),
    }

    #[cfg(target_arch = "aarch64")]
    match level {
        SimdLevel::Neon | SimdLevel::NeonFp16 => {
            return aarch64::neon::masked_select_f64(input, mask, output, len);
        }
        _ => return masked_select_scalar_f64(input, mask, output, len),
    }

    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
    masked_select_scalar_f64(input, mask, output, len)
}

// ============================================================================
// Masked Count - SIMD Dispatch
// ============================================================================

/// SIMD-optimized mask count (popcount).
///
/// Counts the number of true elements in the mask.
pub unsafe fn masked_count(mask: *const u8, len: usize) -> usize {
    let level = detect_simd();

    if len < SIMD_THRESHOLD || level == SimdLevel::Scalar {
        return masked_count_scalar(mask, len);
    }

    #[cfg(target_arch = "x86_64")]
    match level {
        SimdLevel::Avx512 => return avx512::masked_count(mask, len),
        SimdLevel::Avx2Fma => return avx2::masked_count(mask, len),
        _ => return masked_count_scalar(mask, len),
    }

    #[cfg(target_arch = "aarch64")]
    match level {
        SimdLevel::Neon | SimdLevel::NeonFp16 => return aarch64::neon::masked_count(mask, len),
        _ => return masked_count_scalar(mask, len),
    }

    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
    masked_count_scalar(mask, len)
}

// ============================================================================
// Scalar Fallbacks
// ============================================================================

#[inline]
unsafe fn masked_fill_scalar_f32(
    input: *const f32,
    mask: *const u8,
    output: *mut f32,
    len: usize,
    value: f32,
) {
    for i in 0..len {
        *output.add(i) = if *mask.add(i) != 0 {
            value
        } else {
            *input.add(i)
        };
    }
}

#[inline]
unsafe fn masked_fill_scalar_f64(
    input: *const f64,
    mask: *const u8,
    output: *mut f64,
    len: usize,
    value: f64,
) {
    for i in 0..len {
        *output.add(i) = if *mask.add(i) != 0 {
            value
        } else {
            *input.add(i)
        };
    }
}

#[inline]
unsafe fn masked_select_scalar_f32(
    input: *const f32,
    mask: *const u8,
    output: *mut f32,
    len: usize,
) -> usize {
    let mut out_idx = 0;
    for i in 0..len {
        if *mask.add(i) != 0 {
            *output.add(out_idx) = *input.add(i);
            out_idx += 1;
        }
    }
    out_idx
}

#[inline]
unsafe fn masked_select_scalar_f64(
    input: *const f64,
    mask: *const u8,
    output: *mut f64,
    len: usize,
) -> usize {
    let mut out_idx = 0;
    for i in 0..len {
        if *mask.add(i) != 0 {
            *output.add(out_idx) = *input.add(i);
            out_idx += 1;
        }
    }
    out_idx
}

#[inline]
unsafe fn masked_count_scalar(mask: *const u8, len: usize) -> usize {
    let mut count = 0;
    for i in 0..len {
        if *mask.add(i) != 0 {
            count += 1;
        }
    }
    count
}

// ============================================================================
// Generic Dispatcher
// ============================================================================

/// Generic masked fill for any Element type.
///
/// Uses SIMD for f32/f64, scalar for other types.
#[allow(dead_code)]
pub unsafe fn masked_fill<T: Element>(
    input: *const T,
    mask: *const u8,
    output: *mut T,
    len: usize,
    value: f64,
) {
    // For f32/f64, use SIMD paths
    if std::any::TypeId::of::<T>() == std::any::TypeId::of::<f32>() {
        masked_fill_f32(
            input as *const f32,
            mask,
            output as *mut f32,
            len,
            value as f32,
        );
    } else if std::any::TypeId::of::<T>() == std::any::TypeId::of::<f64>() {
        masked_fill_f64(input as *const f64, mask, output as *mut f64, len, value);
    } else {
        // Scalar fallback for other types
        let fill_val = T::from_f64(value);
        for i in 0..len {
            *output.add(i) = if *mask.add(i) != 0 {
                fill_val
            } else {
                *input.add(i)
            };
        }
    }
}

/// Generic masked select for any Element type.
#[allow(dead_code)]
pub unsafe fn masked_select<T: Element>(
    input: *const T,
    mask: *const u8,
    output: *mut T,
    len: usize,
) -> usize {
    // For f32/f64, use SIMD paths
    if std::any::TypeId::of::<T>() == std::any::TypeId::of::<f32>() {
        masked_select_f32(input as *const f32, mask, output as *mut f32, len)
    } else if std::any::TypeId::of::<T>() == std::any::TypeId::of::<f64>() {
        masked_select_f64(input as *const f64, mask, output as *mut f64, len)
    } else {
        // Scalar fallback for other types
        let mut out_idx = 0;
        for i in 0..len {
            if *mask.add(i) != 0 {
                *output.add(out_idx) = *input.add(i);
                out_idx += 1;
            }
        }
        out_idx
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_masked_fill_f32() {
        let input: Vec<f32> = (0..100).map(|i| i as f32).collect();
        let mask: Vec<u8> = (0..100).map(|i| if i % 3 == 0 { 1 } else { 0 }).collect();
        let mut output = vec![0.0f32; 100];
        let fill_value = -999.0f32;

        unsafe {
            masked_fill_f32(
                input.as_ptr(),
                mask.as_ptr(),
                output.as_mut_ptr(),
                100,
                fill_value,
            );
        }

        for i in 0..100 {
            let expected = if i % 3 == 0 { fill_value } else { i as f32 };
            assert_eq!(output[i], expected, "mismatch at index {}", i);
        }
    }

    #[test]
    fn test_masked_select_f32() {
        let input: Vec<f32> = (0..100).map(|i| i as f32).collect();
        let mask: Vec<u8> = (0..100).map(|i| if i % 5 == 0 { 1 } else { 0 }).collect();
        let mut output = vec![0.0f32; 100];

        let count =
            unsafe { masked_select_f32(input.as_ptr(), mask.as_ptr(), output.as_mut_ptr(), 100) };

        assert_eq!(count, 20); // 0, 5, 10, 15, ..., 95 = 20 elements

        for (j, i) in (0..100).filter(|i| i % 5 == 0).enumerate() {
            assert_eq!(output[j], i as f32, "mismatch at output index {}", j);
        }
    }

    #[test]
    fn test_masked_count() {
        let mask: Vec<u8> = (0..256).map(|i| if i % 4 == 0 { 1 } else { 0 }).collect();

        let count = unsafe { masked_count(mask.as_ptr(), 256) };

        assert_eq!(count, 64); // 256 / 4 = 64
    }
}