thermite 0.2.1

High-performance, generic, ISA-portable SIMD library with a policy-configurable transcendental math library
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//! Reduced (sub-native-width) 16-bit registers for x86-v1 and the cast/concat glue bridging
//! the scalar/array 16-bit halves, the native 128-bit `I16x8V1`/`U16x8V1`, and the 32-bit
//! registers they widen into. Mirrors the v3 `half16.rs`, but widen casts use SSE2 `unpack`
//! sign/zero extension (no SSE4.1 `cvtep*`) and narrows fall back to scalar.

use generic_array::typenum::U4;

use super::arch;

use crate::register::{
    CastRegister, ConcatRegister, ExtendRegister, IndexableRegister, NumericRegister, Register, Storage,
    array::ArrayRegister, reduced::ReducedRegister,
};

#[thermite_macros::inline_always]
impl CastRegister<super::I32x4V1> for I16x4V1 {
    // Saturating narrow i32x4 -> i16x4 via SSE2 `packssdw`.
    fn saturating_cast_from(value: Storage<super::I32x4V1>) -> Storage<Self> {
        ReducedRegister::new(unsafe { arch::_mm_packs_epi32(value, value) })
    }

    fn cast_from(value: Storage<super::I32x4V1>) -> Storage<Self> {
        let mut a = [0i32; 4];
        unsafe { arch::_mm_storeu_si128(a.as_mut_ptr() as *mut _, value) };
        ReducedRegister::new(unsafe {
            arch::_mm_setr_epi16(a[0] as i16, a[1] as i16, a[2] as i16, a[3] as i16, 0, 0, 0, 0)
        })
    }
}

// SSE2 has no `packusdw` and no unsigned 32-bit min, so every `u32 -> *` and the `i64`/x2
// pairs clamp into range (the register's own `min`/`max` use the v1 `*_epuNNx_v1` polyfills)
// and reuse the truncating narrow. Idempotent across nested ranges.
macro_rules! sat_clamp_narrow16 {
    ($from:ty, $fe:ty, $ie:ty) => {
        #[inline(always)]
        fn saturating_cast_from(value: Storage<$from>) -> Storage<Self> {
            let lo = <$from as Register>::splat(<$ie>::MIN as $fe);
            let hi = <$from as Register>::splat(<$ie>::MAX as $fe);
            let clamped = <$from as NumericRegister>::min(<$from as NumericRegister>::max(value, lo), hi);
            <Self as CastRegister<$from>>::cast_from(clamped)
        }
    };
}

/// 4-lane signed 16-bit register, backed by the low 4 lanes of a 128-bit `I16x8V1`.
pub type I16x4V1 = ReducedRegister<super::I16x8V1, U4>;
/// 4-lane unsigned 16-bit register, backed by the low 4 lanes of a 128-bit `U16x8V1`.
pub type U16x4V1 = ReducedRegister<super::U16x8V1, U4>;

// --- x4 <- x2 (two scalar-array halves into a 4-lane reduced register) ---

#[thermite_macros::inline_always]
impl ConcatRegister<ArrayRegister<i16, 2>> for I16x4V1 {
    fn concat(lo: Storage<ArrayRegister<i16, 2>>, hi: Storage<ArrayRegister<i16, 2>>) -> Storage<Self> {
        ReducedRegister::new(unsafe { arch::_mm_setr_epi16(lo.0[0], lo.0[1], hi.0[0], hi.0[1], 0, 0, 0, 0) })
    }

    fn split(value: Storage<Self>) -> (Storage<ArrayRegister<i16, 2>>, Storage<ArrayRegister<i16, 2>>) {
        let mut arr = [0i16; 8];
        unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, value.0) };
        (ArrayRegister([arr[0], arr[1]]), ArrayRegister([arr[2], arr[3]]))
    }
}

