xref: /aosp_15_r20/external/libpng/loongarch/loongarch_lsx_init.c (revision a67afe4df73cf47866eedc69947994b8ff839aba)
1*a67afe4dSAndroid Build Coastguard Worker /* loongarch_lsx_init.c - LSX optimized filter functions
2*a67afe4dSAndroid Build Coastguard Worker  *
3*a67afe4dSAndroid Build Coastguard Worker  * Copyright (c) 2021 Loongson Technology Corporation Limited
4*a67afe4dSAndroid Build Coastguard Worker  * All rights reserved.
5*a67afe4dSAndroid Build Coastguard Worker  * Contributed by Jin Bo <[email protected]>
6*a67afe4dSAndroid Build Coastguard Worker  *
7*a67afe4dSAndroid Build Coastguard Worker  * This code is released under the libpng license.
8*a67afe4dSAndroid Build Coastguard Worker  * For conditions of distribution and use, see the disclaimer
9*a67afe4dSAndroid Build Coastguard Worker  * and license in png.h
10*a67afe4dSAndroid Build Coastguard Worker  */
11*a67afe4dSAndroid Build Coastguard Worker 
12*a67afe4dSAndroid Build Coastguard Worker #include "../pngpriv.h"
13*a67afe4dSAndroid Build Coastguard Worker 
14*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_SUPPORTED
15*a67afe4dSAndroid Build Coastguard Worker #if PNG_LOONGARCH_LSX_IMPLEMENTATION == 1
16*a67afe4dSAndroid Build Coastguard Worker 
17*a67afe4dSAndroid Build Coastguard Worker #include <sys/auxv.h>
18*a67afe4dSAndroid Build Coastguard Worker 
19*a67afe4dSAndroid Build Coastguard Worker #define LA_HWCAP_LSX    (1<<4)
png_has_lsx(void)20*a67afe4dSAndroid Build Coastguard Worker static int png_has_lsx(void)
21*a67afe4dSAndroid Build Coastguard Worker {
22*a67afe4dSAndroid Build Coastguard Worker     int flags = 0;
23*a67afe4dSAndroid Build Coastguard Worker     int flag  = (int)getauxval(AT_HWCAP);
24*a67afe4dSAndroid Build Coastguard Worker 
25*a67afe4dSAndroid Build Coastguard Worker     if (flag & LA_HWCAP_LSX)
26*a67afe4dSAndroid Build Coastguard Worker         return 1;
27*a67afe4dSAndroid Build Coastguard Worker 
28*a67afe4dSAndroid Build Coastguard Worker     return 0;
29*a67afe4dSAndroid Build Coastguard Worker }
30*a67afe4dSAndroid Build Coastguard Worker 
31*a67afe4dSAndroid Build Coastguard Worker void
png_init_filter_functions_lsx(png_structp pp,unsigned int bpp)32*a67afe4dSAndroid Build Coastguard Worker png_init_filter_functions_lsx(png_structp pp, unsigned int bpp)
33*a67afe4dSAndroid Build Coastguard Worker {
34*a67afe4dSAndroid Build Coastguard Worker    /* IMPORTANT: any new external functions used here must be declared using
35*a67afe4dSAndroid Build Coastguard Worker     * PNG_INTERNAL_FUNCTION in ../pngpriv.h.  This is required so that the
36*a67afe4dSAndroid Build Coastguard Worker     * 'prefix' option to configure works:
37*a67afe4dSAndroid Build Coastguard Worker     *
38*a67afe4dSAndroid Build Coastguard Worker     *    ./configure --with-libpng-prefix=foobar_
39*a67afe4dSAndroid Build Coastguard Worker     *
40*a67afe4dSAndroid Build Coastguard Worker     * Verify you have got this right by running the above command, doing a build
41*a67afe4dSAndroid Build Coastguard Worker     * and examining pngprefix.h; it must contain a #define for every external
42*a67afe4dSAndroid Build Coastguard Worker     * function you add.  (Notice that this happens automatically for the
43*a67afe4dSAndroid Build Coastguard Worker     * initialization function.)
44*a67afe4dSAndroid Build Coastguard Worker     */
45*a67afe4dSAndroid Build Coastguard Worker 
46*a67afe4dSAndroid Build Coastguard Worker    if (png_has_lsx())
47*a67afe4dSAndroid Build Coastguard Worker    {
48*a67afe4dSAndroid Build Coastguard Worker       pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_lsx;
49*a67afe4dSAndroid Build Coastguard Worker       if (bpp == 3)
50*a67afe4dSAndroid Build Coastguard Worker       {
51*a67afe4dSAndroid Build Coastguard Worker          pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub3_lsx;
52*a67afe4dSAndroid Build Coastguard Worker          pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg3_lsx;
53*a67afe4dSAndroid Build Coastguard Worker          pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = png_read_filter_row_paeth3_lsx;
54*a67afe4dSAndroid Build Coastguard Worker       }
55*a67afe4dSAndroid Build Coastguard Worker       else if (bpp == 4)
56*a67afe4dSAndroid Build Coastguard Worker       {
57*a67afe4dSAndroid Build Coastguard Worker          pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub4_lsx;
58*a67afe4dSAndroid Build Coastguard Worker          pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg4_lsx;
59*a67afe4dSAndroid Build Coastguard Worker          pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = png_read_filter_row_paeth4_lsx;
60*a67afe4dSAndroid Build Coastguard Worker       }
61*a67afe4dSAndroid Build Coastguard Worker    }
62*a67afe4dSAndroid Build Coastguard Worker }
63*a67afe4dSAndroid Build Coastguard Worker 
64*a67afe4dSAndroid Build Coastguard Worker #endif /* PNG_LOONGARCH_LSX_IMPLEMENTATION == 1 */
65*a67afe4dSAndroid Build Coastguard Worker #endif /* PNG_READ_SUPPORTED */
66