blob: 952b8029f91547f492aa3cecfbb13f3b00d2954a [file] [log] [blame]
Chris Craikb50c2172013-07-29 15:28:30 -07001
2/* arm_init.c - NEON optimised filter functions
3 *
Sireesh Tripurarib478e662014-05-09 15:15:10 +05304 * Copyright (c) 2014 Glenn Randers-Pehrson
Chris Craikb50c2172013-07-29 15:28:30 -07005 * Written by Mans Rullgard, 2011.
Sireesh Tripurarib478e662014-05-09 15:15:10 +05306 * Last changed in libpng 1.6.10 [March 6, 2014]
Chris Craikb50c2172013-07-29 15:28:30 -07007 *
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 "../pngpriv.h"
18
19#ifdef PNG_READ_SUPPORTED
20#if PNG_ARM_NEON_OPT > 0
21#ifdef PNG_ARM_NEON_CHECK_SUPPORTED /* Do run-time checks */
Sireesh Tripurarib478e662014-05-09 15:15:10 +053022/* WARNING: it is strongly recommended that you do not build libpng with
23 * run-time checks for CPU features if at all possible. In the case of the ARM
24 * NEON instructions there is no processor-specific way of detecting the
25 * presense of the required support, therefore run-time detectioon is extremely
26 * OS specific.
Chris Craikb50c2172013-07-29 15:28:30 -070027 *
Sireesh Tripurarib478e662014-05-09 15:15:10 +053028 * You may set the macro PNG_ARM_NEON_FILE to the file name of file containing
29 * a fragment of C source code which defines the png_have_neon function. There
30 * are a number of implementations in contrib/arm-neon, but the only one that
31 * has partial support is contrib/arm-neon/linux.c - a generic Linux
32 * implementation which reads /proc/cpufino.
Chris Craikb50c2172013-07-29 15:28:30 -070033 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +053034#ifndef PNG_ARM_NEON_FILE
35# ifdef __linux__
36# define PNG_ARM_NEON_FILE "contrib/arm-neon/linux.c"
37# endif
38#endif
Chris Craikb50c2172013-07-29 15:28:30 -070039
Sireesh Tripurarib478e662014-05-09 15:15:10 +053040#ifdef PNG_ARM_NEON_FILE
Chris Craikb50c2172013-07-29 15:28:30 -070041
Sireesh Tripurarib478e662014-05-09 15:15:10 +053042#include <signal.h> /* for sig_atomic_t */
43static int png_have_neon(png_structp png_ptr);
44#include PNG_ARM_NEON_FILE
Chris Craikb50c2172013-07-29 15:28:30 -070045
Sireesh Tripurarib478e662014-05-09 15:15:10 +053046#else /* PNG_ARM_NEON_FILE */
47# error "PNG_ARM_NEON_FILE undefined: no support for run-time ARM NEON checks"
48#endif /* PNG_ARM_NEON_FILE */
Chris Craikb50c2172013-07-29 15:28:30 -070049#endif /* PNG_ARM_NEON_CHECK_SUPPORTED */
50
51#ifndef PNG_ALIGNED_MEMORY_SUPPORTED
52# error "ALIGNED_MEMORY is required; set: -DPNG_ALIGNED_MEMORY_SUPPORTED"
53#endif
54
55void
56png_init_filter_functions_neon(png_structp pp, unsigned int bpp)
57{
Sireesh Tripurarib478e662014-05-09 15:15:10 +053058 /* The switch statement is compiled in for ARM_NEON_API, the call to
59 * png_have_neon is compiled in for ARM_NEON_CHECK. If both are defined
60 * the check is only performed if the API has not set the NEON option on
61 * or off explicitly. In this case the check controls what happens.
62 *
63 * If the CHECK is not compiled in and the option is UNSET the behavior prior
64 * to 1.6.7 was to use the NEON code - this was a bug caused by having the
65 * wrong order of the 'ON' and 'default' cases. UNSET now defaults to OFF,
66 * as documented in png.h
67 */
Chris Craikb50c2172013-07-29 15:28:30 -070068#ifdef PNG_ARM_NEON_API_SUPPORTED
69 switch ((pp->options >> PNG_ARM_NEON) & 3)
70 {
71 case PNG_OPTION_UNSET:
72 /* Allow the run-time check to execute if it has been enabled -
73 * thus both API and CHECK can be turned on. If it isn't supported
74 * this case will fall through to the 'default' below, which just
75 * returns.
76 */
77#endif /* PNG_ARM_NEON_API_SUPPORTED */
78#ifdef PNG_ARM_NEON_CHECK_SUPPORTED
79 {
80 static volatile sig_atomic_t no_neon = -1; /* not checked */
81
82 if (no_neon < 0)
83 no_neon = !png_have_neon(pp);
84
85 if (no_neon)
86 return;
87 }
88#ifdef PNG_ARM_NEON_API_SUPPORTED
89 break;
90#endif
91#endif /* PNG_ARM_NEON_CHECK_SUPPORTED */
Sireesh Tripurarib478e662014-05-09 15:15:10 +053092
Chris Craikb50c2172013-07-29 15:28:30 -070093#ifdef PNG_ARM_NEON_API_SUPPORTED
Sireesh Tripurarib478e662014-05-09 15:15:10 +053094 default: /* OFF or INVALID */
95 return;
96
Chris Craikb50c2172013-07-29 15:28:30 -070097 case PNG_OPTION_ON:
98 /* Option turned on */
99 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700100 }
101#endif
102
103 /* IMPORTANT: any new external functions used here must be declared using
104 * PNG_INTERNAL_FUNCTION in ../pngpriv.h. This is required so that the
105 * 'prefix' option to configure works:
106 *
107 * ./configure --with-libpng-prefix=foobar_
108 *
109 * Verify you have got this right by running the above command, doing a build
110 * and examining pngprefix.h; it must contain a #define for every external
111 * function you add. (Notice that this happens automatically for the
112 * initialization function.)
113 */
114 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_neon;
115
116 if (bpp == 3)
117 {
118 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub3_neon;
119 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg3_neon;
120 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
121 png_read_filter_row_paeth3_neon;
122 }
123
124 else if (bpp == 4)
125 {
126 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub4_neon;
127 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg4_neon;
128 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
129 png_read_filter_row_paeth4_neon;
130 }
131}
132#endif /* PNG_ARM_NEON_OPT > 0 */
133#endif /* PNG_READ_SUPPORTED */