#[thermite_macros::inline_always]
impl ExtendRegister<ArrayRegister<i16, 2>> for I16x4V1 {
    fn extend(value: Storage<ArrayRegister<i16, 2>>) -> Storage<Self> {
        ReducedRegister::new(unsafe { arch::_mm_setr_epi16(value.0[0], value.0[1], 0, 0, 0, 0, 0, 0) })
    }

    fn narrow(value: Storage<Self>) -> Storage<ArrayRegister<i16, 2>> {
        let mut arr = [0i16; 8];
        unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, value.0) };
        ArrayRegister([arr[0], arr[1]])
    }
}

#[thermite_macros::inline_always]
impl ConcatRegister<ArrayRegister<u16, 2>> for U16x4V1 {
    fn concat(lo: Storage<ArrayRegister<u16, 2>>, hi: Storage<ArrayRegister<u16, 2>>) -> Storage<Self> {
        ReducedRegister::new(unsafe {
            arch::_mm_setr_epi16(
                lo.0[0] as i16,
                lo.0[1] as i16,
                hi.0[0] as i16,
                hi.0[1] as i16,
                0,
                0,
                0,
                0,
            )
        })
    }

    fn split(value: Storage<Self>) -> (Storage<ArrayRegister<u16, 2>>, Storage<ArrayRegister<u16, 2>>) {
        let mut arr = [0u16; 8];
        unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, value.0) };
        (ArrayRegister([arr[0], arr[1]]), ArrayRegister([arr[2], arr[3]]))
    }
}

#[thermite_macros::inline_always]
impl ExtendRegister<ArrayRegister<u16, 2>> for U16x4V1 {
    fn extend(value: Storage<ArrayRegister<u16, 2>>) -> Storage<Self> {
        ReducedRegister::new(unsafe { arch::_mm_setr_epi16(value.0[0] as i16, value.0[1] as i16, 0, 0, 0, 0, 0, 0) })
    }

    fn narrow(value: Storage<Self>) -> Storage<ArrayRegister<u16, 2>> {
        let mut arr = [0u16; 8];
        unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, value.0) };
        ArrayRegister([arr[0], arr[1]])
    }
}

// --- x8 <- x4 (two 4-lane reduced halves into the native 8-lane register) ---

#[thermite_macros::inline_always]
impl ConcatRegister<I16x4V1> for super::I16x8V1 {
    fn concat(lo: Storage<I16x4V1>, hi: Storage<I16x4V1>) -> Storage<Self> {
        unsafe { arch::_mm_unpacklo_epi64(lo.0, hi.0) }
    }

    fn split(value: Storage<Self>) -> (Storage<I16x4V1>, Storage<I16x4V1>) {
        (
            ReducedRegister::new(value),
            ReducedRegister::new(unsafe { arch::_mm_unpackhi_epi64(value, value) }),
        )
    }
}

// `ExtendRegister<I16x4V1> for I16x8V1` is provided by the ReducedRegister blanket.

#[thermite_macros::inline_always]
impl ConcatRegister<U16x4V1> for super::U16x8V1 {
    fn concat(lo: Storage<U16x4V1>, hi: Storage<U16x4V1>) -> Storage<Self> {
        unsafe { arch::_mm_unpacklo_epi64(lo.0, hi.0) }
    }

    fn split(value: Storage<Self>) -> (Storage<U16x4V1>, Storage<U16x4V1>) {
        (
            ReducedRegister::new(value),
            ReducedRegister::new(unsafe { arch::_mm_unpackhi_epi64(value, value) }),
        )
    }
}

// --- mask-side concat (the reduced register is its own Mask; concat from bool-array half) ---

#[inline(always)]
fn bool_to_i16_mask(b: bool) -> i16 {
    if b { !0 } else { 0 }
}

