arm_init.c 6.8 KB

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