blob: 42ef0f6072e30c625adb3eb387219b6c527825e8 [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>
73#endif
74
Guido van Rossum52fa3a61997-02-14 22:59:58 +000075#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 Rossum1aeb1041997-03-14 04:32:25 +000079#include <setjmp.h>
Guido van Rossum52fa3a61997-02-14 22:59:58 +000080static jmp_buf PyFPE_jbuf;
81static int PyFPE_counter = 0;
82#endif
83
Tim Peters4f1b2082000-07-23 21:18:09 +000084typedef void Sigfunc(int);
Guido van Rossum52fa3a61997-02-14 22:59:58 +000085static Sigfunc sigfpe_handler;
86static void fpe_reset(Sigfunc *);
87
88static PyObject *fpe_error;
Martin v. Löwis1a214512008-06-11 05:26:20 +000089
90PyMODINIT_FUNC PyInit_fpectl(void);
Guido van Rossum52fa3a61997-02-14 22:59:58 +000091static PyObject *turnon_sigfpe (PyObject *self,PyObject *args);
92static PyObject *turnoff_sigfpe (PyObject *self,PyObject *args);
93
94static PyMethodDef fpectl_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 {"turnon_sigfpe", (PyCFunction) turnon_sigfpe, METH_VARARGS},
96 {"turnoff_sigfpe", (PyCFunction) turnoff_sigfpe, METH_VARARGS},
Guido van Rossum52fa3a61997-02-14 22:59:58 +000097 {0,0}
98};
99
100static PyObject *turnon_sigfpe(PyObject *self,PyObject *args)
101{
102 /* Do any architecture-specific one-time only initialization here. */
103
104 fpe_reset(sigfpe_handler);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200105 Py_RETURN_NONE;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000106}
107
108static void fpe_reset(Sigfunc *handler)
109{
110 /* Reset the exception handling machinery, and reset the signal
111 * handler for SIGFPE to the given handler.
112 */
113
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000114/*-- SunOS and Solaris ----------------------------------------------------*/
Benjamin Peterson06930632017-09-04 16:36:05 -0700115#if defined(sun)
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000116 /* References: ieee_handler, ieee_sun, ieee_functions, and ieee_flags
117 man pages (SunOS or Solaris)
118 cc -c -I/usr/local/python/include fpectlmodule.c
119 ld -G -o fpectlmodule.so -L/opt/SUNWspro/lib fpectlmodule.o -lsunmath -lm
120 */
121#include <math.h>
Martin v. Löwisc4db4762001-03-07 10:22:20 +0000122#ifndef _SUNMATH_H
123 extern void nonstandard_arithmetic(void);
124 extern int ieee_flags(const char*, const char*, const char*, char **);
125 extern long ieee_handler(const char*, const char*, sigfpe_handler_type);
126#endif
127
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200128 const char *mode="exception", *in="all";
129 char *out;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000130 (void) nonstandard_arithmetic();
131 (void) ieee_flags("clearall",mode,in,&out);
132 (void) ieee_handler("set","common",(sigfpe_handler_type)handler);
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000133 PyOS_setsig(SIGFPE, handler);
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000134
135/*-- HPUX -----------------------------------------------------------------*/
136#elif defined(__hppa) || defined(hppa)
137 /* References: fpsetmask man page */
138 /* cc -Aa +z -c -I/usr/local/python/include fpectlmodule.c */
139 /* ld -b -o fpectlmodule.sl fpectlmodule.o -lm */
140#include <math.h>
141 fpsetdefaults();
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000142 PyOS_setsig(SIGFPE, handler);
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000143
144/*-- IBM AIX --------------------------------------------------------------*/
145#elif defined(__AIX) || defined(_AIX)
146 /* References: fp_trap, fp_enable man pages */
147#include <fptrap.h>
148 fp_trap(FP_TRAP_SYNC);
149 fp_enable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW);
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000150 PyOS_setsig(SIGFPE, handler);
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000151
Guido van Rossum9b1a80b2002-09-26 16:52:02 +0000152/*-- DEC ALPHA LINUX ------------------------------------------------------*/
153#elif defined(__alpha) && defined(linux)
154#include <asm/fpu.h>
155 unsigned long fp_control =
156 IEEE_TRAP_ENABLE_INV | IEEE_TRAP_ENABLE_DZE | IEEE_TRAP_ENABLE_OVF;
157 ieee_set_fp_control(fp_control);
158 PyOS_setsig(SIGFPE, handler);
159
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000160/*-- Cray Unicos ----------------------------------------------------------*/
161#elif defined(cray)
162 /* UNICOS delivers SIGFPE by default, but no matherr */
163#ifdef HAS_LIBMSET
164 libmset(-1);
165#endif
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000166 PyOS_setsig(SIGFPE, handler);
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000167
Guido van Rossum4622e142000-09-19 13:35:40 +0000168/*-- FreeBSD ----------------------------------------------------------------*/
169#elif defined(__FreeBSD__)
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000170 fpresetsticky(fpgetsticky());
171 fpsetmask(FP_X_INV | FP_X_DZ | FP_X_OFL);
172 PyOS_setsig(SIGFPE, handler);
Guido van Rossum4622e142000-09-19 13:35:40 +0000173
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000174/*-- Linux ----------------------------------------------------------------*/
175#elif defined(linux)
Guido van Rossumaa6fa6b1997-12-15 18:07:10 +0000176#ifdef __GLIBC__
177#include <fpu_control.h>
178#else
Guido van Rossume85da651997-10-20 23:50:01 +0000179#include <i386/fpu_control.h>
Guido van Rossumaa6fa6b1997-12-15 18:07:10 +0000180#endif
Andrew M. Kuchling5a571632001-01-04 01:01:12 +0000181#ifdef _FPU_SETCW
182 {
183 fpu_control_t cw = 0x1372;
184 _FPU_SETCW(cw);
185 }
186#else
Guido van Rossume85da651997-10-20 23:50:01 +0000187 __setfpucw(0x1372);
Andrew M. Kuchling5a571632001-01-04 01:01:12 +0000188#endif
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000189 PyOS_setsig(SIGFPE, handler);
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000190
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000191/*-- Microsoft Windows, NT ------------------------------------------------*/
192#elif defined(_MSC_VER)
193 /* Reference: Visual C++ Books Online 4.2,
194 Run-Time Library Reference, _control87, _controlfp */
195#include <float.h>
196 unsigned int cw = _EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW;
197 (void)_controlfp(0, cw);
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000198 PyOS_setsig(SIGFPE, handler);
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000199
200/*-- Give Up --------------------------------------------------------------*/
201#else
202 fputs("Operation not implemented\n", stderr);
203#endif
204
205}
206
207static PyObject *turnoff_sigfpe(PyObject *self,PyObject *args)
208{
Guido van Rossum4622e142000-09-19 13:35:40 +0000209#ifdef __FreeBSD__
Guido van Rossumcf06571a2000-09-21 14:32:04 +0000210 fpresetsticky(fpgetsticky());
211 fpsetmask(0);
Guido van Rossum4622e142000-09-19 13:35:40 +0000212#else
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000213 fputs("Operation not implemented\n", stderr);
Guido van Rossum4622e142000-09-19 13:35:40 +0000214#endif
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200215 Py_RETURN_NONE;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000216}
217
218static void sigfpe_handler(int signo)
219{
220 fpe_reset(sigfpe_handler);
221 if(PyFPE_counter) {
222 longjmp(PyFPE_jbuf, 1);
223 } else {
224 Py_FatalError("Unprotected floating point exception");
225 }
226}
227
Martin v. Löwis1a214512008-06-11 05:26:20 +0000228static struct PyModuleDef fpectlmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 PyModuleDef_HEAD_INIT,
230 "fpectl",
231 NULL,
232 -1,
233 fpectl_methods,
234 NULL,
235 NULL,
236 NULL,
237 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000238};
239
240PyMODINIT_FUNC PyInit_fpectl(void)
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000241{
242 PyObject *m, *d;
Matthias Klose1ff110d2008-11-26 17:18:22 +0000243 m = PyModule_Create(&fpectlmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000244 if (m == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 return NULL;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000246 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000247 fpe_error = PyErr_NewException("fpectl.error", NULL, NULL);
248 if (fpe_error != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 PyDict_SetItemString(d, "error", fpe_error);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000250 return m;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000251}
252
253#ifdef __cplusplus
254}
255#endif