Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 1 | |
| 2 | /* mips_init.c - MSA optimised filter functions |
| 3 | * |
| 4 | * Copyright (c) 2016 Glenn Randers-Pehrson |
| 5 | * Written by Mandar Sahastrabuddhe, 2016. |
Matt Sarett | 851c677 | 2016-11-23 20:24:06 +0000 | [diff] [blame] | 6 | * Last changed in libpng 1.6.25 [September 1, 2016] |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 7 | * |
| 8 | * This code is released under the libpng license. |
| 9 | * For conditions of distribution and use, see the disclaimer |
| 10 | * and license in png.h |
| 11 | */ |
| 12 | /* Below, after checking __linux__, various non-C90 POSIX 1003.1 functions are |
| 13 | * called. |
| 14 | */ |
| 15 | #define _POSIX_SOURCE 1 |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include "../pngpriv.h" |
| 19 | |
| 20 | #ifdef PNG_READ_SUPPORTED |
| 21 | |
| 22 | #if PNG_MIPS_MSA_OPT > 0 |
| 23 | #ifdef PNG_MIPS_MSA_CHECK_SUPPORTED /* Do run-time checks */ |
| 24 | /* WARNING: it is strongly recommended that you do not build libpng with |
| 25 | * run-time checks for CPU features if at all possible. In the case of the MIPS |
| 26 | * MSA instructions there is no processor-specific way of detecting the |
| 27 | * presence of the required support, therefore run-time detection is extremely |
| 28 | * OS specific. |
| 29 | * |
| 30 | * You may set the macro PNG_MIPS_MSA_FILE to the file name of file containing |
| 31 | * a fragment of C source code which defines the png_have_msa function. There |
| 32 | * are a number of implementations in contrib/mips-msa, but the only one that |
| 33 | * has partial support is contrib/mips-msa/linux.c - a generic Linux |
| 34 | * implementation which reads /proc/cpufino. |
| 35 | */ |
| 36 | #ifndef PNG_MIPS_MSA_FILE |
| 37 | # ifdef __linux__ |
| 38 | # define PNG_MIPS_MSA_FILE "contrib/mips-msa/linux.c" |
| 39 | # endif |
| 40 | #endif |
| 41 | |
| 42 | #ifdef PNG_MIPS_MSA_FILE |
| 43 | |
| 44 | #include <signal.h> /* for sig_atomic_t */ |
| 45 | static int png_have_msa(png_structp png_ptr); |
| 46 | #include PNG_MIPS_MSA_FILE |
| 47 | |
| 48 | #else /* PNG_MIPS_MSA_FILE */ |
| 49 | # error "PNG_MIPS_MSA_FILE undefined: no support for run-time MIPS MSA checks" |
| 50 | #endif /* PNG_MIPS_MSA_FILE */ |
| 51 | #endif /* PNG_MIPS_MSA_CHECK_SUPPORTED */ |
| 52 | |
| 53 | #ifndef PNG_ALIGNED_MEMORY_SUPPORTED |
| 54 | # error "ALIGNED_MEMORY is required; set: -DPNG_ALIGNED_MEMORY_SUPPORTED" |
| 55 | #endif |
| 56 | |
| 57 | void |
| 58 | png_init_filter_functions_msa(png_structp pp, unsigned int bpp) |
| 59 | { |
| 60 | /* The switch statement is compiled in for MIPS_MSA_API, the call to |
| 61 | * png_have_msa is compiled in for MIPS_MSA_CHECK. If both are defined |
| 62 | * the check is only performed if the API has not set the MSA option on |
| 63 | * or off explicitly. In this case the check controls what happens. |
| 64 | */ |
| 65 | |
| 66 | #ifdef PNG_MIPS_MSA_API_SUPPORTED |
| 67 | switch ((pp->options >> PNG_MIPS_MSA) & 3) |
| 68 | { |
| 69 | case PNG_OPTION_UNSET: |
| 70 | /* Allow the run-time check to execute if it has been enabled - |
| 71 | * thus both API and CHECK can be turned on. If it isn't supported |
| 72 | * this case will fall through to the 'default' below, which just |
| 73 | * returns. |
| 74 | */ |
| 75 | #endif /* PNG_MIPS_MSA_API_SUPPORTED */ |
| 76 | #ifdef PNG_MIPS_MSA_CHECK_SUPPORTED |
| 77 | { |
| 78 | static volatile sig_atomic_t no_msa = -1; /* not checked */ |
| 79 | |
| 80 | if (no_msa < 0) |
| 81 | no_msa = !png_have_msa(pp); |
| 82 | |
| 83 | if (no_msa) |
| 84 | return; |
| 85 | } |
| 86 | #ifdef PNG_MIPS_MSA_API_SUPPORTED |
| 87 | break; |
| 88 | #endif |
| 89 | #endif /* PNG_MIPS_MSA_CHECK_SUPPORTED */ |
| 90 | |
| 91 | #ifdef PNG_MIPS_MSA_API_SUPPORTED |
| 92 | default: /* OFF or INVALID */ |
| 93 | return; |
| 94 | |
| 95 | case PNG_OPTION_ON: |
| 96 | /* Option turned on */ |
| 97 | break; |
| 98 | } |
| 99 | #endif |
| 100 | |
| 101 | /* IMPORTANT: any new external functions used here must be declared using |
| 102 | * PNG_INTERNAL_FUNCTION in ../pngpriv.h. This is required so that the |
| 103 | * 'prefix' option to configure works: |
| 104 | * |
| 105 | * ./configure --with-libpng-prefix=foobar_ |
| 106 | * |
| 107 | * Verify you have got this right by running the above command, doing a build |
| 108 | * and examining pngprefix.h; it must contain a #define for every external |
| 109 | * function you add. (Notice that this happens automatically for the |
| 110 | * initialization function.) |
| 111 | */ |
| 112 | pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_msa; |
| 113 | |
| 114 | if (bpp == 3) |
| 115 | { |
| 116 | pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub3_msa; |
| 117 | pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg3_msa; |
| 118 | pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = png_read_filter_row_paeth3_msa; |
| 119 | } |
| 120 | |
| 121 | else if (bpp == 4) |
| 122 | { |
| 123 | pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub4_msa; |
| 124 | pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg4_msa; |
| 125 | pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = png_read_filter_row_paeth4_msa; |
| 126 | } |
| 127 | } |
| 128 | #endif /* PNG_MIPS_MSA_OPT > 0 */ |
| 129 | #endif /* READ */ |