kahlo 0.0.3

Optimized software rendering 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
use std::arch::x86_64::{
    __m128i, __m256, __m256i, _mm256_add_epi16, _mm256_add_ps, _mm256_blend_epi16, _mm256_blend_ps,
    _mm256_castsi128_si256, _mm256_castsi256_si128, _mm256_cvtepi32_ps, _mm256_cvtepu16_epi32,
    _mm256_cvtepu8_epi16, _mm256_cvtepu8_epi32, _mm256_cvtps_epi32, _mm256_div_ps,
    _mm256_extracti128_si256, _mm256_fmadd_ps, _mm256_inserti128_si256, _mm256_loadu_epi8,
    _mm256_loadu_si256, _mm256_mul_epu32, _mm256_mul_ps, _mm256_mulhi_epu16, _mm256_mullo_epi16,
    _mm256_mullo_epi32, _mm256_or_si256, _mm256_set1_epi16, _mm256_set1_epi32, _mm256_set1_ps,
    _mm256_shuffle_epi32, _mm256_shuffle_epi8, _mm256_shuffle_ps, _mm256_shufflehi_epi16,
    _mm256_shufflelo_epi16, _mm256_srli_epi16, _mm256_srli_epi64, _mm256_storeu_si256,
    _mm256_sub_epi16, _mm256_sub_ps, _mm_cvtepi8_epi32, _mm_loadu_si128, _mm_loadu_si32,
    _mm_loadu_si64, _mm_or_si128, _mm_set1_epi32, _mm_slli_epi32, _mm_storeu_si128,
    _mm_storeu_si32, _mm_storeu_si64,
};

use crate::colour::Colour;

// multiply by 32897 (unsigned) and shift by 23 to divide by 255
const INV_255: u16 = 32897;

#[derive(Clone, Copy)]
pub struct Ri8x16(pub __m128i);

impl Ri8x16 {
    #[target_feature(enable = "avx2")]
    pub fn load4(data: &[u8; 4]) -> Self {
        Self(unsafe { _mm_loadu_si32(data.as_ptr()) })
    }

    #[target_feature(enable = "avx2")]
    pub fn load4_spaced_4_offset_3(data: &[u8; 4]) -> Self {
        let initial = unsafe { _mm_loadu_si32(data.as_ptr()) };
        // target is:   [0,0,0,a,0,0,0,b,0,0,0,c,0,0,0,d]
        // we now have: [a,b,c,d,0,0,0,0,0,0,0,0,0,0,0,0]
        let spread = _mm_cvtepi8_epi32(initial);
        // we now have: [a,0,0,0,b,0,0,0,c,0,0,0,d,0,0,0]
        // so shifting left by 24 bits gets the desired result
        Self(_mm_slli_epi32::<24>(spread))
    }

    #[target_feature(enable = "avx2")]
    pub fn widen_to_i16(self) -> Ri16x16 {
        Ri16x16(_mm256_cvtepu8_epi16(self.0))
    }
}

#[derive(Clone, Copy)]
pub struct Ri8x32(pub __m256i);

impl Ri8x32 {
    #[target_feature(enable = "avx2")]
    pub fn load32(data: &[u8; 32]) -> Self {
        Self(unsafe { _mm256_loadu_si256(data.as_ptr() as *const __m256i) })
    }

    #[target_feature(enable = "avx2")]
    pub fn store32(self, data: &mut [u8; 32]) {
        unsafe { _mm256_storeu_si256(data.as_ptr() as *mut __m256i, self.0) }
    }

    const ENDIAN_SHUFFLE: [i8; 32] = [
        // low lane, first pixel
        3, 2, 1, 0, // low lane, second pixel
        7, 6, 5, 4, // low lane, third pixel
        11, 10, 9, 8, // low lane, fourth pixel
        15, 14, 13, 12, // high lane, first pixel
        3, 2, 1, 0, // high lane, second pixel
        7, 6, 5, 4, // high lane, third pixel
        11, 10, 9, 8, // high lane, fourth pixel
        15, 14, 13, 12,
    ];

    #[target_feature(enable = "avx2")]
    pub fn swap_endianness(self) -> Self {
        Self(_mm256_shuffle_epi8(self.0, unsafe {
            _mm256_loadu_epi8(Self::ENDIAN_SHUFFLE.as_ptr())
        }))
    }
}

/// standard format for pixel manipulation: each pixel is represented by 64 bits, r/g/b/a with a 16-bit value each
#[derive(Clone, Copy)]
pub struct Ri16x16(pub __m256i);

