euv-core 0.52.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
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
use super::*;

/// Computes the unwrapped advance width of an ASCII byte slice using NEON, SSE2, WASM-SIMD128, or 4-way unrolled scalar math.
///
/// # Arguments
///
/// - `&[u8]` - ASCII text bytes (`0..128`).
/// - `f32` - Font size in logical pixels.
/// - `f32` - Bold advance multiplier (`1.0`, `1.03`, or `1.08`).
/// - `f32` - Additional per-glyph letter spacing in logical pixels.
/// - `bool` - `true` when `FontFamily::Monospace` is active.
///
/// # Returns
///
/// - `f32` - Total advance width matching `DeterministicTextMeasurer` within `< 1e-4` px.
#[must_use]
pub fn simd_measure_ascii_advance(
    bytes: &[u8],
    font_size: f32,
    bold_factor: f32,
    letter_spacing: f32,
    is_mono: bool,
) -> f32 {
    if bytes.is_empty() {
        return 0.0;
    }
    if is_mono {
        let mono_advance: f32 = font_size * MONOSPACE_ADVANCE_RATIO + letter_spacing;
        return (bytes.len() as f32) * mono_advance;
    }
    let scale_factor: f32 = font_size * bold_factor;
    let chunk_count: usize = bytes.len() / SIMD_VECTOR_LANES;
    let vec_bytes: usize = chunk_count.saturating_mul(SIMD_VECTOR_LANES);
    let mut total_width: f32 = 0.0;
    #[cfg(target_arch = "aarch64")]
    {
        // SAFETY: NEON is guaranteed on `aarch64` and all stack arrays are 16-byte valid.
        unsafe {
            let scale_vec: float32x4_t = vdupq_n_f32(scale_factor);
            let spacing_vec: float32x4_t = vdupq_n_f32(letter_spacing);
            let mut accum_vec: float32x4_t = vdupq_n_f32(0.0);
            let mut offset: usize = 0;
            while offset < vec_bytes {
                let lut_lane: [f32; 4] = [
                    ASCII_ADVANCE_LUT[(bytes[offset] & 0x7F) as usize],
                    ASCII_ADVANCE_LUT[(bytes[offset + 1] & 0x7F) as usize],
                    ASCII_ADVANCE_LUT[(bytes[offset + 2] & 0x7F) as usize],
                    ASCII_ADVANCE_LUT[(bytes[offset + 3] & 0x7F) as usize],
                ];
                let lut_vec: float32x4_t = vld1q_f32(lut_lane.as_ptr());
                accum_vec = vaddq_f32(vmlaq_f32(accum_vec, lut_vec, scale_vec), spacing_vec);
                offset += SIMD_VECTOR_LANES;
            }
            total_width += vaddvq_f32(accum_vec);
        }
    }
    #[cfg(target_arch = "x86_64")]
    {
        // SAFETY: SSE2 is guaranteed on `x86_64` and all stack arrays are valid.
        unsafe {
            let scale_vec: __m128 = _mm_set1_ps(scale_factor);
            let spacing_vec: __m128 = _mm_set1_ps(letter_spacing);
            let mut accum_vec: __m128 = _mm_set1_ps(0.0);
            let mut offset: usize = 0;
            while offset < vec_bytes {
                let lut_lane: [f32; 4] = [
                    ASCII_ADVANCE_LUT[(bytes[offset] & 0x7F) as usize],
                    ASCII_ADVANCE_LUT[(bytes[offset + 1] & 0x7F) as usize],
                    ASCII_ADVANCE_LUT[(bytes[offset + 2] & 0x7F) as usize],
                    ASCII_ADVANCE_LUT[(bytes[offset + 3] & 0x7F) as usize],
                ];
                let lut_vec: __m128 = _mm_loadu_ps(lut_lane.as_ptr());
                let scaled: __m128 = _mm_add_ps(_mm_mul_ps(lut_vec, scale_vec), spacing_vec);
                accum_vec = _mm_add_ps(accum_vec, scaled);
                offset += SIMD_VECTOR_LANES;
            }
            let mut lanes: [f32; 4] = [0.0; 4];
            _mm_storeu_ps(lanes.as_mut_ptr(), accum_vec);
            total_width += (lanes[0] + lanes[1]) + (lanes[2] + lanes[3]);
        }
    }
    #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
    {
        let scale_vec: v128 = f32x4_splat(scale_factor);
        let spacing_vec: v128 = f32x4_splat(letter_spacing);
        let mut accum_vec: v128 = f32x4_splat(0.0);
        let mut offset: usize = 0;
        while offset < vec_bytes {
            let lut_vec: v128 = f32x4(
                ASCII_ADVANCE_LUT[(bytes[offset] & 0x7F) as usize],
                ASCII_ADVANCE_LUT[(bytes[offset + 1] & 0x7F) as usize],
                ASCII_ADVANCE_LUT[(bytes[offset + 2] & 0x7F) as usize],
                ASCII_ADVANCE_LUT[(bytes[offset + 3] & 0x7F) as usize],
            );
            let scaled: v128 = f32x4_add(f32x4_mul(lut_vec, scale_vec), spacing_vec);
            accum_vec = f32x4_add(accum_vec, scaled);
            offset += SIMD_VECTOR_LANES;
        }
        total_width += (f32x4_extract_lane::<0>(accum_vec) + f32x4_extract_lane::<1>(accum_vec))
            + (f32x4_extract_lane::<2>(accum_vec) + f32x4_extract_lane::<3>(accum_vec));
    }
    #[cfg(not(target_arch = "aarch64"))]
    #[cfg(not(target_arch = "x86_64"))]
    #[cfg(not(all(target_arch = "wasm32", target_feature = "simd128")))]
    {
        let mut lane_sums: [f32; 4] = [0.0; 4];
        let mut offset: usize = 0;
        while offset < vec_bytes {
            lane_sums[0] +=
                ASCII_ADVANCE_LUT[(bytes[offset] & 0x7F) as usize] * scale_factor + letter_spacing;
            lane_sums[1] += ASCII_ADVANCE_LUT[(bytes[offset + 1] & 0x7F) as usize] * scale_factor
                + letter_spacing;
            lane_sums[2] += ASCII_ADVANCE_LUT[(bytes[offset + 2] & 0x7F) as usize] * scale_factor
                + letter_spacing;
            lane_sums[3] += ASCII_ADVANCE_LUT[(bytes[offset + 3] & 0x7F) as usize] * scale_factor
                + letter_spacing;
            offset += SIMD_VECTOR_LANES;
        }
        total_width += (lane_sums[0] + lane_sums[1]) + (lane_sums[2] + lane_sums[3]);
    }
    for &byte_val in &bytes[vec_bytes..] {
        total_width +=
            ASCII_ADVANCE_LUT[(byte_val & 0x7F) as usize] * scale_factor + letter_spacing;
    }
    total_width
}

