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