-
Notifications
You must be signed in to change notification settings - Fork 17
/
fpehandler.c
132 lines (91 loc) · 3.51 KB
/
fpehandler.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
/* ------- file: -------------------------- fpehandler.c ------------
Version: rh2.0
Author: Han Uitenbroek ([email protected])
Last modified: Mon Nov 22 15:09:15 2010 --
-------------------------- ----------RH-- */
/* --- Trap floating point exceptions on various machines:
-- -------------- */
#include <math.h>
#include <stdio.h>
#include "rh.h"
#include "error.h"
extern char messageStr[];
#if defined(SETNOTRAPS)
/* --- If SETNOTRAPS has been defined (see Makefile) -- ------------- */
void SetFPEtraps(void)
{
/* --- Explicitly do not set traps -- -------------- */
Error(MESSAGE, "SetFPEtraps",
"\nFPE traps have not been set explicitly\n");
}
/* --- end SETNOTRAPS -- -------------- */
#else
/* --- SunOS -- -------------- */
#if defined(SunOS)
/* --- Traps floating point exceptions for SunOS5.
OS: SunOS5
see: ``man ieee_handler''
Requires linking with -lsunmath
-- -------------- */
#include <stdlib.h>
#include <sunmath.h>
#include <siginfo.h>
#include <ucontext.h>
void Trapped_FPE_Exception(int sig, siginfo_t *sip, ucontext_t *uap);
void SetFPEtraps(void)
{
const char routineName[] = "SetFPEtraps";
if (ieee_handler("set", "common",
(sigfpe_handler_type) Trapped_FPE_Exception) != 0)
Error(MESSAGE, routineName, "IEEE trapping not supported here\n");
else
Error(MESSAGE, routineName,
"\n-Setting FPE traps for sparc (SunOS 5.x)\n");
}
void Trapped_FPE_Exception(int sig, siginfo_t *sip, ucontext_t *uap)
{
const char routineName[] = "Trapped_FPE_Exception";
char *type = "unknown";
switch(sip->si_code) {
case FPE_INTDIV: type = "integer divide by zero "; break;
case FPE_INTOVF: type = "integer overflow "; break;
case FPE_FLTDIV: type = "floating point division by zero "; break;
case FPE_FLTUND: type = "floating point underflow "; break;
case FPE_FLTOVF: type = "floating point overflow "; break;
case FPE_FLTRES: type = "floating point inexact "; break;
case FPE_FLTINV: type = "invalid floating point operation"; break;
case FPE_FLTSUB: type = "subscript out of range "; break;
}
sprintf(messageStr, " ---- trapped IEEE FPE: %s ----\n"
" ---- signal: %d, code: 0x%x, aborting ----\n",
type, sig, sip->si_code);
Error(MESSAGE, routineName, messageStr);
abort();
}
/* --- end SunOS -- -------------- */
/* --- Linux -- -------------- */
#elif defined(Linux)
#define _GNU_SOURCE 1
#include <fenv.h>
void SetFPEtraps(void)
{
/* --- Enable some exceptions.
At startup all exceptions are masked. -- -------------- */
/* Tiago: commented this out for the 1.5D version (where it should
not stop execution!)
feenableexcept(FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW); */
}
/* --- end Linux -- -------------- */
#else
/* --- unknown -- -------------- */
void SetFPEtraps(void)
{
/*
Error(MESSAGE, "SetFPEtraps",
"\nUnsupported CPU and/or OS: cannot set FPE traps explicitly\n");
*/
}
#endif
/* --- end else -- -------------- */
#endif
/* ------- end ------------------------------------------------------ */