summary refs log tree commit diff
path: root/math/test-reduce.c
blob: 5149ead34188d95cf265bfb4019dff53365a71fd (plain) (blame)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* Copyright (C) 1997 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Geoffrey Keating <Geoff.Keating@anu.edu.au>, 1997.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The GNU C Library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */

/* This is a generic program for comparing two precisions of a one-input
   mathematical function.  It is amazingly good at detecting when GCC
   folds constants improperly.  */

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <math.h>
#include <ieee754.h>
#include <fenv.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define TSMALL float
#define RSMALL(rfun) ({ unsigned rnum = (rfun); *(float *) &rnum; })
#define TBIG double
#define XDIFF (24)
#define REDUCE(x) \
   ({ union ieee754_float u = { x }; u.ieee.exponent = 0x80; x = u.f; })
#define ABS(x) fabs(x)

#define string_0(x) #x
#define string_1(x) string_0(x)
#define TBIG_NAME string_1(TBIG)
#define TSMALL_NAME string_1(TSMALL)

#define R_NEAREST 1
#define R_ZERO 2
#define R_UP 4
#define R_DOWN 8
#define R_ALL (R_NEAREST|R_ZERO|R_UP|R_DOWN)
static fenv_t rmodes[4];
static const char * const rmnames[4] =
{ "near","zero","+Inf","-Inf" };

static int quiet = 0;

#ifdef FE_ALL_INVALID
static const int invalid_exceptions = (FE_ALL_INVALID
				       | FE_INVALID | FE_DIVBYZERO);
#else
static const int invalid_exceptions = (FE_INVALID | FE_DIVBYZERO);
#endif

static int
checkit (char *fname,
	 TSMALL (*fsmall) (TSMALL), TBIG (*fbig) (TBIG),
	 unsigned smalltries, unsigned largetries)
{
  unsigned int i, nerrors = 0, nwarn;

  int tryone (TSMALL fval)
    {
      int rmode;
      int excepts, exceptsb;
      TSMALL fres;
      TBIG fvalb, fresb, diff;
      char warn;

      fvalb = (TBIG) fval;

      for (rmode = 0; rmode < 4; ++rmode)
	{
	  fesetenv (rmodes + rmode);
	  fres = fsmall (fval);
	  excepts = fetestexcept (invalid_exceptions);
	  fesetenv (rmodes + rmode);
	  fresb = fbig (fvalb);
	  exceptsb = fetestexcept (invalid_exceptions);

	  if (excepts != exceptsb)
	    {
	      unsigned char *fvp = (unsigned char *) &fval;
	      size_t j;

	      printf ("%s(", fname);
	      for (j = 0; j < sizeof (TSMALL); j++)
		printf ("%02x", fvp[j]);
	      printf ("),%s: exceptions %s: %08x, %s: %08x\n",
		      rmnames[rmode],
		      TBIG_NAME, exceptsb, TSMALL_NAME, excepts);
	      if (++nerrors > 10)
		return 1;
	    }

	  diff = ABS (fres - (TSMALL) fresb);
	  if (fres == (TSMALL) fresb
	      || isnan (fres) && isnan (fresb)
	      || logb (fresb) - logb (diff) < XDIFF - 1)
	    continue;

	  if (logb (fresb) - logb (diff) < XDIFF)
	    {
	      if (++nwarn > 10 || quiet)
		continue;
	      warn = 'w';
	    }
	  else
	    {
	      if (++nerrors > 10)
		return 1;
	      warn = 'e';
	    }

	  {
	    TSMALL fresbs = (TSMALL) fresb;
	    unsigned char *fvp = (unsigned char *) &fval;
	    unsigned char *frp = (unsigned char *) &fres;
	    unsigned char *frpb = (unsigned char *) &fresb;
	    unsigned char *frpbs = (unsigned char *) &fresbs;
	    size_t j;

	    printf ("%s(",fname);
	    for (j = 0; j < sizeof (TSMALL); ++j)
	      printf ("%02x", fvp[j]);
	    printf ("),%s: %s ", rmnames[rmode], TBIG_NAME);
	    for (j = 0; j < sizeof (TBIG); ++j)
	      printf ("%02x", frpb[j]);
	    printf (" (");
	    for (j = 0; j < sizeof (TSMALL); ++j)
	      printf ("%02x", frpbs[j]);
	    printf ("), %s ", TSMALL_NAME);
	    for (j = 0; j < sizeof (TSMALL); ++j)
	      printf ("%02x", frp[j]);
	    printf (" %c\n", warn);
	  }
	}
      return 0;
    }

  nwarn = 0;
  for (i = 0; i < smalltries; i++)
    {
      TSMALL fval;

      fval = RSMALL (rand () ^ rand () << 16);
      REDUCE (fval);
      if (tryone (fval))
	break;
    }

  printf ("%s-small: %d errors, %d (%0.2f%%) inaccuracies\n",
	  fname, nerrors, nwarn,
	  nwarn * 0.25 / ((double) smalltries));

  nwarn = 0;
  for (i = 0; i < largetries; ++i)
    {
      TSMALL fval;

      fval = RSMALL (rand () ^ rand () << 16);
      if (tryone (fval))
	break;
    }

  printf ("%s-large: %d errors, %d (%0.2f%%) inaccuracies\n",
	  fname, nerrors, nwarn,
	  nwarn * 0.25 / ((double) largetries));
  return nerrors == 0;
}

int
main (void)
{
  int r;

  _LIB_VERSION = _IEEE_;

  /* Set up environments for rounding modes.  */
  fesetenv (FE_DFL_ENV);
  fesetround (FE_TONEAREST);
  fegetenv (rmodes + 0);
  fesetround (FE_TOWARDSZERO);
  fegetenv (rmodes + 1);
  fesetround (FE_UPWARD);
  fegetenv (rmodes + 2);
  fesetround (FE_DOWNWARD);
  fegetenv (rmodes + 3);

  /* Seed the RNG.  */
  srand (time (0));

  /* Do it.  */
  r  = checkit ("exp2", exp2f, exp2, 1 << 20, 1 << 15);
  r &= checkit ("exp",  expf,  exp,  1 << 20, 1 << 15);
  return r ? 0 : 1;
}