Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_FloatingPointTrap.cpp
Go to the documentation of this file.
1 #if 0 // Disabled!
2 // @HEADER
3 // *****************************************************************************
4 // Teuchos: Common Tools Package
5 //
6 // Copyright 2004 NTESS and the Teuchos contributors.
7 // SPDX-License-Identifier: BSD-3-Clause
8 // *****************************************************************************
9 // @HEADER
10 
12 #include "Teuchos_Assert.hpp"
13 
14 
15 //
16 // Implementation of floating point control
17 //
18 
19 
20 // ???
21 
22 
23 //
24 // We have floating point control!
25 //
26 
27 
28 static void ieee0(bool enableTrap);
29 
30 
31 void Teuchos::doFloatingPointTrap(bool enableTrap)
32 {
33  ieee0(enableTrap);
34 }
35 
36 
37 
38 //
39 // Stuff from uninit.c from f2c and them from Sacado!
40 //
41 
42 
43 //
44 // Up front stuff
45 //
46 
47 
48 #include <cstdio>
49 #include <cstring>
50 
51 #define TYSHORT 2
52 #define TYLONG 3
53 #define TYREAL 4
54 #define TYDREAL 5
55 #define TYCOMPLEX 6
56 #define TYDCOMPLEX 7
57 #define TYINT1 11
58 #define TYQUAD 14
59 
60 #ifndef Long
61 #define Long long
62 #endif // Long
63 
64 #ifdef __mips
65 #define RNAN 0xffc00000
66 #define DNAN0 0xfff80000
67 #define DNAN1 0
68 #endif // __mips
69 
70 #ifdef _PA_RISC1_1
71 #define RNAN 0xffc00000
72 #define DNAN0 0xfff80000
73 #define DNAN1 0
74 #endif // _PA_RISC1_1
75 
76 #ifndef RNAN
77 # define RNAN 0xff800001
78 # ifdef IEEE_MC68k
79 # define DNAN0 0xfff00000
80 # define DNAN1 1
81 # else
82 # define DNAN0 1
83 # define DNAN1 0xfff00000
84 # endif
85 #endif /*RNAN*/
86 
87 
88 static unsigned Long rnan = RNAN, dnan0 = DNAN0, dnan1 = DNAN1;
89 
90 double _0 = 0.;
91 
92 #ifndef MSpc
93 # ifdef MSDOS
94 # define MSpc
95 # else
96 # ifdef _WIN32
97 # define MSpc
98 # endif
99 # endif
100 #endif
101 
102 
103 //
104 // MSpc
105 //
106 
107 
108 #ifdef MSpc
109 
110 #define IEEE0_done
111 #include "cfloat"
112 #include "csignal"
113 
114 static void ieee0(bool enableTrap)
115 {
117  enableTrap == false, std::logic_error,
118  "Error, don't know how to turn off trap for MSpc!"
119  );
120 #ifndef __alpha
121  _control87(EM_DENORMAL | EM_UNDERFLOW | EM_INEXACT, MCW_EM);
122 #endif
123  /* With MS VC++, compiling and linking with -Zi will permit */
124  /* clicking to invoke the MS C++ debugger, which will show */
125  /* the point of error -- provided SIGFPE is SIG_DFL. */
126  signal(SIGFPE, SIG_DFL);
127 }
128 
129 #endif // MSpc
130 
131 
132 //
133 // MIPS
134 //
135 
136 
137 #ifdef __mips /* must link with -lfpe */
138 
139 #define IEEE0_done
140 #include <cstdlib>
141 #include <cstdio>
142 #include "/usr/include/sigfpe.h" /* full pathname for lcc -N */
143 #include "/usr/include/sys/fpu.h"
144 
145 static void ieeeuserhand(unsigned std::exception[5], int val[2])
146 {
147  fflush(stdout);
148  fprintf(stderr,"ieee0() aborting because of ");
149  if(std::exception[0]==_OVERFL) fprintf(stderr,"overflow\n");
150  else if(std::exception[0]==_UNDERFL) fprintf(stderr,"underflow\n");
151  else if(std::exception[0]==_DIVZERO) fprintf(stderr,"divide by 0\n");
152  else if(std::exception[0]==_INVALID) fprintf(stderr,"invalid operation\n");
153  else fprintf(stderr,"\tunknown reason\n");
154  fflush(stderr);
155  abort();
156 }
157 
158 static void ieeeuserhand2(unsigned int **j)
159 {
160  fprintf(stderr,"ieee0() aborting because of confusion\n");
161  abort();
162 }
163 
164 static void ieee0(bool enableTrap)
165 {
167  enableTrap == false, std::logic_error,
168  "Error, don't know how to turn off trap for MIPS!"
169  );
170  int i;
171  for(i=1; i<=4; i++){
172  sigfpe_[i].count = 1000;
173  sigfpe_[i].trace = 1;
174  sigfpe_[i].repls = _USER_DETERMINED;
175  }
176  sigfpe_[1].repls = _ZERO; /* underflow */
177  handle_sigfpes( _ON,
178  _EN_UNDERFL|_EN_OVERFL|_EN_DIVZERO|_EN_INVALID,
179  ieeeuserhand,_ABORT_ON_ERROR,ieeeuserhand2);
180  }
181 #endif /* mips */
182 
183 
184 //
185 // Linux
186 //
187 
188 
189 #ifdef __linux__
190 
191 #define IEEE0_done
192 #include "fpu_control.h"
193 
194 #ifdef __alpha__
195 # ifndef USE_setfpucw
196 # define __setfpucw(x) __fpu_control = (x)
197 # endif
198 #endif
199 
200 #ifndef _FPU_SETCW
201 # undef Can_use__setfpucw
202 # define Can_use__setfpucw
203 #endif
204 
205 static void ieee0(bool enableTrap)
206 {
207 
209  enableTrap == false, std::logic_error,
210  "Error, don't know how to turn off trap for LINUX!"
211  );
212 
213 #if (defined(__mc68000__) || defined(__mc68020__) || defined(mc68020) || defined (__mc68k__))
214 
215  /* Reported 20010705 by Alan Bain <alanb@chiark.greenend.org.uk> */
216  /* Note that IEEE 754 IOP (illegal operation) */
217  /* = Signaling NAN (SNAN) + operation error (OPERR). */
218 
219 #ifdef Can_use__setfpucw /* Has __setfpucw gone missing from S.u.S.E. 6.3? */
220  __setfpucw(_FPU_IEEE + _FPU_DOUBLE + _FPU_MASK_OPERR + _FPU_MASK_DZ + _FPU_MASK_SNAN + _FPU_MASK_OVFL);
221 #else
222  __fpu_control = _FPU_IEEE + _FPU_DOUBLE + _FPU_MASK_OPERR + _FPU_MASK_DZ + _FPU_MASK_SNAN + _FPU_MASK_OVFL;
223  _FPU_SETCW(__fpu_control);
224 #endif
225 
226 #elif (defined(__powerpc__)||defined(_ARCH_PPC)||defined(_ARCH_PWR)) /* !__mc68k__ */
227  /* Reported 20011109 by Alan Bain <alanb@chiark.greenend.org.uk> */
228 
229 #ifdef Can_use__setfpucw
230 
231  /* The following is NOT a mistake -- the author of the fpu_control.h
232  for the PPC has erroneously defined IEEE mode to turn on exceptions
233  other than Inexact! Start from default then and turn on only the ones
234  which we want*/
235 
236  __setfpucw(_FPU_DEFAULT + _FPU_MASK_IM+_FPU_MASK_OM+_FPU_MASK_UM);
237 
238 #else /* PPC && !Can_use__setfpucw */
239 
240  __fpu_control = _FPU_DEFAULT +_FPU_MASK_OM+_FPU_MASK_IM+_FPU_MASK_UM;
241  _FPU_SETCW(__fpu_control);
242 
243 #endif /*Can_use__setfpucw*/
244 
245 #else /* !(mc68000||powerpc) */
246 
247 #ifdef _FPU_IEEE
248 # ifndef _FPU_EXTENDED /* e.g., ARM processor under Linux */
249 # define _FPU_EXTENDED 0
250 #endif
251 
252 #ifndef _FPU_DOUBLE
253 # define _FPU_DOUBLE 0
254 #endif
255 
256 #ifdef Can_use__setfpucw /* Has __setfpucw gone missing from S.u.S.E. 6.3? */
257  __setfpucw(_FPU_IEEE - _FPU_EXTENDED + _FPU_DOUBLE - _FPU_MASK_IM - _FPU_MASK_ZM - _FPU_MASK_OM);
258 #else
259  __fpu_control = _FPU_IEEE - _FPU_EXTENDED + _FPU_DOUBLE - _FPU_MASK_IM - _FPU_MASK_ZM - _FPU_MASK_OM;
260  _FPU_SETCW(__fpu_control);
261 #endif
262 
263 #else /* !_FPU_IEEE */
264 
266  true, std::logic_error,
267  "Error, don't know how to trap floating-point errors on this Linux system!"
268  );
269 
270 #endif /* _FPU_IEEE */
271 
272 #endif /* __mc68k__ */
273 
274 } // ieee0()
275 
276 #endif /* __linux__ */
277 
278 
279 //
280 // Alpha
281 //
282 
283 
284 #ifdef __alpha
285 
286 #ifndef IEEE0_done
287 
288 #define IEEE0_done
289 #include <machine/fpu.h>
290 
291 static void ieee0(bool enableTrap)
292 {
294  enableTrap == false, std::logic_error,
295  "Error, don't know how to turn off trap for Alpha!"
296  );
297  ieee_set_fp_control(IEEE_TRAP_ENABLE_INV);
298 }
299 
300 #endif /*IEEE0_done*/
301 
302 #endif /*__alpha*/
303 
304 
305 //
306 // hpux
307 //
308 
309 
310 
311 #ifdef __hpux
312 
313 #define IEEE0_done
314 #define _INCLUDE_HPUX_SOURCE
315 
316 #include <cmath>
317 
318 #ifndef FP_X_INV
319 # include <fenv.h>
320 # define fpsetmask fesettrapenable
321 # define FP_X_INV FE_INVALID
322 #endif
323 
324 static void ieee0(bool enableTrap)
325 {
327  enableTrap == false, std::logic_error,
328  "Error, don't know how to turn off trap for HPUX!"
329  );
330  fpsetmask(FP_X_INV);
331 }
332 
333 #endif /*__hpux*/
334 
335 
336 //
337 // AIX
338 //
339 
340 
341 #ifdef _AIX
342 
343 #define IEEE0_done
344 #include <fptrap.h>
345 
346 static void ieee0(bool enableTrap)
347 {
349  enableTrap == false, std::logic_error,
350  "Error, don't know how to turn off trap for AIX!"
351  );
352  fp_enable(TRP_INVALID);
353  fp_trap(FP_TRAP_SYNC);
354 }
355 
356 #endif /*_AIX*/
357 
358 
359 //
360 // SUN
361 //
362 
363 
364 #ifdef __sun
365 
366 #define IEEE0_done
367 #include <ieeefp.h>
368 
369 static void ieee0(bool enableTrap)
370 {
372  enableTrap == false, std::logic_error,
373  "Error, don't know how to turn off trap for SUN!"
374  );
375  fpsetmask(FP_X_INV);
376 }
377 
378 #endif // __sun
379 
380 
381 //
382 // Default (none)
383 //
384 
385 
386 #ifndef IEEE0_done
387 
388 static void ieee0(bool enableTrap)
389 {
391  true, std::logic_error,
392  "Error, Don't know how to implement floating-point traps on this platform!"
393  );
394 }
395 
396 #endif // IEEE0_done
397 
398 #endif // 0 // Disabled!
void doFloatingPointTrap(bool enableTrap)
Turn on or off a floating point trap.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.