raden 2026.1.1

2D Vector Graphics 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
use cranelift_codegen::ir::condcodes::IntCC;
use cranelift_codegen::ir::types;
use cranelift_codegen::ir::{InstBuilder, MemFlags, Type};
use cranelift_frontend::FunctionBuilder;

use super::block_args;
use super::core_pipelines::emit_src_over_ag_rb_simd;

// =============================================================================
// Box パイプライン (矩形塗りつぶし専用、y ループ内蔵)
// =============================================================================

/// SrcOver 矩形専用パイプラインを構築する。
///
/// y ループを JIT 内に含み、4x SIMD アンロール (16px/反復) で処理する。
///
/// ## IR 構造
///
/// ```text
/// entry:
///   ループ不変値 (src_ag/rb_vec, inv_alpha_vec 等) を計算
///   count16 = width / 16, tail_quads = (width % 16) / 4, remainder = width % 4
///   → y_loop
///
/// y_loop(scanline_dst, y_i):
///   count16 > 0? → unroll_loop : tail_check
///
/// unroll_loop(x_dst, unroll_i):
///   16px (4x I32X4) を処理
///   → unroll_loop or tail_check
///
/// tail_check(x_dst):
///   tail_quads > 0? → tail_loop : scalar_check
///
/// tail_loop(x_dst, tail_i):
///   4px (1x I32X4) を処理
///   → tail_loop or scalar_check
///
/// scalar_check(x_dst):
///   remainder > 0? → scalar_loop : y_advance
///
/// scalar_loop(x_dst, scalar_i):
///   1px を処理
///   → scalar_loop or y_advance
///
/// y_advance:
///   next_scanline = scanline_dst + stride
///   next_y < height? → y_loop : exit
///
/// exit: return
/// ```
pub(super) fn build_src_over_box(mut bcx: FunctionBuilder, ptr_type: Type) {
    let vec_type = types::I32X4;

    let entry = bcx.create_block();
    let y_loop = bcx.create_block();
    let unroll_loop = bcx.create_block();
    let tail_check = bcx.create_block();
    let tail_loop = bcx.create_block();
    let scalar_check = bcx.create_block();
    let scalar_loop = bcx.create_block();
    let y_advance = bcx.create_block();
    let exit = bcx.create_block();

    // === entry ブロック ===
    bcx.switch_to_block(entry);
    bcx.append_block_params_for_function_params(entry);
    let dst = bcx.block_params(entry)[0];
    let src_solid = bcx.block_params(entry)[1];
    let width = bcx.block_params(entry)[2];
    let height = bcx.block_params(entry)[3];
    let stride = bcx.block_params(entry)[4];

    // AG/RB 分解 (ソース、ループ不変)
    let mask_00ff00ff = bcx.ins().iconst(types::I32, 0x00FF00FFu32 as i64);
    let src_ag = bcx.ins().ushr_imm(src_solid, 8);
    let src_ag = bcx.ins().band(src_ag, mask_00ff00ff);
    let src_rb = bcx.ins().band(src_solid, mask_00ff00ff);

    // inv_alpha = 256 - src_a
    let src_a = bcx.ins().ushr_imm(src_solid, 24);
    let src_a = bcx.ins().band_imm(src_a, 0xFF);
    let c256 = bcx.ins().iconst(types::I32, 256);
    let inv_alpha = bcx.ins().isub(c256, src_a);

    // SIMD 用ループ不変ベクタ
    let src_ag_vec = bcx.ins().splat(vec_type, src_ag);
    let src_rb_vec = bcx.ins().splat(vec_type, src_rb);
    let inv_alpha_vec = bcx.ins().splat(vec_type, inv_alpha);
    let mask_vec = bcx.ins().splat(vec_type, mask_00ff00ff);

    // ループカウント (width は全スキャンラインで同じ)
    let count16 = bcx.ins().ushr_imm(width, 4);
    let tail_quads = bcx.ins().band_imm(width, 0xF);
    let tail_quads = bcx.ins().ushr_imm(tail_quads, 2);
    let remainder = bcx.ins().band_imm(width, 3);
    let zero = bcx.ins().iconst(ptr_type, 0);

    // SIMD 不変値を y_loop のブロックパラメータとして渡す。
    // Cranelift の sink 最適化が不変値をループ内に移動するのを防ぐため、
    // entry → y_loop と y_advance → y_loop の両方で同じ値を渡す。
    bcx.ins().jump(
        y_loop,
        &block_args(&[dst, zero, src_ag_vec, src_rb_vec, inv_alpha_vec, mask_vec]),
    );

    // === y_loop ブロック ===
    // ブロックパラメータでループ不変 SIMD ベクタを保持し、
    // レジスタに固定させることで y ループ先頭での再計算を防ぐ。
    bcx.append_block_param(y_loop, ptr_type); // scanline_dst
    bcx.append_block_param(y_loop, ptr_type); // y_i
    bcx.append_block_param(y_loop, vec_type); // src_ag_vec
    bcx.append_block_param(y_loop, vec_type); // src_rb_vec
    bcx.append_block_param(y_loop, vec_type); // inv_alpha_vec
    bcx.append_block_param(y_loop, vec_type); // mask_vec
    bcx.switch_to_block(y_loop);
    let scanline_dst = bcx.block_params(y_loop)[0];
    let y_i = bcx.block_params(y_loop)[1];
    let src_ag_vec = bcx.block_params(y_loop)[2];
    let src_rb_vec = bcx.block_params(y_loop)[3];
    let inv_alpha_vec = bcx.block_params(y_loop)[4];
    let mask_vec = bcx.block_params(y_loop)[5];

    let has_unroll = bcx.ins().icmp(IntCC::NotEqual, count16, zero);
    bcx.ins().brif(
        has_unroll,
        unroll_loop,
        &block_args(&[scanline_dst, zero]),
        tail_check,
        &block_args(&[scanline_dst]),
    );

    // === unroll_loop ブロック (16px/反復 = 4x I32X4) ===
    bcx.append_block_param(unroll_loop, ptr_type); // x_dst
    bcx.append_block_param(unroll_loop, ptr_type); // unroll_i
    bcx.switch_to_block(unroll_loop);
    let x_dst = bcx.block_params(unroll_loop)[0];
    let unroll_i = bcx.block_params(unroll_loop)[1];

    // チャンク 0: offset 0
    let px0 = bcx.ins().load(vec_type, MemFlags::new(), x_dst, 0);
    let r0 = emit_src_over_ag_rb_simd(
        &mut bcx,
        px0,
        src_ag_vec,
        src_rb_vec,
        inv_alpha_vec,
        mask_vec,
    );
    bcx.ins().store(MemFlags::new(), r0, x_dst, 0);

    // チャンク 1: offset 16
    let px1 = bcx.ins().load(vec_type, MemFlags::new(), x_dst, 16);
    let r1 = emit_src_over_ag_rb_simd(
        &mut bcx,
        px1,
        src_ag_vec,
        src_rb_vec,
        inv_alpha_vec,
        mask_vec,
    );
    bcx.ins().store(MemFlags::new(), r1, x_dst, 16);

    // チャンク 2: offset 32
    let px2 = bcx.ins().load(vec_type, MemFlags::new(), x_dst, 32);
    let r2 = emit_src_over_ag_rb_simd(
        &mut bcx,
        px2,
        src_ag_vec,
        src_rb_vec,
        inv_alpha_vec,
        mask_vec,
    );
    bcx.ins().store(MemFlags::new(), r2, x_dst, 32);

    // チャンク 3: offset 48
    let px3 = bcx.ins().load(vec_type, MemFlags::new(), x_dst, 48);
    let r3 = emit_src_over_ag_rb_simd(
        &mut bcx,
        px3,
        src_ag_vec,
        src_rb_vec,
        inv_alpha_vec,
        mask_vec,
    );
    bcx.ins().store(MemFlags::new(), r3, x_dst, 48);

    let sixty_four = bcx.ins().iconst(ptr_type, 64);
    let next_x_dst = bcx.ins().iadd(x_dst, sixty_four);
    let one = bcx.ins().iconst(ptr_type, 1);
    let next_i = bcx.ins().iadd(unroll_i, one);
    let cont = bcx.ins().icmp(IntCC::UnsignedLessThan, next_i, count16);
    bcx.ins().brif(
        cont,
        unroll_loop,
        &block_args(&[next_x_dst, next_i]),
        tail_check,
        &block_args(&[next_x_dst]),
    );

    // === tail_check ブロック ===
    bcx.append_block_param(tail_check, ptr_type);
    bcx.switch_to_block(tail_check);
    let x_dst = bcx.block_params(tail_check)[0];
    let has_tail = bcx.ins().icmp(IntCC::NotEqual, tail_quads, zero);
    bcx.ins().brif(
        has_tail,
        tail_loop,
        &block_args(&[x_dst, zero]),
        scalar_check,
        &block_args(&[x_dst]),
    );

    // === tail_loop ブロック (4px/反復) ===
    bcx.append_block_param(tail_loop, ptr_type); // x_dst
    bcx.append_block_param(tail_loop, ptr_type); // tail_i
    bcx.switch_to_block(tail_loop);
    let x_dst = bcx.block_params(tail_loop)[0];
    let tail_i = bcx.block_params(tail_loop)[1];

    let px = bcx.ins().load(vec_type, MemFlags::new(), x_dst, 0);
    let r = emit_src_over_ag_rb_simd(
        &mut bcx,
        px,
        src_ag_vec,
        src_rb_vec,
        inv_alpha_vec,
        mask_vec,
    );
    bcx.ins().store(MemFlags::new(), r, x_dst, 0);

    let sixteen = bcx.ins().iconst(ptr_type, 16);
    let next_x_dst = bcx.ins().iadd(x_dst, sixteen);
    let one = bcx.ins().iconst(ptr_type, 1);
    let next_ti = bcx.ins().iadd(tail_i, one);
    let cont = bcx.ins().icmp(IntCC::UnsignedLessThan, next_ti, tail_quads);
    bcx.ins().brif(
        cont,
        tail_loop,
        &block_args(&[next_x_dst, next_ti]),
        scalar_check,
        &block_args(&[next_x_dst]),
    );

    // === scalar_check ブロック ===
    bcx.append_block_param(scalar_check, ptr_type);
    bcx.switch_to_block(scalar_check);
    let x_dst = bcx.block_params(scalar_check)[0];
    let has_remainder = bcx.ins().icmp(IntCC::NotEqual, remainder, zero);
    bcx.ins().brif(
        has_remainder,
        scalar_loop,
        &block_args(&[x_dst, zero]),
        y_advance,
        &[],
    );

    // === scalar_loop ブロック (1px) ===
    bcx.append_block_param(scalar_loop, ptr_type); // x_dst
    bcx.append_block_param(scalar_loop, ptr_type); // scalar_i
    bcx.switch_to_block(scalar_loop);
    let x_dst = bcx.block_params(scalar_loop)[0];
    let scalar_i = bcx.block_params(scalar_loop)[1];

    let dst_pixel = bcx.ins().load(types::I32, MemFlags::new(), x_dst, 0);
    let dst_ag = bcx.ins().ushr_imm(dst_pixel, 8);
    let dst_ag = bcx.ins().band(dst_ag, mask_00ff00ff);
    let dst_rb = bcx.ins().band(dst_pixel, mask_00ff00ff);

    let tmp_ag = bcx.ins().imul(dst_ag, inv_alpha);
    let tmp_ag = bcx.ins().ushr_imm(tmp_ag, 8);
    let tmp_ag = bcx.ins().band(tmp_ag, mask_00ff00ff);
    let out_ag = bcx.ins().iadd(src_ag, tmp_ag);

    let tmp_rb = bcx.ins().imul(dst_rb, inv_alpha);
    let tmp_rb = bcx.ins().ushr_imm(tmp_rb, 8);
    let tmp_rb = bcx.ins().band(tmp_rb, mask_00ff00ff);
    let out_rb = bcx.ins().iadd(src_rb, tmp_rb);

    let result = bcx.ins().ishl_imm(out_ag, 8);
    let result = bcx.ins().bor(result, out_rb);

    bcx.ins().store(MemFlags::new(), result, x_dst, 0);

    let four = bcx.ins().iconst(ptr_type, 4);
    let next_x_dst = bcx.ins().iadd(x_dst, four);
    let one = bcx.ins().iconst(ptr_type, 1);
    let next_si = bcx.ins().iadd(scalar_i, one);
    let cont = bcx.ins().icmp(IntCC::UnsignedLessThan, next_si, remainder);
    bcx.ins().brif(
        cont,
        scalar_loop,
        &block_args(&[next_x_dst, next_si]),
        y_advance,
        &[],
    );

    // === y_advance ブロック ===
    bcx.switch_to_block(y_advance);
    let next_scanline = bcx.ins().iadd(scanline_dst, stride);
    let one = bcx.ins().iconst(ptr_type, 1);
    let next_y = bcx.ins().iadd(y_i, one);
    let cont = bcx.ins().icmp(IntCC::UnsignedLessThan, next_y, height);
    // SIMD 不変値をそのまま渡し戻す (レジスタ固定)
    bcx.ins().brif(
        cont,
        y_loop,
        &block_args(&[
            next_scanline,
            next_y,
            src_ag_vec,
            src_rb_vec,
            inv_alpha_vec,
            mask_vec,
        ]),
        exit,
        &[],
    );

    // === exit ブロック ===
    bcx.switch_to_block(exit);
    bcx.ins().return_(&[]);

    bcx.seal_all_blocks();
    bcx.finalize();
}