macro_rules! impl_bool_concat {
    ($ty:ty) => {
        #[thermite_macros::inline_always]
        impl ConcatRegister<ArrayRegister<bool, 2>> for $ty {
            fn concat(lo: Storage<ArrayRegister<bool, 2>>, hi: Storage<ArrayRegister<bool, 2>>) -> Storage<Self> {
                ReducedRegister::new(unsafe {
                    arch::_mm_setr_epi16(
                        bool_to_i16_mask(lo.0[0]),
                        bool_to_i16_mask(lo.0[1]),
                        bool_to_i16_mask(hi.0[0]),
                        bool_to_i16_mask(hi.0[1]),
                        0,
                        0,
                        0,
                        0,
                    )
                })
            }

            fn split(value: Storage<Self>) -> (Storage<ArrayRegister<bool, 2>>, Storage<ArrayRegister<bool, 2>>) {
                let mut arr = [0i16; 8];
                unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, value.0) };
                (
                    ArrayRegister([arr[0] != 0, arr[1] != 0]),
                    ArrayRegister([arr[2] != 0, arr[3] != 0]),
                )
            }
        }

        #[thermite_macros::inline_always]
        impl ExtendRegister<ArrayRegister<bool, 2>> for $ty {
            fn extend(value: Storage<ArrayRegister<bool, 2>>) -> Storage<Self> {
                ReducedRegister::new(unsafe {
                    arch::_mm_setr_epi16(
                        bool_to_i16_mask(value.0[0]),
                        bool_to_i16_mask(value.0[1]),
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                    )
                })
            }

            fn narrow(value: Storage<Self>) -> Storage<ArrayRegister<bool, 2>> {
                let mut arr = [0i16; 8];
                unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, value.0) };
                ArrayRegister([arr[0] != 0, arr[1] != 0])
            }
        }
    };
}

impl_bool_concat!(I16x4V1);
impl_bool_concat!(U16x4V1);

// --- widen casts to 32-bit (SSE2 unpack sign/zero-extend; narrows are scalar) ---

#[thermite_macros::inline_always]
impl CastRegister<I16x4V1> for super::I32x4V1 {
    fn cast_from(value: Storage<I16x4V1>) -> Storage<Self> {
        // sign-extend the low 4 i16 lanes into 4 i32 lanes
        unsafe { arch::_mm_unpacklo_epi16(value.0, arch::_mm_srai_epi16(value.0, 15)) }
    }
}

#[thermite_macros::inline_always]
impl CastRegister<U16x4V1> for super::U32x4V1 {
    fn cast_from(value: Storage<U16x4V1>) -> Storage<Self> {
        unsafe { arch::_mm_unpacklo_epi16(value.0, arch::_mm_setzero_si128()) }
    }
}

#[thermite_macros::inline_always]
impl CastRegister<super::U32x4V1> for U16x4V1 {
    fn cast_from(value: Storage<super::U32x4V1>) -> Storage<Self> {
        let mut a = [0u32; 4];
        unsafe { arch::_mm_storeu_si128(a.as_mut_ptr() as *mut _, value) };
        ReducedRegister::new(unsafe {
            arch::_mm_setr_epi16(a[0] as i16, a[1] as i16, a[2] as i16, a[3] as i16, 0, 0, 0, 0)
        })
    }

    sat_clamp_narrow16!(super::U32x4V1, u32, u16);
}

#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<i16, 2>> for super::half::I32x2V1 {
    fn cast_from(value: Storage<ArrayRegister<i16, 2>>) -> Storage<Self> {
        ReducedRegister::new(unsafe { arch::_mm_setr_epi32(value.0[0] as i32, value.0[1] as i32, 0, 0) })
    }
}

#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<u16, 2>> for super::half::U32x2V1 {
    fn cast_from(value: Storage<ArrayRegister<u16, 2>>) -> Storage<Self> {
        ReducedRegister::new(unsafe { arch::_mm_setr_epi32(value.0[0] as i32, value.0[1] as i32, 0, 0) })
    }
}

#[thermite_macros::inline_always]
impl CastRegister<super::half::I32x2V1> for ArrayRegister<i16, 2> {
    fn cast_from(value: Storage<super::half::I32x2V1>) -> Storage<Self> {
        let mut arr = [0i32; 4];
        unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, value.0) };
        ArrayRegister([arr[0] as i16, arr[1] as i16])
    }

    sat_clamp_narrow16!(super::half::I32x2V1, i32, i16);
}