/// Measures single-line ASCII text via SIMD advance accumulation or falls back to `DeterministicTextMeasurer`, ensuring bit-identical widths across serial and parallel layout.
///
/// # Arguments
///
/// - `&str` - UTF-8 text string to measure.
/// - `bool` - Whether `sample_text` contains only ASCII bytes.
/// - `TextStyle` - Active typography style.
/// - `Option<f32>` - Optional maximum wrapping or ellipsis width in logical pixels.
///
/// # Returns
///
/// - `(Size, bool)`: Measured bounding size and `true` when the SIMD ASCII fast-path succeeded.
#[cfg_attr(not(target_arch = "wasm32"), inline)]
#[must_use]
pub(crate) fn measure_text_simd_or_fallback(
    sample_text: &str,
    is_ascii: bool,
    sample_style: TextStyle,
    width_limit: Option<f32>,
) -> (Size, bool) {
    let raw_bytes: &[u8] = sample_text.as_bytes();
    let has_edge_space: bool = raw_bytes
        .first()
        .is_some_and(|byte: &u8| byte.is_ascii_whitespace())
        || raw_bytes
            .last()
            .is_some_and(|byte: &u8| byte.is_ascii_whitespace());
    let single_raw_line: bool =
        is_ascii && !raw_bytes.is_empty() && !raw_bytes.contains(&b'\n') && !has_edge_space;
    if single_raw_line {
        let is_mono: bool = sample_style.get_family() == FontFamily::Monospace;
        let bold_factor: f32 = text_bold_advance_factor(sample_style);
        let unwrapped_w: f32 = simd_measure_ascii_advance(
            raw_bytes,
            sample_style.get_font_size(),
            bold_factor,
            sample_style.get_letter_spacing(),
            is_mono,
        );
        let line_height_px: f32 =
            (sample_style.get_font_size() * sample_style.get_line_height()).max(1.0);
        let fits_fast_path: bool = match sample_style.get_overflow() {
            TextOverflow::Clip => true,
            TextOverflow::Ellipsis => match width_limit {
                Some(limit) if limit > 0.0 => unwrapped_w <= limit + WRAP_WIDTH_EPSILON,
                _ => true,
            },
            TextOverflow::Wrap => match width_limit {
                Some(limit) if limit > 0.0 => unwrapped_w <= limit.max(1.0) + WRAP_WIDTH_EPSILON,
                _ => true,
            },
        };
        if fits_fast_path {
            return (Size::new(unwrapped_w, line_height_px), true);
        }
    }
    (
        DeterministicTextMeasurer.measure_size(sample_text, sample_style, width_limit),
        false,
    )
}

