uninit.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "arith.h"
  4. #define TYSHORT 2
  5. #define TYLONG 3
  6. #define TYREAL 4
  7. #define TYDREAL 5
  8. #define TYCOMPLEX 6
  9. #define TYDCOMPLEX 7
  10. #define TYINT1 11
  11. #define TYQUAD 14
  12. #ifndef Long
  13. #define Long long
  14. #endif
  15. #ifdef __mips
  16. #define RNAN 0xffc00000
  17. #define DNAN0 0xfff80000
  18. #define DNAN1 0
  19. #endif
  20. #ifdef _PA_RISC1_1
  21. #define RNAN 0xffc00000
  22. #define DNAN0 0xfff80000
  23. #define DNAN1 0
  24. #endif
  25. #ifndef RNAN
  26. #define RNAN 0xff800001
  27. #ifdef IEEE_MC68k
  28. #define DNAN0 0xfff00000
  29. #define DNAN1 1
  30. #else
  31. #define DNAN0 1
  32. #define DNAN1 0xfff00000
  33. #endif
  34. #endif /*RNAN*/
  35. #ifdef KR_headers
  36. #define Void /*void*/
  37. #define FA7UL (unsigned Long) 0xfa7a7a7aL
  38. #else
  39. #define Void void
  40. #define FA7UL 0xfa7a7a7aUL
  41. #endif
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. static void ieee0(Void);
  46. static unsigned Long rnan = RNAN,
  47. dnan0 = DNAN0,
  48. dnan1 = DNAN1;
  49. double _0 = 0.;
  50. void
  51. #ifdef KR_headers
  52. _uninit_f2c(x, type, len) void *x; int type; long len;
  53. #else
  54. _uninit_f2c(void *x, int type, long len)
  55. #endif
  56. {
  57. static int first = 1;
  58. unsigned Long *lx, *lxe;
  59. if (first) {
  60. first = 0;
  61. ieee0();
  62. }
  63. if (len == 1)
  64. switch(type) {
  65. case TYINT1:
  66. *(char*)x = 'Z';
  67. return;
  68. case TYSHORT:
  69. *(short*)x = 0xfa7a;
  70. break;
  71. case TYLONG:
  72. *(unsigned Long*)x = FA7UL;
  73. return;
  74. case TYQUAD:
  75. case TYCOMPLEX:
  76. case TYDCOMPLEX:
  77. break;
  78. case TYREAL:
  79. *(unsigned Long*)x = rnan;
  80. return;
  81. case TYDREAL:
  82. lx = (unsigned Long*)x;
  83. lx[0] = dnan0;
  84. lx[1] = dnan1;
  85. return;
  86. default:
  87. printf("Surprise type %d in _uninit_f2c\n", type);
  88. }
  89. switch(type) {
  90. case TYINT1:
  91. memset(x, 'Z', len);
  92. break;
  93. case TYSHORT:
  94. *(short*)x = 0xfa7a;
  95. break;
  96. case TYQUAD:
  97. len *= 2;
  98. /* no break */
  99. case TYLONG:
  100. lx = (unsigned Long*)x;
  101. lxe = lx + len;
  102. while(lx < lxe)
  103. *lx++ = FA7UL;
  104. break;
  105. case TYCOMPLEX:
  106. len *= 2;
  107. /* no break */
  108. case TYREAL:
  109. lx = (unsigned Long*)x;
  110. lxe = lx + len;
  111. while(lx < lxe)
  112. *lx++ = rnan;
  113. break;
  114. case TYDCOMPLEX:
  115. len *= 2;
  116. /* no break */
  117. case TYDREAL:
  118. lx = (unsigned Long*)x;
  119. for(lxe = lx + 2*len; lx < lxe; lx += 2) {
  120. lx[0] = dnan0;
  121. lx[1] = dnan1;
  122. }
  123. }
  124. }
  125. #ifdef __cplusplus
  126. }
  127. #endif
  128. #ifndef MSpc
  129. #ifdef MSDOS
  130. #define MSpc
  131. #else
  132. #ifdef _WIN32
  133. #define MSpc
  134. #endif
  135. #endif
  136. #endif
  137. #ifdef MSpc
  138. #define IEEE0_done
  139. #include "float.h"
  140. #include "signal.h"
  141. static void
  142. ieee0(Void)
  143. {
  144. #ifndef __alpha
  145. #ifndef EM_DENORMAL
  146. #define EM_DENORMAL _EM_DENORMAL
  147. #endif
  148. #ifndef EM_UNDERFLOW
  149. #define EM_UNDERFLOW _EM_UNDERFLOW
  150. #endif
  151. #ifndef EM_INEXACT
  152. #define EM_INEXACT _EM_INEXACT
  153. #endif
  154. #ifndef MCW_EM
  155. #define MCW_EM _MCW_EM
  156. #endif
  157. _control87(EM_DENORMAL | EM_UNDERFLOW | EM_INEXACT, MCW_EM);
  158. #endif
  159. /* With MS VC++, compiling and linking with -Zi will permit */
  160. /* clicking to invoke the MS C++ debugger, which will show */
  161. /* the point of error -- provided SIGFPE is SIG_DFL. */
  162. signal(SIGFPE, SIG_DFL);
  163. }
  164. #endif /* MSpc */
  165. #ifdef __mips /* must link with -lfpe */
  166. #define IEEE0_done
  167. /* code from Eric Grosse */
  168. #include <stdlib.h>
  169. #include <stdio.h>
  170. #error #include "/usr/include/sigfpe.h" /* full pathname for lcc -N */
  171. #error #include "/usr/include/sys/fpu.h"
  172. static void
  173. #ifdef KR_headers
  174. ieeeuserhand(exception, val) unsigned exception[5]; int val[2];
  175. #else
  176. ieeeuserhand(unsigned exception[5], int val[2])
  177. #endif
  178. {
  179. fflush(stdout);
  180. fprintf(stderr,"ieee0() aborting because of ");
  181. if(exception[0]==_OVERFL) fprintf(stderr,"overflow\n");
  182. else if(exception[0]==_UNDERFL) fprintf(stderr,"underflow\n");
  183. else if(exception[0]==_DIVZERO) fprintf(stderr,"divide by 0\n");
  184. else if(exception[0]==_INVALID) fprintf(stderr,"invalid operation\n");
  185. else fprintf(stderr,"\tunknown reason\n");
  186. fflush(stderr);
  187. abort();
  188. }
  189. static void
  190. #ifdef KR_headers
  191. ieeeuserhand2(j) unsigned int **j;
  192. #else
  193. ieeeuserhand2(unsigned int **j)
  194. #endif
  195. {
  196. fprintf(stderr,"ieee0() aborting because of confusion\n");
  197. abort();
  198. }
  199. static void
  200. ieee0(Void)
  201. {
  202. int i;
  203. for(i=1; i<=4; i++){
  204. sigfpe_[i].count = 1000;
  205. sigfpe_[i].trace = 1;
  206. sigfpe_[i].repls = _USER_DETERMINED;
  207. }
  208. sigfpe_[1].repls = _ZERO; /* underflow */
  209. handle_sigfpes( _ON,
  210. _EN_UNDERFL|_EN_OVERFL|_EN_DIVZERO|_EN_INVALID,
  211. ieeeuserhand,_ABORT_ON_ERROR,ieeeuserhand2);
  212. }
  213. #endif /* mips */
  214. #if 0
  215. #ifdef __linux__
  216. #define IEEE0_done
  217. #include "fpu_control.h"
  218. #ifdef __alpha__
  219. #ifndef USE_setfpucw
  220. #define __setfpucw(x) __fpu_control = (x)
  221. #endif
  222. #endif
  223. #ifndef _FPU_SETCW
  224. #undef Can_use__setfpucw
  225. #define Can_use__setfpucw
  226. #endif
  227. static void
  228. ieee0(Void)
  229. {
  230. #if (defined(__mc68000__) || defined(__mc68020__) || defined(mc68020) || defined (__mc68k__))
  231. /* Reported 20010705 by Alan Bain <alanb@chiark.greenend.org.uk> */
  232. /* Note that IEEE 754 IOP (illegal operation) */
  233. /* = Signaling NAN (SNAN) + operation error (OPERR). */
  234. #ifdef Can_use__setfpucw
  235. __setfpucw(_FPU_IEEE + _FPU_DOUBLE + _FPU_MASK_OPERR + _FPU_MASK_DZ + _FPU_MASK_SNAN+_FPU_MASK_OVFL);
  236. #else
  237. __fpu_control = _FPU_IEEE + _FPU_DOUBLE + _FPU_MASK_OPERR + _FPU_MASK_DZ + _FPU_MASK_SNAN+_FPU_MASK_OVFL;
  238. _FPU_SETCW(__fpu_control);
  239. #endif
  240. #elif (defined(__powerpc__)||defined(_ARCH_PPC)||defined(_ARCH_PWR)) /* !__mc68k__ */
  241. /* Reported 20011109 by Alan Bain <alanb@chiark.greenend.org.uk> */
  242. #ifdef Can_use__setfpucw
  243. /* The following is NOT a mistake -- the author of the fpu_control.h
  244. for the PPC has erroneously defined IEEE mode to turn on exceptions
  245. other than Inexact! Start from default then and turn on only the ones
  246. which we want*/
  247. __setfpucw(_FPU_DEFAULT + _FPU_MASK_IM+_FPU_MASK_OM+_FPU_MASK_UM);
  248. #else /* PPC && !Can_use__setfpucw */
  249. __fpu_control = _FPU_DEFAULT +_FPU_MASK_OM+_FPU_MASK_IM+_FPU_MASK_UM;
  250. _FPU_SETCW(__fpu_control);
  251. #endif /*Can_use__setfpucw*/
  252. #else /* !(mc68000||powerpc) */
  253. #ifdef _FPU_IEEE
  254. #ifndef _FPU_EXTENDED /* e.g., ARM processor under Linux */
  255. #define _FPU_EXTENDED 0
  256. #endif
  257. #ifndef _FPU_DOUBLE
  258. #define _FPU_DOUBLE 0
  259. #endif
  260. #ifdef Can_use__setfpucw /* pre-1997 (?) Linux */
  261. __setfpucw(_FPU_IEEE - _FPU_MASK_IM - _FPU_MASK_ZM - _FPU_MASK_OM);
  262. #else
  263. #ifdef UNINIT_F2C_PRECISION_53 /* 20051004 */
  264. /* unmask invalid, etc., and change rounding precision to double */
  265. __fpu_control = _FPU_IEEE - _FPU_EXTENDED + _FPU_DOUBLE - _FPU_MASK_IM - _FPU_MASK_ZM - _FPU_MASK_OM;
  266. _FPU_SETCW(__fpu_control);
  267. #else
  268. /* unmask invalid, etc., and keep current rounding precision */
  269. fpu_control_t cw;
  270. _FPU_GETCW(cw);
  271. cw &= ~(_FPU_MASK_IM | _FPU_MASK_ZM | _FPU_MASK_OM);
  272. _FPU_SETCW(cw);
  273. #endif
  274. #endif
  275. #else /* !_FPU_IEEE */
  276. fprintf(stderr, "\n%s\n%s\n%s\n%s\n",
  277. "WARNING: _uninit_f2c in libf2c does not know how",
  278. "to enable trapping on this system, so f2c's -trapuv",
  279. "option will not detect uninitialized variables unless",
  280. "you can enable trapping manually.");
  281. fflush(stderr);
  282. #endif /* _FPU_IEEE */
  283. #endif /* __mc68k__ */
  284. }
  285. #endif /* __linux__ */
  286. #endif
  287. #ifdef __alpha
  288. #ifndef IEEE0_done
  289. #define IEEE0_done
  290. #include <machine/fpu.h>
  291. static void
  292. ieee0(Void)
  293. {
  294. ieee_set_fp_control(IEEE_TRAP_ENABLE_INV);
  295. }
  296. #endif /*IEEE0_done*/
  297. #endif /*__alpha*/
  298. #ifdef __hpux
  299. #define IEEE0_done
  300. #define _INCLUDE_HPUX_SOURCE
  301. #include <math.h>
  302. #ifndef FP_X_INV
  303. #include <fenv.h>
  304. #define fpsetmask fesettrapenable
  305. #define FP_X_INV FE_INVALID
  306. #endif
  307. static void
  308. ieee0(Void)
  309. {
  310. fpsetmask(FP_X_INV);
  311. }
  312. #endif /*__hpux*/
  313. #ifdef _AIX
  314. #define IEEE0_done
  315. #include <fptrap.h>
  316. static void
  317. ieee0(Void)
  318. {
  319. fp_enable(TRP_INVALID);
  320. fp_trap(FP_TRAP_SYNC);
  321. }
  322. #endif /*_AIX*/
  323. #ifdef __sun
  324. #define IEEE0_done
  325. #include <ieeefp.h>
  326. static void
  327. ieee0(Void)
  328. {
  329. fpsetmask(FP_X_INV);
  330. }
  331. #endif /*__sparc*/
  332. #ifndef IEEE0_done
  333. static void
  334. ieee0(Void) {}
  335. #endif