Skip to content

Commit

Permalink
Add loongarch support and LSX SIMD optimizations
Browse files Browse the repository at this point in the history
Enable LSX by default:
    ./configure && make

Disable LSX:
    ./configure --enable-loongarch-lsx=no && make

Signed-off-by: Cosmin Truta <[email protected]>
  • Loading branch information
jinboson authored and ctruta committed Jan 17, 2024
1 parent 2a4f0f5 commit 6b0d1bd
Show file tree
Hide file tree
Showing 5 changed files with 589 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ libpng@PNGLIB_MAJOR@@PNGLIB_MINOR@_la_SOURCES += powerpc/powerpc_init.c\
powerpc/filter_vsx_intrinsics.c
endif

if PNG_LOONGARCH_LSX
noinst_LTLIBRARIES= libpng@PNGLIB_MAJOR@@[email protected]
libpng@PNGLIB_MAJOR@@PNGLIB_MINOR@lsx_la_SOURCES = loongarch/loongarch_lsx_init.c\
loongarch/filter_lsx_intrinsics.c
libpng@PNGLIB_MAJOR@@PNGLIB_MINOR@lsx_la_CFLAGS = -mlsx
libpng@PNGLIB_MAJOR@@PNGLIB_MINOR@_la_LIBADD = libpng@PNGLIB_MAJOR@@[email protected]
# libpng@PNGLIB_MAJOR@@PNGLIB_MINOR@_la_DEPENDENCIES = libpng@PNGLIB_MAJOR@@[email protected]
endif

nodist_libpng@PNGLIB_MAJOR@@PNGLIB_MINOR@_la_SOURCES = pnglibconf.h

libpng@PNGLIB_MAJOR@@PNGLIB_MINOR@_la_LDFLAGS = -no-undefined -export-dynamic \
Expand All @@ -147,6 +156,10 @@ else
libpng@PNGLIB_MAJOR@@PNGLIB_MINOR@_la_DEPENDENCIES = libpng.sym
endif

if PNG_LOONGARCH_LSX
libpng@PNGLIB_MAJOR@@PNGLIB_MINOR@_la_DEPENDENCIES += libpng@PNGLIB_MAJOR@@[email protected]
endif

#distribute headers in /usr/include/libpng/*
pkgincludedir= $(includedir)/$(PNGLIB_BASENAME)
pkginclude_HEADERS= png.h pngconf.h
Expand Down
71 changes: 71 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ AC_ARG_ENABLE([hardware-optimizations],
enable_intel_sse=no
AC_DEFINE([PNG_INTEL_SSE_OPT], [0],
[Disable INTEL_SSE optimizations])
enable_loongarch_lsx=no
AC_DEFINE([PNG_LOONGARCH_LSX_OPT], [0],
[Disable LOONGARCH_LSX optimizations])
;;
*)
# allow enabling hardware optimization on any system:
Expand All @@ -358,6 +361,11 @@ AC_ARG_ENABLE([hardware-optimizations],
AC_DEFINE([PNG_POWERPC_VSX_OPT], [2],
[Enable POWERPC VSX optimizations])
;;
loongarch*)
enable_loongarch_lsx=yes
AC_DEFINE([PNG_LOONGARCH_LSX_OPT], [1],
[Enable LOONGARCH_LSX optimizations])
;;
esac
;;
esac])
Expand Down Expand Up @@ -535,6 +543,69 @@ AM_CONDITIONAL([PNG_POWERPC_VSX],
powerpc*|ppc64*) : ;;
esac])

# LOONGARCH
# ===
#
# LOONGARCH LSX (SIMD) support

if test "$LSX_CFLAGS" = ''; then
LSX_CFLAGS="-mlsx"
fi

compiler_support_loongarch_lsx=no
AC_MSG_CHECKING(whether to use loongarch LSX intrinsics)
save_CFLAGS=$CFLAGS
CFLAGS="$CFLAGS $LSX_CFLAGS"
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
#include<lsxintrin.h>
int main(){
__m128i a, b, c;
a = __lsx_vadd_w(b, c);
return 0;
}]])],compiler_support_loongarch_lsx=yes)
CFLAGS=$save_CFLAGS
AC_MSG_RESULT($compiler_support_loongarch_lsx)

AC_ARG_ENABLE([loongarch-lsx],
AS_HELP_STRING([[[--enable-loongarch-lsx]]],
[Enable LOONGARCH LSX optimizations: =no/off, yes/on:]
[no/off: disable the optimizations;]
[yes/on: turn on unconditionally.]
[If not specified: determined by the compiler.]),
[case "$enableval" in
no|off)
# disable the default enabling on __loongarch_simd systems:
AC_DEFINE([PNG_LOONGARCH_LSX_OPT], [0],
[Disable LOONGARCH LSX optimizations])
# Prevent inclusion of the assembler files below:
enable_loongarch_lsx=no;;
yes|on)
AC_DEFINE([PNG_LOONGARCH_LSX_OPT], [1],
[Enable LOONGARCH LSX optimizations])
;;
*)
AC_MSG_ERROR([--enable-loongarch-lsx=${enable_loongarch_lsx}: invalid value])
esac])

if test "$enable_loongarch_lsx" != 'no'; then
if test $compiler_support_loongarch_lsx = yes; then
AC_DEFINE([PNG_LOONGARCH_LSX_OPT], [1], [Enable LOONGARCH LSX optimizations])
else
AC_MSG_WARN([Compiler does not support loongarch LSX.])
fi
fi

# Add LOONGARCH specific files to all builds where the host_cpu is loongarch ('loongarch*') or
# where LOONGARCH optimizations were explicitly requested (this allows a fallback if a
# future host CPU does not match 'loongarch*')

AM_CONDITIONAL([PNG_LOONGARCH_LSX],
[test "$enable_loongarch_lsx" != 'no' && test $compiler_support_loongarch_lsx = yes &&
case "$host_cpu" in
loongarch*) :;;
*) test "$enable_loongarch_lsx" != '';;
esac])

AC_MSG_NOTICE([[Extra options for compiler: $PNG_COPTS]])

# Config files, substituting as above
Expand Down
Loading

0 comments on commit 6b0d1bd

Please sign in to comment.