annotate Modules/fpectlmodule.c @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 2d4c6cba01bf
children 2c29b8982643
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /*
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 ---------------------------------------------------------------------
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 / Copyright (c) 1996. \
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 | The Regents of the University of California. |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 | All rights reserved. |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 | |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 | Permission to use, copy, modify, and distribute this software for |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 | any purpose without fee is hereby granted, provided that this en- |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 | tire notice is included in all copies of any software which is or |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 | includes a copy or modification of this software and in all |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 | copies of the supporting documentation for such software. |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 | |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 | This work was produced at the University of California, Lawrence |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 | Livermore National Laboratory under contract no. W-7405-ENG-48 |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 | between the U.S. Department of Energy and The Regents of the |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 | University of California for the operation of UC LLNL. |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 | |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 | DISCLAIMER |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 | |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 | This software was prepared as an account of work sponsored by an |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 | agency of the United States Government. Neither the United States |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 | Government nor the University of California nor any of their em- |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 | ployees, makes any warranty, express or implied, or assumes any |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 | liability or responsibility for the accuracy, completeness, or |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 | usefulness of any information, apparatus, product, or process |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 | disclosed, or represents that its use would not infringe |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 | privately-owned rights. Reference herein to any specific commer- |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 | cial products, process, or service by trade name, trademark, |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 | manufacturer, or otherwise, does not necessarily constitute or |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 | imply its endorsement, recommendation, or favoring by the United |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 | States Government or the University of California. The views and |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 | opinions of authors expressed herein do not necessarily state or |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 | reflect those of the United States Government or the University |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 | of California, and shall not be used for advertising or product |
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 \ endorsement purposes. /
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 ---------------------------------------------------------------------
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 */
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 /*
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 Floating point exception control module.
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 This Python module provides bare-bones control over floating point
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 units from several hardware manufacturers. Specifically, it allows
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 the user to turn on the generation of SIGFPE whenever any of the
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 three serious IEEE 754 exceptions (Division by Zero, Overflow,
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 Invalid Operation) occurs. We currently ignore Underflow and
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 Inexact Result exceptions, although those could certainly be added
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 if desired.
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 The module also establishes a signal handler for SIGFPE during
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 initialization. This builds on code found in the Python
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 distribution at Include/pyfpe.h and Python/pyfpe.c. If those files
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 are not in your Python distribution, find them in a patch at
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 ftp://icf.llnl.gov/pub/python/busby/patches.961108.tgz.
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 This module is only useful to you if it happens to include code
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 specific for your hardware and software environment. If you can
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 contribute OS-specific code for new platforms, or corrections for
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 the code provided, it will be greatly appreciated.
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 ** Version 1.0: September 20, 1996. Lee Busby, LLNL.
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 */
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 #ifdef __cplusplus
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 extern "C" {
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 #endif
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 #include "Python.h"
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 #include <signal.h>
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 #ifndef WANT_SIGFPE_HANDLER
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 /* Define locally if they are not defined in Python. This gives only
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 * the limited control to induce a core dump in case of an exception.
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 */
5196
8ee81ef4e982 Include setjmp.h so it compiles if WANT_SIGFPE_HANDLER is undefined.
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
75 #include <setjmp.h>
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 static jmp_buf PyFPE_jbuf;
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 static int PyFPE_counter = 0;
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 #endif
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 typedef RETSIGTYPE Sigfunc(int);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 static Sigfunc sigfpe_handler;
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 static void fpe_reset(Sigfunc *);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 static PyObject *fpe_error;
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 void initfpectl(void);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 static PyObject *turnon_sigfpe (PyObject *self,PyObject *args);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 static PyObject *turnoff_sigfpe (PyObject *self,PyObject *args);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 static PyMethodDef fpectl_methods[] = {
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 {"turnon_sigfpe", (PyCFunction) turnon_sigfpe, 1},
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 {"turnoff_sigfpe", (PyCFunction) turnoff_sigfpe, 1},
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 {0,0}
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 };
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 static PyObject *turnon_sigfpe(PyObject *self,PyObject *args)
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 {
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 /* Do any architecture-specific one-time only initialization here. */
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 fpe_reset(sigfpe_handler);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 Py_INCREF (Py_None);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 return Py_None;
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 }
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 static void fpe_reset(Sigfunc *handler)
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 {
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 /* Reset the exception handling machinery, and reset the signal
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 * handler for SIGFPE to the given handler.
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 */
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 /*-- IRIX -----------------------------------------------------------------*/
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 #if defined(sgi)
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 /* See man page on handle_sigfpes -- must link with -lfpe
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 * My usage doesn't follow the man page exactly. Maybe somebody
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 * else can explain handle_sigfpes to me....
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 * cc -c -I/usr/local/python/include fpectlmodule.c
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 * ld -shared -o fpectlmodule.so fpectlmodule.o -lfpe
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 */
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 #include <sigfpe.h>
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 typedef void user_routine (unsigned[5], int[2]);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 typedef void abort_routine (unsigned long);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 handle_sigfpes(_OFF, 0,
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 (user_routine *)0,
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 _TURN_OFF_HANDLER_ON_ERROR,
5380
2d4c6cba01bf Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 5196
diff changeset
124 NULL);
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 handle_sigfpes(_ON, _EN_OVERFL | _EN_DIVZERO | _EN_INVALID,
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 (user_routine *)0,
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 _ABORT_ON_ERROR,
5380
2d4c6cba01bf Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 5196
diff changeset
128 NULL);
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 signal(SIGFPE, handler);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 /*-- SunOS and Solaris ----------------------------------------------------*/
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 #elif defined(sun)
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 /* References: ieee_handler, ieee_sun, ieee_functions, and ieee_flags
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 man pages (SunOS or Solaris)
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 cc -c -I/usr/local/python/include fpectlmodule.c
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 ld -G -o fpectlmodule.so -L/opt/SUNWspro/lib fpectlmodule.o -lsunmath -lm
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 */
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 #include <math.h>
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 char *mode="exception", *in="all", *out;
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 (void) nonstandard_arithmetic();
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 (void) ieee_flags("clearall",mode,in,&out);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 (void) ieee_handler("set","common",(sigfpe_handler_type)handler);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 signal(SIGFPE, handler);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 /*-- HPUX -----------------------------------------------------------------*/
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 #elif defined(__hppa) || defined(hppa)
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 /* References: fpsetmask man page */
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 /* cc -Aa +z -c -I/usr/local/python/include fpectlmodule.c */
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 /* ld -b -o fpectlmodule.sl fpectlmodule.o -lm */
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 #include <math.h>
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 fpsetdefaults();
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 signal(SIGFPE, handler);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 /*-- IBM AIX --------------------------------------------------------------*/
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 #elif defined(__AIX) || defined(_AIX)
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 /* References: fp_trap, fp_enable man pages */
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 #include <fptrap.h>
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 fp_trap(FP_TRAP_SYNC);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 fp_enable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 signal(SIGFPE, handler);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 /*-- DEC ALPHA OSF --------------------------------------------------------*/
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 #elif defined(__alpha)
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 /* References: exception_intro, ieee man pages */
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 /* cc -c -I/usr/local/python/include fpectlmodule.c */
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 /* ld -shared -o fpectlmodule.so fpectlmodule.o */
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 #include <machine/fpu.h>
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 unsigned long fp_control =
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 IEEE_TRAP_ENABLE_INV | IEEE_TRAP_ENABLE_DZE | IEEE_TRAP_ENABLE_OVF;
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 ieee_set_fp_control(fp_control);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 signal(SIGFPE, handler);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 /*-- Cray Unicos ----------------------------------------------------------*/
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 #elif defined(cray)
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 /* UNICOS delivers SIGFPE by default, but no matherr */
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 #ifdef HAS_LIBMSET
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 libmset(-1);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 #endif
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 signal(SIGFPE, handler);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 /*-- Linux ----------------------------------------------------------------*/
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 #elif defined(linux)
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 /* Linux delivers SIGFPE by default,
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 except for log(0), atanh(-1), 0.^0.
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 */
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 signal(SIGFPE, handler);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 /*-- NeXT -----------------------------------------------------------------*/
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 #elif defined(NeXT) && defined(m68k) && defined(__GNUC__)
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 /* NeXT needs explicit csr set to generate SIGFPE */
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 asm("fmovel #0x1400,fpcr"); /* set OVFL and ZD bits */
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 signal(SIGFPE, handler);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 /*-- Microsoft Windows, NT ------------------------------------------------*/
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 #elif defined(_MSC_VER)
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 /* Reference: Visual C++ Books Online 4.2,
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 Run-Time Library Reference, _control87, _controlfp */
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 #include <float.h>
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 unsigned int cw = _EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW;
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 (void)_controlfp(0, cw);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 signal(SIGFPE, handler);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 /*-- Give Up --------------------------------------------------------------*/
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 #else
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 fputs("Operation not implemented\n", stderr);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 #endif
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 }
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 static PyObject *turnoff_sigfpe(PyObject *self,PyObject *args)
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 {
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 fputs("Operation not implemented\n", stderr);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 Py_INCREF (Py_None);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 return Py_None;
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 }
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 static void sigfpe_handler(int signo)
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 {
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 fpe_reset(sigfpe_handler);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 if(PyFPE_counter) {
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 longjmp(PyFPE_jbuf, 1);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 } else {
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 Py_FatalError("Unprotected floating point exception");
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 }
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 }
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 void initfpectl(void)
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 {
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 PyObject *m, *d;
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 static int already_initialized = 0;
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
231
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 if (already_initialized) return;
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 m = Py_InitModule("fpectl", fpectl_methods);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 d = PyModule_GetDict(m);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 fpe_error = PyString_FromString("fpectl.error");
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 PyDict_SetItemString(d, "error", fpe_error);
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
237
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 if (PyErr_Occurred())
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 Py_FatalError("Cannot initialize module fpectl");
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 already_initialized = 1;
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 }
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
242
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 #ifdef __cplusplus
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 }
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 #endif