Skip to content

Commit b91226b

Browse files
iii-ifneddy
authored andcommitted
s390x: vectorize crc32
Use vector extensions when compiling for s390x and binutils knows about them. At runtime, check whether kernel supports vector extensions (it has to be not just the CPU, but also the kernel) and choose between the regular and the vectorized implementations.
1 parent 5a82f71 commit b91226b

File tree

9 files changed

+493
-75
lines changed

9 files changed

+493
-75
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ if(NOT ZLIB_CONF_WRITTEN)
7676
mark_as_advanced(ZLIB_CONF_WRITTEN)
7777
endif(NOT ZLIB_CONF_WRITTEN)
7878

79+
#
80+
# Add contrib code
81+
#
82+
add_subdirectory(contrib/s390x)
7983
#
8084
# Check to see if we have large file support
8185
#
@@ -201,6 +205,7 @@ if(ZLIB_BUILD_SHARED)
201205
UNIX
202206
AND NOT APPLE
203207
AND NOT (CMAKE_SYSTEM_NAME STREQUAL AIX))
208+
target_link_libraries(zlib PRIVATE $<TARGET_NAME_IF_EXISTS:zlib_crc32_vx>)
204209
endif(ZLIB_BUILD_SHARED)
205210

206211
if(ZLIB_BUILD_STATIC)
@@ -223,6 +228,7 @@ if(ZLIB_BUILD_STATIC)
223228
set_target_properties(
224229
zlibstatic PROPERTIES EXPORT_NAME ZLIBSTATIC OUTPUT_NAME
225230
z${zlib_static_suffix})
231+
target_link_libraries(zlibstatic PRIVATE $<TARGET_NAME_IF_EXISTS:zlib_crc32_vx>)
226232
endif(ZLIB_BUILD_STATIC)
227233

228234
if(ZLIB_INSTALL)

Makefile.in

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ LDFLAGS=
2727
TEST_LIBS=-L. libz.a
2828
LDSHARED=$(CC)
2929
CPP=$(CC) -E
30+
VGFMAFLAG=
3031

3132
STATICLIB=libz.a
3233
SHAREDLIB=libz.so
@@ -164,6 +165,12 @@ adler32.o: $(SRCDIR)adler32.c
164165
crc32.o: $(SRCDIR)crc32.c
165166
$(CC) $(CFLAGS) $(ZINC) -c -o $@ $(SRCDIR)crc32.c
166167

168+
crc32-vx.o: $(SRCDIR)contrib/s390x/crc32-vx.c
169+
$(CC) $(CFLAGS) $(VGFMAFLAG) $(ZINC) -c -o $@ $(SRCDIR)contrib/s390x/crc32-vx.c
170+
171+
s390x-functable.o: $(SRCDIR)contrib/s390x/s390x-functable.c
172+
$(CC) $(CFLAGS) $(VGFMAFLAG) $(ZINC) -c -o $@ $(SRCDIR)contrib/s390x/s390x-functable.c
173+
167174
deflate.o: $(SRCDIR)deflate.c
168175
$(CC) $(CFLAGS) $(ZINC) -c -o $@ $(SRCDIR)deflate.c
169176

@@ -214,6 +221,16 @@ crc32.lo: $(SRCDIR)crc32.c
214221
$(CC) $(SFLAGS) $(ZINC) -DPIC -c -o objs/crc32.o $(SRCDIR)crc32.c
215222
-@mv objs/crc32.o $@
216223

224+
crc32-vx.lo: $(SRCDIR)contrib/s390x/crc32-vx.c
225+
-@mkdir objs 2>/dev/null || test -d objs
226+
$(CC) $(SFLAGS) $(VGFMAFLAG) $(ZINC) -DPIC -c -o objs/crc32-vx.o $(SRCDIR)contrib/s390x/crc32-vx.c
227+
-@mv objs/crc32-vx.o $@
228+
229+
s390x-functable.lo: $(SRCDIR)contrib/s390x/s390x-functable.c
230+
-@mkdir objs 2>/dev/null || test -d objs
231+
$(CC) $(SFLAGS) $(VGFMAFLAG) $(ZINC) -DPIC -c -o objs/s390x-functable.o $(SRCDIR)contrib/s390x/s390x-functable.c
232+
-@mv objs/s390x-functable.o $@
233+
217234
deflate.lo: $(SRCDIR)deflate.c
218235
-@mkdir objs 2>/dev/null || test -d objs
219236
$(CC) $(SFLAGS) $(ZINC) -DPIC -c -o objs/deflate.o $(SRCDIR)deflate.c

configure

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,55 @@ EOF
870870
fi
871871
fi
872872

