1 
2 /* mips_init.c - MSA optimised filter functions
3  *
4  * Copyright (c) 2018 Cosmin Truta
5  * Copyright (c) 2016 Glenn Randers-Pehrson
6  * Written by Mandar Sahastrabuddhe, 2016.
7  *
8  * This code is released under the libpng license.
9  * For conditions of distribution and use, see the disclaimer
10  * and license in png.h
11  */
12 
13 /* Below, after checking __linux__, various non-C90 POSIX 1003.1 functions are
14  * called.
15  */
16 #define _POSIX_SOURCE 1
17 
18 #include <stdio.h>
19 #include "../pngpriv.h"
20 
21 #ifdef PNG_READ_SUPPORTED
22 
23 #if PNG_MIPS_MSA_OPT > 0
24 #ifdef PNG_MIPS_MSA_CHECK_SUPPORTED /* Do run-time checks */
25 /* WARNING: it is strongly recommended that you do not build libpng with
26  * run-time checks for CPU features if at all possible.  In the case of the MIPS
27  * MSA instructions there is no processor-specific way of detecting the
28  * presence of the required support, therefore run-time detection is extremely
29  * OS specific.
30  *
31  * You may set the macro PNG_MIPS_MSA_FILE to the file name of file containing
32  * a fragment of C source code which defines the png_have_msa function.  There
33  * are a number of implementations in contrib/mips-msa, but the only one that
34  * has partial support is contrib/mips-msa/linux.c - a generic Linux
35  * implementation which reads /proc/cpufino.
36  */
37 #ifndef PNG_MIPS_MSA_FILE
38 #  ifdef __linux__
39 #     define PNG_MIPS_MSA_FILE "contrib/mips-msa/linux.c"
40 #  endif
41 #endif
42 
43 #ifdef PNG_MIPS_MSA_FILE
44 
45 #include <signal.h> /* for sig_atomic_t */
46 static int png_have_msa(png_structp png_ptr);
47 #include PNG_MIPS_MSA_FILE
48 
49 #else  /* PNG_MIPS_MSA_FILE */
50 #  error "PNG_MIPS_MSA_FILE undefined: no support for run-time MIPS MSA checks"
51 #endif /* PNG_MIPS_MSA_FILE */
52 #endif /* PNG_MIPS_MSA_CHECK_SUPPORTED */
53 
54 #ifndef PNG_ALIGNED_MEMORY_SUPPORTED
55 #  error "ALIGNED_MEMORY is required; set: -DPNG_ALIGNED_MEMORY_SUPPORTED"
56 #endif
57 
58 void
png_init_filter_functions_msa(png_structp pp,unsigned int bpp)59 png_init_filter_functions_msa(png_structp pp, unsigned int bpp)
60 {
61    /* The switch statement is compiled in for MIPS_MSA_API, the call to
62     * png_have_msa is compiled in for MIPS_MSA_CHECK. If both are defined
63     * the check is only performed if the API has not set the MSA option on
64     * or off explicitly. In this case the check controls what happens.
65     */
66 
67 #ifdef PNG_MIPS_MSA_API_SUPPORTED
68    switch ((pp->options >> PNG_MIPS_MSA) & 3)
69    {
70       case PNG_OPTION_UNSET:
71          /* Allow the run-time check to execute if it has been enabled -
72           * thus both API and CHECK can be turned on.  If it isn't supported
73           * this case will fall through to the 'default' below, which just
74           * returns.
75           */
76 #endif /* PNG_MIPS_MSA_API_SUPPORTED */
77 #ifdef PNG_MIPS_MSA_CHECK_SUPPORTED
78          {
79             static volatile sig_atomic_t no_msa = -1; /* not checked */
80 
81             if (no_msa < 0)
82                no_msa = !png_have_msa(pp);
83 
84             if (no_msa)
85                return;
86          }
87 #ifdef PNG_MIPS_MSA_API_SUPPORTED
88          break;
89 #endif
90 #endif /* PNG_MIPS_MSA_CHECK_SUPPORTED */
91 
92 #ifdef PNG_MIPS_MSA_API_SUPPORTED
93       default: /* OFF or INVALID */
94          return;
95 
96       case PNG_OPTION_ON:
97          /* Option turned on */
98          break;
99    }
100 #endif
101 
102    /* IMPORTANT: any new external functions used here must be declared using
103     * PNG_INTERNAL_FUNCTION in ../pngpriv.h.  This is required so that the
104     * 'prefix' option to configure works:
105     *
106     *    ./configure --with-libpng-prefix=foobar_
107     *
108     * Verify you have got this right by running the above command, doing a build
109     * and examining pngprefix.h; it must contain a #define for every external
110     * function you add.  (Notice that this happens automatically for the
111     * initialization function.)
112     */
113    pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_msa;
114 
115    if (bpp == 3)
116    {
117       pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub3_msa;
118       pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg3_msa;
119       pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = png_read_filter_row_paeth3_msa;
120    }
121 
122    else if (bpp == 4)
123    {
124       pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub4_msa;
125       pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg4_msa;
126       pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = png_read_filter_row_paeth4_msa;
127    }
128 }
129 #endif /* PNG_MIPS_MSA_OPT > 0 */
130 #endif /* READ */
131