1dnl $Id: acinclude.m4,v 1.10 2010/02/15 14:51:50 fredette Exp $
2
3dnl acinclude.m4 - additional tme autoconf macros:
4
5dnl Copyright (c) 2001, 2003, 2004 Matt Fredette
6dnl All rights reserved.
7dnl
8dnl Redistribution and use in source and binary forms, with or without
9dnl modification, are permitted provided that the following conditions
10dnl are met:
11dnl 1. Redistributions of source code must retain the above copyright
12dnl    notice, this list of conditions and the following disclaimer.
13dnl 2. Redistributions in binary form must reproduce the above copyright
14dnl    notice, this list of conditions and the following disclaimer in the
15dnl    documentation and/or other materials provided with the distribution.
16dnl 3. All advertising materials mentioning features or use of this software
17dnl    must display the following acknowledgement:
18dnl      This product includes software developed by Matt Fredette.
19dnl 4. The name of the author may not be used to endorse or promote products
20dnl    derived from this software without specific prior written permission.
21dnl
22dnl THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23dnl IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24dnl WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25dnl DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26dnl INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27dnl (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28dnl SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29dnl HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30dnl STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31dnl ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32dnl POSSIBILITY OF SUCH DAMAGE.
33
34dnl AC_CHECK_ALIGNOF(BITS)
35AC_DEFUN([AC_CHECK_ALIGNOF],
36[changequote(<<, >>)dnl
37dnl The name to #define.
38define(<<AC_TYPE_NAME>>, translit(alignof_int$1_t, [a-z *], [A-Z_P]))dnl
39dnl The cache variable name.
40define(<<AC_CV_NAME>>, translit(ac_cv_alignof_int$1_t, [ *], [_p]))dnl
41changequote([, ])dnl
42AC_MSG_CHECKING(minimum alignment of int$1_t)
43AC_CACHE_VAL(AC_CV_NAME,
44[AC_TRY_RUN([#include <stdio.h>
45#include <sys/types.h>
46main()
47{
48#if (SIZEOF_SHORT * 8) == $1
49#define _type short
50#elif (SIZEOF_INT * 8) == $1
51#define _type int
52#else
53#define _type long
54#endif
55  char try_align_buffer[sizeof(_type) * 2];
56  int min_align, try_align, status;
57  _type value;
58  FILE *f=fopen("conftestval", "w");
59  if (!f) exit(1);
60  min_align = sizeof(_type);
61  for(try_align = sizeof(_type); try_align-- > 1;) {
62    switch(fork()) {
63    case -1: exit(1);
64    case 0: value = *((_type *) &try_align_buffer[try_align]);
65      fprintf(stderr, "%d\n", (int) (value / 2)); exit(0);
66    default: break;
67    }
68    wait(&status);
69    if (!status && try_align < min_align) {
70      min_align = try_align;
71    }
72  }
73  fprintf(f, "%d\n", min_align * 8);
74  exit(0);
75}], AC_CV_NAME=`cat conftestval`, AC_CV_NAME=$1, AC_CV_NAME=$1)])dnl
76AC_CV_NAME=`expr $AC_CV_NAME / 8`
77AC_MSG_RESULT($AC_CV_NAME)
78AC_DEFINE_UNQUOTED(AC_TYPE_NAME, $AC_CV_NAME, [Define to the minimum alignment, in bytes, of int$1_t.])
79undefine([AC_TYPE_NAME])dnl
80undefine([AC_CV_NAME])dnl
81])
82
83dnl AC_CHECK_SHIFTMAX(BITS)
84AC_DEFUN([AC_CHECK_SHIFTMAX],
85[changequote(<<, >>)dnl
86dnl The name to #define.
87define(<<AC_TYPE_NAME>>, translit(shiftmax_int$1_t, [a-z *], [A-Z_P]))dnl
88dnl The cache variable name.
89define(<<AC_CV_NAME>>, translit(ac_cv_shiftmax_int$1_t, [ *], [_p]))dnl
90changequote([, ])dnl
91AC_MSG_CHECKING(maximum shift count for int$1_t)
92AC_CACHE_VAL(AC_CV_NAME,
93[AC_TRY_RUN([#include <stdio.h>
94#include <sys/types.h>
95main()
96{
97#if 8 == $1
98#define _type char
99#elif (SIZEOF_SHORT * 8) == $1
100#define _type short
101#elif (SIZEOF_INT * 8) == $1
102#define _type int
103#elif (SIZEOF_LONG * 8) == $1
104#define _type long
105#endif
106  _type center, right, left;
107  unsigned int shift, max_shift;
108  FILE *f=fopen("conftestval", "w");
109  if (!f) exit(1);
110  center = 3;
111  center <<= ((sizeof(center) * 4) - 1);
112  max_shift = 2047;
113  sscanf("0", "%d", &shift);
114  for (shift += (sizeof(center) * 8);
115       shift < 2048;
116       shift <<= 1) {
117    right = (center >> shift);
118    left = (center << shift);
119    if (right != 0
120	|| left != 0) {
121      right = (center >> (shift | 1));
122      left = (center << (shift | 1));
123      max_shift = ((right == (center >> 1)
124		    && left == (center << 1))
125		   ? shift - 1
126		   : (sizeof(center) * 8) - 1);
127      break;
128    }
129  }
130  fprintf(f, "%d\n", max_shift + 1);
131  exit(0);
132}], AC_CV_NAME=`cat conftestval`, AC_CV_NAME=$1, AC_CV_NAME=$1)])dnl
133AC_CV_NAME=`expr $AC_CV_NAME - 1`
134AC_MSG_RESULT($AC_CV_NAME)
135AC_DEFINE_UNQUOTED(AC_TYPE_NAME, $AC_CV_NAME, [Define to the maximum shift count for a int$1_t.])
136undefine([AC_TYPE_NAME])dnl
137undefine([AC_CV_NAME])dnl
138])
139
140dnl AC_CHECK_SHIFTSIGNED(BITS)
141AC_DEFUN([AC_CHECK_SHIFTSIGNED],
142[changequote(<<, >>)dnl
143dnl The name to #define.
144define(<<AC_TYPE_NAME>>, translit(shiftsigned_int$1_t, [a-z *], [A-Z_P]))dnl
145dnl The cache variable name.
146define(<<AC_CV_NAME>>, translit(ac_cv_shiftsigned_int$1_t, [ *], [_p]))dnl
147changequote([, ])dnl
148AC_MSG_CHECKING(for arithmetic right shifts of int$1_t)
149AC_CACHE_VAL(AC_CV_NAME,
150[AC_TRY_RUN([#include <stdio.h>
151#include <sys/types.h>
152main()
153{
154#if 8 == $1
155#define _type signed char
156#elif (SIZEOF_SHORT * 8) == $1
157#define _type signed short
158#elif (SIZEOF_INT * 8) == $1
159#define _type signed int
160#elif (SIZEOF_LONG * 8) == $1
161#define _type signed long
162#endif
163  _type prime, shifted, dividend;
164  unsigned int shift, divides;
165  FILE *f;
166
167  f=fopen("conftestval", "w");
168  if (!f) exit(1);
169
170  /* set value to the largest magnitude negative prime that it
171     will hold: */
172#if $1 == 8
173  prime = -127;
174#elif $1 == 16
175  prime = -32749;
176#elif $1 == 32
177  prime = -2147483647;
178#elif $1 == 64
179  /* this is a crafty way of constructing -9223372036854775783,
180     which is 0x8000000000000000 - 0x19, without having to know
181     any compiler suffix for 64-bit literals: */
182  prime = 1;
183  prime <<= ($1 - 2);
184  prime = -prime;
185  prime *= 2;
186  prime -= 0x19;
187#else
188#error "need another prime"
189#endif
190
191  /* all right shifts from a single bit up to SHIFTMAX_INT$1_T
192     must be arithmetic, otherwise shifts aren't arithmetic: */
193  for (shift = 1;
194       shift <= SHIFTMAX_INT$1_T;
195       shift++) {
196    shifted = prime >> shift;
197    dividend = prime;
198    divides = 0;
199    do {
200      dividend &= ~((_type) 1); /* guarantee no remainder */
201      dividend /= 2;
202    } while (++divides < shift);
203    if (shifted != dividend) {
204      break;
205    }
206  }
207
208  fprintf(f, "%d\n", (shift > SHIFTMAX_INT$1_T ? 1 : 0));
209  exit(0);
210}], AC_CV_NAME=`cat conftestval`, AC_CV_NAME=0, AC_CV_NAME=0)])dnl
211if test $AC_CV_NAME = 1; then
212  AC_MSG_RESULT(yes)
213  AC_DEFINE_UNQUOTED(AC_TYPE_NAME, [], [Define if all right shifts of int$1_t are arithmetic.])
214else
215  AC_MSG_RESULT(no)
216fi
217undefine([AC_TYPE_NAME])dnl
218undefine([AC_CV_NAME])dnl
219])
220
221dnl AC_CHECK_FLOAT_FORMAT(TYPE)
222AC_DEFUN([AC_CHECK_FLOAT_FORMAT],
223[changequote(<<, >>)dnl
224dnl The name to #define.
225define(<<AC_TYPE_NAME>>, translit(FLOAT_FORMAT_$1, [a-z *], [A-Z_P]))dnl
226dnl The cache variable name.
227define(<<AC_CV_NAME>>, translit(ac_cv_float_format_$1, [ *], [_p]))dnl
228changequote([, ])dnl
229AC_MSG_CHECKING(the floating point format of $1)
230AC_CACHE_VAL(AC_CV_NAME,
231[AC_TRY_RUN([#include <stdio.h>
232#include <sys/types.h>
233main()
234{
235  $1 value;
236  unsigned short value_buffer[16];
237#ifdef WORDS_BIGENDIAN
238#define UINT16_PART(v, i) (value_buffer[(sizeof(v) / sizeof(value_buffer[0])) - ((i) + 1)])
239#else
240#define UINT16_PART(v, i) (value_buffer[(i)])
241#endif
242
243  FILE *f=fopen("conftestval", "w");
244  if (!f) exit(1);
245
246  /* see if this type is IEEE754 single precision: */
247  value = 3.4793309169e-05;
248  memcpy(value_buffer, &value, sizeof(value));
249  if (sizeof(value) == 4
250      && UINT16_PART(value, 1) == 0x3811
251      && UINT16_PART(value, 0) == 0xef08) {
252    fprintf(f, "IEEE754_SINGLE\n");
253    exit (0);
254  }
255
256  /* see if this type is IEEE754 double precision: */
257  value = 7.91534994289814532176e-04;
258  memcpy(value_buffer, &value, sizeof(value));
259  if (sizeof(value) == 8
260      && UINT16_PART(value, 3) == 0x3f49
261      && UINT16_PART(value, 2) == 0xefe0
262      && UINT16_PART(value, 1) == 0x7501
263      && UINT16_PART(value, 0) == 0xb288) {
264    fprintf(f, "IEEE754_DOUBLE\n");
265    exit (0);
266  }
267
268  /* see if this type might be IEEE754 extended 80-bit precision: */
269  value = 1.1356523406294143949491931077970765006170e+04;
270  memcpy(value_buffer, &value, sizeof(value));
271  if (sizeof(value) >= 12
272      && UINT16_PART(value, 0) == 0x7800
273      && UINT16_PART(value, 1) == 0xd1cf
274      && UINT16_PART(value, 2) == 0x17f7
275      && UINT16_PART(value, 3) == 0xb172) {
276
277    /* check for i387-style IEEE754 extended precision: */
278    if (sizeof(value) == 12
279	&& UINT16_PART(value, 4) == 0x400c) {
280      fprintf(f, "IEEE754_EXTENDED80_I387\n");
281      exit (0);
282    }
283
284    /* check for m68881-style IEEE754 extended precision: */
285    if (sizeof(value) == 12
286	&& UINT16_PART(value, 5) == 0x400c) {
287      fprintf(f, "IEEE754_EXTENDED80_M68881\n");
288      exit (0);
289    }
290  }
291
292  /* otherwise, this is some native type: */
293  fprintf(f, "NATIVE\n");
294  exit (0);
295}], AC_CV_NAME=`cat conftestval`, AC_CV_NAME=NATIVE, AC_CV_NAME=NATIVE)])dnl
296AC_MSG_RESULT($AC_CV_NAME)
297if test $AC_CV_NAME != NATIVE; then
298  AC_DEFINE_UNQUOTED(AC_TYPE_NAME, $AC_CV_NAME, [Define to the floating point format of a $1.])
299fi
300undefine([AC_TYPE_NAME])dnl
301undefine([AC_CV_NAME])dnl
302])
303
304dnl AC_CHECK_FLOAT_LIMITS(TYPE, LIMITS)
305AC_DEFUN([AC_CHECK_FLOAT_LIMITS],
306[changequote(<<, >>)dnl
307dnl The names to #define.
308define(<<AC_TYPE_NAME_MAX>>, translit(FLOAT_MAX_$1, [a-z *], [A-Z_P]))dnl
309define(<<AC_TYPE_NAME_MIN>>, translit(FLOAT_MIN_$1, [a-z *], [A-Z_P]))dnl
310dnl The cache variable name.
311define(<<AC_CV_NAME>>, translit(ac_cv_float_limits_$1, [ *], [_p]))dnl
312changequote([, ])dnl
313AC_MSG_CHECKING(the limits of $1)
314AC_CACHE_VAL(AC_CV_NAME,
315[for limits in $2; do
316  max=`echo $limits | sed -e 's%^\(.*\)/\(.*\)$%\1%'`
317  min=`echo $limits | sed -e 's%^\(.*\)/\(.*\)$%\2%'`
318  AC_TRY_COMPILE([#include <sys/types.h>
319#ifdef HAVE_FLOAT_H
320#include <float.h>
321#endif
322#ifdef HAVE_LIMITS_H
323#include <limits.h>
324#endif], [ $1 x; x = $max - $min; ], [AC_CV_NAME=$limits ; break], AC_CV_NAME= )
325done])
326if test "x$AC_CV_NAME" = x; then
327  AC_MSG_ERROR(can't determine the limits of $1)
328fi
329AC_MSG_RESULT($AC_CV_NAME)
330max=`echo $AC_CV_NAME | sed -e 's%^\(.*\)/\(.*\)$%\1%'`
331min=`echo $AC_CV_NAME | sed -e 's%^\(.*\)/\(.*\)$%\2%'`
332AC_DEFINE_UNQUOTED(AC_TYPE_NAME_MAX, ($max), [Define to the maximum value of a $1.])
333AC_DEFINE_UNQUOTED(AC_TYPE_NAME_MIN, ($min), [Define to the minimum value of a $1.])
334undefine([AC_TYPE_NAME_MAX])dnl
335undefine([AC_TYPE_NAME_MIN])dnl
336undefine([AC_CV_NAME])dnl
337])
338
339dnl AC_CHECK_FUNC_LONG(FUNC, FUNCTION-BODY, [INCLUDES, [LIBRARIES]])
340AC_DEFUN([AC_CHECK_FUNC_LONG],
341[changequote(<<, >>)dnl
342dnl The name to #define.
343define(<<AC_FUNC_NAME>>, translit(have_$1, [a-z *], [A-Z_P]))dnl
344dnl The cache variable name.
345define(<<AC_CV_NAME>>, translit(ac_cv_have_$1, [ *], [_p]))dnl
346changequote([, ])dnl
347AC_MSG_CHECKING(for $1)
348AC_CACHE_VAL(AC_CV_NAME,
349[ac_func_long_LIBS=$LIBS
350LIBS="${LIBS-} $4"
351AC_TRY_LINK([
352$3
353], [
354$2
355], AC_CV_NAME=yes, AC_CV_NAME=no)
356LIBS=$ac_func_long_LIBS])dnl
357AC_MSG_RESULT($AC_CV_NAME)
358if test $AC_CV_NAME = yes; then
359  AC_DEFINE_UNQUOTED(AC_FUNC_NAME, [], [Define if you have $1().])
360fi
361undefine([AC_FUNC_NAME])dnl
362undefine([AC_CV_NAME])dnl
363])
364
365dnl The AC_HEADER_CHECK_PROTOTYPE, AC_HEADER_CHECK_PROTOTYPES,
366dnl and AC_SYS_SOCKADDR_SA_LEN macros bear the following copyright:
367dnl
368dnl Copyright (C) 1997,1998,1999 by the Massachusetts Institute of Technology,
369dnl Cambridge, MA, USA.  All Rights Reserved.
370dnl
371dnl This software is being provided to you, the LICENSEE, by the
372dnl Massachusetts Institute of Technology (M.I.T.) under the following
373dnl license.  By obtaining, using and/or copying this software, you agree
374dnl that you have read, understood, and will comply with these terms and
375dnl conditions:
376dnl
377dnl WITHIN THOSE CONSTRAINTS, permission to use, copy, modify and distribute
378dnl this software and its documentation for any purpose and without fee or
379dnl royalty is hereby granted, provided that you agree to comply with the
380dnl following copyright notice and statements, including the disclaimer, and
381dnl that the same appear on ALL copies of the software and documentation,
382dnl including modifications that you make for internal use or for
383dnl distribution:
384dnl
385dnl THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS
386dnl OR WARRANTIES, EXPRESS OR IMPLIED.  By way of example, but not
387dnl limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF
388dnl MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF
389dnl THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY
390dnl PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
391dnl
392dnl The name of the Massachusetts Institute of Technology or M.I.T. may NOT
393dnl be used in advertising or publicity pertaining to distribution of the
394dnl software.  Title to copyright in this software and any associated
395dnl documentation shall at all times remain with M.I.T., and USER agrees to
396dnl preserve same.
397
398dnl AC_HEADER_CHECK_PROTOTYPE(FUNCTION, INCLUDES, [ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]])
399AC_DEFUN([AC_HEADER_CHECK_PROTOTYPE],
400[AC_MSG_CHECKING([for a prototype for $1])
401AC_CACHE_VAL(ac_cv_proto_$1,
402[AC_TRY_COMPILE($2 [
403struct bonch { int a, b; };
404struct bonch $1();
405], , eval "ac_cv_proto_$1=no", eval "ac_cv_proto_$1=yes")])
406if eval "test \"`echo '$ac_cv_proto_'$1`\" = yes"; then
407  AC_MSG_RESULT(yes)
408  ifelse([$3], , :, [$3])
409else
410  AC_MSG_RESULT(no)
411ifelse([$4], , , [$4
412])dnl
413fi
414])
415
416dnl AC_HEADER_CHECK_PROTOTYPES(INCLUDES, FUNCTION...)
417AC_DEFUN([AC_HEADER_CHECK_PROTOTYPES],
418[for ac_func in $2
419do
420AC_HEADER_CHECK_PROTOTYPE($ac_func, $1,
421[changequote(, )dnl
422  ac_tr_func=PROTO_`echo $ac_func | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'`
423changequote([, ])dnl
424  AC_DEFINE_UNQUOTED($ac_tr_func)])dnl
425done
426])
427
428dnl AC_SYS_SOCKADDR_SA_LEN
429AC_DEFUN([AC_SYS_SOCKADDR_SA_LEN],
430[AC_MSG_CHECKING([for sa_len in struct sockaddr])
431AC_CACHE_VAL(ac_cv_sys_sockaddr_sa_len,
432[AC_TRY_COMPILE([
433#include <sys/types.h>
434#include <sys/socket.h>
435], [
436int length;
437struct sockaddr sock;
438length = sock.sa_len;
439], ac_cv_sys_sockaddr_sa_len=yes, ac_cv_sys_sockaddr_sa_len=no)])dnl
440if test $ac_cv_sys_sockaddr_sa_len = yes; then
441  AC_MSG_RESULT(yes)
442  AC_DEFINE_UNQUOTED(HAVE_SOCKADDR_SA_LEN, [], [Define if your struct sockaddr has sa_len.])
443else
444  AC_MSG_RESULT(no)
445fi
446])dnl
447