/[escript]/trunk/esysUtils/src/maths.h
ViewVC logotype

Annotation of /trunk/esysUtils/src/maths.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2884 - (hide annotations)
Thu Jan 28 05:00:59 2010 UTC (9 years, 7 months ago) by jfenwick
Original Path: trunk/paso/src/Common.h
File MIME type: text/plain
File size: 8478 byte(s)
Updated various nan checks to consider the windows _isnan

The default compiler flags have changed as well.
+ intel will now take -std=c99 instead of -ansi
+ gcc has -ansi removed which means it defaults to gnu99

We could have set gcc to -std=c99 as well but that gives a 
warning on g++ which gets converted into an error by our
pedantic warning.

Rationale:
We need something more than ansi to get proper nan handling.
- We currently don't have any code which does not comply with ansi
  but the nan checks don't work.

Impact:
If we want our code to still be able to compile on older compilers
(at reduced functionality) we need to be careful not to introduce other
c99-isms.
If we don't care, then it's time for some celebratory // comments.


1 ksteube 1312
2     /*******************************************************
3 ksteube 1811 *
4 jfenwick 2881 * Copyright (c) 2003-2010 by University of Queensland
5 ksteube 1811 * Earth Systems Science Computational Center (ESSCC)
6     * http://www.uq.edu.au/esscc
7     *
8     * Primary Business: Queensland, Australia
9     * Licensed under the Open Software License version 3.0
10     * http://www.opensource.org/licenses/osl-3.0.php
11     *
12     *******************************************************/
13 dhawcroft 631
14 ksteube 1811
15 jgs 150 #ifndef INC_PASO_COMMON
16     #define INC_PASO_COMMON
17    
18     /**************************************************************/
19    
20     /* Finley finite element solver: common include file */
21    
22     /**************************************************************/
23    
24     /* Copyrights by ACcESS Australia, 2003 */
25     /* Version: $Id$ */
26    
27     /**************************************************************/
28    
29     /* some system values */
30 phornby 1628 /* FIXME: This is not satisfactory. */
31     /* _ECC, __INTEL_COMPILER, and other */
32     /* intel compiler pre-defines need to be handled */
33     /* (__ICL, __ICC come to mind) */
34 phornby 2019 #if defined(_WIN32) && defined(__INTEL_COMPILER)
35 woo409 757 #include <mathimf.h>
36     #else
37     #include <math.h>
38     #endif
39 jgs 150
40 gross 1565 #if (defined __OPENMP)
41     #include <omp.h>
42     #endif
43    
44 jgs 150 #include <float.h>
45     #include <stdio.h>
46     #include <limits.h>
47     #include <stdlib.h>
48     #include <string.h>
49    
50     #define LenString_MAX FILENAME_MAX*2
51     #define LenErrorMsg_MAX LenString_MAX
52    
53     /* on some arcitectures it could be a good idea to use long rather than int */
54     /* this has not really been tested */
55    
56     typedef int dim_t;
57     typedef int index_t;
58     typedef int bool_t;
59     typedef int type_t;
60     typedef int err_t;
61    
62     #define INDEX_T_MAX INT_MAX
63 gross 1716 #define INDEX_T_MIN -INT_MAX
64 jgs 150 #define EPSILON DBL_EPSILON
65 gross 1370 #define LARGE_POSITIVE_FLOAT DBL_MAX
66 gross 1804 #define SMALL_NEGATIVE_FLOAT -DBL_MAX
67 jfenwick 2879 /*#ifndef NAN
68 gross 2748 #define NAN (0.0/0.0)
69     #endif
70 jfenwick 2879 */
71     /*#define IS_NAN(__VAL__) ( (__VAL__) == NAN )*/ /* this does not work */
72 gross 2748 /* #define IS_NAN(__VAL__) ( ! ( ( (__VAL__) >= 0. ) || ( (__VAL__) <= 0. ) ) ) this does not work */
73 jgs 150
74 jfenwick 2879 #ifdef isnan
75     #define IS_NAN(__VAL__) (isnan(__VAL__))
76 jfenwick 2884 #elif defined _isnan
77     #define IS_NAN(__VAL__) (_isnan(__VAL__))
78 jfenwick 2879 #else
79     /* If we do not have isnan then we can't reliably check for NaN - return false */
80     #define IS_NAN(__VAL__) (0)
81     #endif
82    
83 jgs 150 /**************************************************************/
84    
85     /* some useful functions: */
86    
87     #define FALSE 0
88     #define TRUE 1
89     #define UNKNOWN -1
90     #define DBLE(_x_) (double)(_x_)
91     #define INDEX1(_X1_) (_X1_)
92     #define INDEX2(_X1_,_X2_,_N1_) ((_X1_)+(_N1_)*(_X2_))
93     #define INDEX3(_X1_,_X2_,_X3_,_N1_,_N2_) ((_X1_)+(_N1_)*INDEX2(_X2_,_X3_,_N2_))
94     #define INDEX4(_X1_,_X2_,_X3_,_X4_,_N1_,_N2_,_N3_) ((_X1_)+(_N1_)*INDEX3(_X2_,_X3_,_X4_,_N2_,_N3_))
95     #define INDEX5(_X1_,_X2_,_X3_,_X4_,_X5_,_N1_,_N2_,_N3_,_N4_) ((_X1_)+(_N1_)*INDEX4(_X2_,_X3_,_X4_,_X5_,_N2_,_N3_,_N4_))
96     #define INDEX6(_X1_,_X2_,_X3_,_X4_,_X5_,_X6_,_N1_,_N2_,_N3_,_N4_,_N5_) ((_X1_)+(_N1_)*INDEX5(_X2_,_X3_,_X4_,_X5_,_X6_,_N2_,_N3_,_N4_,_N5_))
97    
98     #define MAX(_arg1_,_arg2_) ((_arg1_)>(_arg2_) ? (_arg1_) : (_arg2_))
99 ksteube 1312 #define MAX3(_arg1_,_arg2_,_arg3_) MAX(_arg1_,MAX(_arg2_,_arg3_))
100 jgs 150 #define MIN(_arg1_,_arg2_) ((_arg1_)>(_arg2_) ? (_arg2_) : (_arg1_))
101 ksteube 1312 #define MIN3(_arg1_,_arg2_,_arg3_) MIN(_arg1_,MIN(_arg2_,_arg3_))
102 jgs 150 #define ABS(_arg_) MAX((_arg_),-(_arg_))
103 gross 1361 #define SIGN(_arg_) ((_arg_)>0 ? 1 : ((_arg_)<0 ? -1 : 0 ))
104 gross 1562 #define SWAP(_a0_,_a1_,_type_) { \
105     _type_ s; \
106     s=(_a0_); \
107     _a0_= (_a1_); \
108     _a1_=s; \
109     }
110 gross 2554 #define XNOR(_a0_,_a1_) ( ( (_a0_) && (_a1_) ) || ( !(_a0_) && !(_a1_) ) )
111 jgs 150 /**************************************************************/
112     /* memory allocation: */
113 phornby 1628 /* Wise to not use PASO_MALLOC/FREE/REALLOC and */
114     /* PASO_THREAD... directly. These are only for tailoring */
115     /* the main macros that follow */
116     /**************************************************************/
117 jgs 150
118 phornby 1628 #if defined(_WIN32) /* Use python for memory management on windows. */
119 ksteube 1312
120 phornby 1628 #include <python.h>
121 ksteube 1312
122 phornby 1628 #define PASO_MALLOC PyMem_Malloc
123     #define PASO_FREE PyMem_Free
124     #define PASO_REALLOC PyMem_Realloc
125 bcumming 730
126 jgs 150 #else
127 phornby 1628
128 ksteube 1662 #include <stdlib.h>
129 phornby 1628
130     #define PASO_MALLOC malloc
131     #define PASO_FREE free
132     #define PASO_REALLOC realloc
133    
134 jgs 150 #endif
135    
136 phornby 2028 #ifndef __const
137     # if (defined __STDC__ && __STDC__)
138     # define __const const
139     # else
140     # define __const
141     # endif
142     #endif
143 gross 1407
144 phornby 2028
145 phornby 1628 /* FIXME: This is not satisfactory. */
146     /* _ECC, __INTEL_COMPILER, and other */
147     /* intel compiler pre-defines need to be handled */
148     /* (__ICL, __ICC come to mind) */
149     /* Also, _WIN32 may take this branch one day... */
150     /* SO KEEP ALL THREAD_MEMALLOC/FREEs CONFINED TO THE PASO LIBRARY. */
151    
152     #if defined(__ECC) && defined(_OPENMP) /* ECC version of intel compiler with openmp. */
153     #include <omp.h>
154     #define PASO_THREAD_MALLOC kmp_malloc
155     #define PASO_THREAD_FREE kmp_free
156     #else
157     #define PASO_THREAD_MALLOC PASO_MALLOC
158     #define PASO_THREAD_FREE PASO_FREE
159     #endif
160    
161 phornby 2056
162     /* Prepare for the day that this becomes sharable. */
163     /* and we wish to do multi-file optimisations on windows */
164    
165     #define PASO_DLL_API
166    
167     #ifdef _WIN32
168     # ifndef PASO_STATIC_LIB
169     # undef PASO_DLL_API
170     # ifdef PASO_EXPORTS
171     # define PASO_DLL_API __declspec(dllexport)
172     # else
173     # define PASO_DLL_API __declspec(dllimport)
174     # endif
175     # endif
176     #endif
177    
178    
179 phornby 1628 /******************The main macros ************************************/
180    
181     #define MEMALLOC(_LENGTH_,_TYPE_) \
182     (_TYPE_*) PASO_MALLOC(((size_t)(_LENGTH_))*sizeof(_TYPE_))
183    
184     /* do {} while(0) - an old trick for bracketing a macro that */
185     /* makes sure a semi-colon does no harm. */
186    
187     #define MEMFREE(_PTR_) \
188     do \
189     { \
190     if ((void *)(_PTR_) != NULL ) { PASO_FREE(_PTR_); (_PTR_) = NULL; } \
191     } while(0)
192    
193 phornby 1875 #define MEMREALLOC(_RETP_,_POINTER_,_LENGTH_,_TYPE_) \
194 phornby 1628 do \
195     { \
196     if( (_POINTER_)!=NULL ) \
197     { \
198 phornby 1875 _RETP_ = (_TYPE_*)PASO_REALLOC((void*)(_POINTER_), \
199 phornby 1628 ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
200     } \
201     else \
202     { \
203 phornby 1875 _RETP_ = (_TYPE_*)PASO_MALLOC( ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
204 phornby 1628 } \
205     } while(0)
206    
207     #define TMPMEMALLOC MEMALLOC
208     #define TMPMEMFREE MEMFREE
209 phornby 1875 #define TMPMEMREALLOC MEMREALLOC
210 phornby 1628
211     #define THREAD_MEMALLOC(_LENGTH_,_TYPE_) \
212     (_TYPE_*) PASO_THREAD_MALLOC/**/(((size_t)(_LENGTH_))*sizeof(_TYPE_))
213    
214     #define THREAD_MEMFREE(_PTR_) \
215     do \
216     { \
217     if ((void *)(_PTR_) != NULL ) { PASO_THREAD_FREE(_PTR_); (_PTR_) = NULL; } \
218     } while(0)
219    
220    
221     /*
222     This was useful for seeing what memory is being allocated if you get an "Out of memory" error
223     if used again, bracket with do {} while(0)
224     #define TMPMEMALLOC(_LENGTH_,_TYPE_) (printf("TMPMEMALLOC at %s %d #bytes=%d*%d=%d\n", __FILE__, __LINE__, _LENGTH_, sizeof(_TYPE_), ((size_t)(_LENGTH_) * sizeof(_TYPE_))) , (_TYPE_*) malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_)))
225    
226     This was useful for seeing where memory is being freed if you get an "glibc detected...free" error
227     if used again, bracket with do {} while(0)
228     #define TMPMEMFREE(_PTR_) if ((void *)(_PTR_) != NULL ) { printf("TMPMEMFREE AAA %s %d\n", __FILE__, __LINE__); free(_PTR_); (_PTR_) = NULL; printf("TMPMEMFREE BBB %s %d\n", __FILE__, __LINE__); }
229     */
230    
231 jgs 150 #endif /* #ifndef INC_PASO_COMMON */

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26