Blame sysdeps/powerpc/test-arith.c

Packit 6c4009
/* Test floating-point arithmetic operations.
Packit 6c4009
   Copyright (C) 1997-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
#ifndef _GNU_SOURCE
Packit 6c4009
#define _GNU_SOURCE
Packit 6c4009
#endif
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <fenv.h>
Packit 6c4009
#include <assert.h>
Packit 6c4009
Packit 6c4009
#ifndef ESIZE
Packit 6c4009
typedef double tocheck_t;
Packit 6c4009
#define ESIZE 11
Packit 6c4009
#define MSIZE 52
Packit 6c4009
#define FUNC(x) x
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#define R_NEAREST 1
Packit 6c4009
#define R_ZERO 2
Packit 6c4009
#define R_UP 4
Packit 6c4009
#define R_DOWN 8
Packit 6c4009
#define R_ALL (R_NEAREST|R_ZERO|R_UP|R_DOWN)
Packit 6c4009
static fenv_t rmodes[4];
Packit 6c4009
static const char * const rmnames[4] =
Packit 6c4009
{ "nearest","zero","+Inf","-Inf" };
Packit 6c4009
Packit 6c4009
typedef union {
Packit 6c4009
  tocheck_t tc;
Packit 6c4009
  unsigned char c[sizeof(tocheck_t)];
Packit 6c4009
} union_t;
Packit 6c4009
Packit 6c4009
/* Don't try reading these in a font that doesn't distinguish
Packit 6c4009
   O and zero.  */