#[allow(unused)]
impl Ri16x16 {
    pub fn unwrap(self) -> __m256i {
        self.0
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn load8_and_expand(val: &[u8; 16]) -> Self {
        Self(unsafe { _mm256_cvtepu8_epi16(_mm_loadu_si128(val.as_ptr() as *const __m128i)) })
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn load_from(val: &[u8; 32]) -> Self {
        Self(unsafe { _mm256_loadu_epi8(val.as_ptr() as *const i8) })
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn from_constant(val: u16) -> Self {
        Self(_mm256_set1_epi16(val as i16))
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn shrink_and_store8(self, into: &mut [u8; 16]) {
        // since we don't have AVX512, there's no convenient _mm256_cvtepi16_epi8
        // available. hence we byte-shuffle the LSBs to be contiguous with zeroes
        // in the upper/lower half of the 128-bit lanes, then OR the two together.

        // shuffle vector
        const SHUFFLE: [i8; 32] = [
            0, 2, 4, 6, 8, 10, 12, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, 0, 2, 4, 6, 8, 10, 12, 14,
        ];

        let shuffled = _mm256_shuffle_epi8(self.0, unsafe { _mm256_loadu_epi8(SHUFFLE.as_ptr()) });

        let recombined = _mm_or_si128(
            _mm256_castsi256_si128(shuffled),
            _mm256_extracti128_si256::<1>(shuffled),
        );
        unsafe { _mm_storeu_si128(into.as_mut_ptr() as *mut __m128i, recombined) };
    }

    /// extracts alpha of each pixel and broadcasts its value to the r/g/b channels
    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn extract_alpha_rgba(self) -> Self {
        Self(_mm256_shufflelo_epi16::<0b11_11_11_11>(
            _mm256_shufflehi_epi16::<0b11_11_11_11>(self.0),
        ))
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn div_255(self) -> Self {
        Ri16x16(_mm256_srli_epi16::<7>(_mm256_mulhi_epu16(
            _mm256_add_epi16(self.0, _mm256_set1_epi16(128)),
            _mm256_set1_epi16(INV_255 as i16),
        )))
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn to_i32x8(self) -> (Ri32x8, Ri32x8) {
        (
            Ri32x8(_mm256_cvtepu16_epi32(_mm256_castsi256_si128(self.0))),
            Ri32x8(_mm256_cvtepu16_epi32(_mm256_extracti128_si256::<1>(self.0))),
        )
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn from_i32x8(lo: Ri32x8, hi: Ri32x8) -> Ri16x16 {
        // we don't have _mm256_cvtepi32_epi16, as nice as it would be, since that's AVX-512.
        // so instead, do a byte-level shuffle to shove the two least-significant bytes to one
        // half of the 128-bit lane
        const SHUFFLE: [i8; 32] = [
            // two least-significant bytes for each of the first four i32s
            0, 1, 4, 5, 8, 9, 12, 13, // high 64 bits of the first 128-bit lane
            -1, -1, -1, -1, -1, -1, -1, -1, // low 64 bits of the second 128-bit lane
            -1, -1, -1, -1, -1, -1, -1, -1,
            // two-least significant bytes for each of the second four i32s
            0, 1, 4, 5, 8, 9, 12, 13,
        ];
        let shuffle = unsafe { _mm256_loadu_epi8(SHUFFLE.as_ptr()) };
        let lo_shuf = _mm256_shuffle_epi8(lo.0, shuffle);
        let hi_shuf = _mm256_shuffle_epi8(hi.0, shuffle);

        // now combine the low and high halves of each shuffled __mm256i
        let lo16 = _mm_or_si128(
            _mm256_castsi256_si128(lo_shuf),
            _mm256_extracti128_si256::<1>(lo_shuf),
        );

        let hi16 = _mm_or_si128(
            _mm256_castsi256_si128(hi_shuf),
            _mm256_extracti128_si256::<1>(hi_shuf),
        );

        Ri16x16(_mm256_inserti128_si256::<1>(
            _mm256_castsi128_si256(lo16),
            hi16,
        ))
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn mix(&self, other: Ri16x16, amount: Ri16x16) -> Ri16x16 {
        ((*self * amount) + (other * (Ri16x16::from_constant(255) - amount))).div_255()
    }

    /// Combines the alpha values from another Ri16x16 and the rgb values from this Ri16x16 and
    /// returns the result.
    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn copy_alpha_rgba(&self, other: Ri16x16) -> Ri16x16 {
        Self(_mm256_blend_epi16::<0b_1000_1000>(self.0, other.0))
    }
}

impl std::fmt::Debug for Ri16x16 {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        let a = unsafe { std::mem::transmute::<_, [u16; 16]>(self.0) };

        write!(
            fmt,
            "Ri16x16 {{ {} | {} | {} | {} }}",
            format!("{:04x} {:04x} {:04x} {:04x}", a[0], a[1], a[2], a[3]),
            format!("{:04x} {:04x} {:04x} {:04x}", a[4], a[5], a[6], a[7]),
            format!("{:04x} {:04x} {:04x} {:04x}", a[8], a[9], a[10], a[11]),
            format!("{:04x} {:04x} {:04x} {:04x}", a[12], a[13], a[14], a[15]),
        )
    }
}

impl std::ops::Add for Ri16x16 {
    type Output = Ri16x16;
    fn add(self, r: Self) -> Self {
        Self(unsafe { _mm256_add_epi16(self.0, r.0) })
    }
}

impl std::ops::Sub for Ri16x16 {
    type Output = Ri16x16;
    fn sub(self, r: Self) -> Self {
        Self(unsafe { _mm256_sub_epi16(self.0, r.0) })
    }
}

impl std::ops::Mul for Ri16x16 {
    type Output = Ri16x16;
    fn mul(self, r: Self) -> Self {
        Self(unsafe { _mm256_mullo_epi16(self.0, r.0) })
    }
}

#[derive(Clone, Copy)]
pub struct Ri32x4(pub __m128i);

#[allow(unused)]
impl Ri32x4 {
    pub fn unwrap(self) -> __m128i {
        self.0
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn from_constant(val: u32) -> Self {
        Self(_mm_set1_epi32(val as i32))
    }

    pub fn cast_to_i8x16(self) -> Ri8x16 {
        Ri8x16(self.0)
    }
}

#[derive(Clone, Copy)]
pub struct Ri32x8(pub __m256i);

#[allow(unused)]
impl Ri32x8 {
    pub fn unwrap(self) -> __m256i {
        self.0
    }

    pub fn cast_to_i16x16(self) -> Ri16x16 {
        Ri16x16(self.0)
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn from_constant(val: u32) -> Self {
        Self(_mm256_set1_epi32(val as i32))
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn div_255(&self) -> Self {
        // first add 128 for rounding
        let withround = _mm256_add_epi16(self.0, _mm256_set1_epi32(128));

        // the multiplication by INV_255 can and will result in a value larger than 32 bits, so we
        // need to use the 64-bit multiplication result instead.
        let loprod = _mm256_mul_epu32(self.0, _mm256_set1_epi32(INV_255 as i32));
        // need to swap the high/low 64-bit groups in each lane and then multiply
        let hiprod = _mm256_mul_epu32(
            _mm256_shuffle_epi32::<0b_10_11_00_01>(self.0),
            _mm256_set1_epi32(INV_255 as i32),
        );

        // at this point, loprod has (v*INV_255).aceg and hiprod has (v*INV_255).bdfh as u64s

        // do the post-multiplication reduction...
        let loshift = _mm256_srli_epi64::<23>(loprod);
        let hishift = _mm256_srli_epi64::<23>(hiprod);

        // at this point, loprod has (v*INV_255 >> 23).aceg and hiprod has (v*INV_255 >> 23).bdfh as u64s,
        // so we want to convert down to u32s. we want:
        // - loprod to a_c_e_g_
        // - hiprod to _b_d_f_h
        //
        // but loprod is already in this form, so we just have to shuffle hiprod around a bit
        let hiswizz = _mm256_shuffle_epi32::<0b_10_11_00_01>(hishift);

        Ri32x8(_mm256_or_si256(loshift, hiswizz))
    }
}

impl std::fmt::Debug for Ri32x8 {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        let a = unsafe { std::mem::transmute::<_, [u32; 8]>(self.0) };

        write!(
            fmt,
            "Ri32x8 {{ {} | {} }}",
            format!("{:08x} {:08x} {:08x} {:08x}", a[0], a[1], a[2], a[3]),
            format!("{:08x} {:08x} {:08x} {:08x}", a[4], a[5], a[6], a[7]),
        )
    }
}

impl std::ops::Add for Ri32x8 {
    type Output = Self;
    fn add(self, r: Self) -> Self {
        Self(unsafe { _mm256_add_epi16(self.0, r.0) })
    }
}

impl std::ops::Sub for Ri32x8 {
    type Output = Self;
    fn sub(self, r: Self) -> Self {
        Self(unsafe { _mm256_sub_epi16(self.0, r.0) })
    }
}

impl std::ops::Mul for Ri32x8 {
    type Output = Self;
    fn mul(self, r: Self) -> Self {
        Self(unsafe { _mm256_mullo_epi32(self.0, r.0) })
    }
}

#[derive(Clone, Copy)]
pub struct Ri64x4(pub __m256i);

impl std::fmt::Debug for Ri64x4 {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        let a = unsafe { std::mem::transmute::<_, [u64; 4]>(self.0) };

        write!(
            fmt,
            "Ri32x8 {{ {} }}",
            format!("{:016x} {:016x} | {:016x} {:016x}", a[0], a[1], a[2], a[3]),
        )
    }
}

#[derive(Clone, Copy)]
pub struct Rf32x8(pub __m256);

impl Rf32x8 {
    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn load8_and_expand(val: &[u8; 8]) -> Self {
        // _mm_loadu_si128(val.as_ptr() as *const __m128i)
        Self(_mm256_cvtepi32_ps(_mm256_cvtepu8_epi32(unsafe {
            _mm_loadu_si64(val.as_ptr())
        })))
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn load4_and_expand(val: &[u8; 4]) -> Self {
        Self(_mm256_cvtepi32_ps(_mm256_cvtepu8_epi32(unsafe {
            _mm_loadu_si32(val.as_ptr())
        })))
    }

    const SHRINK_SHUFFLE: [i8; 32] = [
        // least-significant byte for the first four i32s
        0, 4, 8, 12, // high 96 bits of the low lane
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        // skip the low 32 bits of the high lane
        -1, -1, -1, -1, // least-significant byte for the next four i32s
        0, 4, 8, 12, // high 64 bits of the high lane
        -1, -1, -1, -1, -1, -1, -1, -1,
    ];

    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn shrink_and_store8(self, val: &mut [u8; 8]) {
        // really would love to use _mm256_cvtepi32_epi8, but that's a avx512f mnemonic, so...
        // shuffling it is.

        let shuffle = unsafe { _mm256_loadu_epi8(Self::SHRINK_SHUFFLE.as_ptr()) };
        let shuffled = _mm256_shuffle_epi8(_mm256_cvtps_epi32(self.0), shuffle);
        let combined = _mm_or_si128(
            _mm256_castsi256_si128(shuffled),
            _mm256_extracti128_si256::<1>(shuffled),
        );

        unsafe { _mm_storeu_si64(val.as_mut_ptr(), combined) }
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn shrink_and_store4(self, val: &mut [u8; 4]) {
        // really would love to use _mm256_cvtepi32_epi8, but that's a avx512f mnemonic, so...
        // shuffling it is.

        let shuffle = unsafe { _mm256_loadu_epi8(Self::SHRINK_SHUFFLE.as_ptr()) };
        let shuffled = _mm256_shuffle_epi8(_mm256_cvtps_epi32(self.0), shuffle);
        // unlike the store8 case, here we have no upper lane to extract bits from, so we can skip
        // straight to the store.
        unsafe { _mm_storeu_si32(val.as_mut_ptr(), _mm256_castsi256_si128(shuffled)) }
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn from_constant(val: f32) -> Self {
        Self(_mm256_set1_ps(val))
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn from_colours_rgba(lo: Colour, hi: Colour) -> Self {
        let data = [lo.r, lo.g, lo.b, lo.a, hi.r, hi.g, hi.b, hi.a];
        Self(_mm256_cvtepi32_ps(_mm256_cvtepu8_epi32(unsafe {
            _mm_loadu_si64(data.as_ptr())
        })))
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn extract_alpha_rgba(self) -> Self {
        Self(_mm256_shuffle_ps::<0b_11_11_11_11>(self.0, self.0))
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    pub fn copy_alpha_from(self, alpha: Self) -> Self {
        Self(_mm256_blend_ps::<0b_1000_1000>(self.0, alpha.0))
    }

    #[target_feature(enable = "fma")]
    #[inline]
    pub fn fma(self, m: Self, a: Self) -> Self {
        Self(_mm256_fmadd_ps(self.0, m.0, a.0))
    }
}

impl std::fmt::Debug for Rf32x8 {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        let a = unsafe { std::mem::transmute::<_, [f32; 8]>(self.0) };

        write!(
            fmt,
            "Rf32x8 {{ {} | {} }}",
            format_args!("{:.4} {:.4} {:.4} {:.4}", a[0], a[1], a[2], a[3]),
            format_args!("{:.4} {:.4} {:.4} {:.4}", a[4], a[5], a[6], a[7]),
        )
    }
}

impl std::ops::Add for Rf32x8 {
    type Output = Self;
    fn add(self, r: Self) -> Self {
        Self(unsafe { _mm256_add_ps(self.0, r.0) })
    }
}

impl std::ops::Sub for Rf32x8 {
    type Output = Self;
    fn sub(self, r: Self) -> Self {
        Self(unsafe { _mm256_sub_ps(self.0, r.0) })
    }
}

impl std::ops::Mul for Rf32x8 {
    type Output = Self;
    fn mul(self, r: Self) -> Self {
        Self(unsafe { _mm256_mul_ps(self.0, r.0) })
    }
}

impl std::ops::Div for Rf32x8 {
    type Output = Self;
    fn div(self, r: Self) -> Self {
        Self(unsafe { _mm256_div_ps(self.0, r.0) })
    }
}