Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
uninit.c
Go to the documentation of this file.
1 /* uninit.c is part of the libf2c source (libf2c.zip) in f2c available at
2  * http://www.netlib.org/f2c/. It is used by Rad to fill memory locations
3  * with NaN values so that uninitialized accesses of those memory locations
4  * will throw IEEE exceptions. The libf2c source includes a Notice file
5  * giving copyright and permission to use. The contents of this file
6  * appear immediately below.
7  */
8 /****************************************************************
9 Copyright 1990 - 1997 by AT&T, Lucent Technologies and Bellcore.
10 
11 Permission to use, copy, modify, and distribute this software
12 and its documentation for any purpose and without fee is hereby
13 granted, provided that the above copyright notice appear in all
14 copies and that both that the copyright notice and this
15 permission notice and warranty disclaimer appear in supporting
16 documentation, and that the names of AT&T, Bell Laboratories,
17 Lucent or Bellcore or any of their entities not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20 
21 AT&T, Lucent and Bellcore disclaim all warranties with regard to
22 this software, including all implied warranties of
23 merchantability and fitness. In no event shall AT&T, Lucent or
24 Bellcore be liable for any special, indirect or consequential
25 damages or any damages whatsoever resulting from loss of use,
26 data or profits, whether in an action of contract, negligence or
27 other tortious action, arising out of or in connection with the
28 use or performance of this software.
29 ****************************************************************/
30 
31 #include <stdio.h>
32 #include <string.h>
33 /*#include "arith.h"*/
34 
35 #define TYSHORT 2
36 #define TYLONG 3
37 #define TYREAL 4
38 #define TYDREAL 5
39 #define TYCOMPLEX 6
40 #define TYDCOMPLEX 7
41 #define TYINT1 11
42 #define TYQUAD 14
43 #ifndef Long
44 #define Long long
45 #endif
46 
47 #ifdef __mips
48 #define RNAN 0xffc00000
49 #define DNAN0 0xfff80000
50 #define DNAN1 0
51 #endif
52 
53 #ifdef _PA_RISC1_1
54 #define RNAN 0xffc00000
55 #define DNAN0 0xfff80000
56 #define DNAN1 0
57 #endif
58 
59 #ifndef RNAN
60 #define RNAN 0xff800001
61 #ifdef IEEE_MC68k
62 #define DNAN0 0xfff00000
63 #define DNAN1 1
64 #else
65 #define DNAN0 1
66 #define DNAN1 0xfff00000
67 #endif
68 #endif /*RNAN*/
69 
70 #ifdef KR_headers
71 #define Void /*void*/
72 #define FA7UL (unsigned Long) 0xfa7a7a7aL
73 #else
74 #define Void void
75 #define FA7UL 0xfa7a7a7aUL
76 #endif
77 
78 #ifdef __cplusplus
79 extern "C" {
80 #endif
81 
82 static void ieee0(Void);
83 
84 static unsigned Long rnan = RNAN,
87 
88 double _0 = 0.;
89 
90  void
91 #ifdef KR_headers
92 _uninit_f2c(x, type, len) void *x; int type; long len;
93 #else
94 _uninit_f2c(void *x, int type, long len)
95 #endif
96 {
97  static int first = 1;
98 
99  unsigned Long *lx, *lxe;
100 
101  if (first) {
102  first = 0;
103  ieee0();
104  }
105  if (len == 1)
106  switch(type) {
107  case TYINT1:
108  *(char*)x = 'Z';
109  return;
110  case TYSHORT:
111  *(unsigned short*)x = 0xfa7a;
112  break;
113  case TYLONG:
114  *(unsigned Long*)x = FA7UL;
115  return;
116  case TYQUAD:
117  case TYCOMPLEX:
118  case TYDCOMPLEX:
119  break;
120  case TYREAL:
121  *(unsigned Long*)x = rnan;
122  return;
123  case TYDREAL:
124  lx = (unsigned Long*)x;
125  lx[0] = dnan0;
126  lx[1] = dnan1;
127  return;
128  default:
129  printf("Surprise type %d in _uninit_f2c\n", type);
130  }
131  switch(type) {
132  case TYINT1:
133  memset(x, 'Z', len);
134  break;
135  case TYSHORT:
136  *(unsigned short*)x = 0xfa7a;
137  break;
138  case TYQUAD:
139  len *= 2;
140  /* no break */
141  case TYLONG:
142  lx = (unsigned Long*)x;
143  lxe = lx + len;
144  while(lx < lxe)
145  *lx++ = FA7UL;
146  break;
147  case TYCOMPLEX:
148  len *= 2;
149  /* no break */
150  case TYREAL:
151  lx = (unsigned Long*)x;
152  lxe = lx + len;
153  while(lx < lxe)
154  *lx++ = rnan;
155  break;
156  case TYDCOMPLEX:
157  len *= 2;
158  /* no break */
159  case TYDREAL:
160  lx = (unsigned Long*)x;
161  for(lxe = lx + 2*len; lx < lxe; lx += 2) {
162  lx[0] = dnan0;
163  lx[1] = dnan1;
164  }
165  }
166  }
167 #ifdef __cplusplus
168 }
169 #endif
170 
171 #ifndef MSpc
172 #ifdef MSDOS
173 #define MSpc
174 #else
175 #ifdef _WIN32
176 #define MSpc
177 #endif
178 #endif
179 #endif
180 
181 #ifdef MSpc
182 #define IEEE0_done
183 #include "float.h"
184 #include "signal.h"
185 
186  static void
187 ieee0(Void)
188 {
189 #ifndef __alpha
190  _control87(EM_DENORMAL | EM_UNDERFLOW | EM_INEXACT, MCW_EM);
191 #endif
192  /* With MS VC++, compiling and linking with -Zi will permit */
193  /* clicking to invoke the MS C++ debugger, which will show */
194  /* the point of error -- provided SIGFPE is SIG_DFL. */
195  signal(SIGFPE, SIG_DFL);
196  }
197 #endif /* MSpc */
198 
199 #ifdef __mips /* must link with -lfpe */
200 #define IEEE0_done
201 /* code from Eric Grosse */
202 #include <stdlib.h>
203 #include <stdio.h>
204 #include "/usr/include/sigfpe.h" /* full pathname for lcc -N */
205 #include "/usr/include/sys/fpu.h"
206 
207  static void
208 #ifdef KR_headers
209 ieeeuserhand(exception, val) unsigned exception[5]; int val[2];
210 #else
211 ieeeuserhand(unsigned exception[5], int val[2])
212 #endif
213 {
214  fflush(stdout);
215  fprintf(stderr,"ieee0() aborting because of ");
216  if(exception[0]==_OVERFL) fprintf(stderr,"overflow\n");
217  else if(exception[0]==_UNDERFL) fprintf(stderr,"underflow\n");
218  else if(exception[0]==_DIVZERO) fprintf(stderr,"divide by 0\n");
219  else if(exception[0]==_INVALID) fprintf(stderr,"invalid operation\n");
220  else fprintf(stderr,"\tunknown reason\n");
221  fflush(stderr);
222  abort();
223 }
224 
225  static void
226 #ifdef KR_headers
227 ieeeuserhand2(j) unsigned int **j;
228 #else
229 ieeeuserhand2(unsigned int **j)
230 #endif
231 {
232  fprintf(stderr,"ieee0() aborting because of confusion\n");
233  abort();
234 }
235 
236  static void
237 ieee0(Void)
238 {
239  int i;
240  for(i=1; i<=4; i++){
241  sigfpe_[i].count = 1000;
242  sigfpe_[i].trace = 1;
243  sigfpe_[i].repls = _USER_DETERMINED;
244  }
245  sigfpe_[1].repls = _ZERO; /* underflow */
246  handle_sigfpes( _ON,
247  _EN_UNDERFL|_EN_OVERFL|_EN_DIVZERO|_EN_INVALID,
248  ieeeuserhand,_ABORT_ON_ERROR,ieeeuserhand2);
249  }
250 #endif /* mips */
251 
252 #ifdef __linux__
253 #define IEEE0_done
254 #include "fpu_control.h"
255 
256 #ifdef __alpha__
257 #ifndef USE_setfpucw
258 #define __setfpucw(x) __fpu_control = (x)
259 #endif
260 #endif
261 
262 #ifndef _FPU_SETCW
263 #undef Can_use__setfpucw
264 #define Can_use__setfpucw
265 #endif
266 
267  static void
268 ieee0(Void)
269 {
270 #if (defined(__mc68000__) || defined(__mc68020__) || defined(mc68020) || defined (__mc68k__))
271 /* Reported 20010705 by Alan Bain <alanb@chiark.greenend.org.uk> */
272 /* Note that IEEE 754 IOP (illegal operation) */
273 /* = Signaling NAN (SNAN) + operation error (OPERR). */
274 #ifdef Can_use__setfpucw /* Has __setfpucw gone missing from S.u.S.E. 6.3? */
275  __setfpucw(_FPU_IEEE + _FPU_DOUBLE + _FPU_MASK_OPERR + _FPU_MASK_DZ + _FPU_MASK_SNAN+_FPU_MASK_OVFL);
276 #else
277  __fpu_control = _FPU_IEEE + _FPU_DOUBLE + _FPU_MASK_OPERR + _FPU_MASK_DZ + _FPU_MASK_SNAN+_FPU_MASK_OVFL;
278  _FPU_SETCW(__fpu_control);
279 #endif
280 
281 #elif (defined(__powerpc__)||defined(_ARCH_PPC)||defined(_ARCH_PWR)) /* !__mc68k__ */
282 /* Reported 20011109 by Alan Bain <alanb@chiark.greenend.org.uk> */
283 
284 #ifdef Can_use__setfpucw
285 
286 /* The following is NOT a mistake -- the author of the fpu_control.h
287 for the PPC has erroneously defined IEEE mode to turn on exceptions
288 other than Inexact! Start from default then and turn on only the ones
289 which we want*/
290 
291  __setfpucw(_FPU_DEFAULT + _FPU_MASK_IM+_FPU_MASK_OM+_FPU_MASK_UM);
292 
293 #else /* PPC && !Can_use__setfpucw */
294 
295  __fpu_control = _FPU_DEFAULT +_FPU_MASK_OM+_FPU_MASK_IM+_FPU_MASK_UM;
296  _FPU_SETCW(__fpu_control);
297 
298 #endif /*Can_use__setfpucw*/
299 
300 #else /* !(mc68000||powerpc) */
301 
302 #ifdef _FPU_IEEE
303 #ifndef _FPU_EXTENDED /* e.g., ARM processor under Linux */
304 #define _FPU_EXTENDED 0
305 #endif
306 #ifndef _FPU_DOUBLE
307 #define _FPU_DOUBLE 0
308 #endif
309 #ifdef Can_use__setfpucw /* Has __setfpucw gone missing from S.u.S.E. 6.3? */
310  __setfpucw(_FPU_IEEE - _FPU_EXTENDED + _FPU_DOUBLE - _FPU_MASK_IM - _FPU_MASK_ZM - _FPU_MASK_OM);
311 #else
312  __fpu_control = _FPU_IEEE - _FPU_EXTENDED + _FPU_DOUBLE - _FPU_MASK_IM - _FPU_MASK_ZM - _FPU_MASK_OM;
313  _FPU_SETCW(__fpu_control);
314 #endif
315 
316 #else /* !_FPU_IEEE */
317 
318  fprintf(stderr, "\n%s\n%s\n%s\n%s\n",
319  "WARNING: _uninit_f2c in libf2c does not know how",
320  "to enable trapping on this system, so f2c's -trapuv",
321  "option will not detect uninitialized variables unless",
322  "you can enable trapping manually.");
323  fflush(stderr);
324 
325 #endif /* _FPU_IEEE */
326 #endif /* __mc68k__ */
327  }
328 #endif /* __linux__ */
329 
330 #ifdef __alpha
331 #ifndef IEEE0_done
332 #define IEEE0_done
333 #include <machine/fpu.h>
334  static void
335 ieee0(Void)
336 {
337  ieee_set_fp_control(IEEE_TRAP_ENABLE_INV);
338  }
339 #endif /*IEEE0_done*/
340 #endif /*__alpha*/
341 
342 #ifdef __hpux
343 #define IEEE0_done
344 #define _INCLUDE_HPUX_SOURCE
345 #include <math.h>
346 
347 #ifndef FP_X_INV
348 #include <fenv.h>
349 #define fpsetmask fesettrapenable
350 #define FP_X_INV FE_INVALID
351 #endif
352 
353  static void
354 ieee0(Void)
355 {
356  fpsetmask(FP_X_INV);
357  }
358 #endif /*__hpux*/
359 
360 #ifdef _AIX
361 #define IEEE0_done
362 #include <fptrap.h>
363 
364  static void
365 ieee0(Void)
366 {
367  fp_enable(TRP_INVALID);
368  fp_trap(FP_TRAP_SYNC);
369  }
370 #endif /*_AIX*/
371 
372 #ifdef __sun
373 #define IEEE0_done
374 #include <ieeefp.h>
375 
376  static void
377 ieee0(Void)
378 {
379  fpsetmask(FP_X_INV);
380  }
381 #endif /*__sparc*/
382 
383 #ifndef IEEE0_done
384  static void
386 #endif
#define TYINT1
Definition: uninit.c:41
unsigned Long * lxe
Definition: uninit.c:99
#define TYDREAL
Definition: uninit.c:38
#define TYDCOMPLEX
Definition: uninit.c:40
static unsigned Long rnan
Definition: uninit.c:84
double _0
Definition: uninit.c:88
#define DNAN1
Definition: uninit.c:66
#define FA7UL
Definition: uninit.c:75
expr val()
#define Long
Definition: uninit.c:44
static unsigned Long dnan0
Definition: uninit.c:85
#define Void
Definition: uninit.c:74
static void ieee0(Void)
Definition: uninit.c:385
#define TYSHORT
Definition: uninit.c:35
void
Definition: uninit.c:96
static unsigned Long dnan1
Definition: uninit.c:86
#define TYREAL
Definition: uninit.c:37
#define RNAN
Definition: uninit.c:60
#define TYCOMPLEX
Definition: uninit.c:39
#define DNAN0
Definition: uninit.c:65
unsigned Long * lx
Definition: uninit.c:99
#define TYLONG
Definition: uninit.c:36
#define TYQUAD
Definition: uninit.c:42