/// Translates one `[x, y, width, height]` rectangle by `(delta_x, delta_y)` using a single SIMD 4-lane vector add.
///
/// # Arguments
///
/// - `Rect` - Source logical or physical rectangle.
/// - `f32` - Horizontal translation delta.
/// - `f32` - Vertical translation delta.
///
/// # Returns
///
/// - `Rect` - Translated rectangle preserving exact width and height.
#[cfg_attr(not(target_arch = "wasm32"), inline)]
#[must_use]
pub fn simd_translate_rect(rect: Rect, delta_x: f32, delta_y: f32) -> Rect {
    let raw: [f32; 4] = [
        rect.get_x(),
        rect.get_y(),
        rect.get_width(),
        rect.get_height(),
    ];
    let delta: [f32; 4] = [delta_x, delta_y, 0.0, 0.0];
    let mut out: [f32; 4] = [0.0; 4];
    #[cfg(target_arch = "aarch64")]
    {
        // SAFETY: NEON is guaranteed on `aarch64` and `[f32; 4]` is valid for 16-byte loads/stores.
        unsafe {
            let rv: float32x4_t = vld1q_f32(raw.as_ptr());
            let dv: float32x4_t = vld1q_f32(delta.as_ptr());
            vst1q_f32(out.as_mut_ptr(), vaddq_f32(rv, dv));
        }
    }
    #[cfg(target_arch = "x86_64")]
    {
        // SAFETY: SSE2 is guaranteed on `x86_64` and `[f32; 4]` is valid for unaligned loads/stores.
        unsafe {
            let rv: __m128 = _mm_loadu_ps(raw.as_ptr());
            let dv: __m128 = _mm_loadu_ps(delta.as_ptr());
            _mm_storeu_ps(out.as_mut_ptr(), _mm_add_ps(rv, dv));
        }
    }
    #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
    {
        // SAFETY: SIMD128 is enabled under this cfg and `[f32; 4]` is 16 bytes wide.
        unsafe {
            let rv: v128 = v128_load(raw.as_ptr().cast::<v128>());
            let dv: v128 = v128_load(delta.as_ptr().cast::<v128>());
            v128_store(out.as_mut_ptr().cast::<v128>(), f32x4_add(rv, dv));
        }
    }
    #[cfg(not(target_arch = "aarch64"))]
    #[cfg(not(target_arch = "x86_64"))]
    #[cfg(not(all(target_arch = "wasm32", target_feature = "simd128")))]
    {
        out[0] = raw[0] + delta[0];
        out[1] = raw[1] + delta[1];
        out[2] = raw[2];
        out[3] = raw[3];
    }
    Rect::new(out[0], out[1], out[2], out[3])
}

