Skip to content
Snippets Groups Projects
Commit db266ed1 authored by Matthieu Schaller's avatar Matthieu Schaller
Browse files

Added functions to efficiently compute x^n for small positive integers n

parent e464d65c
No related branches found
No related tags found
1 merge request!1077Improved multipole acceptance criterion (MAC)
...@@ -52,7 +52,7 @@ include_HEADERS = space.h runner.h queue.h task.h lock.h cell.h part.h const.h \ ...@@ -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 \ 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 \ 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 \ 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_accept.h multipole_struct.h binomial.h \ multipole.h multipole_accept.h multipole_struct.h binomial.h integer_power.h \
star_formation_struct.h star_formation.h star_formation_iact.h \ star_formation_struct.h star_formation.h star_formation_iact.h \
star_formation_logger.h star_formation_logger_struct.h \ star_formation_logger.h star_formation_logger_struct.h \
pressure_floor.h pressure_floor_struct.h pressure_floor_iact.h \ pressure_floor.h pressure_floor_struct.h pressure_floor_iact.h \
......
/*******************************************************************************
* 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_INTEGER_POWER_H
#define SWIFT_INTEGER_POWER_H
/* Config parameters. */
#include "../config.h"
/* Local headers */
#include "error.h"
#include "inline.h"
/**
* @brief Computes the power of x to the n for a (small) positive integer n.
*
* Only valid for values 0 <= n <= 8.
*/
__attribute__((const)) INLINE static int integer_pow(const double x,
const unsigned int n) {
#ifdef SWIFT_DEBUG_CHECKS
assert(n <= 8);
#endif
switch (n) {
case 0:
return 1.;
case 1:
return x;
case 2:
return x * x;
case 3:
return x * x * x;
case 4: {
const double y = x * x;
return y * y;
}
case 5: {
const double y = x * x;
return x * y * y;
}
case 6: {
const double y = x * x;
return y * y * y;
}
case 7: {
const double y = x * x;
return x * y * y * y;
}
case 8: {
const double y = x * x;
const double z = y * y;
return z * z;
}
}
}
/**
* @brief Computes the power of x to the n for a (small) positive integer n.
*
* Only valid for values 0 <= n <= 8.
*/
__attribute__((const)) INLINE static int integer_powf(const float x,
const unsigned int n) {
#ifdef SWIFT_DEBUG_CHECKS
assert(n <= 8);
#endif
switch (n) {
case 0:
return 1.f;
case 1:
return x;
case 2:
return x * x;
case 3:
return x * x * x;
case 4: {
const float y = x * x;
return y * y;
}
case 5: {
const float y = x * x;
return x * y * y;
}
case 6: {
const float y = x * x;
return y * y * y;
}
case 7: {
const float y = x * x;
return x * y * y * y;
}
case 8: {
const float y = x * x;
const float z = y * y;
return z * z;
}
}
}
#endif /* SWIFT_INTEGER_POWER_H */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment