-
Notifications
You must be signed in to change notification settings - Fork 4
/
matherr.c
301 lines (244 loc) · 5.46 KB
/
matherr.c
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/********************************************
matherr.c
copyright 2009-2012,2013 Thomas E. Dickey
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.
********************************************/
/*
* $MawkId: matherr.c,v 1.27 2013/12/27 00:45:11 tom Exp $
*
* @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 "init.h"
#include <math.h>
#ifdef HAVE_SIGACTION_SA_SIGACTION
#define FPE_ARGS int sig, siginfo_t *sip, void *data
#define FPE_DECL int why = sip->si_code
#else
#define FPE_ARGS int sig, int why
#define FPE_DECL /* nothing */
#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 HAVE_SIGINFO_H
#include <siginfo.h>
#define FPE_UNDERFLOW FPE_FLTUND
#endif
#ifdef FPE_TRAPS_ON
#include <signal.h>
#ifndef FPE_FLTDIV
#ifdef WIN32
#include <crt/float.h>
#define FPE_FLTDIV FPE_ZERODIVIDE
#define FPE_FLTOVF FPE_OVERFLOW
#define FPE_FLTUND FPE_UNDERFLOW
#endif
#endif
/* machine dependent changes might be needed here */
static void
fpe_catch(FPE_ARGS)
{
FPE_DECL;
#if defined(NOINFO_SIGFPE)
rt_error("floating point exception, probably overflow");
/* does not return */
#else
switch (why) {
#ifdef FPE_FLTDIV
case FPE_FLTDIV:
rt_error("floating point division by zero");
#endif
#ifdef FPE_FLTOVF
case FPE_FLTOVF:
rt_error("floating point overflow");
#endif
#ifdef FPE_FLTUND
case FPE_FLTUND:
rt_error("floating point underflow");
#endif
default:
rt_error("floating point exception");
}
#endif /* noinfo_sigfpe */
}
void
fpe_init(void)
{
#ifdef HAVE_MATH__LIB_VERSION
_LIB_VERSION = _IEEE_;
#endif
TURN_ON_FPE_TRAPS;
#ifdef HAVE_SIGACTION_SA_SIGACTION
{
struct sigaction x;
memset(&x, 0, sizeof(x));
x.sa_sigaction = fpe_catch;
x.sa_flags = SA_SIGINFO;
sigaction(SIGFPE, &x, (struct sigaction *) 0);
}
#else
signal(SIGFPE, fpe_catch);
#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(void)
{
TURN_OFF_FPE_TRAPS;
}
#endif
#ifdef HAVE_MATHERR
#ifndef FPE_TRAPS_ON
/* If we are not trapping math errors, we will shutup the library calls
*/
struct exception;
int
matherr(struct exception *e)
{
(void) e;
return 1;
}
#else /* print error message and exit */
int
matherr(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
*/
#if defined(BSD43_VAX) || defined(__vax__)
#include <errno.h>
double
infnan(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.
*/
#ifdef 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(void)
{
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(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