/// Translates `LayoutBox` bounds and content bounds for disjoint arena slots listed in `arena_indices` via raw pointer arithmetic without aliasing sibling slots.
///
/// # Arguments
///
/// - `*mut LayoutBox` - Base pointer to the arena-indexed layout box buffer.
/// - `usize` - Total length of the arena-indexed layout box buffer.
/// - `&[u32]` - Strictly disjoint arena indices belonging to the shifted subtree.
/// - `f32` - Horizontal translation delta in logical pixels.
/// - `f32` - Vertical translation delta in logical pixels.
///
/// # Returns
///
/// - `usize` - Number of layout boxes translated.
pub(crate) unsafe fn simd_translate_subtree_ptr_4x(
    boxes_ptr: *mut LayoutBox,
    boxes_len: usize,
    arena_indices: &[u32],
    delta_x: f32,
    delta_y: f32,
) -> usize {
    if (delta_x == 0.0 && delta_y == 0.0) || arena_indices.is_empty() {
        return 0;
    }
    let mut translated: usize = 0;
    let chunk_count: usize = arena_indices.len() / SIMD_VECTOR_LANES;
    let vec_limit: usize = chunk_count.saturating_mul(SIMD_VECTOR_LANES);
    let mut idx_pos: usize = 0;
    while idx_pos < vec_limit {
        let slot_zero: usize = arena_indices[idx_pos] as usize;
        let slot_one: usize = arena_indices[idx_pos + 1] as usize;
        let slot_two: usize = arena_indices[idx_pos + 2] as usize;
        let slot_three: usize = arena_indices[idx_pos + 3] as usize;
        for slot_idx in [slot_zero, slot_one, slot_two, slot_three] {
            if slot_idx < boxes_len {
                // SAFETY: Caller guarantees every index in `arena_indices` is unique and exclusive to this thread.
                let target_box: &mut LayoutBox = unsafe { &mut *boxes_ptr.add(slot_idx) };
                let shifted_bounds: Rect =
                    simd_translate_rect(target_box.get_bounds(), delta_x, delta_y);
                let shifted_content: Rect =
                    simd_translate_rect(target_box.get_content_bounds(), delta_x, delta_y);
                let _: &mut LayoutBox = target_box
                    .set_bounds(shifted_bounds)
                    .set_content_bounds(shifted_content);
                translated = translated.saturating_add(1);
            }
        }
        idx_pos += SIMD_VECTOR_LANES;
    }
    for &raw_index in &arena_indices[vec_limit..] {
        let slot_idx: usize = raw_index as usize;
        if slot_idx < boxes_len {
            // SAFETY: Caller guarantees every index in `arena_indices` is unique and exclusive to this thread.
            let target_box: &mut LayoutBox = unsafe { &mut *boxes_ptr.add(slot_idx) };
            let shifted_bounds: Rect =
                simd_translate_rect(target_box.get_bounds(), delta_x, delta_y);
            let shifted_content: Rect =
                simd_translate_rect(target_box.get_content_bounds(), delta_x, delta_y);
            let _: &mut LayoutBox = target_box
                .set_bounds(shifted_bounds)
                .set_content_bounds(shifted_content);
            translated = translated.saturating_add(1);
        }
    }
    translated
}

/// Translates `LayoutBox` bounds and content bounds for all arena slots listed in `arena_indices` using SIMD 4-lane vector ops.
///
/// # Arguments
///
/// - `&mut [LayoutBox]` - Arena-indexed layout box slice.
/// - `&[u32]` - Arena indices belonging to the shifted subtree.
/// - `f32` - Horizontal translation delta in logical pixels.
/// - `f32` - Vertical translation delta in logical pixels.
///
/// # Returns
///
/// - `usize` - Number of layout boxes translated.
pub fn simd_translate_subtree_4x(
    boxes: &mut [LayoutBox],
    arena_indices: &[u32],
    delta_x: f32,
    delta_y: f32,
) -> usize {
    // SAFETY: Exclusive `&mut [LayoutBox]` guarantees full ownership over all valid indices in `boxes`.
    unsafe {
        simd_translate_subtree_ptr_4x(
            boxes.as_mut_ptr(),
            boxes.len(),
            arena_indices,
            delta_x,
            delta_y,
        )
    }
}