/// SrcCopy 矩形専用パイプラインを構築する。
///
/// y ループを JIT 内に含み、4x SIMD アンロール (16px/反復) で処理する。
/// SrcCopy は splat 済みベクタをストアするだけなので非常に高速。
pub(super) fn build_src_copy_box(mut bcx: FunctionBuilder, ptr_type: Type) {
    let entry = bcx.create_block();
    let y_loop = bcx.create_block();
    let unroll_loop = bcx.create_block();
    let tail_check = bcx.create_block();
    let tail_loop = bcx.create_block();
    let scalar_check = bcx.create_block();
    let scalar_loop = bcx.create_block();
    let y_advance = bcx.create_block();
    let exit = bcx.create_block();

    // === entry ブロック ===
    bcx.switch_to_block(entry);
    bcx.append_block_params_for_function_params(entry);
    let dst = bcx.block_params(entry)[0];
    let src_solid = bcx.block_params(entry)[1];
    let width = bcx.block_params(entry)[2];
    let height = bcx.block_params(entry)[3];
    let stride = bcx.block_params(entry)[4];

    let src_vec = bcx.ins().splat(types::I32X4, src_solid);

    let count16 = bcx.ins().ushr_imm(width, 4);
    let tail_quads = bcx.ins().band_imm(width, 0xF);
    let tail_quads = bcx.ins().ushr_imm(tail_quads, 2);
    let remainder = bcx.ins().band_imm(width, 3);
    let zero = bcx.ins().iconst(ptr_type, 0);

    bcx.ins().jump(y_loop, &block_args(&[dst, zero]));

    // === y_loop ブロック ===
    bcx.append_block_param(y_loop, ptr_type);
    bcx.append_block_param(y_loop, ptr_type);
    bcx.switch_to_block(y_loop);
    let scanline_dst = bcx.block_params(y_loop)[0];
    let y_i = bcx.block_params(y_loop)[1];

    let has_unroll = bcx.ins().icmp(IntCC::NotEqual, count16, zero);
    bcx.ins().brif(
        has_unroll,
        unroll_loop,
        &block_args(&[scanline_dst, zero]),
        tail_check,
        &block_args(&[scanline_dst]),
    );

    // === unroll_loop ブロック (16px/反復) ===
    bcx.append_block_param(unroll_loop, ptr_type);
    bcx.append_block_param(unroll_loop, ptr_type);
    bcx.switch_to_block(unroll_loop);
    let x_dst = bcx.block_params(unroll_loop)[0];
    let unroll_i = bcx.block_params(unroll_loop)[1];

    bcx.ins().store(MemFlags::new(), src_vec, x_dst, 0);
    bcx.ins().store(MemFlags::new(), src_vec, x_dst, 16);
    bcx.ins().store(MemFlags::new(), src_vec, x_dst, 32);
    bcx.ins().store(MemFlags::new(), src_vec, x_dst, 48);

    let sixty_four = bcx.ins().iconst(ptr_type, 64);
    let next_x_dst = bcx.ins().iadd(x_dst, sixty_four);
    let one = bcx.ins().iconst(ptr_type, 1);
    let next_i = bcx.ins().iadd(unroll_i, one);
    let cont = bcx.ins().icmp(IntCC::UnsignedLessThan, next_i, count16);
    bcx.ins().brif(
        cont,
        unroll_loop,
        &block_args(&[next_x_dst, next_i]),
        tail_check,
        &block_args(&[next_x_dst]),
    );

    // === tail_check ブロック ===
    bcx.append_block_param(tail_check, ptr_type);
    bcx.switch_to_block(tail_check);
    let x_dst = bcx.block_params(tail_check)[0];
    let has_tail = bcx.ins().icmp(IntCC::NotEqual, tail_quads, zero);
    bcx.ins().brif(
        has_tail,
        tail_loop,
        &block_args(&[x_dst, zero]),
        scalar_check,
        &block_args(&[x_dst]),
    );

    // === tail_loop ブロック (4px/反復) ===
    bcx.append_block_param(tail_loop, ptr_type);
    bcx.append_block_param(tail_loop, ptr_type);
    bcx.switch_to_block(tail_loop);
    let x_dst = bcx.block_params(tail_loop)[0];
    let tail_i = bcx.block_params(tail_loop)[1];

    bcx.ins().store(MemFlags::new(), src_vec, x_dst, 0);

    let sixteen = bcx.ins().iconst(ptr_type, 16);
    let next_x_dst = bcx.ins().iadd(x_dst, sixteen);
    let one = bcx.ins().iconst(ptr_type, 1);
    let next_ti = bcx.ins().iadd(tail_i, one);
    let cont = bcx.ins().icmp(IntCC::UnsignedLessThan, next_ti, tail_quads);
    bcx.ins().brif(
        cont,
        tail_loop,
        &block_args(&[next_x_dst, next_ti]),
        scalar_check,
        &block_args(&[next_x_dst]),
    );

    // === scalar_check ブロック ===
    bcx.append_block_param(scalar_check, ptr_type);
    bcx.switch_to_block(scalar_check);
    let x_dst = bcx.block_params(scalar_check)[0];
    let has_remainder = bcx.ins().icmp(IntCC::NotEqual, remainder, zero);
    bcx.ins().brif(
        has_remainder,
        scalar_loop,
        &block_args(&[x_dst, zero]),
        y_advance,
        &[],
    );

    // === scalar_loop ブロック (1px) ===
    bcx.append_block_param(scalar_loop, ptr_type);
    bcx.append_block_param(scalar_loop, ptr_type);
    bcx.switch_to_block(scalar_loop);
    let x_dst = bcx.block_params(scalar_loop)[0];
    let scalar_i = bcx.block_params(scalar_loop)[1];

    bcx.ins().store(MemFlags::new(), src_solid, x_dst, 0);

    let four = bcx.ins().iconst(ptr_type, 4);
    let next_x_dst = bcx.ins().iadd(x_dst, four);
    let one = bcx.ins().iconst(ptr_type, 1);
    let next_si = bcx.ins().iadd(scalar_i, one);
    let cont = bcx.ins().icmp(IntCC::UnsignedLessThan, next_si, remainder);
    bcx.ins().brif(
        cont,
        scalar_loop,
        &block_args(&[next_x_dst, next_si]),
        y_advance,
        &[],
    );

    // === y_advance ブロック ===
    bcx.switch_to_block(y_advance);
    let next_scanline = bcx.ins().iadd(scanline_dst, stride);
    let one = bcx.ins().iconst(ptr_type, 1);
    let next_y = bcx.ins().iadd(y_i, one);
    let cont = bcx.ins().icmp(IntCC::UnsignedLessThan, next_y, height);
    bcx.ins().brif(
        cont,
        y_loop,
        &block_args(&[next_scanline, next_y]),
        exit,
        &[],
    );

    // === exit ブロック ===
    bcx.switch_to_block(exit);
    bcx.ins().return_(&[]);

    bcx.seal_all_blocks();
    bcx.finalize();
}