1 
2 /* powerpc_init.c - POWERPC optimised filter functions
3  *
4  * Copyright (c) 2018 Cosmin Truta
5  * Copyright (c) 2017 Glenn Randers-Pehrson
6  * Written by Vadim Barkov, 2017.
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_POWERPC_VSX_OPT > 0
24 #ifdef PNG_POWERPC_VSX_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 PowerPC
27  * VSX 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_POWERPC_VSX_FILE to the file name of file containing
32  * a fragment of C source code which defines the png_have_vsx function.  There
33  * are a number of implementations in contrib/powerpc-vsx, but the only one that
34  * has partial support is contrib/powerpc-vsx/linux.c - a generic Linux
35  * implementation which reads /proc/cpufino.
36  */
37 #ifndef PNG_POWERPC_VSX_FILE
38 #  ifdef __linux__
39 #     define  PNG_POWERPC_VSX_FILE "contrib/powerpc-vsx/linux_aux.c"
40 #  endif
41 #endif
42 
43 #ifdef PNG_POWERPC_VSX_FILE
44 
45 #include <signal.h> /* for sig_atomic_t */
46 static int png_have_vsx(png_structp png_ptr);
47 #include PNG_POWERPC_VSX_FILE
48 
49 #else  /* PNG_POWERPC_VSX_FILE */
50 #  error "PNG_POWERPC_VSX_FILE undefined: no support for run-time POWERPC VSX checks"
51 #endif /* PNG_POWERPC_VSX_FILE */
52 #endif /* PNG_POWERPC_VSX_CHECK_SUPPORTED */
53 
54 void
png_init_filter_functions_vsx(png_structp pp,unsigned int bpp)55 png_init_filter_functions_vsx(png_structp pp, unsigned int bpp)
56 {
57    /* The switch statement is compiled in for POWERPC_VSX_API, the call to
58     * png_have_vsx is compiled in for POWERPC_VSX_CHECK. If both are defined
59     * the check is only performed if the API has not set the PowerPC option on
60     * or off explicitly. In this case the check controls what happens.
61     */
62 
63 #ifdef PNG_POWERPC_VSX_API_SUPPORTED
64    switch ((pp->options >> PNG_POWERPC_VSX) & 3)
65    {
66       case PNG_OPTION_UNSET:
67          /* Allow the run-time check to execute if it has been enabled -
68           * thus both API and CHECK can be turned on.  If it isn't supported
69           * this case will fall through to the 'default' below, which just
70           * returns.
71           */
72 #endif /* PNG_POWERPC_VSX_API_SUPPORTED */
73 #ifdef PNG_POWERPC_VSX_CHECK_SUPPORTED
74          {
75             static volatile sig_atomic_t no_vsx = -1; /* not checked */
76 
77             if (no_vsx < 0)
78                no_vsx = !png_have_vsx(pp);
79 
80             if (no_vsx)
81                return;
82          }
83 #ifdef PNG_POWERPC_VSX_API_SUPPORTED
84          break;
85 #endif
86 #endif /* PNG_POWERPC_VSX_CHECK_SUPPORTED */
87 
88 #ifdef PNG_POWERPC_VSX_API_SUPPORTED
89       default: /* OFF or INVALID */
90          return;
91 
92       case PNG_OPTION_ON:
93          /* Option turned on */
94          break;
95    }
96 #endif
97 
98    /* IMPORTANT: any new internal functions used here must be declared using
99     * PNG_INTERNAL_FUNCTION in ../pngpriv.h.  This is required so that the
100     * 'prefix' option to configure works:
101     *
102     *    ./configure --with-libpng-prefix=foobar_
103     *
104     * Verify you have got this right by running the above command, doing a build
105     * and examining pngprefix.h; it must contain a #define for every external
106     * function you add.  (Notice that this happens automatically for the
107     * initialization function.)
108     */
109    pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_vsx;
110 
111    if (bpp == 3)
112    {
113       pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub3_vsx;
114       pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg3_vsx;
115       pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = png_read_filter_row_paeth3_vsx;
116    }
117 
118    else if (bpp == 4)
119    {
120       pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub4_vsx;
121       pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg4_vsx;
122       pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = png_read_filter_row_paeth4_vsx;
123    }
124 }
125 #endif /* PNG_POWERPC_VSX_OPT > 0 */
126 #endif /* READ */
127