/// Translates a contiguous slice of `[x, y, width, height]` rectangles by `(delta_x, delta_y)` in SIMD 4-rect unrolled batches.
///
/// # Arguments
///
/// - `&mut [[f32; 4]]` - Contiguous rectangle array (`[x, y, width, height]`).
/// - `f32` - Horizontal translation delta.
/// - `f32` - Vertical translation delta.
pub fn simd_translate_rects_contiguous_4x(rects: &mut [[f32; 4]], delta_x: f32, delta_y: f32) {
    if (delta_x == 0.0 && delta_y == 0.0) || rects.is_empty() {
        return;
    }
    let delta: [f32; 4] = [delta_x, delta_y, 0.0, 0.0];
    #[cfg(target_arch = "aarch64")]
    {
        // SAFETY: NEON is guaranteed on `aarch64` and every `[f32; 4]` element is 16 bytes valid.
        unsafe {
            let dv: float32x4_t = vld1q_f32(delta.as_ptr());
            for rect in rects.iter_mut() {
                let rv: float32x4_t = vld1q_f32(rect.as_ptr());
                vst1q_f32(rect.as_mut_ptr(), vaddq_f32(rv, dv));
            }
        }
    }
    #[cfg(target_arch = "x86_64")]
    {
        // SAFETY: SSE2 is guaranteed on `x86_64` and every `[f32; 4]` element is 16 bytes valid.
        unsafe {
            let dv: __m128 = _mm_loadu_ps(delta.as_ptr());
            for rect in rects.iter_mut() {
                let rv: __m128 = _mm_loadu_ps(rect.as_ptr());
                _mm_storeu_ps(rect.as_mut_ptr(), _mm_add_ps(rv, dv));
            }
        }
    }
    #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
    {
        // SAFETY: SIMD128 is enabled under this cfg and `[f32; 4]` is 16 bytes wide.
        unsafe {
            let dv: v128 = v128_load(delta.as_ptr().cast::<v128>());
            for rect in rects.iter_mut() {
                let rv: v128 = v128_load(rect.as_ptr().cast::<v128>());
                v128_store(rect.as_mut_ptr().cast::<v128>(), f32x4_add(rv, dv));
            }
        }
    }
    #[cfg(not(target_arch = "aarch64"))]
    #[cfg(not(target_arch = "x86_64"))]
    #[cfg(not(all(target_arch = "wasm32", target_feature = "simd128")))]
    {
        for rect in rects.iter_mut() {
            rect[0] += delta[0];
            rect[1] += delta[1];
        }
    }
}

