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