blob: 1bb51cf7e34264362a36f390e3da8f77e66752fd [file] [log] [blame]
Guido van Rossum52fa3a61997-02-14 22:59:58 +00001/*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002 ---------------------------------------------------------------------
3 / Copyright (c) 1996. \
Guido van Rossum52fa3a61997-02-14 22:59:58 +00004 | The Regents of the University of California. |
5 | All rights reserved. |
6 | |
7 | Permission to use, copy, modify, and distribute this software for |
8 | any purpose without fee is hereby granted, provided that this en- |
9 | tire notice is included in all copies of any software which is or |
10 | includes a copy or modification of this software and in all |
11 | copies of the supporting documentation for such software. |
12 | |
13 | This work was produced at the University of California, Lawrence |
14 | Livermore National Laboratory under contract no. W-7405-ENG-48 |
15 | between the U.S. Department of Energy and The Regents of the |
16 | University of California for the operation of UC LLNL. |
17 | |
18 | DISCLAIMER |
19 | |
20 | This software was prepared as an account of work sponsored by an |
21 | agency of the United States Government. Neither the United States |
22 | Government nor the University of California nor any of their em- |
23 | ployees, makes any warranty, express or implied, or assumes any |
24 | liability or responsibility for the accuracy, completeness, or |
25 | usefulness of any information, apparatus, product, or process |
26 | disclosed, or represents that its use would not infringe |
27 | privately-owned rights. Reference herein to any specific commer- |
28 | cial products, process, or service by trade name, trademark, |
29 | manufacturer, or otherwise, does not necessarily constitute or |
30 | imply its endorsement, recommendation, or favoring by the United |
31 | States Government or the University of California. The views and |
32 | opinions of authors expressed herein do not necessarily state or |
33 | reflect those of the United States Government or the University |
34 | of California, and shall not be used for advertising or product |
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 \ endorsement purposes. /
36 ---------------------------------------------------------------------
Guido van Rossum52fa3a61997-02-14 22:59:58 +000037*/
38
39/*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 Floating point exception control module.
Guido van Rossum52fa3a61997-02-14 22:59:58 +000041
42 This Python module provides bare-bones control over floating point
43 units from several hardware manufacturers. Specifically, it allows
44 the user to turn on the generation of SIGFPE whenever any of the
45 three serious IEEE 754 exceptions (Division by Zero, Overflow,
46 Invalid Operation) occurs. We currently ignore Underflow and
47 Inexact Result exceptions, although those could certainly be added
48 if desired.
49
50 The module also establishes a signal handler for SIGFPE during
51 initialization. This builds on code found in the Python
52 distribution at Include/pyfpe.h and Python/pyfpe.c. If those files
53 are not in your Python distribution, find them in a patch at
54 ftp://icf.llnl.gov/pub/python/busby/patches.961108.tgz.
55
56 This module is only useful to you if it happens to include code
57 specific for your hardware and software environment. If you can
58 contribute OS-specific code for new platforms, or corrections for
59 the code provided, it will be greatly appreciated.
60
61 ** Version 1.0: September 20, 1996. Lee Busby, LLNL.
62 */
63
64#ifdef __cplusplus
65extern "C" {
66#endif
67
68#include "Python.h"
69#include <signal.h>
70
Guido van Rossum4622e142000-09-19 13:35:40 +000071#if defined(__FreeBSD__)
72# include <ieeefp.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000073#elif defined(__VMS)
74#define __NEW_STARLET
75#include <starlet.h>
76#include <ieeedef.h>
Guido van Rossum4622e142000-09-19 13:35:40 +000077#endif
78
Guido van Rossum52fa3a61997-02-14 22:59:58 +000079#ifndef WANT_SIGFPE_HANDLER
80/* Define locally if they are not defined in Python. This gives only
81 * the limited control to induce a core dump in case of an exception.
82 */
Guido van Rossum1aeb1041997-03-14 04:32:25 +000083#include <setjmp.h>
Guido van Rossum52fa3a61997-02-14 22:59:58 +000084static jmp_buf PyFPE_jbuf;
85static int PyFPE_counter = 0;
86#endif
87
Tim Peters4f1b2082000-07-23 21:18:09 +000088typedef void Sigfunc(int);
Guido van Rossum52fa3a61997-02-14 22:59:58 +000089static Sigfunc sigfpe_handler;
90static void fpe_reset(Sigfunc *);
91
92static PyObject *fpe_error;
Martin v. Löwis1a214512008-06-11 05:26:20 +000093
94PyMODINIT_FUNC PyInit_fpectl(void);
Guido van Rossum52fa3a61997-02-14 22:59:58 +000095static PyObject *turnon_sigfpe (PyObject *self,PyObject *args);
96static PyObject *turnoff_sigfpe (PyObject *self,PyObject *args);
97
98static PyMethodDef fpectl_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 {"turnon_sigfpe", (PyCFunction) turnon_sigfpe, METH_VARARGS},
100 {"turnoff_sigfpe", (PyCFunction) turnoff_sigfpe, METH_VARARGS},
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000101 {0,0}
102};
103
104static PyObject *turnon_sigfpe(PyObject *self,PyObject *args)
105{
106 /* Do any architecture-specific one-time only initialization here. */
107
108 fpe_reset(sigfpe_handler);
109 Py_INCREF (Py_None);
110 return Py_None;
111}
112
113static void fpe_reset(Sigfunc *handler)
114{
115 /* Reset the exception handling machinery, and reset the signal
116 * handler for SIGFPE to the given handler.
117 */
118
119/*-- IRIX -----------------------------------------------------------------*/
120#if defined(sgi)
121 /* See man page on handle_sigfpes -- must link with -lfpe
122 * My usage doesn't follow the man page exactly. Maybe somebody
123 * else can explain handle_sigfpes to me....
124 * cc -c -I/usr/local/python/include fpectlmodule.c
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 * ld -shared -o fpectlmodule.so fpectlmodule.o -lfpe
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000126 */
127#include <sigfpe.h>
128 typedef void user_routine (unsigned[5], int[2]);
129 typedef void abort_routine (unsigned long);
130 handle_sigfpes(_OFF, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 (user_routine *)0,
132 _TURN_OFF_HANDLER_ON_ERROR,
133 NULL);
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000134 handle_sigfpes(_ON, _EN_OVERFL | _EN_DIVZERO | _EN_INVALID,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 (user_routine *)0,
136 _ABORT_ON_ERROR,
137 NULL);
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000138 PyOS_setsig(SIGFPE, handler);
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000139
140/*-- SunOS and Solaris ----------------------------------------------------*/
141#elif defined(sun)
142 /* References: ieee_handler, ieee_sun, ieee_functions, and ieee_flags
143 man pages (SunOS or Solaris)
144 cc -c -I/usr/local/python/include fpectlmodule.c
145 ld -G -o fpectlmodule.so -L/opt/SUNWspro/lib fpectlmodule.o -lsunmath -lm
146 */
147#include <math.h>
Martin v. Löwisc4db4762001-03-07 10:22:20 +0000148#ifndef _SUNMATH_H
149 extern void nonstandard_arithmetic(void);
150 extern int ieee_flags(const char*, const char*, const char*, char **);
151 extern long ieee_handler(const char*, const char*, sigfpe_handler_type);
152#endif
153
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000154 char *mode="exception", *in="all", *out;
155 (void) nonstandard_arithmetic();
156 (void) ieee_flags("clearall",mode,in,&out);
157 (void) ieee_handler("set","common",(sigfpe_handler_type)handler);
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000158 PyOS_setsig(SIGFPE, handler);
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000159
160/*-- HPUX -----------------------------------------------------------------*/
161#elif defined(__hppa) || defined(hppa)
162 /* References: fpsetmask man page */
163 /* cc -Aa +z -c -I/usr/local/python/include fpectlmodule.c */
164 /* ld -b -o fpectlmodule.sl fpectlmodule.o -lm */
165#include <math.h>
166 fpsetdefaults();
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000167 PyOS_setsig(SIGFPE, handler);
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000168
169/*-- IBM AIX --------------------------------------------------------------*/
170#elif defined(__AIX) || defined(_AIX)
171 /* References: fp_trap, fp_enable man pages */
172#include <fptrap.h>
173 fp_trap(FP_TRAP_SYNC);
174 fp_enable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW);
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000175 PyOS_setsig(SIGFPE, handler);
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000176
177/*-- DEC ALPHA OSF --------------------------------------------------------*/
Guido van Rossuma7379d91999-04-19 16:50:26 +0000178#elif defined(__alpha) && defined(__osf__)
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000179 /* References: exception_intro, ieee man pages */
180 /* cc -c -I/usr/local/python/include fpectlmodule.c */
181 /* ld -shared -o fpectlmodule.so fpectlmodule.o */
182#include <machine/fpu.h>
183 unsigned long fp_control =
184 IEEE_TRAP_ENABLE_INV | IEEE_TRAP_ENABLE_DZE | IEEE_TRAP_ENABLE_OVF;
185 ieee_set_fp_control(fp_control);
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000186 PyOS_setsig(SIGFPE, handler);
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000187
Guido van Rossum9b1a80b2002-09-26 16:52:02 +0000188/*-- DEC ALPHA LINUX ------------------------------------------------------*/
189#elif defined(__alpha) && defined(linux)
190#include <asm/fpu.h>
191 unsigned long fp_control =
192 IEEE_TRAP_ENABLE_INV | IEEE_TRAP_ENABLE_DZE | IEEE_TRAP_ENABLE_OVF;
193 ieee_set_fp_control(fp_control);
194 PyOS_setsig(SIGFPE, handler);
195
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000196/*-- DEC ALPHA VMS --------------------------------------------------------*/
197#elif defined(__ALPHA) && defined(__VMS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 IEEE clrmsk;
199 IEEE setmsk;
200 clrmsk.ieee$q_flags =
201 IEEE$M_TRAP_ENABLE_UNF | IEEE$M_TRAP_ENABLE_INE |
202 IEEE$M_MAP_UMZ;
203 setmsk.ieee$q_flags =
204 IEEE$M_TRAP_ENABLE_INV | IEEE$M_TRAP_ENABLE_DZE |
205 IEEE$M_TRAP_ENABLE_OVF;
206 sys$ieee_set_fp_control(&clrmsk, &setmsk, 0);
207 PyOS_setsig(SIGFPE, handler);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000208
209/*-- HP IA64 VMS --------------------------------------------------------*/
210#elif defined(__ia64) && defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000211 PyOS_setsig(SIGFPE, handler);
212
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000213/*-- Cray Unicos ----------------------------------------------------------*/
214#elif defined(cray)
215 /* UNICOS delivers SIGFPE by default, but no matherr */
216#ifdef HAS_LIBMSET
217 libmset(-1);
218#endif
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000219 PyOS_setsig(SIGFPE, handler);
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000220
Guido van Rossum4622e142000-09-19 13:35:40 +0000221/*-- FreeBSD ----------------------------------------------------------------*/
222#elif defined(__FreeBSD__)
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000223 fpresetsticky(fpgetsticky());
224 fpsetmask(FP_X_INV | FP_X_DZ | FP_X_OFL);
225 PyOS_setsig(SIGFPE, handler);
Guido van Rossum4622e142000-09-19 13:35:40 +0000226
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000227/*-- Linux ----------------------------------------------------------------*/
228#elif defined(linux)
Guido van Rossumaa6fa6b1997-12-15 18:07:10 +0000229#ifdef __GLIBC__
230#include <fpu_control.h>
231#else
Guido van Rossume85da651997-10-20 23:50:01 +0000232#include <i386/fpu_control.h>
Guido van Rossumaa6fa6b1997-12-15 18:07:10 +0000233#endif
Andrew M. Kuchling5a571632001-01-04 01:01:12 +0000234#ifdef _FPU_SETCW
235 {
236 fpu_control_t cw = 0x1372;
237 _FPU_SETCW(cw);
238 }
239#else
Guido van Rossume85da651997-10-20 23:50:01 +0000240 __setfpucw(0x1372);
Andrew M. Kuchling5a571632001-01-04 01:01:12 +0000241#endif
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000242 PyOS_setsig(SIGFPE, handler);
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000243
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000244/*-- Microsoft Windows, NT ------------------------------------------------*/
245#elif defined(_MSC_VER)
246 /* Reference: Visual C++ Books Online 4.2,
247 Run-Time Library Reference, _control87, _controlfp */
248#include <float.h>
249 unsigned int cw = _EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW;
250 (void)_controlfp(0, cw);
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000251 PyOS_setsig(SIGFPE, handler);
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000252
253/*-- Give Up --------------------------------------------------------------*/
254#else
255 fputs("Operation not implemented\n", stderr);
256#endif
257
258}
259
260static PyObject *turnoff_sigfpe(PyObject *self,PyObject *args)
261{
Guido van Rossum4622e142000-09-19 13:35:40 +0000262#ifdef __FreeBSD__
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000263 fpresetsticky(fpgetsticky());
264 fpsetmask(0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000265#elif defined(__VMS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 IEEE clrmsk;
267 clrmsk.ieee$q_flags =
268 IEEE$M_TRAP_ENABLE_UNF | IEEE$M_TRAP_ENABLE_INE |
269 IEEE$M_MAP_UMZ | IEEE$M_TRAP_ENABLE_INV |
270 IEEE$M_TRAP_ENABLE_DZE | IEEE$M_TRAP_ENABLE_OVF |
271 IEEE$M_INHERIT;
272 sys$ieee_set_fp_control(&clrmsk, 0, 0);
Guido van Rossum4622e142000-09-19 13:35:40 +0000273#else
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000274 fputs("Operation not implemented\n", stderr);
Guido van Rossum4622e142000-09-19 13:35:40 +0000275#endif
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000276 Py_INCREF(Py_None);
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000277 return Py_None;
278}
279
280static void sigfpe_handler(int signo)
281{
282 fpe_reset(sigfpe_handler);
283 if(PyFPE_counter) {
284 longjmp(PyFPE_jbuf, 1);
285 } else {
286 Py_FatalError("Unprotected floating point exception");
287 }
288}
289
Martin v. Löwis1a214512008-06-11 05:26:20 +0000290static struct PyModuleDef fpectlmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 PyModuleDef_HEAD_INIT,
292 "fpectl",
293 NULL,
294 -1,
295 fpectl_methods,
296 NULL,
297 NULL,
298 NULL,
299 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000300};
301
302PyMODINIT_FUNC PyInit_fpectl(void)
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000303{
304 PyObject *m, *d;
Matthias Klose1ff110d2008-11-26 17:18:22 +0000305 m = PyModule_Create(&fpectlmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000306 if (m == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 return NULL;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000308 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000309 fpe_error = PyErr_NewException("fpectl.error", NULL, NULL);
310 if (fpe_error != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 PyDict_SetItemString(d, "error", fpe_error);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000312 return m;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000313}
314
315#ifdef __cplusplus
316}
317#endif