Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add static dispatch macros #318

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ set(TARGET_MKRENAME_GNUABI "mkrename_gnuabi")
set(TARGET_MKMASKED_GNUABI "mkmasked_gnuabi")
# Generates the helper executable file mkdisp needed to write the sleef header
set(TARGET_MKDISP "mkdisp")
# Generates the helper executable file mkstatic for generating the static dispatch macros
set(TARGET_MKSTATIC "mkstatic")
set(TARGET_MKALIAS "mkalias")
# Generates static library common
# Defined in src/common/CMakeLists.txt via command add_library
Expand Down
23 changes: 22 additions & 1 deletion sleef-config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,30 @@

// FEATURE DETECTION ***********************************************************

#cmakedefine COMPILER_SUPPORTS_BUILTIN_MATH
#cmakedefine COMPILER_SUPPORTS_LONG_DOUBLE
#cmakedefine COMPILER_SUPPORTS_FLOAT128
#cmakedefine COMPILER_SUPPORTS_OPENMP
#cmakedefine COMPILER_SUPPORTS_SYS_GETRANDOM
#cmakedefine COMPILER_SUPPORTS_WEAK_ALIASES

#cmakedefine COMPILER_SUPPORTS_ADVSIMD
#cmakedefine COMPILER_SUPPORTS_ADVSIMDNOFMA
#cmakedefine COMPILER_SUPPORTS_AVX
#cmakedefine COMPILER_SUPPORTS_AVX2
#cmakedefine COMPILER_SUPPORTS_AVX2128
#cmakedefine COMPILER_SUPPORTS_AVX512F
#cmakedefine COMPILER_SUPPORTS_AVX512FNOFMA
#cmakedefine COMPILER_SUPPORTS_FLOAT128
#cmakedefine COMPILER_SUPPORTS_FMA4
#cmakedefine COMPILER_SUPPORTS_NEON32
#cmakedefine COMPILER_SUPPORTS_NEON32VFPV4
#cmakedefine COMPILER_SUPPORTS_PURECFMA_SCALAR
#cmakedefine COMPILER_SUPPORTS_PUREC_SCALAR
#cmakedefine COMPILER_SUPPORTS_SSE2
#cmakedefine COMPILER_SUPPORTS_SSE4
#cmakedefine COMPILER_SUPPORTS_SVE
#cmakedefine COMPILER_SUPPORTS_SVENOFMA
#cmakedefine COMPILER_SUPPORTS_VSX
#cmakedefine COMPILER_SUPPORTS_VSXNOFMA

#endif // CONFIG_H
10 changes: 10 additions & 0 deletions src/libm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ foreach(SIMD ${SLEEF_HEADER_LIST})
list(APPEND SLEEF_HEADER_COMMANDS COMMAND echo Generating sleef.h: ${TARGET_MKRENAME} ${HEADER_PARAMS_${SIMD}})
list(APPEND SLEEF_HEADER_COMMANDS COMMAND $<TARGET_FILE:${TARGET_MKRENAME}> ${HEADER_PARAMS_${SIMD}} >> ${SLEEF_INCLUDE_HEADER})
endforeach()
list(APPEND SLEEF_HEADER_COMMANDS COMMAND echo Generating sleef.h: ${TARGET_MKSTATIC})
list(APPEND SLEEF_HEADER_COMMANDS COMMAND $<TARGET_FILE:${TARGET_MKSTATIC}> >> ${SLEEF_INCLUDE_HEADER})

if((MSVC OR MINGW AND WIN32) OR SLEEF_CLANG_ON_WINDOWS)
string(REPLACE "/" "\\" sleef_footer_input_file "${SLEEF_ORG_FOOTER}")
Expand All @@ -35,6 +37,7 @@ add_custom_command(OUTPUT ${SLEEF_INCLUDE_HEADER}
${SLEEF_ORG_HEADER}
${SLEEF_ORG_FOOTER}
${TARGET_MKRENAME}
${TARGET_MKSTATIC}
)

# --------------------------------------------------------------------
Expand Down Expand Up @@ -155,6 +158,13 @@ set_target_properties(
PROPERTIES C_STANDARD 99
)

# --------------------------------------------------------------------
# TARGET_MKDISP
# --------------------------------------------------------------------
# Helper executable: dispatcher for the vector extensions
add_host_executable(${TARGET_MKSTATIC} mkstatic.c)
target_include_directories(${TARGET_MKSTATIC} PRIVATE ${PROJECT_BINARY_DIR}/include)

# --------------------------------------------------------------------
# TARGET_LIBSLEEF
# --------------------------------------------------------------------
Expand Down
150 changes: 150 additions & 0 deletions src/libm/mkstatic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright Naoki Shibata and contributors 2010 - 2020.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>

#include "funcproto.h"
#include <sleef-config.h>

static const size_t vecSizes[] = {
0, 16, 32, 64,
};

