testExp.c 2.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*******************************************************************************
 * 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/>.
 *
 ******************************************************************************/

#include "../config.h"
#include "swift.h"

/* Standard includes */
#include <fenv.h>
#include <math.h>

/**
 * @brief Check that a and b are consistent (up to some relative error)
 *
 * @param a First value
 * @param b Second value
32 33
 * @param tol Relative tolerance
 * @param x Value for which we tested the function (for error messages)
34
 */
Matthieu Schaller's avatar
Matthieu Schaller committed
35
void check_value(double a, double b, const double tol, const double x) {
36
  if (fabs(a - b) / fabs(a + b) > tol)
Matthieu Schaller's avatar
Matthieu Schaller committed
37 38
    error("Values are inconsistent: %12.15e %12.15e rel=%e (for x=%e).", a, b,
          fabs(a - b) / fabs(a + b), x);
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
}

int main(int argc, char* argv[]) {

  /* Initialize CPU frequency, this also starts time. */
  unsigned long long cpufreq = 0;
  clocks_set_cpufreq(cpufreq);

/* Choke on FPEs */
#ifdef HAVE_FE_ENABLE_EXCEPT
  feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
#endif

  /* Get some randomness going */
  const int seed = time(NULL);
  message("Seed = %d", seed);
  srand(seed);

  /* Loop over some values */
  for (float x = 0.; x < 32.; x += 0.000001) {
Matthieu Schaller's avatar
Matthieu Schaller committed
59

60 61 62 63 64
    const double exact_p = exp(x);
    const double exact_n = exp(-x);
    const double swift_exp_p = optimized_expf(x);
    const double swift_exp_n = optimized_expf(-x);

65 66
    check_value(exact_p, swift_exp_p, 2.1e-6, x);
    check_value(exact_n, swift_exp_n, 2.1e-6, x);
67 68 69 70
  }

  return 0;
}