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