1 /*
2  * jsimd_mips.c
3  *
4  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
5  * Copyright (C) 2009-2011, 2014, 2016, 2018, D. R. Commander.
6  * Copyright (C) 2013-2014, MIPS Technologies, Inc., California.
7  * Copyright (C) 2015-2016, 2018, Matthieu Darbois.
8  *
9  * Based on the x86 SIMD extension for IJG JPEG library,
10  * Copyright (C) 1999-2006, MIYASAKA Masaru.
11  * For conditions of distribution and use, see copyright notice in jsimdext.inc
12  *
13  * This file contains the interface between the "normal" portions
14  * of the library and the SIMD implementations when running on a
15  * MIPS architecture.
16  */
17 
18 #define JPEG_INTERNALS
19 #include "../../jinclude.h"
20 #include "../../jpeglib.h"
21 #include "../../jsimd.h"
22 #include "../../jdct.h"
23 #include "../../jsimddct.h"
24 #include "../jsimd.h"
25 
26 #include <stdio.h>
27 #include <string.h>
28 #include <ctype.h>
29 
30 static unsigned int simd_support = ~0;
31 
32 #if defined(__linux__)
33 
34 LOCAL(int)
parse_proc_cpuinfo(const char * search_string)35 parse_proc_cpuinfo(const char *search_string)
36 {
37   const char *file_name = "/proc/cpuinfo";
38   char cpuinfo_line[256];
39   FILE *f = NULL;
40 
41   simd_support = 0;
42 
43   if ((f = fopen(file_name, "r")) != NULL) {
44     while (fgets(cpuinfo_line, sizeof(cpuinfo_line), f) != NULL) {
45       if (strstr(cpuinfo_line, search_string) != NULL) {
46         fclose(f);
47         simd_support |= JSIMD_DSPR2;
48         return 1;
49       }
50     }
51     fclose(f);
52   }
53   /* Did not find string in the proc file, or not Linux ELF. */
54   return 0;
55 }
56 
57 #endif
58 
59 /*
60  * Check what SIMD accelerations are supported.
61  *
62  * FIXME: This code is racy under a multi-threaded environment.
63  */
64 LOCAL(void)
init_simd(void)65 init_simd(void)
66 {
67 #ifndef NO_GETENV
68   char *env = NULL;
69 #endif
70 
71   if (simd_support != ~0U)
72     return;
73 
74   simd_support = 0;
75 
76 #if defined(__MIPSEL__) && defined(__mips_dsp) && (__mips_dsp_rev >= 2)
77   simd_support |= JSIMD_DSPR2;
78 #elif defined(__linux__)
79   /* We still have a chance to use MIPS DSPR2 regardless of globally used
80    * -mdspr2 options passed to gcc by performing runtime detection via
81    * /proc/cpuinfo parsing on linux */
82   if (!parse_proc_cpuinfo("MIPS 74K"))
83     return;
84 #endif
85 
86 #ifndef NO_GETENV
87   /* Force different settings through environment variables */
88   env = getenv("JSIMD_FORCEDSPR2");
89   if ((env != NULL) && (strcmp(env, "1") == 0))
90     simd_support = JSIMD_DSPR2;
91   env = getenv("JSIMD_FORCENONE");
92   if ((env != NULL) && (strcmp(env, "1") == 0))
93     simd_support = 0;
94 #endif
95 }
96 
97 static const int mips_idct_ifast_coefs[4] = {
98   0x45404540,           /* FIX( 1.082392200 / 2) =  17734 = 0x4546 */
99   0x5A805A80,           /* FIX( 1.414213562 / 2) =  23170 = 0x5A82 */
100   0x76407640,           /* FIX( 1.847759065 / 2) =  30274 = 0x7642 */
101   0xAC60AC60            /* FIX(-2.613125930 / 4) = -21407 = 0xAC61 */
102 };
103 
104 /* The following struct is borrowed from jdsample.c */
105 typedef void (*upsample1_ptr) (j_decompress_ptr cinfo,
106                                jpeg_component_info *compptr,
107                                JSAMPARRAY input_data,
108                                JSAMPARRAY *output_data_ptr);
109 typedef struct {
110   struct jpeg_upsampler pub;
111   JSAMPARRAY color_buf[MAX_COMPONENTS];
112   upsample1_ptr methods[MAX_COMPONENTS];
113   int next_row_out;
114   JDIMENSION rows_to_go;
115   int rowgroup_height[MAX_COMPONENTS];
116   UINT8 h_expand[MAX_COMPONENTS];
117   UINT8 v_expand[MAX_COMPONENTS];
118 } my_upsampler;
119 
120 typedef my_upsampler *my_upsample_ptr;
121 
122 GLOBAL(int)
jsimd_can_rgb_ycc(void)123 jsimd_can_rgb_ycc(void)
124 {
125   init_simd();
126 
127   /* The code is optimised for these values only */
128   if (BITS_IN_JSAMPLE != 8)
129     return 0;
130   if (sizeof(JDIMENSION) != 4)
131     return 0;
132   if ((RGB_PIXELSIZE != 3) && (RGB_PIXELSIZE != 4))
133     return 0;
134 
135   if (simd_support & JSIMD_DSPR2)
136     return 1;
137 
138   return 0;
139 }
140 
141 GLOBAL(int)
jsimd_can_rgb_gray(void)142 jsimd_can_rgb_gray(void)
143 {
144   init_simd();
145 
146   /* The code is optimised for these values only */
147   if (BITS_IN_JSAMPLE != 8)
148     return 0;
149   if (sizeof(JDIMENSION) != 4)
150     return 0;
151   if ((RGB_PIXELSIZE != 3) && (RGB_PIXELSIZE != 4))
152     return 0;
153 
154   if (simd_support & JSIMD_DSPR2)
155     return 1;
156 
157   return 0;
158 }
159 
160 GLOBAL(int)
jsimd_can_ycc_rgb(void)161 jsimd_can_ycc_rgb(void)
162 {
163   init_simd();
164 
165   /* The code is optimised for these values only */
166   if (BITS_IN_JSAMPLE != 8)
167     return 0;
168   if (sizeof(JDIMENSION) != 4)
169     return 0;
170   if ((RGB_PIXELSIZE != 3) && (RGB_PIXELSIZE != 4))
171     return 0;
172 
173   if (simd_support & JSIMD_DSPR2)
174     return 1;
175 
176   return 0;
177 }
178 
179 GLOBAL(int)
jsimd_can_ycc_rgb565(void)180 jsimd_can_ycc_rgb565(void)
181 {
182   return 0;
183 }
184 
185 GLOBAL(int)
jsimd_c_can_null_convert(void)186 jsimd_c_can_null_convert(void)
187 {
188   init_simd();
189 
190   /* The code is optimised for these values only */
191   if (BITS_IN_JSAMPLE != 8)
192     return 0;
193   if (sizeof(JDIMENSION) != 4)
194     return 0;
195 
196   if (simd_support & JSIMD_DSPR2)
197     return 1;
198 
199   return 0;
200 }
201 
202 GLOBAL(void)
jsimd_rgb_ycc_convert(j_compress_ptr cinfo,JSAMPARRAY input_buf,JSAMPIMAGE output_buf,JDIMENSION output_row,int num_rows)203 jsimd_rgb_ycc_convert(j_compress_ptr cinfo, JSAMPARRAY input_buf,
204                       JSAMPIMAGE output_buf, JDIMENSION output_row,
205                       int num_rows)
206 {
207   void (*dspr2fct) (JDIMENSION, JSAMPARRAY, JSAMPIMAGE, JDIMENSION, int);
208 
209   switch (cinfo->in_color_space) {
210   case JCS_EXT_RGB:
211     dspr2fct = jsimd_extrgb_ycc_convert_dspr2;
212     break;
213   case JCS_EXT_RGBX:
214   case JCS_EXT_RGBA:
215     dspr2fct = jsimd_extrgbx_ycc_convert_dspr2;
216     break;
217   case JCS_EXT_BGR:
218     dspr2fct = jsimd_extbgr_ycc_convert_dspr2;
219     break;
220   case JCS_EXT_BGRX:
221   case JCS_EXT_BGRA:
222     dspr2fct = jsimd_extbgrx_ycc_convert_dspr2;
223     break;
224   case JCS_EXT_XBGR:
225   case JCS_EXT_ABGR:
226     dspr2fct = jsimd_extxbgr_ycc_convert_dspr2;
227     break;
228   case JCS_EXT_XRGB:
229   case JCS_EXT_ARGB:
230     dspr2fct = jsimd_extxrgb_ycc_convert_dspr2;
231     break;
232   default:
233     dspr2fct = jsimd_extrgb_ycc_convert_dspr2;
234     break;
235   }
236 
237   dspr2fct(cinfo->image_width, input_buf, output_buf, output_row, num_rows);
238 }
239 
240 GLOBAL(void)
jsimd_rgb_gray_convert(j_compress_ptr cinfo,JSAMPARRAY input_buf,JSAMPIMAGE output_buf,JDIMENSION output_row,int num_rows)241 jsimd_rgb_gray_convert(j_compress_ptr cinfo, JSAMPARRAY input_buf,
242                        JSAMPIMAGE output_buf, JDIMENSION output_row,
243                        int num_rows)
244 {
245   void (*dspr2fct) (JDIMENSION, JSAMPARRAY, JSAMPIMAGE, JDIMENSION, int);
246 
247   switch (cinfo->in_color_space) {
248   case JCS_EXT_RGB:
249     dspr2fct = jsimd_extrgb_gray_convert_dspr2;
250     break;
251   case JCS_EXT_RGBX:
252   case JCS_EXT_RGBA:
253     dspr2fct = jsimd_extrgbx_gray_convert_dspr2;
254     break;
255   case JCS_EXT_BGR:
256     dspr2fct = jsimd_extbgr_gray_convert_dspr2;
257     break;
258   case JCS_EXT_BGRX:
259   case JCS_EXT_BGRA:
260     dspr2fct = jsimd_extbgrx_gray_convert_dspr2;
261     break;
262   case JCS_EXT_XBGR:
263   case JCS_EXT_ABGR:
264     dspr2fct = jsimd_extxbgr_gray_convert_dspr2;
265     break;
266   case JCS_EXT_XRGB:
267   case JCS_EXT_ARGB:
268     dspr2fct = jsimd_extxrgb_gray_convert_dspr2;
269     break;
270   default:
271     dspr2fct = jsimd_extrgb_gray_convert_dspr2;
272     break;
273   }
274 
275   dspr2fct(cinfo->image_width, input_buf, output_buf, output_row, num_rows);
276 }
277 
278 GLOBAL(void)
jsimd_ycc_rgb_convert(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION input_row,JSAMPARRAY output_buf,int num_rows)279 jsimd_ycc_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
280                       JDIMENSION input_row, JSAMPARRAY output_buf,
281                       int num_rows)
282 {
283   void (*dspr2fct) (JDIMENSION, JSAMPIMAGE, JDIMENSION, JSAMPARRAY, int);
284 
285   switch (cinfo->out_color_space) {
286   case JCS_EXT_RGB:
287     dspr2fct = jsimd_ycc_extrgb_convert_dspr2;
288     break;
289   case JCS_EXT_RGBX:
290   case JCS_EXT_RGBA:
291     dspr2fct = jsimd_ycc_extrgbx_convert_dspr2;
292     break;
293   case JCS_EXT_BGR:
294     dspr2fct = jsimd_ycc_extbgr_convert_dspr2;
295     break;
296   case JCS_EXT_BGRX:
297   case JCS_EXT_BGRA:
298     dspr2fct = jsimd_ycc_extbgrx_convert_dspr2;
299     break;
300   case JCS_EXT_XBGR:
301   case JCS_EXT_ABGR:
302     dspr2fct = jsimd_ycc_extxbgr_convert_dspr2;
303     break;
304   case JCS_EXT_XRGB:
305   case JCS_EXT_ARGB:
306     dspr2fct = jsimd_ycc_extxrgb_convert_dspr2;
307     break;
308   default:
309     dspr2fct = jsimd_ycc_extrgb_convert_dspr2;
310     break;
311   }
312 
313   dspr2fct(cinfo->output_width, input_buf, input_row, output_buf, num_rows);
314 }
315 
316 GLOBAL(void)
jsimd_ycc_rgb565_convert(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION input_row,JSAMPARRAY output_buf,int num_rows)317 jsimd_ycc_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
318                          JDIMENSION input_row, JSAMPARRAY output_buf,
319                          int num_rows)
320 {
321 }
322 
323 GLOBAL(void)
jsimd_c_null_convert(j_compress_ptr cinfo,JSAMPARRAY input_buf,JSAMPIMAGE output_buf,JDIMENSION output_row,int num_rows)324 jsimd_c_null_convert(j_compress_ptr cinfo, JSAMPARRAY input_buf,
325                      JSAMPIMAGE output_buf, JDIMENSION output_row,
326                      int num_rows)
327 {
328   jsimd_c_null_convert_dspr2(cinfo->image_width, input_buf, output_buf,
329                              output_row, num_rows, cinfo->num_components);
330 }
331 
332 GLOBAL(int)
jsimd_can_h2v2_downsample(void)333 jsimd_can_h2v2_downsample(void)
334 {
335   init_simd();
336 
337   /* The code is optimised for these values only */
338   if (BITS_IN_JSAMPLE != 8)
339     return 0;
340   if (sizeof(JDIMENSION) != 4)
341     return 0;
342 
343   if (simd_support & JSIMD_DSPR2)
344     return 1;
345 
346   return 0;
347 }
348 
349 GLOBAL(int)
jsimd_can_h2v2_smooth_downsample(void)350 jsimd_can_h2v2_smooth_downsample(void)
351 {
352   init_simd();
353 
354   /* The code is optimised for these values only */
355   if (BITS_IN_JSAMPLE != 8)
356     return 0;
357   if (sizeof(JDIMENSION) != 4)
358     return 0;
359   if (DCTSIZE != 8)
360     return 0;
361 
362   if (simd_support & JSIMD_DSPR2)
363     return 1;
364 
365   return 0;
366 }
367 
368 GLOBAL(int)
jsimd_can_h2v1_downsample(void)369 jsimd_can_h2v1_downsample(void)
370 {
371   init_simd();
372 
373   /* The code is optimised for these values only */
374   if (BITS_IN_JSAMPLE != 8)
375     return 0;
376   if (sizeof(JDIMENSION) != 4)
377     return 0;
378 
379   if (simd_support & JSIMD_DSPR2)
380     return 1;
381 
382   return 0;
383 }
384 
385 GLOBAL(void)
jsimd_h2v2_downsample(j_compress_ptr cinfo,jpeg_component_info * compptr,JSAMPARRAY input_data,JSAMPARRAY output_data)386 jsimd_h2v2_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,
387                       JSAMPARRAY input_data, JSAMPARRAY output_data)
388 {
389   jsimd_h2v2_downsample_dspr2(cinfo->image_width, cinfo->max_v_samp_factor,
390                               compptr->v_samp_factor, compptr->width_in_blocks,
391                               input_data, output_data);
392 }
393 
394 GLOBAL(void)
jsimd_h2v2_smooth_downsample(j_compress_ptr cinfo,jpeg_component_info * compptr,JSAMPARRAY input_data,JSAMPARRAY output_data)395 jsimd_h2v2_smooth_downsample(j_compress_ptr cinfo,
396                              jpeg_component_info *compptr,
397                              JSAMPARRAY input_data, JSAMPARRAY output_data)
398 {
399   jsimd_h2v2_smooth_downsample_dspr2(input_data, output_data,
400                                      compptr->v_samp_factor,
401                                      cinfo->max_v_samp_factor,
402                                      cinfo->smoothing_factor,
403                                      compptr->width_in_blocks,
404                                      cinfo->image_width);
405 }
406 
407 GLOBAL(void)
jsimd_h2v1_downsample(j_compress_ptr cinfo,jpeg_component_info * compptr,JSAMPARRAY input_data,JSAMPARRAY output_data)408 jsimd_h2v1_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,
409                       JSAMPARRAY input_data, JSAMPARRAY output_data)
410 {
411   jsimd_h2v1_downsample_dspr2(cinfo->image_width, cinfo->max_v_samp_factor,
412                               compptr->v_samp_factor, compptr->width_in_blocks,
413                               input_data, output_data);
414 }
415 
416 GLOBAL(int)
jsimd_can_h2v2_upsample(void)417 jsimd_can_h2v2_upsample(void)
418 {
419   init_simd();
420 
421   /* The code is optimised for these values only */
422   if (BITS_IN_JSAMPLE != 8)
423     return 0;
424   if (sizeof(JDIMENSION) != 4)
425     return 0;
426 
427   if (simd_support & JSIMD_DSPR2)
428     return 1;
429 
430   return 0;
431 }
432 
433 GLOBAL(int)
jsimd_can_h2v1_upsample(void)434 jsimd_can_h2v1_upsample(void)
435 {
436   init_simd();
437 
438   /* The code is optimised for these values only */
439   if (BITS_IN_JSAMPLE != 8)
440     return 0;
441   if (sizeof(JDIMENSION) != 4)
442     return 0;
443 
444   if (simd_support & JSIMD_DSPR2)
445     return 1;
446 
447   return 0;
448 }
449 
450 GLOBAL(int)
jsimd_can_int_upsample(void)451 jsimd_can_int_upsample(void)
452 {
453   init_simd();
454 
455   /* The code is optimised for these values only */
456   if (BITS_IN_JSAMPLE != 8)
457     return 0;
458   if (sizeof(JDIMENSION) != 4)
459     return 0;
460 
461   if (simd_support & JSIMD_DSPR2)
462     return 1;
463 
464   return 0;
465 }
466 
467 GLOBAL(void)
jsimd_h2v2_upsample(j_decompress_ptr cinfo,jpeg_component_info * compptr,JSAMPARRAY input_data,JSAMPARRAY * output_data_ptr)468 jsimd_h2v2_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
469                     JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
470 {
471   jsimd_h2v2_upsample_dspr2(cinfo->max_v_samp_factor, cinfo->output_width,
472                             input_data, output_data_ptr);
473 }
474 
475 GLOBAL(void)
jsimd_h2v1_upsample(j_decompress_ptr cinfo,jpeg_component_info * compptr,JSAMPARRAY input_data,JSAMPARRAY * output_data_ptr)476 jsimd_h2v1_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
477                     JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
478 {
479   jsimd_h2v1_upsample_dspr2(cinfo->max_v_samp_factor, cinfo->output_width,
480                             input_data, output_data_ptr);
481 }
482 
483 GLOBAL(void)
jsimd_int_upsample(j_decompress_ptr cinfo,jpeg_component_info * compptr,JSAMPARRAY input_data,JSAMPARRAY * output_data_ptr)484 jsimd_int_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
485                    JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
486 {
487   my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
488 
489   jsimd_int_upsample_dspr2(upsample->h_expand[compptr->component_index],
490                            upsample->v_expand[compptr->component_index],
491                            input_data, output_data_ptr, cinfo->output_width,
492                            cinfo->max_v_samp_factor);
493 }
494 
495 GLOBAL(int)
jsimd_can_h2v2_fancy_upsample(void)496 jsimd_can_h2v2_fancy_upsample(void)
497 {
498   init_simd();
499 
500   /* The code is optimised for these values only */
501   if (BITS_IN_JSAMPLE != 8)
502     return 0;
503   if (sizeof(JDIMENSION) != 4)
504     return 0;
505 
506   if (simd_support & JSIMD_DSPR2)
507     return 1;
508 
509   return 0;
510 }
511 
512 GLOBAL(int)
jsimd_can_h2v1_fancy_upsample(void)513 jsimd_can_h2v1_fancy_upsample(void)
514 {
515   init_simd();
516 
517   /* The code is optimised for these values only */
518   if (BITS_IN_JSAMPLE != 8)
519     return 0;
520   if (sizeof(JDIMENSION) != 4)
521     return 0;
522 
523   if (simd_support & JSIMD_DSPR2)
524     return 1;
525 
526   return 0;
527 }
528 
529 GLOBAL(void)
jsimd_h2v2_fancy_upsample(j_decompress_ptr cinfo,jpeg_component_info * compptr,JSAMPARRAY input_data,JSAMPARRAY * output_data_ptr)530 jsimd_h2v2_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
531                           JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
532 {
533   jsimd_h2v2_fancy_upsample_dspr2(cinfo->max_v_samp_factor,
534                                   compptr->downsampled_width, input_data,
535                                   output_data_ptr);
536 }
537 
538 GLOBAL(void)
jsimd_h2v1_fancy_upsample(j_decompress_ptr cinfo,jpeg_component_info * compptr,JSAMPARRAY input_data,JSAMPARRAY * output_data_ptr)539 jsimd_h2v1_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
540                           JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
541 {
542   jsimd_h2v1_fancy_upsample_dspr2(cinfo->max_v_samp_factor,
543                                   compptr->downsampled_width, input_data,
544                                   output_data_ptr);
545 }
546 
547 GLOBAL(int)
jsimd_can_h2v2_merged_upsample(void)548 jsimd_can_h2v2_merged_upsample(void)
549 {
550   init_simd();
551 
552   /* The code is optimised for these values only */
553   if (BITS_IN_JSAMPLE != 8)
554     return 0;
555   if (sizeof(JDIMENSION) != 4)
556     return 0;
557 
558   if (simd_support & JSIMD_DSPR2)
559     return 1;
560 
561   return 0;
562 }
563 
564 GLOBAL(int)
jsimd_can_h2v1_merged_upsample(void)565 jsimd_can_h2v1_merged_upsample(void)
566 {
567   init_simd();
568 
569   /* The code is optimised for these values only */
570   if (BITS_IN_JSAMPLE != 8)
571     return 0;
572   if (sizeof(JDIMENSION) != 4)
573     return 0;
574 
575   if (simd_support & JSIMD_DSPR2)
576     return 1;
577 
578   return 0;
579 }
580 
581 GLOBAL(void)
jsimd_h2v2_merged_upsample(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION in_row_group_ctr,JSAMPARRAY output_buf)582 jsimd_h2v2_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
583                            JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
584 {
585   void (*dspr2fct) (JDIMENSION, JSAMPIMAGE, JDIMENSION, JSAMPARRAY, JSAMPLE *);
586 
587   switch (cinfo->out_color_space) {
588   case JCS_EXT_RGB:
589     dspr2fct = jsimd_h2v2_extrgb_merged_upsample_dspr2;
590     break;
591   case JCS_EXT_RGBX:
592   case JCS_EXT_RGBA:
593     dspr2fct = jsimd_h2v2_extrgbx_merged_upsample_dspr2;
594     break;
595   case JCS_EXT_BGR:
596     dspr2fct = jsimd_h2v2_extbgr_merged_upsample_dspr2;
597     break;
598   case JCS_EXT_BGRX:
599   case JCS_EXT_BGRA:
600     dspr2fct = jsimd_h2v2_extbgrx_merged_upsample_dspr2;
601     break;
602   case JCS_EXT_XBGR:
603   case JCS_EXT_ABGR:
604     dspr2fct = jsimd_h2v2_extxbgr_merged_upsample_dspr2;
605     break;
606   case JCS_EXT_XRGB:
607   case JCS_EXT_ARGB:
608     dspr2fct = jsimd_h2v2_extxrgb_merged_upsample_dspr2;
609     break;
610   default:
611     dspr2fct = jsimd_h2v2_extrgb_merged_upsample_dspr2;
612     break;
613   }
614 
615   dspr2fct(cinfo->output_width, input_buf, in_row_group_ctr, output_buf,
616            cinfo->sample_range_limit);
617 }
618 
619 GLOBAL(void)
jsimd_h2v1_merged_upsample(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION in_row_group_ctr,JSAMPARRAY output_buf)620 jsimd_h2v1_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
621                            JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
622 {
623   void (*dspr2fct) (JDIMENSION, JSAMPIMAGE, JDIMENSION, JSAMPARRAY, JSAMPLE *);
624 
625   switch (cinfo->out_color_space) {
626   case JCS_EXT_RGB:
627     dspr2fct = jsimd_h2v1_extrgb_merged_upsample_dspr2;
628     break;
629   case JCS_EXT_RGBX:
630   case JCS_EXT_RGBA:
631     dspr2fct = jsimd_h2v1_extrgbx_merged_upsample_dspr2;
632     break;
633   case JCS_EXT_BGR:
634     dspr2fct = jsimd_h2v1_extbgr_merged_upsample_dspr2;
635     break;
636   case JCS_EXT_BGRX:
637   case JCS_EXT_BGRA:
638     dspr2fct = jsimd_h2v1_extbgrx_merged_upsample_dspr2;
639     break;
640   case JCS_EXT_XBGR:
641   case JCS_EXT_ABGR:
642     dspr2fct = jsimd_h2v1_extxbgr_merged_upsample_dspr2;
643     break;
644   case JCS_EXT_XRGB:
645   case JCS_EXT_ARGB:
646     dspr2fct = jsimd_h2v1_extxrgb_merged_upsample_dspr2;
647     break;
648   default:
649     dspr2fct = jsimd_h2v1_extrgb_merged_upsample_dspr2;
650     break;
651   }
652 
653   dspr2fct(cinfo->output_width, input_buf, in_row_group_ctr, output_buf,
654            cinfo->sample_range_limit);
655 }
656 
657 GLOBAL(int)
jsimd_can_convsamp(void)658 jsimd_can_convsamp(void)
659 {
660   init_simd();
661 
662   /* The code is optimised for these values only */
663   if (DCTSIZE != 8)
664     return 0;
665   if (BITS_IN_JSAMPLE != 8)
666     return 0;
667   if (sizeof(JDIMENSION) != 4)
668     return 0;
669   if (sizeof(DCTELEM) != 2)
670     return 0;
671 
672   if (simd_support & JSIMD_DSPR2)
673     return 1;
674 
675   return 0;
676 }
677 
678 GLOBAL(int)
jsimd_can_convsamp_float(void)679 jsimd_can_convsamp_float(void)
680 {
681   init_simd();
682 
683   /* The code is optimised for these values only */
684   if (DCTSIZE != 8)
685     return 0;
686   if (sizeof(JCOEF) != 2)
687     return 0;
688   if (BITS_IN_JSAMPLE != 8)
689     return 0;
690   if (sizeof(JDIMENSION) != 4)
691     return 0;
692   if (sizeof(ISLOW_MULT_TYPE) != 2)
693     return 0;
694 
695 #ifndef __mips_soft_float
696   if (simd_support & JSIMD_DSPR2)
697     return 1;
698 #endif
699 
700   return 0;
701 }
702 
703 GLOBAL(void)
jsimd_convsamp(JSAMPARRAY sample_data,JDIMENSION start_col,DCTELEM * workspace)704 jsimd_convsamp(JSAMPARRAY sample_data, JDIMENSION start_col,
705                DCTELEM *workspace)
706 {
707   jsimd_convsamp_dspr2(sample_data, start_col, workspace);
708 }
709 
710 GLOBAL(void)
jsimd_convsamp_float(JSAMPARRAY sample_data,JDIMENSION start_col,FAST_FLOAT * workspace)711 jsimd_convsamp_float(JSAMPARRAY sample_data, JDIMENSION start_col,
712                      FAST_FLOAT *workspace)
713 {
714 #ifndef __mips_soft_float
715   jsimd_convsamp_float_dspr2(sample_data, start_col, workspace);
716 #endif
717 }
718 
719 GLOBAL(int)
jsimd_can_fdct_islow(void)720 jsimd_can_fdct_islow(void)
721 {
722   init_simd();
723 
724   /* The code is optimised for these values only */
725   if (DCTSIZE != 8)
726     return 0;
727   if (sizeof(DCTELEM) != 2)
728     return 0;
729 
730   if (simd_support & JSIMD_DSPR2)
731     return 1;
732 
733   return 0;
734 }
735 
736 GLOBAL(int)
jsimd_can_fdct_ifast(void)737 jsimd_can_fdct_ifast(void)
738 {
739   init_simd();
740 
741   /* The code is optimised for these values only */
742   if (DCTSIZE != 8)
743     return 0;
744   if (sizeof(DCTELEM) != 2)
745     return 0;
746 
747   if (simd_support & JSIMD_DSPR2)
748     return 1;
749 
750   return 0;
751 }
752 
753 GLOBAL(int)
jsimd_can_fdct_float(void)754 jsimd_can_fdct_float(void)
755 {
756   return 0;
757 }
758 
759 GLOBAL(void)
jsimd_fdct_islow(DCTELEM * data)760 jsimd_fdct_islow(DCTELEM *data)
761 {
762   jsimd_fdct_islow_dspr2(data);
763 }
764 
765 GLOBAL(void)
jsimd_fdct_ifast(DCTELEM * data)766 jsimd_fdct_ifast(DCTELEM *data)
767 {
768   jsimd_fdct_ifast_dspr2(data);
769 }
770 
771 GLOBAL(void)
jsimd_fdct_float(FAST_FLOAT * data)772 jsimd_fdct_float(FAST_FLOAT *data)
773 {
774 }
775 
776 GLOBAL(int)
jsimd_can_quantize(void)777 jsimd_can_quantize(void)
778 {
779   init_simd();
780 
781   /* The code is optimised for these values only */
782   if (DCTSIZE != 8)
783     return 0;
784   if (sizeof(JCOEF) != 2)
785     return 0;
786   if (sizeof(DCTELEM) != 2)
787     return 0;
788 
789   if (simd_support & JSIMD_DSPR2)
790     return 1;
791 
792   return 0;
793 }
794 
795 GLOBAL(int)
jsimd_can_quantize_float(void)796 jsimd_can_quantize_float(void)
797 {
798   init_simd();
799 
800   /* The code is optimised for these values only */
801   if (DCTSIZE != 8)
802     return 0;
803   if (sizeof(JCOEF) != 2)
804     return 0;
805   if (BITS_IN_JSAMPLE != 8)
806     return 0;
807   if (sizeof(JDIMENSION) != 4)
808     return 0;
809   if (sizeof(ISLOW_MULT_TYPE) != 2)
810     return 0;
811 
812 #ifndef __mips_soft_float
813   if (simd_support & JSIMD_DSPR2)
814     return 1;
815 #endif
816 
817   return 0;
818 }
819 
820 GLOBAL(void)
jsimd_quantize(JCOEFPTR coef_block,DCTELEM * divisors,DCTELEM * workspace)821 jsimd_quantize(JCOEFPTR coef_block, DCTELEM *divisors, DCTELEM *workspace)
822 {
823   jsimd_quantize_dspr2(coef_block, divisors, workspace);
824 }
825 
826 GLOBAL(void)
jsimd_quantize_float(JCOEFPTR coef_block,FAST_FLOAT * divisors,FAST_FLOAT * workspace)827 jsimd_quantize_float(JCOEFPTR coef_block, FAST_FLOAT *divisors,
828                      FAST_FLOAT *workspace)
829 {
830 #ifndef __mips_soft_float
831   jsimd_quantize_float_dspr2(coef_block, divisors, workspace);
832 #endif
833 }
834 
835 GLOBAL(int)
jsimd_can_idct_2x2(void)836 jsimd_can_idct_2x2(void)
837 {
838   init_simd();
839 
840   /* The code is optimised for these values only */
841   if (DCTSIZE != 8)
842     return 0;
843   if (sizeof(JCOEF) != 2)
844     return 0;
845   if (BITS_IN_JSAMPLE != 8)
846     return 0;
847   if (sizeof(JDIMENSION) != 4)
848     return 0;
849   if (sizeof(ISLOW_MULT_TYPE) != 2)
850     return 0;
851 
852   if (simd_support & JSIMD_DSPR2)
853     return 1;
854 
855   return 0;
856 }
857 
858 GLOBAL(int)
jsimd_can_idct_4x4(void)859 jsimd_can_idct_4x4(void)
860 {
861   init_simd();
862 
863   /* The code is optimised for these values only */
864   if (DCTSIZE != 8)
865     return 0;
866   if (sizeof(JCOEF) != 2)
867     return 0;
868   if (BITS_IN_JSAMPLE != 8)
869     return 0;
870   if (sizeof(JDIMENSION) != 4)
871     return 0;
872   if (sizeof(ISLOW_MULT_TYPE) != 2)
873     return 0;
874 
875   if (simd_support & JSIMD_DSPR2)
876     return 1;
877 
878   return 0;
879 }
880 
881 GLOBAL(int)
jsimd_can_idct_6x6(void)882 jsimd_can_idct_6x6(void)
883 {
884   init_simd();
885 
886   /* The code is optimised for these values only */
887   if (DCTSIZE != 8)
888     return 0;
889   if (sizeof(JCOEF) != 2)
890     return 0;
891   if (BITS_IN_JSAMPLE != 8)
892     return 0;
893   if (sizeof(JDIMENSION) != 4)
894     return 0;
895   if (sizeof(ISLOW_MULT_TYPE) != 2)
896     return 0;
897 
898   if (simd_support & JSIMD_DSPR2)
899     return 1;
900 
901   return 0;
902 }
903 
904 GLOBAL(int)
jsimd_can_idct_12x12(void)905 jsimd_can_idct_12x12(void)
906 {
907   init_simd();
908 
909   if (BITS_IN_JSAMPLE != 8)
910     return 0;
911   if (DCTSIZE != 8)
912     return 0;
913   if (sizeof(JCOEF) != 2)
914     return 0;
915   if (sizeof(JDIMENSION) != 4)
916     return 0;
917   if (sizeof(ISLOW_MULT_TYPE) != 2)
918     return 0;
919 
920   if (simd_support & JSIMD_DSPR2)
921     return 1;
922 
923   return 0;
924 }
925 
926 GLOBAL(void)
jsimd_idct_2x2(j_decompress_ptr cinfo,jpeg_component_info * compptr,JCOEFPTR coef_block,JSAMPARRAY output_buf,JDIMENSION output_col)927 jsimd_idct_2x2(j_decompress_ptr cinfo, jpeg_component_info *compptr,
928                JCOEFPTR coef_block, JSAMPARRAY output_buf,
929                JDIMENSION output_col)
930 {
931   jsimd_idct_2x2_dspr2(compptr->dct_table, coef_block, output_buf, output_col);
932 }
933 
934 GLOBAL(void)
jsimd_idct_4x4(j_decompress_ptr cinfo,jpeg_component_info * compptr,JCOEFPTR coef_block,JSAMPARRAY output_buf,JDIMENSION output_col)935 jsimd_idct_4x4(j_decompress_ptr cinfo, jpeg_component_info *compptr,
936                JCOEFPTR coef_block, JSAMPARRAY output_buf,
937                JDIMENSION output_col)
938 {
939   int workspace[DCTSIZE * 4]; /* buffers data between passes */
940 
941   jsimd_idct_4x4_dspr2(compptr->dct_table, coef_block, output_buf, output_col,
942                        workspace);
943 }
944 
945 GLOBAL(void)
jsimd_idct_6x6(j_decompress_ptr cinfo,jpeg_component_info * compptr,JCOEFPTR coef_block,JSAMPARRAY output_buf,JDIMENSION output_col)946 jsimd_idct_6x6(j_decompress_ptr cinfo, jpeg_component_info *compptr,
947                JCOEFPTR coef_block, JSAMPARRAY output_buf,
948                JDIMENSION output_col)
949 {
950   jsimd_idct_6x6_dspr2(compptr->dct_table, coef_block, output_buf, output_col);
951 }
952 
953 GLOBAL(void)
jsimd_idct_12x12(j_decompress_ptr cinfo,jpeg_component_info * compptr,JCOEFPTR coef_block,JSAMPARRAY output_buf,JDIMENSION output_col)954 jsimd_idct_12x12(j_decompress_ptr cinfo, jpeg_component_info *compptr,
955                  JCOEFPTR coef_block, JSAMPARRAY output_buf,
956                  JDIMENSION output_col)
957 {
958   int workspace[96];
959   int output[12] = {
960     (int)(output_buf[0] + output_col),
961     (int)(output_buf[1] + output_col),
962     (int)(output_buf[2] + output_col),
963     (int)(output_buf[3] + output_col),
964     (int)(output_buf[4] + output_col),
965     (int)(output_buf[5] + output_col),
966     (int)(output_buf[6] + output_col),
967     (int)(output_buf[7] + output_col),
968     (int)(output_buf[8] + output_col),
969     (int)(output_buf[9] + output_col),
970     (int)(output_buf[10] + output_col),
971     (int)(output_buf[11] + output_col)
972   };
973 
974   jsimd_idct_12x12_pass1_dspr2(coef_block, compptr->dct_table, workspace);
975   jsimd_idct_12x12_pass2_dspr2(workspace, output);
976 }
977 
978 GLOBAL(int)
jsimd_can_idct_islow(void)979 jsimd_can_idct_islow(void)
980 {
981   init_simd();
982 
983   /* The code is optimised for these values only */
984   if (DCTSIZE != 8)
985     return 0;
986   if (sizeof(JCOEF) != 2)
987     return 0;
988   if (BITS_IN_JSAMPLE != 8)
989     return 0;
990   if (sizeof(JDIMENSION) != 4)
991     return 0;
992   if (sizeof(ISLOW_MULT_TYPE) != 2)
993     return 0;
994 
995   if (simd_support & JSIMD_DSPR2)
996     return 1;
997 
998   return 0;
999 }
1000 
1001 GLOBAL(int)
jsimd_can_idct_ifast(void)1002 jsimd_can_idct_ifast(void)
1003 {
1004   init_simd();
1005 
1006   /* The code is optimised for these values only */
1007   if (DCTSIZE != 8)
1008     return 0;
1009   if (sizeof(JCOEF) != 2)
1010     return 0;
1011   if (BITS_IN_JSAMPLE != 8)
1012     return 0;
1013   if (sizeof(JDIMENSION) != 4)
1014     return 0;
1015   if (sizeof(IFAST_MULT_TYPE) != 2)
1016     return 0;
1017   if (IFAST_SCALE_BITS != 2)
1018     return 0;
1019 
1020   if (simd_support & JSIMD_DSPR2)
1021     return 1;
1022 
1023   return 0;
1024 }
1025 
1026 GLOBAL(int)
jsimd_can_idct_float(void)1027 jsimd_can_idct_float(void)
1028 {
1029   return 0;
1030 }
1031 
1032 GLOBAL(void)
jsimd_idct_islow(j_decompress_ptr cinfo,jpeg_component_info * compptr,JCOEFPTR coef_block,JSAMPARRAY output_buf,JDIMENSION output_col)1033 jsimd_idct_islow(j_decompress_ptr cinfo, jpeg_component_info *compptr,
1034                  JCOEFPTR coef_block, JSAMPARRAY output_buf,
1035                  JDIMENSION output_col)
1036 {
1037   int output[8] = {
1038     (int)(output_buf[0] + output_col),
1039     (int)(output_buf[1] + output_col),
1040     (int)(output_buf[2] + output_col),
1041     (int)(output_buf[3] + output_col),
1042     (int)(output_buf[4] + output_col),
1043     (int)(output_buf[5] + output_col),
1044     (int)(output_buf[6] + output_col),
1045     (int)(output_buf[7] + output_col)
1046   };
1047 
1048   jsimd_idct_islow_dspr2(coef_block, compptr->dct_table, output,
1049                          IDCT_range_limit(cinfo));
1050 }
1051 
1052 GLOBAL(void)
jsimd_idct_ifast(j_decompress_ptr cinfo,jpeg_component_info * compptr,JCOEFPTR coef_block,JSAMPARRAY output_buf,JDIMENSION output_col)1053 jsimd_idct_ifast(j_decompress_ptr cinfo, jpeg_component_info *compptr,
1054                  JCOEFPTR coef_block, JSAMPARRAY output_buf,
1055                  JDIMENSION output_col)
1056 {
1057   JCOEFPTR inptr;
1058   IFAST_MULT_TYPE *quantptr;
1059   DCTELEM workspace[DCTSIZE2];  /* buffers data between passes */
1060 
1061   /* Pass 1: process columns from input, store into work array. */
1062 
1063   inptr = coef_block;
1064   quantptr = (IFAST_MULT_TYPE *)compptr->dct_table;
1065 
1066   jsimd_idct_ifast_cols_dspr2(inptr, quantptr, workspace,
1067                               mips_idct_ifast_coefs);
1068 
1069   /* Pass 2: process rows from work array, store into output array. */
1070   /* Note that we must descale the results by a factor of 8 == 2**3, */
1071   /* and also undo the PASS1_BITS scaling. */
1072 
1073   jsimd_idct_ifast_rows_dspr2(workspace, output_buf, output_col,
1074                               mips_idct_ifast_coefs);
1075 }
1076 
1077 GLOBAL(void)
jsimd_idct_float(j_decompress_ptr cinfo,jpeg_component_info * compptr,JCOEFPTR coef_block,JSAMPARRAY output_buf,JDIMENSION output_col)1078 jsimd_idct_float(j_decompress_ptr cinfo, jpeg_component_info *compptr,
1079                  JCOEFPTR coef_block, JSAMPARRAY output_buf,
1080                  JDIMENSION output_col)
1081 {
1082 }
1083 
1084 GLOBAL(int)
jsimd_can_huff_encode_one_block(void)1085 jsimd_can_huff_encode_one_block(void)
1086 {
1087   return 0;
1088 }
1089 
1090 GLOBAL(JOCTET *)
jsimd_huff_encode_one_block(void * state,JOCTET * buffer,JCOEFPTR block,int last_dc_val,c_derived_tbl * dctbl,c_derived_tbl * actbl)1091 jsimd_huff_encode_one_block(void *state, JOCTET *buffer, JCOEFPTR block,
1092                             int last_dc_val, c_derived_tbl *dctbl,
1093                             c_derived_tbl *actbl)
1094 {
1095   return NULL;
1096 }
1097 
1098 GLOBAL(int)
jsimd_can_encode_mcu_AC_first_prepare(void)1099 jsimd_can_encode_mcu_AC_first_prepare(void)
1100 {
1101   return 0;
1102 }
1103 
1104 GLOBAL(void)
jsimd_encode_mcu_AC_first_prepare(const JCOEF * block,const int * jpeg_natural_order_start,int Sl,int Al,JCOEF * values,size_t * zerobits)1105 jsimd_encode_mcu_AC_first_prepare(const JCOEF *block,
1106                                   const int *jpeg_natural_order_start, int Sl,
1107                                   int Al, JCOEF *values, size_t *zerobits)
1108 {
1109 }
1110 
1111 GLOBAL(int)
jsimd_can_encode_mcu_AC_refine_prepare(void)1112 jsimd_can_encode_mcu_AC_refine_prepare(void)
1113 {
1114   return 0;
1115 }
1116 
1117 GLOBAL(int)
jsimd_encode_mcu_AC_refine_prepare(const JCOEF * block,const int * jpeg_natural_order_start,int Sl,int Al,JCOEF * absvalues,size_t * bits)1118 jsimd_encode_mcu_AC_refine_prepare(const JCOEF *block,
1119                                    const int *jpeg_natural_order_start, int Sl,
1120                                    int Al, JCOEF *absvalues, size_t *bits)
1121 {
1122   return 0;
1123 }
1124