rsimd

Make SIMD instruction sets easier to use
git clone git://git.meso-star.fr/rsimd.git
Log | Files | Refs | README | LICENSE

commit 5e03975b44126a914c1839240f4b5b0f8ffc92dd
parent 871b4275beb53ab1e019aee28875aeec9bd3d25d
Author: vaplv <vaplv@free.fr>
Date:   Tue, 13 May 2025 09:19:17 +0200

Rewriting the Makefile

Delete the make.sh script. It implemented functions that, while
contributing to the clarity of the Makefile, made the construction
system less compact. Almost all the same features can be implemented
with a few lines of shell code, without any major impact on the clarity
of Makefile. Perhaps on the contrary, since everything is in one place.
In addition, the overall build system is therefore simpler. For example,
querying Makefile macros no longer requires tricks.

However, the features of the build system are not strictly the same.
Automatic detection of the SIMD instruction set to be used has been
removed. It is now always configured in the config.mk file (by default,
the SIMD_WIDTH macro is set to 256 bits, i.e. SSE and AVX are enabled).
This automatic detection made the Makefile more complex, requiring
intermediate targets in order to add the automatically-defined
SIMD_WIDTH macro to the Makefile. Removing this automatic detection
makes the Makefile simpler, clearer and faster. In other words, it sucks
far less less than its predecessor, whose complexity was driven by
a dispensable feature.

Add the LIBPREFIX and INCPREFIX macros to control the subdirectories
into which libraries and header files are copied. Systems may use
locations other than those proposed by default.

Remove the "distclean" Makefile target. In fact, it does the wrong
thing. distclean, in addition to a normal cleanup target, should also
delete files created by a dist target, i.e.  a target generating files
for distribution, such as a compressed archive. But it was used to
delete automatically generated dependency files. These files can simply
be removed by normal cleanup. What's more, as the project is designed to
be installed by compiling its sources directly, no distribution target
is implemented, so no distclean is required.

Diffstat:
MMakefile | 133+++++++++++++++++++++++++++++++++++++++++++------------------------------------
Mconfig.mk | 18++++++++----------
Dmake.sh | 117-------------------------------------------------------------------------------
3 files changed, 80 insertions(+), 188 deletions(-)

