Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | --------------------------------------------------------------------- |
| 3 | / Copyright (c) 1996. \ |
| 4 | | 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 | |
| 35 | \ endorsement purposes. / |
| 36 | --------------------------------------------------------------------- |
| 37 | */ |
| 38 | |
| 39 | /* |
| 40 | Floating point exception control module. |
| 41 | |
| 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 |
| 65 | extern "C" { |
| 66 | #endif |
| 67 | |
| 68 | #include "Python.h" |
| 69 | #include <signal.h> |
| 70 | |
Guido van Rossum | 4622e14 | 2000-09-19 13:35:40 +0000 | [diff] [blame] | 71 | #if defined(__FreeBSD__) |
| 72 | # include <ieeefp.h> |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 73 | #elif defined(__VMS) |
| 74 | #define __NEW_STARLET |
| 75 | #include <starlet.h> |
| 76 | #include <ieeedef.h> |
Guido van Rossum | 4622e14 | 2000-09-19 13:35:40 +0000 | [diff] [blame] | 77 | #endif |
| 78 | |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 79 | #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 Rossum | 1aeb104 | 1997-03-14 04:32:25 +0000 | [diff] [blame] | 83 | #include <setjmp.h> |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 84 | static jmp_buf PyFPE_jbuf; |
| 85 | static int PyFPE_counter = 0; |
| 86 | #endif |
| 87 | |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 88 | typedef void Sigfunc(int); |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 89 | static Sigfunc sigfpe_handler; |
| 90 | static void fpe_reset(Sigfunc *); |
| 91 | |
| 92 | static PyObject *fpe_error; |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 93 | PyMODINIT_FUNC initfpectl(void); |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 94 | static PyObject *turnon_sigfpe (PyObject *self,PyObject *args); |
| 95 | static PyObject *turnoff_sigfpe (PyObject *self,PyObject *args); |
| 96 | |
| 97 | static PyMethodDef fpectl_methods[] = { |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 98 | {"turnon_sigfpe", (PyCFunction) turnon_sigfpe, METH_VARARGS}, |
| 99 | {"turnoff_sigfpe", (PyCFunction) turnoff_sigfpe, METH_VARARGS}, |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 100 | {0,0} |
| 101 | }; |
| 102 | |
| 103 | static PyObject *turnon_sigfpe(PyObject *self,PyObject *args) |
| 104 | { |
| 105 | /* Do any architecture-specific one-time only initialization here. */ |
| 106 | |
| 107 | fpe_reset(sigfpe_handler); |
| 108 | Py_INCREF (Py_None); |
| 109 | return Py_None; |
| 110 | } |
| 111 | |
| 112 | static void fpe_reset(Sigfunc *handler) |
| 113 | { |
| 114 | /* Reset the exception handling machinery, and reset the signal |
| 115 | * handler for SIGFPE to the given handler. |
| 116 | */ |
| 117 | |
| 118 | /*-- IRIX -----------------------------------------------------------------*/ |
| 119 | #if defined(sgi) |
| 120 | /* See man page on handle_sigfpes -- must link with -lfpe |
| 121 | * My usage doesn't follow the man page exactly. Maybe somebody |
| 122 | * else can explain handle_sigfpes to me.... |
| 123 | * cc -c -I/usr/local/python/include fpectlmodule.c |
| 124 | * ld -shared -o fpectlmodule.so fpectlmodule.o -lfpe |
| 125 | */ |
| 126 | #include <sigfpe.h> |
| 127 | typedef void user_routine (unsigned[5], int[2]); |
| 128 | typedef void abort_routine (unsigned long); |
| 129 | handle_sigfpes(_OFF, 0, |
| 130 | (user_routine *)0, |
| 131 | _TURN_OFF_HANDLER_ON_ERROR, |
Guido van Rossum | 1ed5e57 | 1997-04-29 21:34:16 +0000 | [diff] [blame] | 132 | NULL); |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 133 | handle_sigfpes(_ON, _EN_OVERFL | _EN_DIVZERO | _EN_INVALID, |
| 134 | (user_routine *)0, |
| 135 | _ABORT_ON_ERROR, |
Guido van Rossum | 1ed5e57 | 1997-04-29 21:34:16 +0000 | [diff] [blame] | 136 | NULL); |
Guido van Rossum | cf06571a | 2000-09-21 14:32:04 +0000 | [diff] [blame] | 137 | PyOS_setsig(SIGFPE, handler); |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 138 | |
| 139 | /*-- SunOS and Solaris ----------------------------------------------------*/ |
| 140 | #elif defined(sun) |
| 141 | /* References: ieee_handler, ieee_sun, ieee_functions, and ieee_flags |
| 142 | man pages (SunOS or Solaris) |
| 143 | cc -c -I/usr/local/python/include fpectlmodule.c |
| 144 | ld -G -o fpectlmodule.so -L/opt/SUNWspro/lib fpectlmodule.o -lsunmath -lm |
| 145 | */ |
| 146 | #include <math.h> |
Martin v. Löwis | c4db476 | 2001-03-07 10:22:20 +0000 | [diff] [blame] | 147 | #ifndef _SUNMATH_H |
| 148 | extern void nonstandard_arithmetic(void); |
| 149 | extern int ieee_flags(const char*, const char*, const char*, char **); |
| 150 | extern long ieee_handler(const char*, const char*, sigfpe_handler_type); |
| 151 | #endif |
| 152 | |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 153 | char *mode="exception", *in="all", *out; |
| 154 | (void) nonstandard_arithmetic(); |
| 155 | (void) ieee_flags("clearall",mode,in,&out); |
| 156 | (void) ieee_handler("set","common",(sigfpe_handler_type)handler); |
Guido van Rossum | cf06571a | 2000-09-21 14:32:04 +0000 | [diff] [blame] | 157 | PyOS_setsig(SIGFPE, handler); |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 158 | |
| 159 | /*-- HPUX -----------------------------------------------------------------*/ |
| 160 | #elif defined(__hppa) || defined(hppa) |
| 161 | /* References: fpsetmask man page */ |
| 162 | /* cc -Aa +z -c -I/usr/local/python/include fpectlmodule.c */ |
| 163 | /* ld -b -o fpectlmodule.sl fpectlmodule.o -lm */ |
| 164 | #include <math.h> |
| 165 | fpsetdefaults(); |
Guido van Rossum | cf06571a | 2000-09-21 14:32:04 +0000 | [diff] [blame] | 166 | PyOS_setsig(SIGFPE, handler); |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 167 | |
| 168 | /*-- IBM AIX --------------------------------------------------------------*/ |
| 169 | #elif defined(__AIX) || defined(_AIX) |
| 170 | /* References: fp_trap, fp_enable man pages */ |
| 171 | #include <fptrap.h> |
| 172 | fp_trap(FP_TRAP_SYNC); |
| 173 | fp_enable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW); |
Guido van Rossum | cf06571a | 2000-09-21 14:32:04 +0000 | [diff] [blame] | 174 | PyOS_setsig(SIGFPE, handler); |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 175 | |
| 176 | /*-- DEC ALPHA OSF --------------------------------------------------------*/ |
Guido van Rossum | a7379d9 | 1999-04-19 16:50:26 +0000 | [diff] [blame] | 177 | #elif defined(__alpha) && defined(__osf__) |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 178 | /* References: exception_intro, ieee man pages */ |
| 179 | /* cc -c -I/usr/local/python/include fpectlmodule.c */ |
| 180 | /* ld -shared -o fpectlmodule.so fpectlmodule.o */ |
| 181 | #include <machine/fpu.h> |
| 182 | unsigned long fp_control = |
| 183 | IEEE_TRAP_ENABLE_INV | IEEE_TRAP_ENABLE_DZE | IEEE_TRAP_ENABLE_OVF; |
| 184 | ieee_set_fp_control(fp_control); |
Guido van Rossum | cf06571a | 2000-09-21 14:32:04 +0000 | [diff] [blame] | 185 | PyOS_setsig(SIGFPE, handler); |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 186 | |
Guido van Rossum | 9b1a80b | 2002-09-26 16:52:02 +0000 | [diff] [blame] | 187 | /*-- DEC ALPHA LINUX ------------------------------------------------------*/ |
| 188 | #elif defined(__alpha) && defined(linux) |
| 189 | #include <asm/fpu.h> |
| 190 | unsigned long fp_control = |
| 191 | IEEE_TRAP_ENABLE_INV | IEEE_TRAP_ENABLE_DZE | IEEE_TRAP_ENABLE_OVF; |
| 192 | ieee_set_fp_control(fp_control); |
| 193 | PyOS_setsig(SIGFPE, handler); |
| 194 | |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 195 | /*-- DEC ALPHA VMS --------------------------------------------------------*/ |
| 196 | #elif defined(__ALPHA) && defined(__VMS) |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 197 | IEEE clrmsk; |
| 198 | IEEE setmsk; |
| 199 | clrmsk.ieee$q_flags = |
| 200 | IEEE$M_TRAP_ENABLE_UNF | IEEE$M_TRAP_ENABLE_INE | |
| 201 | IEEE$M_MAP_UMZ; |
| 202 | setmsk.ieee$q_flags = |
| 203 | IEEE$M_TRAP_ENABLE_INV | IEEE$M_TRAP_ENABLE_DZE | |
| 204 | IEEE$M_TRAP_ENABLE_OVF; |
| 205 | sys$ieee_set_fp_control(&clrmsk, &setmsk, 0); |
| 206 | PyOS_setsig(SIGFPE, handler); |
| 207 | |
| 208 | /*-- HP IA64 VMS --------------------------------------------------------*/ |
| 209 | #elif defined(__ia64) && defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 210 | PyOS_setsig(SIGFPE, handler); |
| 211 | |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 212 | /*-- Cray Unicos ----------------------------------------------------------*/ |
| 213 | #elif defined(cray) |
| 214 | /* UNICOS delivers SIGFPE by default, but no matherr */ |
| 215 | #ifdef HAS_LIBMSET |
| 216 | libmset(-1); |
| 217 | #endif |
Guido van Rossum | cf06571a | 2000-09-21 14:32:04 +0000 | [diff] [blame] | 218 | PyOS_setsig(SIGFPE, handler); |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 219 | |
Guido van Rossum | 4622e14 | 2000-09-19 13:35:40 +0000 | [diff] [blame] | 220 | /*-- FreeBSD ----------------------------------------------------------------*/ |
| 221 | #elif defined(__FreeBSD__) |
Guido van Rossum | cf06571a | 2000-09-21 14:32:04 +0000 | [diff] [blame] | 222 | fpresetsticky(fpgetsticky()); |
| 223 | fpsetmask(FP_X_INV | FP_X_DZ | FP_X_OFL); |
| 224 | PyOS_setsig(SIGFPE, handler); |
Guido van Rossum | 4622e14 | 2000-09-19 13:35:40 +0000 | [diff] [blame] | 225 | |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 226 | /*-- Linux ----------------------------------------------------------------*/ |
| 227 | #elif defined(linux) |
Guido van Rossum | aa6fa6b | 1997-12-15 18:07:10 +0000 | [diff] [blame] | 228 | #ifdef __GLIBC__ |
| 229 | #include <fpu_control.h> |
| 230 | #else |
Guido van Rossum | e85da65 | 1997-10-20 23:50:01 +0000 | [diff] [blame] | 231 | #include <i386/fpu_control.h> |
Guido van Rossum | aa6fa6b | 1997-12-15 18:07:10 +0000 | [diff] [blame] | 232 | #endif |
Andrew M. Kuchling | 5a57163 | 2001-01-04 01:01:12 +0000 | [diff] [blame] | 233 | #ifdef _FPU_SETCW |
| 234 | { |
| 235 | fpu_control_t cw = 0x1372; |
| 236 | _FPU_SETCW(cw); |
| 237 | } |
| 238 | #else |
Guido van Rossum | e85da65 | 1997-10-20 23:50:01 +0000 | [diff] [blame] | 239 | __setfpucw(0x1372); |
Andrew M. Kuchling | 5a57163 | 2001-01-04 01:01:12 +0000 | [diff] [blame] | 240 | #endif |
Guido van Rossum | cf06571a | 2000-09-21 14:32:04 +0000 | [diff] [blame] | 241 | PyOS_setsig(SIGFPE, handler); |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 242 | |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 243 | /*-- Microsoft Windows, NT ------------------------------------------------*/ |
| 244 | #elif defined(_MSC_VER) |
| 245 | /* Reference: Visual C++ Books Online 4.2, |
| 246 | Run-Time Library Reference, _control87, _controlfp */ |
| 247 | #include <float.h> |
| 248 | unsigned int cw = _EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW; |
| 249 | (void)_controlfp(0, cw); |
Guido van Rossum | cf06571a | 2000-09-21 14:32:04 +0000 | [diff] [blame] | 250 | PyOS_setsig(SIGFPE, handler); |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 251 | |
| 252 | /*-- Give Up --------------------------------------------------------------*/ |
| 253 | #else |
| 254 | fputs("Operation not implemented\n", stderr); |
| 255 | #endif |
| 256 | |
| 257 | } |
| 258 | |
| 259 | static PyObject *turnoff_sigfpe(PyObject *self,PyObject *args) |
| 260 | { |
Guido van Rossum | 4622e14 | 2000-09-19 13:35:40 +0000 | [diff] [blame] | 261 | #ifdef __FreeBSD__ |
Guido van Rossum | cf06571a | 2000-09-21 14:32:04 +0000 | [diff] [blame] | 262 | fpresetsticky(fpgetsticky()); |
| 263 | fpsetmask(0); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 264 | #elif defined(__VMS) |
| 265 | IEEE clrmsk; |
| 266 | clrmsk.ieee$q_flags = |
| 267 | IEEE$M_TRAP_ENABLE_UNF | IEEE$M_TRAP_ENABLE_INE | |
| 268 | IEEE$M_MAP_UMZ | IEEE$M_TRAP_ENABLE_INV | |
| 269 | IEEE$M_TRAP_ENABLE_DZE | IEEE$M_TRAP_ENABLE_OVF | |
| 270 | IEEE$M_INHERIT; |
| 271 | sys$ieee_set_fp_control(&clrmsk, 0, 0); |
Guido van Rossum | 4622e14 | 2000-09-19 13:35:40 +0000 | [diff] [blame] | 272 | #else |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 273 | fputs("Operation not implemented\n", stderr); |
Guido van Rossum | 4622e14 | 2000-09-19 13:35:40 +0000 | [diff] [blame] | 274 | #endif |
Guido van Rossum | cf06571a | 2000-09-21 14:32:04 +0000 | [diff] [blame] | 275 | Py_INCREF(Py_None); |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 276 | return Py_None; |
| 277 | } |
| 278 | |
| 279 | static void sigfpe_handler(int signo) |
| 280 | { |
| 281 | fpe_reset(sigfpe_handler); |
| 282 | if(PyFPE_counter) { |
| 283 | longjmp(PyFPE_jbuf, 1); |
| 284 | } else { |
| 285 | Py_FatalError("Unprotected floating point exception"); |
| 286 | } |
| 287 | } |
| 288 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 289 | PyMODINIT_FUNC initfpectl(void) |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 290 | { |
| 291 | PyObject *m, *d; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 292 | m = Py_InitModule("fpectl", fpectl_methods); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 293 | if (m == NULL) |
| 294 | return; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 295 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 296 | fpe_error = PyErr_NewException("fpectl.error", NULL, NULL); |
| 297 | if (fpe_error != NULL) |
| 298 | PyDict_SetItemString(d, "error", fpe_error); |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | #ifdef __cplusplus |
| 302 | } |
| 303 | #endif |