873+
# check for ibm s390x build
874+
HAVE_S390X=0
875+
876+
# preset the compiler specific flags
877+
if test $clang -eq 1; then
878+
VGFMAFLAG=-fzvector
879+
else
880+
VGFMAFLAG=-mzarch
881+
fi
882+
883+
cat > $test.c <<EOF
884+
#ifndef __s390x__
885+
#error
886+
#endif
887+
#include <vecintrin.h>
888+
int main(void) {
889+
unsigned long long a __attribute__((vector_size(16))) = { 0 };
890+
unsigned long long b __attribute__((vector_size(16))) = { 0 };
891+
unsigned char c __attribute__((vector_size(16))) = { 0 };
892+
c = vec_gfmsum_accum_128(a, b, c);
893+
return c[0];
894+
}
895+
EOF
896+
897+
# cflags already contains a valid march
898+
if try $CC -c $CFLAGS $VGFMAFLAG $test.c; then
899+
echo "Checking for s390x build ... Yes." | tee -a configure.log
900+
HAVE_S390X=1
901+
# or set march for our compile units
902+
elif try $CC -c $CFLAGS $VGFMAFLAG -march=z13 $test.c; then
903+
echo "Checking for s390x build (march=z13) ... Yes." | tee -a configure.log
904+
HAVE_S390X=1
905+
VGFMAFLAG="$VGFMAFLAG -march=z13"
906+
# else we are not on s390x
907+
else
908+
echo "Checking for s390x build ... No." | tee -a configure.log
909+
fi
910+
911+
# prepare compiling for s390x
912+
if test $HAVE_S390X -eq 1; then
913+
CFLAGS="$CFLAGS -DHAVE_S390X_VX"
914+
SFLAGS="$SFLAGS -DHAVE_S390X_VX"
915+
OBJC="$OBJC crc32-vx.o s390x-functable.o"
916+
PIC_OBJC="$PIC_OBJC crc32-vx.lo s390x-functable.lo"
917+
else
918+
# this is not a s390x build
919+
VGFMAFLAG=""
920+
fi
921+
873922
# show the results in the log
874923
echo >> configure.log
875924
echo ALL = $ALL >> configure.log
@@ -901,6 +950,8 @@ echo mandir = $mandir >> configure.log
901950
echo prefix = $prefix >> configure.log
902951
echo sharedlibdir = $sharedlibdir >> configure.log
903952
echo uname = $uname >> configure.log
953+
echo HAVE_S390X = $HAVE_S390X >> configure.log
954+
echo VGFMAFLAG = $VGFMAFLAG >> configure.log
904955

905956
# update Makefile with the configure results
906957
sed < ${SRCDIR}Makefile.in "
@@ -912,6 +963,7 @@ sed < ${SRCDIR}Makefile.in "
912963
/^LDFLAGS *=/s#=.*#=$LDFLAGS#
913964
/^LDSHARED *=/s#=.*#=$LDSHARED#
914965
/^CPP *=/s#=.*#=$CPP#
966+
/^VGFMAFLAG *=/s#=.*#=$VGFMAFLAG#
915967
/^STATICLIB *=/s#=.*#=$STATICLIB#
916968
/^SHAREDLIB *=/s#=.*#=$SHAREDLIB#
917969
/^SHAREDLIBV *=/s#=.*#=$SHAREDLIBV#

contrib/functable/functable.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef Z_FUNCTABLE_H__
2+
#define Z_FUNCTABLE_H__
3+
4+
#include "../../zutil.h"
5+
#include "../../zonce.h"
6+
7+
struct zfunctable_s {
8+
unsigned long (*crc32_z)(unsigned long crc, const unsigned char FAR *buf,
9+
z_size_t len);
10+
/* int (*deflate)(z_streamp strm, int flush); */
11+
/* int (*inflate)(z_streamp strm, int flush); */
12+
};
13+
14+
extern struct zfunctable_s ZLIB_INTERNAL arch_functable;
15+
extern once_t ZLIB_INTERNAL arch_functable_init_done;
16+
/* to be implemented by architecture specific code */
17+
void ZLIB_INTERNAL arch_functable_init(void);
18+
19+
#endif

contrib/s390x/CMakeLists.txt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
option(ZLIB_CRC32VX "Enable building S390-CRC32VX implementation" ON)
2+
3+
#
4+
# Check for IBM S390X extensions
5+
#
6+
if(ZLIB_CRC32VX)
7+
8+
# preset the compiler specific flags
9+
if (CMAKE_C_COMPILER_ID STREQUAL "Clang")
10+
set(VGFMAFLAG "-fzvector")
11+
else()
12+
set(VGFMAFLAG "-mzarch")
13+
endif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
14+
15+
set(S390X_VX_TEST
16+
"#ifndef __s390x__ \n\
17+
#error \n\
18+
#endif \n\
19+
#include <vecintrin.h> \n\
20+
int main(void) { \
21+
unsigned long long a __attribute__((vector_size(16))) = { 0 }; \
22+
unsigned long long b __attribute__((vector_size(16))) = { 0 }; \
23+
unsigned char c __attribute__((vector_size(16))) = { 0 }; \
24+
c = vec_gfmsum_accum_128(a, b, c); \
25+
return c[0]; \
26+
}")
27+
28+
# cflags already contains a valid march
29+
set(CMAKE_REQUIRED_FLAGS "${VGFMAFLAG}")
30+
check_c_source_compiles("${S390X_VX_TEST}" HAS_S390X_VX_SUPPORT)
31+
unset(CMAKE_REQUIRED_FLAGS)
32+
33+
# or set march for our compile units
34+
if(NOT HAS_S390X_VX_SUPPORT)
35+
set(CMAKE_REQUIRED_FLAGS "${VGFMAFLAG} -march=z13")
36+
check_c_source_compiles("${S390X_VX_TEST}" HAS_Z13_S390X_VX_SUPPORT)
37+
unset(CMAKE_REQUIRED_FLAGS )
38+
list(APPEND VGFMAFLAG "-march=z13")
39+
endif(NOT HAS_S390X_VX_SUPPORT)
40+
41+
# prepare compiling for s390x
42+
if(HAS_S390X_VX_SUPPORT OR HAS_Z13_S390X_VX_SUPPORT)
43+
add_library(
44+
zlib_crc32_vx OBJECT
45+
s390x-functable.c
46+
crc32-vx.c
47+
../functable/functable.h)
48+
set_source_files_properties(
49+
crc32-vx.c
50+
PROPERTIES COMPILE_OPTIONS "${VGFMAFLAG}")
51+
target_compile_definitions(
52+
zlib_crc32_vx PUBLIC HAVE_S390X_VX=1)
53+
endif(HAS_S390X_VX_SUPPORT OR HAS_Z13_S390X_VX_SUPPORT)
54+
endif(ZLIB_CRC32VX)

0 commit comments

Comments
 (0)