]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libpng/lib/contrib/arm/arm_init.c
17aa010ae27040a4bb02037493a99ca3563f3d45
[l4.git] / l4 / pkg / libpng / lib / contrib / arm / arm_init.c
1
2 /* arm_init.c - NEON optimised filter functions
3  *
4  * Copyright (c) 2013 Glenn Randers-Pehrson
5  * Written by Mans Rullgard, 2011.
6  * Last changed in libpng 1.5.17 [July 18, 2013]
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 /* 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_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) ==
21      PNG_OPTION_ON)
22 #if PNG_ARM_NEON_OPT > 0
23 #ifdef PNG_ARM_NEON_CHECK_SUPPORTED /* Do run-time checks */
24 #include <signal.h> /* for sig_atomic_t */
25
26 #ifdef __ANDROID__
27 /* Linux provides access to information about CPU capabilites via
28  * /proc/self/auxv, however Android blocks this while still claiming to be
29  * Linux.  The Andoid NDK, however, provides appropriate support.
30  *
31  * Documentation: http://www.kandroid.org/ndk/docs/CPU-ARM-NEON.html
32  */
33 #include <cpu-features.h>
34
35 static int
36 png_have_neon(png_structp png_ptr)
37 {
38    /* This is a whole lot easier than the mess below, however it is probably
39     * implemented as below, therefore it is better to cache the result (these
40     * function calls may be slow!)
41     */
42    PNG_UNUSED(png_ptr)
43    return android_getCpuFamily() == ANDROID_CPU_FAMILY_ARM &&
44       (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0;
45 }
46 #elif defined(__linux__)
47 /* The generic __linux__ implementation requires reading /proc/self/auxv and
48  * looking at each element for one that records NEON capabilities.
49  */
50 #include <unistd.h> /* for POSIX 1003.1 */
51 #include <errno.h>  /* for EINTR */
52
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 #include <fcntl.h>
56 #include <elf.h>
57 #include <asm/hwcap.h>
58
59 /* A read call may be interrupted, in which case it returns -1 and sets errno to
60  * EINTR if nothing was done, otherwise (if something was done) a partial read
61  * may result.
62  */
63 static size_t
64 safe_read(png_structp png_ptr, int fd, void *buffer_in, size_t nbytes)
65 {
66    size_t ntotal = 0;
67    char *buffer = png_voidcast(char*, buffer_in);
68
69    while (nbytes > 0)
70    {
71       unsigned int nread;
72       int iread;
73
74       /* Passing nread > INT_MAX to read is implementation defined in POSIX
75        * 1003.1, therefore despite the unsigned argument portable code must
76        * limit the value to INT_MAX!
77        */
78       if (nbytes > INT_MAX)
79          nread = INT_MAX;
80
81       else
82          nread = (unsigned int)/*SAFE*/nbytes;
83
84       iread = read(fd, buffer, nread);
85
86       if (iread == -1)
87       {
88          /* This is the devil in the details, a read can terminate early with 0
89           * bytes read because of EINTR, yet it still returns -1 otherwise end
90           * of file cannot be distinguished.
91           */
92          if (errno != EINTR)
93          {
94             png_warning(png_ptr, "/proc read failed");
95             return 0; /* I.e. a permanent failure */
96          }
97       }
98
99       else if (iread < 0)
100       {
101          /* Not a valid 'read' result: */
102          png_warning(png_ptr, "OS /proc read bug");
103          return 0;
104       }
105
106       else if (iread > 0)
107       {
108          /* Continue reading until a permanent failure, or EOF */
109          buffer += iread;
110          nbytes -= (unsigned int)/*SAFE*/iread;
111          ntotal += (unsigned int)/*SAFE*/iread;
112       }
113
114       else
115          return ntotal;
116    }
117
118    return ntotal; /* nbytes == 0 */
119 }
120
121 static int
122 png_have_neon(png_structp png_ptr)
123 {
124    int fd = open("/proc/self/auxv", O_RDONLY);
125    Elf32_auxv_t aux;
126
127    /* Failsafe: failure to open means no NEON */
128    if (fd == -1)
129    {
130       png_warning(png_ptr, "/proc/self/auxv open failed");
131       return 0;
132    }
133
134    while (safe_read(png_ptr, fd, &aux, sizeof aux) == sizeof aux)
135    {
136       if (aux.a_type == AT_HWCAP && (aux.a_un.a_val & HWCAP_NEON) != 0)
137       {
138          close(fd);
139          return 1;
140       }
141    }
142
143    close(fd);
144    return 0;
145 }
146 #else
147    /* We don't know how to do a run-time check on this system */
148 #  error "no support for run-time ARM NEON checks"
149 #endif /* OS checks */
150 #endif /* PNG_ARM_NEON_CHECK_SUPPORTED */
151
152 #ifndef PNG_ALIGNED_MEMORY_SUPPORTED
153 #  error "ALIGNED_MEMORY is required; set: -DPNG_ALIGNED_MEMORY_SUPPORTED"
154 #endif
155
156 void
157 png_init_filter_functions_neon(png_structp pp, unsigned int bpp)
158 {
159 #ifdef PNG_ARM_NEON_API_SUPPORTED
160    switch ((pp->options >> PNG_ARM_NEON) & 3)
161    {
162       case PNG_OPTION_UNSET:
163          /* Allow the run-time check to execute if it has been enabled -
164           * thus both API and CHECK can be turned on.  If it isn't supported
165           * this case will fall through to the 'default' below, which just
166           * returns.
167           */
168 #endif /* PNG_ARM_NEON_API_SUPPORTED */
169 #ifdef PNG_ARM_NEON_CHECK_SUPPORTED
170          {
171             static volatile sig_atomic_t no_neon = -1; /* not checked */
172
173             if (no_neon < 0)
174                no_neon = !png_have_neon(pp);
175
176             if (no_neon)
177                return;
178          }
179 #ifdef PNG_ARM_NEON_API_SUPPORTED
180          break;
181 #endif
182 #endif /* PNG_ARM_NEON_CHECK_SUPPORTED */
183 #ifdef PNG_ARM_NEON_API_SUPPORTED
184       case PNG_OPTION_ON:
185          /* Option turned on */
186          break;
187
188       default: /* OFF or INVALID */
189          return;
190    }
191 #endif
192
193    /* IMPORTANT: any new external functions used here must be declared using
194     * PNG_INTERNAL_FUNCTION in ../pngpriv.h.  This is required so that the
195     * 'prefix' option to configure works:
196     *
197     *    ./configure --with-libpng-prefix=foobar_
198     *
199     * Verify you have got this right by running the above command, doing a build
200     * and examining pngprefix.h; it must contain a #define for every external
201     * function you add.  (Notice that this happens automatically for the
202     * initialization function.)
203     */
204    pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_neon;
205
206    if (bpp == 3)
207    {
208       pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub3_neon;
209       pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg3_neon;
210       pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
211          png_read_filter_row_paeth3_neon;
212    }
213
214    else if (bpp == 4)
215    {
216       pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub4_neon;
217       pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg4_neon;
218       pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
219           png_read_filter_row_paeth4_neon;
220    }
221 }
222 #endif /* PNG_ARM_NEON_OPT > 0 */
223 #endif /* PNG_READ_SUPPORTED */