diff --git a/Makefile b/Makefile @@ -22,6 +22,9 @@ LIBNAME_STATIC = librsimd.a LIBNAME_SHARED = librsimd.so LIBNAME = $(LIBNAME_$(LIB_TYPE)) +default: library +all: library tests + ################################################################################ # Library building ################################################################################ @@ -31,9 +34,10 @@ SRC = $(SRC_SIMD$(SIMD_WIDTH)) OBJ = $(SRC:.c=.o) DEP = $(SRC:.c=.d) -build_library: +CFLAGS_LIB = -std=c99 $(CFLAGS_SO) $(INCS) -DRSIMD_SHARED_BUILD +LDFLAGS_LIB = $(LDFLAGS_SO) $(LIBS) -build_library__: .config $(DEP) +library: .config $(DEP) @$(MAKE) -f.simd -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) \ $$(if [ -n "$(LIBNAME)" ]; then\ echo "$(LIBNAME)";\ @@ -57,10 +61,10 @@ src/math.h: src/math.h.in config.mk cp src/math.h.in $@; \ fi -$(DEP) $(OBJ): config.mk +$(DEP) $(OBJ): config.mk src/math.h src/rsimd.h $(LIBNAME_SHARED): $(OBJ) - $(CC) -std=c99 $(CFLAGS_SO) $(DPDC_CFLAGS) -o $@ $(OBJ) $(LDFLAGS_SO) $(DPDC_LIBS) + $(CC) $(CFLAGS_LIB) -o $@ $(OBJ) $(LDFLAGS_LIB) $(LIBNAME_STATIC): librsimd.o $(AR) -rc $@ $? @@ -70,41 +74,17 @@ librsimd.o: $(OBJ) $(LD) -r $(OBJ) -o $@ $(OBJCOPY) $(OCPFLAGS) $@ -.config: make.sh config.mk - @if ! $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys; then \ - echo "rsys $(RSYS_VERSION) not found"; exit 1; fi - @if ! $(PKG_CONFIG) --atleast-version $(SLEEF_VERSION) sleef; then \ - echo "sleef $(SLEEF_VERSION) not found"; exit 1; fi - @echo "config done" > $@ - -.simd: make.sh config.mk - @$(SHELL) make.sh config_simd $(MAKE) > $@ +.config: config.mk + $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys + $(PKG_CONFIG) --atleast-version $(SLEEF_VERSION) sleef + echo "config done" > $@ .SUFFIXES: .c .d .o .c.d: - @$(CC) -std=c99 $(CFLAGS_SO) $(DPDC_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@ + @$(CC) $(CFLAGS_LIB) -MM -MT "$(@:.d=.o) $@" $< -MF $@ .c.o: - $(CC) -std=c99 $(CFLAGS_SO) $(DPDC_CFLAGS) -DRSIMD_SHARED_BUILD -c $< -o $@ - -################################################################################ -# Miscellaneous targets -################################################################################ -all: build_library build_tests - -clean__: clean_test - rm -f $(OBJ) $(TEST_OBJ) $(LIBNAME) - rm -f .config .simd .test librsimd.o rsimd.pc rsimd-local.pc - rm -f src/math.h src/rsimd.h - -distclean__: clean__ - rm -f $(DEP) $(TEST_DEP) .test - -lint: - shellcheck -o all make.sh - -build_library build_tests clean distclean install .test test uninstall: .simd - @$(MAKE) -f.simd -fMakefile $@__ + $(CC) $(CFLAGS_LIB) -c $< -o $@ ################################################################################ # Installation @@ -168,18 +148,28 @@ rsimd-local.pc: rsimd.pc.in -e 's#-I$${includedir}##g'\ rsimd.pc.in > $@ -install__: build_library pkg - @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib" $(LIBNAME) - @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib/pkgconfig" rsimd.pc - @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/include/rsimd" $(API) - @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/doc/rsimd" COPYING README.md - -uninstall__: - rm -f $(DESTDIR)$(PREFIX)/lib/$(LIBNAME) - rm -f $(DESTDIR)$(PREFIX)/lib/pkgconfig/rsimd.pc +install: library pkg + install() { mode="$$1"; prefix="$$2"; shift 2; \ + mkdir -p "$${prefix}"; \ + cp "$$@" "$${prefix}"; \ + chmod "$${mode}" "$$@"; \ + }; \ + install 755 "$(DESTDIR)$(LIBPREFIX)" $(LIBNAME); \ + install 644 "$(DESTDIR)$(LIBPREFIX)/pkgconfig" rsimd.pc; \ + install 644 "$(DESTDIR)$(INCPREFIX)/rsimd" $(API); \ + install 644 "$(DESTDIR)$(PREFIX)/share/doc/rsimd" COPYING README.md + +uninstall: + rm -f $(DESTDIR)$(LIBPREFIX)/$(LIBNAME) + rm -f $(DESTDIR)$(LIBPREFIX)/pkgconfig/rsimd.pc rm -f $(DESTDIR)$(PREFIX)/share/doc/rsimd/COPYING rm -f $(DESTDIR)$(PREFIX)/share/doc/rsimd/README.md - rm -f $$(echo $(API) | sed 's,src\/,$(DESTDIR)$(PREFIX)\/include\/rsimd\/,g') + rm -f $$(echo $(API) | sed 's,src\/,$(DESTDIR)$(INCPREFIX)\/rsimd\/,g') + +clean: clean_test + rm -f $(OBJ) $(DEP) $(LIBNAME) + rm -f .config librsimd.o rsimd.pc rsimd-local.pc + rm -f src/math.h src/rsimd.h ################################################################################ # Tests @@ -203,30 +193,33 @@ TEST_SIMD256=\ TEST_SRC = $(TEST_SIMD$(SIMD_WIDTH)) TEST_OBJ = $(TEST_SRC:.c=.o) TEST_DEP = $(TEST_SRC:.c=.d) +TEST_TGT = $(TEST_SRC:.c=.t) PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG) -RSIMD_CFLAGS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsimd-local.pc) -RSIMD_LIBS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsimd-local.pc) +INCS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsimd-local rsys) +LIBS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsimd-local rsys) -build_tests__: build_library $(TEST_DEP) .test .simd - @$(MAKE) -f.simd -fMakefile -f.test \ - $$(for i in $(TEST_DEP); do echo -f"$${i}"; done) test_bin +CFLAGS_TEST = -std=c89 $(CFLAGS_EXE) $(INCS_TEST) +LDFLAGS_TEST = $(LDFLAGS_EXE) $(LIBS_TEST) -test__: build_tests__ - @$(SHELL) make.sh run_test $(TEST_SRC) +tests: library $(TEST_DEP) $(TEST_TGT) + @$(MAKE) -fMakefile \ + $$(for i in $(TEST_DEP); do echo -f"$${i}"; done) \ + $$(for i in $(TEST_TGT); do echo -f"$${i}"; done) \ + test_list -.test__: Makefile make.sh - @$(SHELL) make.sh config_test $(TEST_SRC) > .test - -clean_test: - @$(SHELL) make.sh clean_test $(TEST_SRC) +$(TEST_TGT): + @{ \ + exe="$$(basename "$@" ".t")"; \ + printf '%s: %s\n' "$${exe}" $(@:.t=.o); \ + printf 'test_list: %s\n' "$${exe}"; \ + } > $@ $(TEST_DEP): config.mk rsimd-local.pc - @$(CC) -std=c89 $(CFLAGS_EXE) $(RSIMD_CFLAGS) $(RSYS_CFLAGS) \ - -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@ + @$(CC) $(CFLAGS_TEST) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@ $(TEST_OBJ): config.mk rsimd-local.pc - $(CC) -std=c89 $(CFLAGS_EXE) $(RSIMD_CFLAGS) $(RSYS_CFLAGS) -c $(@:.o=.c) -o $@ + $(CC) $(CFLAGS_TEST) -c $(@:.o=.c) -o $@ test_aosf33 \ test_aosf44 \ @@ -240,9 +233,27 @@ test_soa8f4 \ test_v4f \ test_v4i \ : config.mk rsimd-local.pc $(LIBNAME) - $(CC) -std=c89 $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) $(RSIMD_LIBS) $(RSYS_LIBS) + $(CC) -std=c89 $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST) test_math4 \ test_math8 \ : config.mk rsimd-local.pc $(LIBNAME) - $(CC) -std=c89 $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) $(RSIMD_LIBS) $(RSYS_LIBS) -lm + $(CC) -std=c89 $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST) -lm + +clean_test: + rm -f $(TEST_DEP) $(TEST_OBJ) $(TEST_TGT) + for i in $(TEST_SRC); do rm -f "$$(basename "$${i}" ".c")"; done + +test: tests + @err=0; \ + for i in $(TEST_SRC); do \ + test="$$(basename "$${i}" ".c")"; \ + printf '%s' "$${test}"; \ + if "./$${test}" > /dev/null 2>&1; then \ + printf '\n'; \ + else \ + printf ': error %s\n' "$$?"; \ + err=$$((err+1)); \ + fi \ + done; \ + [ "$${err}" -eq 0 ] diff --git a/config.mk b/config.mk @@ -7,9 +7,12 @@ LIB_TYPE = SHARED BUILD_TYPE = RELEASE #BUILD_TYPE = DEBUG -# If not set, SIMD WIDTH is retrieved from host CPU -#SIMD_WIDTH = 128 -#SIMD_WIDTH = 256 +# Instruction sets to be used +# SIMD_WIDTH = 128 +SIMD_WIDTH = 256 + +LIBPREFIX = $(PREFIX)/lib +INCPREFIX = $(PREFIX)/include ################################################################################ # Tools @@ -29,15 +32,10 @@ PCFLAGS_STATIC = --static PCFLAGS = $(PCFLAGS_$(LIB_TYPE)) RSYS_VERSION = 0.14 -RSYS_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys) -RSYS_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys) - SLEEF_VERSION = 3.6 -SLEEF_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags sleef) -SLEEF_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs sleef) -DPDC_CFLAGS = $(RSYS_CFLAGS) $(SLEEF_CFLAGS) -DPDC_LIBS = $(RSYS_LIBS) $(SLEEF_LIBS) +INCS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys sleef) +LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys sleef) ################################################################################ # Compilation options diff --git a/make.sh b/make.sh @@ -1,117 +0,0 @@ -#!/bin/sh - -# Copyright (C) 2014-2019, 2021, 2023 Vincent Forest (vaplv@free.fr) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -set -e - -################################################################################ -# Helper functions -################################################################################ -# Print the value of a variable in config.mk -showvar() -{ - var="$1" - shift 1 - - # To avoid messages from Make being displayed instead of the value of the - # queried variable, we redirect its output to /dev/null and open a new file - # descriptor to stdout to print the variable -<< EOF "$@" -f 3>&1 1>/dev/null 2>&1 - || kill -HUP $$ -.POSIX: -include config.mk -showvar: - @1>&3 echo \$(${var}) -EOF - exec 3<&- # Close file descriptor 3 -} - -check_cpuflag() -{ - sed -n "/^flags[[:blank:]]\{1,\}:/{p;q}" /proc/cpuinfo \ -| sed "s/.*[[:blank:]]\{1,\}\($1\)[[:blank:]]\{1,\}.*/\1/" -} - -################################################################################ -# Main functions -################################################################################ -config_simd() -{ - simd_width="$(showvar SIMD_WIDTH "$@")" - avx="$(check_cpuflag avx)" - if [ -z "${simd_width}" ] \ - && [ -n "${avx}" ]; then - simd_width=256 - fi - printf "SIMD_WIDTH = %s\n" "${simd_width}" -} - -config_test() -{ - for i in "$@"; do - test=$(basename "${i}" ".c") - test_list="${test_list} ${test}" - printf "%s: src/%s.o\n" "${test}" "${test}" - done - printf "test_bin: %s\n" "${test_list}" -} - -run_test() -{ - for i in "$@"; do - test=$(basename "${i}" ".c") - - printf "%s " "${test}" - if ./"${test}" > /dev/null 2>&1; then - printf "\033[1;32mOK\033[m\n" - else - printf "\033[1;31mError\033[m\n" - n=$((n+1)) - fi - done 2> /dev/null -} - -clean_test() -{ - for i in "$@"; do - rm -f "$(basename "${i}" ".c")" - done -} - -install() -{ - prefix=$1 - shift 1 - - for i in "$@"; do - # Remove the "src" directory and append the "prefix" - dst="${prefix}/${i#*/}" - - # Create the Install directory if required - dir="${dst%/*}" - if [ ! -d "${dir}" ]; then - mkdir -p "${dir}" - fi - - if cmp -s "${i}" "${dst}"; then - printf "Up to date %s\n" "${dst}" - else - printf "Installing %s\n" "${dst}" - cp "${i}" "${dst}" - fi - done -} - -"$@"