Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Die on error when determining minimal type boundary
[simgrid.git] / acmacro / gras_arch.m4
1 # BEGIN OF GRAS ARCH CHECK
2 dnl
3 dnl GRAS_DO_CHECK_SIZEOF: Get the size of a datatype (even in cross-compile)
4 dnl  1: type tested
5 dnl  2: extra include lines
6 dnl  3: extra sizes to test
7 dnl ("adapted" from openldap)
8 dnl
9 AC_DEFUN([GRAS_DO_CHECK_SIZEOF],
10 [changequote(<<, >>)dnl 
11  dnl The cache variable name (and of the result). 
12  define(<<GRAS_CHECK_SIZEOF_RES>>, translit(ac_cv_sizeof_$1, [ *()], [_pLR]))dnl
13  changequote([, ])dnl 
14  AC_CACHE_VAL(GRAS_CHECK_SIZEOF_RES, 
15  [for ac_size in 4 8 1 2 16 $3 ; do # List sizes in rough order of prevalence. 
16    AC_COMPILE_IFELSE(AC_LANG_PROGRAM([#include "confdefs.h" 
17    #include <sys/types.h> 
18    $2 
19    ], [switch (0) case 0: case (sizeof ($1) == $ac_size):;]), GRAS_CHECK_SIZEOF_RES=$ac_size) 
20    if test x$GRAS_CHECK_SIZEOF_RES != x ; then break; fi 
21   done 
22  ]) 
23 ])
24
25 dnl
26 dnl GRAS_CHECK_SIZEOF: Get the size of a datatype (even in cross-compile), with msg display, and define the result
27 dnl  1: type tested
28 dnl  2: extra include lines
29 dnl  3: extra sizes to test
30 dnl ("adapted" from openldap)
31 dnl
32 AC_DEFUN([GRAS_CHECK_SIZEOF],
33 [AC_MSG_CHECKING(size of $1) 
34 GRAS_DO_CHECK_SIZEOF($1,$2)
35 if test x$GRAS_CHECK_SIZEOF_RES = x ; then 
36   AC_MSG_ERROR([cannot determine a size for $1]) 
37 fi 
38 AC_MSG_RESULT($GRAS_CHECK_SIZEOF_RES) 
39 ])
40
41 dnl
42 dnl GRAS_TWO_COMPLIMENT([type]): Make sure the type is two-compliment
43 dnl warning, this does not work with char (quite logical)
44 dnl
45 AC_DEFUN([GRAS_TWO_COMPLIMENT],
46 [
47 AC_MSG_CHECKING(whether $1 is two-compliment)
48 AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include "confdefs.h"
49 union {
50    signed $1 testInt;
51    unsigned char bytes[sizeof($1)];
52 } intTester;
53 ]],[[
54    intTester.testInt = -2;
55    return ((unsigned int)intTester.bytes[0] +
56            (unsigned int)intTester.bytes[sizeof($1) - 1]) - 509; /* should be 0 */
57 ]])], dnl end of AC LANG PROGRAM
58 [two_compliment=yes],[two_compliment=no] )dnl end of AC COMPILE IFELSE
59
60 AC_MSG_RESULT($two_compliment)
61 if test x$two_compliment != xyes ; then
62   AC_MSG_ERROR([GRAS works only two-compliment integers (yet)])
63 fi
64 ])
65
66 dnl
67 dnl GRAS_SIGNED_SIZEOF: Get the size of the datatype, and make sure that
68 dnl signed, unsigned and unspecified have the same size
69 dnl
70 AC_DEFUN([GRAS_SIGNED_SIZEOF],
71 [AC_MSG_CHECKING(size of $1)
72 GRAS_DO_CHECK_SIZEOF($1,$2)
73 unspecif=$GRAS_CHECK_SIZEOF_RES
74 if test x$unspecif = x ; then
75   AC_MSG_ERROR([cannot determine a size for $1]) 
76 fi 
77
78 GRAS_DO_CHECK_SIZEOF(signed $1,$2)
79 signed=$GRAS_CHECK_SIZEOF_RES
80 if test x$signed = x ; then
81   AC_MSG_ERROR([cannot determine a size for signed $1]) 
82 fi 
83 if test x$unspecif != x$signed ; then
84   AC_MSG_ERROR(['signed $1' and '$1' have different sizes ! ($signed != $unspecif)]) 
85 fi 
86
87 GRAS_DO_CHECK_SIZEOF(unsigned $1,$2)
88 unsigned=$GRAS_CHECK_SIZEOF_RES
89 if test x$unsigned = x ; then
90   AC_MSG_ERROR([cannot determine a size for unsigned $1]) 
91 fi 
92 if test x$unsigned != x$signed ; then
93   AC_MSG_ERROR(['signed $1' and 'unsigned $1' have different sizes ! ($signed != $unsigned)]) 
94 fi 
95
96 AC_MSG_RESULT($GRAS_CHECK_SIZEOF_RES) 
97 ])
98
99 dnl
100 dnl End of CHECK_SIGNED_SIZEOF
101 dnl
102
103 # GRAS_STRUCT_BOUNDARY(): Check the minimal alignment boundary of $1 in structures
104 # ---------------------
105 # (using integer, I hope no arch let it change with the content)
106 AC_DEFUN([GRAS_STRUCT_BOUNDARY],
107 [changequote(<<, >>)dnl 
108  dnl The cache variable name (and of the result). 
109  define(<<GRAS_STRUCT_BOUNDARY_RES>>, translit(ac_cv_struct_boundary_$1, [ *()], [_pLR]))dnl
110  changequote([, ])dnl 
111  AC_MSG_CHECKING(for the minimal structure boundary of $1)
112
113  AC_CACHE_VAL(GRAS_STRUCT_BOUNDARY_RES, 
114  [for ac_size in 1 2 4 8 16 32 64 ; do # List sizes in rough order of prevalence. 
115    AC_COMPILE_IFELSE(AC_LANG_PROGRAM([#include "confdefs.h" 
116      #include <sys/types.h> 
117      struct s { char c; $1 i; };
118      ],[switch (0) case 0: case (sizeof (struct s) == $ac_size+sizeof($1)):;]), GRAS_STRUCT_BOUNDARY_RES=$ac_size)
119    if test x$GRAS_STRUCT_BOUNDARY_RES != x ; then break; fi 
120   done 
121  ])
122  AC_MSG_RESULT($GRAS_STRUCT_BOUNDARY_RES)
123  if test x$GRAS_STRUCT_BOUNDARY_RES = x ; then
124    AC_MSG_ERROR([Cannot determine the minimal alignment boundary of $1 in structures])
125  fi
126 ])
127
128 # GRAS_ARCH(): check the gras_architecture of this plateform
129 # -----------
130 # The different cases here must be syncronized with the array in src/base/DataDesc/ddt_convert.c
131 #
132 AC_DEFUN([GRAS_ARCH],
133 [
134 # Check for the architecture
135 AC_C_BIGENDIAN(endian=1,endian=0,AC_MSG_ERROR([GRAS works only for little or big endian systems (yet)]))
136 dnl Make sure we don't run on a non-two-compliment arch, since we dunno convert them
137 GRAS_TWO_COMPLIMENT(int)
138 AC_DEFINE_UNQUOTED(GRAS_BIGENDIAN,$endian,[define if big endian])
139           
140 GRAS_SIGNED_SIZEOF(char)              GRAS_STRUCT_BOUNDARY(char)
141 GRAS_SIGNED_SIZEOF(short int)         GRAS_STRUCT_BOUNDARY(short int)
142 GRAS_SIGNED_SIZEOF(int)               GRAS_STRUCT_BOUNDARY(int)
143 GRAS_SIGNED_SIZEOF(long int)          GRAS_STRUCT_BOUNDARY(long int)
144 GRAS_SIGNED_SIZEOF(long long int)     GRAS_STRUCT_BOUNDARY(long long int)
145
146 GRAS_STRUCT_BOUNDARY(float)           GRAS_STRUCT_BOUNDARY(double)
147
148 GRAS_CHECK_SIZEOF(void *)             GRAS_STRUCT_BOUNDARY(void *)
149 GRAS_CHECK_SIZEOF(void (*) (void))    
150                                                                   
151
152 AC_MSG_CHECKING(the GRAS signature of this architecture)
153 if test x$endian = x1 ; then
154   trace_endian=B
155 else
156   trace_endian=l
157 fi
158 gras_arch=unknown
159 trace="$trace_endian"
160
161 trace="${trace}_C:${ac_cv_sizeof_char}/${ac_cv_struct_boundary_char}:"
162
163 trace="${trace}_I:${ac_cv_sizeof_short_int}/${ac_cv_struct_boundary_short_int}"
164 trace="${trace}:${ac_cv_sizeof_int}/${ac_cv_struct_boundary_int}"
165 trace="${trace}:${ac_cv_sizeof_long_int}/${ac_cv_struct_boundary_long_int}"
166 trace="${trace}:${ac_cv_sizeof_long_long_int}/${ac_cv_struct_boundary_long_long_int}:"
167
168 trace="${trace}_P:${ac_cv_sizeof_void_p}/${ac_cv_struct_boundary_void_p}"
169 trace="${trace}:${ac_cv_sizeof_void_LpR_LvoidR}/${ac_cv_struct_boundary_void_p}:"
170
171 trace="${trace}_D:4/${ac_cv_struct_boundary_float}:8/${ac_cv_struct_boundary_double}:"
172
173 # sizeof float/double are not tested since IEEE 754 is assumed.
174 # Check README.IEEE for rational.
175 case $trace in
176   l_C:1/1:_I:2/2:4/4:4/4:8/4:_P:4/4:4/4:_D:4/4:8/4:) gras_arch=0; gras_arch_name=little32;;
177   l_C:1/1:_I:2/2:4/4:8/8:8/8:_P:8/8:8/8:_D:4/4:8/8:) gras_arch=1; gras_arch_name=little64;;
178   B_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/8:) gras_arch=2; gras_arch_name=big32;;
179   B_C:1/1:_I:2/2:4/4:8/8:8/8:_P:8/8:8/8:_D:4/4:8/8:) gras_arch=3; gras_arch_name=big64;;
180   B_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/4:) gras_arch=4; gras_arch_name=aix;;
181 esac
182 if test x$gras_arch = xunknown ; then
183   AC_MSG_RESULT([damnit ($trace)])
184   AC_MSG_ERROR([Impossible to determine the GRAS architecture signature.
185 Please report this architecture trace ($trace) and what it corresponds to.])
186 fi
187 echo "$as_me:$LINENO: GRAS trace of this machine: $trace" >&AS_MESSAGE_LOG_FD
188 AC_DEFINE_UNQUOTED(GRAS_THISARCH,$gras_arch,[defines the GRAS architecture signature of this machine])
189 AC_MSG_RESULT($gras_arch ($gras_arch_name))
190 ])
191 # END OF GRAS ARCH CHECK
192
193 AC_DEFUN([GRAS_CHECK_STRUCT_COMPACTION],
194 [  AC_MSG_CHECKING(whether the struct gets packed)
195    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include "confdefs.h" 
196         #include <sys/types.h>
197         #include <stddef.h> /* offsetof() */
198         struct s {char c; double d;}; 
199        ]],[[switch (0) 
200         case 0: 
201         case (sizeof(struct s) == sizeof(double)+sizeof(char)):;
202        ]])],[gras_struct_packed=yes],[gras_struct_packed=no])
203      
204    AC_MSG_RESULT($gras_struct_packed)
205    if test x$gras_struct_packed = "xyes" ; then
206      AC_MSG_ERROR([GRAS does not support packed structures since it leads to nasty misalignments])
207    fi
208    
209    AC_MSG_CHECKING(whether the struct gets compacted)
210    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include "confdefs.h" 
211         #include <sys/types.h>
212         #include <stddef.h> /* offsetof() */
213         struct s {double d; int i; char c;}; 
214        ]],[[switch (0) 
215         case 0: 
216         case (offsetof(struct s,c) == sizeof(double)+sizeof(int)):;
217        ]])],[gras_struct_compact=yes],[gras_struct_compact=no])
218      
219    AC_MSG_RESULT($gras_struct_compact)
220    
221    if test x$gras_struct_compact != xyes ; then
222      AC_MSG_ERROR([GRAS works only on structure compacting architectures (yet)])
223    fi
224    AC_DEFINE_UNQUOTED(GRAS_STRUCT_COMPACT, 1,
225       [Defined if structures are compacted when possible.
226           
227        Consider this structure: struct s {double d; int i; char c;}; 
228           
229        If it is allowed, the char is placed just after the int. If not,
230        it has to be on the 8 bytes boundary imposed by the double.
231        
232        For now, GRAS requires the structures to be compacted.
233       ])
234    AC_MSG_CHECKING(whether arrays can straddle struct alignment boundaries)
235
236    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include "confdefs.h" 
237       #include <sys/types.h>
238         #include <stddef.h> /* offsetof() */
239         struct s { double d; int i; char c[6]; };
240      ]],[[switch (0) 
241       case 0: 
242       case (offsetof(struct s,c) == sizeof(double)+sizeof(int)):;
243      ]])],[gras_array_straddle_struct=yes],[gras_array_straddle_struct=no])
244      
245    AC_MSG_RESULT($gras_array_straddle_struct)
246
247    if test x$gras_array_straddle_struct = xyes ; then
248      AC_DEFINE_UNQUOTED(GRAS_ARRAY_STRADDLE_STRUCT, 1,
249         [Defined if arrays in struct can straddle struct alignment boundaries.
250         
251          This is like than the structure compaction above, but this time,
252          the argument to be compacted is an array whom each element would be
253          normally compacted. Exemple:
254          
255          struct s { double d; int i; char c[6]; };
256          
257          Arrays can straddle if c is allowed to come just after i.
258          
259          Note that GRAS only support architecture presenting this
260          caracteristic so far.
261         ])
262    else
263      AC_MSG_ERROR([GRAS can only work on architectures allowing array fields to straddle alignment boundaries (yet)])
264    fi
265 ])
266 # END OF GRAS CHECK STRUCT COMPACTION
267
268 AC_DEFUN([GRAS_C_COMPACT_STRUCT],
269 [  AC_MSG_CHECKING(whether the struct gets compacted)
270    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include "confdefs.h" 
271         #include <sys/types.h>
272         #include <stddef.h> /* offsetof() */
273         struct s {double d; int a; int b;}; 
274        ]],[[switch (0) 
275         case 0: 
276         case (offsetof(struct s,b) == sizeof(double)+sizeof(int)):;
277        ]])],[gras_compact_struct=yes],[gras_compact_struct=no])
278      
279    AC_MSG_RESULT($gras_compact_struct)
280    
281    if test x$gras_compact_struct = xyes ; then
282      AC_DEFINE_UNQUOTED(GRAS_COMPACT_STRUCT, 1,
283           [Defined if structures are compacted when possible])
284    fi     
285 ])
286 # END OF GRAS COMPACT STRUCT
287