OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libm/] [test/] [math2.c] - Diff between revs 1010 and 1765

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 1010 Rev 1765
 
 
#include "test.h"
#include "test.h"
#include <errno.h>
#include <errno.h>
 
 
 
 
int
int
_DEFUN_VOID(randi)
_DEFUN_VOID(randi)
{
{
  static int next;
  static int next;
  next = (next * 1103515245) + 12345;
  next = (next * 1103515245) + 12345;
  return ((next >> 16) & 0xffff);
  return ((next >> 16) & 0xffff);
}
}
 
 
double _DEFUN_VOID(randx)
double _DEFUN_VOID(randx)
{
{
  double res;
  double res;
 
 
  do
  do
  {
  {
    union {
    union {
        short parts[4];
        short parts[4];
        double res;
        double res;
      } u;
      } u;
 
 
    u.parts[0] = randi();
    u.parts[0] = randi();
    u.parts[1] = randi();
    u.parts[1] = randi();
    u.parts[2] = randi();
    u.parts[2] = randi();
    u.parts[3] = randi();
    u.parts[3] = randi();
    res = u.res;
    res = u.res;
 
 
  } while (!finite(res));
  } while (!finite(res));
 
 
  return res ;
  return res ;
}
}
 
 
/* Return a random double, but bias for numbers closer to 0 */
/* Return a random double, but bias for numbers closer to 0 */
double _DEFUN_VOID(randy)
double _DEFUN_VOID(randy)
{
{
  int pow;
  int pow;
  double r= randx();
  double r= randx();
  r = frexp(r, &pow);
  r = frexp(r, &pow);
  return ldexp(r, randi() & 0x1f);
  return ldexp(r, randi() & 0x1f);
}
}
 
 
void
void
_DEFUN_VOID(test_frexp)
_DEFUN_VOID(test_frexp)
{
{
  int i;
  int i;
  double r;
  double r;
  int t;
  int t;
 
 
  float xf;
  float xf;
  double gives;
  double gives;
 
 
  int pow;
  int pow;
 
 
 
 
  /* Frexp of x return a and n, where a * 2**n == x, so test this with a
  /* Frexp of x return a and n, where a * 2**n == x, so test this with a
     set of random numbers */
     set of random numbers */
  for (t = 0; t < 2; t++)
  for (t = 0; t < 2; t++)
  {
  {
    for (i = 0; i < 1000; i++)
    for (i = 0; i < 1000; i++)
    {
    {
 
 
      double x = randx();
      double x = randx();
      line(i);
      line(i);
      switch (t)
      switch (t)
      {
      {
      case 0:
      case 0:
        newfunc("frexp/ldexp");
        newfunc("frexp/ldexp");
        r = frexp(x, &pow);
        r = frexp(x, &pow);
        if (r > 1.0 || r < -1.0)
        if (r > 1.0 || r < -1.0)
        {
        {
          /* Answer can never be > 1 or < 1 */
          /* Answer can never be > 1 or < 1 */
          test_iok(0,1);
          test_iok(0,1);
        }
        }
 
 
        gives = ldexp(r ,pow);
        gives = ldexp(r ,pow);
        test_mok(gives,x,62);
        test_mok(gives,x,62);
        break;
        break;
      case 1:
      case 1:
        newfunc("frexpf/ldexpf");
        newfunc("frexpf/ldexpf");
        if (x > FLT_MIN && x < FLT_MAX)
        if (x > FLT_MIN && x < FLT_MAX)
        {
        {
          /* test floats too, but they have a smaller range so make sure x
          /* test floats too, but they have a smaller range so make sure x
             isn't too big. Also x can get smaller than a float can
             isn't too big. Also x can get smaller than a float can
             represent to make sure that doesn't happen too */
             represent to make sure that doesn't happen too */
          xf = x;
          xf = x;
          r = frexpf(xf, &pow);
          r = frexpf(xf, &pow);
          if (r > 1.0 || r < -1.0)
          if (r > 1.0 || r < -1.0)
          {
          {
            /* Answer can never be > 1 or < -1 */
            /* Answer can never be > 1 or < -1 */
            test_iok(0,1);
            test_iok(0,1);
          }
          }
 
 
          gives = ldexpf(r ,pow);
          gives = ldexpf(r ,pow);
          test_mok(gives,x, 32);
          test_mok(gives,x, 32);
 
 
        }
        }
      }
      }
 
 
    }
    }
 
 
  }
  }
 
 
  /* test a few numbers manually to make sure frexp/ldexp are not
  /* test a few numbers manually to make sure frexp/ldexp are not
     testing as ok because both are broken */
     testing as ok because both are broken */
 
 
  r = frexp(64.0, &i);
  r = frexp(64.0, &i);
 
 
  test_mok(r, 0.5,64);
  test_mok(r, 0.5,64);
  test_iok(i, 7);
  test_iok(i, 7);
 
 
  r = frexp(96.0, &i);
  r = frexp(96.0, &i);
 
 
  test_mok(r, 0.75, 64);
  test_mok(r, 0.75, 64);
  test_iok(i, 7);
  test_iok(i, 7);
 
 
}
}
 
 
/* Test mod - this is given a real hammering by the strtod type
/* Test mod - this is given a real hammering by the strtod type
   routines, here are some more tests.
   routines, here are some more tests.
 
 
   By definition
   By definition
 
 
   modf = func(value, &iptr)
   modf = func(value, &iptr)
 
 
      (*iptr + modf) == value
      (*iptr + modf) == value
 
 
   we test this
   we test this
 
 
*/
*/
void
void
_DEFUN_VOID(test_mod)
_DEFUN_VOID(test_mod)
{
{
  int i;
  int i;
 
 
  newfunc("modf");
  newfunc("modf");
 
 
 
 
  for (i = 0; i < 1000; i++)
  for (i = 0; i < 1000; i++)
  {
  {
    double intpart;
    double intpart;
    double n;
    double n;
    line(i);
    line(i);
    n  = randx();
    n  = randx();
    if (finite(n) && n != 0.0 )
    if (finite(n) && n != 0.0 )
    {
    {
      double r = modf(n, &intpart);
      double r = modf(n, &intpart);
      line(i);
      line(i);
      test_mok(intpart + r, n, 63);
      test_mok(intpart + r, n, 63);
    }
    }
 
 
  }
  }
  newfunc("modff");
  newfunc("modff");
 
 
  for (i = 0; i < 1000; i++)
  for (i = 0; i < 1000; i++)
  {
  {
    float intpart;
    float intpart;
    double nd;
    double nd;
    line(i);
    line(i);
    nd  = randx() ;
    nd  = randx() ;
    if (nd < FLT_MAX && finitef(nd) && nd != 0.0)
    if (nd < FLT_MAX && finitef(nd) && nd != 0.0)
    {
    {
      float n = nd;
      float n = nd;
      double r = modff(n, &intpart);
      double r = modff(n, &intpart);
      line(i);
      line(i);
      test_mok(intpart + r, n, 32);
      test_mok(intpart + r, n, 32);
    }
    }
  }
  }
 
 
 
 
}
}
 
 
/*
/*
Test pow by multiplying logs
Test pow by multiplying logs
*/
*/
void
void
_DEFUN_VOID(test_pow)
_DEFUN_VOID(test_pow)
{
{
  unsigned int i;
  unsigned int i;
  newfunc("pow");
  newfunc("pow");
 
 
  for (i = 0; i < 1000; i++)
  for (i = 0; i < 1000; i++)
  {
  {
    double n1;
    double n1;
    double n2;
    double n2;
    double res;
    double res;
    double shouldbe;
    double shouldbe;
 
 
    line(i);
    line(i);
    n1 = fabs(randy());
    n1 = fabs(randy());
    n2 = fabs(randy()/100.0);
    n2 = fabs(randy()/100.0);
    res = pow(n1, n2);
    res = pow(n1, n2);
    shouldbe = exp(log(n1) * n2);
    shouldbe = exp(log(n1) * n2);
    test_mok(shouldbe, res,64);
    test_mok(shouldbe, res,64);
  }
  }
 
 
  newfunc("powf");
  newfunc("powf");
 
 
  for (i = 0; i < 1000; i++)
  for (i = 0; i < 1000; i++)
  {
  {
    double n1;
    double n1;
    double n2;
    double n2;
    double res;
    double res;
    double shouldbe;
    double shouldbe;
 
 
    errno = 0;
    errno = 0;
 
 
    line(i);
    line(i);
    n1 = fabs(randy());
    n1 = fabs(randy());
    n2 = fabs(randy()/100.0);
    n2 = fabs(randy()/100.0);
    res = powf(n1, n2);
    res = powf(n1, n2);
    shouldbe = expf(logf(n1) * n2);
    shouldbe = expf(logf(n1) * n2);
    if (!errno)
    if (!errno)
     test_mok(shouldbe, res,28);
     test_mok(shouldbe, res,28);
  }
  }
 
 
 
 
 
 
 
 
}
}
 
 
 
 
 
 
void
void
_DEFUN_VOID(test_math2)
_DEFUN_VOID(test_math2)
{
{
  test_mod();
  test_mod();
  test_frexp();
  test_frexp();
  test_pow();
  test_pow();
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.