Skip to content

Commit

Permalink
MIPS support clean-up
Browse files Browse the repository at this point in the history
Everything has been moved into the "mips" directory - loongarch is now a
sub-directory - and there is just one MIPS initialization function which
makes the choices between MSA, MMI and LSX.  This resolves the issues
with the poorly defined precedence of the optimizations and removes the
wacky #if nesting.

Signed-off-by: John Bowler <[email protected]>
  • Loading branch information
jbowler committed Sep 13, 2024
1 parent 3da8b3b commit fcb1c19
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 36 deletions.
11 changes: 6 additions & 5 deletions mips/check.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
* For conditions of distribution and use, see the disclaimer
* and license in png.h
*/
#ifdef PNG_READ_SUPPORTED

#if defined(__mips_msa) && (__mips_isa_rev >= 5)
# ifndef PNG_MIPS_MSA_IMPLEMENTATION
# if defined(__mips_msa)
Expand Down Expand Up @@ -44,11 +42,14 @@
# define PNG_MIPS_MMI_IMPLEMENTATION 0
#endif /* __mips_loongson_mmi && _MIPS_SIM == _ABI64 */

#if PNG_MIPS_MSA_IMPLEMENTATION == 1 || PNG_MIPS_MMI_IMPLEMENTATION > 0
#include "loongarch/check.h"

#if PNG_MIPS_MSA_IMPLEMENTATION == 1 ||\
PNG_MIPS_MMI_IMPLEMENTATION > 0 ||\
PNG_MIPS_LSX_IMPLEMENTATION > 0
# define PNG_TARGET_CODE_IMPLEMENTATION "mips/mips_init.c"
/*PNG_TARGET_STORES_DATA*/
# define PNG_TARGET_IMPLEMENTS_FILTERS
/*PNG_TARGET_IMPLEMENTS_EXPAND_PALETTE*/
# define PNG_TARGET_ROW_ALIGNMENT 16
#endif /* MIPS MSA or MMI */
#endif /* READ */
#endif /* MIPS MSA or Loongson MMI or Loongarch SX */
File renamed without changes.
26 changes: 26 additions & 0 deletions mips/loongarch/check.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* check.h - LSX optimized filter functions
* copied from loongarch_lsx_init.c
*
* Copyright (c) 2021 Loongson Technology Corporation Limited
* All rights reserved.
* Contributed by Jin Bo <[email protected]>
*
* This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer
* and license in png.h
*
* Modified 2024 by John Bowler, changes
* Copyright (c) 2024 John Bowler, licensed under the libpng license:
*/
#if defined(__loongarch_sx) && defined(__GLIBC__)
/* The code uses the GNU glibc specific function getauxval so this is
* required:
*/
# if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16)
# define PNG_MIPS_LSX_IMPLEMENTATION 1
# else
# define PNG_MIPS_LSX_IMPLEMENTATION 0
# endif /* glibc >= 2.16 */
#else
# define PNG_MIPS_LSX_IMPLEMENTATION 0
#endif /* __loongarch_sx */
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,3 @@ png_read_filter_row_paeth4_lsx(png_row_infop row_info, png_bytep row,
n -= 4;
}
}

#endif /* PNG_LOONGARCH_LSX_IMPLEMENTATION == 1 (intrinsics) */
#endif /* PNG_READ_SUPPORTED */
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,20 @@
* This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer
* and license in png.h
*
* Modified 2024 by John Bowler, changes
* Copyright (c) 2024 John Bowler, licensed under the libpng license.
*/
#ifdef __loongarch_sx
#define png_target_impl "loongarch-sx"

#include <sys/auxv.h>

#include "filter_lsx_intrinsics.c"

#define LA_HWCAP_LSX (1<<4)
static int png_has_lsx(void)
{
int flags = 0;
int flag = (int)getauxval(AT_HWCAP);

if (flag & LA_HWCAP_LSX)
return 1;

return 0;
return (getauxval(AT_HWCAP) & LA_HWCAP_LSX) != 0U;
}

