1 | n/a | /* |
---|
2 | n/a | --------------------------------------------------------------------- |
---|
3 | n/a | / Copyright (c) 1996. \ |
---|
4 | n/a | | The Regents of the University of California. | |
---|
5 | n/a | | All rights reserved. | |
---|
6 | n/a | | | |
---|
7 | n/a | | Permission to use, copy, modify, and distribute this software for | |
---|
8 | n/a | | any purpose without fee is hereby granted, provided that this en- | |
---|
9 | n/a | | tire notice is included in all copies of any software which is or | |
---|
10 | n/a | | includes a copy or modification of this software and in all | |
---|
11 | n/a | | copies of the supporting documentation for such software. | |
---|
12 | n/a | | | |
---|
13 | n/a | | This work was produced at the University of California, Lawrence | |
---|
14 | n/a | | Livermore National Laboratory under contract no. W-7405-ENG-48 | |
---|
15 | n/a | | between the U.S. Department of Energy and The Regents of the | |
---|
16 | n/a | | University of California for the operation of UC LLNL. | |
---|
17 | n/a | | | |
---|
18 | n/a | | DISCLAIMER | |
---|
19 | n/a | | | |
---|
20 | n/a | | This software was prepared as an account of work sponsored by an | |
---|
21 | n/a | | agency of the United States Government. Neither the United States | |
---|
22 | n/a | | Government nor the University of California nor any of their em- | |
---|
23 | n/a | | ployees, makes any warranty, express or implied, or assumes any | |
---|
24 | n/a | | liability or responsibility for the accuracy, completeness, or | |
---|
25 | n/a | | usefulness of any information, apparatus, product, or process | |
---|
26 | n/a | | disclosed, or represents that its use would not infringe | |
---|
27 | n/a | | privately-owned rights. Reference herein to any specific commer- | |
---|
28 | n/a | | cial products, process, or service by trade name, trademark, | |
---|
29 | n/a | | manufacturer, or otherwise, does not necessarily constitute or | |
---|
30 | n/a | | imply its endorsement, recommendation, or favoring by the United | |
---|
31 | n/a | | States Government or the University of California. The views and | |
---|
32 | n/a | | opinions of authors expressed herein do not necessarily state or | |
---|
33 | n/a | | reflect those of the United States Government or the University | |
---|
34 | n/a | | of California, and shall not be used for advertising or product | |
---|
35 | n/a | \ endorsement purposes. / |
---|
36 | n/a | --------------------------------------------------------------------- |
---|
37 | n/a | */ |
---|
38 | n/a | |
---|
39 | n/a | /* |
---|
40 | n/a | Floating point exception control module. |
---|
41 | n/a | |
---|
42 | n/a | This Python module provides bare-bones control over floating point |
---|
43 | n/a | units from several hardware manufacturers. Specifically, it allows |
---|
44 | n/a | the user to turn on the generation of SIGFPE whenever any of the |
---|
45 | n/a | three serious IEEE 754 exceptions (Division by Zero, Overflow, |
---|
46 | n/a | Invalid Operation) occurs. We currently ignore Underflow and |
---|
47 | n/a | Inexact Result exceptions, although those could certainly be added |
---|
48 | n/a | if desired. |
---|
49 | n/a | |
---|
50 | n/a | The module also establishes a signal handler for SIGFPE during |
---|
51 | n/a | initialization. This builds on code found in the Python |
---|
52 | n/a | distribution at Include/pyfpe.h and Python/pyfpe.c. If those files |
---|
53 | n/a | are not in your Python distribution, find them in a patch at |
---|
54 | n/a | ftp://icf.llnl.gov/pub/python/busby/patches.961108.tgz. |
---|
55 | n/a | |
---|
56 | n/a | This module is only useful to you if it happens to include code |
---|
57 | n/a | specific for your hardware and software environment. If you can |
---|
58 | n/a | contribute OS-specific code for new platforms, or corrections for |
---|
59 | n/a | the code provided, it will be greatly appreciated. |
---|
60 | n/a | |
---|
61 | n/a | ** Version 1.0: September 20, 1996. Lee Busby, LLNL. |
---|
62 | n/a | */ |
---|
63 | n/a | |
---|
64 | n/a | #ifdef __cplusplus |
---|
65 | n/a | extern "C" { |
---|
66 | n/a | #endif |
---|
67 | n/a | |
---|
68 | n/a | #include "Python.h" |
---|
69 | n/a | #include <signal.h> |
---|
70 | n/a | |
---|
71 | n/a | #if defined(__FreeBSD__) |
---|
72 | n/a | # include <ieeefp.h> |
---|
73 | n/a | #endif |
---|
74 | n/a | |
---|
75 | n/a | #ifndef WANT_SIGFPE_HANDLER |
---|
76 | n/a | /* Define locally if they are not defined in Python. This gives only |
---|
77 | n/a | * the limited control to induce a core dump in case of an exception. |
---|
78 | n/a | */ |
---|
79 | n/a | #include <setjmp.h> |
---|
80 | n/a | static jmp_buf PyFPE_jbuf; |
---|
81 | n/a | static int PyFPE_counter = 0; |
---|
82 | n/a | #endif |
---|
83 | n/a | |
---|
84 | n/a | typedef void Sigfunc(int); |
---|
85 | n/a | static Sigfunc sigfpe_handler; |
---|
86 | n/a | static void fpe_reset(Sigfunc *); |
---|
87 | n/a | |
---|
88 | n/a | static PyObject *fpe_error; |
---|
89 | n/a | |
---|
90 | n/a | PyMODINIT_FUNC PyInit_fpectl(void); |
---|
91 | n/a | static PyObject *turnon_sigfpe (PyObject *self,PyObject *args); |
---|
92 | n/a | static PyObject *turnoff_sigfpe (PyObject *self,PyObject *args); |
---|
93 | n/a | |
---|
94 | n/a | static PyMethodDef fpectl_methods[] = { |
---|
95 | n/a | {"turnon_sigfpe", (PyCFunction) turnon_sigfpe, METH_VARARGS}, |
---|
96 | n/a | {"turnoff_sigfpe", (PyCFunction) turnoff_sigfpe, METH_VARARGS}, |
---|
97 | n/a | {0,0} |
---|
98 | n/a | }; |
---|
99 | n/a | |
---|
100 | n/a | static PyObject *turnon_sigfpe(PyObject *self,PyObject *args) |
---|
101 | n/a | { |
---|
102 | n/a | /* Do any architecture-specific one-time only initialization here. */ |
---|
103 | n/a | |
---|
104 | n/a | fpe_reset(sigfpe_handler); |
---|
105 | n/a | Py_RETURN_NONE; |
---|
106 | n/a | } |
---|
107 | n/a | |
---|
108 | n/a | static void fpe_reset(Sigfunc *handler) |
---|
109 | n/a | { |
---|
110 | n/a | /* Reset the exception handling machinery, and reset the signal |
---|
111 | n/a | * handler for SIGFPE to the given handler. |
---|
112 | n/a | */ |
---|
113 | n/a | |
---|
114 | n/a | /*-- IRIX -----------------------------------------------------------------*/ |
---|
115 | n/a | #if defined(sgi) |
---|
116 | n/a | /* See man page on handle_sigfpes -- must link with -lfpe |
---|
117 | n/a | * My usage doesn't follow the man page exactly. Maybe somebody |
---|
118 | n/a | * else can explain handle_sigfpes to me.... |
---|
119 | n/a | * cc -c -I/usr/local/python/include fpectlmodule.c |
---|
120 | n/a | * ld -shared -o fpectlmodule.so fpectlmodule.o -lfpe |
---|
121 | n/a | */ |
---|
122 | n/a | #include <sigfpe.h> |
---|
123 | n/a | typedef void user_routine (unsigned[5], int[2]); |
---|
124 | n/a | typedef void abort_routine (unsigned long); |
---|
125 | n/a | handle_sigfpes(_OFF, 0, |
---|
126 | n/a | (user_routine *)0, |
---|
127 | n/a | _TURN_OFF_HANDLER_ON_ERROR, |
---|
128 | n/a | NULL); |
---|
129 | n/a | handle_sigfpes(_ON, _EN_OVERFL | _EN_DIVZERO | _EN_INVALID, |
---|
130 | n/a | (user_routine *)0, |
---|
131 | n/a | _ABORT_ON_ERROR, |
---|
132 | n/a | NULL); |
---|
133 | n/a | PyOS_setsig(SIGFPE, handler); |
---|
134 | n/a | |
---|
135 | n/a | /*-- SunOS and Solaris ----------------------------------------------------*/ |
---|
136 | n/a | #elif defined(sun) |
---|
137 | n/a | /* References: ieee_handler, ieee_sun, ieee_functions, and ieee_flags |
---|
138 | n/a | man pages (SunOS or Solaris) |
---|
139 | n/a | cc -c -I/usr/local/python/include fpectlmodule.c |
---|
140 | n/a | ld -G -o fpectlmodule.so -L/opt/SUNWspro/lib fpectlmodule.o -lsunmath -lm |
---|
141 | n/a | */ |
---|
142 | n/a | #include <math.h> |
---|
143 | n/a | #ifndef _SUNMATH_H |
---|
144 | n/a | extern void nonstandard_arithmetic(void); |
---|
145 | n/a | extern int ieee_flags(const char*, const char*, const char*, char **); |
---|
146 | n/a | extern long ieee_handler(const char*, const char*, sigfpe_handler_type); |
---|
147 | n/a | #endif |
---|
148 | n/a | |
---|
149 | n/a | char *mode="exception", *in="all", *out; |
---|
150 | n/a | (void) nonstandard_arithmetic(); |
---|
151 | n/a | (void) ieee_flags("clearall",mode,in,&out); |
---|
152 | n/a | (void) ieee_handler("set","common",(sigfpe_handler_type)handler); |
---|
153 | n/a | PyOS_setsig(SIGFPE, handler); |
---|
154 | n/a | |
---|
155 | n/a | /*-- HPUX -----------------------------------------------------------------*/ |
---|
156 | n/a | #elif defined(__hppa) || defined(hppa) |
---|
157 | n/a | /* References: fpsetmask man page */ |
---|
158 | n/a | /* cc -Aa +z -c -I/usr/local/python/include fpectlmodule.c */ |
---|
159 | n/a | /* ld -b -o fpectlmodule.sl fpectlmodule.o -lm */ |
---|
160 | n/a | #include <math.h> |
---|
161 | n/a | fpsetdefaults(); |
---|
162 | n/a | PyOS_setsig(SIGFPE, handler); |
---|
163 | n/a | |
---|
164 | n/a | /*-- IBM AIX --------------------------------------------------------------*/ |
---|
165 | n/a | #elif defined(__AIX) || defined(_AIX) |
---|
166 | n/a | /* References: fp_trap, fp_enable man pages */ |
---|
167 | n/a | #include <fptrap.h> |
---|
168 | n/a | fp_trap(FP_TRAP_SYNC); |
---|
169 | n/a | fp_enable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW); |
---|
170 | n/a | PyOS_setsig(SIGFPE, handler); |
---|
171 | n/a | |
---|
172 | n/a | /*-- DEC ALPHA LINUX ------------------------------------------------------*/ |
---|
173 | n/a | #elif defined(__alpha) && defined(linux) |
---|
174 | n/a | #include <asm/fpu.h> |
---|
175 | n/a | unsigned long fp_control = |
---|
176 | n/a | IEEE_TRAP_ENABLE_INV | IEEE_TRAP_ENABLE_DZE | IEEE_TRAP_ENABLE_OVF; |
---|
177 | n/a | ieee_set_fp_control(fp_control); |
---|
178 | n/a | PyOS_setsig(SIGFPE, handler); |
---|
179 | n/a | |
---|
180 | n/a | /*-- Cray Unicos ----------------------------------------------------------*/ |
---|
181 | n/a | #elif defined(cray) |
---|
182 | n/a | /* UNICOS delivers SIGFPE by default, but no matherr */ |
---|
183 | n/a | #ifdef HAS_LIBMSET |
---|
184 | n/a | libmset(-1); |
---|
185 | n/a | #endif |
---|
186 | n/a | PyOS_setsig(SIGFPE, handler); |
---|
187 | n/a | |
---|
188 | n/a | /*-- FreeBSD ----------------------------------------------------------------*/ |
---|
189 | n/a | #elif defined(__FreeBSD__) |
---|
190 | n/a | fpresetsticky(fpgetsticky()); |
---|
191 | n/a | fpsetmask(FP_X_INV | FP_X_DZ | FP_X_OFL); |
---|
192 | n/a | PyOS_setsig(SIGFPE, handler); |
---|
193 | n/a | |
---|
194 | n/a | /*-- Linux ----------------------------------------------------------------*/ |
---|
195 | n/a | #elif defined(linux) |
---|
196 | n/a | #ifdef __GLIBC__ |
---|
197 | n/a | #include <fpu_control.h> |
---|
198 | n/a | #else |
---|
199 | n/a | #include <i386/fpu_control.h> |
---|
200 | n/a | #endif |
---|
201 | n/a | #ifdef _FPU_SETCW |
---|
202 | n/a | { |
---|
203 | n/a | fpu_control_t cw = 0x1372; |
---|
204 | n/a | _FPU_SETCW(cw); |
---|
205 | n/a | } |
---|
206 | n/a | #else |
---|
207 | n/a | __setfpucw(0x1372); |
---|
208 | n/a | #endif |
---|
209 | n/a | PyOS_setsig(SIGFPE, handler); |
---|
210 | n/a | |
---|
211 | n/a | /*-- Microsoft Windows, NT ------------------------------------------------*/ |
---|
212 | n/a | #elif defined(_MSC_VER) |
---|
213 | n/a | /* Reference: Visual C++ Books Online 4.2, |
---|
214 | n/a | Run-Time Library Reference, _control87, _controlfp */ |
---|
215 | n/a | #include <float.h> |
---|
216 | n/a | unsigned int cw = _EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW; |
---|
217 | n/a | (void)_controlfp(0, cw); |
---|
218 | n/a | PyOS_setsig(SIGFPE, handler); |
---|
219 | n/a | |
---|
220 | n/a | /*-- Give Up --------------------------------------------------------------*/ |
---|
221 | n/a | #else |
---|
222 | n/a | fputs("Operation not implemented\n", stderr); |
---|
223 | n/a | #endif |
---|
224 | n/a | |
---|
225 | n/a | } |
---|
226 | n/a | |
---|
227 | n/a | static PyObject *turnoff_sigfpe(PyObject *self,PyObject *args) |
---|
228 | n/a | { |
---|
229 | n/a | #ifdef __FreeBSD__ |
---|
230 | n/a | fpresetsticky(fpgetsticky()); |
---|
231 | n/a | fpsetmask(0); |
---|
232 | n/a | #else |
---|
233 | n/a | fputs("Operation not implemented\n", stderr); |
---|
234 | n/a | #endif |
---|
235 | n/a | Py_RETURN_NONE; |
---|
236 | n/a | } |
---|
237 | n/a | |
---|
238 | n/a | static void sigfpe_handler(int signo) |
---|
239 | n/a | { |
---|
240 | n/a | fpe_reset(sigfpe_handler); |
---|
241 | n/a | if(PyFPE_counter) { |
---|
242 | n/a | longjmp(PyFPE_jbuf, 1); |
---|
243 | n/a | } else { |
---|
244 | n/a | Py_FatalError("Unprotected floating point exception"); |
---|
245 | n/a | } |
---|
246 | n/a | } |
---|
247 | n/a | |
---|
248 | n/a | static struct PyModuleDef fpectlmodule = { |
---|
249 | n/a | PyModuleDef_HEAD_INIT, |
---|
250 | n/a | "fpectl", |
---|
251 | n/a | NULL, |
---|
252 | n/a | -1, |
---|
253 | n/a | fpectl_methods, |
---|
254 | n/a | NULL, |
---|
255 | n/a | NULL, |
---|
256 | n/a | NULL, |
---|
257 | n/a | NULL |
---|
258 | n/a | }; |
---|
259 | n/a | |
---|
260 | n/a | PyMODINIT_FUNC PyInit_fpectl(void) |
---|
261 | n/a | { |
---|
262 | n/a | PyObject *m, *d; |
---|
263 | n/a | m = PyModule_Create(&fpectlmodule); |
---|
264 | n/a | if (m == NULL) |
---|
265 | n/a | return NULL; |
---|
266 | n/a | d = PyModule_GetDict(m); |
---|
267 | n/a | fpe_error = PyErr_NewException("fpectl.error", NULL, NULL); |
---|
268 | n/a | if (fpe_error != NULL) |
---|
269 | n/a | PyDict_SetItemString(d, "error", fpe_error); |
---|
270 | n/a | return m; |
---|
271 | n/a | } |
---|
272 | n/a | |
---|
273 | n/a | #ifdef __cplusplus |
---|
274 | n/a | } |
---|
275 | n/a | #endif |
---|