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