static void
static int
png_init_filter_functions_lsx(png_structp pp, unsigned int bpp)
{
if (png_has_lsx())
Expand All @@ -45,9 +38,7 @@ png_init_filter_functions_lsx(png_structp pp, unsigned int bpp)
pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg4_lsx;
pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = png_read_filter_row_paeth4_lsx;
}
return 1; /* using Loongarch SX extensions */
}
return 0; /* nothing done */
}

#define png_target_init_filter_functions png_init_filter_functions_lsx

#endif /* __loongarch_sx */
48 changes: 36 additions & 12 deletions mips/mips_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,44 @@
* This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer
* and license in png.h
*/
/* MIPS supports two optimizations: MMI and MSA. When both are available the
* appropriate optimization is chosen at runtime using the png_set_option
* settings.
*
* NOTE: see also the separate loongson code...
* Modified 2024 by John Bowler, changes
* Copyright (c) 2024 John Bowler, licensed under the libpng license
*/
/* MIPS supports three optimizations: MSA, MSI and LSX (Loongarch SX). When two
* or more are available the appropriate optimization is chosen at runtime using
* the png_set_option settings and/or runtime checks.
*/
#if PNG_MIPS_MSA_IMPLEMENATION == 1
# include "filter_msa_intrinsics.c"
#endif
#if PNG_MIPS_MMI_IMPLEMENTATION > 0
# include "filter_mmi_inline_assembly.c"
#endif
#if PNG_MIPS_LSX_IMPLEMENTATION > 0
# include "loongarch/loongarch_lsx_init.c"
#endif

static void
png_init_filter_functions_mips(png_structp pp, unsigned int bpp)
{
# if PNG_MIPS_MMI_IMPLEMENTATION > 0
# if PNG_MIPS_LSX_IMPLEMENTATION > 0
# define png_target_impl_lsx "+lsx"

/* TODO: put in an option check. */
if (png_init_filter_functions_lsx(pp, bpp))
return;
/* Else fall through to see if something else is available: */
# else
# define png_target_impl_lsx ""
# endif

# if PNG_MIPS_MMI_IMPLEMENTATION > 0
/* Check the option if MSA is also supported: */
# define png_target_impl_mmi "+mmi"
# if PNG_MIPS_MSA_IMPLEMENATION == 1
# define png_target_impl "mips-msa+msi"
/* NOTE: if this is false the code below will not be executed. */
if (((pp->options >> PNG_MIPS_USE_MMI) & 3) == PNG_OPTION_ON)
# else
# define png_target_impl "mips-mmi"
# endif
{
/* This is the MMI implementation: */
Expand All @@ -57,8 +70,12 @@ png_init_filter_functions_mips(png_structp pp, unsigned int bpp)
}
return;
}
# else /* !(PNG_MIPS_MMI_IMPLEMENTATION > 0) */
# define png_target_impl "mips-msa"
# else /* PNG_MIPS_MMI_IMPLEMENTATION == 0 */
# define png_target_impl_mmi ""
# endif

# if PNG_MIPS_MSA_IMPLEMENATION == 1
# define png_target_impl_msa "+msa"
pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_msa;

if (bpp == 3)
Expand All @@ -79,4 +96,11 @@ png_init_filter_functions_mips(png_structp pp, unsigned int bpp)
# endif /* PNG_MIPS_MSA_IMPLEMENTATION == 1 */
}

#define png_target_init_filter_functions_impl png_init_filter_functions_mips
#if defined(png_target_impl_msa) || defined(png_target_impl_msi) ||\
defined(png_target_impl_lsx)
# define png_target_impl "mips"\
png_target_impl_msa png_target_impl_mmi png_target_impl_lsx
# define png_target_init_filter_functions_impl png_init_filter_functions_mips
#else
# error HARDWARE: MIPS: no implementations defined
#endif

0 comments on commit fcb1c19

Please sign in to comment.