Improved some error messages for command line processing.
[python/dscho.git] / Modules / fpectlmodule.c
blobe2a8022723580be34fdc889ac61f4b047603edf2
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 ---------------------------------------------------------------------
40 Floating point exception control module.
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.
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.
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.
61 ** Version 1.0: September 20, 1996. Lee Busby, LLNL.
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
68 #include "Python.h"
69 #include <signal.h>
71 #ifndef WANT_SIGFPE_HANDLER
72 /* Define locally if they are not defined in Python. This gives only
73 * the limited control to induce a core dump in case of an exception.
75 #include <setjmp.h>
76 static jmp_buf PyFPE_jbuf;
77 static int PyFPE_counter = 0;
78 #endif
80 typedef RETSIGTYPE Sigfunc(int);
81 static Sigfunc sigfpe_handler;
82 static void fpe_reset(Sigfunc *);
84 static PyObject *fpe_error;
85 void initfpectl(void);
86 static PyObject *turnon_sigfpe (PyObject *self,PyObject *args);
87 static PyObject *turnoff_sigfpe (PyObject *self,PyObject *args);
89 static PyMethodDef fpectl_methods[] = {
90 {"turnon_sigfpe", (PyCFunction) turnon_sigfpe, 1},
91 {"turnoff_sigfpe", (PyCFunction) turnoff_sigfpe, 1},
92 {0,0}
95 static PyObject *turnon_sigfpe(PyObject *self,PyObject *args)
97 /* Do any architecture-specific one-time only initialization here. */
99 fpe_reset(sigfpe_handler);
100 Py_INCREF (Py_None);
101 return Py_None;
104 static void fpe_reset(Sigfunc *handler)
106 /* Reset the exception handling machinery, and reset the signal
107 * handler for SIGFPE to the given handler.
110 /*-- IRIX -----------------------------------------------------------------*/
111 #if defined(sgi)
112 /* See man page on handle_sigfpes -- must link with -lfpe
113 * My usage doesn't follow the man page exactly. Maybe somebody
114 * else can explain handle_sigfpes to me....
115 * cc -c -I/usr/local/python/include fpectlmodule.c
116 * ld -shared -o fpectlmodule.so fpectlmodule.o -lfpe
118 #include <sigfpe.h>
119 typedef void user_routine (unsigned[5], int[2]);
120 typedef void abort_routine (unsigned long);
121 handle_sigfpes(_OFF, 0,
122 (user_routine *)0,
123 _TURN_OFF_HANDLER_ON_ERROR,
124 NULL);
125 handle_sigfpes(_ON, _EN_OVERFL | _EN_DIVZERO | _EN_INVALID,
126 (user_routine *)0,
127 _ABORT_ON_ERROR,
128 NULL);
129 signal(SIGFPE, handler);
131 /*-- SunOS and Solaris ----------------------------------------------------*/
132 #elif defined(sun)
133 /* References: ieee_handler, ieee_sun, ieee_functions, and ieee_flags
134 man pages (SunOS or Solaris)
135 cc -c -I/usr/local/python/include fpectlmodule.c
136 ld -G -o fpectlmodule.so -L/opt/SUNWspro/lib fpectlmodule.o -lsunmath -lm
138 #include <math.h>
139 char *mode="exception", *in="all", *out;
140 (void) nonstandard_arithmetic();
141 (void) ieee_flags("clearall",mode,in,&out);
142 (void) ieee_handler("set","common",(sigfpe_handler_type)handler);
143 signal(SIGFPE, handler);
145 /*-- HPUX -----------------------------------------------------------------*/
146 #elif defined(__hppa) || defined(hppa)
147 /* References: fpsetmask man page */
148 /* cc -Aa +z -c -I/usr/local/python/include fpectlmodule.c */
149 /* ld -b -o fpectlmodule.sl fpectlmodule.o -lm */
150 #include <math.h>
151 fpsetdefaults();
152 signal(SIGFPE, handler);
154 /*-- IBM AIX --------------------------------------------------------------*/
155 #elif defined(__AIX) || defined(_AIX)
156 /* References: fp_trap, fp_enable man pages */
157 #include <fptrap.h>
158 fp_trap(FP_TRAP_SYNC);
159 fp_enable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW);
160 signal(SIGFPE, handler);
162 /*-- DEC ALPHA OSF --------------------------------------------------------*/
163 #elif defined(__alpha)
164 /* References: exception_intro, ieee man pages */
165 /* cc -c -I/usr/local/python/include fpectlmodule.c */
166 /* ld -shared -o fpectlmodule.so fpectlmodule.o */
167 #include <machine/fpu.h>
168 unsigned long fp_control =
169 IEEE_TRAP_ENABLE_INV | IEEE_TRAP_ENABLE_DZE | IEEE_TRAP_ENABLE_OVF;
170 ieee_set_fp_control(fp_control);
171 signal(SIGFPE, handler);
173 /*-- Cray Unicos ----------------------------------------------------------*/
174 #elif defined(cray)
175 /* UNICOS delivers SIGFPE by default, but no matherr */
176 #ifdef HAS_LIBMSET
177 libmset(-1);
178 #endif
179 signal(SIGFPE, handler);
181 /*-- Linux ----------------------------------------------------------------*/
182 #elif defined(linux)
183 #ifdef __GLIBC__
184 #include <fpu_control.h>
185 #else
186 #include <i386/fpu_control.h>
187 #endif
188 __setfpucw(0x1372);
189 signal(SIGFPE, handler);
191 /*-- NeXT -----------------------------------------------------------------*/
192 #elif defined(NeXT) && defined(m68k) && defined(__GNUC__)
193 /* NeXT needs explicit csr set to generate SIGFPE */
194 asm("fmovel #0x1400,fpcr"); /* set OVFL and ZD bits */
195 signal(SIGFPE, handler);
197 /*-- Microsoft Windows, NT ------------------------------------------------*/
198 #elif defined(_MSC_VER)
199 /* Reference: Visual C++ Books Online 4.2,
200 Run-Time Library Reference, _control87, _controlfp */
201 #include <float.h>
202 unsigned int cw = _EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW;
203 (void)_controlfp(0, cw);
204 signal(SIGFPE, handler);
206 /*-- Give Up --------------------------------------------------------------*/
207 #else
208 fputs("Operation not implemented\n", stderr);
209 #endif
213 static PyObject *turnoff_sigfpe(PyObject *self,PyObject *args)
215 fputs("Operation not implemented\n", stderr);
216 Py_INCREF (Py_None);
217 return Py_None;
220 static void sigfpe_handler(int signo)
222 fpe_reset(sigfpe_handler);
223 if(PyFPE_counter) {
224 longjmp(PyFPE_jbuf, 1);
225 } else {
226 Py_FatalError("Unprotected floating point exception");
230 void initfpectl(void)
232 PyObject *m, *d;
233 m = Py_InitModule("fpectl", fpectl_methods);
234 d = PyModule_GetDict(m);
235 fpe_error = PyErr_NewException("fpectl.error", NULL, NULL);
236 if (fpe_error != NULL)
237 PyDict_SetItemString(d, "error", fpe_error);
240 #ifdef __cplusplus
242 #endif