fix for openout message (HH)
[luatex.git] / source / libs / libpng / libpng-1.6.16 / contrib / arm-neon / linux-auxv.c
blob696e297ef4c4cfed27b9be56d506de5ad5f76d38
1 /* contrib/arm-neon/linux-auxv.c
3 * Copyright (c) 2014 Glenn Randers-Pehrson
4 * Written by Mans Rullgard, 2011.
5 * Last changed in libpng 1.6.10 [March 6, 2014]
7 * This code is released under the libpng license.
8 * For conditions of distribution and use, see the disclaimer
9 * and license in png.h
11 * SEE contrib/arm-neon/README before reporting bugs
13 * STATUS: COMPILED, TESTED
14 * BUG REPORTS: png-mng-implement@sourceforge.net
16 * png_have_neon implemented for Linux versions which allow access to
17 * /proc/self/auxv. This is probably faster, cleaner and safer than the code to
18 * read /proc/cpuinfo in contrib/arm-neon/linux, however it is yet another piece
19 * of potentially untested code and has more complex dependencies than the code
20 * to read cpuinfo.
22 * This generic __linux__ implementation requires reading /proc/self/auxv and
23 * looking at each element for one that records NEON capabilities.
25 #include <unistd.h> /* for POSIX 1003.1 */
26 #include <errno.h> /* for EINTR */
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <elf.h>
32 #include <asm/hwcap.h>
34 /* A read call may be interrupted, in which case it returns -1 and sets errno to
35 * EINTR if nothing was done, otherwise (if something was done) a partial read
36 * may result.
38 static size_t
39 safe_read(png_structp png_ptr, int fd, void *buffer_in, size_t nbytes)
41 size_t ntotal = 0;
42 char *buffer = png_voidcast(char*, buffer_in);
44 while (nbytes > 0)
46 unsigned int nread;
47 int iread;
49 /* Passing nread > INT_MAX to read is implementation defined in POSIX
50 * 1003.1, therefore despite the unsigned argument portable code must
51 * limit the value to INT_MAX!
53 if (nbytes > INT_MAX)
54 nread = INT_MAX;
56 else
57 nread = (unsigned int)/*SAFE*/nbytes;
59 iread = read(fd, buffer, nread);
61 if (iread == -1)
63 /* This is the devil in the details, a read can terminate early with 0
64 * bytes read because of EINTR, yet it still returns -1 otherwise end
65 * of file cannot be distinguished.
67 if (errno != EINTR)
69 png_warning(png_ptr, "/proc read failed");
70 return 0; /* I.e., a permanent failure */
74 else if (iread < 0)
76 /* Not a valid 'read' result: */
77 png_warning(png_ptr, "OS /proc read bug");
78 return 0;
81 else if (iread > 0)
83 /* Continue reading until a permanent failure, or EOF */
84 buffer += iread;
85 nbytes -= (unsigned int)/*SAFE*/iread;
86 ntotal += (unsigned int)/*SAFE*/iread;
89 else
90 return ntotal;
93 return ntotal; /* nbytes == 0 */
96 static int
97 png_have_neon(png_structp png_ptr)
99 int fd = open("/proc/self/auxv", O_RDONLY);
100 Elf32_auxv_t aux;
102 /* Failsafe: failure to open means no NEON */
103 if (fd == -1)
105 png_warning(png_ptr, "/proc/self/auxv open failed");
106 return 0;
109 while (safe_read(png_ptr, fd, &aux, sizeof aux) == sizeof aux)
111 if (aux.a_type == AT_HWCAP && (aux.a_un.a_val & HWCAP_NEON) != 0)
113 close(fd);
114 return 1;
118 close(fd);
119 return 0;