#[thermite_macros::inline_always]
impl CastRegister<super::half::U32x2V1> for ArrayRegister<u16, 2> {
    fn cast_from(value: Storage<super::half::U32x2V1>) -> Storage<Self> {
        let mut arr = [0u32; 4];
        unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, value.0) };
        ArrayRegister([arr[0] as u16, arr[1] as u16])
    }

    sat_clamp_narrow16!(super::half::U32x2V1, u32, u16);
}

// ===========================================================================================
// Widen/narrow casts to 64-bit (SSE2: store-and-rebuild widen/narrow; no pmovsx/pshufb).
//   widen 16 -> 64 via store-then-`_mm_set_epi64x` (sign/zero-extend each word to i64).
//   narrow 64 -> 16 via store-then-`_mm_setr_epi16` (low word of each i64, wrapping like `as`).
//   x2:  ArrayRegister<i16,2> <-> I64x2V1 (native 2-lane).
//   x4:  I16x4V1 <-> ArrayRegister<I64x2V1, 2> (the v1 i64x4).
//   x8:  I16x8V1 <-> ArrayRegister<I64x2V1, 4> (the v1 i64x8).
//   x16: ArrayRegister<I16x8V1, 2> <-> ArrayRegister<I64x2V1, 8> (i16x16 / i64x16).
// ===========================================================================================

#[inline(always)]
fn store_words(v: arch::__m128i) -> [i16; 8] {
    let mut arr = [0i16; 8];
    unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, v) };
    arr
}

#[inline(always)]
fn store_qwords(v: arch::__m128i) -> [i64; 2] {
    let mut arr = [0i64; 2];
    unsafe { arch::_mm_storeu_si128(arr.as_mut_ptr() as *mut _, v) };
    arr
}

// --- x2 widen i16 -> i64 (ArrayRegister<i16,2> -> I64x2V1 native) ---
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<i16, 2>> for super::I64x2V1 {
    fn cast_from(value: Storage<ArrayRegister<i16, 2>>) -> Storage<Self> {
        unsafe { arch::_mm_cvt2epi16_epi64x_v1(value.0[0], value.0[1]) }
    }
}
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<u16, 2>> for super::U64x2V1 {
    fn cast_from(value: Storage<ArrayRegister<u16, 2>>) -> Storage<Self> {
        unsafe { arch::_mm_cvt2epu16_epi64x_v1(value.0[0], value.0[1]) }
    }
}

// --- x2 narrow i64 -> i16 (I64x2V1 native -> ArrayRegister<i16,2>) ---
#[thermite_macros::inline_always]
impl CastRegister<super::I64x2V1> for ArrayRegister<i16, 2> {
    fn cast_from(value: Storage<super::I64x2V1>) -> Storage<Self> {
        let a = store_qwords(value);
        ArrayRegister([a[0] as i16, a[1] as i16])
    }

    sat_clamp_narrow16!(super::I64x2V1, i64, i16);
}
#[thermite_macros::inline_always]
impl CastRegister<super::U64x2V1> for ArrayRegister<u16, 2> {
    fn cast_from(value: Storage<super::U64x2V1>) -> Storage<Self> {
        let a = store_qwords(value);
        ArrayRegister([a[0] as u16, a[1] as u16])
    }

    sat_clamp_narrow16!(super::U64x2V1, u64, u16);
}

// --- x4 widen i16 -> i64 (low 4 words -> two 2x i64 lanes) ---
#[thermite_macros::inline_always]
impl CastRegister<I16x4V1> for ArrayRegister<super::I64x2V1, 2> {
    fn cast_from(value: Storage<I16x4V1>) -> Storage<Self> {
        let a = store_words(value.0);
        ArrayRegister(unsafe {
            [
                arch::_mm_cvt2epi16_epi64x_v1(a[0], a[1]),
                arch::_mm_cvt2epi16_epi64x_v1(a[2], a[3]),
            ]
        })
    }
}
#[thermite_macros::inline_always]
impl CastRegister<U16x4V1> for ArrayRegister<super::U64x2V1, 2> {
    fn cast_from(value: Storage<U16x4V1>) -> Storage<Self> {
        let a = store_words(value.0);
        ArrayRegister(unsafe {
            [
                arch::_mm_cvt2epu16_epi64x_v1(a[0] as u16, a[1] as u16),
                arch::_mm_cvt2epu16_epi64x_v1(a[2] as u16, a[3] as u16),
            ]
        })
    }
}

