You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
9.9 KiB

  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. Floating point exception control module.
  40. This Python module provides bare-bones control over floating point
  41. units from several hardware manufacturers. Specifically, it allows
  42. the user to turn on the generation of SIGFPE whenever any of the
  43. three serious IEEE 754 exceptions (Division by Zero, Overflow,
  44. Invalid Operation) occurs. We currently ignore Underflow and
  45. Inexact Result exceptions, although those could certainly be added
  46. if desired.
  47. The module also establishes a signal handler for SIGFPE during
  48. initialization. This builds on code found in the Python
  49. distribution at Include/pyfpe.h and Python/pyfpe.c. If those files
  50. are not in your Python distribution, find them in a patch at
  51. ftp://icf.llnl.gov/pub/python/busby/patches.961108.tgz.
  52. This module is only useful to you if it happens to include code
  53. specific for your hardware and software environment. If you can
  54. contribute OS-specific code for new platforms, or corrections for
  55. the code provided, it will be greatly appreciated.
  56. ** Version 1.0: September 20, 1996. Lee Busby, LLNL.
  57. */
  58. #ifdef __cplusplus
  59. extern "C" {
  60. #endif
  61. #include "Python.h"
  62. #include <signal.h>
  63. #if defined(__FreeBSD__)
  64. # include <ieeefp.h>
  65. #endif
  66. #ifndef WANT_SIGFPE_HANDLER
  67. /* Define locally if they are not defined in Python. This gives only
  68. * the limited control to induce a core dump in case of an exception.
  69. */
  70. #include <setjmp.h>
  71. static jmp_buf PyFPE_jbuf;
  72. static int PyFPE_counter = 0;
  73. #endif
  74. typedef void Sigfunc(int);
  75. static Sigfunc sigfpe_handler;
  76. static void fpe_reset(Sigfunc *);
  77. static PyObject *fpe_error;
  78. PyMODINIT_FUNC PyInit_fpectl(void);
  79. static PyObject *turnon_sigfpe (PyObject *self,PyObject *args);
  80. static PyObject *turnoff_sigfpe (PyObject *self,PyObject *args);
  81. static PyMethodDef fpectl_methods[] = {
  82. {"turnon_sigfpe", (PyCFunction) turnon_sigfpe, METH_VARARGS},
  83. {"turnoff_sigfpe", (PyCFunction) turnoff_sigfpe, METH_VARARGS},
  84. {0,0}
  85. };
  86. static PyObject *turnon_sigfpe(PyObject *self,PyObject *args)
  87. {
  88. /* Do any architecture-specific one-time only initialization here. */
  89. fpe_reset(sigfpe_handler);
  90. Py_INCREF (Py_None);
  91. return Py_None;
  92. }
  93. static void fpe_reset(Sigfunc *handler)
  94. {
  95. /* Reset the exception handling machinery, and reset the signal
  96. * handler for SIGFPE to the given handler.
  97. */
  98. /*-- IRIX -----------------------------------------------------------------*/
  99. #if defined(sgi)
  100. /* See man page on handle_sigfpes -- must link with -lfpe
  101. * My usage doesn't follow the man page exactly. Maybe somebody
  102. * else can explain handle_sigfpes to me....
  103. * cc -c -I/usr/local/python/include fpectlmodule.c
  104. * ld -shared -o fpectlmodule.so fpectlmodule.o -lfpe
  105. */
  106. #include <sigfpe.h>
  107. typedef void user_routine (unsigned[5], int[2]);
  108. typedef void abort_routine (unsigned long);
  109. handle_sigfpes(_OFF, 0,
  110. (user_routine *)0,
  111. _TURN_OFF_HANDLER_ON_ERROR,
  112. NULL);
  113. handle_sigfpes(_ON, _EN_OVERFL | _EN_DIVZERO | _EN_INVALID,
  114. (user_routine *)0,
  115. _ABORT_ON_ERROR,
  116. NULL);
  117. PyOS_setsig(SIGFPE, handler);
  118. /*-- SunOS and Solaris ----------------------------------------------------*/
  119. #elif defined(sun)
  120. /* References: ieee_handler, ieee_sun, ieee_functions, and ieee_flags
  121. man pages (SunOS or Solaris)
  122. cc -c -I/usr/local/python/include fpectlmodule.c
  123. ld -G -o fpectlmodule.so -L/opt/SUNWspro/lib fpectlmodule.o -lsunmath -lm
  124. */
  125. #include <math.h>
  126. #ifndef _SUNMATH_H
  127. extern void nonstandard_arithmetic(void);
  128. extern int ieee_flags(const char*, const char*, const char*, char **);
  129. extern long ieee_handler(const char*, const char*, sigfpe_handler_type);
  130. #endif
  131. char *mode="exception", *in="all", *out;
  132. (void) nonstandard_arithmetic();
  133. (void) ieee_flags("clearall",mode,in,&out);
  134. (void) ieee_handler("set","common",(sigfpe_handler_type)handler);
  135. PyOS_setsig(SIGFPE, handler);
  136. /*-- HPUX -----------------------------------------------------------------*/
  137. #elif defined(__hppa) || defined(hppa)
  138. /* References: fpsetmask man page */
  139. /* cc -Aa +z -c -I/usr/local/python/include fpectlmodule.c */
  140. /* ld -b -o fpectlmodule.sl fpectlmodule.o -lm */
  141. #include <math.h>
  142. fpsetdefaults();
  143. PyOS_setsig(SIGFPE, handler);
  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);
  150. PyOS_setsig(SIGFPE, handler);
  151. /*-- DEC ALPHA LINUX ------------------------------------------------------*/
  152. #elif defined(__alpha) && defined(linux)
  153. #include <asm/fpu.h>
  154. unsigned long fp_control =
  155. IEEE_TRAP_ENABLE_INV | IEEE_TRAP_ENABLE_DZE | IEEE_TRAP_ENABLE_OVF;
  156. ieee_set_fp_control(fp_control);
  157. PyOS_setsig(SIGFPE, handler);
  158. /*-- Cray Unicos ----------------------------------------------------------*/
  159. #elif defined(cray)
  160. /* UNICOS delivers SIGFPE by default, but no matherr */
  161. #ifdef HAS_LIBMSET
  162. libmset(-1);
  163. #endif
  164. PyOS_setsig(SIGFPE, handler);
  165. /*-- FreeBSD ----------------------------------------------------------------*/
  166. #elif defined(__FreeBSD__)
  167. fpresetsticky(fpgetsticky());
  168. fpsetmask(FP_X_INV | FP_X_DZ | FP_X_OFL);
  169. PyOS_setsig(SIGFPE, handler);
  170. /*-- Linux ----------------------------------------------------------------*/
  171. #elif defined(linux)
  172. #ifdef __GLIBC__
  173. #include <fpu_control.h>
  174. #else
  175. #include <i386/fpu_control.h>
  176. #endif
  177. #ifdef _FPU_SETCW
  178. {
  179. fpu_control_t cw = 0x1372;
  180. _FPU_SETCW(cw);
  181. }
  182. #else
  183. __setfpucw(0x1372);
  184. #endif
  185. PyOS_setsig(SIGFPE, handler);
  186. /*-- Microsoft Windows, NT ------------------------------------------------*/
  187. #elif defined(_MSC_VER)
  188. /* Reference: Visual C++ Books Online 4.2,
  189. Run-Time Library Reference, _control87, _controlfp */
  190. #include <float.h>
  191. unsigned int cw = _EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW;
  192. (void)_controlfp(0, cw);
  193. PyOS_setsig(SIGFPE, handler);
  194. /*-- Give Up --------------------------------------------------------------*/
  195. #else
  196. fputs("Operation not implemented\n", stderr);
  197. #endif
  198. }
  199. static PyObject *turnoff_sigfpe(PyObject *self,PyObject *args)
  200. {
  201. #ifdef __FreeBSD__
  202. fpresetsticky(fpgetsticky());
  203. fpsetmask(0);
  204. #else
  205. fputs("Operation not implemented\n", stderr);
  206. #endif
  207. Py_INCREF(Py_None);
  208. return Py_None;
  209. }
  210. static void sigfpe_handler(int signo)
  211. {
  212. fpe_reset(sigfpe_handler);
  213. if(PyFPE_counter) {
  214. longjmp(PyFPE_jbuf, 1);
  215. } else {
  216. Py_FatalError("Unprotected floating point exception");
  217. }
  218. }
  219. static struct PyModuleDef fpectlmodule = {
  220. PyModuleDef_HEAD_INIT,
  221. "fpectl",
  222. NULL,
  223. -1,
  224. fpectl_methods,
  225. NULL,
  226. NULL,
  227. NULL,
  228. NULL
  229. };
  230. PyMODINIT_FUNC PyInit_fpectl(void)
  231. {
  232. PyObject *m, *d;
  233. m = PyModule_Create(&fpectlmodule);
  234. if (m == NULL)
  235. return NULL;
  236. d = PyModule_GetDict(m);
  237. fpe_error = PyErr_NewException("fpectl.error", NULL, NULL);
  238. if (fpe_error != NULL)
  239. PyDict_SetItemString(d, "error", fpe_error);
  240. return m;
  241. }
  242. #ifdef __cplusplus
  243. }
  244. #endif