diff --git a/configure.ac b/configure.ac index eb56edb2526838843557f70a183f69e259757be1..e044a46b42e4b54cc39182882527735a558e6fde 100644 --- a/configure.ac +++ b/configure.ac @@ -1382,6 +1382,17 @@ if test "$ax_cv_c_compiler_vendor" = "clang"; then AC_CHECK_LIB([m],[__exp10f], [AC_DEFINE([HAVE___EXP10F],1,[The __exp10f function is present.])]) fi +# Check if we have native sincos and sincosf functions. If not failback to our +# implementations. On Apple/CLANG we have __sincos, so also check for that +# if the compiler is clang. +AC_CHECK_LIB([m],[sincos], [AC_DEFINE([HAVE_SINCOS],1,[The sincos function is present.])]) +AC_CHECK_LIB([m],[sincosf], [AC_DEFINE([HAVE_SINCOSF],1,[The sincosf function is present.])]) +if test "$ax_cv_c_compiler_vendor" = "clang"; then + AC_CHECK_LIB([m],[__sincos], [AC_DEFINE([HAVE___SINCOS],1,[The __sincos function is present.])]) + AC_CHECK_LIB([m],[__sincosf], [AC_DEFINE([HAVE___SINCOSF],1,[The __sincosf function is present.])]) +fi + + # Add warning flags by default, if these can be used. Option =error adds # -Werror to GCC, clang and Intel. Note do this last as compiler tests may # become errors, if that's an issue don't use CFLAGS for these, use an AC_SUBST(). diff --git a/src/Makefile.am b/src/Makefile.am index b7cbf531c3c25aca56c80fcde74a1f646bad2617..09b3ae7a073a003d49265ac0573c3e452af72716 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -52,7 +52,7 @@ include_HEADERS = space.h runner.h queue.h task.h lock.h cell.h part.h const.h \ chemistry.h chemistry_io.h chemistry_struct.h cosmology.h restart.h space_getsid.h utilities.h \ mesh_gravity.h cbrt.h exp10.h velociraptor_interface.h swift_velociraptor_part.h outputlist.h \ logger_io.h tracers_io.h tracers.h tracers_struct.h star_formation_io.h fof.h fof_struct.h fof_io.h \ - multipole.h multipole_struct.h \ + multipole.h multipole_struct.h sincos.h \ star_formation_struct.h star_formation.h star_formation_iact.h \ star_formation_logger.h star_formation_logger_struct.h \ pressure_floor.h pressure_floor_struct.h pressure_floor_iact.h \ diff --git a/src/sincos.h b/src/sincos.h new file mode 100644 index 0000000000000000000000000000000000000000..fbbee10fe54ce5ac48d1eaaca083cdc0eb51f52c --- /dev/null +++ b/src/sincos.h @@ -0,0 +1,84 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2020 Matthieu Schaller (schaller@strw.leidenuniv.nl) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ +#ifndef SWIFT_SINCOS_H +#define SWIFT_SINCOS_H + +/* Config parameters. */ +#include "../config.h" + +/* Some standard headers. */ +#include <math.h> + +/* Local headers. */ +#include "inline.h" + +#if !defined(HAVE_SINCOS) && !defined(HAVE___SINCOS) + +/** + * @brief Compute both the sin() and cos() of a number. + * + * This function is only used as a replacement for compilers that do + * not implement GNU extensions to the C language. + * + * @param x The input value. + * @param sin (return) The sine of x. + * @param cos (return) The cosine of x. + */ +__attribute__((always_inline)) INLINE static void sincos(const double x, + double *sin, + double *cos) { + + *sin = sin(x); + *cos = cos(x); +} + +#endif + +#if !defined(HAVE_SINCOSF) && !defined(HAVE___SINCOSF) + +/** + * @brief Compute both the sin() and cos() of a number. + * + * This function is only used as a replacement for compilers that do + * not implement GNU extensions to the C language. + * + * @param x The input value. + * @param sin (return) The sine of x. + * @param cos (return) The cosine of x. + */ +__attribute__((always_inline)) INLINE static void sincosf(const float x, + float *sin, + float *cos) { + + *sin = sinf(x); + *cos = cosf(x); +} + +#endif + +/* Use the __sincos and __sincosf versions if needed. */ +#if !defined(HAVE_SINCOS) && defined(HAVE___SINCOS) +#define sincos(x, s, c) __sincos(x, s, c) +#endif + +#if !defined(HAVE_SINCOSF) && defined(HAVE___SINCOSF) +#define sincosf(x, s, c) __sincosf(x, s, c) +#endif + +#endif /* SWIFT_SINCOS_H */