blob: fb3d50d04ae8dc5dad891905d13127a379494e59 [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.
Matt Sarett9ea75692016-01-08 13:00:42 -05006 * Last changed in libpng 1.6.16 [December 22, 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
Matt Sarett9ea75692016-01-08 13:00:42 -050020
Chris Craikb50c2172013-07-29 15:28:30 -070021#if PNG_ARM_NEON_OPT > 0
22#ifdef PNG_ARM_NEON_CHECK_SUPPORTED /* Do run-time checks */
Sireesh Tripurarib478e662014-05-09 15:15:10 +053023/* WARNING: it is strongly recommended that you do not build libpng with
24 * run-time checks for CPU features if at all possible. In the case of the ARM
25 * NEON instructions there is no processor-specific way of detecting the
Matt Sarett9ea75692016-01-08 13:00:42 -050026 * presence of the required support, therefore run-time detection is extremely
Sireesh Tripurarib478e662014-05-09 15:15:10 +053027 * OS specific.
Chris Craikb50c2172013-07-29 15:28:30 -070028 *
Sireesh Tripurarib478e662014-05-09 15:15:10 +053029 * You may set the macro PNG_ARM_NEON_FILE to the file name of file containing
30 * a fragment of C source code which defines the png_have_neon function. There
31 * are a number of implementations in contrib/arm-neon, but the only one that
32 * has partial support is contrib/arm-neon/linux.c - a generic Linux
33 * implementation which reads /proc/cpufino.
Chris Craikb50c2172013-07-29 15:28:30 -070034 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +053035#ifndef PNG_ARM_NEON_FILE
36# ifdef __linux__
37# define PNG_ARM_NEON_FILE "contrib/arm-neon/linux.c"
38# endif
39#endif
Chris Craikb50c2172013-07-29 15:28:30 -070040
Sireesh Tripurarib478e662014-05-09 15:15:10 +053041#ifdef PNG_ARM_NEON_FILE
Chris Craikb50c2172013-07-29 15:28:30 -070042
Sireesh Tripurarib478e662014-05-09 15:15:10 +053043#include <signal.h> /* for sig_atomic_t */
44static int png_have_neon(png_structp png_ptr);
45#include PNG_ARM_NEON_FILE
Chris Craikb50c2172013-07-29 15:28:30 -070046
Sireesh Tripurarib478e662014-05-09 15:15:10 +053047#else /* PNG_ARM_NEON_FILE */
48# error "PNG_ARM_NEON_FILE undefined: no support for run-time ARM NEON checks"
49#endif /* PNG_ARM_NEON_FILE */
Chris Craikb50c2172013-07-29 15:28:30 -070050#endif /* PNG_ARM_NEON_CHECK_SUPPORTED */
51
52#ifndef PNG_ALIGNED_MEMORY_SUPPORTED
53# error "ALIGNED_MEMORY is required; set: -DPNG_ALIGNED_MEMORY_SUPPORTED"
54#endif
55
56void
57png_init_filter_functions_neon(png_structp pp, unsigned int bpp)
58{
Sireesh Tripurarib478e662014-05-09 15:15:10 +053059 /* The switch statement is compiled in for ARM_NEON_API, the call to
60 * png_have_neon is compiled in for ARM_NEON_CHECK. If both are defined
61 * the check is only performed if the API has not set the NEON option on
62 * or off explicitly. In this case the check controls what happens.
63 *
64 * If the CHECK is not compiled in and the option is UNSET the behavior prior
65 * to 1.6.7 was to use the NEON code - this was a bug caused by having the
66 * wrong order of the 'ON' and 'default' cases. UNSET now defaults to OFF,
67 * as documented in png.h
68 */
Chris Craikb50c2172013-07-29 15:28:30 -070069#ifdef PNG_ARM_NEON_API_SUPPORTED
70 switch ((pp->options >> PNG_ARM_NEON) & 3)
71 {
72 case PNG_OPTION_UNSET:
73 /* Allow the run-time check to execute if it has been enabled -
74 * thus both API and CHECK can be turned on. If it isn't supported
75 * this case will fall through to the 'default' below, which just
76 * returns.
77 */
78#endif /* PNG_ARM_NEON_API_SUPPORTED */
79#ifdef PNG_ARM_NEON_CHECK_SUPPORTED
80 {
81 static volatile sig_atomic_t no_neon = -1; /* not checked */
82
83 if (no_neon < 0)
84 no_neon = !png_have_neon(pp);
85
86 if (no_neon)
87 return;
88 }
89#ifdef PNG_ARM_NEON_API_SUPPORTED
90 break;
91#endif
92#endif /* PNG_ARM_NEON_CHECK_SUPPORTED */
Sireesh Tripurarib478e662014-05-09 15:15:10 +053093
Chris Craikb50c2172013-07-29 15:28:30 -070094#ifdef PNG_ARM_NEON_API_SUPPORTED
Sireesh Tripurarib478e662014-05-09 15:15:10 +053095 default: /* OFF or INVALID */
96 return;
97
Chris Craikb50c2172013-07-29 15:28:30 -070098 case PNG_OPTION_ON:
99 /* Option turned on */
100 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700101 }
102#endif
103
104 /* IMPORTANT: any new external functions used here must be declared using
105 * PNG_INTERNAL_FUNCTION in ../pngpriv.h. This is required so that the
106 * 'prefix' option to configure works:
107 *
108 * ./configure --with-libpng-prefix=foobar_
109 *
110 * Verify you have got this right by running the above command, doing a build
111 * and examining pngprefix.h; it must contain a #define for every external
112 * function you add. (Notice that this happens automatically for the
113 * initialization function.)
114 */
115 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_neon;
116
117 if (bpp == 3)
118 {
119 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub3_neon;
120 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg3_neon;
121 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
122 png_read_filter_row_paeth3_neon;
123 }
124
125 else if (bpp == 4)
126 {
127 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub4_neon;
128 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg4_neon;
129 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
130 png_read_filter_row_paeth4_neon;
131 }
132}
133#endif /* PNG_ARM_NEON_OPT > 0 */
Matt Sarett9ea75692016-01-08 13:00:42 -0500134#endif /* READ */