/* $Id: fma-tests.c 17487 2007-05-21 11:03:03Z lefevre $
 *
 * Miscellaneous fma() test.
 */

#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <math.h>

#define WRONG(S,X,Y,Z,F,C)                                        \
  fprintf (stderr, "ERROR in test '" S "'!\nfma (%a, %a, %a)\n"   \
           "returned %a instead of %a.\n", X, Y, Z, F, C)

/* Modified Nelson H. F. Beebe's fma() test from stds-754 list, 2006-06-09 */
static int beebe (void)
{
  volatile double eps, e2, f, x, z;

  eps = DBL_EPSILON;
  e2 = eps * eps;
  x = 1.0 + eps;
  z = -1.0 - 2.0 * eps;
  f = fma (x, x, z);
  if (f != e2)
    {
      WRONG ("beebe", x, x, z, f, e2);
      return 1;
    }
  return 0;
}

static int overflowed_mul (void)
{
  volatile double x, f;

  x = DBL_MAX;
  f = fma (x, 2.0, -x);
  if (f != x)
    {
      WRONG ("overflowed_mul", x, 2.0, -x, f, x);
      return 1;
    }
  return 0;
}

int main (void)
{
  int err = 0;

  err += beebe ();
  err += overflowed_mul ();
  return err;
}