static const struct variantInfo {
size_t vecSize;
const char* ppTest;
const char* suffix;
} variantList[] = {

/* Arm */

#if defined(COMPILER_SUPPORTS_SVE)
{ 0, "defined(__ARM_SVE)", "sve" },
#endif
#if defined(COMPILER_SUPPORTS_SVENOFMA)
{ 0, "defined(__ARM_SVE)", "svenofma" },
#endif

#if defined(COMPILER_SUPPORTS_ADVSIMD)
/* Not sure what the feature check for FMA should look like */
{ 16, "defined(__ARM_NEON) && defined(__aarch64__)", "advsimd" },
#endif
#if defined(COMPILER_SUPPORTS_ADVSIMDNOFMA)
{ 16, "defined(__ARM_NEON) && defined(__aarch64__)", "advsimdnofma" },
#endif

#if defined(COMPILER_SUPPORTS_NEON32VFPV4)
{ 16, "defined(__ARM_NEON) && (__ARM_NEON_FP >= 4)", "neonvfpv4" },
#endif
#if defined(COMPILER_SUPPORTS_NEON32VFPV4)
{ 16, "defined(__ARM_NEON)", "neon" },
#endif

/* POWER */

#if defined(COMPILER_SUPPORTS_VSX)
/* Not sure what the feature check for FMA should look like */
{ 16, "defined(__VSX__) && defined(__ARCH_PWR8) && defined(__LITTLE_ENDIAN__)", "vsx" },
#endif
#if defined(COMPILER_SUPPORTS_VSXNOFMA)
{ 16, "defined(__VSX__) && defined(__ARCH_PWR8) && defined(__LITTLE_ENDIAN__)", "vsxnofma" },
#endif

/* x86 */

#if defined(COMPILER_SUPPORTS_AVX512F)
{ 64, "defined(__AVX512F__) && defined(__FMA4__)", "avx512f" },
#endif
#if defined(COMPILER_SUPPORTS_AVX512FNOFMA)
{ 64, "defined(__AVX512F__)", "avx512fnofma" },
#endif

#if defined(COMPILER_SUPPORTS_FMA4)
{ 32, "defined(__FMA4__)", "fma4" },
#endif
#if defined(COMPILER_SUPPORTS_AVX2)
{ 32, "defined(__AVX2__)", "avx2" },
#endif
#if defined(COMPILER_SUPPORTS_AVX)
{ 32, "defined(__AVX__)", "avx" },
#endif

#if defined(COMPILER_SUPPORTS_AVX2128)
{ 16, "defined(__AVX2__)", "avx2128" },
#endif
#if defined(COMPILER_SUPPORTS_SSE4)
{ 16, "defined(__SSE4_1__)", "sse4" },
#endif
#if defined(COMPILER_SUPPORTS_SSE2)
{ 16, "defined(__SSE2__)", "sse2" },
#endif

{ 0, NULL, NULL }
};

void
write_static_dispatch(FILE* fp, size_t vecSize) {
int first = 1;
for (const struct variantInfo* vi = variantList ; vi->suffix != NULL ; vi++) {
if (vi->vecSize != vecSize)
continue;

if (first) {
first = 0;
fprintf(fp, "#if %s\n", vi->ppTest);
} else {
fprintf(fp, "#elif %s\n", vi->ppTest);
}

for (funcSpec* func = funcList ; func->name != NULL ; func++) {
if (func->funcType >= 7) {
continue;
}

if (vecSize != 0) {
if (func->ulp > 0) {
fprintf(fp, "# define Sleef_%sf%zu_u%02dstatic Sleef_%sf%zu_u%02d%s\n",
func->name, vecSize / sizeof(float), func->ulp,
func->name, vecSize / sizeof(float), func->ulp, vi->suffix);
} else {
fprintf(fp, "# define Sleef_%sf%zu_static Sleef_%sf%zu_%s\n",
func->name, vecSize / sizeof(float),
func->name, vecSize / sizeof(float), vi->suffix);
}
} else {
if (func->ulp > 0) {
fprintf(fp, "# define Sleef_%sfx_u%02dstatic Sleef_%sfx_u%02d%s\n",
func->name, func->ulp,
func->name, func->ulp, vi->suffix);
} else {
fprintf(fp, "# define Sleef_%sfx_static Sleef_%sfx_%s\n",
func->name,
func->name, vi->suffix);
}
}
}
}

if (!first) {
fputs("#endif\n\n", fp);
}
}

int
main(void) {
fprintf(stdout, "\n/* Begin static dispatch macros */\n\n");

for (size_t i = 0 ; i < (sizeof(vecSizes) / sizeof(vecSizes[0])) ; i++) {
write_static_dispatch(stdout, vecSizes[i]);
}

fprintf(stdout, "/* End static distpatch macros */\n\n");

return 0;
}