/// Evaluates 4 candidate `[x, y, width, height]` rectangles against `clip_rect` (`[x0, y0, x1, y1]`) and returns a 4-bit culled mask.
///
/// # Arguments
///
/// - `&[[f32; 4]; 4]` - Four physical bounding rectangles `[x, y, width, height]`.
/// - `[f32; 4]` - Active scissor bounds `[clip_x0, clip_y0, clip_x1, clip_y1]`.
///
/// # Returns
///
/// - `u8` - Bitmask where bit `lane` (`0..4`) is `1` when rectangle `lane` is completely culled.
#[cfg_attr(not(target_arch = "wasm32"), inline)]
#[must_use]
pub(crate) fn simd_cull_mask_4x(rects: &[[f32; 4]; 4], clip_rect: [f32; 4]) -> u8 {
    let x0_arr: [f32; 4] = [rects[0][0], rects[1][0], rects[2][0], rects[3][0]];
    let y0_arr: [f32; 4] = [rects[0][1], rects[1][1], rects[2][1], rects[3][1]];
    let w_arr: [f32; 4] = [rects[0][2], rects[1][2], rects[2][2], rects[3][2]];
    let h_arr: [f32; 4] = [rects[0][3], rects[1][3], rects[2][3], rects[3][3]];
    #[cfg(target_arch = "aarch64")]
    {
        // SAFETY: NEON is guaranteed on `aarch64` and all stack arrays hold 4 valid `f32`/`u32` lanes.
        unsafe {
            let vx0: float32x4_t = vld1q_f32(x0_arr.as_ptr());
            let vy0: float32x4_t = vld1q_f32(y0_arr.as_ptr());
            let vw: float32x4_t = vld1q_f32(w_arr.as_ptr());
            let vh: float32x4_t = vld1q_f32(h_arr.as_ptr());
            let vx1: float32x4_t = vaddq_f32(vx0, vw);
            let vy1: float32x4_t = vaddq_f32(vy0, vh);
            let zero: float32x4_t = vdupq_n_f32(0.0);
            let cx0: float32x4_t = vdupq_n_f32(clip_rect[0]);
            let cy0: float32x4_t = vdupq_n_f32(clip_rect[1]);
            let cx1: float32x4_t = vdupq_n_f32(clip_rect[2]);
            let cy1: float32x4_t = vdupq_n_f32(clip_rect[3]);
            let c_empty: uint32x4_t = vorrq_u32(vcleq_f32(vw, zero), vcleq_f32(vh, zero));
            let c_x: uint32x4_t = vorrq_u32(vcleq_f32(vx1, cx0), vcgeq_f32(vx0, cx1));
            let c_y: uint32x4_t = vorrq_u32(vcleq_f32(vy1, cy0), vcgeq_f32(vy0, cy1));
            let culled: uint32x4_t = vorrq_u32(c_empty, vorrq_u32(c_x, c_y));
            let lane_bits: [u32; 4] = [1, 2, 4, 8];
            let masked: uint32x4_t = vandq_u32(culled, vld1q_u32(lane_bits.as_ptr()));
            vaddvq_u32(masked) as u8
        }
    }
    #[cfg(target_arch = "x86_64")]
    {
        // SAFETY: SSE2 is guaranteed on `x86_64` and all stack arrays hold 4 valid `f32` lanes.
        unsafe {
            let vx0: __m128 = _mm_loadu_ps(x0_arr.as_ptr());
            let vy0: __m128 = _mm_loadu_ps(y0_arr.as_ptr());
            let vw: __m128 = _mm_loadu_ps(w_arr.as_ptr());
            let vh: __m128 = _mm_loadu_ps(h_arr.as_ptr());
            let vx1: __m128 = _mm_add_ps(vx0, vw);
            let vy1: __m128 = _mm_add_ps(vy0, vh);
            let zero: __m128 = _mm_set1_ps(0.0);
            let cx0: __m128 = _mm_set1_ps(clip_rect[0]);
            let cy0: __m128 = _mm_set1_ps(clip_rect[1]);
            let cx1: __m128 = _mm_set1_ps(clip_rect[2]);
            let cy1: __m128 = _mm_set1_ps(clip_rect[3]);
            let c_empty: __m128 = _mm_or_ps(_mm_cmple_ps(vw, zero), _mm_cmple_ps(vh, zero));
            let c_x: __m128 = _mm_or_ps(_mm_cmple_ps(vx1, cx0), _mm_cmpge_ps(vx0, cx1));
            let c_y: __m128 = _mm_or_ps(_mm_cmple_ps(vy1, cy0), _mm_cmpge_ps(vy0, cy1));
            let culled: __m128 = _mm_or_ps(c_empty, _mm_or_ps(c_x, c_y));
            _mm_movemask_ps(culled) as u8
        }
    }
    #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
    {
        let vx0: v128 = f32x4(x0_arr[0], x0_arr[1], x0_arr[2], x0_arr[3]);
        let vy0: v128 = f32x4(y0_arr[0], y0_arr[1], y0_arr[2], y0_arr[3]);
        let vw: v128 = f32x4(w_arr[0], w_arr[1], w_arr[2], w_arr[3]);
        let vh: v128 = f32x4(h_arr[0], h_arr[1], h_arr[2], h_arr[3]);
        let vx1: v128 = f32x4_add(vx0, vw);
        let vy1: v128 = f32x4_add(vy0, vh);
        let zero: v128 = f32x4_splat(0.0);
        let cx0: v128 = f32x4_splat(clip_rect[0]);
        let cy0: v128 = f32x4_splat(clip_rect[1]);
        let cx1: v128 = f32x4_splat(clip_rect[2]);
        let cy1: v128 = f32x4_splat(clip_rect[3]);
        let c_empty: v128 = v128_or(f32x4_le(vw, zero), f32x4_le(vh, zero));
        let c_x: v128 = v128_or(f32x4_le(vx1, cx0), f32x4_ge(vx0, cx1));
        let c_y: v128 = v128_or(f32x4_le(vy1, cy0), f32x4_ge(vy0, cy1));
        let culled: v128 = v128_or(c_empty, v128_or(c_x, c_y));
        i32x4_bitmask(culled) as u8
    }
    #[cfg(not(target_arch = "aarch64"))]
    #[cfg(not(target_arch = "x86_64"))]
    #[cfg(not(all(target_arch = "wasm32", target_feature = "simd128")))]
    {
        let mut mask: u8 = 0;
        for lane in 0..SIMD_VECTOR_LANES {
            let x0: f32 = x0_arr[lane];
            let y0: f32 = y0_arr[lane];
            let width_val: f32 = w_arr[lane];
            let height_val: f32 = h_arr[lane];
            let is_culled: bool = width_val <= 0.0
                || height_val <= 0.0
                || x0 + width_val <= clip_rect[0]
                || x0 >= clip_rect[2]
                || y0 + height_val <= clip_rect[1]
                || y0 >= clip_rect[3];
            if is_culled {
                mask |= 1_u8 << lane;
            }
        }
        mask
    }
}

