blob: a34ecdbef7350a935f15aee8cfac6c75a75e4a87 [file] [log] [blame]
Chris Craikb50c2172013-07-29 15:28:30 -07001
2/* arm_init.c - NEON optimised filter functions
3 *
xNombred07bb0d2020-03-10 20:17:12 +01004 * Copyright (c) 2018 Cosmin Truta
Alex Naidis7a055fd2016-10-01 12:23:07 +02005 * Copyright (c) 2014,2016 Glenn Randers-Pehrson
Chris Craikb50c2172013-07-29 15:28:30 -07006 * Written by Mans Rullgard, 2011.
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 */
xNombred07bb0d2020-03-10 20:17:12 +010012
Chris Craikb50c2172013-07-29 15:28:30 -070013/* Below, after checking __linux__, various non-C90 POSIX 1003.1 functions are
14 * called.
15 */
16#define _POSIX_SOURCE 1
17
18#include "../pngpriv.h"
19
20#ifdef PNG_READ_SUPPORTED
Matt Sarett9b1fe632015-11-25 10:21:17 -050021
Chris Craikb50c2172013-07-29 15:28:30 -070022#if PNG_ARM_NEON_OPT > 0
23#ifdef PNG_ARM_NEON_CHECK_SUPPORTED /* Do run-time checks */
Sireesh Tripurarib478e662014-05-09 15:15:10 +053024/* 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 ARM
26 * NEON instructions there is no processor-specific way of detecting the
Matt Sarett9b1fe632015-11-25 10:21:17 -050027 * presence of the required support, therefore run-time detection is extremely
Sireesh Tripurarib478e662014-05-09 15:15:10 +053028 * OS specific.
Chris Craikb50c2172013-07-29 15:28:30 -070029 *
Sireesh Tripurarib478e662014-05-09 15:15:10 +053030 * You may set the macro PNG_ARM_NEON_FILE to the file name of file containing
31 * a fragment of C source code which defines the png_have_neon function. There
32 * are a number of implementations in contrib/arm-neon, but the only one that
33 * has partial support is contrib/arm-neon/linux.c - a generic Linux
34 * implementation which reads /proc/cpufino.
Chris Craikb50c2172013-07-29 15:28:30 -070035 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +053036#ifndef PNG_ARM_NEON_FILE
37# ifdef __linux__
38# define PNG_ARM_NEON_FILE "contrib/arm-neon/linux.c"
39# endif
40#endif
Chris Craikb50c2172013-07-29 15:28:30 -070041
Sireesh Tripurarib478e662014-05-09 15:15:10 +053042#ifdef PNG_ARM_NEON_FILE
Chris Craikb50c2172013-07-29 15:28:30 -070043
Sireesh Tripurarib478e662014-05-09 15:15:10 +053044#include <signal.h> /* for sig_atomic_t */
45static int png_have_neon(png_structp png_ptr);
46#include PNG_ARM_NEON_FILE
Chris Craikb50c2172013-07-29 15:28:30 -070047
Sireesh Tripurarib478e662014-05-09 15:15:10 +053048#else /* PNG_ARM_NEON_FILE */
49# error "PNG_ARM_NEON_FILE undefined: no support for run-time ARM NEON checks"
50#endif /* PNG_ARM_NEON_FILE */
Chris Craikb50c2172013-07-29 15:28:30 -070051#endif /* PNG_ARM_NEON_CHECK_SUPPORTED */
52
53#ifndef PNG_ALIGNED_MEMORY_SUPPORTED
54# error "ALIGNED_MEMORY is required; set: -DPNG_ALIGNED_MEMORY_SUPPORTED"
55#endif
56
57void
58png_init_filter_functions_neon(png_structp pp, unsigned int bpp)
59{
Sireesh Tripurarib478e662014-05-09 15:15:10 +053060 /* The switch statement is compiled in for ARM_NEON_API, the call to
61 * png_have_neon is compiled in for ARM_NEON_CHECK. If both are defined
62 * the check is only performed if the API has not set the NEON option on
63 * or off explicitly. In this case the check controls what happens.
64 *
65 * If the CHECK is not compiled in and the option is UNSET the behavior prior
66 * to 1.6.7 was to use the NEON code - this was a bug caused by having the
67 * wrong order of the 'ON' and 'default' cases. UNSET now defaults to OFF,
68 * as documented in png.h
69 */
Alex Naidis7a055fd2016-10-01 12:23:07 +020070 png_debug(1, "in png_init_filter_functions_neon");
Chris Craikb50c2172013-07-29 15:28:30 -070071#ifdef PNG_ARM_NEON_API_SUPPORTED
72 switch ((pp->options >> PNG_ARM_NEON) & 3)
73 {
74 case PNG_OPTION_UNSET:
75 /* Allow the run-time check to execute if it has been enabled -
76 * thus both API and CHECK can be turned on. If it isn't supported
77 * this case will fall through to the 'default' below, which just
78 * returns.
79 */
80#endif /* PNG_ARM_NEON_API_SUPPORTED */
81#ifdef PNG_ARM_NEON_CHECK_SUPPORTED
82 {
83 static volatile sig_atomic_t no_neon = -1; /* not checked */
84
85 if (no_neon < 0)
86 no_neon = !png_have_neon(pp);
87
88 if (no_neon)
89 return;
90 }
91#ifdef PNG_ARM_NEON_API_SUPPORTED
92 break;
93#endif
94#endif /* PNG_ARM_NEON_CHECK_SUPPORTED */
Sireesh Tripurarib478e662014-05-09 15:15:10 +053095
Chris Craikb50c2172013-07-29 15:28:30 -070096#ifdef PNG_ARM_NEON_API_SUPPORTED
Sireesh Tripurarib478e662014-05-09 15:15:10 +053097 default: /* OFF or INVALID */
98 return;
99
Chris Craikb50c2172013-07-29 15:28:30 -0700100 case PNG_OPTION_ON:
101 /* Option turned on */
102 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700103 }
104#endif
105
106 /* IMPORTANT: any new external functions used here must be declared using
107 * PNG_INTERNAL_FUNCTION in ../pngpriv.h. This is required so that the
108 * 'prefix' option to configure works:
109 *
110 * ./configure --with-libpng-prefix=foobar_
111 *
112 * Verify you have got this right by running the above command, doing a build
113 * and examining pngprefix.h; it must contain a #define for every external
114 * function you add. (Notice that this happens automatically for the
115 * initialization function.)
116 */
117 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_neon;
118
119 if (bpp == 3)
120 {
121 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub3_neon;
122 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg3_neon;
123 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
124 png_read_filter_row_paeth3_neon;
125 }
126
127 else if (bpp == 4)
128 {
129 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub4_neon;
130 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg4_neon;
131 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
132 png_read_filter_row_paeth4_neon;
133 }
134}
135#endif /* PNG_ARM_NEON_OPT > 0 */
Matt Sarett9b1fe632015-11-25 10:21:17 -0500136#endif /* READ */