// --- x4 narrow i64 -> i16 (word 0 of each of 4 lanes -> low 4 words) ---
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<super::I64x2V1, 2>> for I16x4V1 {
    fn cast_from(value: Storage<ArrayRegister<super::I64x2V1, 2>>) -> Storage<Self> {
        let lo = store_qwords(value.0[0]);
        let hi = store_qwords(value.0[1]);
        ReducedRegister::new(unsafe {
            arch::_mm_setr_epi16(lo[0] as i16, lo[1] as i16, hi[0] as i16, hi[1] as i16, 0, 0, 0, 0)
        })
    }

    sat_clamp_narrow16!(ArrayRegister<super::I64x2V1, 2>, i64, i16);
}
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<super::U64x2V1, 2>> for U16x4V1 {
    fn cast_from(value: Storage<ArrayRegister<super::U64x2V1, 2>>) -> Storage<Self> {
        let lo = store_qwords(value.0[0]);
        let hi = store_qwords(value.0[1]);
        ReducedRegister::new(unsafe {
            arch::_mm_setr_epi16(lo[0] as i16, lo[1] as i16, hi[0] as i16, hi[1] as i16, 0, 0, 0, 0)
        })
    }

    sat_clamp_narrow16!(ArrayRegister<super::U64x2V1, 2>, u64, u16);
}

// --- x8 widen i16 -> i64 (8 words -> four 2x i64 lanes) ---
#[thermite_macros::inline_always]
impl CastRegister<super::I16x8V1> for ArrayRegister<super::I64x2V1, 4> {
    fn cast_from(value: Storage<super::I16x8V1>) -> Storage<Self> {
        unsafe { ArrayRegister(arch::_mm_cvtepi16_4epi64x_v1(value)) }
    }
}
#[thermite_macros::inline_always]
impl CastRegister<super::U16x8V1> for ArrayRegister<super::U64x2V1, 4> {
    fn cast_from(value: Storage<super::U16x8V1>) -> Storage<Self> {
        unsafe { ArrayRegister(arch::_mm_cvtepu16_4epi64x_v1(value)) }
    }
}

#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<super::U64x2V1, 4>> for super::U16x8V1 {
    fn cast_from(value: Storage<ArrayRegister<super::U64x2V1, 4>>) -> Storage<Self> {
        unsafe { arch::_mm_cvt4epi64_epi16x_v1(value.0) }
    }

    sat_clamp_narrow16!(ArrayRegister<super::U64x2V1, 4>, u64, u16);
}

// --- x16 widen i16 -> i64 (ArrayRegister<I16x8V1, 2> -> ArrayRegister<I64x2V1, 8>) ---
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<super::I16x8V1, 2>> for ArrayRegister<super::I64x2V1, 8> {
    fn cast_from(value: Storage<ArrayRegister<super::I16x8V1, 2>>) -> Storage<Self> {
        unsafe {
            let lo = arch::_mm_cvtepi16_4epi64x_v1(value.0[0]);
            let hi = arch::_mm_cvtepi16_4epi64x_v1(value.0[1]);
            ArrayRegister([lo[0], lo[1], lo[2], lo[3], hi[0], hi[1], hi[2], hi[3]])
        }
    }
}
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<super::U16x8V1, 2>> for ArrayRegister<super::U64x2V1, 8> {
    fn cast_from(value: Storage<ArrayRegister<super::U16x8V1, 2>>) -> Storage<Self> {
        unsafe {
            let lo = arch::_mm_cvtepu16_4epi64x_v1(value.0[0]);
            let hi = arch::_mm_cvtepu16_4epi64x_v1(value.0[1]);
            ArrayRegister([lo[0], lo[1], lo[2], lo[3], hi[0], hi[1], hi[2], hi[3]])
        }
    }
}

