-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimd_buffer_bytes.mbt
More file actions
908 lines (843 loc) · 41 KB
/
Copy pathsimd_buffer_bytes.mbt
File metadata and controls
908 lines (843 loc) · 41 KB
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
///|
/// Byte sibling of `SimdBuffer`. Linear-memory backed, 1 byte per element.
/// Targets wasm + wasm-gc. Use when you want SIMD base64 / memcpy / scan
/// on byte buffers without paying the FixedArray-on-GC overhead.
pub struct SimdBufferBytes {
addr : Int
len : Int
}
///|
pub fn SimdBufferBytes::make(len : Int) -> SimdBufferBytes {
// +16 trailing slack so final v128.load (16 bytes) stays in-bounds.
let bytes = len + 16
let addr = buf_alloc(bytes)
{ addr, len }
}
///|
pub fn SimdBufferBytes::length(self : SimdBufferBytes) -> Int {
self.len
}
///|
/// Linear-memory address of byte 0. wasm + wasm-gc only — gives
/// downstream packages (e.g. `@simdjson`) a way to plug inline-WAT
/// kernels on top of SimdBufferBytes without exposing the struct
/// internals.
pub fn SimdBufferBytes::raw_addr(self : SimdBufferBytes) -> Int {
self.addr
}
///|
pub fn SimdBufferBytes::get(self : SimdBufferBytes, i : Int) -> Byte {
if i < 0 || i >= self.len {
abort("SimdBufferBytes.get: index out of bounds")
}
buf_load_byte(self.addr + i).to_byte()
}
///|
pub fn SimdBufferBytes::set(self : SimdBufferBytes, i : Int, v : Byte) -> Unit {
if i < 0 || i >= self.len {
abort("SimdBufferBytes.set: index out of bounds")
}
buf_store_byte(self.addr + i, v.to_int())
}
///|
/// Base64 encode this buffer. Returns a freshly-allocated SimdBufferBytes
/// holding the RFC 4648 padded encoding. SIMD path (12 in / 16 out) for the
/// bulk; arithmetic scalar for the trailing 1-2 bytes.
pub fn SimdBufferBytes::base64_encode(
input : SimdBufferBytes,
) -> SimdBufferBytes {
let n = input.len
let out_len = (n + 2) / 3 * 4
let out = SimdBufferBytes::make(out_len)
let simd_chunks_initial = if n >= 16 { (n - 16) / 12 + 1 } else { 0 }
let simd_chunks = if simd_chunks_initial > 0 &&
(simd_chunks_initial - 1) * 12 + 16 > n {
simd_chunks_initial - 1
} else {
simd_chunks_initial
}
if simd_chunks > 0 {
buf_base64_encode(input.addr, out.addr, simd_chunks)
}
let consumed = simd_chunks * 12
let mut q = simd_chunks * 16
let mut p = consumed
// Scalar full triples.
while p + 3 <= n {
let b0 = buf_load_byte(input.addr + p)
let b1 = buf_load_byte(input.addr + p + 1)
let b2 = buf_load_byte(input.addr + p + 2)
let v = (b0 << 16) | (b1 << 8) | b2
buf_store_byte(out.addr + q, encode_sextet((v >> 18) & 63))
buf_store_byte(out.addr + q + 1, encode_sextet((v >> 12) & 63))
buf_store_byte(out.addr + q + 2, encode_sextet((v >> 6) & 63))
buf_store_byte(out.addr + q + 3, encode_sextet(v & 63))
p = p + 3
q = q + 4
}
let rem = n - p
if rem == 1 {
let v = buf_load_byte(input.addr + p) << 16
buf_store_byte(out.addr + q, encode_sextet((v >> 18) & 63))
buf_store_byte(out.addr + q + 1, encode_sextet((v >> 12) & 63))
buf_store_byte(out.addr + q + 2, '='.to_int())
buf_store_byte(out.addr + q + 3, '='.to_int())
} else if rem == 2 {
let b0 = buf_load_byte(input.addr + p)
let b1 = buf_load_byte(input.addr + p + 1)
let v = (b0 << 16) | (b1 << 8)
buf_store_byte(out.addr + q, encode_sextet((v >> 18) & 63))
buf_store_byte(out.addr + q + 1, encode_sextet((v >> 12) & 63))
buf_store_byte(out.addr + q + 2, encode_sextet((v >> 6) & 63))
buf_store_byte(out.addr + q + 3, '='.to_int())
}
out
}
///|
/// Base64 decode. Returns `None` if the input length is not a multiple of 4
/// or if any character is out of alphabet (excluding the trailing `=`
/// padding). SIMD bulk + scalar tail for the last 4-char group.
pub fn SimdBufferBytes::base64_decode(
input : SimdBufferBytes,
) -> SimdBufferBytes? {
let n = input.len
if n == 0 {
return Some(SimdBufferBytes::make(0))
}
if n % 4 != 0 {
return None
}
let pad = base64_count_padding(input)
let out_len = n / 4 * 3 - pad
let out = SimdBufferBytes::make(out_len)
let bulk_chunks = if n >= 32 { (n - 4) / 16 } else { 0 }
let mut consumed = 0
if bulk_chunks > 0 {
let err = buf_base64_decode(input.addr, out.addr, bulk_chunks)
if err != 0 {
// Fall back to scalar from the start.
return base64_decode_scalar(input, out)
}
consumed = bulk_chunks * 16
}
// Scalar tail.
let tail_q = bulk_chunks * 12
match base64_decode_tail(input, out, consumed, tail_q, pad) {
Some(_) => Some(out)
None => None
}
}
///|
/// In-place encode into a caller-supplied output buffer. Useful when you
/// want to reuse a SimdBufferBytes across many calls (avoids `memory.grow`
/// per call). `out` must have at least `(input.length() + 2) / 3 * 4` bytes.
/// Caller is responsible for sizing.
pub fn SimdBufferBytes::base64_encode_into(
input : SimdBufferBytes,
out : SimdBufferBytes,
) -> Unit {
let n = input.len
let simd_chunks_initial = if n >= 16 { (n - 16) / 12 + 1 } else { 0 }
let simd_chunks = if simd_chunks_initial > 0 &&
(simd_chunks_initial - 1) * 12 + 16 > n {
simd_chunks_initial - 1
} else {
simd_chunks_initial
}
if simd_chunks > 0 {
buf_base64_encode(input.addr, out.addr, simd_chunks)
}
let consumed = simd_chunks * 12
let mut q = simd_chunks * 16
let mut p = consumed
while p + 3 <= n {
let b0 = buf_load_byte(input.addr + p)
let b1 = buf_load_byte(input.addr + p + 1)
let b2 = buf_load_byte(input.addr + p + 2)
let v = (b0 << 16) | (b1 << 8) | b2
buf_store_byte(out.addr + q, encode_sextet((v >> 18) & 63))
buf_store_byte(out.addr + q + 1, encode_sextet((v >> 12) & 63))
buf_store_byte(out.addr + q + 2, encode_sextet((v >> 6) & 63))
buf_store_byte(out.addr + q + 3, encode_sextet(v & 63))
p = p + 3
q = q + 4
}
let rem = n - p
if rem == 1 {
let v = buf_load_byte(input.addr + p) << 16
buf_store_byte(out.addr + q, encode_sextet((v >> 18) & 63))
buf_store_byte(out.addr + q + 1, encode_sextet((v >> 12) & 63))
buf_store_byte(out.addr + q + 2, '='.to_int())
buf_store_byte(out.addr + q + 3, '='.to_int())
} else if rem == 2 {
let b0 = buf_load_byte(input.addr + p)
let b1 = buf_load_byte(input.addr + p + 1)
let v = (b0 << 16) | (b1 << 8)
buf_store_byte(out.addr + q, encode_sextet((v >> 18) & 63))
buf_store_byte(out.addr + q + 1, encode_sextet((v >> 12) & 63))
buf_store_byte(out.addr + q + 2, encode_sextet((v >> 6) & 63))
buf_store_byte(out.addr + q + 3, '='.to_int())
}
}
///|
/// In-place decode counterpart. Returns the number of decoded bytes
/// written, or `None` on invalid input. `out` must be large enough.
pub fn SimdBufferBytes::base64_decode_into(
input : SimdBufferBytes,
out : SimdBufferBytes,
) -> Int? {
let n = input.len
if n == 0 {
return Some(0)
}
if n % 4 != 0 {
return None
}
let pad = base64_count_padding(input)
let bulk_chunks = if n >= 32 { (n - 4) / 16 } else { 0 }
let mut consumed = 0
if bulk_chunks > 0 {
let err = buf_base64_decode(input.addr, out.addr, bulk_chunks)
if err != 0 {
return base64_decode_tail(input, out, 0, 0, pad)
}
consumed = bulk_chunks * 16
}
let tail_q = bulk_chunks * 12
base64_decode_tail(input, out, consumed, tail_q, pad)
}
///|
fn base64_count_padding(input : SimdBufferBytes) -> Int {
let n = input.len
if n == 0 {
return 0
}
if buf_load_byte(input.addr + n - 1) != '='.to_int() {
return 0
}
if n >= 2 && buf_load_byte(input.addr + n - 2) == '='.to_int() {
return 2
}
1
}
///|
/// Inline 6-bit → ASCII via arithmetic offsets (same recipe as the SIMD
/// encode arithmetic stage).
fn encode_sextet(v : Int) -> Int {
let mut r = v + 65
if v >= 26 {
r = r + 6
}
if v >= 52 {
r = r - 75
}
if v >= 62 {
r = r - 15
}
if v >= 63 {
r = r + 3
}
r
}
///|
/// Inverse: ASCII → 6-bit, or -1 if not a Base64 alphabet char.
fn decode_sextet(c : Int) -> Int {
if c >= 65 && c <= 90 {
return c - 65
}
if c >= 97 && c <= 122 {
return c - 71
}
if c >= 48 && c <= 57 {
return c + 4
}
if c == 43 {
return 62
}
if c == 47 {
return 63
}
-1
}
///|
/// Full-scalar fallback for decode when the SIMD path reported an
/// out-of-alphabet character.
fn base64_decode_scalar(
input : SimdBufferBytes,
out : SimdBufferBytes,
) -> SimdBufferBytes? {
match base64_decode_tail(input, out, 0, 0, base64_count_padding(input)) {
Some(_) => Some(out)
None => None
}
}
///|
/// Decode the input range `[p, n)` into the output starting at `q`, where
/// `pad` is the trailing `=` count (0, 1, 2). Returns Some(out_len) or None
/// on invalid input.
fn base64_decode_tail(
input : SimdBufferBytes,
out : SimdBufferBytes,
p0 : Int,
q0 : Int,
pad : Int,
) -> Int? {
let n = input.len
if n == 0 {
return Some(0)
}
let mut p = p0
let mut q = q0
while p + 4 <= n - 4 {
let d0 = decode_sextet(buf_load_byte(input.addr + p))
let d1 = decode_sextet(buf_load_byte(input.addr + p + 1))
let d2 = decode_sextet(buf_load_byte(input.addr + p + 2))
let d3 = decode_sextet(buf_load_byte(input.addr + p + 3))
if (d0 | d1 | d2 | d3) < 0 {
return None
}
let v = (d0 << 18) | (d1 << 12) | (d2 << 6) | d3
buf_store_byte(out.addr + q, (v >> 16) & 0xFF)
buf_store_byte(out.addr + q + 1, (v >> 8) & 0xFF)
buf_store_byte(out.addr + q + 2, v & 0xFF)
p = p + 4
q = q + 3
}
// Final 4-char group.
let d0 = decode_sextet(buf_load_byte(input.addr + p))
let d1 = decode_sextet(buf_load_byte(input.addr + p + 1))
if (d0 | d1) < 0 {
return None
}
if pad == 2 {
let v = (d0 << 18) | (d1 << 12)
buf_store_byte(out.addr + q, (v >> 16) & 0xFF)
Some(q + 1)
} else {
let d2 = decode_sextet(buf_load_byte(input.addr + p + 2))
if d2 < 0 {
return None
}
if pad == 1 {
let v = (d0 << 18) | (d1 << 12) | (d2 << 6)
buf_store_byte(out.addr + q, (v >> 16) & 0xFF)
buf_store_byte(out.addr + q + 1, (v >> 8) & 0xFF)
Some(q + 2)
} else {
let d3 = decode_sextet(buf_load_byte(input.addr + p + 3))
if d3 < 0 {
return None
}
let v = (d0 << 18) | (d1 << 12) | (d2 << 6) | d3
buf_store_byte(out.addr + q, (v >> 16) & 0xFF)
buf_store_byte(out.addr + q + 1, (v >> 8) & 0xFF)
buf_store_byte(out.addr + q + 2, v & 0xFF)
Some(q + 3)
}
}
}
// ---- inline-WAT primitives ----
///|
fn buf_load_byte(addr : Int) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.load8_u)
///|
fn buf_store_byte(addr : Int, value : Int) -> Unit =
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8)
///|
/// SIMD base64 encode of `chunks` × 12 input bytes → `chunks` × 16 output
/// bytes. Identical body to `encode_simd_chunks` in `base64/base64_wasm.mbt`
/// but takes raw i32 addresses and assumes src_off / dst_off = 0.
fn buf_base64_encode(src_addr : Int, dst_addr : Int, chunks : Int) -> Unit =
#|(func (param i32) (param i32) (param i32) (local i32) (local i32) (local i32) (local v128) (local v128) (local v128) i32.const 0 local.set 4 i32.const 0 local.set 5 i32.const 0 local.set 3 block loop local.get 3 local.get 2 i32.ge_s br_if 1 local.get 0 local.get 4 i32.add v128.load local.tee 6 local.get 6 i8x16.shuffle 1 0 2 1 4 3 5 4 7 6 8 7 10 9 11 10 local.set 6 local.get 6 i32.const 10 i16x8.shr_u i32.const 63 i32x4.splat v128.and local.set 7 local.get 6 i32.const 4 i16x8.shr_u i32.const 8 i16x8.shl i32.const 16128 i32x4.splat v128.and local.get 7 v128.or local.set 7 local.get 6 i32.const 6 i16x8.shr_u i32.const 4128768 i32x4.splat v128.and local.get 7 v128.or local.set 7 local.get 6 i32.const 8 i16x8.shl i32.const 1056964608 i32x4.splat v128.and local.get 7 v128.or local.set 7 local.get 7 i32.const 65 i8x16.splat i8x16.add local.set 8 local.get 8 local.get 7 i32.const 26 i8x16.splat i8x16.ge_s i32.const 6 i8x16.splat v128.and i8x16.add local.set 8 local.get 8 local.get 7 i32.const 52 i8x16.splat i8x16.ge_s i32.const 75 i8x16.splat v128.and i8x16.sub local.set 8 local.get 8 local.get 7 i32.const 62 i8x16.splat i8x16.ge_s i32.const 15 i8x16.splat v128.and i8x16.sub local.set 8 local.get 8 local.get 7 i32.const 63 i8x16.splat i8x16.ge_s i32.const 3 i8x16.splat v128.and i8x16.add local.set 8 local.get 1 local.get 5 i32.add local.get 8 v128.store local.get 4 i32.const 12 i32.add local.set 4 local.get 5 i32.const 16 i32.add local.set 5 local.get 3 i32.const 1 i32.add local.set 3 br 0 end end)
///|
/// SIMD base64 decode of `chunks` × 16 input ASCII bytes → `chunks` × 12
/// decoded bytes. Same body as `decode_simd_chunks` in `base64/base64_wasm.mbt`;
/// returns 0 on success, non-zero if any chunk contained an out-of-alphabet
/// byte (caller falls back to scalar in that case).
fn buf_base64_decode(src_addr : Int, dst_addr : Int, chunks : Int) -> Int =
#|(func (param i32) (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) (local v128) (local v128) (local v128) (local v128) (local v128) (local v128) i32.const 0 local.set 4 i32.const 0 local.set 5 i32.const 0 local.set 3 i32.const 0 local.set 6 block loop local.get 3 local.get 2 i32.ge_s br_if 1 local.get 0 local.get 4 i32.add v128.load local.set 7 local.get 7 i32.const 65 i8x16.splat i8x16.ge_s local.get 7 i32.const 90 i8x16.splat i8x16.le_s v128.and local.set 8 local.get 7 i32.const 97 i8x16.splat i8x16.ge_s local.get 7 i32.const 122 i8x16.splat i8x16.le_s v128.and local.set 9 local.get 7 i32.const 48 i8x16.splat i8x16.ge_s local.get 7 i32.const 57 i8x16.splat i8x16.le_s v128.and local.set 10 local.get 7 i32.const 43 i8x16.splat i8x16.eq local.set 11 local.get 7 i32.const 47 i8x16.splat i8x16.eq local.set 12 local.get 8 local.get 9 v128.or local.get 10 v128.or local.get 11 v128.or local.get 12 v128.or i8x16.bitmask i32.const 65535 i32.ne if local.get 6 i32.const 1 i32.or local.set 6 end local.get 7 local.get 8 i32.const 191 i8x16.splat v128.and i8x16.add local.get 9 i32.const 185 i8x16.splat v128.and i8x16.add local.get 10 i32.const 4 i8x16.splat v128.and i8x16.add local.get 11 i32.const 19 i8x16.splat v128.and i8x16.add local.get 12 i32.const 16 i8x16.splat v128.and i8x16.add local.set 7 local.get 7 i32.const 2 i32x4.shl i32.const 252 i32x4.splat v128.and local.get 7 i32.const 12 i32x4.shr_u i32.const 3 i32x4.splat v128.and v128.or local.get 7 i32.const 4 i32x4.shl i32.const 61440 i32x4.splat v128.and v128.or local.get 7 i32.const 10 i32x4.shr_u i32.const 3840 i32x4.splat v128.and v128.or local.get 7 i32.const 6 i32x4.shl i32.const 12582912 i32x4.splat v128.and v128.or local.get 7 i32.const 8 i32x4.shr_u i32.const 4128768 i32x4.splat v128.and v128.or local.tee 7 local.get 7 i8x16.shuffle 0 1 2 4 5 6 8 9 10 12 13 14 0 1 2 4 local.set 7 local.get 1 local.get 5 i32.add local.get 7 v128.store local.get 4 i32.const 16 i32.add local.set 4 local.get 5 i32.const 12 i32.add local.set 5 local.get 3 i32.const 1 i32.add local.set 3 br 0 end end local.get 6)
// ---- byte bulk ops ----
///|
/// SIMD popcount. Count of `1` bits across all bytes in the buffer.
pub fn SimdBufferBytes::popcount(self : SimdBufferBytes) -> Int {
if self.len == 0 {
return 0
}
buf_popcount_bytes(self.addr, self.len)
}
///|
/// SIMD memcpy: copy `len` bytes from `src` (offset `si`) to `dst` (offset
/// `di`). Buffers may not overlap.
pub fn SimdBufferBytes::memcpy(
src : SimdBufferBytes,
si : Int,
dst : SimdBufferBytes,
di : Int,
len : Int,
) -> Unit {
if len <= 0 {
return
}
if si < 0 || di < 0 || si + len > src.len || di + len > dst.len {
abort("SimdBufferBytes.memcpy: range out of bounds")
}
buf_memcpy_bytes(src.addr, si, dst.addr, di, len)
}
///|
/// SIMD memset: write `value` to `len` bytes starting at `off`.
pub fn SimdBufferBytes::memset(
self : SimdBufferBytes,
off : Int,
len : Int,
value : Byte,
) -> Unit {
if len <= 0 {
return
}
if off < 0 || off + len > self.len {
abort("SimdBufferBytes.memset: range out of bounds")
}
buf_memset_bytes(self.addr, off, len, value.to_int())
}
///|
/// SIMD byte equality of the first `len` bytes of both buffers.
pub fn SimdBufferBytes::equal(
a : SimdBufferBytes,
b : SimdBufferBytes,
len : Int,
) -> Bool {
if len <= 0 {
return true
}
if len > a.len || len > b.len {
abort("SimdBufferBytes.equal: len exceeds buffer")
}
buf_equal_bytes(a.addr, b.addr, len) != 0
}
///|
/// SIMD find first byte equal to `needle`. Returns -1 if not present.
pub fn SimdBufferBytes::find_byte(self : SimdBufferBytes, needle : Byte) -> Int {
if self.len == 0 {
return -1
}
buf_find_byte(self.addr, self.len, needle.to_int())
}
///|
/// SIMD count of bytes equal to `needle`.
pub fn SimdBufferBytes::count_byte(
self : SimdBufferBytes,
needle : Byte,
) -> Int {
if self.len == 0 {
return 0
}
buf_count_byte(self.addr, self.len, needle.to_int())
}
///|
/// `true` iff every byte is < 0x80.
pub fn SimdBufferBytes::is_ascii(self : SimdBufferBytes) -> Bool {
if self.len == 0 {
return true
}
buf_is_ascii(self.addr, self.len) != 0
}
///|
/// In-place ASCII A-Z → a-z.
pub fn SimdBufferBytes::to_lower_ascii(self : SimdBufferBytes) -> Unit {
if self.len == 0 {
return
}
buf_to_lower_ascii(self.addr, self.len)
}
// ---- byte-wise binary arithmetic (Tier 1 — 0.3.0) ----
///|
/// Element-wise byte add (mod 256): `out[i] = (a[i] + b[i]) & 0xFF`.
/// Three buffers must have the same length.
pub fn SimdBufferBytes::byte_add(
a : SimdBufferBytes,
b : SimdBufferBytes,
out : SimdBufferBytes,
) -> Unit {
if a.len != b.len || a.len != out.len {
abort("SimdBufferBytes.byte_add: length mismatch")
}
if a.len == 0 {
return
}
buf_byte_add(a.addr, b.addr, out.addr, a.len)
}
///|
/// Element-wise byte subtract (mod 256): `out[i] = (a[i] - b[i]) & 0xFF`.
pub fn SimdBufferBytes::byte_sub(
a : SimdBufferBytes,
b : SimdBufferBytes,
out : SimdBufferBytes,
) -> Unit {
if a.len != b.len || a.len != out.len {
abort("SimdBufferBytes.byte_sub: length mismatch")
}
if a.len == 0 {
return
}
buf_byte_sub(a.addr, b.addr, out.addr, a.len)
}
///|
/// Element-wise byte average with rounding: `out[i] = (a[i] + b[i] + 1) / 2`.
/// Maps directly to `i8x16.avgr_u`. Useful for box blur, PNG Average filter,
/// midpoint color computation.
pub fn SimdBufferBytes::byte_avg(
a : SimdBufferBytes,
b : SimdBufferBytes,
out : SimdBufferBytes,
) -> Unit {
if a.len != b.len || a.len != out.len {
abort("SimdBufferBytes.byte_avg: length mismatch")
}
if a.len == 0 {
return
}
buf_byte_avg(a.addr, b.addr, out.addr, a.len)
}
///|
/// Saturating unsigned byte add: `out[i] = min(255, a[i] + b[i])`.
/// `i8x16.add_sat_u`. Useful for accumulator-style ops (AA coverage,
/// audio mixing) where overflow should clamp instead of wrap.
pub fn SimdBufferBytes::sat_add(
a : SimdBufferBytes,
b : SimdBufferBytes,
out : SimdBufferBytes,
) -> Unit {
if a.len != b.len || a.len != out.len {
abort("SimdBufferBytes.sat_add: length mismatch")
}
if a.len == 0 {
return
}
buf_sat_add(a.addr, b.addr, out.addr, a.len)
}
///|
/// Saturating unsigned byte subtract: `out[i] = max(0, a[i] - b[i])`.
/// `i8x16.sub_sat_u`.
pub fn SimdBufferBytes::sat_sub(
a : SimdBufferBytes,
b : SimdBufferBytes,
out : SimdBufferBytes,
) -> Unit {
if a.len != b.len || a.len != out.len {
abort("SimdBufferBytes.sat_sub: length mismatch")
}
if a.len == 0 {
return
}
buf_sat_sub(a.addr, b.addr, out.addr, a.len)
}
///|
/// Clamp each byte to `[lo, hi]`. SIMD chains `i8x16.max_u(lo)` then
/// `i8x16.min_u(hi)`. Useful for image normalization, audio limiting.
pub fn SimdBufferBytes::clamp(
self : SimdBufferBytes,
lo : Byte,
hi : Byte,
out : SimdBufferBytes,
) -> Unit {
if self.len != out.len {
abort("SimdBufferBytes.clamp: length mismatch")
}
if self.len == 0 {
return
}
buf_clamp_u8(self.addr, lo.to_int(), hi.to_int(), out.addr, self.len)
}
///|
/// Subtract a shifted view of the same buffer from itself: `out[i] = (src[i] -
/// src[i - stride]) & 0xFF` for `i >= stride`, with `out[i] = src[i]` for
/// `i < stride`. This is PNG's Sub filter pattern; the SIMD body reads two
/// `v128.load`s offset by `stride`, with a scalar head + tail.
pub fn SimdBufferBytes::byte_sub_offset(
src : SimdBufferBytes,
stride : Int,
out : SimdBufferBytes,
) -> Unit {
if src.len != out.len {
abort("SimdBufferBytes.byte_sub_offset: length mismatch")
}
if stride <= 0 {
abort("SimdBufferBytes.byte_sub_offset: stride must be >= 1")
}
let n = src.len
if n == 0 {
return
}
// Head: i < stride → out[i] = src[i].
let head = if stride > n { n } else { stride }
for i in 0..<head {
buf_store_byte(out.addr + i, buf_load_byte(src.addr + i))
}
if n > stride {
buf_byte_sub_offset(src.addr, stride, out.addr, n)
}
}
///|
/// In-place ASCII a-z → A-Z.
pub fn SimdBufferBytes::to_upper_ascii(self : SimdBufferBytes) -> Unit {
if self.len == 0 {
return
}
buf_to_upper_ascii(self.addr, self.len)
}
///|
/// Structural UTF-8 validation (per `scalar_validate_utf8_from`'s recipe).
/// SIMD ASCII fast-path skips all-ASCII 16-byte chunks; scalar state machine
/// takes over at the first chunk that contains any high-bit byte.
pub fn SimdBufferBytes::validate_utf8(self : SimdBufferBytes) -> Bool {
let n = self.len
let start = if n >= 16 { buf_first_non_ascii_chunk(self.addr, n) } else { 0 }
self.validate_utf8_scalar(start)
}
///|
/// Strict UTF-8 validation (RFC 3629): also rejects overlong encodings,
/// surrogates, and code points > U+10FFFF. SIMD ASCII skip + strict scalar.
pub fn SimdBufferBytes::validate_utf8_strict(self : SimdBufferBytes) -> Bool {
let n = self.len
let start = if n >= 16 { buf_first_non_ascii_chunk(self.addr, n) } else { 0 }
self.validate_utf8_strict_scalar(start)
}
///|
/// Strict scalar UTF-8 walk from `start` (tight first-continuation ranges per
/// leading byte). Reads via `buf_load_byte` on the linear-memory buffer.
fn SimdBufferBytes::validate_utf8_strict_scalar(
self : SimdBufferBytes,
start : Int,
) -> Bool {
let n = self.len
let mut i = start
while i < n {
let b = buf_load_byte(self.addr + i)
if b < 0x80 {
i = i + 1
} else if b < 0xC2 {
return false
} else if b < 0xE0 {
if i + 1 >= n || (buf_load_byte(self.addr + i + 1) & 0xC0) != 0x80 {
return false
}
i = i + 2
} else if b < 0xF0 {
if i + 2 >= n {
return false
}
let c1 = buf_load_byte(self.addr + i + 1)
let c2 = buf_load_byte(self.addr + i + 2)
let lo = if b == 0xE0 { 0xA0 } else { 0x80 }
let hi = if b == 0xED { 0x9F } else { 0xBF }
if c1 < lo || c1 > hi || c2 < 0x80 || c2 > 0xBF {
return false
}
i = i + 3
} else if b < 0xF5 {
if i + 3 >= n {
return false
}
let c1 = buf_load_byte(self.addr + i + 1)
let c2 = buf_load_byte(self.addr + i + 2)
let c3 = buf_load_byte(self.addr + i + 3)
let lo = if b == 0xF0 { 0x90 } else { 0x80 }
let hi = if b == 0xF4 { 0x8F } else { 0xBF }
if c1 < lo || c1 > hi || c2 < 0x80 || c2 > 0xBF || c3 < 0x80 || c3 > 0xBF {
return false
}
i = i + 4
} else {
return false
}
}
true
}
///|
/// Scalar UTF-8 structural validation from `start` to end-of-buffer.
/// Inlined from `@internal.scalar_validate_utf8_from` so we don't depend on
/// FixedArray storage.
fn SimdBufferBytes::validate_utf8_scalar(
self : SimdBufferBytes,
start : Int,
) -> Bool {
let n = self.len
let mut i = start
while i < n {
let b = buf_load_byte(self.addr + i)
if b < 0x80 {
i = i + 1
} else if b < 0xC2 {
return false
} else if b < 0xE0 {
if i + 1 >= n {
return false
}
if (buf_load_byte(self.addr + i + 1) & 0xC0) != 0x80 {
return false
}
i = i + 2
} else if b < 0xF0 {
if i + 2 >= n {
return false
}
if (buf_load_byte(self.addr + i + 1) & 0xC0) != 0x80 {
return false
}
if (buf_load_byte(self.addr + i + 2) & 0xC0) != 0x80 {
return false
}
i = i + 3
} else if b < 0xF5 {
if i + 3 >= n {
return false
}
if (buf_load_byte(self.addr + i + 1) & 0xC0) != 0x80 {
return false
}
if (buf_load_byte(self.addr + i + 2) & 0xC0) != 0x80 {
return false
}
if (buf_load_byte(self.addr + i + 3) & 0xC0) != 0x80 {
return false
}
i = i + 4
} else {
return false
}
}
true
}
// ---- adler32 ----
///|
/// Adler-32 weight vector [16, 15, ..., 1]. Allocated once at module init in
/// linear memory; the address is reused by every adler32 call. Costs one
/// `memory.grow` page (~64 KiB) per process.
let adler_weights_addr : Int = {
let buf = SimdBufferBytes::make(16)
for i in 0..<16 {
buf_store_byte(buf.addr + i, 16 - i)
}
buf.addr
}
///|
/// SIMD Adler-32 (RFC 1950 / zlib NMAX=5552). Returns the standard 32-bit
/// packed checksum.
pub fn SimdBufferBytes::adler32(self : SimdBufferBytes) -> UInt {
let mod_adler : UInt = 65521
let n = self.len
let mut a : UInt = 1
let mut b : UInt = 0
let mut i = 0
while i < n {
let remaining = n - i
let block = if remaining > 5552 { 5552 } else { remaining }
let block_end = i + block
let simd_chunks = block / 16
if simd_chunks > 0 {
let packed = buf_adler32_chunks(
self.addr,
i,
simd_chunks,
adler_weights_addr,
a.reinterpret_as_int(),
b.reinterpret_as_int(),
)
a = (packed & 0xFFFFFFFFL).to_int().reinterpret_as_uint()
b = (packed.reinterpret_as_uint64() >> 32).to_int().reinterpret_as_uint()
i = i + simd_chunks * 16
}
while i < block_end {
a = a + buf_load_byte(self.addr + i).reinterpret_as_uint()
b = b + a
i = i + 1
}
a = a % mod_adler
b = b % mod_adler
}
(b << 16) | a
}
// ---- inline-WAT primitives for the byte ops (identical bodies to the
// FixedArray versions in simd_wasm_bytes.mbt) ----
///|
fn buf_popcount_bytes(addr : Int, len : Int) -> Int =
#|(func (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local v128) local.get 1 i32.const 4 i32.shr_s i32.const 4 i32.shl local.set 3 block loop local.get 2 local.get 3 i32.ge_s br_if 1 local.get 0 local.get 2 i32.add v128.load i8x16.popcnt i16x8.extadd_pairwise_i8x16_u i32x4.extadd_pairwise_i16x8_u local.set 5 local.get 4 local.get 5 i32x4.extract_lane 0 i32.add local.get 5 i32x4.extract_lane 1 i32.add local.get 5 i32x4.extract_lane 2 i32.add local.get 5 i32x4.extract_lane 3 i32.add local.set 4 local.get 2 i32.const 16 i32.add local.set 2 br 0 end end block loop local.get 2 local.get 1 i32.ge_s br_if 1 local.get 4 local.get 0 local.get 2 i32.add i32.load8_u i32.popcnt i32.add local.set 4 local.get 2 i32.const 1 i32.add local.set 2 br 0 end end local.get 4)
///|
fn buf_memcpy_bytes(
src_addr : Int,
si : Int,
dst_addr : Int,
di : Int,
len : Int,
) -> Unit =
#|(func (param i32) (param i32) (param i32) (param i32) (param i32) (local i32) (local i32) local.get 4 i32.const 4 i32.shr_s i32.const 4 i32.shl local.set 6 block loop local.get 5 local.get 6 i32.ge_s br_if 1 local.get 2 local.get 3 i32.add local.get 5 i32.add local.get 0 local.get 1 i32.add local.get 5 i32.add v128.load v128.store local.get 5 i32.const 16 i32.add local.set 5 br 0 end end block loop local.get 5 local.get 4 i32.ge_s br_if 1 local.get 2 local.get 3 i32.add local.get 5 i32.add local.get 0 local.get 1 i32.add local.get 5 i32.add i32.load8_u i32.store8 local.get 5 i32.const 1 i32.add local.set 5 br 0 end end)
///|
fn buf_memset_bytes(dst_addr : Int, off : Int, len : Int, value : Int) -> Unit =
#|(func (param i32) (param i32) (param i32) (param i32) (local i32) (local i32) (local v128) local.get 3 i8x16.splat local.set 6 local.get 2 i32.const 4 i32.shr_s i32.const 4 i32.shl local.set 5 block loop local.get 4 local.get 5 i32.ge_s br_if 1 local.get 0 local.get 1 i32.add local.get 4 i32.add local.get 6 v128.store local.get 4 i32.const 16 i32.add local.set 4 br 0 end end block loop local.get 4 local.get 2 i32.ge_s br_if 1 local.get 0 local.get 1 i32.add local.get 4 i32.add local.get 3 i32.store8 local.get 4 i32.const 1 i32.add local.set 4 br 0 end end)
///|
fn buf_equal_bytes(a : Int, b : Int, len : Int) -> Int =
#|(func (param i32) (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) local.get 2 i32.const 4 i32.shr_s i32.const 4 i32.shl local.set 4 block loop local.get 3 local.get 4 i32.ge_s br_if 1 local.get 5 local.get 0 local.get 3 i32.add v128.load local.get 1 local.get 3 i32.add v128.load i8x16.eq i8x16.bitmask i32.const 65535 i32.ne i32.or local.set 5 local.get 3 i32.const 16 i32.add local.set 3 br 0 end end block loop local.get 3 local.get 2 i32.ge_s br_if 1 local.get 5 local.get 0 local.get 3 i32.add i32.load8_u local.get 1 local.get 3 i32.add i32.load8_u i32.ne i32.or local.set 5 local.get 3 i32.const 1 i32.add local.set 3 br 0 end end local.get 5 i32.eqz)
///|
fn buf_find_byte(addr : Int, len : Int, needle : Int) -> Int =
#|(func (param i32) (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) (local v128) local.get 2 i8x16.splat local.set 7 local.get 1 i32.const 4 i32.shr_s i32.const 4 i32.shl local.set 4 i32.const -1 local.set 5 block loop local.get 3 local.get 4 i32.ge_s br_if 1 local.get 0 local.get 3 i32.add v128.load local.get 7 i8x16.eq i8x16.bitmask local.set 6 local.get 3 local.get 6 i32.ctz i32.add local.get 5 local.get 5 i32.const -1 i32.eq local.get 6 i32.mul select local.set 5 local.get 3 i32.const 16 i32.add local.set 3 br 0 end end block loop local.get 3 local.get 1 i32.ge_s br_if 1 local.get 3 local.get 5 local.get 5 i32.const -1 i32.eq local.get 0 local.get 3 i32.add i32.load8_u local.get 2 i32.eq i32.and select local.set 5 local.get 3 i32.const 1 i32.add local.set 3 br 0 end end local.get 5)
///|
fn buf_count_byte(addr : Int, len : Int, needle : Int) -> Int =
#|(func (param i32) (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local v128) local.get 2 i8x16.splat local.set 6 local.get 1 i32.const 4 i32.shr_s i32.const 4 i32.shl local.set 4 block loop local.get 3 local.get 4 i32.ge_s br_if 1 local.get 5 local.get 0 local.get 3 i32.add v128.load local.get 6 i8x16.eq i8x16.bitmask i32.popcnt i32.add local.set 5 local.get 3 i32.const 16 i32.add local.set 3 br 0 end end block loop local.get 3 local.get 1 i32.ge_s br_if 1 local.get 5 local.get 0 local.get 3 i32.add i32.load8_u local.get 2 i32.eq i32.add local.set 5 local.get 3 i32.const 1 i32.add local.set 3 br 0 end end local.get 5)
///|
fn buf_first_non_ascii_chunk(addr : Int, len : Int) -> Int =
#|(func (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) (local i32) local.get 1 i32.const 4 i32.shr_s i32.const 4 i32.shl local.set 3 local.get 3 local.set 4 block loop local.get 2 local.get 3 i32.ge_s br_if 1 local.get 0 local.get 2 i32.add v128.load i8x16.bitmask local.set 5 local.get 2 local.get 4 local.get 4 local.get 3 i32.eq local.get 5 i32.mul select local.set 4 local.get 2 i32.const 16 i32.add local.set 2 br 0 end end local.get 4)
///|
fn buf_is_ascii(addr : Int, len : Int) -> Int =
#|(func (param i32) (param i32) (result i32) (local i32) (local i32) (local i32) local.get 1 i32.const 4 i32.shr_s i32.const 4 i32.shl local.set 3 block loop local.get 2 local.get 3 i32.ge_s br_if 1 local.get 4 local.get 0 local.get 2 i32.add v128.load i8x16.bitmask i32.or local.set 4 local.get 2 i32.const 16 i32.add local.set 2 br 0 end end block loop local.get 2 local.get 1 i32.ge_s br_if 1 local.get 4 local.get 0 local.get 2 i32.add i32.load8_u i32.const 128 i32.and i32.or local.set 4 local.get 2 i32.const 1 i32.add local.set 2 br 0 end end local.get 4 i32.eqz)
///|
fn buf_to_lower_ascii(addr : Int, len : Int) -> Unit =
#|(func (param i32) (param i32) (local i32) (local i32) (local v128) (local v128) (local v128) (local v128) i32.const 65 i8x16.splat local.set 4 i32.const 90 i8x16.splat local.set 5 i32.const 32 i8x16.splat local.set 6 local.get 1 i32.const 4 i32.shr_s i32.const 4 i32.shl local.set 3 block loop local.get 2 local.get 3 i32.ge_s br_if 1 local.get 0 local.get 2 i32.add v128.load local.set 7 local.get 0 local.get 2 i32.add local.get 7 local.get 4 i8x16.ge_s local.get 7 local.get 5 i8x16.le_s v128.and local.get 6 v128.and local.get 7 i8x16.add v128.store local.get 2 i32.const 16 i32.add local.set 2 br 0 end end block loop local.get 2 local.get 1 i32.ge_s br_if 1 local.get 0 local.get 2 i32.add local.get 0 local.get 2 i32.add i32.load8_u local.get 0 local.get 2 i32.add i32.load8_u i32.const 65 i32.ge_s local.get 0 local.get 2 i32.add i32.load8_u i32.const 90 i32.le_s i32.and i32.const 32 i32.mul i32.add i32.store8 local.get 2 i32.const 1 i32.add local.set 2 br 0 end end)
///|
fn buf_to_upper_ascii(addr : Int, len : Int) -> Unit =
#|(func (param i32) (param i32) (local i32) (local i32) (local v128) (local v128) (local v128) (local v128) i32.const 97 i8x16.splat local.set 4 i32.const 122 i8x16.splat local.set 5 i32.const 32 i8x16.splat local.set 6 local.get 1 i32.const 4 i32.shr_s i32.const 4 i32.shl local.set 3 block loop local.get 2 local.get 3 i32.ge_s br_if 1 local.get 0 local.get 2 i32.add v128.load local.set 7 local.get 0 local.get 2 i32.add local.get 7 local.get 7 local.get 4 i8x16.ge_s local.get 7 local.get 5 i8x16.le_s v128.and local.get 6 v128.and i8x16.sub v128.store local.get 2 i32.const 16 i32.add local.set 2 br 0 end end block loop local.get 2 local.get 1 i32.ge_s br_if 1 local.get 0 local.get 2 i32.add local.get 0 local.get 2 i32.add i32.load8_u local.get 0 local.get 2 i32.add i32.load8_u i32.const 97 i32.ge_s local.get 0 local.get 2 i32.add i32.load8_u i32.const 122 i32.le_s i32.and i32.const 32 i32.mul i32.sub i32.store8 local.get 2 i32.const 1 i32.add local.set 2 br 0 end end)
///|
fn buf_adler32_chunks(
data_addr : Int,
offset : Int,
count16 : Int,
weights_addr : Int,
a : Int,
b : Int,
) -> Int64 =
#|(func (param i32) (param i32) (param i32) (param i32) (param i32) (param i32) (result i64) (local i32) (local i32) (local i32) (local i32) (local v128) (local v128) (local v128) local.get 3 v128.load local.set 10 local.get 1 local.get 2 i32.const 4 i32.shl i32.add local.set 7 local.get 1 local.set 6 block loop local.get 6 local.get 7 i32.ge_s br_if 1 local.get 0 local.get 6 i32.add v128.load local.set 11 local.get 11 i16x8.extadd_pairwise_i8x16_u i32x4.extadd_pairwise_i16x8_u local.set 12 local.get 12 i32x4.extract_lane 0 local.get 12 i32x4.extract_lane 1 i32.add local.get 12 i32x4.extract_lane 2 i32.add local.get 12 i32x4.extract_lane 3 i32.add local.set 8 local.get 11 local.get 10 i16x8.extmul_low_i8x16_u i32x4.extadd_pairwise_i16x8_u local.get 11 local.get 10 i16x8.extmul_high_i8x16_u i32x4.extadd_pairwise_i16x8_u i32x4.add local.set 12 local.get 12 i32x4.extract_lane 0 local.get 12 i32x4.extract_lane 1 i32.add local.get 12 i32x4.extract_lane 2 i32.add local.get 12 i32x4.extract_lane 3 i32.add local.set 9 local.get 5 local.get 4 i32.const 4 i32.shl i32.add local.get 9 i32.add local.set 5 local.get 4 local.get 8 i32.add local.set 4 local.get 6 i32.const 16 i32.add local.set 6 br 0 end end local.get 5 i64.extend_i32_u i64.const 32 i64.shl local.get 4 i64.extend_i32_u i64.or)
// ---- byte binary arithmetic (Tier 1 — 0.3.0) ----
//
// All six functions below share the same inline-WAT skeleton:
// * Locals: 4 = vec_end (= len & ~15), 5 = i
// * 16-byte chunk: v128.load a, v128.load b, OP, v128.store out
// * Scalar tail: byte load, byte op, byte store
// Only the SIMD op (i8x16.{add,sub,avgr_u,add_sat_u,sub_sat_u}) and the
// scalar tail equivalent differ.
///|
fn buf_byte_add(a : Int, b : Int, out : Int, len : Int) -> Unit =
#|(func (param i32) (param i32) (param i32) (param i32) (local i32) (local i32) local.get 3 i32.const 4 i32.shr_s i32.const 4 i32.shl local.set 4 block loop local.get 5 local.get 4 i32.ge_s br_if 1 local.get 2 local.get 5 i32.add local.get 0 local.get 5 i32.add v128.load local.get 1 local.get 5 i32.add v128.load i8x16.add v128.store local.get 5 i32.const 16 i32.add local.set 5 br 0 end end block loop local.get 5 local.get 3 i32.ge_s br_if 1 local.get 2 local.get 5 i32.add local.get 0 local.get 5 i32.add i32.load8_u local.get 1 local.get 5 i32.add i32.load8_u i32.add i32.store8 local.get 5 i32.const 1 i32.add local.set 5 br 0 end end)
///|
fn buf_byte_sub(a : Int, b : Int, out : Int, len : Int) -> Unit =
#|(func (param i32) (param i32) (param i32) (param i32) (local i32) (local i32) local.get 3 i32.const 4 i32.shr_s i32.const 4 i32.shl local.set 4 block loop local.get 5 local.get 4 i32.ge_s br_if 1 local.get 2 local.get 5 i32.add local.get 0 local.get 5 i32.add v128.load local.get 1 local.get 5 i32.add v128.load i8x16.sub v128.store local.get 5 i32.const 16 i32.add local.set 5 br 0 end end block loop local.get 5 local.get 3 i32.ge_s br_if 1 local.get 2 local.get 5 i32.add local.get 0 local.get 5 i32.add i32.load8_u local.get 1 local.get 5 i32.add i32.load8_u i32.sub i32.store8 local.get 5 i32.const 1 i32.add local.set 5 br 0 end end)
///|
fn buf_byte_avg(a : Int, b : Int, out : Int, len : Int) -> Unit =
#|(func (param i32) (param i32) (param i32) (param i32) (local i32) (local i32) local.get 3 i32.const 4 i32.shr_s i32.const 4 i32.shl local.set 4 block loop local.get 5 local.get 4 i32.ge_s br_if 1 local.get 2 local.get 5 i32.add local.get 0 local.get 5 i32.add v128.load local.get 1 local.get 5 i32.add v128.load i8x16.avgr_u v128.store local.get 5 i32.const 16 i32.add local.set 5 br 0 end end block loop local.get 5 local.get 3 i32.ge_s br_if 1 local.get 2 local.get 5 i32.add local.get 0 local.get 5 i32.add i32.load8_u local.get 1 local.get 5 i32.add i32.load8_u i32.add i32.const 1 i32.add i32.const 1 i32.shr_u i32.store8 local.get 5 i32.const 1 i32.add local.set 5 br 0 end end)
///|
fn buf_sat_add(a : Int, b : Int, out : Int, len : Int) -> Unit =
#|(func (param i32) (param i32) (param i32) (param i32) (local i32) (local i32) (local i32) local.get 3 i32.const 4 i32.shr_s i32.const 4 i32.shl local.set 4 block loop local.get 5 local.get 4 i32.ge_s br_if 1 local.get 2 local.get 5 i32.add local.get 0 local.get 5 i32.add v128.load local.get 1 local.get 5 i32.add v128.load i8x16.add_sat_u v128.store local.get 5 i32.const 16 i32.add local.set 5 br 0 end end block loop local.get 5 local.get 3 i32.ge_s br_if 1 local.get 0 local.get 5 i32.add i32.load8_u local.get 1 local.get 5 i32.add i32.load8_u i32.add local.tee 6 i32.const 255 i32.const 255 local.get 6 i32.gt_s select local.set 6 local.get 2 local.get 5 i32.add local.get 6 i32.store8 local.get 5 i32.const 1 i32.add local.set 5 br 0 end end)
///|
fn buf_sat_sub(a : Int, b : Int, out : Int, len : Int) -> Unit =
#|(func (param i32) (param i32) (param i32) (param i32) (local i32) (local i32) (local i32) local.get 3 i32.const 4 i32.shr_s i32.const 4 i32.shl local.set 4 block loop local.get 5 local.get 4 i32.ge_s br_if 1 local.get 2 local.get 5 i32.add local.get 0 local.get 5 i32.add v128.load local.get 1 local.get 5 i32.add v128.load i8x16.sub_sat_u v128.store local.get 5 i32.const 16 i32.add local.set 5 br 0 end end block loop local.get 5 local.get 3 i32.ge_s br_if 1 local.get 0 local.get 5 i32.add i32.load8_u local.get 1 local.get 5 i32.add i32.load8_u i32.sub local.set 6 local.get 6 i32.const 0 local.get 6 i32.const 0 i32.ge_s select local.set 6 local.get 2 local.get 5 i32.add local.get 6 i32.store8 local.get 5 i32.const 1 i32.add local.set 5 br 0 end end)
///|
/// Clamp: `out[i] = min(hi, max(lo, src[i]))` byte-wise.
/// Locals: 5 = vec_end, 6 = i, 7 = byte_tmp, 8 = lo_splat (v128), 9 = hi_splat (v128).
fn buf_clamp_u8(src : Int, lo : Int, hi : Int, out : Int, len : Int) -> Unit =
#|(func (param i32) (param i32) (param i32) (param i32) (param i32) (local i32) (local i32) (local i32) (local v128) (local v128) local.get 1 i8x16.splat local.set 8 local.get 2 i8x16.splat local.set 9 local.get 4 i32.const 4 i32.shr_s i32.const 4 i32.shl local.set 5 block loop local.get 6 local.get 5 i32.ge_s br_if 1 local.get 3 local.get 6 i32.add local.get 0 local.get 6 i32.add v128.load local.get 8 i8x16.max_u local.get 9 i8x16.min_u v128.store local.get 6 i32.const 16 i32.add local.set 6 br 0 end end block loop local.get 6 local.get 4 i32.ge_s br_if 1 local.get 0 local.get 6 i32.add i32.load8_u local.set 7 local.get 7 local.get 1 local.get 7 local.get 1 i32.ge_s select local.set 7 local.get 7 local.get 2 local.get 7 local.get 2 i32.le_s select local.set 7 local.get 3 local.get 6 i32.add local.get 7 i32.store8 local.get 6 i32.const 1 i32.add local.set 6 br 0 end end)
///|
/// Shifted-self subtraction (PNG Sub filter): for i in stride..<len,
/// `out[i] = (src[i] - src[i - stride]) & 0xFF`.
/// Locals: 4 = vec_end (= ((len - stride) & ~15) + stride), 5 = i.
fn buf_byte_sub_offset(src : Int, stride : Int, out : Int, len : Int) -> Unit =
#|(func (param i32) (param i32) (param i32) (param i32) (local i32) (local i32) local.get 3 local.get 1 i32.sub i32.const 4 i32.shr_s i32.const 4 i32.shl local.get 1 i32.add local.set 4 local.get 1 local.set 5 block loop local.get 5 local.get 4 i32.ge_s br_if 1 local.get 2 local.get 5 i32.add local.get 0 local.get 5 i32.add v128.load local.get 0 local.get 5 i32.add local.get 1 i32.sub v128.load i8x16.sub v128.store local.get 5 i32.const 16 i32.add local.set 5 br 0 end end block loop local.get 5 local.get 3 i32.ge_s br_if 1 local.get 2 local.get 5 i32.add local.get 0 local.get 5 i32.add i32.load8_u local.get 0 local.get 5 i32.add local.get 1 i32.sub i32.load8_u i32.sub i32.store8 local.get 5 i32.const 1 i32.add local.set 5 br 0 end end)
// (Import keep-alive lives in simd_buffer_imports.mbt so it applies on
// every target including js, where no other simd_buffer file is built.)