xref: /aosp_15_r20/external/libpng/arm/arm_init.c (revision a67afe4df73cf47866eedc69947994b8ff839aba)
1*a67afe4dSAndroid Build Coastguard Worker 
2*a67afe4dSAndroid Build Coastguard Worker /* arm_init.c - NEON optimised filter functions
3*a67afe4dSAndroid Build Coastguard Worker  *
4*a67afe4dSAndroid Build Coastguard Worker  * Copyright (c) 2018-2022 Cosmin Truta
5*a67afe4dSAndroid Build Coastguard Worker  * Copyright (c) 2014,2016 Glenn Randers-Pehrson
6*a67afe4dSAndroid Build Coastguard Worker  * Written by Mans Rullgard, 2011.
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 /* This module requires POSIX 1003.1 functions. */
14*a67afe4dSAndroid Build Coastguard Worker #define _POSIX_SOURCE 1
15*a67afe4dSAndroid Build Coastguard Worker 
16*a67afe4dSAndroid Build Coastguard Worker #include "../pngpriv.h"
17*a67afe4dSAndroid Build Coastguard Worker 
18*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_SUPPORTED
19*a67afe4dSAndroid Build Coastguard Worker 
20*a67afe4dSAndroid Build Coastguard Worker #if PNG_ARM_NEON_OPT > 0
21*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_ARM_NEON_CHECK_SUPPORTED /* Do run-time checks */
22*a67afe4dSAndroid Build Coastguard Worker /* WARNING: it is strongly recommended that you do not build libpng with
23*a67afe4dSAndroid Build Coastguard Worker  * run-time checks for CPU features if at all possible.  In the case of the ARM
24*a67afe4dSAndroid Build Coastguard Worker  * NEON instructions there is no processor-specific way of detecting the
25*a67afe4dSAndroid Build Coastguard Worker  * presence of the required support, therefore run-time detection is extremely
26*a67afe4dSAndroid Build Coastguard Worker  * OS specific.
27*a67afe4dSAndroid Build Coastguard Worker  *
28*a67afe4dSAndroid Build Coastguard Worker  * You may set the macro PNG_ARM_NEON_FILE to the file name of file containing
29*a67afe4dSAndroid Build Coastguard Worker  * a fragment of C source code which defines the png_have_neon function.  There
30*a67afe4dSAndroid Build Coastguard Worker  * are a number of implementations in contrib/arm-neon, but the only one that
31*a67afe4dSAndroid Build Coastguard Worker  * has partial support is contrib/arm-neon/linux.c - a generic Linux
32*a67afe4dSAndroid Build Coastguard Worker  * implementation which reads /proc/cpufino.
33*a67afe4dSAndroid Build Coastguard Worker  */
34*a67afe4dSAndroid Build Coastguard Worker #include <signal.h> /* for sig_atomic_t */
35*a67afe4dSAndroid Build Coastguard Worker 
36*a67afe4dSAndroid Build Coastguard Worker #ifndef PNG_ARM_NEON_FILE
37*a67afe4dSAndroid Build Coastguard Worker #  if defined(__aarch64__) || defined(_M_ARM64)
38*a67afe4dSAndroid Build Coastguard Worker      /* ARM Neon is expected to be unconditionally available on ARM64. */
39*a67afe4dSAndroid Build Coastguard Worker #    error "PNG_ARM_NEON_CHECK_SUPPORTED must not be defined on ARM64"
40*a67afe4dSAndroid Build Coastguard Worker #  elif defined(__ARM_NEON__) || defined(__ARM_NEON)
41*a67afe4dSAndroid Build Coastguard Worker      /* ARM Neon is expected to be available on the target CPU architecture. */
42*a67afe4dSAndroid Build Coastguard Worker #    error "PNG_ARM_NEON_CHECK_SUPPORTED must not be defined on this CPU arch"
43*a67afe4dSAndroid Build Coastguard Worker #  elif defined(__linux__)
44*a67afe4dSAndroid Build Coastguard Worker #    define PNG_ARM_NEON_FILE "contrib/arm-neon/linux.c"
45*a67afe4dSAndroid Build Coastguard Worker #  else
46*a67afe4dSAndroid Build Coastguard Worker #    error "No support for run-time ARM Neon checking; use compile-time options"
47*a67afe4dSAndroid Build Coastguard Worker #  endif
48*a67afe4dSAndroid Build Coastguard Worker #endif
49*a67afe4dSAndroid Build Coastguard Worker 
50*a67afe4dSAndroid Build Coastguard Worker static int png_have_neon(png_structp png_ptr);
51*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_ARM_NEON_FILE
52*a67afe4dSAndroid Build Coastguard Worker #  include PNG_ARM_NEON_FILE
53*a67afe4dSAndroid Build Coastguard Worker #endif
54*a67afe4dSAndroid Build Coastguard Worker #endif /* PNG_ARM_NEON_CHECK_SUPPORTED */
55*a67afe4dSAndroid Build Coastguard Worker 
56*a67afe4dSAndroid Build Coastguard Worker #ifndef PNG_ALIGNED_MEMORY_SUPPORTED
57*a67afe4dSAndroid Build Coastguard Worker #  error "ALIGNED_MEMORY is required; set: -DPNG_ALIGNED_MEMORY_SUPPORTED"
58*a67afe4dSAndroid Build Coastguard Worker #endif
59*a67afe4dSAndroid Build Coastguard Worker 
60*a67afe4dSAndroid Build Coastguard Worker void
png_init_filter_functions_neon(png_structp pp,unsigned int bpp)61*a67afe4dSAndroid Build Coastguard Worker png_init_filter_functions_neon(png_structp pp, unsigned int bpp)
62*a67afe4dSAndroid Build Coastguard Worker {
63*a67afe4dSAndroid Build Coastguard Worker    /* The switch statement is compiled in for ARM_NEON_API, the call to
64*a67afe4dSAndroid Build Coastguard Worker     * png_have_neon is compiled in for ARM_NEON_CHECK.  If both are defined
65*a67afe4dSAndroid Build Coastguard Worker     * the check is only performed if the API has not set the NEON option on
66*a67afe4dSAndroid Build Coastguard Worker     * or off explicitly.  In this case the check controls what happens.
67*a67afe4dSAndroid Build Coastguard Worker     *
68*a67afe4dSAndroid Build Coastguard Worker     * If the CHECK is not compiled in and the option is UNSET the behavior prior
69*a67afe4dSAndroid Build Coastguard Worker     * to 1.6.7 was to use the NEON code - this was a bug caused by having the
70*a67afe4dSAndroid Build Coastguard Worker     * wrong order of the 'ON' and 'default' cases.  UNSET now defaults to OFF,
71*a67afe4dSAndroid Build Coastguard Worker     * as documented in png.h
72*a67afe4dSAndroid Build Coastguard Worker     */
73*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_init_filter_functions_neon");
74*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_ARM_NEON_API_SUPPORTED
75*a67afe4dSAndroid Build Coastguard Worker    switch ((pp->options >> PNG_ARM_NEON) & 3)
76*a67afe4dSAndroid Build Coastguard Worker    {
77*a67afe4dSAndroid Build Coastguard Worker       case PNG_OPTION_UNSET:
78*a67afe4dSAndroid Build Coastguard Worker          /* Allow the run-time check to execute if it has been enabled -
79*a67afe4dSAndroid Build Coastguard Worker           * thus both API and CHECK can be turned on.  If it isn't supported
80*a67afe4dSAndroid Build Coastguard Worker           * this case will fall through to the 'default' below, which just
81*a67afe4dSAndroid Build Coastguard Worker           * returns.
82*a67afe4dSAndroid Build Coastguard Worker           */
83*a67afe4dSAndroid Build Coastguard Worker #endif /* PNG_ARM_NEON_API_SUPPORTED */
84*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_ARM_NEON_CHECK_SUPPORTED
85*a67afe4dSAndroid Build Coastguard Worker          {
86*a67afe4dSAndroid Build Coastguard Worker             static volatile sig_atomic_t no_neon = -1; /* not checked */
87*a67afe4dSAndroid Build Coastguard Worker 
88*a67afe4dSAndroid Build Coastguard Worker             if (no_neon < 0)
89*a67afe4dSAndroid Build Coastguard Worker                no_neon = !png_have_neon(pp);
90*a67afe4dSAndroid Build Coastguard Worker 
91*a67afe4dSAndroid Build Coastguard Worker             if (no_neon)
92*a67afe4dSAndroid Build Coastguard Worker                return;
93*a67afe4dSAndroid Build Coastguard Worker          }
94*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_ARM_NEON_API_SUPPORTED
95*a67afe4dSAndroid Build Coastguard Worker          break;
96*a67afe4dSAndroid Build Coastguard Worker #endif
97*a67afe4dSAndroid Build Coastguard Worker #endif /* PNG_ARM_NEON_CHECK_SUPPORTED */
98*a67afe4dSAndroid Build Coastguard Worker 
99*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_ARM_NEON_API_SUPPORTED
100*a67afe4dSAndroid Build Coastguard Worker       default: /* OFF or INVALID */
101*a67afe4dSAndroid Build Coastguard Worker          return;
102*a67afe4dSAndroid Build Coastguard Worker 
103*a67afe4dSAndroid Build Coastguard Worker       case PNG_OPTION_ON:
104*a67afe4dSAndroid Build Coastguard Worker          /* Option turned on */
105*a67afe4dSAndroid Build Coastguard Worker          break;
106*a67afe4dSAndroid Build Coastguard Worker    }
107*a67afe4dSAndroid Build Coastguard Worker #endif
108*a67afe4dSAndroid Build Coastguard Worker 
109*a67afe4dSAndroid Build Coastguard Worker    /* IMPORTANT: any new external functions used here must be declared using
110*a67afe4dSAndroid Build Coastguard Worker     * PNG_INTERNAL_FUNCTION in ../pngpriv.h.  This is required so that the
111*a67afe4dSAndroid Build Coastguard Worker     * 'prefix' option to configure works:
112*a67afe4dSAndroid Build Coastguard Worker     *
113*a67afe4dSAndroid Build Coastguard Worker     *    ./configure --with-libpng-prefix=foobar_
114*a67afe4dSAndroid Build Coastguard Worker     *
115*a67afe4dSAndroid Build Coastguard Worker     * Verify you have got this right by running the above command, doing a build
116*a67afe4dSAndroid Build Coastguard Worker     * and examining pngprefix.h; it must contain a #define for every external
117*a67afe4dSAndroid Build Coastguard Worker     * function you add.  (Notice that this happens automatically for the
118*a67afe4dSAndroid Build Coastguard Worker     * initialization function.)
119*a67afe4dSAndroid Build Coastguard Worker     */
120*a67afe4dSAndroid Build Coastguard Worker    pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_neon;
121*a67afe4dSAndroid Build Coastguard Worker 
122*a67afe4dSAndroid Build Coastguard Worker    if (bpp == 3)
123*a67afe4dSAndroid Build Coastguard Worker    {
124*a67afe4dSAndroid Build Coastguard Worker       pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub3_neon;
125*a67afe4dSAndroid Build Coastguard Worker       pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg3_neon;
126*a67afe4dSAndroid Build Coastguard Worker       pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
127*a67afe4dSAndroid Build Coastguard Worker          png_read_filter_row_paeth3_neon;
128*a67afe4dSAndroid Build Coastguard Worker    }
129*a67afe4dSAndroid Build Coastguard Worker 
130*a67afe4dSAndroid Build Coastguard Worker    else if (bpp == 4)
131*a67afe4dSAndroid Build Coastguard Worker    {
132*a67afe4dSAndroid Build Coastguard Worker       pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub4_neon;
133*a67afe4dSAndroid Build Coastguard Worker       pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg4_neon;
134*a67afe4dSAndroid Build Coastguard Worker       pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
135*a67afe4dSAndroid Build Coastguard Worker           png_read_filter_row_paeth4_neon;
136*a67afe4dSAndroid Build Coastguard Worker    }
137*a67afe4dSAndroid Build Coastguard Worker }
138*a67afe4dSAndroid Build Coastguard Worker #endif /* PNG_ARM_NEON_OPT > 0 */
139*a67afe4dSAndroid Build Coastguard Worker #endif /* READ */
140