-
Notifications
You must be signed in to change notification settings - Fork 5
/
libjpeg-turbo-3.0.2-e2k.patch
4897 lines (4892 loc) · 158 KB
/
libjpeg-turbo-3.0.2-e2k.patch
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
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From 5c6ff06bc9aec237e1ba222a3dde057cbfa81c9d Mon Sep 17 00:00:00 2001
From: Ilya Kurdyukov <jpegqs@gmail.com>
Date: Fri, 9 Feb 2024 09:57:55 +0700
Subject: [PATCH] libjpeg-turbo-3.0.2 e2k support
---
CMakeLists.txt | 5 +
simd/CMakeLists.txt | 23 ++
simd/e2k/jccolext-e2k.c | 213 +++++++++++
simd/e2k/jccolor-e2k.c | 163 +++++++++
simd/e2k/jchuff-e2k.c | 307 ++++++++++++++++
simd/e2k/jcphuff-e2k.c | 145 ++++++++
simd/e2k/jcsample-e2k.c | 203 +++++++++++
simd/e2k/jcsample.h | 28 ++
simd/e2k/jdcolext-e2k.c | 258 +++++++++++++
simd/e2k/jdcolor-e2k.c | 289 +++++++++++++++
simd/e2k/jdcoltab-e2k.c | 80 ++++
simd/e2k/jdsample-e2k.c | 389 ++++++++++++++++++++
simd/e2k/jfdctflt-e2k.c | 127 +++++++
simd/e2k/jfdctfst-e2k.c | 145 ++++++++
simd/e2k/jfdctint-e2k.c | 255 +++++++++++++
simd/e2k/jidctflt-e2k.c | 215 +++++++++++
simd/e2k/jidctfst-e2k.c | 187 ++++++++++
simd/e2k/jidctint-e2k.c | 294 +++++++++++++++
simd/e2k/jquantf-e2k.c | 121 +++++++
simd/e2k/jquanti-e2k.c | 178 +++++++++
simd/e2k/jsimd.c | 761 +++++++++++++++++++++++++++++++++++++++
simd/e2k/jsimd_api_e2k.h | 94 +++++
simd/e2k/jsimd_e2k.h | 207 +++++++++++
23 files changed, 4687 insertions(+)
create mode 100644 simd/e2k/jccolext-e2k.c
create mode 100644 simd/e2k/jccolor-e2k.c
create mode 100644 simd/e2k/jchuff-e2k.c
create mode 100644 simd/e2k/jcphuff-e2k.c
create mode 100644 simd/e2k/jcsample-e2k.c
create mode 100644 simd/e2k/jcsample.h
create mode 100644 simd/e2k/jdcolext-e2k.c
create mode 100644 simd/e2k/jdcolor-e2k.c
create mode 100644 simd/e2k/jdcoltab-e2k.c
create mode 100644 simd/e2k/jdsample-e2k.c
create mode 100644 simd/e2k/jfdctflt-e2k.c
create mode 100644 simd/e2k/jfdctfst-e2k.c
create mode 100644 simd/e2k/jfdctint-e2k.c
create mode 100644 simd/e2k/jidctflt-e2k.c
create mode 100644 simd/e2k/jidctfst-e2k.c
create mode 100644 simd/e2k/jidctint-e2k.c
create mode 100644 simd/e2k/jquantf-e2k.c
create mode 100644 simd/e2k/jquanti-e2k.c
create mode 100644 simd/e2k/jsimd.c
create mode 100644 simd/e2k/jsimd_api_e2k.h
create mode 100644 simd/e2k/jsimd_e2k.h
diff --git a/CMakeLists.txt b/CMakeLists.txt
index adb0ca4..3b445a0 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -125,6 +125,9 @@ elseif(CMAKE_SYSTEM_PROCESSOR_LC STREQUAL "aarch64" OR
elseif(CMAKE_SYSTEM_PROCESSOR_LC MATCHES "^ppc" OR
CMAKE_SYSTEM_PROCESSOR_LC MATCHES "^powerpc")
set(CPU_TYPE powerpc)
+elseif(CMAKE_SYSTEM_PROCESSOR_LC STREQUAL "e2k" OR
+ CMAKE_SYSTEM_PROCESSOR_LC STREQUAL "elbrus")
+ set(CPU_TYPE e2k)
else()
set(CPU_TYPE ${CMAKE_SYSTEM_PROCESSOR_LC})
endif()
@@ -906,6 +909,8 @@ if(CPU_TYPE STREQUAL "x86_64" OR CPU_TYPE STREQUAL "i386")
elseif(CPU_TYPE STREQUAL "x86_64")
set(DEFAULT_FLOATTEST8 no-fp-contract)
endif()
+elseif(WITH_SIMD AND CPU_TYPE STREQUAL "e2k")
+ set(DEFAULT_FLOATTEST8 sse)
elseif(CPU_TYPE STREQUAL "powerpc" OR CPU_TYPE STREQUAL "arm64")
if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
if(CMAKE_C_COMPILER_VERSION VERSION_EQUAL 14.0.0 OR
diff --git a/simd/CMakeLists.txt b/simd/CMakeLists.txt
index 6024900..789fa3f 100644
--- a/simd/CMakeLists.txt
+++ b/simd/CMakeLists.txt
@@ -531,6 +531,29 @@ if(CMAKE_POSITION_INDEPENDENT_CODE OR ENABLE_SHARED)
set_target_properties(simd PROPERTIES POSITION_INDEPENDENT_CODE 1)
endif()
+###############################################################################
+# Elbrus (Intrinsics)
+###############################################################################
+
+elseif(CPU_TYPE STREQUAL "e2k")
+
+set(SIMD_SOURCES e2k/jquanti-e2k.c e2k/jquantf-e2k.c
+ e2k/jccolor-e2k.c e2k/jcsample-e2k.c
+ e2k/jdcolor-e2k.c e2k/jdsample-e2k.c
+ e2k/jfdctint-e2k.c e2k/jfdctfst-e2k.c e2k/jfdctflt-e2k.c
+ e2k/jidctint-e2k.c e2k/jidctfst-e2k.c e2k/jidctflt-e2k.c
+ e2k/jchuff-e2k.c e2k/jcphuff-e2k.c)
+
+set_source_files_properties(${SIMD_SOURCES} PROPERTIES
+ COMPILE_FLAGS -msse4.1)
+
+set(SIMD_SOURCES ${SIMD_SOURCES} e2k/jsimd.c)
+
+add_library(simd OBJECT ${SIMD_SOURCES})
+
+if(CMAKE_POSITION_INDEPENDENT_CODE OR ENABLE_SHARED)
+ set_target_properties(simd PROPERTIES POSITION_INDEPENDENT_CODE 1)
+endif()
###############################################################################
# None
diff --git a/simd/e2k/jccolext-e2k.c b/simd/e2k/jccolext-e2k.c
new file mode 100644
index 0000000..49abdb4
--- /dev/null
+++ b/simd/e2k/jccolext-e2k.c
@@ -0,0 +1,213 @@
+/*
+ * Elbrus optimizations for libjpeg-turbo
+ *
+ * Copyright (C) 2014-2015, D. R. Commander. All Rights Reserved.
+ * Copyright (C) 2014, Jay Foad. All Rights Reserved.
+ * Copyright (C) 2021, Ilya Kurdyukov <jpegqs@gmail.com> for BaseALT, Ltd
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ */
+
+/* This file is included by jccolor-e2k.c */
+
+void rgbn_ycc_convert(JDIMENSION img_width, JSAMPARRAY input_buf,
+ JSAMPIMAGE output_buf, JDIMENSION output_row,
+ int num_rows, int shuf_idx)
+{
+ JSAMPROW inptr, outptr0, outptr1, outptr2;
+ unsigned char __attribute__((aligned(16))) tmpbuf[PIXELSIZE * 16];
+
+ __m128i pb_zero = _mm_setzero_si128();
+ __m128i pb_shuf0 = VEC_LD(rgb_ycc_shuf_const[shuf_idx]);
+#if PIXELSIZE == 4
+ __m128i rgb3 = pb_zero;
+#else
+ __m128i pb_shuf4 = VEC_LD(rgb_ycc_shuf_const[shuf_idx] + 16);
+#endif
+ __m128i rgb0, rgb1 = pb_zero, rgb2 = pb_zero,
+ rgbg0, rgbg1, rgbg2, rgbg3, rg0, rg1, rg2, rg3, bg0, bg1, bg2, bg3;
+ __m128i y, yl, yh, y0, y1, y2, y3;
+ __m128i cb, cr, crl, crh, cbl, cbh;
+ __m128i cr0, cr1, cr2, cr3, cb0, cb1, cb2, cb3;
+
+ /* Constants */
+ __m128i pw_f0299_f0337 = _mm_setr_epi16(__4X2(F_0_299, F_0_337)),
+ pw_f0114_f0250 = _mm_setr_epi16(__4X2(F_0_114, F_0_250)),
+ pw_mf016_mf033 = _mm_setr_epi16(__4X2(-F_0_168, -F_0_331)),
+ pw_mf008_mf041 = _mm_setr_epi16(__4X2(-F_0_081, -F_0_418)),
+ pw_mf050_f000 = _mm_setr_epi16(__4X2(-F_0_500, 0)),
+ pd_onehalf = _mm_set1_epi32(ONE_HALF),
+ pd_onehalfm1_cj = _mm_set1_epi32(ONE_HALF - 1 + (CENTERJSAMPLE << SCALEBITS));
+#ifdef NEED_ALIGN8
+ ALIGN8_COMMON
+ ALIGN8_VARS(src)
+#endif
+
+ if (img_width > 0)
+ while (--num_rows >= 0) {
+ int num_cols;
+ inptr = *input_buf++;
+ outptr0 = output_buf[0][output_row];
+ outptr1 = output_buf[1][output_row];
+ outptr2 = output_buf[2][output_row];
+ output_row++;
+
+ if (img_width >= 16) {
+#ifdef NEED_ALIGN8
+ ALIGN8_START(inptr, src)
+ inptr += (img_width & -16) * PIXELSIZE;
+#endif
+
+ PRAGMA_E2K("ivdep")
+ for (num_cols = img_width; num_cols >= 16; num_cols -= 16,
+ outptr0 += 16, outptr1 += 16, outptr2 += 16) {
+#ifdef NEED_ALIGN8
+ ALIGN8_READ16(rgb0, src, 0)
+ ALIGN8_READ16(rgb1, src, 1)
+ ALIGN8_READ16(rgb2, src, 2)
+#if PIXELSIZE == 4
+ ALIGN8_READ16(rgb3, src, 3)
+#endif
+ src_ptr += PIXELSIZE * 2;
+#else
+ rgb0 = VEC_LD(inptr);
+ rgb1 = VEC_LD(inptr + 16);
+ rgb2 = VEC_LD(inptr + 32);
+#if PIXELSIZE == 4
+ rgb3 = VEC_LD(inptr + 48);
+#endif
+ inptr += PIXELSIZE * 16;
+#endif
+ RGB_SHUFFLE
+ CALC_Y(outptr0)
+ CALC_CC(outptr1, outptr2)
+ }
+ }
+
+ num_cols = img_width & 15;
+ if (num_cols) {
+ int i;
+ memcpy(tmpbuf, inptr, num_cols * PIXELSIZE);
+ rgb0 = VEC_LD(tmpbuf);
+ rgb1 = VEC_LD(tmpbuf + 16);
+ rgb2 = VEC_LD(tmpbuf + 32);
+#if PIXELSIZE == 4
+ rgb3 = VEC_LD(tmpbuf + 48);
+#endif
+ RGB_SHUFFLE
+ CALC_Y(tmpbuf)
+ CALC_CC(tmpbuf + 16, tmpbuf + 32)
+
+ for (i = 0; i < num_cols; i++) {
+ outptr0[i] = tmpbuf[i];
+ outptr1[i] = tmpbuf[i + 16];
+ outptr2[i] = tmpbuf[i + 32];
+ }
+ }
+ }
+}
+
+void rgbn_gray_convert(JDIMENSION img_width, JSAMPARRAY input_buf,
+ JSAMPIMAGE output_buf, JDIMENSION output_row,
+ int num_rows, int shuf_idx)
+{
+ JSAMPROW inptr, outptr;
+ uint8_t __attribute__((aligned(16))) tmpbuf[PIXELSIZE * 16];
+
+ __m128i pb_zero = _mm_setzero_si128();
+ __m128i pb_shuf0 = VEC_LD(rgb_ycc_shuf_const[shuf_idx]);
+#if PIXELSIZE == 4
+ __m128i rgb3 = pb_zero;
+#else
+ __m128i pb_shuf4 = VEC_LD(rgb_ycc_shuf_const[shuf_idx] + 16);
+#endif
+ __m128i rgb0, rgb1 = pb_zero, rgb2 = pb_zero,
+ rgbg0, rgbg1, rgbg2, rgbg3, rg0, rg1, rg2, rg3, bg0, bg1, bg2, bg3;
+ __m128i y, yl, yh, y0, y1, y2, y3;
+
+ /* Constants */
+ __m128i pw_f0299_f0337 = _mm_setr_epi16(__4X2(F_0_299, F_0_337)),
+ pw_f0114_f0250 = _mm_setr_epi16(__4X2(F_0_114, F_0_250)),
+ pd_onehalf = _mm_set1_epi32(ONE_HALF);
+#ifdef NEED_ALIGN8
+ ALIGN8_COMMON
+ ALIGN8_VARS(src)
+#endif
+
+ if (img_width > 0)
+ while (--num_rows >= 0) {
+ int num_cols;
+ inptr = *input_buf++;
+ outptr = output_buf[0][output_row];
+ output_row++;
+
+ if (img_width >= 16) {
+#ifdef NEED_ALIGN8
+ ALIGN8_START(inptr, src)
+ inptr += (img_width & -16) * PIXELSIZE;
+#endif
+
+ PRAGMA_E2K("ivdep")
+ for (num_cols = img_width; num_cols >= 16; num_cols -= 16,
+ outptr += 16) {
+#ifdef NEED_ALIGN8
+ ALIGN8_READ16(rgb0, src, 0)
+ ALIGN8_READ16(rgb1, src, 1)
+ ALIGN8_READ16(rgb2, src, 2)
+#if PIXELSIZE == 4
+ ALIGN8_READ16(rgb3, src, 3)
+#endif
+ src_ptr += PIXELSIZE * 2;
+#else
+ rgb0 = VEC_LD(inptr);
+ rgb1 = VEC_LD(inptr + 16);
+ rgb2 = VEC_LD(inptr + 32);
+#if PIXELSIZE == 4
+ rgb3 = VEC_LD(inptr + 48);
+#endif
+ inptr += PIXELSIZE * 16;
+#endif
+ RGB_SHUFFLE
+ CALC_Y(outptr)
+ }
+ }
+
+ num_cols = img_width & 15;
+ if (num_cols) {
+ int i;
+ memcpy(tmpbuf, inptr, num_cols * PIXELSIZE);
+ rgb0 = VEC_LD(tmpbuf);
+ rgb1 = VEC_LD(tmpbuf + 16);
+ rgb2 = VEC_LD(tmpbuf + 32);
+#if PIXELSIZE == 4
+ rgb3 = VEC_LD(tmpbuf + 48);
+#endif
+ RGB_SHUFFLE
+ CALC_Y(tmpbuf)
+
+ for (i = 0; i < num_cols; i++) {
+ outptr[i] = tmpbuf[i];
+ }
+ }
+ }
+}
+
+#undef RGB_SHUFFLE
+#undef PIXELSIZE
+#undef rgbn_ycc_convert
+#undef rgbn_gray_convert
+
diff --git a/simd/e2k/jccolor-e2k.c b/simd/e2k/jccolor-e2k.c
new file mode 100644
index 0000000..0af2626
--- /dev/null
+++ b/simd/e2k/jccolor-e2k.c
@@ -0,0 +1,163 @@
+/*
+ * Elbrus optimizations for libjpeg-turbo
+ *
+ * Copyright (C) 2014, D. R. Commander. All Rights Reserved.
+ * Copyright (C) 2021, Ilya Kurdyukov <jpegqs@gmail.com> for BaseALT, Ltd
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ */
+
+/* RGB --> YCC CONVERSION */
+
+#include "jsimd_e2k.h"
+
+#define F_0_081 5329 /* FIX(0.08131) */
+#define F_0_114 7471 /* FIX(0.11400) */
+#define F_0_168 11059 /* FIX(0.16874) */
+#define F_0_250 16384 /* FIX(0.25000) */
+#define F_0_299 19595 /* FIX(0.29900) */
+#define F_0_331 21709 /* FIX(0.33126) */
+#define F_0_418 27439 /* FIX(0.41869) */
+#define F_0_500 32768 /* FIX(0.50000) */
+#define F_0_587 38470 /* FIX(0.58700) */
+#define F_0_337 (F_0_587 - F_0_250) /* FIX(0.58700) - FIX(0.25000) */
+#define F_0_413 (65536 - F_0_587) /* FIX(1.00000) - FIX(0.58700) */
+
+#define SCALEBITS 16
+#define ONE_HALF (1 << (SCALEBITS - 1))
+
+#define RGBG_INDEX_(name, color, i, x) \
+ name##_##color + i * name##_PIXELSIZE + x, \
+ name##_GREEN + i * name##_PIXELSIZE + x
+#define RGBG_INDEX(name, x) \
+ RGBG_INDEX_(name, RED, 0, x), RGBG_INDEX_(name, RED, 1, x), \
+ RGBG_INDEX_(name, RED, 2, x), RGBG_INDEX_(name, RED, 3, x), \
+ RGBG_INDEX_(name, BLUE, 0, x), RGBG_INDEX_(name, BLUE, 1, x), \
+ RGBG_INDEX_(name, BLUE, 2, x), RGBG_INDEX_(name, BLUE, 3, x)
+
+static const uint8_t __attribute__((aligned(16)))
+rgb_ycc_shuf_const[7][32] = {
+ { RGBG_INDEX(RGB, 0), RGBG_INDEX(RGB, 4) },
+ { RGBG_INDEX(EXT_RGB, 0), RGBG_INDEX(EXT_RGB, 4) },
+ { RGBG_INDEX(EXT_RGBX, 0), RGBG_INDEX(EXT_RGBX, 4) },
+ { RGBG_INDEX(EXT_BGR, 0), RGBG_INDEX(EXT_BGR, 4) },
+ { RGBG_INDEX(EXT_BGRX, 0), RGBG_INDEX(EXT_BGRX, 4) },
+ { RGBG_INDEX(EXT_XBGR, 0), RGBG_INDEX(EXT_XBGR, 4) },
+ { RGBG_INDEX(EXT_XRGB, 0), RGBG_INDEX(EXT_XRGB, 4) }
+};
+
+ /* rgbg0 = R0 G0 R1 G1 R2 G2 R3 G3 B0 G0 B1 G1 B2 G2 B3 G3
+ * rgbg1 = R4 G4 R5 G5 R6 G6 R7 G7 B4 G4 B5 G5 B6 G6 B7 G7
+ * rgbg2 = R8 G8 R9 G9 Ra Ga Rb Gb B8 G8 B9 G9 Ba Ga Bb Gb
+ * rgbg3 = Rc Gc Rd Gd Re Ge Rf Gf Bc Gc Bd Gd Be Ge Bf Gf
+ *
+ * rg0 = R0 G0 R1 G1 R2 G2 R3 G3
+ * bg0 = B0 G0 B1 G1 B2 G2 B3 G3
+ * ...
+ */
+
+ /* (Original)
+ * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
+ * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
+ * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
+ *
+ * (This implementation)
+ * Y = 0.29900 * R + 0.33700 * G + 0.11400 * B + 0.25000 * G
+ * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
+ * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
+ */
+
+#define CALC_Y(outptr0) \
+ rg0 = _mm_unpacklo_epi8(rgbg0, pb_zero); \
+ bg0 = _mm_unpackhi_epi8(rgbg0, pb_zero); \
+ rg1 = _mm_unpacklo_epi8(rgbg1, pb_zero); \
+ bg1 = _mm_unpackhi_epi8(rgbg1, pb_zero); \
+ rg2 = _mm_unpacklo_epi8(rgbg2, pb_zero); \
+ bg2 = _mm_unpackhi_epi8(rgbg2, pb_zero); \
+ rg3 = _mm_unpacklo_epi8(rgbg3, pb_zero); \
+ bg3 = _mm_unpackhi_epi8(rgbg3, pb_zero); \
+ \
+ /* Calculate Y values */ \
+ y0 = _mm_add_epi32(_mm_madd_epi16(rg0, pw_f0299_f0337), pd_onehalf); \
+ y1 = _mm_add_epi32(_mm_madd_epi16(rg1, pw_f0299_f0337), pd_onehalf); \
+ y2 = _mm_add_epi32(_mm_madd_epi16(rg2, pw_f0299_f0337), pd_onehalf); \
+ y3 = _mm_add_epi32(_mm_madd_epi16(rg3, pw_f0299_f0337), pd_onehalf); \
+ y0 = _mm_add_epi32(_mm_madd_epi16(bg0, pw_f0114_f0250), y0); \
+ y1 = _mm_add_epi32(_mm_madd_epi16(bg1, pw_f0114_f0250), y1); \
+ y2 = _mm_add_epi32(_mm_madd_epi16(bg2, pw_f0114_f0250), y2); \
+ y3 = _mm_add_epi32(_mm_madd_epi16(bg3, pw_f0114_f0250), y3); \
+ \
+ yl = _mm_packhi_epi32(y0, y1); \
+ yh = _mm_packhi_epi32(y2, y3); \
+ y = _mm_packus_epi16(yl, yh); \
+ VEC_ST(outptr0, y);
+
+#define CALC_CC(outptr1, outptr2) \
+ /* Calculate Cb values */ \
+ cb0 = _mm_add_epi32(_mm_madd_epi16(rg0, pw_mf016_mf033), pd_onehalfm1_cj); \
+ cb1 = _mm_add_epi32(_mm_madd_epi16(rg1, pw_mf016_mf033), pd_onehalfm1_cj); \
+ cb2 = _mm_add_epi32(_mm_madd_epi16(rg2, pw_mf016_mf033), pd_onehalfm1_cj); \
+ cb3 = _mm_add_epi32(_mm_madd_epi16(rg3, pw_mf016_mf033), pd_onehalfm1_cj); \
+ cb0 = _mm_sub_epi32(cb0, _mm_madd_epi16(bg0, pw_mf050_f000)); \
+ cb1 = _mm_sub_epi32(cb1, _mm_madd_epi16(bg1, pw_mf050_f000)); \
+ cb2 = _mm_sub_epi32(cb2, _mm_madd_epi16(bg2, pw_mf050_f000)); \
+ cb3 = _mm_sub_epi32(cb3, _mm_madd_epi16(bg3, pw_mf050_f000)); \
+ \
+ cbl = _mm_packhi_epi32(cb0, cb1); \
+ cbh = _mm_packhi_epi32(cb2, cb3); \
+ cb = _mm_packus_epi16(cbl, cbh); \
+ VEC_ST(outptr1, cb); \
+ \
+ /* Calculate Cr values */ \
+ cr0 = _mm_add_epi32(_mm_madd_epi16(bg0, pw_mf008_mf041), pd_onehalfm1_cj); \
+ cr1 = _mm_add_epi32(_mm_madd_epi16(bg1, pw_mf008_mf041), pd_onehalfm1_cj); \
+ cr2 = _mm_add_epi32(_mm_madd_epi16(bg2, pw_mf008_mf041), pd_onehalfm1_cj); \
+ cr3 = _mm_add_epi32(_mm_madd_epi16(bg3, pw_mf008_mf041), pd_onehalfm1_cj); \
+ cr0 = _mm_sub_epi32(cr0, _mm_madd_epi16(rg0, pw_mf050_f000)); \
+ cr1 = _mm_sub_epi32(cr1, _mm_madd_epi16(rg1, pw_mf050_f000)); \
+ cr2 = _mm_sub_epi32(cr2, _mm_madd_epi16(rg2, pw_mf050_f000)); \
+ cr3 = _mm_sub_epi32(cr3, _mm_madd_epi16(rg3, pw_mf050_f000)); \
+ \
+ crl = _mm_packhi_epi32(cr0, cr1); \
+ crh = _mm_packhi_epi32(cr2, cr3); \
+ cr = _mm_packus_epi16(crl, crh); \
+ VEC_ST(outptr2, cr);
+
+
+#define PIXELSIZE 3
+#define RGB_SHUFFLE \
+ rgbg0 = _mm_shuffle_epi8(rgb0, pb_shuf0); \
+ rgbg1 = _mm_shuffle_epi8(VEC_ALIGNR8(rgb1, rgb0), pb_shuf4); \
+ rgbg2 = _mm_shuffle_epi8(VEC_ALIGNR8(rgb2, rgb1), pb_shuf0); \
+ rgbg3 = _mm_shuffle_epi8(rgb2, pb_shuf4);
+
+#define rgbn_ycc_convert jsimd_rgb3_ycc_convert_e2k
+#define rgbn_gray_convert jsimd_rgb3_gray_convert_e2k
+#include "jccolext-e2k.c"
+
+
+#define PIXELSIZE 4
+#define RGB_SHUFFLE \
+ rgbg0 = _mm_shuffle_epi8(rgb0, pb_shuf0); \
+ rgbg1 = _mm_shuffle_epi8(rgb1, pb_shuf0); \
+ rgbg2 = _mm_shuffle_epi8(rgb2, pb_shuf0); \
+ rgbg3 = _mm_shuffle_epi8(rgb3, pb_shuf0);
+
+#define rgbn_ycc_convert jsimd_rgb4_ycc_convert_e2k
+#define rgbn_gray_convert jsimd_rgb4_gray_convert_e2k
+#include "jccolext-e2k.c"
+
diff --git a/simd/e2k/jchuff-e2k.c b/simd/e2k/jchuff-e2k.c
new file mode 100644
index 0000000..ec4329e
--- /dev/null
+++ b/simd/e2k/jchuff-e2k.c
@@ -0,0 +1,307 @@
+/*
+ * Elbrus optimizations for libjpeg-turbo
+ *
+ * Copyright (C) 2022, Ilya Kurdyukov <jpegqs@gmail.com> for BaseALT, Ltd
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ *
+ * NOTE: All referenced figures are from
+ * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.
+ */
+
+/* Encode a single block's worth of coefficients */
+
+#include "jsimd_e2k.h"
+
+#if __SIZEOF_SIZE_T__ != 8
+#error
+#endif
+
+typedef unsigned long long bit_buf_type;
+#define BIT_BUF_SIZE 64
+
+typedef struct {
+ bit_buf_type put_buffer; /* current bit accumulation buffer */
+ int free_bits; /* # of bits available in it */
+ int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
+} savable_state;
+
+typedef struct {
+ JOCTET *next_output_byte; /* => next byte to write in buffer */
+ size_t free_in_buffer; /* # of byte spaces remaining in buffer */
+ savable_state cur; /* Current bit buffer & DC state */
+ j_compress_ptr cinfo; /* dump_buffer needs access to this */
+ int simd;
+} working_state;
+
+#define EMIT_BYTE(b) { \
+ buffer[0] = (JOCTET)(b); \
+ buffer[1] = 0; \
+ buffer -= -2 + ((JOCTET)(b) < 0xFF); \
+}
+
+#define FLUSH() { \
+ if (put_buffer & 0x8080808080808080 & ~(put_buffer + 0x0101010101010101)) { \
+ EMIT_BYTE(put_buffer >> 56) \
+ EMIT_BYTE(put_buffer >> 48) \
+ EMIT_BYTE(put_buffer >> 40) \
+ EMIT_BYTE(put_buffer >> 32) \
+ EMIT_BYTE(put_buffer >> 24) \
+ EMIT_BYTE(put_buffer >> 16) \
+ EMIT_BYTE(put_buffer >> 8) \
+ EMIT_BYTE(put_buffer ) \
+ } else { \
+ *(uint64_t*)buffer = __builtin_bswap64(put_buffer); \
+ buffer += 8; \
+ } \
+}
+
+#define PUT_AND_FLUSH(code, size) { \
+ put_buffer = (put_buffer << (size + free_bits)) | (code >> -free_bits); \
+ FLUSH() \
+ free_bits += BIT_BUF_SIZE; \
+ put_buffer = code; \
+}
+
+#define PUT_BITS(code, size) { \
+ free_bits -= size; \
+ if (free_bits < 0) \
+ PUT_AND_FLUSH(code, size) \
+ else \
+ put_buffer = (put_buffer << size) | code; \
+}
+
+#define PUT_CODE(code, size) { \
+ /* temp &= (((JLONG)1) << nbits) - 1; */ \
+ /* temp |= code << nbits; */ \
+ temp = __builtin_e2k_insfd(code, __builtin_e2k_insfd(nbits, 6 * 63 + 64, -nbits), temp); \
+ nbits += size; \
+ PUT_BITS(temp, nbits) \
+}
+
+#define KLOOP_PREPARE(mask, i) \
+ t0 = _mm_cmpeq_epi8(_mm_packs_epi16(v0, v1), zero); \
+ t1 = _mm_cmpeq_epi8(_mm_packs_epi16(v2, v3), zero); \
+ mask = (uint32_t)(_mm_movemask_epi8(t0) | _mm_movemask_epi8(t1) << 16); \
+ t0 = _mm_add_epi16(_mm_srai_epi16(v0, 15), v0); \
+ t1 = _mm_add_epi16(_mm_srai_epi16(v1, 15), v1); \
+ t2 = _mm_add_epi16(_mm_srai_epi16(v2, 15), v2); \
+ t3 = _mm_add_epi16(_mm_srai_epi16(v3, 15), v3); \
+ v0 = _mm_abs_epi16(v0); \
+ v1 = _mm_abs_epi16(v1); \
+ v2 = _mm_abs_epi16(v2); \
+ v3 = _mm_abs_epi16(v3); \
+ VEC_ST(block_nbits + i, v0); \
+ VEC_ST(block_nbits + i + 8, v1); \
+ VEC_ST(block_nbits + i + 16, v2); \
+ VEC_ST(block_nbits + i + 24, v3); \
+ VEC_ST(block_diff + i, t0); \
+ VEC_ST(block_diff + i + 8, t1); \
+ VEC_ST(block_diff + i + 16, t2); \
+ VEC_ST(block_diff + i + 24, t3);
+
+#define SHUF16X4(a, b, c, d) _mm_setr_pi8( \
+ a * 2, a * 2 + 1, b * 2, b * 2 + 1, c * 2, c * 2 + 1, d * 2, d * 2 + 1)
+#define VEC_COMBINE(h0, h1) _mm_unpacklo_epi64( \
+ _mm_movpi64_epi64(h0), _mm_movpi64_epi64(h1))
+#define INSFI_M64(a, b, c, d) _mm_cvtsi64_m64(__builtin_e2k_insfd( \
+ _mm_cvtm64_si64(a), (b & 63) | (d & 63) << 6, _mm_cvtm64_si64(c)))
+
+GLOBAL(JOCTET *)
+jsimd_huff_encode_one_block_e2k(void *state, JOCTET *buffer,
+ JCOEFPTR block, int last_dc_val,
+ c_derived_tbl *dctbl, c_derived_tbl *actbl) {
+ uint64_t temp, nbits;
+ uint64_t i, r, code, size;
+ uint64_t code_0xf0 = actbl->ehufco[0xf0];
+ uint64_t size_0xf0 = actbl->ehufsi[0xf0];
+
+ working_state *state_ptr = (working_state*)state;
+ bit_buf_type put_buffer = state_ptr->cur.put_buffer;
+ int64_t free_bits = state_ptr->cur.free_bits;
+
+ __m128i zero = _mm_setzero_si128();
+ __m128i v0, v1, v2, v3, t0, t1, t2, t3;
+ int64_t mask, mask1;
+ uint16_t __attribute__((aligned(16))) block_nbits[DCTSIZE2];
+ int16_t __attribute__((aligned(16))) block_diff[DCTSIZE2];
+
+#if 1 /* faster this way */
+ {
+ __m64 d0l, d0h, d1l, d1h, d2l, d2h, d3l, d3h;
+ __m64 d4l, d4h, d5l, d5h, d6l, d6h, d7l, d7h;
+ __m64 h0, h1, h2, h3, r0, r1, c1256 = SHUF16X4(1, 2, 5, 6);
+
+ d0l = *(__m64*)(block + 8 * 0); d0h = *(__m64*)(block + 8 * 0 + 4); // 0 4
+ d1l = *(__m64*)(block + 8 * 1); d1h = *(__m64*)(block + 8 * 1 + 4); // 8 12
+ d2l = *(__m64*)(block + 8 * 2); d2h = *(__m64*)(block + 8 * 2 + 4); // 16 20
+ d3l = *(__m64*)(block + 8 * 3); d3h = *(__m64*)(block + 8 * 3 + 4); // 24 28
+ d4l = *(__m64*)(block + 8 * 4); d4h = *(__m64*)(block + 8 * 4 + 4); // 32 36
+ d5l = *(__m64*)(block + 8 * 5); d5h = *(__m64*)(block + 8 * 5 + 4); // 40 44
+ d6l = *(__m64*)(block + 8 * 6); d6h = *(__m64*)(block + 8 * 6 + 4); // 48 52
+ d7l = *(__m64*)(block + 8 * 7); d7h = *(__m64*)(block + 8 * 7 + 4); // 56 60
+
+ // d0l[0] d0l[1] d1l[0] d2l[0]
+ // d1l[1] d0l[2] d0l[3] d1l[2]
+ h0 = _mm_unpacklo_pi16(d1l, d2l);
+ r0 = _mm_unpacklo_pi32(d0l, h0);
+ r1 = _mm_shuffle2_pi8(d1l, d0l, SHUF16X4(1, 6, 7, 2));
+ r0 = _mm_sub_pi16(r0, _mm_cvtsi64_m64((uint16_t)last_dc_val));
+ v0 = VEC_COMBINE(r0, r1);
+
+ // d2l[1] d3l[0] d4l[0] d3l[1]
+ // d2l[2] d1l[3] d0h[0] d0h[1]
+ h0 = _mm_srli_si64(_mm_unpacklo_pi32(d2l, d4l), 16);
+ h2 = INSFI_M64(d1l, 0, d2l, 48);
+ r0 = _mm_unpacklo_pi16(h0, d3l);
+ r1 = _mm_alignr_pi8(d0h, h2, 4);
+ v1 = VEC_COMBINE(r0, r1);
+
+ // d1h[0] d2l[3] d3l[2] d4l[1]
+ // d5l[0] d6l[0] d5l[1] d4l[2]
+ h0 = INSFI_M64(d2l, 32, d1h, 16);
+ h1 = INSFI_M64(d4l, -32, d3l, 48);
+ h2 = INSFI_M64(d4l, 16, d6l, 16);
+ r0 = INSFI_M64(h1, 0, h0, 32);
+ r1 = _mm_unpacklo_pi16(d5l, h2);
+ v2 = VEC_COMBINE(r0, r1);
+
+ // d3l[3] d2h[0] d1h[1] d0h[2]
+ // d0h[3] d1h[2] d2h[1] d3h[0]
+ h0 = _mm_alignr_pi8(d2h, d3l, 6);
+ h1 = INSFI_M64(d0h, 0, d1h, 32);
+ h2 = _mm_unpackhi_pi32(d0h, d1h);
+ h3 = _mm_unpacklo_pi32(d2h, d3h);
+ r0 = INSFI_M64(h1, -16, h0, 32);
+ r1 = _mm_shuffle2_pi8(h2, h3, c1256);
+ v3 = VEC_COMBINE(r0, r1);
+
+ KLOOP_PREPARE(mask, 0)
+
+ // d4l[3] d5l[2] d6l[1] d7l[0]
+ // d7l[1] d6l[2] d5l[3] d4h[0]
+ h0 = _mm_unpackhi_pi32(d4l, d5l);
+ h1 = _mm_unpacklo_pi32(d6l, d7l);
+ h2 = INSFI_M64(d6l, 0, d7l, 32);
+ h2 = INSFI_M64(d5l, 0, h2, 48);
+ r0 = _mm_shuffle2_pi8(h0, h1, c1256);
+ r1 = _mm_alignr_pi8(d4h, h2, 2);
+ v0 = VEC_COMBINE(r0, r1);
+
+ // d3h[1] d2h[2] d1h[3] d2h[3]
+ // d3h[2] d4h[1] d5h[0] d6l[3]
+ h0 = _mm_slli_si64(INSFI_M64(d1h, 16, d3h, 32), 16);
+ h2 = INSFI_M64(d4h, -32, d3h, 48);
+ h3 = INSFI_M64(d6l, 32, d5h, 16);
+ r0 = _mm_unpackhi_pi16(h0, d2h);
+ r1 = _mm_alignr_pi8(h3, h2, 4);
+ v1 = VEC_COMBINE(r0, r1);
+
+ // d7l[2] d7l[3] d6h[0] d5h[1]
+ // d4h[2] d3h[3] d4h[3] d5h[2]
+ h0 = INSFI_M64(d5h, 0, d6h, 16);
+ h2 = _mm_slli_si64(_mm_unpackhi_pi32(d3h, d5h), 16);
+ r0 = _mm_alignr_pi8(h0, d7l, 4);
+ r1 = _mm_unpackhi_pi16(d4h, h2);
+ v2 = VEC_COMBINE(r0, r1);
+
+ // d6h[1] d7h[0] d7h[1] d6h[2]
+ // d5h[3] d6h[3] d7h[2] d7h[3]
+ h0 = INSFI_M64(d6h, -16, d7h, 32);
+ h2 = _mm_unpackhi_pi16(d5h, d6h);
+ r0 = _mm_shuffle_pi16(h0, 0xd2);
+ r1 = _mm_unpackhi_pi32(h2, d7h);
+ v3 = VEC_COMBINE(r0, r1);
+ }
+#else
+ v0 = _mm_setr_epi16(
+ block[0] - last_dc_val, block[1], block[8], block[16],
+ block[9], block[2], block[3], block[10]);
+ v1 = _mm_setr_epi16(
+ block[17], block[24], block[32], block[25],
+ block[18], block[11], block[4], block[5]);
+ v2 = _mm_setr_epi16(
+ block[12], block[19], block[26], block[33],
+ block[40], block[48], block[41], block[34]);
+ v3 = _mm_setr_epi16(
+ block[27], block[20], block[13], block[6],
+ block[7], block[14], block[21], block[28]);
+
+ KLOOP_PREPARE(mask, 0)
+
+ v0 = _mm_setr_epi16(
+ block[35], block[42], block[49], block[56],
+ block[57], block[50], block[43], block[36]);
+ v1 = _mm_setr_epi16(
+ block[29], block[22], block[15], block[23],
+ block[30], block[37], block[44], block[51]);
+ v2 = _mm_setr_epi16(
+ block[58], block[59], block[52], block[45],
+ block[38], block[31], block[39], block[46]);
+ v3 = _mm_setr_epi16(
+ block[53], block[60], block[61], block[54],
+ block[47], block[55], block[62], block[63]);
+#endif
+
+ KLOOP_PREPARE(mask1, 32)
+ mask |= mask1 << 32;
+ mask = ~mask;
+
+ /* Encode the DC coefficient difference per section F.1.2.1 */
+
+ nbits = block_nbits[0];
+ temp = block_diff[0];
+ nbits = nbits ? 32 - __builtin_clz(nbits) : 0;
+
+ /* Emit the Huffman-coded symbol for the number of bits */
+ code = dctbl->ehufco[nbits];
+ size = dctbl->ehufsi[nbits];
+ PUT_CODE(code, size)
+
+ /* Encode the AC coefficients per section F.1.2.2 */
+
+ /* e2k doesn't have a tzcnt instruction */
+ mask = __builtin_e2k_bitrevd(mask) << 1;
+
+ for (i = 1; mask; i++, mask <<= 1) {
+ r = __builtin_clzll(mask);
+ mask <<= r;
+ i += r;
+ /* if run length > 15, must emit special run-length-16 codes (0xF0) */
+ while (r > 15) {
+ PUT_BITS(code_0xf0, size_0xf0)
+ r -= 16;
+ }
+ nbits = block_nbits[i];
+ temp = block_diff[i];
+ nbits = 32 - __builtin_clz(nbits);
+ /* Emit Huffman symbol for run length / number of bits */
+ /* r = r << 4 | nbits; */
+ r = __builtin_e2k_insfd(r, 4 * 63 + 64, nbits);
+ code = actbl->ehufco[r];
+ size = actbl->ehufsi[r];
+ PUT_CODE(code, size)
+ }
+
+ if (i != 64) {
+ PUT_BITS(actbl->ehufco[0], actbl->ehufsi[0])
+ }
+
+ state_ptr->cur.put_buffer = put_buffer;
+ state_ptr->cur.free_bits = free_bits;
+ return buffer;
+}
diff --git a/simd/e2k/jcphuff-e2k.c b/simd/e2k/jcphuff-e2k.c
new file mode 100644
index 0000000..f69afeb
--- /dev/null
+++ b/simd/e2k/jcphuff-e2k.c
@@ -0,0 +1,145 @@
+/*
+ * Elbrus optimizations for libjpeg-turbo
+ *
+ * Copyright (C) 2022, Ilya Kurdyukov <jpegqs@gmail.com> for BaseALT, Ltd
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ */
+
+#include "jsimd_e2k.h"
+
+#define X(i) coefs[i] = block[jpeg_natural_order_start[i]];
+#define Y(i) coefs[i] = i < rem ? block[jpeg_natural_order_start[i]] : 0;
+
+#define LOOP \
+ for (i = 0; i < Sl >> 4; i++) { \
+ X(0) X(1) X(2) X(3) X(4) X(5) X(6) X(7) \
+ X(8) X(9) X(10) X(11) X(12) X(13) X(14) X(15) \
+ BLOCK16 \
+ jpeg_natural_order_start += 16; \
+ } \
+ rem = Sl & 15; \
+ if (Sl & 8) { \
+ X(0) X(1) X(2) X(3) X(4) X(5) X(6) X(7) \
+ Y(8) Y(9) Y(10) Y(11) Y(12) Y(13) Y(14) \
+ coefs[15] = 0; \
+ BLOCK16 \
+ } else if (rem > 0) { \
+ Y(0) Y(1) Y(2) Y(3) Y(4) Y(5) Y(6) Y(7) \
+ BLOCK8 \
+ }
+
+void jsimd_encode_mcu_AC_first_prepare_e2k
+ (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
+ JCOEF *values, size_t *zerobits)
+{
+ JCOEF *diff = values + DCTSIZE2;
+ int16_t __attribute__((aligned(16))) coefs[16];
+ __m128i v0, v1, v2, v3;
+ __m128i c0 = _mm_setzero_si128(), shr = _mm_cvtsi32_si128(Al);
+ int i, rem;
+
+#define BLOCK16 \
+ v0 = _mm_load_si128((__m128i*)coefs); \
+ v1 = _mm_load_si128((__m128i*)coefs + 1); \
+ v2 = _mm_srai_epi16(v0, 15); \
+ v3 = _mm_srai_epi16(v1, 15); \
+ v0 = _mm_sra_epi16(_mm_abs_epi16(v0), shr); \
+ v1 = _mm_sra_epi16(_mm_abs_epi16(v1), shr); \
+ v2 = _mm_xor_si128(v0, v2); \
+ v3 = _mm_xor_si128(v1, v3); \
+ _mm_store_si128((__m128i*)values, v0); \
+ _mm_store_si128((__m128i*)values + 1, v1); \
+ _mm_store_si128((__m128i*)diff, v2); \
+ _mm_store_si128((__m128i*)diff + 1, v3); \
+ v2 = _mm_cmpeq_epi8(_mm_packus_epi16(v0, v1), c0); \
+ ((uint16_t*)zerobits)[i] = ~_mm_movemask_epi8(v2); \
+ values += 16; diff += 16;
+
+#define BLOCK8 \
+ v0 = _mm_load_si128((__m128i*)coefs); \
+ v2 = _mm_srai_epi16(v0, 15); \
+ v0 = _mm_sra_epi16(_mm_abs_epi16(v0), shr); \
+ v2 = _mm_xor_si128(v0, v2); \
+ _mm_store_si128((__m128i*)values, v0); \
+ _mm_store_si128((__m128i*)diff, v2); \
+ v2 = _mm_cmpeq_epi8(_mm_packus_epi16(v0, c0), c0); \
+ ((uint16_t*)zerobits)[i] = ~_mm_movemask_epi8(v2); \
+ values += 8; diff += 8;
+
+ ((uint64_t*)zerobits)[0] = 0;
+ LOOP
+#undef BLOCK16
+#undef BLOCK8
+
+ for (i = (64 - Sl) >> 3; i; i--) {
+ _mm_store_si128((__m128i*)values, c0);
+ _mm_store_si128((__m128i*)diff, c0);
+ values += 8; diff += 8;
+ }
+}
+
+int jsimd_encode_mcu_AC_refine_prepare_e2k
+ (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
+ JCOEF *absvalues, size_t *bits)
+{
+ union { uint64_t q; uint16_t w[4]; } mask1 = { 0 };
+ int16_t __attribute__((aligned(16))) coefs[16];
+ __m128i v0, v1, v2, c1 = _mm_set1_epi8(1);
+ __m128i c0 = _mm_setzero_si128(), shr = _mm_cvtsi32_si128(Al);
+ int i, rem;
+
+#define BLOCK16 \
+ v0 = _mm_load_si128((__m128i*)coefs); \
+ v1 = _mm_load_si128((__m128i*)coefs + 1); \
+ v2 = _mm_packs_epi16(v0, v1); \
+ ((uint16_t*)bits)[4 + i] = ~_mm_movemask_epi8(v2); \
+ v0 = _mm_sra_epi16(_mm_abs_epi16(v0), shr); \
+ v1 = _mm_sra_epi16(_mm_abs_epi16(v1), shr); \
+ v2 = _mm_cmpeq_epi8(_mm_packus_epi16(v0, v1), c0); \
+ ((uint16_t*)bits)[i] = ~_mm_movemask_epi8(v2); \
+ _mm_store_si128((__m128i*)absvalues, v0); \
+ _mm_store_si128((__m128i*)absvalues + 1, v1); \
+ v2 = _mm_cmpeq_epi8(_mm_packus_epi16(v0, v1), c1); \
+ mask1.w[i] = _mm_movemask_epi8(v2); \
+ absvalues += 16;
+
+#define BLOCK8 \
+ v0 = _mm_load_si128((__m128i*)coefs); \
+ v2 = _mm_packs_epi16(v0, c0); \
+ ((uint16_t*)bits)[4 + i] = ~_mm_movemask_epi8(v2); \
+ v0 = _mm_sra_epi16(_mm_abs_epi16(v0), shr); \
+ v2 = _mm_cmpeq_epi8(_mm_packus_epi16(v0, c0), c0); \
+ ((uint16_t*)bits)[i] = ~_mm_movemask_epi8(v2); \
+ _mm_store_si128((__m128i*)absvalues, v0); \
+ v2 = _mm_cmpeq_epi8(_mm_packus_epi16(v0, c0), c1); \
+ mask1.w[i] = _mm_movemask_epi8(v2); \
+ absvalues += 8;
+
+ ((uint64_t*)bits)[0] = 0; /* zero */
+ ((uint64_t*)bits)[1] = 0; /* sign */
+ LOOP
+#undef BLOCK16
+#undef BLOCK8
+
+ for (i = (64 - Sl) >> 3; i; i--) {
+ _mm_store_si128((__m128i*)absvalues, c0);
+ absvalues += 8;
+ }
+
+ return 63 - __builtin_clzll(mask1.q | 1);
+}
diff --git a/simd/e2k/jcsample-e2k.c b/simd/e2k/jcsample-e2k.c
new file mode 100644
index 0000000..cac8897
--- /dev/null
+++ b/simd/e2k/jcsample-e2k.c
@@ -0,0 +1,203 @@
+/*
+ * Elbrus optimizations for libjpeg-turbo
+ *
+ * Copyright (C) 2015, D. R. Commander. All Rights Reserved.
+ * Copyright (C) 2021, Ilya Kurdyukov <jpegqs@gmail.com> for BaseALT, Ltd
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ */
+
+/* CHROMA DOWNSAMPLING */
+
+#include "jsimd_e2k.h"
+#include "jcsample.h"
+
+void jsimd_h2v1_downsample_e2k(JDIMENSION image_width,
+ int max_v_samp_factor,
+ JDIMENSION v_samp_factor,
+ JDIMENSION width_in_blocks,
+ JSAMPARRAY input_data,