fukasawa e60969
fukasawa e60969
/* arm_init.c - NEON optimised filter functions
fukasawa e60969
 *
fukasawa e60969
 * Copyright (c) 2014 Glenn Randers-Pehrson
fukasawa e60969
 * Written by Mans Rullgard, 2011.
fukasawa e60969
 * Last changed in libpng 1.6.16 [December 22, 2014]
fukasawa e60969
 *
fukasawa e60969
 * This code is released under the libpng license.
fukasawa e60969
 * For conditions of distribution and use, see the disclaimer
fukasawa e60969
 * and license in png.h
fukasawa e60969
 */
fukasawa e60969
/* Below, after checking __linux__, various non-C90 POSIX 1003.1 functions are
fukasawa e60969
 * called.
fukasawa e60969
 */
fukasawa e60969
#define _POSIX_SOURCE 1
fukasawa e60969
fukasawa e60969
#include "../pngpriv.h"
fukasawa e60969
fukasawa e60969
#ifdef PNG_READ_SUPPORTED
fukasawa e60969
fukasawa e60969
#if PNG_ARM_NEON_OPT > 0
fukasawa e60969
#ifdef PNG_ARM_NEON_CHECK_SUPPORTED /* Do run-time checks */
fukasawa e60969
/* WARNING: it is strongly recommended that you do not build libpng with
fukasawa e60969
 * run-time checks for CPU features if at all possible.  In the case of the ARM
fukasawa e60969
 * NEON instructions there is no processor-specific way of detecting the
fukasawa e60969
 * presence of the required support, therefore run-time detection is extremely
fukasawa e60969
 * OS specific.
fukasawa e60969
 *
fukasawa e60969
 * You may set the macro PNG_ARM_NEON_FILE to the file name of file containing
fukasawa e60969
 * a fragment of C source code which defines the png_have_neon function.  There
fukasawa e60969
 * are a number of implementations in contrib/arm-neon, but the only one that
fukasawa e60969
 * has partial support is contrib/arm-neon/linux.c - a generic Linux
fukasawa e60969
 * implementation which reads /proc/cpufino.
fukasawa e60969
 */
fukasawa e60969
#ifndef PNG_ARM_NEON_FILE
fukasawa e60969
#  ifdef __linux__
fukasawa e60969
#     define PNG_ARM_NEON_FILE "contrib/arm-neon/linux.c"
fukasawa e60969
#  endif
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_ARM_NEON_FILE
fukasawa e60969
fukasawa e60969
#include <signal.h> /* for sig_atomic_t */</signal.h>
fukasawa e60969
static int png_have_neon(png_structp png_ptr);
fukasawa e60969
#include PNG_ARM_NEON_FILE
fukasawa e60969
fukasawa e60969
#else  /* PNG_ARM_NEON_FILE */
fukasawa e60969
#  error "PNG_ARM_NEON_FILE undefined: no support for run-time ARM NEON checks"
fukasawa e60969
#endif /* PNG_ARM_NEON_FILE */
fukasawa e60969
#endif /* PNG_ARM_NEON_CHECK_SUPPORTED */
fukasawa e60969
fukasawa e60969
#ifndef PNG_ALIGNED_MEMORY_SUPPORTED
fukasawa e60969
#  error "ALIGNED_MEMORY is required; set: -DPNG_ALIGNED_MEMORY_SUPPORTED"
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
void
fukasawa e60969
png_init_filter_functions_neon(png_structp pp, unsigned int bpp)
fukasawa e60969
{
fukasawa e60969
   /* The switch statement is compiled in for ARM_NEON_API, the call to
fukasawa e60969
    * png_have_neon is compiled in for ARM_NEON_CHECK.  If both are defined
fukasawa e60969
    * the check is only performed if the API has not set the NEON option on
fukasawa e60969
    * or off explicitly.  In this case the check controls what happens.
fukasawa e60969
    *
fukasawa e60969
    * If the CHECK is not compiled in and the option is UNSET the behavior prior
fukasawa e60969
    * to 1.6.7 was to use the NEON code - this was a bug caused by having the
fukasawa e60969
    * wrong order of the 'ON' and 'default' cases.  UNSET now defaults to OFF,
fukasawa e60969
    * as documented in png.h
fukasawa e60969
    */
fukasawa e60969
#ifdef PNG_ARM_NEON_API_SUPPORTED
fukasawa e60969
   switch ((pp->options >> PNG_ARM_NEON) & 3)
fukasawa e60969
   {
fukasawa e60969
      case PNG_OPTION_UNSET:
fukasawa e60969
         /* Allow the run-time check to execute if it has been enabled -
fukasawa e60969
          * thus both API and CHECK can be turned on.  If it isn't supported
fukasawa e60969
          * this case will fall through to the 'default' below, which just
fukasawa e60969
          * returns.
fukasawa e60969
          */
fukasawa e60969
#endif /* PNG_ARM_NEON_API_SUPPORTED */
fukasawa e60969
#ifdef PNG_ARM_NEON_CHECK_SUPPORTED
fukasawa e60969
         {
fukasawa e60969
            static volatile sig_atomic_t no_neon = -1; /* not checked */
fukasawa e60969
fukasawa e60969
            if (no_neon < 0)
fukasawa e60969
               no_neon = !png_have_neon(pp);
fukasawa e60969
fukasawa e60969
            if (no_neon)
fukasawa e60969
               return;
fukasawa e60969
         }
fukasawa e60969
#ifdef PNG_ARM_NEON_API_SUPPORTED
fukasawa e60969
         break;
fukasawa e60969
#endif
fukasawa e60969
#endif /* PNG_ARM_NEON_CHECK_SUPPORTED */
fukasawa e60969
fukasawa e60969
#ifdef PNG_ARM_NEON_API_SUPPORTED
fukasawa e60969
      default: /* OFF or INVALID */
fukasawa e60969
         return;
fukasawa e60969
fukasawa e60969
      case PNG_OPTION_ON:
fukasawa e60969
         /* Option turned on */
fukasawa e60969
         break;
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* IMPORTANT: any new external functions used here must be declared using
fukasawa e60969
    * PNG_INTERNAL_FUNCTION in ../pngpriv.h.  This is required so that the
fukasawa e60969
    * 'prefix' option to configure works:
fukasawa e60969
    *
fukasawa e60969
    *    ./configure --with-libpng-prefix=foobar_
fukasawa e60969
    *
fukasawa e60969
    * Verify you have got this right by running the above command, doing a build
fukasawa e60969
    * and examining pngprefix.h; it must contain a #define for every external
fukasawa e60969
    * function you add.  (Notice that this happens automatically for the
fukasawa e60969
    * initialization function.)
fukasawa e60969
    */
fukasawa e60969
   pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_neon;
fukasawa e60969
fukasawa e60969
   if (bpp == 3)
fukasawa e60969
   {
fukasawa e60969
      pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub3_neon;
fukasawa e60969
      pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg3_neon;
fukasawa e60969
      pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
fukasawa e60969
         png_read_filter_row_paeth3_neon;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if (bpp == 4)
fukasawa e60969
   {
fukasawa e60969
      pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub4_neon;
fukasawa e60969
      pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg4_neon;
fukasawa e60969
      pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
fukasawa e60969
          png_read_filter_row_paeth4_neon;
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
#endif /* PNG_ARM_NEON_OPT > 0 */
fukasawa e60969
#endif /* READ */