arm_init.c 4.9 KB

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