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