File: matherr.c

package info (click to toggle)
mawk 1.3.3-17
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 1,484 kB
  • ctags: 1,506
  • sloc: ansic: 13,008; yacc: 994; awk: 629; makefile: 113; sh: 107
file content (277 lines) | stat: -rw-r--r-- 5,246 bytes parent folder | download | duplicates (8)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

/********************************************
matherr.c
copyright 1991, Michael D. Brennan

This is a source file for mawk, an implementation of
the AWK programming language.

Mawk is distributed without warranty under the terms of
the GNU General Public License, version 2, 1991.
********************************************/

/*$Log: matherr.c,v $
 *Revision 1.9  1996/09/01 16:54:35  mike
 *Third try at bug fix for solaris strtod.
 *
 * Revision 1.6  1994/12/18  20:53:43  mike
 * check NetBSD mathlib defines
 *
 * Revision 1.5  1994/12/14  14:48:57  mike
 * add <siginfo.h> include -- sysV doesn't have it inside <signal.h>
 * restore #else that had been removed
 *
 * Revision 1.4  1994/10/11  00:36:17  mike
 * systemVr4 siginfo
 *
 * Revision 1.3  1993/07/17  13:23:04  mike
 * indent and general code cleanup
 *
 * Revision 1.2	 1993/07/04  12:52:03  mike
 * start on autoconfig changes
 *
 * Revision 5.2	 1992/03/31  16:14:44  brennan
 * patch2:
 * TURN_ON_FPE_TRAPS() macro
 * USE_IEEEFP_H macro
 *
 * Revision 5.1	 91/12/05  07:56:18  brennan
 * 1.1 pre-release
 *
*/

#include  "mawk.h"
#include  <math.h>

/* Sets up NetBSD 1.0A for ieee floating point */
#if defined(_LIB_VERSION_TYPE) && defined(_LIB_VERSION) && defined(_IEEE_)
_LIB_VERSION_TYPE _LIB_VERSION = _IEEE_;
#endif

#ifdef	USE_IEEEFP_H
#include <ieeefp.h>
#ifdef   HAVE_STRTOD_OVF_BUG
static fp_except entry_mask ;
static fp_except working_mask ;
#endif
#endif

#ifndef	 TURN_OFF_FPE_TRAPS
#define	 TURN_OFF_FPE_TRAPS()	/* nothing */
#endif

#ifndef	 TURN_ON_FPE_TRAPS
#define	 TURN_ON_FPE_TRAPS()	/* nothing */
#endif

#ifdef  SV_SIGINFO
#include <siginfo.h>
#define  FPE_ZERODIVIDE  FPE_FLTDIV
#define  FPE_OVERFLOW    FPE_FLTOVF
#endif

#ifdef	 FPE_TRAPS_ON
#include <signal.h>

/* machine dependent changes might be needed here */

#ifdef   SV_SIGINFO
static void
fpe_catch(signal, sip)
   int signal; 
   siginfo_t *sip ;
{
   int why = sip->si_code ;

#else

static void
fpe_catch(signal, why)
   int signal, why ;
{
#endif /* SV_SIGINFO  */

#if   NOINFO_SIGFPE
   rt_error("floating point exception, probably overflow") ;
   /* does not return */
#else

   switch (why)
   {
      case FPE_ZERODIVIDE:
	 rt_error("division by zero") ;

      case FPE_OVERFLOW:
	 rt_error("floating point overflow") ;

      default:
	 rt_error("floating point exception") ;
   }
#endif /* noinfo_sigfpe */
}

void
fpe_init()
{
   TURN_ON_FPE_TRAPS() ;

#ifndef  SV_SIGINFO
   signal(SIGFPE, fpe_catch) ;

#else
   { struct sigaction x ;

     memset(&x, 0, sizeof(x)) ;
     x.sa_handler = fpe_catch ;
     x.sa_flags = SA_SIGINFO ;

     sigaction(SIGFPE, &x, (struct sigaction*)0) ;
   }
#endif

#ifdef  HAVE_STRTOD_OVF_BUG
   /* we've already turned the traps on */
   working_mask = fpgetmask() ;
   entry_mask = working_mask & ~FP_X_DZ & ~FP_X_OFL ;
#endif
}

#else /* FPE_TRAPS not defined */

void
fpe_init()
{
   TURN_OFF_FPE_TRAPS() ;
}
#endif

#ifndef	 NO_MATHERR

#ifndef	 FPE_TRAPS_ON

/* If we are not trapping math errors, we will shutup the library calls
*/

int
matherr(e)
   struct exception *e ;
{
   return 1 ;
}

#else /* print error message and exit */

int
matherr(e)
   struct exception *e ;
{
   char *error ;

   switch (e->type)
   {
      case DOMAIN:
      case SING:
	 error = "domain error" ;
	 break ;

      case OVERFLOW:
	 error = "overflow" ;
	 break ;

      case TLOSS:
      case PLOSS:
	 error = "loss of significance" ;
	 break ;

      case UNDERFLOW:
	 e->retval = 0.0 ;
	 return 1 ;		 /* ignore it */
   }

   if (strcmp(e->name, "atan2") == 0)  rt_error("atan2(%g,%g) : %s",
	       e->arg1, e->arg2, error) ;
   else	 rt_error("%s(%g) : %s", e->name, e->arg1, error) ;

   /* won't get here */
   return 0 ;
}
#endif /* FPE_TRAPS_ON */

#endif /*  ! no matherr */


/* this is how one gets the libm calls to do the right
thing on bsd43_vax
*/

#ifdef	 BSD43_VAX

#include <errno.h>

double	infnan(arg)
   int arg ;
{
   switch (arg)
   {
	 case  ERANGE : errno = ERANGE ; return HUGE ;
	 case -ERANGE : errno = EDOM ; return -HUGE ;
      default:
	 errno = EDOM ;
   }
   return 0.0 ;
}

#endif /* BSD43_VAX */

/* This routine is for XENIX-68K 2.3A.
    Error check routine to be called after fp arithmetic.
*/

#if SW_FP_CHECK
/* Definitions of bit values in iserr() return value */

#define OVFLOW		2
#define UFLOW		4
#define ZERODIV		8
#define OVFLFIX		32
#define INFNAN		64

void
fpcheck()
{
   register int fperrval ;
   char *errdesc ;

   if ((fperrval = iserr()) == 0)
      return ;			 /* no error */

   errdesc = (char *) 0 ;

   if (fperrval & INFNAN)  errdesc = "arg is infinity or NAN" ;
   else if (fperrval & ZERODIV)	 errdesc = "division by zero" ;
   else if (fperrval & OVFLOW)	errdesc = "overflow" ;
   else if (fperrval & UFLOW) ; /* ignored */

   if (errdesc)	 rt_error("%s", errdesc) ;
}

#endif

#ifdef HAVE_STRTOD_OVF_BUG
/* buggy strtod in solaris, probably any sysv with ieee754
   strtod can generate an fpe  */

double
strtod_with_ovf_bug(s, ep)
   const char *s ;
   char **ep ;
{
   double ret ;

   fpsetmask(entry_mask) ;  /* traps off */
#undef strtod               /* make real strtod visible */
   ret = strtod(s, ep) ;
   fpsetmask(working_mask) ; /* traps on */
   return ret ;
}
#endif