4 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
5 * Copyright (C) 2009-2011, 2014, 2016, D. R. Commander.
6 * Copyright (C) 2015, Matthieu Darbois.
8 * Based on the x86 SIMD extension for IJG JPEG library,
9 * Copyright (C) 1999-2006, MIYASAKA Masaru.
10 * For conditions of distribution and use, see copyright notice in jsimdext.inc
12 * This file contains the interface between the "normal" portions
13 * of the library and the SIMD implementations when running on a
14 * 64-bit x86 architecture.
17 #define JPEG_INTERNALS
18 #include "../jinclude.h"
19 #include "../jpeglib.h"
22 #include "../jsimddct.h"
26 * In the PIC cases, we have no guarantee that constants will keep
27 * their alignment. This macro allows us to verify it at runtime.
29 #define IS_ALIGNED(ptr, order) (((size_t)ptr & ((1 << order) - 1)) == 0)
31 #define IS_ALIGNED_SSE(ptr) (IS_ALIGNED(ptr, 4)) /* 16 byte alignment */
33 static unsigned int simd_support = ~0;
34 static unsigned int simd_huffman = 1;
37 * Check what SIMD accelerations are supported.
39 * FIXME: This code is racy under a multi-threaded environment.
46 if (simd_support != ~0U)
49 simd_support = JSIMD_SSE2 | JSIMD_SSE;
51 /* Force different settings through environment variables */
52 env = getenv("JSIMD_FORCENONE");
53 if ((env != NULL) && (strcmp(env, "1") == 0))
55 env = getenv("JSIMD_NOHUFFENC");
56 if ((env != NULL) && (strcmp(env, "1") == 0))
61 jsimd_can_rgb_ycc (void)
65 /* The code is optimised for these values only */
66 if (BITS_IN_JSAMPLE != 8)
68 if (sizeof(JDIMENSION) != 4)
70 if ((RGB_PIXELSIZE != 3) && (RGB_PIXELSIZE != 4))
73 if ((simd_support & JSIMD_SSE2) &&
74 IS_ALIGNED_SSE(jconst_rgb_ycc_convert_sse2))
81 jsimd_can_rgb_gray (void)
85 /* The code is optimised for these values only */
86 if (BITS_IN_JSAMPLE != 8)
88 if (sizeof(JDIMENSION) != 4)
90 if ((RGB_PIXELSIZE != 3) && (RGB_PIXELSIZE != 4))
93 if ((simd_support & JSIMD_SSE2) &&
94 IS_ALIGNED_SSE(jconst_rgb_gray_convert_sse2))
101 jsimd_can_ycc_rgb (void)
105 /* The code is optimised for these values only */
106 if (BITS_IN_JSAMPLE != 8)
108 if (sizeof(JDIMENSION) != 4)
110 if ((RGB_PIXELSIZE != 3) && (RGB_PIXELSIZE != 4))
113 if ((simd_support & JSIMD_SSE2) &&
114 IS_ALIGNED_SSE(jconst_ycc_rgb_convert_sse2))
121 jsimd_can_ycc_rgb565 (void)
127 jsimd_rgb_ycc_convert (j_compress_ptr cinfo,
128 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
129 JDIMENSION output_row, int num_rows)
131 void (*sse2fct)(JDIMENSION, JSAMPARRAY, JSAMPIMAGE, JDIMENSION, int);
133 switch(cinfo->in_color_space) {
135 sse2fct=jsimd_extrgb_ycc_convert_sse2;
139 sse2fct=jsimd_extrgbx_ycc_convert_sse2;
142 sse2fct=jsimd_extbgr_ycc_convert_sse2;
146 sse2fct=jsimd_extbgrx_ycc_convert_sse2;
150 sse2fct=jsimd_extxbgr_ycc_convert_sse2;
154 sse2fct=jsimd_extxrgb_ycc_convert_sse2;
157 sse2fct=jsimd_rgb_ycc_convert_sse2;
161 sse2fct(cinfo->image_width, input_buf, output_buf, output_row, num_rows);
165 jsimd_rgb_gray_convert (j_compress_ptr cinfo,
166 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
167 JDIMENSION output_row, int num_rows)
169 void (*sse2fct)(JDIMENSION, JSAMPARRAY, JSAMPIMAGE, JDIMENSION, int);
171 switch(cinfo->in_color_space) {
173 sse2fct=jsimd_extrgb_gray_convert_sse2;
177 sse2fct=jsimd_extrgbx_gray_convert_sse2;
180 sse2fct=jsimd_extbgr_gray_convert_sse2;
184 sse2fct=jsimd_extbgrx_gray_convert_sse2;
188 sse2fct=jsimd_extxbgr_gray_convert_sse2;
192 sse2fct=jsimd_extxrgb_gray_convert_sse2;
195 sse2fct=jsimd_rgb_gray_convert_sse2;
199 sse2fct(cinfo->image_width, input_buf, output_buf, output_row, num_rows);
203 jsimd_ycc_rgb_convert (j_decompress_ptr cinfo,
204 JSAMPIMAGE input_buf, JDIMENSION input_row,
205 JSAMPARRAY output_buf, int num_rows)
207 void (*sse2fct)(JDIMENSION, JSAMPIMAGE, JDIMENSION, JSAMPARRAY, int);
209 switch(cinfo->out_color_space) {
211 sse2fct=jsimd_ycc_extrgb_convert_sse2;
215 sse2fct=jsimd_ycc_extrgbx_convert_sse2;
218 sse2fct=jsimd_ycc_extbgr_convert_sse2;
222 sse2fct=jsimd_ycc_extbgrx_convert_sse2;
226 sse2fct=jsimd_ycc_extxbgr_convert_sse2;
230 sse2fct=jsimd_ycc_extxrgb_convert_sse2;
233 sse2fct=jsimd_ycc_rgb_convert_sse2;
237 sse2fct(cinfo->output_width, input_buf, input_row, output_buf, num_rows);
241 jsimd_ycc_rgb565_convert (j_decompress_ptr cinfo,
242 JSAMPIMAGE input_buf, JDIMENSION input_row,
243 JSAMPARRAY output_buf, int num_rows)
248 jsimd_can_h2v2_downsample (void)
252 /* The code is optimised for these values only */
253 if (BITS_IN_JSAMPLE != 8)
255 if (sizeof(JDIMENSION) != 4)
258 if (simd_support & JSIMD_SSE2)
265 jsimd_can_h2v1_downsample (void)
269 /* The code is optimised for these values only */
270 if (BITS_IN_JSAMPLE != 8)
272 if (sizeof(JDIMENSION) != 4)
275 if (simd_support & JSIMD_SSE2)
282 jsimd_h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
283 JSAMPARRAY input_data, JSAMPARRAY output_data)
285 jsimd_h2v2_downsample_sse2(cinfo->image_width, cinfo->max_v_samp_factor,
286 compptr->v_samp_factor, compptr->width_in_blocks,
287 input_data, output_data);
291 jsimd_h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
292 JSAMPARRAY input_data, JSAMPARRAY output_data)
294 jsimd_h2v1_downsample_sse2(cinfo->image_width, cinfo->max_v_samp_factor,
295 compptr->v_samp_factor, compptr->width_in_blocks,
296 input_data, output_data);
300 jsimd_can_h2v2_upsample (void)
304 /* The code is optimised for these values only */
305 if (BITS_IN_JSAMPLE != 8)
307 if (sizeof(JDIMENSION) != 4)
310 if (simd_support & JSIMD_SSE2)
317 jsimd_can_h2v1_upsample (void)
321 /* The code is optimised for these values only */
322 if (BITS_IN_JSAMPLE != 8)
324 if (sizeof(JDIMENSION) != 4)
327 if (simd_support & JSIMD_SSE2)
334 jsimd_h2v2_upsample (j_decompress_ptr cinfo,
335 jpeg_component_info *compptr,
336 JSAMPARRAY input_data,
337 JSAMPARRAY *output_data_ptr)
339 jsimd_h2v2_upsample_sse2(cinfo->max_v_samp_factor, cinfo->output_width,
340 input_data, output_data_ptr);
344 jsimd_h2v1_upsample (j_decompress_ptr cinfo,
345 jpeg_component_info *compptr,
346 JSAMPARRAY input_data,
347 JSAMPARRAY *output_data_ptr)
349 jsimd_h2v1_upsample_sse2(cinfo->max_v_samp_factor, cinfo->output_width,
350 input_data, output_data_ptr);
354 jsimd_can_h2v2_fancy_upsample (void)
358 /* The code is optimised for these values only */
359 if (BITS_IN_JSAMPLE != 8)
361 if (sizeof(JDIMENSION) != 4)
364 if ((simd_support & JSIMD_SSE2) &&
365 IS_ALIGNED_SSE(jconst_fancy_upsample_sse2))
372 jsimd_can_h2v1_fancy_upsample (void)
376 /* The code is optimised for these values only */
377 if (BITS_IN_JSAMPLE != 8)
379 if (sizeof(JDIMENSION) != 4)
382 if ((simd_support & JSIMD_SSE2) &&
383 IS_ALIGNED_SSE(jconst_fancy_upsample_sse2))
390 jsimd_h2v2_fancy_upsample (j_decompress_ptr cinfo,
391 jpeg_component_info *compptr,
392 JSAMPARRAY input_data,
393 JSAMPARRAY *output_data_ptr)
395 jsimd_h2v2_fancy_upsample_sse2(cinfo->max_v_samp_factor,
396 compptr->downsampled_width, input_data,
401 jsimd_h2v1_fancy_upsample (j_decompress_ptr cinfo,
402 jpeg_component_info *compptr,
403 JSAMPARRAY input_data,
404 JSAMPARRAY *output_data_ptr)
406 jsimd_h2v1_fancy_upsample_sse2(cinfo->max_v_samp_factor,
407 compptr->downsampled_width, input_data,
412 jsimd_can_h2v2_merged_upsample (void)
416 /* The code is optimised for these values only */
417 if (BITS_IN_JSAMPLE != 8)
419 if (sizeof(JDIMENSION) != 4)
422 if ((simd_support & JSIMD_SSE2) &&
423 IS_ALIGNED_SSE(jconst_merged_upsample_sse2))
430 jsimd_can_h2v1_merged_upsample (void)
434 /* The code is optimised for these values only */
435 if (BITS_IN_JSAMPLE != 8)
437 if (sizeof(JDIMENSION) != 4)
440 if ((simd_support & JSIMD_SSE2) &&
441 IS_ALIGNED_SSE(jconst_merged_upsample_sse2))
448 jsimd_h2v2_merged_upsample (j_decompress_ptr cinfo,
449 JSAMPIMAGE input_buf,
450 JDIMENSION in_row_group_ctr,
451 JSAMPARRAY output_buf)
453 void (*sse2fct)(JDIMENSION, JSAMPIMAGE, JDIMENSION, JSAMPARRAY);
455 switch(cinfo->out_color_space) {
457 sse2fct=jsimd_h2v2_extrgb_merged_upsample_sse2;
461 sse2fct=jsimd_h2v2_extrgbx_merged_upsample_sse2;
464 sse2fct=jsimd_h2v2_extbgr_merged_upsample_sse2;
468 sse2fct=jsimd_h2v2_extbgrx_merged_upsample_sse2;
472 sse2fct=jsimd_h2v2_extxbgr_merged_upsample_sse2;
476 sse2fct=jsimd_h2v2_extxrgb_merged_upsample_sse2;
479 sse2fct=jsimd_h2v2_merged_upsample_sse2;
483 sse2fct(cinfo->output_width, input_buf, in_row_group_ctr, output_buf);
487 jsimd_h2v1_merged_upsample (j_decompress_ptr cinfo,
488 JSAMPIMAGE input_buf,
489 JDIMENSION in_row_group_ctr,
490 JSAMPARRAY output_buf)
492 void (*sse2fct)(JDIMENSION, JSAMPIMAGE, JDIMENSION, JSAMPARRAY);
494 switch(cinfo->out_color_space) {
496 sse2fct=jsimd_h2v1_extrgb_merged_upsample_sse2;
500 sse2fct=jsimd_h2v1_extrgbx_merged_upsample_sse2;
503 sse2fct=jsimd_h2v1_extbgr_merged_upsample_sse2;
507 sse2fct=jsimd_h2v1_extbgrx_merged_upsample_sse2;
511 sse2fct=jsimd_h2v1_extxbgr_merged_upsample_sse2;
515 sse2fct=jsimd_h2v1_extxrgb_merged_upsample_sse2;
518 sse2fct=jsimd_h2v1_merged_upsample_sse2;
522 sse2fct(cinfo->output_width, input_buf, in_row_group_ctr, output_buf);
526 jsimd_can_convsamp (void)
530 /* The code is optimised for these values only */
533 if (BITS_IN_JSAMPLE != 8)
535 if (sizeof(JDIMENSION) != 4)
537 if (sizeof(DCTELEM) != 2)
540 if (simd_support & JSIMD_SSE2)
547 jsimd_can_convsamp_float (void)
551 /* The code is optimised for these values only */
554 if (BITS_IN_JSAMPLE != 8)
556 if (sizeof(JDIMENSION) != 4)
558 if (sizeof(FAST_FLOAT) != 4)
561 if (simd_support & JSIMD_SSE2)
568 jsimd_convsamp (JSAMPARRAY sample_data, JDIMENSION start_col,
571 jsimd_convsamp_sse2(sample_data, start_col, workspace);
575 jsimd_convsamp_float (JSAMPARRAY sample_data, JDIMENSION start_col,
576 FAST_FLOAT *workspace)
578 jsimd_convsamp_float_sse2(sample_data, start_col, workspace);
582 jsimd_can_fdct_islow (void)
586 /* The code is optimised for these values only */
589 if (sizeof(DCTELEM) != 2)
592 if ((simd_support & JSIMD_SSE2) && IS_ALIGNED_SSE(jconst_fdct_islow_sse2))
599 jsimd_can_fdct_ifast (void)
603 /* The code is optimised for these values only */
606 if (sizeof(DCTELEM) != 2)
609 if ((simd_support & JSIMD_SSE2) && IS_ALIGNED_SSE(jconst_fdct_ifast_sse2))
616 jsimd_can_fdct_float (void)
620 /* The code is optimised for these values only */
623 if (sizeof(FAST_FLOAT) != 4)
626 if ((simd_support & JSIMD_SSE) && IS_ALIGNED_SSE(jconst_fdct_float_sse))
633 jsimd_fdct_islow (DCTELEM *data)
635 jsimd_fdct_islow_sse2(data);
639 jsimd_fdct_ifast (DCTELEM *data)
641 jsimd_fdct_ifast_sse2(data);
645 jsimd_fdct_float (FAST_FLOAT *data)
647 jsimd_fdct_float_sse(data);
651 jsimd_can_quantize (void)
655 /* The code is optimised for these values only */
658 if (sizeof(JCOEF) != 2)
660 if (sizeof(DCTELEM) != 2)
663 if (simd_support & JSIMD_SSE2)
670 jsimd_can_quantize_float (void)
674 /* The code is optimised for these values only */
677 if (sizeof(JCOEF) != 2)
679 if (sizeof(FAST_FLOAT) != 4)
682 if (simd_support & JSIMD_SSE2)
689 jsimd_quantize (JCOEFPTR coef_block, DCTELEM *divisors,
692 jsimd_quantize_sse2(coef_block, divisors, workspace);
696 jsimd_quantize_float (JCOEFPTR coef_block, FAST_FLOAT *divisors,
697 FAST_FLOAT *workspace)
699 jsimd_quantize_float_sse2(coef_block, divisors, workspace);
703 jsimd_can_idct_2x2 (void)
707 /* The code is optimised for these values only */
710 if (sizeof(JCOEF) != 2)
712 if (BITS_IN_JSAMPLE != 8)
714 if (sizeof(JDIMENSION) != 4)
716 if (sizeof(ISLOW_MULT_TYPE) != 2)
719 if ((simd_support & JSIMD_SSE2) && IS_ALIGNED_SSE(jconst_idct_red_sse2))
726 jsimd_can_idct_4x4 (void)
730 /* The code is optimised for these values only */
733 if (sizeof(JCOEF) != 2)
735 if (BITS_IN_JSAMPLE != 8)
737 if (sizeof(JDIMENSION) != 4)
739 if (sizeof(ISLOW_MULT_TYPE) != 2)
742 if ((simd_support & JSIMD_SSE2) && IS_ALIGNED_SSE(jconst_idct_red_sse2))
749 jsimd_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
750 JCOEFPTR coef_block, JSAMPARRAY output_buf,
751 JDIMENSION output_col)
753 jsimd_idct_2x2_sse2(compptr->dct_table, coef_block, output_buf, output_col);
757 jsimd_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
758 JCOEFPTR coef_block, JSAMPARRAY output_buf,
759 JDIMENSION output_col)
761 jsimd_idct_4x4_sse2(compptr->dct_table, coef_block, output_buf, output_col);
765 jsimd_can_idct_islow (void)
769 /* The code is optimised for these values only */
772 if (sizeof(JCOEF) != 2)
774 if (BITS_IN_JSAMPLE != 8)
776 if (sizeof(JDIMENSION) != 4)
778 if (sizeof(ISLOW_MULT_TYPE) != 2)
781 if ((simd_support & JSIMD_SSE2) && IS_ALIGNED_SSE(jconst_idct_islow_sse2))
788 jsimd_can_idct_ifast (void)
792 /* The code is optimised for these values only */
795 if (sizeof(JCOEF) != 2)
797 if (BITS_IN_JSAMPLE != 8)
799 if (sizeof(JDIMENSION) != 4)
801 if (sizeof(IFAST_MULT_TYPE) != 2)
803 if (IFAST_SCALE_BITS != 2)
806 if ((simd_support & JSIMD_SSE2) && IS_ALIGNED_SSE(jconst_idct_ifast_sse2))
813 jsimd_can_idct_float (void)
819 if (sizeof(JCOEF) != 2)
821 if (BITS_IN_JSAMPLE != 8)
823 if (sizeof(JDIMENSION) != 4)
825 if (sizeof(FAST_FLOAT) != 4)
827 if (sizeof(FLOAT_MULT_TYPE) != 4)
830 if ((simd_support & JSIMD_SSE2) && IS_ALIGNED_SSE(jconst_idct_float_sse2))
837 jsimd_idct_islow (j_decompress_ptr cinfo, jpeg_component_info *compptr,
838 JCOEFPTR coef_block, JSAMPARRAY output_buf,
839 JDIMENSION output_col)
841 jsimd_idct_islow_sse2(compptr->dct_table, coef_block, output_buf,
846 jsimd_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info *compptr,
847 JCOEFPTR coef_block, JSAMPARRAY output_buf,
848 JDIMENSION output_col)
850 jsimd_idct_ifast_sse2(compptr->dct_table, coef_block, output_buf,
855 jsimd_idct_float (j_decompress_ptr cinfo, jpeg_component_info *compptr,
856 JCOEFPTR coef_block, JSAMPARRAY output_buf,
857 JDIMENSION output_col)
859 jsimd_idct_float_sse2(compptr->dct_table, coef_block, output_buf,
864 jsimd_can_huff_encode_one_block (void)
870 if (sizeof(JCOEF) != 2)
873 if ((simd_support & JSIMD_SSE2) && simd_huffman &&
874 IS_ALIGNED_SSE(jconst_huff_encode_one_block))
881 jsimd_huff_encode_one_block (void *state, JOCTET *buffer, JCOEFPTR block,
882 int last_dc_val, c_derived_tbl *dctbl,
883 c_derived_tbl *actbl)
885 return jsimd_huff_encode_one_block_sse2(state, buffer, block, last_dc_val,