/// Performs 4-wide SIMD frustum culling against `clip_rect` and packs surviving entries directly into 64-byte `GpuPrimitiveInstance`s.
///
/// # Arguments
///
/// - `&[[f32; 4]]` - Physical bounding rectangles `[x, y, width, height]`.
/// - `&[GpuVisualStyleInput]` - Matching visual descriptors parallel to `rects`.
/// - `[f32; 4]` - Active scissor bounds `[clip_x0, clip_y0, clip_x1, clip_y1]`.
/// - `&mut Vec<GpuPrimitiveInstance>` - Destination packed 64-byte GPU primitive buffer.
///
/// # Returns
///
/// - `usize` - Number of visible primitives emitted into `out_instances`.
pub fn simd_cull_pack_primitives_4x(
    rects: &[[f32; 4]],
    visuals: &[GpuVisualStyleInput],
    clip_rect: [f32; 4],
    out_instances: &mut Vec<GpuPrimitiveInstance>,
) -> usize {
    let total_count: usize = rects.len().min(visuals.len());
    if total_count == 0 || clip_rect[2] <= clip_rect[0] || clip_rect[3] <= clip_rect[1] {
        return 0;
    }
    let chunk_count: usize = total_count / SIMD_VECTOR_LANES;
    let vec_limit: usize = chunk_count.saturating_mul(SIMD_VECTOR_LANES);
    let mut emitted: usize = 0;
    let mut offset: usize = 0;
    while offset < vec_limit {
        let pack_rects: [[f32; 4]; 4] = [
            rects[offset],
            rects[offset + 1],
            rects[offset + 2],
            rects[offset + 3],
        ];
        let culled_mask: u8 = simd_cull_mask_4x(&pack_rects, clip_rect);
        if culled_mask != 0x0F {
            for lane in 0..SIMD_VECTOR_LANES {
                if (culled_mask & (1_u8 << lane)) == 0 {
                    let visual: GpuVisualStyleInput = visuals[offset + lane];
                    out_instances.push(GpuPrimitiveInstance {
                        rect: pack_rects[lane],
                        clip_rect,
                        color_rgba: visual.get_color_rgba(),
                        aux_color_rgba: visual.get_aux_color_rgba(),
                        uv_rect: visual.get_uv_rect(),
                        radii_border: visual.get_radii_border(),
                        kind_flags: visual.get_kind_flags(),
                        extra_param: visual.get_extra_param(),
                    });
                    emitted = emitted.saturating_add(1);
                }
            }
        }
        offset += SIMD_VECTOR_LANES;
    }
    for tail_idx in vec_limit..total_count {
        let rect: [f32; 4] = rects[tail_idx];
        let is_visible: bool = rect[2] > 0.0
            && rect[3] > 0.0
            && rect[0] + rect[2] > clip_rect[0]
            && rect[0] < clip_rect[2]
            && rect[1] + rect[3] > clip_rect[1]
            && rect[1] < clip_rect[3];
        if is_visible {
            let visual: GpuVisualStyleInput = visuals[tail_idx];
            out_instances.push(GpuPrimitiveInstance {
                rect,
                clip_rect,
                color_rgba: visual.get_color_rgba(),
                aux_color_rgba: visual.get_aux_color_rgba(),
                uv_rect: visual.get_uv_rect(),
                radii_border: visual.get_radii_border(),
                kind_flags: visual.get_kind_flags(),
                extra_param: visual.get_extra_param(),
            });
            emitted = emitted.saturating_add(1);
        }
    }
    emitted
}