// --- x16 narrow i64 -> i16 (ArrayRegister<I64x2V1, 8> -> ArrayRegister<I16x8V1, 2>) ---
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<super::I64x2V1, 8>> for ArrayRegister<super::I16x8V1, 2> {
    fn cast_from(value: Storage<ArrayRegister<super::I64x2V1, 8>>) -> Storage<Self> {
        let v = value.0;
        unsafe {
            ArrayRegister([
                arch::_mm_cvt4epi64_epi16x_v1([v[0], v[1], v[2], v[3]]),
                arch::_mm_cvt4epi64_epi16x_v1([v[4], v[5], v[6], v[7]]),
            ])
        }
    }

    sat_clamp_narrow16!(ArrayRegister<super::I64x2V1, 8>, i64, i16);
}
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<super::U64x2V1, 8>> for ArrayRegister<super::U16x8V1, 2> {
    fn cast_from(value: Storage<ArrayRegister<super::U64x2V1, 8>>) -> Storage<Self> {
        let v = value.0;
        unsafe {
            ArrayRegister([
                arch::_mm_cvt4epi64_epi16x_v1([v[0], v[1], v[2], v[3]]),
                arch::_mm_cvt4epi64_epi16x_v1([v[4], v[5], v[6], v[7]]),
            ])
        }
    }

    sat_clamp_narrow16!(ArrayRegister<super::U64x2V1, 8>, u64, u16);
}

// ===========================================================================================
// 16 <-> f32/f64 direct casts (SSE2: no pmovsx/pshufb; widen via unpack to i32 then native
// i32<->float converts; narrow via truncating float->i32 then the existing store-rebuild word
// narrows).
//   WIDEN  i16/u16 -> f32 = widen low 4 words to i32x4 then `_mm_cvtepi32_ps` (exact).
//   NARROW f32 -> i16/u16 = `_mm_cvttps_epi32` (truncate) then store-rebuild low word per lane.
//   WIDEN  i16/u16 -> f64 = widen to i32 then `_mm_cvtepi32_pd` (2 lanes per F64x2V1, fan out).
//   NARROW f64 -> i16/u16 = `_mm_cvttpd_epi32` (truncate, low 2 lanes) then store-rebuild words.
// Unsigned 16-bit values fit in positive i32, so the signed i32->float convert is exact for them.
// ===========================================================================================

// --- x2 (ArrayRegister<i16,2> <-> ReducedRegister<F32x4V1,2> = F32x2V1) ---
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<i16, 2>> for super::half::F32x2V1 {
    fn cast_from(value: Storage<ArrayRegister<i16, 2>>) -> Storage<Self> {
        let ints = unsafe { arch::_mm_setr_epi32(value.0[0] as i32, value.0[1] as i32, 0, 0) };
        ReducedRegister::new(unsafe { arch::_mm_cvtepi32_ps(ints) })
    }
}
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<u16, 2>> for super::half::F32x2V1 {
    fn cast_from(value: Storage<ArrayRegister<u16, 2>>) -> Storage<Self> {
        let ints = unsafe { arch::_mm_setr_epi32(value.0[0] as i32, value.0[1] as i32, 0, 0) };
        ReducedRegister::new(unsafe { arch::_mm_cvtepi32_ps(ints) })
    }
}

