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
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
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>
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
39 safe_read(png_structp png_ptr
, int fd
, void *buffer_in
, size_t nbytes
)
42 char *buffer
= png_voidcast(char*, buffer_in
);
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!
57 nread
= (unsigned int)/*SAFE*/nbytes
;
59 iread
= read(fd
, buffer
, nread
);
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.
69 png_warning(png_ptr
, "/proc read failed");
70 return 0; /* I.e., a permanent failure */
76 /* Not a valid 'read' result: */
77 png_warning(png_ptr
, "OS /proc read bug");
83 /* Continue reading until a permanent failure, or EOF */
85 nbytes
-= (unsigned int)/*SAFE*/iread
;
86 ntotal
+= (unsigned int)/*SAFE*/iread
;
93 return ntotal
; /* nbytes == 0 */
97 png_have_neon(png_structp png_ptr
)
99 int fd
= open("/proc/self/auxv", O_RDONLY
);
102 /* Failsafe: failure to open means no NEON */
105 png_warning(png_ptr
, "/proc/self/auxv open failed");
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)