/* arm_init.c - NEON optimised filter functions1*2* Copyright (c) 2018-2022 Cosmin Truta3* Copyright (c) 2014,2016 Glenn Randers-Pehrson4* Written by Mans Rullgard, 2011.5*6* This code is released under the libpng license.7* For conditions of distribution and use, see the disclaimer8* and license in png.h9*/1011/* This module requires POSIX 1003.1 functions. */12#define _POSIX_SOURCE 11314#include "../pngpriv.h"1516#ifdef PNG_READ_SUPPORTED1718#if PNG_ARM_NEON_OPT > 019#ifdef PNG_ARM_NEON_CHECK_SUPPORTED /* Do run-time checks */20/* WARNING: it is strongly recommended that you do not build libpng with21* run-time checks for CPU features if at all possible. In the case of the ARM22* NEON instructions there is no processor-specific way of detecting the23* presence of the required support, therefore run-time detection is extremely24* OS specific.25*26* You may set the macro PNG_ARM_NEON_FILE to the file name of file containing27* a fragment of C source code which defines the png_have_neon function. There28* are a number of implementations in contrib/arm-neon, but the only one that29* has partial support is contrib/arm-neon/linux.c - a generic Linux30* implementation which reads /proc/cpufino.31*/32#include <signal.h> /* for sig_atomic_t */3334#ifndef PNG_ARM_NEON_FILE35# if defined(__aarch64__) || defined(_M_ARM64)36/* ARM Neon is expected to be unconditionally available on ARM64. */37# error PNG_ARM_NEON_CHECK_SUPPORTED must not be defined on ARM6438# elif defined(__ARM_NEON__) || defined(__ARM_NEON)39/* ARM Neon is expected to be available on the target CPU architecture. */40# error PNG_ARM_NEON_CHECK_SUPPORTED must not be defined on this CPU arch41# elif defined(__linux__)42# define PNG_ARM_NEON_FILE "contrib/arm-neon/linux.c"43# else44# error No support for run-time ARM Neon checking; use compile-time options45# endif46#endif4748static int png_have_neon(png_structp png_ptr);49#ifdef PNG_ARM_NEON_FILE50# include PNG_ARM_NEON_FILE51#endif52#endif /* PNG_ARM_NEON_CHECK_SUPPORTED */5354#ifndef PNG_ALIGNED_MEMORY_SUPPORTED55# error ALIGNED_MEMORY is required; please define PNG_ALIGNED_MEMORY_SUPPORTED56#endif5758void59png_init_filter_functions_neon(png_structp pp, unsigned int bpp)60{61/* The switch statement is compiled in for ARM_NEON_API, the call to62* png_have_neon is compiled in for ARM_NEON_CHECK. If both are defined63* the check is only performed if the API has not set the NEON option on64* or off explicitly. In this case the check controls what happens.65*66* If the CHECK is not compiled in and the option is UNSET the behavior prior67* to 1.6.7 was to use the NEON code - this was a bug caused by having the68* wrong order of the 'ON' and 'default' cases. UNSET now defaults to OFF,69* as documented in png.h70*/71png_debug(1, "in png_init_filter_functions_neon");72#ifdef PNG_ARM_NEON_API_SUPPORTED73switch ((pp->options >> PNG_ARM_NEON) & 3)74{75case PNG_OPTION_UNSET:76/* Allow the run-time check to execute if it has been enabled -77* thus both API and CHECK can be turned on. If it isn't supported78* this case will fall through to the 'default' below, which just79* returns.80*/81#endif /* PNG_ARM_NEON_API_SUPPORTED */82#ifdef PNG_ARM_NEON_CHECK_SUPPORTED83{84static volatile sig_atomic_t no_neon = -1; /* not checked */8586if (no_neon < 0)87no_neon = !png_have_neon(pp);8889if (no_neon)90return;91}92#ifdef PNG_ARM_NEON_API_SUPPORTED93break;94#endif95#endif /* PNG_ARM_NEON_CHECK_SUPPORTED */9697#ifdef PNG_ARM_NEON_API_SUPPORTED98default: /* OFF or INVALID */99return;100101case PNG_OPTION_ON:102/* Option turned on */103break;104}105#endif106107/* IMPORTANT: any new external functions used here must be declared using108* PNG_INTERNAL_FUNCTION in ../pngpriv.h. This is required so that the109* 'prefix' option to configure works:110*111* ./configure --with-libpng-prefix=foobar_112*113* Verify you have got this right by running the above command, doing a build114* and examining pngprefix.h; it must contain a #define for every external115* function you add. (Notice that this happens automatically for the116* initialization function.)117*/118pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_neon;119120if (bpp == 3)121{122pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub3_neon;123pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg3_neon;124pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =125png_read_filter_row_paeth3_neon;126}127128else if (bpp == 4)129{130pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub4_neon;131pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg4_neon;132pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =133png_read_filter_row_paeth4_neon;134}135}136#endif /* PNG_ARM_NEON_OPT > 0 */137#endif /* READ */138139140