// --- x2 (ArrayRegister<i16,2> <-> F64x2V1 native) ---
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<i16, 2>> for super::F64x2V1 {
    fn cast_from(value: Storage<ArrayRegister<i16, 2>>) -> Storage<Self> {
        let ints = unsafe { arch::_mm_setr_epi32(value.0[0] as i32, value.0[1] as i32, 0, 0) };
        unsafe { arch::_mm_cvtepi32_pd(ints) }
    }
}
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<u16, 2>> for super::F64x2V1 {
    fn cast_from(value: Storage<ArrayRegister<u16, 2>>) -> Storage<Self> {
        let ints = unsafe { arch::_mm_setr_epi32(value.0[0] as i32, value.0[1] as i32, 0, 0) };
        unsafe { arch::_mm_cvtepi32_pd(ints) }
    }
}

// --- x4 (I16x4V1 <-> F32x4V1 native) ---
#[thermite_macros::inline_always]
impl CastRegister<I16x4V1> for super::F32x4V1 {
    fn cast_from(value: Storage<I16x4V1>) -> Storage<Self> {
        unsafe { arch::_mm_cvtepi32_ps(arch::_mm_cvtepi16_epi32x_v1(value.0)) }
    }
}
#[thermite_macros::inline_always]
impl CastRegister<U16x4V1> for super::F32x4V1 {
    fn cast_from(value: Storage<U16x4V1>) -> Storage<Self> {
        unsafe { arch::_mm_cvtepi32_ps(arch::_mm_cvtepu16_epi32x_v1(value.0)) }
    }
}

// --- x4 (I16x4V1 <-> ArrayRegister<F64x2V1, 2>) ---
#[thermite_macros::inline_always]
impl CastRegister<I16x4V1> for ArrayRegister<super::F64x2V1, 2> {
    fn cast_from(value: Storage<I16x4V1>) -> Storage<Self> {
        unsafe {
            let ints = arch::_mm_cvtepi16_epi32x_v1(value.0);
            ArrayRegister(arch::_mm_cvtepi32_2pdx_v1(ints))
        }
    }
}
#[thermite_macros::inline_always]
impl CastRegister<U16x4V1> for ArrayRegister<super::F64x2V1, 2> {
    fn cast_from(value: Storage<U16x4V1>) -> Storage<Self> {
        unsafe {
            let ints = arch::_mm_cvtepu16_epi32x_v1(value.0);
            ArrayRegister(arch::_mm_cvtepi32_2pdx_v1(ints))
        }
    }
}

// --- x8 (I16x8V1 native <-> ArrayRegister<F32x4V1, 2>) ---
#[thermite_macros::inline_always]
impl CastRegister<super::I16x8V1> for ArrayRegister<super::F32x4V1, 2> {
    fn cast_from(value: Storage<super::I16x8V1>) -> Storage<Self> {
        unsafe {
            let lo = arch::_mm_cvtepi16_epi32x_v1(value);
            let hi = arch::_mm_cvtepi16_epi32x_v1(arch::_mm_srli_si128(value, 8));
            ArrayRegister([arch::_mm_cvtepi32_ps(lo), arch::_mm_cvtepi32_ps(hi)])
        }
    }
}
#[thermite_macros::inline_always]
impl CastRegister<super::U16x8V1> for ArrayRegister<super::F32x4V1, 2> {
    fn cast_from(value: Storage<super::U16x8V1>) -> Storage<Self> {
        unsafe {
            let lo = arch::_mm_cvtepu16_epi32x_v1(value);
            let hi = arch::_mm_cvtepu16_epi32x_v1(arch::_mm_srli_si128(value, 8));
            ArrayRegister([arch::_mm_cvtepi32_ps(lo), arch::_mm_cvtepi32_ps(hi)])
        }
    }
}

