uninit.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. #include "/usr/include/sigfpe.h" /* full pathname for lcc -N */
  171. #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. #ifdef __linux__
  215. #define IEEE0_done
  216. #include "fpu_control.h"
  217. #ifdef __alpha__
  218. #ifndef USE_setfpucw
  219. #define __setfpucw(x) __fpu_control = (x)
  220. #endif
  221. #endif
  222. #ifndef _FPU_SETCW
  223. #undef Can_use__setfpucw
  224. #define Can_use__setfpucw
  225. #endif
  226. static void
  227. ieee0(Void)
  228. {
  229. #if (defined(__mc68000__) || defined(__mc68020__) || defined(mc68020) || defined (__mc68k__))
  230. /* Reported 20010705 by Alan Bain <alanb@chiark.greenend.org.uk> */
  231. /* Note that IEEE 754 IOP (illegal operation) */
  232. /* = Signaling NAN (SNAN) + operation error (OPERR). */
  233. #ifdef Can_use__setfpucw
  234. __setfpucw(_FPU_IEEE + _FPU_DOUBLE + _FPU_MASK_OPERR + _FPU_MASK_DZ + _FPU_MASK_SNAN+_FPU_MASK_OVFL);
  235. #else
  236. __fpu_control = _FPU_IEEE + _FPU_DOUBLE + _FPU_MASK_OPERR + _FPU_MASK_DZ + _FPU_MASK_SNAN+_FPU_MASK_OVFL;
  237. _FPU_SETCW(__fpu_control);
  238. #endif
  239. #elif (defined(__powerpc__)||defined(_ARCH_PPC)||defined(_ARCH_PWR)) /* !__mc68k__ */
  240. /* Reported 20011109 by Alan Bain <alanb@chiark.greenend.org.uk> */
  241. #ifdef Can_use__setfpucw
  242. /* The following is NOT a mistake -- the author of the fpu_control.h
  243. for the PPC has erroneously defined IEEE mode to turn on exceptions
  244. other than Inexact! Start from default then and turn on only the ones
  245. which we want*/
  246. __setfpucw(_FPU_DEFAULT + _FPU_MASK_IM+_FPU_MASK_OM+_FPU_MASK_UM);
  247. #else /* PPC && !Can_use__setfpucw */
  248. __fpu_control = _FPU_DEFAULT +_FPU_MASK_OM+_FPU_MASK_IM+_FPU_MASK_UM;
  249. _FPU_SETCW(__fpu_control);
  250. #endif /*Can_use__setfpucw*/
  251. #else /* !(mc68000||powerpc) */
  252. #ifdef _FPU_IEEE
  253. #ifndef _FPU_EXTENDED /* e.g., ARM processor under Linux */
  254. #define _FPU_EXTENDED 0
  255. #endif
  256. #ifndef _FPU_DOUBLE
  257. #define _FPU_DOUBLE 0
  258. #endif
  259. #ifdef Can_use__setfpucw /* pre-1997 (?) Linux */
  260. __setfpucw(_FPU_IEEE - _FPU_MASK_IM - _FPU_MASK_ZM - _FPU_MASK_OM);
  261. #else
  262. #ifdef UNINIT_F2C_PRECISION_53 /* 20051004 */
  263. /* unmask invalid, etc., and change rounding precision to double */
  264. __fpu_control = _FPU_IEEE - _FPU_EXTENDED + _FPU_DOUBLE - _FPU_MASK_IM - _FPU_MASK_ZM - _FPU_MASK_OM;
  265. _FPU_SETCW(__fpu_control);
  266. #else
  267. /* unmask invalid, etc., and keep current rounding precision */
  268. fpu_control_t cw;
  269. _FPU_GETCW(cw);
  270. cw &= ~(_FPU_MASK_IM | _FPU_MASK_ZM | _FPU_MASK_OM);
  271. _FPU_SETCW(cw);
  272. #endif
  273. #endif
  274. #else /* !_FPU_IEEE */
  275. fprintf(stderr, "\n%s\n%s\n%s\n%s\n",
  276. "WARNING: _uninit_f2c in libf2c does not know how",
  277. "to enable trapping on this system, so f2c's -trapuv",
  278. "option will not detect uninitialized variables unless",
  279. "you can enable trapping manually.");
  280. fflush(stderr);
  281. #endif /* _FPU_IEEE */
  282. #endif /* __mc68k__ */
  283. }
  284. #endif /* __linux__ */
  285. #ifdef __alpha
  286. #ifndef IEEE0_done
  287. #define IEEE0_done
  288. #include <machine/fpu.h>
  289. static void
  290. ieee0(Void)
  291. {
  292. ieee_set_fp_control(IEEE_TRAP_ENABLE_INV);
  293. }
  294. #endif /*IEEE0_done*/
  295. #endif /*__alpha*/
  296. #ifdef __hpux
  297. #define IEEE0_done
  298. #define _INCLUDE_HPUX_SOURCE
  299. #include <math.h>
  300. #ifndef FP_X_INV
  301. #include <fenv.h>
  302. #define fpsetmask fesettrapenable
  303. #define FP_X_INV FE_INVALID
  304. #endif
  305. static void
  306. ieee0(Void)
  307. {
  308. fpsetmask(FP_X_INV);
  309. }
  310. #endif /*__hpux*/
  311. #ifdef _AIX
  312. #define IEEE0_done
  313. #include <fptrap.h>
  314. static void
  315. ieee0(Void)
  316. {
  317. fp_enable(TRP_INVALID);
  318. fp_trap(FP_TRAP_SYNC);
  319. }
  320. #endif /*_AIX*/
  321. #ifdef __sun
  322. #define IEEE0_done
  323. #include <ieeefp.h>
  324. static void
  325. ieee0(Void)
  326. {
  327. fpsetmask(FP_X_INV);
  328. }
  329. #endif /*__sparc*/
  330. #ifndef IEEE0_done
  331. static void
  332. ieee0(Void) {}
  333. #endif