pic-scale 0.7.8

High performance image scaling
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
/*
 * Copyright (c) Radzivon Bartoshyk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1.  Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * 2.  Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3.  Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
use crate::filter_weights::FilterWeights;
use crate::sse::_mm_prefer_fma_ps;
#[cfg(target_arch = "x86")]
use std::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;

#[inline(always)]
fn conv_horiz_rgba_1_u16<const FMA: bool>(
    start_x: usize,
    src: &[u16],
    w0: __m128,
    store: __m128,
) -> __m128 {
    unsafe {
        const CN: usize = 4;
        let src_ptr = src.get_unchecked((start_x * CN)..);
        let rgba_pixel = _mm_loadu_si64(src_ptr.as_ptr() as *const u8);
        _mm_prefer_fma_ps::<FMA>(
            store,
            _mm_cvtepi32_ps(_mm_unpacklo_epi16(rgba_pixel, _mm_setzero_si128())),
            w0,
        )
    }
}

#[inline(always)]
fn conv_horiz_rgba_2_u16<const FMA: bool>(
    start_x: usize,
    src: &[u16],
    w0: __m128,
    w1: __m128,
    store: __m128,
) -> __m128 {
    unsafe {
        const CN: usize = 4;
        let src_ptr = src.get_unchecked((start_x * CN)..);

        let rgba_pixel = _mm_loadu_si128(src_ptr.as_ptr() as *const __m128i);

        let acc = _mm_prefer_fma_ps::<FMA>(
            store,
            _mm_cvtepi32_ps(_mm_unpacklo_epi16(rgba_pixel, _mm_setzero_si128())),
            w0,
        );
        _mm_prefer_fma_ps::<FMA>(
            acc,
            _mm_cvtepi32_ps(_mm_unpacklo_epi16(rgba_pixel, _mm_setzero_si128())),
            w1,
        )
    }
}

#[inline]
fn conv_horiz_rgba_4_u16<const FMA: bool>(
    start_x: usize,
    src: &[u16],
    w0: __m128,
    w1: __m128,
    w2: __m128,
    w3: __m128,
    store: __m128,
) -> __m128 {
    unsafe {
        const CN: usize = 4;
        let src_ptr = src.get_unchecked((start_x * CN)..);

        let rgba_pixel0 = _mm_loadu_si128(src_ptr.as_ptr() as *const __m128i);
        let rgba_pixel1 = _mm_loadu_si128(src_ptr.get_unchecked(8..).as_ptr() as *const __m128i);

        let acc = _mm_prefer_fma_ps::<FMA>(
            store,
            _mm_cvtepi32_ps(_mm_unpackhi_epi16(rgba_pixel1, _mm_setzero_si128())),
            w3,
        );
        let acc = _mm_prefer_fma_ps::<FMA>(
            acc,
            _mm_cvtepi32_ps(_mm_unpacklo_epi16(rgba_pixel1, _mm_setzero_si128())),
            w2,
        );
        let acc = _mm_prefer_fma_ps::<FMA>(
            acc,
            _mm_cvtepi32_ps(_mm_unpackhi_epi16(rgba_pixel0, _mm_setzero_si128())),
            w1,
        );
        _mm_prefer_fma_ps::<FMA>(
            acc,
            _mm_cvtepi32_ps(_mm_unpackhi_epi16(rgba_pixel0, _mm_setzero_si128())),
            w0,
        )
    }
}

#[inline(always)]
fn conv_horiz_rgba_8_u16<const FMA: bool>(
    start_x: usize,
    src: &[u16],
    set1: (__m128, __m128, __m128, __m128),
    set2: (__m128, __m128, __m128, __m128),
    store: __m128,
) -> __m128 {
    unsafe {
        const CN: usize = 4;
        let src_ptr = src.get_unchecked((start_x * CN)..);

        let zeros = _mm_setzero_si128();

        let rgba_pixel0 = _mm_loadu_si128(src_ptr.as_ptr() as *const __m128i);
        let rgba_pixel1 = _mm_loadu_si128(src_ptr.get_unchecked(8..).as_ptr() as *const __m128i);
        let rgba_pixel2 = _mm_loadu_si128(src_ptr.get_unchecked(16..).as_ptr() as *const __m128i);
        let rgba_pixel3 = _mm_loadu_si128(src_ptr.get_unchecked(24..).as_ptr() as *const __m128i);

        let mut acc = _mm_prefer_fma_ps::<FMA>(
            store,
            _mm_cvtepi32_ps(_mm_unpackhi_epi16(rgba_pixel1, zeros)),
            set1.3,
        );
        acc = _mm_prefer_fma_ps::<FMA>(
            acc,
            _mm_cvtepi32_ps(_mm_unpacklo_epi16(rgba_pixel1, zeros)),
            set1.2,
        );
        acc = _mm_prefer_fma_ps::<FMA>(
            acc,
            _mm_cvtepi32_ps(_mm_unpackhi_epi16(rgba_pixel0, zeros)),
            set1.1,
        );
        acc = _mm_prefer_fma_ps::<FMA>(
            acc,
            _mm_cvtepi32_ps(_mm_unpacklo_epi16(rgba_pixel0, zeros)),
            set1.0,
        );

        acc = _mm_prefer_fma_ps::<FMA>(
            acc,
            _mm_cvtepi32_ps(_mm_unpackhi_epi16(rgba_pixel3, zeros)),
            set2.3,
        );
        acc = _mm_prefer_fma_ps::<FMA>(
            acc,
            _mm_cvtepi32_ps(_mm_unpacklo_epi16(rgba_pixel3, zeros)),
            set2.2,
        );
        acc = _mm_prefer_fma_ps::<FMA>(
            acc,
            _mm_cvtepi32_ps(_mm_unpackhi_epi16(rgba_pixel2, zeros)),
            set2.1,
        );
        acc = _mm_prefer_fma_ps::<FMA>(
            acc,
            _mm_cvtepi32_ps(_mm_unpacklo_epi16(rgba_pixel2, zeros)),
            set2.0,
        );
        acc
    }
}

pub(crate) fn convolve_horizontal_rgba_sse_rows_4_u16(
    src: &[u16],
    src_stride: usize,
    dst: &mut [u16],
    dst_stride: usize,
    filter_weights: &FilterWeights<f32>,
    bit_depth: u32,
) {
    unsafe {
        convolve_horizontal_rgba_sse_rows_4_u16_def(
            src,
            src_stride,
            dst,
            dst_stride,
            filter_weights,
            bit_depth,
        );
    }
}

#[target_feature(enable = "sse4.1")]
/// This inlining is required to activate all features for runtime dispatch.
fn convolve_horizontal_rgba_sse_rows_4_u16_def(
    src: &[u16],
    src_stride: usize,
    dst: &mut [u16],
    dst_stride: usize,
    filter_weights: &FilterWeights<f32>,
    bit_depth: u32,
) {
    convolve_horizontal_rgba_sse_rows_4_u16_impl::<false>(
        src,
        src_stride,
        dst,
        dst_stride,
        filter_weights,
        bit_depth,
    );
}

#[inline(always)]
fn convolve_horizontal_rgba_sse_rows_4_u16_impl<const FMA: bool>(
    src: &[u16],
    src_stride: usize,
    dst: &mut [u16],
    dst_stride: usize,
    filter_weights: &FilterWeights<f32>,
    bit_depth: u32,
) {
    unsafe {
        const CN: usize = 4;

        let v_max_colors = _mm_set1_epi32((1 << bit_depth) - 1);

        let (row0_ref, rest) = dst.split_at_mut(dst_stride);
        let (row1_ref, rest) = rest.split_at_mut(dst_stride);
        let (row2_ref, row3_ref) = rest.split_at_mut(dst_stride);

        let iter_row0 = row0_ref.as_chunks_mut::<CN>().0.iter_mut();
        let iter_row1 = row1_ref.as_chunks_mut::<CN>().0.iter_mut();
        let iter_row2 = row2_ref.as_chunks_mut::<CN>().0.iter_mut();
        let iter_row3 = row3_ref.as_chunks_mut::<CN>().0.iter_mut();

        for (((((chunk0, chunk1), chunk2), chunk3), &bounds), weights) in iter_row0
            .zip(iter_row1)
            .zip(iter_row2)
            .zip(iter_row3)
            .zip(filter_weights.bounds.iter())
            .zip(
                filter_weights
                    .weights
                    .chunks_exact(filter_weights.aligned_size),
            )
        {
            let mut jx = 0usize;
            let mut store_0 = _mm_setzero_ps();
            let mut store_1 = _mm_setzero_ps();
            let mut store_2 = _mm_setzero_ps();
            let mut store_3 = _mm_setzero_ps();

            let bounds_size = bounds.size;

            let src0 = src;
            let src1 = src0.get_unchecked(src_stride..);
            let src2 = src1.get_unchecked(src_stride..);
            let src3 = src2.get_unchecked(src_stride..);

            while jx + 8 <= bounds_size {
                let bounds_start = bounds.start + jx;
                let w_ptr = weights.get_unchecked(jx..(jx + 8));
                let w0 = _mm_load1_ps(w_ptr.as_ptr());
                let w1 = _mm_load1_ps(w_ptr.as_ptr().add(1));
                let w2 = _mm_load1_ps(w_ptr.as_ptr().add(2));
                let w3 = _mm_load1_ps(w_ptr.as_ptr().add(3));
                let w4 = _mm_load1_ps(w_ptr.as_ptr().add(4));
                let w5 = _mm_load1_ps(w_ptr.as_ptr().add(5));
                let w6 = _mm_load1_ps(w_ptr.as_ptr().add(6));
                let w7 = _mm_load1_ps(w_ptr.as_ptr().add(7));
                let set1 = (w0, w1, w2, w3);
                let set2 = (w4, w5, w6, w7);
                store_0 = conv_horiz_rgba_8_u16::<FMA>(bounds_start, src0, set1, set2, store_0);
                store_1 = conv_horiz_rgba_8_u16::<FMA>(bounds_start, src1, set1, set2, store_1);
                store_2 = conv_horiz_rgba_8_u16::<FMA>(bounds_start, src2, set1, set2, store_2);
                store_3 = conv_horiz_rgba_8_u16::<FMA>(bounds_start, src3, set1, set2, store_3);
                jx += 8;
            }

            while jx + 4 <= bounds_size {
                let bounds_start = bounds.start + jx;
                let w_ptr = weights.get_unchecked(jx..(jx + 4));
                let w0 = _mm_load1_ps(w_ptr.as_ptr());
                let w1 = _mm_load1_ps(w_ptr.as_ptr().add(1));
                let w2 = _mm_load1_ps(w_ptr.as_ptr().add(2));
                let w3 = _mm_load1_ps(w_ptr.as_ptr().add(3));
                store_0 = conv_horiz_rgba_4_u16::<FMA>(bounds_start, src0, w0, w1, w2, w3, store_0);
                store_1 = conv_horiz_rgba_4_u16::<FMA>(bounds_start, src1, w0, w1, w2, w3, store_1);
                store_2 = conv_horiz_rgba_4_u16::<FMA>(bounds_start, src2, w0, w1, w2, w3, store_2);
                store_3 = conv_horiz_rgba_4_u16::<FMA>(bounds_start, src3, w0, w1, w2, w3, store_3);
                jx += 4;
            }

            while jx + 2 <= bounds_size {
                let w_ptr = weights.get_unchecked(jx..(jx + 2));
                let bounds_start = bounds.start + jx;
                let w0 = _mm_load1_ps(w_ptr.as_ptr());
                let w1 = _mm_load1_ps(w_ptr.as_ptr().add(1));
                store_0 = conv_horiz_rgba_2_u16::<FMA>(bounds_start, src0, w0, w1, store_0);
                store_1 = conv_horiz_rgba_2_u16::<FMA>(bounds_start, src1, w0, w1, store_1);
                store_2 = conv_horiz_rgba_2_u16::<FMA>(bounds_start, src2, w0, w1, store_2);
                store_3 = conv_horiz_rgba_2_u16::<FMA>(bounds_start, src3, w0, w1, store_3);
                jx += 2;
            }

            while jx < bounds_size {
                let w_ptr = weights.get_unchecked(jx..(jx + 1));
                let bounds_start = bounds.start + jx;
                let w0 = _mm_load1_ps(w_ptr.as_ptr());
                store_0 = conv_horiz_rgba_1_u16::<FMA>(bounds_start, src0, w0, store_0);
                store_1 = conv_horiz_rgba_1_u16::<FMA>(bounds_start, src1, w0, store_1);
                store_2 = conv_horiz_rgba_1_u16::<FMA>(bounds_start, src2, w0, store_2);
                store_3 = conv_horiz_rgba_1_u16::<FMA>(bounds_start, src3, w0, store_3);
                jx += 1;
            }

            let v_st0 = _mm_min_epi32(
                _mm_cvtps_epi32(_mm_max_ps(store_0, _mm_setzero_ps())),
                v_max_colors,
            );
            let v_st1 = _mm_min_epi32(
                _mm_cvtps_epi32(_mm_max_ps(store_1, _mm_setzero_ps())),
                v_max_colors,
            );
            let v_st2 = _mm_min_epi32(
                _mm_cvtps_epi32(_mm_max_ps(store_2, _mm_setzero_ps())),
                v_max_colors,
            );
            let v_st3 = _mm_min_epi32(
                _mm_cvtps_epi32(_mm_max_ps(store_3, _mm_setzero_ps())),
                v_max_colors,
            );

            let store_16_0 = _mm_packus_epi32(v_st0, v_st0);
            let store_16_1 = _mm_packus_epi32(v_st1, v_st1);
            let store_16_2 = _mm_packus_epi32(v_st2, v_st2);
            let store_16_3 = _mm_packus_epi32(v_st3, v_st3);

            _mm_storeu_si64(chunk0.as_mut_ptr() as *mut u8, store_16_0);
            _mm_storeu_si64(chunk1.as_mut_ptr() as *mut u8, store_16_1);
            _mm_storeu_si64(chunk2.as_mut_ptr() as *mut u8, store_16_2);
            _mm_storeu_si64(chunk3.as_mut_ptr() as *mut u8, store_16_3);
        }
    }
}

pub(crate) fn convolve_horizontal_rgba_sse_u16_row(
    src: &[u16],
    dst: &mut [u16],
    filter_weights: &FilterWeights<f32>,
    bit_depth: u32,
) {
    unsafe {
        convolve_horizontal_rgba_sse_u16_row_def(src, dst, filter_weights, bit_depth);
    }
}

#[target_feature(enable = "sse4.1")]
/// This inlining is required to activate all features for runtime dispatch.
fn convolve_horizontal_rgba_sse_u16_row_def(
    src: &[u16],
    dst: &mut [u16],
    filter_weights: &FilterWeights<f32>,
    bit_depth: u32,
) {
    convolve_horizontal_rgba_sse_u16_row_impl::<false>(src, dst, filter_weights, bit_depth);
}

#[inline(always)]
fn convolve_horizontal_rgba_sse_u16_row_impl<const FMA: bool>(
    src: &[u16],
    dst: &mut [u16],
    filter_weights: &FilterWeights<f32>,
    bit_depth: u32,
) {
    unsafe {
        const CN: usize = 4;

        let v_max_colors = _mm_set1_epi32((1 << bit_depth) - 1);

        for ((dst, bounds), weights) in dst
            .as_chunks_mut::<CN>()
            .0
            .iter_mut()
            .zip(filter_weights.bounds.iter())
            .zip(
                filter_weights
                    .weights
                    .chunks_exact(filter_weights.aligned_size),
            )
        {
            let bounds_size = bounds.size;
            let mut jx = 0usize;
            let mut store = _mm_setzero_ps();

            while jx + 8 <= bounds_size {
                let bounds_start = bounds.start + jx;
                let w_ptr = weights.get_unchecked(jx..(jx + 8));
                let w0 = _mm_load1_ps(w_ptr.as_ptr());
                let w1 = _mm_load1_ps(w_ptr.as_ptr().add(1));
                let w2 = _mm_load1_ps(w_ptr.as_ptr().add(2));
                let w3 = _mm_load1_ps(w_ptr.as_ptr().add(3));
                let w4 = _mm_load1_ps(w_ptr.as_ptr().add(4));
                let w5 = _mm_load1_ps(w_ptr.as_ptr().add(5));
                let w6 = _mm_load1_ps(w_ptr.as_ptr().add(6));
                let w7 = _mm_load1_ps(w_ptr.as_ptr().add(7));
                let set1 = (w0, w1, w2, w3);
                let set2 = (w4, w5, w6, w7);
                store = conv_horiz_rgba_8_u16::<FMA>(bounds_start, src, set1, set2, store);
                jx += 8;
            }

            while jx + 4 <= bounds_size {
                let w_ptr = weights.get_unchecked(jx..(jx + 4));
                let w0 = _mm_load1_ps(w_ptr.as_ptr());
                let w1 = _mm_load1_ps(w_ptr.as_ptr().add(1));
                let w2 = _mm_load1_ps(w_ptr.as_ptr().add(2));
                let w3 = _mm_load1_ps(w_ptr.as_ptr().add(3));
                let bounds_start = bounds.start + jx;
                store = conv_horiz_rgba_4_u16::<FMA>(bounds_start, src, w0, w1, w2, w3, store);
                jx += 4;
            }

            while jx + 2 <= bounds_size {
                let w_ptr = weights.get_unchecked(jx..(jx + 2));
                let bounds_start = bounds.start + jx;
                let w0 = _mm_load1_ps(w_ptr.as_ptr());
                let w1 = _mm_load1_ps(w_ptr.as_ptr().add(1));
                store = conv_horiz_rgba_2_u16::<FMA>(bounds_start, src, w0, w1, store);
                jx += 2;
            }

            while jx < bounds_size {
                let w_ptr = weights.get_unchecked(jx..(jx + 1));
                let w0 = _mm_load1_ps(w_ptr.as_ptr());
                let bounds_start = bounds.start + jx;
                store = conv_horiz_rgba_1_u16::<FMA>(bounds_start, src, w0, store);
                jx += 1;
            }

            let v_st = _mm_min_epi32(
                _mm_cvtps_epi32(_mm_max_ps(store, _mm_setzero_ps())),
                v_max_colors,
            );

            let store_16_0 = _mm_packus_epi32(v_st, v_st);
            _mm_storeu_si64(dst.as_mut_ptr() as *mut u8, store_16_0);
        }
    }
}