Packit 6c4009
typedef enum {
Packit 6c4009
  P_Z    = 0x0,  /* 00000...0 */
Packit 6c4009
  P_000O = 0x1,  /* 00011...1 */
Packit 6c4009
  P_001Z = 0x2,  /* 00100...0 */
Packit 6c4009
  P_00O  = 0x3,  /* 00111...1 */
Packit 6c4009
  P_01Z  = 0x4,  /* 01000...0 */
Packit 6c4009
  P_010O = 0x5,  /* 01011...1 */
Packit 6c4009
  P_011Z = 0x6,  /* 01100...0 */
Packit 6c4009
  P_0O   = 0x7,  /* 01111...1 */
Packit 6c4009
  P_1Z   = 0x8,  /* 10000...0 */
Packit 6c4009
  P_100O = 0x9,  /* 10011...1 */
Packit 6c4009
  P_101Z = 0xa,  /* 10100...0 */
Packit 6c4009
  P_10O  = 0xb,  /* 10111...1 */
Packit 6c4009
  P_11Z  = 0xc,  /* 11000...0 */
Packit 6c4009
  P_110O = 0xd,  /* 11011...1 */
Packit 6c4009
  P_111Z = 0xe,  /* 11100...0 */
Packit 6c4009
  P_O    = 0xf,  /* 11111...1 */
Packit 6c4009
  P_Z1   = 0x11, /* 000...001 */
Packit 6c4009
  P_Z10  = 0x12, /* 000...010 */
Packit 6c4009
  P_Z11  = 0x13, /* 000...011 */
Packit 6c4009
  P_0O00 = 0x14, /* 011...100 */
Packit 6c4009
  P_0O01 = 0x15, /* 011...101 */
Packit 6c4009
  P_0O0  = 0x16, /* 011...110 */
Packit 6c4009
  P_1Z1  = 0x19, /* 100...001 */
Packit 6c4009
  P_1Z10 = 0x1a, /* 100...010 */
Packit 6c4009
  P_1Z11 = 0x1b, /* 100...011 */
Packit 6c4009
  P_O00  = 0x1c, /* 111...100 */
Packit 6c4009
  P_O01  = 0x1d, /* 111...101 */
Packit 6c4009
  P_O0   = 0x1e, /* 111...110 */
Packit 6c4009
  P_R    = 0x20, /* rrr...rrr */ /* ('r' means random. ) */
Packit 6c4009
  P_Ro   = 0x21, /* rrr...rrr, with odd parity.  */
Packit 6c4009
  P_0R   = 0x22, /* 0rr...rrr */
Packit 6c4009
  P_1R   = 0x23, /* 1rr...rrr */
Packit 6c4009
  P_Rno  = 0x24, /* rrr...rrr, but not all ones.  */
Packit 6c4009
} pattern_t;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
pattern_fill(pattern_t ptn, unsigned char *start, int bitoffset, int count)
Packit 6c4009
{
Packit 6c4009
#define bitset(count, value) \
Packit 6c4009
      start[(count)/8] = (start[(count)/8] & ~(1 << 7-(count)%8)  \
Packit 6c4009
                          |  (value) << 7-(count)%8)
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  if (ptn >= 0 && ptn <= 0xf)
Packit 6c4009
    {
Packit 6c4009
      /* Patterns between 0 and 0xF have the following format:
Packit 6c4009
	 The LSBit is used to fill the last n-3 bits of the pattern;
Packit 6c4009
	 The next 3 bits are the first 3 bits of the pattern. */
Packit 6c4009
      for (i = 0; i < count; i++)
Packit 6c4009
	if (i < 3)
Packit 6c4009
	  bitset((bitoffset+i), ptn >> (3-i) & 1);
Packit 6c4009
	else
Packit 6c4009
	  bitset((bitoffset+i), ptn >> 0 & 1);
Packit 6c4009
    }
Packit 6c4009
  else if (ptn <= 0x1f)
Packit 6c4009
    {
Packit 6c4009
      /* Patterns between 0x10 and 0x1F have the following format:
Packit 6c4009
	 The two LSBits are the last two bits of the pattern;
Packit 6c4009
	 The 0x8 bit is the first bit of the pattern;
Packit 6c4009
	 The 0x4 bit is used to fill the remainder. */
Packit 6c4009
      for (i = 0; i < count; i++)
Packit 6c4009
	if (i == 0)
Packit 6c4009
	  bitset((bitoffset+i), ptn >> 3 & 1);
Packit 6c4009
	else if (i >= count-2)
Packit 6c4009
	  bitset((bitoffset+i), ptn >> (count-1-i) & 1);
Packit 6c4009
	else
Packit 6c4009
	  bitset((bitoffset+i), ptn >> 2 & 1);
Packit 6c4009
    }
Packit 6c4009
  else switch (ptn)
Packit 6c4009
    {
Packit 6c4009
    case P_0R: case P_1R:
Packit 6c4009
      assert(count > 0);
Packit 6c4009
      bitset(bitoffset, ptn & 1);
Packit 6c4009
      count--;
Packit 6c4009
      bitoffset++;
Packit 6c4009
    case P_R:
Packit 6c4009
      for (; count > 0; count--, bitoffset++)
Packit 6c4009
	bitset(bitoffset, rand() & 1);
Packit 6c4009
      break;
Packit 6c4009
    case P_Ro:
Packit 6c4009
      {
Packit 6c4009
	int op = 1;
Packit 6c4009
	assert(count > 0);
Packit 6c4009
	for (; count > 1; count--, bitoffset++)
Packit 6c4009
	  bitset(bitoffset, op ^= (rand() & 1));
Packit 6c4009
	bitset(bitoffset, op);
Packit 6c4009
	break;
Packit 6c4009
      }
Packit 6c4009
    case P_Rno:
Packit 6c4009
      {
Packit 6c4009
	int op = 1;
Packit 6c4009
	assert(count > 0);
Packit 6c4009
	for (; count > 1; count--, bitoffset++)
Packit 6c4009
	{
Packit 6c4009
	  int r = rand() & 1;
Packit 6c4009
	  op &= r;
Packit 6c4009
	  bitset(bitoffset, r);
Packit 6c4009
	}
Packit 6c4009
	bitset(bitoffset, rand() & (op ^ 1));
Packit 6c4009
	break;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
    default:
Packit 6c4009
      assert(0);
Packit 6c4009
    }
Packit 6c4009
#undef bitset
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static tocheck_t
Packit 6c4009
pattern(int negative, pattern_t exp, pattern_t mant)
Packit 6c4009
{
Packit 6c4009
  union_t result;
Packit 6c4009
#if 0
Packit 6c4009
  int i;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  pattern_fill(negative ? P_O : P_Z, result.c, 0, 1);
Packit 6c4009
  pattern_fill(exp, result.c, 1, ESIZE);
Packit 6c4009
  pattern_fill(mant, result.c, ESIZE+1, MSIZE);
Packit 6c4009
#if 0
Packit 6c4009
  printf("neg=%d exp=%02x mant=%02x: ", negative, exp, mant);
Packit 6c4009
  for (i = 0; i < sizeof(tocheck_t); i++)
Packit 6c4009
    printf("%02x", result.c[i]);
Packit 6c4009
  printf("\n");
Packit 6c4009
#endif
Packit 6c4009
  return result.tc;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Return the closest different tocheck_t to 'x' in the direction of
Packit 6c4009
   'direction', or 'x' if there is no such value.  Assumes 'x' is not
Packit 6c4009
   a NaN.  */
Packit 6c4009
static tocheck_t
Packit 6c4009
delta(tocheck_t x, int direction)
Packit 6c4009
{
Packit 6c4009
  union_t xx;
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  xx.tc = x;
Packit 6c4009
  if (xx.c[0] & 0x80)
Packit 6c4009
    direction = -direction;
Packit 6c4009
  if (direction == +1)
Packit 6c4009
    {
Packit 6c4009
      union_t tx;
Packit 6c4009
      tx.tc = pattern(xx.c[0] >> 7, P_O, P_Z);
Packit 6c4009
      if (memcmp(tx.c, xx.c, sizeof(tocheck_t)) == 0)
Packit 6c4009
	return x;
Packit 6c4009
    }
Packit 6c4009
  for (i = sizeof(tocheck_t)-1; i > 0; i--)
Packit 6c4009
    {
Packit 6c4009
      xx.c[i] += direction;
Packit 6c4009
      if (xx.c[i] != (direction > 0 ? 0 : 0xff))
Packit 6c4009
	return xx.tc;
Packit 6c4009
    }
Packit 6c4009
  if (direction < 0 && (xx.c[0] & 0x7f) == 0)
Packit 6c4009
    return pattern(~(xx.c[0] >> 7) & 1, P_Z, P_Z1);
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      xx.c[0] += direction;
Packit 6c4009
      return xx.tc;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int nerrors = 0;
Packit 6c4009
Packit 6c4009
#ifdef FE_ALL_INVALID
Packit 6c4009
static const int all_exceptions = FE_ALL_INVALID | FE_ALL_EXCEPT;
Packit 6c4009
#else
Packit 6c4009
static const int all_exceptions = FE_ALL_EXCEPT;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
check_result(int line, const char *rm, tocheck_t expected, tocheck_t actual)
Packit 6c4009
{
Packit 6c4009
  if (memcmp(&expected, &actual, sizeof(tocheck_t)) != 0)
Packit 6c4009
    {
Packit 6c4009
      unsigned char *ex, *ac;
Packit 6c4009
      size_t i;
Packit 6c4009
Packit 6c4009
      printf("%s:%d:round %s:result failed\n"
Packit 6c4009
	     " expected result 0x", __FILE__, line, rm);
Packit 6c4009
      ex = (unsigned char *)&expecte;;
Packit 6c4009
      ac = (unsigned char *)&actual;
Packit 6c4009
      for (i = 0; i < sizeof(tocheck_t); i++)
Packit 6c4009
	printf("%02x", ex[i]);
Packit 6c4009
      printf(" got 0x");
Packit 6c4009
      for (i = 0; i < sizeof(tocheck_t); i++)
Packit 6c4009
	printf("%02x", ac[i]);
Packit 6c4009
      printf("\n");
Packit 6c4009
      nerrors++;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static const struct {
Packit 6c4009
  int except;
Packit 6c4009
  const char *name;
Packit 6c4009
} excepts[] = {
Packit 6c4009
#define except_entry(ex) { ex, #ex } ,
Packit 6c4009
#ifdef FE_INEXACT
Packit 6c4009
  except_entry(FE_INEXACT)
Packit 6c4009
#else
Packit 6c4009
# define FE_INEXACT 0
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_DIVBYZERO
Packit 6c4009
  except_entry(FE_DIVBYZERO)
Packit 6c4009
#else
Packit 6c4009
# define FE_DIVBYZERO 0
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_UNDERFLOW
Packit 6c4009
  except_entry(FE_UNDERFLOW)
Packit 6c4009
#else
Packit 6c4009
# define FE_UNDERFLOW 0
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_OVERFLOW
Packit 6c4009
  except_entry(FE_OVERFLOW)
Packit 6c4009
#else
Packit 6c4009
# define FE_OVERFLOW 0
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_INVALID
Packit 6c4009
  except_entry(FE_INVALID)
Packit 6c4009
#else
Packit 6c4009
# define FE_INVALID 0
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_INVALID_SNAN
Packit 6c4009
  except_entry(FE_INVALID_SNAN)
Packit 6c4009
#else
Packit 6c4009
# define FE_INVALID_SNAN FE_INVALID
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_INVALID_ISI
Packit 6c4009
  except_entry(FE_INVALID_ISI)
Packit 6c4009
#else
Packit 6c4009
# define FE_INVALID_ISI FE_INVALID
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_INVALID_IDI
Packit 6c4009
  except_entry(FE_INVALID_IDI)
Packit 6c4009
#else
Packit 6c4009
# define FE_INVALID_IDI FE_INVALID
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_INVALID_ZDZ
Packit 6c4009
  except_entry(FE_INVALID_ZDZ)
Packit 6c4009
#else
Packit 6c4009
# define FE_INVALID_ZDZ FE_INVALID
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_INVALID_COMPARE
Packit 6c4009
  except_entry(FE_INVALID_COMPARE)
Packit 6c4009
#else
Packit 6c4009
# define FE_INVALID_COMPARE FE_INVALID
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_INVALID_SOFTWARE
Packit 6c4009
  except_entry(FE_INVALID_SOFTWARE)
Packit 6c4009
#else
Packit 6c4009
# define FE_INVALID_SOFTWARE FE_INVALID
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_INVALID_SQRT
Packit 6c4009
  except_entry(FE_INVALID_SQRT)
Packit 6c4009
#else
Packit 6c4009
# define FE_INVALID_SQRT FE_INVALID
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_INVALID_INTEGER_CONVERSION
Packit 6c4009
  except_entry(FE_INVALID_INTEGER_CONVERSION)
Packit 6c4009
#else
Packit 6c4009
# define FE_INVALID_INTEGER_CONVERSION FE_INVALID
Packit 6c4009
#endif
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static int excepts_missing = 0;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
check_excepts(int line, const char *rm, int expected, int actual)
Packit 6c4009
{
Packit 6c4009
  if (expected & excepts_missing)
Packit 6c4009
    expected = expected & ~excepts_missing | FE_INVALID_SNAN;
Packit 6c4009
  if ((expected & all_exceptions) != actual)
Packit 6c4009
    {
Packit 6c4009
      size_t i;
Packit 6c4009
      printf("%s:%d:round %s:exceptions failed\n"
Packit 6c4009
	     " expected exceptions ", __FILE__, line,rm);
Packit 6c4009
      for (i = 0; i < sizeof(excepts)/sizeof(excepts[0]); i++)
Packit 6c4009
	if (expected & excepts[i].except)
Packit 6c4009
	  printf("%s ",excepts[i].name);
Packit 6c4009
      if ((expected & all_exceptions) == 0)
Packit 6c4009
	printf("- ");
Packit 6c4009
      printf("got");
Packit 6c4009
      for (i = 0; i < sizeof(excepts)/sizeof(excepts[0]); i++)
Packit 6c4009
	if (actual & excepts[i].except)
Packit 6c4009
	  printf(" %s",excepts[i].name);
Packit 6c4009
      if ((actual & all_exceptions) == 0)
Packit 6c4009
	printf("- ");
Packit 6c4009
      printf(".\n");
Packit 6c4009
      nerrors++;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
typedef enum {
Packit 6c4009
  B_ADD, B_SUB, B_MUL, B_DIV, B_NEG, B_ABS, B_SQRT
Packit 6c4009
} op_t;
Packit 6c4009
typedef struct {
Packit 6c4009
  int line;
Packit 6c4009
  op_t op;
Packit 6c4009
  int a_sgn;
Packit 6c4009
  pattern_t a_exp, a_mant;
Packit 6c4009
  int b_sgn;
Packit 6c4009
  pattern_t b_exp, b_mant;
Packit 6c4009
  int rmode;
Packit 6c4009
  int excepts;
Packit 6c4009
  int x_sgn;
Packit 6c4009
  pattern_t x_exp, x_mant;
Packit 6c4009
} optest_t;
Packit 6c4009
static const optest_t optests[] = {
Packit 6c4009
  /* Additions of zero.  */
Packit 6c4009
  {__LINE__,B_ADD, 0,P_Z,P_Z, 0,P_Z,P_Z, R_ALL,0, 0,P_Z,P_Z },
Packit 6c4009
  {__LINE__,B_ADD, 1,P_Z,P_Z, 0,P_Z,P_Z, R_ALL & ~R_DOWN,0, 0,P_Z,P_Z },
Packit 6c4009
  {__LINE__,B_ADD, 1,P_Z,P_Z, 0,P_Z,P_Z, R_DOWN,0, 1,P_Z,P_Z },
Packit 6c4009
  {__LINE__,B_ADD, 1,P_Z,P_Z, 1,P_Z,P_Z, R_ALL,0, 1,P_Z,P_Z },
Packit 6c4009
Packit 6c4009
  /* Additions with NaN.  */
Packit 6c4009
  {__LINE__,B_ADD, 0,P_O,P_101Z, 0,P_Z,P_Z, R_ALL,0, 0,P_O,P_101Z },
Packit 6c4009
  {__LINE__,B_ADD, 0,P_O,P_01Z, 0,P_Z,P_Z, R_ALL,
Packit 6c4009
   FE_INVALID | FE_INVALID_SNAN, 0,P_O,P_11Z },
Packit 6c4009
  {__LINE__,B_ADD, 0,P_O,P_Z, 0,P_O,P_0O, R_ALL,
Packit 6c4009
   FE_INVALID | FE_INVALID_SNAN, 0,P_O,P_O },
Packit 6c4009
  {__LINE__,B_ADD, 0,P_Z,P_Z, 0,P_O,P_11Z, R_ALL,0, 0,P_O,P_11Z },
Packit 6c4009
  {__LINE__,B_ADD, 0,P_O,P_001Z, 0,P_O,P_001Z, R_ALL,
Packit 6c4009
   FE_INVALID | FE_INVALID_SNAN, 0,P_O,P_101Z },
Packit 6c4009
  {__LINE__,B_ADD, 0,P_O,P_1Z, 0,P_Z,P_Z, R_ALL,0, 0,P_O,P_1Z },
Packit 6c4009
  {__LINE__,B_ADD, 0,P_0O,P_Z, 0,P_O,P_10O, R_ALL,0, 0,P_O,P_10O },
Packit 6c4009
Packit 6c4009
  /* Additions with infinity.  */
Packit 6c4009
  {__LINE__,B_ADD, 0,P_O,P_Z, 0,P_Z,P_Z, R_ALL,0, 0,P_O,P_Z },
Packit 6c4009
  {__LINE__,B_ADD, 0,P_O,P_Z, 1,P_Z,P_Z, R_ALL,0, 0,P_O,P_Z },
Packit 6c4009
  {__LINE__,B_ADD, 1,P_O,P_Z, 0,P_Z,P_Z, R_ALL,0, 1,P_O,P_Z },
Packit 6c4009
  {__LINE__,B_ADD, 1,P_O,P_Z, 1,P_Z,P_Z, R_ALL,0, 1,P_O,P_Z },
Packit 6c4009
  {__LINE__,B_ADD, 0,P_O,P_Z, 0,P_O,P_Z, R_ALL,0, 0,P_O,P_Z },
Packit 6c4009
  {__LINE__,B_ADD, 1,P_O,P_Z, 1,P_O,P_Z, R_ALL,0, 1,P_O,P_Z },
Packit 6c4009
  {__LINE__,B_ADD, 0,P_O,P_Z, 1,P_O,P_Z, R_ALL,
Packit 6c4009
   FE_INVALID | FE_INVALID_ISI, 0,P_O,P_1Z },
Packit 6c4009
  {__LINE__,B_ADD, 1,P_O,P_Z, 0,P_O,P_Z, R_ALL,
Packit 6c4009
   FE_INVALID | FE_INVALID_ISI, 0,P_O,P_1Z },
Packit 6c4009
  {__LINE__,B_ADD, 0,P_O,P_Z, 0,P_0O,P_Z, R_ALL,0, 0,P_O,P_Z },
Packit 6c4009
  {__LINE__,B_ADD, 1,P_O,P_Z, 0,P_0O,P_Z, R_ALL,0, 1,P_O,P_Z },
Packit 6c4009
  {__LINE__,B_ADD, 0,P_O,P_Z, 1,P_0O,P_Z, R_ALL,0, 0,P_O,P_Z },
Packit 6c4009
  {__LINE__,B_ADD, 1,P_O,P_Z, 1,P_0O,P_Z, R_ALL,0, 1,P_O,P_Z },
Packit 6c4009
Packit 6c4009
  /* Overflow (and zero).  */
Packit 6c4009
  {__LINE__,B_ADD, 0,P_O0,P_Z, 0,P_O0,P_Z, R_NEAREST | R_UP,
Packit 6c4009
   FE_INEXACT | FE_OVERFLOW, 0,P_O,P_Z },
Packit 6c4009
  {__LINE__,B_ADD, 0,P_O0,P_Z, 0,P_O0,P_Z, R_ZERO | R_DOWN,
Packit 6c4009
   FE_INEXACT | FE_OVERFLOW, 0,P_O0,P_O },
Packit 6c4009
  {__LINE__,B_ADD, 1,P_O0,P_Z, 1,P_O0,P_Z, R_NEAREST | R_DOWN,
Packit 6c4009
   FE_INEXACT | FE_OVERFLOW, 1,P_O,P_Z },
Packit 6c4009
  {__LINE__,B_ADD, 1,P_O0,P_Z, 1,P_O0,P_Z, R_ZERO | R_UP,
Packit 6c4009
   FE_INEXACT | FE_OVERFLOW, 1,P_O0,P_O },
Packit 6c4009
  {__LINE__,B_ADD, 0,P_O0,P_Z, 1,P_O0,P_Z, R_ALL & ~R_DOWN,
Packit 6c4009
   0, 0,P_Z,P_Z },
Packit 6c4009
  {__LINE__,B_ADD, 0,P_O0,P_Z, 1,P_O0,P_Z, R_DOWN,
Packit 6c4009
   0, 1,P_Z,P_Z },
Packit 6c4009
Packit 6c4009
  /* Negation.  */
Packit 6c4009
  {__LINE__,B_NEG, 0,P_Z,P_Z,   0,0,0, R_ALL, 0, 1,P_Z,P_Z },
Packit 6c4009
  {__LINE__,B_NEG, 1,P_Z,P_Z,   0,0,0, R_ALL, 0, 0,P_Z,P_Z },
Packit 6c4009
  {__LINE__,B_NEG, 0,P_O,P_Z,   0,0,0, R_ALL, 0, 1,P_O,P_Z },
Packit 6c4009
  {__LINE__,B_NEG, 1,P_O,P_Z,   0,0,0, R_ALL, 0, 0,P_O,P_Z },
Packit 6c4009
  {__LINE__,B_NEG, 0,P_O,P_1Z,  0,0,0, R_ALL, 0, 1,P_O,P_1Z },
Packit 6c4009
  {__LINE__,B_NEG, 1,P_O,P_1Z,  0,0,0, R_ALL, 0, 0,P_O,P_1Z },
Packit 6c4009
  {__LINE__,B_NEG, 0,P_O,P_01Z, 0,0,0, R_ALL, 0, 1,P_O,P_01Z },
Packit 6c4009
  {__LINE__,B_NEG, 1,P_O,P_01Z, 0,0,0, R_ALL, 0, 0,P_O,P_01Z },
Packit 6c4009
  {__LINE__,B_NEG, 0,P_1Z,P_1Z1, 0,0,0, R_ALL, 0, 1,P_1Z,P_1Z1 },
Packit 6c4009
  {__LINE__,B_NEG, 1,P_1Z,P_1Z1, 0,0,0, R_ALL, 0, 0,P_1Z,P_1Z1 },
Packit 6c4009
  {__LINE__,B_NEG, 0,P_Z,P_Z1,  0,0,0, R_ALL, 0, 1,P_Z,P_Z1 },
Packit 6c4009
  {__LINE__,B_NEG, 1,P_Z,P_Z1,  0,0,0, R_ALL, 0, 0,P_Z,P_Z1 },
Packit 6c4009
Packit 6c4009
  /* Absolute value.  */
Packit 6c4009
  {__LINE__,B_ABS, 0,P_Z,P_Z,   0,0,0, R_ALL, 0, 0,P_Z,P_Z },
Packit 6c4009
  {__LINE__,B_ABS, 1,P_Z,P_Z,   0,0,0, R_ALL, 0, 0,P_Z,P_Z },
Packit 6c4009
  {__LINE__,B_ABS, 0,P_O,P_Z,   0,0,0, R_ALL, 0, 0,P_O,P_Z },
Packit 6c4009
  {__LINE__,B_ABS, 1,P_O,P_Z,   0,0,0, R_ALL, 0, 0,P_O,P_Z },
Packit 6c4009
  {__LINE__,B_ABS, 0,P_O,P_1Z,  0,0,0, R_ALL, 0, 0,P_O,P_1Z },
Packit 6c4009
  {__LINE__,B_ABS, 1,P_O,P_1Z,  0,0,0, R_ALL, 0, 0,P_O,P_1Z },
Packit 6c4009
  {__LINE__,B_ABS, 0,P_O,P_01Z, 0,0,0, R_ALL, 0, 0,P_O,P_01Z },
Packit 6c4009
  {__LINE__,B_ABS, 1,P_O,P_01Z, 0,0,0, R_ALL, 0, 0,P_O,P_01Z },
Packit 6c4009
  {__LINE__,B_ABS, 0,P_1Z,P_1Z1, 0,0,0, R_ALL, 0, 0,P_1Z,P_1Z1 },
Packit 6c4009
  {__LINE__,B_ABS, 1,P_1Z,P_1Z1, 0,0,0, R_ALL, 0, 0,P_1Z,P_1Z1 },
Packit 6c4009
  {__LINE__,B_ABS, 0,P_Z,P_Z1,  0,0,0, R_ALL, 0, 0,P_Z,P_Z1 },
Packit 6c4009
  {__LINE__,B_ABS, 1,P_Z,P_Z1,  0,0,0, R_ALL, 0, 0,P_Z,P_Z1 },
Packit 6c4009
Packit 6c4009
  /* Square root.  */
Packit 6c4009
  {__LINE__,B_SQRT, 0,P_Z,P_Z,   0,0,0, R_ALL, 0, 0,P_Z,P_Z },
Packit 6c4009
  {__LINE__,B_SQRT, 1,P_Z,P_Z,   0,0,0, R_ALL, 0, 1,P_Z,P_Z },
Packit 6c4009
  {__LINE__,B_SQRT, 0,P_O,P_1Z,  0,0,0, R_ALL, 0, 0,P_O,P_1Z },
Packit 6c4009
  {__LINE__,B_SQRT, 1,P_O,P_1Z,  0,0,0, R_ALL, 0, 1,P_O,P_1Z },
Packit 6c4009
  {__LINE__,B_SQRT, 0,P_O,P_01Z, 0,0,0, R_ALL,
Packit 6c4009
   FE_INVALID | FE_INVALID_SNAN, 0,P_O,P_11Z },
Packit 6c4009
  {__LINE__,B_SQRT, 1,P_O,P_01Z, 0,0,0, R_ALL,
Packit 6c4009
   FE_INVALID | FE_INVALID_SNAN, 1,P_O,P_11Z },
Packit 6c4009
Packit 6c4009
  {__LINE__,B_SQRT, 0,P_O,P_Z,   0,0,0, R_ALL, 0, 0,P_O,P_Z },
Packit 6c4009
  {__LINE__,B_SQRT, 0,P_0O,P_Z,  0,0,0, R_ALL, 0, 0,P_0O,P_Z },
Packit 6c4009
Packit 6c4009
  {__LINE__,B_SQRT, 1,P_O,P_Z,   0,0,0, R_ALL,
Packit 6c4009
   FE_INVALID | FE_INVALID_SQRT, 0,P_O,P_1Z },
Packit 6c4009
  {__LINE__,B_SQRT, 1,P_1Z,P_1Z1, 0,0,0, R_ALL,
Packit 6c4009
   FE_INVALID | FE_INVALID_SQRT, 0,P_O,P_1Z },
Packit 6c4009
  {__LINE__,B_SQRT, 1,P_Z,P_Z1,  0,0,0, R_ALL,
Packit 6c4009
   FE_INVALID | FE_INVALID_SQRT, 0,P_O,P_1Z },
Packit 6c4009
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
check_op(void)
Packit 6c4009
{
Packit 6c4009
  size_t i;
Packit 6c4009
  int j;
Packit 6c4009
  tocheck_t r, a, b, x;
Packit 6c4009
  int raised;
Packit 6c4009
Packit 6c4009
  for (i = 0; i < sizeof(optests)/sizeof(optests[0]); i++)
Packit 6c4009
    {
Packit 6c4009
      a = pattern(optests[i].a_sgn, optests[i].a_exp,
Packit 6c4009
		  optests[i].a_mant);
Packit 6c4009
      b = pattern(optests[i].b_sgn, optests[i].b_exp,
Packit 6c4009
		  optests[i].b_mant);
Packit 6c4009
      x = pattern(optests[i].x_sgn, optests[i].x_exp,
Packit 6c4009
		  optests[i].x_mant);
Packit 6c4009
      for (j = 0; j < 4; j++)
Packit 6c4009
	if (optests[i].rmode & 1<
Packit 6c4009
	  {
Packit 6c4009
	    fesetenv(rmodes+j);
Packit 6c4009
	    switch (optests[i].op)
Packit 6c4009
	      {
Packit 6c4009
	      case B_ADD: r = a + b; break;
Packit 6c4009
	      case B_SUB: r = a - b; break;
Packit 6c4009
	      case B_MUL: r = a * b; break;
Packit 6c4009
	      case B_DIV: r = a / b; break;
Packit 6c4009
	      case B_NEG: r = -a; break;
Packit 6c4009
	      case B_ABS: r = FUNC(fabs)(a); break;
Packit 6c4009
	      case B_SQRT: r = FUNC(sqrt)(a); break;
Packit 6c4009
	      }
Packit 6c4009
	    raised = fetestexcept(all_exceptions);
Packit 6c4009
	    check_result(optests[i].line,rmnames[j],x,r);
Packit 6c4009
	    check_excepts(optests[i].line,rmnames[j],
Packit 6c4009
			  optests[i].excepts,raised);
Packit 6c4009
	  }
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
fail_xr(int line, const char *rm, tocheck_t x, tocheck_t r, tocheck_t xx,
Packit 6c4009
	int xflag)
Packit 6c4009
{
Packit 6c4009
  size_t i;
Packit 6c4009
  unsigned char *cx, *cr, *cxx;
Packit 6c4009
Packit 6c4009
  printf("%s:%d:round %s:fail\n with x=0x", __FILE__, line,rm);
Packit 6c4009
  cx = (unsigned char *)&x;
Packit 6c4009
  cr = (unsigned char *)&r;
Packit 6c4009
  cxx = (unsigned char *)&xx;
Packit 6c4009
  for (i = 0; i < sizeof(tocheck_t); i++)
Packit 6c4009
    printf("%02x", cx[i]);
Packit 6c4009
  printf(" r=0x");
Packit 6c4009
  for (i = 0; i < sizeof(tocheck_t); i++)
Packit 6c4009
    printf("%02x", cr[i]);
Packit 6c4009
  printf(" xx=0x");
Packit 6c4009
  for (i = 0; i < sizeof(tocheck_t); i++)
Packit 6c4009
    printf("%02x", cxx[i]);
Packit 6c4009
  printf(" inexact=%d\n", xflag != 0);
Packit 6c4009
  nerrors++;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
check_sqrt(tocheck_t a)
Packit 6c4009
{
Packit 6c4009
  int j;
Packit 6c4009
  tocheck_t r0, r1, r2, x0, x1, x2;
Packit 6c4009
  int raised = 0;
Packit 6c4009
  int ok;
Packit 6c4009
Packit 6c4009
  for (j = 0; j < 4; j++)
Packit 6c4009
    {
Packit 6c4009
      int excepts;
Packit 6c4009
Packit 6c4009
      fesetenv(rmodes+j);
Packit 6c4009
      r1 = FUNC(sqrt)(a);
Packit 6c4009
      excepts = fetestexcept(all_exceptions);
Packit 6c4009
      fesetenv(FE_DFL_ENV);
Packit 6c4009
      raised |= excepts & ~FE_INEXACT;
Packit 6c4009
      x1 = r1 * r1 - a;
Packit 6c4009
      if (excepts & FE_INEXACT)
Packit 6c4009
	{
Packit 6c4009
	  r0 = delta(r1,-1); r2 = delta(r1,1);
Packit 6c4009
	  switch (1 << j)
Packit 6c4009
	    {
Packit 6c4009
	    case R_NEAREST:
Packit 6c4009
	      x0 = r0 * r0 - a; x2 = r2 * r2 - a;
Packit 6c4009
	      ok = fabs(x0) >= fabs(x1) && fabs(x1) <= fabs(x2);
Packit 6c4009
	      break;
Packit 6c4009
	    case R_ZERO:  case R_DOWN:
Packit 6c4009
	      x2 = r2 * r2 - a;
Packit 6c4009
	      ok = x1 <= 0 && x2 >= 0;
Packit 6c4009
	      break;
Packit 6c4009
	    case R_UP:
Packit 6c4009
	      x0 = r0 * r0 - a;
Packit 6c4009
	      ok = x1 >= 0 && x0 <= 0;
Packit 6c4009
	      break;
Packit 6c4009
	    default:
Packit 6c4009
	      assert(0);
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	ok = x1 == 0;
Packit 6c4009
      if (!ok)
Packit 6c4009
	fail_xr(__LINE__,rmnames[j],a,r1,x1,excepts&FE_INEXACT);
Packit 6c4009
    }
Packit 6c4009
  check_excepts(__LINE__,"all",0,raised);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int main(int argc, char **argv)
Packit 6c4009
{
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  _LIB_VERSION = _IEEE_;
Packit 6c4009
Packit 6c4009
  /* Set up environments for rounding modes.  */
Packit 6c4009
  fesetenv(FE_DFL_ENV);
Packit 6c4009
  fesetround(FE_TONEAREST);
Packit 6c4009
  fegetenv(rmodes+0);
Packit 6c4009
  fesetround(FE_TOWARDZERO);
Packit 6c4009
  fegetenv(rmodes+1);
Packit 6c4009
  fesetround(FE_UPWARD);
Packit 6c4009
  fegetenv(rmodes+2);
Packit 6c4009
  fesetround(FE_DOWNWARD);
Packit 6c4009
  fegetenv(rmodes+3);
Packit 6c4009
Packit 6c4009
#if defined(FE_INVALID_SOFTWARE) || defined(FE_INVALID_SQRT)
Packit 6c4009
  /* There's this really stupid feature of the 601... */
Packit 6c4009
  fesetenv(FE_DFL_ENV);
Packit 6c4009
  feraiseexcept(FE_INVALID_SOFTWARE);
Packit 6c4009
  if (!fetestexcept(FE_INVALID_SOFTWARE))
Packit 6c4009
    excepts_missing |= FE_INVALID_SOFTWARE;
Packit 6c4009
  fesetenv(FE_DFL_ENV);
Packit 6c4009
  feraiseexcept(FE_INVALID_SQRT);
Packit 6c4009
  if (!fetestexcept(FE_INVALID_SQRT))
Packit 6c4009
    excepts_missing |= FE_INVALID_SQRT;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  check_op();
Packit 6c4009
  for (i = 0; i < 100000; i++)
Packit 6c4009
    check_sqrt(pattern(0, P_Rno, P_R));
Packit 6c4009
  for (i = 0; i < 100; i++)
Packit 6c4009
    check_sqrt(pattern(0, P_Z, P_R));
Packit 6c4009
  check_sqrt(pattern(0,P_Z,P_Z1));
Packit 6c4009
Packit 6c4009
  printf("%d errors.\n", nerrors);
Packit 6c4009
  return nerrors == 0 ? 0 : 1;
Packit 6c4009
}