// --- x8 (I16x8V1 native <-> ArrayRegister<F64x2V1, 4>) ---
#[thermite_macros::inline_always]
impl CastRegister<super::I16x8V1> for ArrayRegister<super::F64x2V1, 4> {
    fn cast_from(value: Storage<super::I16x8V1>) -> Storage<Self> {
        unsafe {
            let a = arch::_mm_cvtepi32_2pdx_v1(arch::_mm_cvtepi16_epi32x_v1(value));
            let b = arch::_mm_cvtepi32_2pdx_v1(arch::_mm_cvtepi16_epi32x_v1(arch::_mm_srli_si128(value, 8)));
            ArrayRegister([a[0], a[1], b[0], b[1]])
        }
    }
}
#[thermite_macros::inline_always]
impl CastRegister<super::U16x8V1> for ArrayRegister<super::F64x2V1, 4> {
    fn cast_from(value: Storage<super::U16x8V1>) -> Storage<Self> {
        unsafe {
            let a = arch::_mm_cvtepi32_2pdx_v1(arch::_mm_cvtepu16_epi32x_v1(value));
            let b = arch::_mm_cvtepi32_2pdx_v1(arch::_mm_cvtepu16_epi32x_v1(arch::_mm_srli_si128(value, 8)));
            ArrayRegister([a[0], a[1], b[0], b[1]])
        }
    }
}

// --- x16 16 <-> f32: ArrayRegister<I16x8V1, 2> <-> ArrayRegister<F32x4V1, 4> is provided for
//     free by the ArrayRegister 2<->4 reshape cast blanket (array.rs), so no explicit impl. ---

// --- x16 (ArrayRegister<I16x8V1, 2> <-> ArrayRegister<F64x2V1, 8>) ---
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<super::I16x8V1, 2>> for ArrayRegister<super::F64x2V1, 8> {
    fn cast_from(value: Storage<ArrayRegister<super::I16x8V1, 2>>) -> Storage<Self> {
        let v = value.0;
        unsafe {
            let q0 = arch::_mm_cvtepi16_epi32x_v1(v[0]);
            let q1 = arch::_mm_cvtepi16_epi32x_v1(arch::_mm_srli_si128(v[0], 8));
            let q2 = arch::_mm_cvtepi16_epi32x_v1(v[1]);
            let q3 = arch::_mm_cvtepi16_epi32x_v1(arch::_mm_srli_si128(v[1], 8));
            let a = arch::_mm_cvtepi32_2pdx_v1(q0);
            let b = arch::_mm_cvtepi32_2pdx_v1(q1);
            let c = arch::_mm_cvtepi32_2pdx_v1(q2);
            let d = arch::_mm_cvtepi32_2pdx_v1(q3);
            ArrayRegister([a[0], a[1], b[0], b[1], c[0], c[1], d[0], d[1]])
        }
    }
}
#[thermite_macros::inline_always]
impl CastRegister<ArrayRegister<super::U16x8V1, 2>> for ArrayRegister<super::F64x2V1, 8> {
    fn cast_from(value: Storage<ArrayRegister<super::U16x8V1, 2>>) -> Storage<Self> {
        let v = value.0;
        unsafe {
            let q0 = arch::_mm_cvtepu16_epi32x_v1(v[0]);
            let q1 = arch::_mm_cvtepu16_epi32x_v1(arch::_mm_srli_si128(v[0], 8));
            let q2 = arch::_mm_cvtepu16_epi32x_v1(v[1]);
            let q3 = arch::_mm_cvtepu16_epi32x_v1(arch::_mm_srli_si128(v[1], 8));
            let a = arch::_mm_cvtepi32_2pdx_v1(q0);
            let b = arch::_mm_cvtepi32_2pdx_v1(q1);
            let c = arch::_mm_cvtepi32_2pdx_v1(q2);
            let d = arch::_mm_cvtepi32_2pdx_v1(q3);
            ArrayRegister([a[0], a[1], b[0], b[1], c[0], c[1], d[0], d[1]])
        }
    }
}

// --- scalar-fallback gather markers for the reduced 16-bit registers ---
impl IndexableRegister<<super::super::X86V1 as crate::simd::Simd>::u32x4> for I16x4V1 {}
impl IndexableRegister<<super::super::X86V1 as crate::simd::Simd>::u32x4> for U16x4V1 {}
impl IndexableRegister<<super::super::X86V1 as crate::simd::Simd>::u64x4> for I16x4V1 {}
impl IndexableRegister<<super::super::X86V1 as crate::simd::Simd>::u64x4> for U16x4V1 {}