xref: /netbsd-src/external/lgpl3/gmp/dist/acinclude.m4 (revision 41f3ac3e09f0c1c4d8b911b4c8a1d6450bd14f46)
1dnl  GMP specific autoconf macros
2
3
4dnl  Copyright 2000-2006, 2009, 2011, 2013-2018 Free Software Foundation, Inc.
5dnl
6dnl  This file is part of the GNU MP Library.
7dnl
8dnl  The GNU MP Library is free software; you can redistribute it and/or modify
9dnl  it under the terms of either:
10dnl
11dnl    * the GNU Lesser General Public License as published by the Free
12dnl      Software Foundation; either version 3 of the License, or (at your
13dnl      option) any later version.
14dnl
15dnl  or
16dnl
17dnl    * the GNU General Public License as published by the Free Software
18dnl      Foundation; either version 2 of the License, or (at your option) any
19dnl      later version.
20dnl
21dnl  or both in parallel, as here.
22dnl
23dnl  The GNU MP Library is distributed in the hope that it will be useful, but
24dnl  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25dnl  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26dnl  for more details.
27dnl
28dnl  You should have received copies of the GNU General Public License and the
29dnl  GNU Lesser General Public License along with the GNU MP Library.  If not,
30dnl  see https://www.gnu.org/licenses/.
31
32
33dnl  Some tests use, or must delete, the default compiler output.  The
34dnl  possible filenames are based on what autoconf looks for, namely
35dnl
36dnl    a.out - normal unix style
37dnl    b.out - i960 systems, including gcc there
38dnl    a.exe - djgpp
39dnl    a_out.exe - OpenVMS DEC C called via GNV wrapper (gnv.sourceforge.net)
40dnl    conftest.exe - various DOS compilers
41
42
43define(IA64_PATTERN,
44[[ia64*-*-* | itanium-*-* | itanium2-*-*]])
45
46define(M5407_PATTERN,
47[[m5407-*-*]])
48
49dnl  Need to be careful not to match m6811, m6812, m68hc11 and m68hc12, all
50dnl  of which config.sub accepts.  (Though none of which are likely to work
51dnl  with GMP.)
52dnl
53define(M68K_PATTERN,
54[[m68k-*-* | m68[0-9][0-9][0-9]-*-*]])
55
56define(POWERPC64_PATTERN,
57[[powerpc64-*-* | powerpc64le-*-* | powerpc620-*-* | powerpc630-*-* | powerpc970-*-* | power[3-9]-*-*]])
58
59define(S390_PATTERN,
60[[s390-*-* | z900esa-*-* | z990esa-*-* | z9esa-*-* | z10esa-*-* | z196esa-*-*]])
61
62define(S390X_PATTERN,
63[[s390x-*-* | z900-*-* | z990-*-* | z9-*-* | z10-*-* | z196-*-*]])
64
65define(X86_PATTERN,
66[[i?86*-*-* | k[5-8]*-*-* | pentium*-*-* | athlon-*-* | viac3*-*-* | geode*-*-* | atom-*-*]])
67
68define(X86_64_PATTERN,
69[[athlon64-*-* | k8-*-* | k10-*-* | bobcat-*-* | jaguar*-*-* | bulldozer*-*-* | piledriver*-*-* | steamroller*-*-* | excavator*-*-* | zen*-*-* | pentium4-*-* | atom-*-* | silvermont-*-* | goldmont-*-* | core2-*-* | corei*-*-* | x86_64-*-* | nano-*-* | nehalem*-*-* | westmere*-*-* | sandybridge*-*-* | ivybridge*-*-* | haswell*-*-* | broadwell*-*-* | skylake*-*-* | kabylake*-*-*]])
70
71dnl  GMP_FAT_SUFFIX(DSTVAR, DIRECTORY)
72dnl  ---------------------------------
73dnl  Emit code to set shell variable DSTVAR to the suffix for a fat binary
74dnl  routine from DIRECTORY.  DIRECTORY can be a shell expression like $foo
75dnl  etc.
76dnl
77dnl  The suffix is directory separators / or \ changed to underscores, and
78dnl  if there's more than one directory part, then the first is dropped.
79dnl
80dnl  For instance,
81dnl
82dnl      x86         ->  x86
83dnl      x86/k6      ->  k6
84dnl      x86/k6/mmx  ->  k6_mmx
85
86define(GMP_FAT_SUFFIX,
87[[$1=`echo $2 | sed -e '/\//s:^[^/]*/::' -e 's:[\\/]:_:g'`]])
88
89
90dnl  GMP_REMOVE_FROM_LIST(listvar,item)
91dnl  ----------------------------------
92dnl  Emit code to remove any occurrence of ITEM from $LISTVAR.  ITEM can be a
93dnl  shell expression like $foo if desired.
94
95define(GMP_REMOVE_FROM_LIST,
96[remove_from_list_tmp=
97for remove_from_list_i in $[][$1]; do
98  if test $remove_from_list_i = [$2]; then :;
99  else
100     remove_from_list_tmp="$remove_from_list_tmp $remove_from_list_i"
101  fi
102done
103[$1]=$remove_from_list_tmp
104])
105
106
107dnl  GMP_STRIP_PATH(subdir)
108dnl  ----------------------
109dnl  Strip entries */subdir from $path and $fat_path.
110
111define(GMP_STRIP_PATH,
112[GMP_STRIP_PATH_VAR(path, [$1])
113GMP_STRIP_PATH_VAR(fat_path, [$1])
114])
115
116define(GMP_STRIP_PATH_VAR,
117[tmp_path=
118for i in $[][$1]; do
119  case $i in
120    */[$2]) ;;
121    *) tmp_path="$tmp_path $i" ;;
122  esac
123done
124[$1]="$tmp_path"
125])
126
127
128dnl  GMP_INCLUDE_GMP_H
129dnl  -----------------
130dnl  Expand to the right way to #include gmp-h.in.  This must be used
131dnl  instead of gmp.h, since that file isn't generated until the end of the
132dnl  configure.
133dnl
134dnl  Dummy value for GMP_LIMB_BITS is enough
135dnl  for all current configure-time uses of gmp.h.
136
137define(GMP_INCLUDE_GMP_H,
138[[#define __GMP_WITHIN_CONFIGURE 1   /* ignore template stuff */
139#define GMP_NAIL_BITS $GMP_NAIL_BITS
140#define GMP_LIMB_BITS 123
141$DEFN_LONG_LONG_LIMB
142#include "$srcdir/gmp-h.in"]
143])
144
145
146dnl  GMP_HEADER_GETVAL(NAME,FILE)
147dnl  ----------------------------
148dnl  Expand at autoconf time to the value of a "#define NAME" from the given
149dnl  FILE.  The regexps here aren't very rugged, but are enough for gmp.
150dnl  /dev/null as a parameter prevents a hang if $2 is accidentally omitted.
151
152define(GMP_HEADER_GETVAL,
153[patsubst(patsubst(
154esyscmd([grep "^#define $1 " $2 /dev/null 2>/dev/null]),
155[^.*$1[ 	]+],[]),
156[[
157 	]*$],[])])
158
159
160dnl  GMP_VERSION
161dnl  -----------
162dnl  The gmp version number, extracted from the #defines in gmp-h.in at
163dnl  autoconf time.  Two digits like 3.0 if patchlevel <= 0, or three digits
164dnl  like 3.0.1 if patchlevel > 0.
165
166define(GMP_VERSION,
167[GMP_HEADER_GETVAL(__GNU_MP_VERSION,gmp-h.in)[]dnl
168.GMP_HEADER_GETVAL(__GNU_MP_VERSION_MINOR,gmp-h.in)[]dnl
169.GMP_HEADER_GETVAL(__GNU_MP_VERSION_PATCHLEVEL,gmp-h.in)])
170
171
172dnl  GMP_SUBST_CHECK_FUNCS(func,...)
173dnl  ------------------------------
174dnl  Setup an AC_SUBST of HAVE_FUNC_01 for each argument.
175
176AC_DEFUN([GMP_SUBST_CHECK_FUNCS],
177[m4_if([$1],,,
178[_GMP_SUBST_CHECK_FUNCS(ac_cv_func_[$1],HAVE_[]m4_translit([$1],[a-z],[A-Z])_01)
179GMP_SUBST_CHECK_FUNCS(m4_shift($@))])])
180
181dnl  Called: _GMP_SUBST_CHECK_FUNCS(cachevar,substvar)
182AC_DEFUN([_GMP_SUBST_CHECK_FUNCS],
183[case $[$1] in
184yes) AC_SUBST([$2],1) ;;
185no)  [$2]=0 ;;
186esac
187])
188
189
190dnl  GMP_SUBST_CHECK_HEADERS(foo.h,...)
191dnl  ----------------------------------
192dnl  Setup an AC_SUBST of HAVE_FOO_H_01 for each argument.
193
194AC_DEFUN([GMP_SUBST_CHECK_HEADERS],
195[m4_if([$1],,,
196[_GMP_SUBST_CHECK_HEADERS(ac_cv_header_[]m4_translit([$1],[./],[__]),
197HAVE_[]m4_translit([$1],[a-z./],[A-Z__])_01)
198GMP_SUBST_CHECK_HEADERS(m4_shift($@))])])
199
200dnl  Called: _GMP_SUBST_CHECK_HEADERS(cachevar,substvar)
201AC_DEFUN([_GMP_SUBST_CHECK_HEADERS],
202[case $[$1] in
203yes) AC_SUBST([$2],1) ;;
204no)  [$2]=0 ;;
205esac
206])
207
208
209dnl  GMP_COMPARE_GE(A1,B1, A2,B2, ...)
210dnl  ---------------------------------
211dnl  Compare two version numbers A1.A2.etc and B1.B2.etc.  Set
212dnl  $gmp_compare_ge to yes or no according to the result.  The A parts
213dnl  should be variables, the B parts fixed numbers.  As many parts as
214dnl  desired can be included.  An empty string in an A part is taken to be
215dnl  zero, the B parts should be non-empty and non-zero.
216dnl
217dnl  For example,
218dnl
219dnl      GMP_COMPARE($major,10, $minor,3, $subminor,1)
220dnl
221dnl  would test whether $major.$minor.$subminor is greater than or equal to
222dnl  10.3.1.
223
224AC_DEFUN([GMP_COMPARE_GE],
225[gmp_compare_ge=no
226GMP_COMPARE_GE_INTERNAL($@)
227])
228
229AC_DEFUN([GMP_COMPARE_GE_INTERNAL],
230[ifelse(len([$3]),0,
231[if test -n "$1" && test "$1" -ge $2; then
232  gmp_compare_ge=yes
233fi],
234[if test -n "$1"; then
235  if test "$1" -gt $2; then
236    gmp_compare_ge=yes
237  else
238    if test "$1" -eq $2; then
239      GMP_COMPARE_GE_INTERNAL(m4_shift(m4_shift($@)))
240    fi
241  fi
242fi])
243])
244
245
246dnl  GMP_PROG_AR
247dnl  -----------
248dnl  GMP additions to $AR.
249dnl
250dnl  A cross-"ar" may be necessary when cross-compiling since the build
251dnl  system "ar" might try to interpret the object files to build a symbol
252dnl  table index, hence the use of AC_CHECK_TOOL.
253dnl
254dnl  A user-selected $AR is always left unchanged.  AC_CHECK_TOOL is still
255dnl  run to get the "checking" message printed though.
256dnl
257dnl  If extra flags are added to AR, then ac_cv_prog_AR and
258dnl  ac_cv_prog_ac_ct_AR are set too, since libtool (cvs 2003-03-31 at
259dnl  least) will do an AC_CHECK_TOOL and that will AR from one of those two
260dnl  cached variables.  (ac_cv_prog_AR is used if there's an ac_tool_prefix,
261dnl  or ac_cv_prog_ac_ct_AR is used otherwise.)  FIXME: This is highly
262dnl  dependent on autoconf internals, perhaps it'd work to put our extra
263dnl  flags into AR_FLAGS instead.
264dnl
265dnl  $AR_FLAGS is set to "cq" rather than leaving it to libtool "cru".  The
266dnl  latter fails when libtool goes into piecewise mode and is unlucky
267dnl  enough to have two same-named objects in separate pieces, as happens
268dnl  for instance to random.o (and others) on vax-dec-ultrix4.5.  Naturally
269dnl  a user-selected $AR_FLAGS is left unchanged.
270dnl
271dnl  For reference, $ARFLAGS is used by automake (1.8) for its ".a" archive
272dnl  file rules.  This doesn't get used by the piecewise linking, so we
273dnl  leave it at the default "cru".
274dnl
275dnl  FIXME: Libtool 1.5.2 has its own arrangements for "cq", but that version
276dnl  is broken in other ways.  When we can upgrade, remove the forcible
277dnl  AR_FLAGS=cq.
278
279AC_DEFUN([GMP_PROG_AR],
280[dnl  Want to establish $AR before libtool initialization.
281AC_BEFORE([$0],[AC_PROG_LIBTOOL])
282gmp_user_AR=$AR
283AC_CHECK_TOOL(AR, ar, ar)
284if test -z "$gmp_user_AR"; then
285                        eval arflags=\"\$ar${abi1}_flags\"
286  test -n "$arflags" || eval arflags=\"\$ar${abi2}_flags\"
287  if test -n "$arflags"; then
288    AC_MSG_CHECKING([for extra ar flags])
289    AR="$AR $arflags"
290    ac_cv_prog_AR="$AR $arflags"
291    ac_cv_prog_ac_ct_AR="$AR $arflags"
292    AC_MSG_RESULT([$arflags])
293  fi
294fi
295if test -z "$AR_FLAGS"; then
296  AR_FLAGS=cq
297fi
298])
299
300
301dnl  GMP_PROG_M4
302dnl  -----------
303dnl  Find a working m4, either in $PATH or likely locations, and setup $M4
304dnl  and an AC_SUBST accordingly.  If $M4 is already set then it's a user
305dnl  choice and is accepted with no checks.  GMP_PROG_M4 is like
306dnl  AC_PATH_PROG or AC_CHECK_PROG, but tests each m4 found to see if it's
307dnl  good enough.
308dnl
309dnl  See mpn/asm-defs.m4 for details on the known bad m4s.
310
311AC_DEFUN([GMP_PROG_M4],
312[AC_ARG_VAR(M4,[m4 macro processor])
313AC_CACHE_CHECK([for suitable m4],
314                gmp_cv_prog_m4,
315[if test -n "$M4"; then
316  gmp_cv_prog_m4="$M4"
317else
318  cat >conftest.m4 <<\EOF
319dnl  Must protect this against being expanded during autoconf m4!
320dnl  Dont put "dnl"s in this as autoconf will flag an error for unexpanded
321dnl  macros.
322[define(dollarhash,``$][#'')ifelse(dollarhash(x),1,`define(t1,Y)',
323``bad: $][# not supported (SunOS /usr/bin/m4)
324'')ifelse(eval(89),89,`define(t2,Y)',
325`bad: eval() doesnt support 8 or 9 in a constant (OpenBSD 2.6 m4)
326')ifelse(eval(9,9),10,`define(t3,Y)',
327`bad: eval() doesnt support radix in eval (FreeBSD 8.x,9.0,9.1,9.2 m4)
328')ifelse(t1`'t2`'t3,YYY,`good
329')]
330EOF
331dnl ' <- balance the quotes for emacs sh-mode
332  echo "trying m4" >&AC_FD_CC
333  gmp_tmp_val=`(m4 conftest.m4) 2>&AC_FD_CC`
334  echo "$gmp_tmp_val" >&AC_FD_CC
335  if test "$gmp_tmp_val" = good; then
336    gmp_cv_prog_m4="m4"
337  else
338    IFS="${IFS= 	}"; ac_save_ifs="$IFS"; IFS=":"
339dnl $ac_dummy forces splitting on constant user-supplied paths.
340dnl POSIX.2 word splitting is done only on the output of word expansions,
341dnl not every word.  This closes a longstanding sh security hole.
342    ac_dummy="$PATH:/usr/5bin"
343    for ac_dir in $ac_dummy; do
344      test -z "$ac_dir" && ac_dir=.
345      echo "trying $ac_dir/m4" >&AC_FD_CC
346      gmp_tmp_val=`($ac_dir/m4 conftest.m4) 2>&AC_FD_CC`
347      echo "$gmp_tmp_val" >&AC_FD_CC
348      if test "$gmp_tmp_val" = good; then
349        gmp_cv_prog_m4="$ac_dir/m4"
350        break
351      fi
352    done
353    IFS="$ac_save_ifs"
354    if test -z "$gmp_cv_prog_m4"; then
355      AC_MSG_ERROR([No usable m4 in \$PATH or /usr/5bin (see config.log for reasons).])
356    fi
357  fi
358  rm -f conftest.m4
359fi])
360M4="$gmp_cv_prog_m4"
361AC_SUBST(M4)
362])
363
364
365dnl  GMP_M4_M4WRAP_SPURIOUS
366dnl  ----------------------
367dnl  Check for spurious output from m4wrap(), as described in mpn/asm-defs.m4.
368dnl
369dnl  The following systems have been seen with the problem.
370dnl
371dnl  - Unicos alpha, but its assembler doesn't seem to mind.
372dnl  - MacOS X Darwin, its assembler fails.
373dnl  - NetBSD 1.4.1 m68k, and gas 1.92.3 there gives a warning and ignores
374dnl    the bad last line since it doesn't have a newline.
375dnl  - NetBSD 1.4.2 alpha, but its assembler doesn't seem to mind.
376dnl  - HP-UX ia64.
377dnl
378dnl  Enhancement: Maybe this could be in GMP_PROG_M4, and attempt to prefer
379dnl  an m4 with a working m4wrap, if it can be found.
380
381AC_DEFUN([GMP_M4_M4WRAP_SPURIOUS],
382[AC_REQUIRE([GMP_PROG_M4])
383AC_CACHE_CHECK([if m4wrap produces spurious output],
384               gmp_cv_m4_m4wrap_spurious,
385[# hide the d-n-l from autoconf's error checking
386tmp_d_n_l=d""nl
387cat >conftest.m4 <<EOF
388[changequote({,})define(x,)m4wrap({x})$tmp_d_n_l]
389EOF
390echo test input is >&AC_FD_CC
391cat conftest.m4 >&AC_FD_CC
392tmp_chars=`$M4 conftest.m4 | wc -c`
393echo produces $tmp_chars chars output >&AC_FD_CC
394rm -f conftest.m4
395if test $tmp_chars = 0; then
396  gmp_cv_m4_m4wrap_spurious=no
397else
398  gmp_cv_m4_m4wrap_spurious=yes
399fi
400])
401GMP_DEFINE_RAW(["define(<M4WRAP_SPURIOUS>,<$gmp_cv_m4_m4wrap_spurious>)"])
402])
403
404
405dnl  GMP_PROG_NM
406dnl  -----------
407dnl  GMP additions to libtool AC_PROG_NM.
408dnl
409dnl  Note that if AC_PROG_NM can't find a working nm it still leaves
410dnl  $NM set to "nm", so $NM can't be assumed to actually work.
411dnl
412dnl  A user-selected $NM is always left unchanged.  AC_PROG_NM is still run
413dnl  to get the "checking" message printed though.
414dnl
415dnl  Perhaps it'd be worthwhile checking that nm works, by running it on an
416dnl  actual object file.  For instance on sparcv9 solaris old versions of
417dnl  GNU nm don't recognise 64-bit objects.  Checking would give a better
418dnl  error message than just a failure in later tests like GMP_ASM_W32 etc.
419dnl
420dnl  On the other hand it's not really normal autoconf practice to take too
421dnl  much trouble over detecting a broken set of tools.  And libtool doesn't
422dnl  do anything at all for say ranlib or strip.  So for now we're inclined
423dnl  to just demand that the user provides a coherent environment.
424
425AC_DEFUN([GMP_PROG_NM],
426[dnl  Make sure we're the first to call AC_PROG_NM, so our extra flags are
427dnl   used by everyone.
428AC_BEFORE([$0],[AC_PROG_NM])
429gmp_user_NM=$NM
430AC_PROG_NM
431
432# FIXME: When cross compiling (ie. $ac_tool_prefix not empty), libtool
433# defaults to plain "nm" if a "${ac_tool_prefix}nm" is not found.  In this
434# case run it again to try the native "nm", firstly so that likely locations
435# are searched, secondly so that -B or -p are added if necessary for BSD
436# format.  This is necessary for instance on OSF with "./configure
437# --build=alphaev5-dec-osf --host=alphaev6-dec-osf".
438#
439if test -z "$gmp_user_NM" && test -n "$ac_tool_prefix" && test "$NM" = nm; then
440  $as_unset lt_cv_path_NM
441  gmp_save_ac_tool_prefix=$ac_tool_prefix
442  ac_tool_prefix=
443  NM=
444  AC_PROG_NM
445  ac_tool_prefix=$gmp_save_ac_tool_prefix
446fi
447
448if test -z "$gmp_user_NM"; then
449                        eval nmflags=\"\$nm${abi1}_flags\"
450  test -n "$nmflags" || eval nmflags=\"\$nm${abi2}_flags\"
451  if test -n "$nmflags"; then
452    AC_MSG_CHECKING([for extra nm flags])
453    NM="$NM $nmflags"
454    AC_MSG_RESULT([$nmflags])
455  fi
456fi
457])
458
459
460dnl  GMP_PROG_CC_WORKS(cc+cflags,[ACTION-IF-WORKS][,ACTION-IF-NOT-WORKS])
461dnl  --------------------------------------------------------------------
462dnl  Check if cc+cflags can compile and link.
463dnl
464dnl  This test is designed to be run repeatedly with different cc+cflags
465dnl  selections, so the result is not cached.
466dnl
467dnl  For a native build, meaning $cross_compiling == no, we require that the
468dnl  generated program will run.  This is the same as AC_PROG_CC does in
469dnl  _AC_COMPILER_EXEEXT_WORKS, and checking here will ensure we don't pass
470dnl  a CC/CFLAGS combination that it rejects.
471dnl
472dnl  sparc-*-solaris2.7 can compile ABI=64 but won't run it if the kernel
473dnl  was booted in 32-bit mode.  The effect of requiring the compiler output
474dnl  will run is that a plain native "./configure" falls back on ABI=32, but
475dnl  ABI=64 is still available as a cross-compile.
476dnl
477dnl  The various specific problems we try to detect are done in separate
478dnl  compiles.  Although this is probably a bit slower than one test
479dnl  program, it makes it easy to indicate the problem in AC_MSG_RESULT,
480dnl  hence giving the user a clue about why we rejected the compiler.
481
482AC_DEFUN([GMP_PROG_CC_WORKS],
483[AC_MSG_CHECKING([compiler $1])
484gmp_prog_cc_works=yes
485
486# first see a simple "main()" works, then go on to other checks
487GMP_PROG_CC_WORKS_PART([$1], [])
488
489GMP_PROG_CC_WORKS_PART([$1], [function pointer return],
490[/* The following provokes an internal error from gcc 2.95.2 -mpowerpc64
491   (without -maix64), hence detecting an unusable compiler */
492void *g() { return (void *) 0; }
493void *f() { return g(); }
494])
495
496GMP_PROG_CC_WORKS_PART([$1], [cmov instruction],
497[/* The following provokes an invalid instruction syntax from i386 gcc
498   -march=pentiumpro on Solaris 2.8.  The native sun assembler
499   requires a non-standard syntax for cmov which gcc (as of 2.95.2 at
500   least) doesn't know.  */
501int n;
502int cmov () { return (n >= 0 ? n : 0); }
503])
504
505GMP_PROG_CC_WORKS_PART([$1], [double -> ulong conversion],
506[/* The following provokes a linker invocation problem with gcc 3.0.3
507   on AIX 4.3 under "-maix64 -mpowerpc64 -mcpu=630".  The -mcpu=630
508   option causes gcc to incorrectly select the 32-bit libgcc.a, not
509   the 64-bit one, and consequently it misses out on the __fixunsdfdi
510   helper (double -> uint64 conversion).  */
511double d;
512unsigned long gcc303 () { return (unsigned long) d; }
513])
514
515GMP_PROG_CC_WORKS_PART([$1], [double negation],
516[/* The following provokes an error from hppa gcc 2.95 under -mpa-risc-2-0 if
517   the assembler doesn't know hppa 2.0 instructions.  fneg is a 2.0
518   instruction, and a negation like this comes out using it.  */
519double fneg_data;
520unsigned long fneg () { return -fneg_data; }
521])
522
523GMP_PROG_CC_WORKS_PART([$1], [double -> float conversion],
524[/* The following makes gcc 3.3 -march=pentium4 generate an SSE2 xmm insn
525   (cvtsd2ss) which will provoke an error if the assembler doesn't recognise
526   those instructions.  Not sure how much of the gmp code will come out
527   wanting sse2, but it's easiest to reject an option we know is bad.  */
528double ftod_data;
529float ftod () { return (float) ftod_data; }
530])
531
532GMP_PROG_CC_WORKS_PART([$1], [gnupro alpha ev6 char spilling],
533[/* The following provokes an internal compiler error from gcc version
534   "2.9-gnupro-99r1" under "-O2 -mcpu=ev6", apparently relating to char
535   values being spilled into floating point registers.  The problem doesn't
536   show up all the time, but has occurred enough in GMP for us to reject
537   this compiler+flags.  */
538#include <string.h>  /* for memcpy */
539struct try_t
540{
541 char dst[2];
542 char size;
543 long d0, d1, d2, d3, d4, d5, d6;
544 char overlap;
545};
546struct try_t param[6];
547int
548param_init ()
549{
550 struct try_t *p;
551 memcpy (p, &param[ 2 ], sizeof (*p));
552 memcpy (p, &param[ 2 ], sizeof (*p));
553 p->size = 2;
554 memcpy (p, &param[ 1 ], sizeof (*p));
555 p->dst[0] = 1;
556 p->overlap = 2;
557 memcpy (p, &param[ 3 ], sizeof (*p));
558 p->dst[0] = 1;
559 p->overlap = 8;
560 memcpy (p, &param[ 4 ], sizeof (*p));
561 memcpy (p, &param[ 4 ], sizeof (*p));
562 p->overlap = 8;
563 memcpy (p, &param[ 5 ], sizeof (*p));
564 memcpy (p, &param[ 5 ], sizeof (*p));
565 memcpy (p, &param[ 5 ], sizeof (*p));
566 return 0;
567}
568])
569
570# __builtin_alloca is not available everywhere, check it exists before
571# seeing that it works
572GMP_PROG_CC_WORKS_PART_TEST([$1],[__builtin_alloca availability],
573[int k; int foo () { __builtin_alloca (k); }],
574  [GMP_PROG_CC_WORKS_PART([$1], [alloca array],
575[/* The following provokes an internal compiler error from Itanium HP-UX cc
576    under +O2 or higher.  We use this sort of code in mpn/generic/mul_fft.c. */
577int k;
578int foo ()
579{
580  int i, **a;
581  a = __builtin_alloca (k);
582  for (i = 0; i <= k; i++)
583    a[i] = __builtin_alloca (1 << i);
584}
585])])
586
587GMP_PROG_CC_WORKS_PART([$1], [abs int -> double conversion],
588[/* The following provokes an internal error from the assembler on
589   power2-ibm-aix4.3.1.0.  gcc -mrios2 compiles to nabs+fcirz, and this
590   results in "Internal error related to the source program domain".
591
592   For reference it seems to be the combination of nabs+fcirz which is bad,
593   not either alone.  This sort of thing occurs in mpz/get_str.c with the
594   way double chars_per_bit_exactly is applied in MPN_SIZEINBASE.  Perhaps
595   if that code changes to a scaled-integer style then we won't need this
596   test.  */
597
598double fp[1];
599int x;
600int f ()
601{
602  int a;
603  a = (x >= 0 ? x : -x);
604  return a * fp[0];
605}
606])
607
608GMP_PROG_CC_WORKS_PART([$1], [long long reliability test 1],
609[/* The following provokes a segfault in the compiler on powerpc-apple-darwin.
610   Extracted from tests/mpn/t-iord_u.c.  Causes Apple's gcc 3.3 build 1640 and
611   1666 to segfault with e.g., -O2 -mpowerpc64.  */
612
613#if defined (__GNUC__) && ! defined (__cplusplus)
614typedef unsigned long long t1;typedef t1*t2;
615void g(){}
616void h(){}
617static __inline__ t1 e(t2 rp,t2 up,int n,t1 v0)
618{t1 c,x,r;int i;if(v0){c=1;for(i=1;i<n;i++){x=up[i];r=x+1;rp[i]=r;}}return c;}
619void f(){static const struct{t1 n;t1 src[9];t1 want[9];}d[]={{1,{0},{1}},};t1 got[9];int i;
620for(i=0;i<1;i++){if(e(got,got,9,d[i].n)==0)h();g(i,d[i].src,d[i].n,got,d[i].want,9);if(d[i].n)h();}}
621#else
622int dummy;
623#endif
624])
625
626GMP_PROG_CC_WORKS_PART([$1], [long long reliability test 2],
627[/* The following provokes an internal compiler error on powerpc-apple-darwin.
628   Extracted from mpz/cfdiv_q_2exp.c.  Causes Apple's gcc 3.3 build 1640 and
629   1666 to get an ICE with -O1 -mpowerpc64.  */
630
631#if defined (__GNUC__) && ! defined (__cplusplus)
632int g();
633void f(int u){int i;long long x;x=u?~0:0;if(x)for(i=0;i<9;i++);x&=g();if(x)g();}
634int g(){return 0;}
635#else
636int dummy;
637#endif
638])
639
640GMP_PROG_CC_WORKS_PART([$1], [freebsd hacked gcc],
641[/* Provokes an ICE on i386-freebsd with the FreeBSD-hacked gcc, under
642   -O2 -march=amdfam10.  We call helper functions here "open" and "close" in
643   order for linking to succeed.  */
644
645#if defined (__GNUC__) && ! defined (__cplusplus)
646int open(int*,int*,int);void*close(int);void g(int*rp,int*up,int un){
647__builtin_expect(un<=0x7f00,1)?__builtin_alloca(un):close(un);if(__builtin_clzl
648(up[un])){open(rp,up,un);while(1){if(rp[un-1]!=0)break;un--;}}}
649#else
650int dummy;
651#endif
652])
653
654GMP_PROG_CC_WORKS_PART_MAIN([$1], [mpn_lshift_com optimization],
655[/* The following is mis-compiled by HP ia-64 cc version
656        cc: HP aC++/ANSI C B3910B A.05.55 [Dec 04 2003]
657   under "cc +O3", both in +DD32 and +DD64 modes.  The mpn_lshift_com gets
658   inlined and its return value somehow botched to be 0 instead of 1.  This
659   arises in the real mpn_lshift_com in mul_fft.c.  A lower optimization
660   level, like +O2 seems ok.  This code needs to be run to show the problem,
661   but that's fine, the offending cc is a native-only compiler so we don't
662   have to worry about cross compiling.  */
663
664#if ! defined (__cplusplus)
665unsigned long
666lshift_com (rp, up, n, cnt)
667  unsigned long *rp;
668  unsigned long *up;
669  long n;
670  unsigned cnt;
671{
672  unsigned long retval, high_limb, low_limb;
673  unsigned tnc;
674  long i;
675  tnc = 8 * sizeof (unsigned long) - cnt;
676  low_limb = *up++;
677  retval = low_limb >> tnc;
678  high_limb = low_limb << cnt;
679  for (i = n - 1; i != 0; i--)
680    {
681      low_limb = *up++;
682      *rp++ = ~(high_limb | (low_limb >> tnc));
683      high_limb = low_limb << cnt;
684    }
685  return retval;
686}
687int
688main ()
689{
690  unsigned long cy, rp[2], up[2];
691  up[0] = ~ 0L;
692  up[1] = 0;
693  cy = lshift_com (rp, up, 2L, 1);
694  if (cy != 1L)
695    return 1;
696  return 0;
697}
698#else
699int
700main ()
701{
702  return 0;
703}
704#endif
705])
706
707GMP_PROG_CC_WORKS_PART_MAIN([$1], [mpn_lshift_com optimization 2],
708[/* The following is mis-compiled by Intel ia-64 icc version 1.8 under
709    "icc -O3",  After several calls, the function writes partial garbage to
710    the result vector.  Perhaps relates to the chk.a.nc insn.  This code needs
711    to be run to show the problem, but that's fine, the offending cc is a
712    native-only compiler so we don't have to worry about cross compiling.  */
713
714#if ! defined (__cplusplus)
715#include <stdlib.h>
716void
717lshift_com (rp, up, n, cnt)
718  unsigned long *rp;
719  unsigned long *up;
720  long n;
721  unsigned cnt;
722{
723  unsigned long high_limb, low_limb;
724  unsigned tnc;
725  long i;
726  up += n;
727  rp += n;
728  tnc = 8 * sizeof (unsigned long) - cnt;
729  low_limb = *--up;
730  high_limb = low_limb << cnt;
731  for (i = n - 1; i != 0; i--)
732    {
733      low_limb = *--up;
734      *--rp = ~(high_limb | (low_limb >> tnc));
735      high_limb = low_limb << cnt;
736    }
737  *--rp = ~high_limb;
738}
739int
740main ()
741{
742  unsigned long *r, *r2;
743  unsigned long a[88 + 1];
744  long i;
745  for (i = 0; i < 88 + 1; i++)
746    a[i] = ~0L;
747  r = calloc (10000, sizeof (unsigned long));
748  r2 = r;
749  for (i = 0; i < 528; i += 23)
750    {
751      lshift_com (r2, a,
752		  i / (8 * sizeof (unsigned long)) + 1,
753		  i % (8 * sizeof (unsigned long)));
754      r2 += 88 + 1;
755    }
756  if (r[2048] != 0 || r[2049] != 0 || r[2050] != 0 || r[2051] != 0 ||
757      r[2052] != 0 || r[2053] != 0 || r[2054] != 0)
758    abort ();
759  free (r);
760  return 0;
761}
762#else
763int
764main ()
765{
766  return 0;
767}
768#endif
769])
770
771
772# A certain _GLOBAL_OFFSET_TABLE_ problem in past versions of gas, tickled
773# by recent versions of gcc.
774#
775if test "$gmp_prog_cc_works" = yes; then
776  case $host in
777    X86_PATTERN)
778      # this problem only arises in PIC code, so don't need to test when
779      # --disable-shared.  We don't necessarily have $enable_shared set to
780      # yes at this point, it will still be unset for the default (which is
781      # yes); hence the use of "!= no".
782      if test "$enable_shared" != no; then
783        GMP_PROG_CC_X86_GOT_EAX_EMITTED([$1],
784          [GMP_ASM_X86_GOT_EAX_OK([$1],,
785            [gmp_prog_cc_works="no, bad gas GOT with eax"])])
786      fi
787      ;;
788  esac
789fi
790
791AC_MSG_RESULT($gmp_prog_cc_works)
792case $gmp_prog_cc_works in
793  yes)
794    [$2]
795    ;;
796  *)
797    [$3]
798    ;;
799esac
800])
801
802dnl  Called: GMP_PROG_CC_WORKS_PART(CC+CFLAGS,FAIL-MESSAGE [,CODE])
803dnl  A dummy main() is appended to the CODE given.
804dnl
805AC_DEFUN([GMP_PROG_CC_WORKS_PART],
806[GMP_PROG_CC_WORKS_PART_MAIN([$1],[$2],
807[$3]
808[int main () { return 0; }])
809])
810
811dnl  Called: GMP_PROG_CC_WORKS_PART_MAIN(CC+CFLAGS,FAIL-MESSAGE,CODE)
812dnl  CODE must include a main().
813dnl
814AC_DEFUN([GMP_PROG_CC_WORKS_PART_MAIN],
815[GMP_PROG_CC_WORKS_PART_TEST([$1],[$2],[$3],
816  [],
817  gmp_prog_cc_works="no[]m4_if([$2],,,[[, ]])[$2]",
818  gmp_prog_cc_works="no[]m4_if([$2],,,[[, ]])[$2][[, program does not run]]")
819])
820
821dnl  Called: GMP_PROG_CC_WORKS_PART_TEST(CC+CFLAGS,TITLE,[CODE],
822dnl            [ACTION-GOOD],[ACTION-BAD][ACTION-NORUN])
823dnl
824AC_DEFUN([GMP_PROG_CC_WORKS_PART_TEST],
825[if test "$gmp_prog_cc_works" = yes; then
826  # remove anything that might look like compiler output to our "||" expression
827  rm -f conftest* a.out b.out a.exe a_out.exe
828  cat >conftest.c <<EOF
829[$3]
830EOF
831  echo "Test compile: [$2]" >&AC_FD_CC
832  gmp_compile="$1 conftest.c >&AC_FD_CC"
833  if AC_TRY_EVAL(gmp_compile); then
834    cc_works_part=yes
835    if test "$cross_compiling" = no; then
836      if AC_TRY_COMMAND([./a.out || ./b.out || ./a.exe || ./a_out.exe || ./conftest]); then :;
837      else
838        cc_works_part=norun
839      fi
840    fi
841  else
842    cc_works_part=no
843  fi
844  if test "$cc_works_part" != yes; then
845    echo "failed program was:" >&AC_FD_CC
846    cat conftest.c >&AC_FD_CC
847  fi
848  rm -f conftest* a.out b.out a.exe a_out.exe
849  case $cc_works_part in
850    yes)
851      $4
852      ;;
853    no)
854      $5
855      ;;
856    norun)
857      $6
858      ;;
859  esac
860fi
861])
862
863
864dnl  GMP_PROG_CC_WORKS_LONGLONG(cc+cflags,[ACTION-YES][,ACTION-NO])
865dnl  --------------------------------------------------------------
866dnl  Check that cc+cflags accepts "long long".
867dnl
868dnl  This test is designed to be run repeatedly with different cc+cflags
869dnl  selections, so the result is not cached.
870
871AC_DEFUN([GMP_PROG_CC_WORKS_LONGLONG],
872[AC_MSG_CHECKING([compiler $1 has long long])
873cat >conftest.c <<EOF
874long long  foo;
875long long  bar () { return foo; }
876int main () { return 0; }
877EOF
878gmp_prog_cc_works=no
879gmp_compile="$1 -c conftest.c >&AC_FD_CC"
880if AC_TRY_EVAL(gmp_compile); then
881  gmp_prog_cc_works=yes
882else
883  echo "failed program was:" >&AC_FD_CC
884  cat conftest.c >&AC_FD_CC
885fi
886rm -f conftest* a.out b.out a.exe a_out.exe
887AC_MSG_RESULT($gmp_prog_cc_works)
888if test $gmp_prog_cc_works = yes; then
889  ifelse([$2],,:,[$2])
890else
891  ifelse([$3],,:,[$3])
892fi
893])
894
895
896dnl  GMP_C_TEST_SIZEOF(cc/cflags,test,[ACTION-GOOD][,ACTION-BAD])
897dnl  ------------------------------------------------------------
898dnl  The given cc/cflags compiler is run to check the size of a type
899dnl  specified by the "test" argument.  "test" can either be a string, or a
900dnl  variable like $foo.  The value should be for instance "sizeof-long-4",
901dnl  to test that sizeof(long)==4.
902dnl
903dnl  This test is designed to be run for different compiler and/or flags
904dnl  combinations, so the result is not cached.
905dnl
906dnl  The idea for making an array that has a negative size if the desired
907dnl  condition test is false comes from autoconf AC_CHECK_SIZEOF.  The cast
908dnl  to "long" in the array dimension also follows autoconf, apparently it's
909dnl  a workaround for a HP compiler bug.
910
911AC_DEFUN([GMP_C_TEST_SIZEOF],
912[echo "configure: testlist $2" >&AC_FD_CC
913[gmp_sizeof_type=`echo "$2" | sed 's/sizeof-\([a-z\*]*\).*/\1/'`]
914[gmp_sizeof_want=`echo "$2" | sed 's/sizeof-[a-z\*]*-\([0-9]*\).*/\1/'`]
915AC_MSG_CHECKING([compiler $1 has sizeof($gmp_sizeof_type)==$gmp_sizeof_want])
916cat >conftest.c <<EOF
917[int
918main ()
919{
920  static int test_array [1 - 2 * (long) (sizeof ($gmp_sizeof_type) != $gmp_sizeof_want)];
921  test_array[0] = 0;
922  return 0;
923}]
924EOF
925gmp_c_testlist_sizeof=no
926gmp_compile="$1 -c conftest.c >&AC_FD_CC"
927if AC_TRY_EVAL(gmp_compile); then
928  gmp_c_testlist_sizeof=yes
929fi
930rm -f conftest*
931AC_MSG_RESULT($gmp_c_testlist_sizeof)
932if test $gmp_c_testlist_sizeof = yes; then
933  ifelse([$3],,:,[$3])
934else
935  ifelse([$4],,:,[$4])
936fi
937])
938
939
940dnl  GMP_PROG_CC_IS_GNU(CC,[ACTIONS-IF-YES][,ACTIONS-IF-NO])
941dnl  -------------------------------------------------------
942dnl  Determine whether the given compiler is GNU C.
943dnl
944dnl  This test is the same as autoconf _AC_LANG_COMPILER_GNU, but doesn't
945dnl  cache the result.  The same "ifndef" style test is used, to avoid
946dnl  problems with syntax checking cpp's used on NeXT and Apple systems.
947
948AC_DEFUN([GMP_PROG_CC_IS_GNU],
949[cat >conftest.c <<EOF
950#if ! defined (__GNUC__) || defined (__INTEL_COMPILER)
951  choke me
952#endif
953EOF
954gmp_compile="$1 -c conftest.c >&AC_FD_CC"
955if AC_TRY_EVAL(gmp_compile); then
956  rm -f conftest*
957  AC_MSG_CHECKING([whether $1 is gcc])
958  AC_MSG_RESULT(yes)
959  ifelse([$2],,:,[$2])
960else
961  rm -f conftest*
962  ifelse([$3],,:,[$3])
963fi
964])
965
966
967dnl  GMP_PROG_CC_IS_XLC(CC,[ACTIONS-IF-YES][,ACTIONS-IF-NO])
968dnl  -------------------------------------------------------
969dnl  Determine whether the given compiler is IBM xlc (on AIX).
970dnl
971dnl  There doesn't seem to be a preprocessor symbol to test for this, or if
972dnl  there is one then it's well hidden in xlc 3.1 on AIX 4.3, so just grep
973dnl  the man page printed when xlc is invoked with no arguments.
974
975AC_DEFUN([GMP_PROG_CC_IS_XLC],
976[gmp_command="$1 2>&1 | grep xlc >/dev/null"
977if AC_TRY_EVAL(gmp_command); then
978  AC_MSG_CHECKING([whether $1 is xlc])
979  AC_MSG_RESULT(yes)
980  ifelse([$2],,:,[$2])
981else
982  ifelse([$3],,:,[$3])
983fi
984])
985
986
987dnl  GMP_PROG_CC_X86_GOT_EAX_EMITTED(CC+CFLAGS, [ACTION-YES] [, ACTION-NO])
988dnl  ----------------------------------------------------------------------
989dnl  Determine whether CC+CFLAGS emits instructions using %eax with
990dnl  _GLOBAL_OFFSET_TABLE_.  This test is for use on x86 systems.
991dnl
992dnl  Recent versions of gcc will use %eax for the GOT in leaf functions, for
993dnl  instance gcc 3.3.3 with -O3.  This avoids having to save and restore
994dnl  %ebx which otherwise usually holds the GOT, and is what gcc used in the
995dnl  past.
996dnl
997dnl  %ecx and %edx are also candidates for this sort of optimization, and
998dnl  are used under lesser optimization levels, like -O2 in 3.3.3.  FIXME:
999dnl  It's not quite clear what the conditions for using %eax are, we might
1000dnl  need more test code to provoke it.
1001dnl
1002dnl  The motivation for this test is that past versions of gas have bugs
1003dnl  affecting this usage, see GMP_ASM_X86_GOT_EAX_OK.
1004dnl
1005dnl  This test is not specific to gcc, other compilers might emit %eax GOT
1006dnl  insns like this, though we've not investigated that.
1007dnl
1008dnl  This is for use by compiler probing in GMP_PROG_CC_WORKS, so we doesn't
1009dnl  cache the result.
1010dnl
1011dnl  -fPIC is hard coded here, because this test is for use before libtool
1012dnl  has established the pic options.  It's right for gcc, but perhaps not
1013dnl  other compilers.
1014
1015AC_DEFUN([GMP_PROG_CC_X86_GOT_EAX_EMITTED],
1016[echo "Testing gcc GOT with eax emitted" >&AC_FD_CC
1017cat >conftest.c <<\EOF
1018[int foo;
1019int bar () { return foo; }
1020]EOF
1021tmp_got_emitted=no
1022gmp_compile="$1 -fPIC -S conftest.c >&AC_FD_CC 2>&1"
1023if AC_TRY_EVAL(gmp_compile); then
1024  if grep "addl.*_GLOBAL_OFFSET_TABLE_.*eax" conftest.s >/dev/null; then
1025    tmp_got_emitted=yes
1026  fi
1027fi
1028rm -f conftest.*
1029echo "Result: $tmp_got_emitted" >&AC_FD_CC
1030if test "$tmp_got_emitted" = yes; then
1031  ifelse([$2],,:,[$2])
1032else
1033  ifelse([$3],,:,[$3])
1034fi
1035])
1036
1037
1038dnl  GMP_HPC_HPPA_2_0(cc,[ACTION-IF-GOOD][,ACTION-IF-BAD])
1039dnl  ---------------------------------------------------------
1040dnl  Find out whether a HP compiler is good enough to generate hppa 2.0.
1041dnl
1042dnl  This test might be repeated for different compilers, so the result is
1043dnl  not cached.
1044
1045AC_DEFUN([GMP_HPC_HPPA_2_0],
1046[AC_MSG_CHECKING([whether HP compiler $1 is good for 64-bits])
1047# Bad compiler output:
1048#   ccom: HP92453-01 G.10.32.05 HP C Compiler
1049# Good compiler output:
1050#   ccom: HP92453-01 A.10.32.30 HP C Compiler
1051# Let A.10.32.30 or higher be ok.
1052echo >conftest.c
1053gmp_tmp_vs=`$1 $2 -V -c -o conftest.$OBJEXT conftest.c 2>&1 | grep "^ccom:"`
1054echo "Version string: $gmp_tmp_vs" >&AC_FD_CC
1055rm conftest*
1056gmp_tmp_v1=`echo $gmp_tmp_vs | sed 's/.* .\.\([[0-9]]*\).*/\1/'`
1057gmp_tmp_v2=`echo $gmp_tmp_vs | sed 's/.* .\..*\.\(.*\)\..* HP C.*/\1/'`
1058gmp_tmp_v3=`echo $gmp_tmp_vs | sed 's/.* .\..*\..*\.\(.*\) HP C.*/\1/'`
1059echo "Version number: $gmp_tmp_v1.$gmp_tmp_v2.$gmp_tmp_v3" >&AC_FD_CC
1060if test -z "$gmp_tmp_v1"; then
1061  gmp_hpc_64bit=not-applicable
1062else
1063  GMP_COMPARE_GE($gmp_tmp_v1, 10, $gmp_tmp_v2, 32, $gmp_tmp_v3, 30)
1064  gmp_hpc_64bit=$gmp_compare_ge
1065fi
1066AC_MSG_RESULT($gmp_hpc_64bit)
1067if test $gmp_hpc_64bit = yes; then
1068  ifelse([$2],,:,[$2])
1069else
1070  ifelse([$3],,:,[$3])
1071fi
1072])
1073
1074
1075dnl  GMP_GCC_ARM_UMODSI(CC,[ACTIONS-IF-GOOD][,ACTIONS-IF-BAD])
1076dnl  ---------------------------------------------------------
1077dnl  gcc 2.95.3 and earlier on arm has a bug in the libgcc __umodsi routine
1078dnl  making "%" give wrong results for some operands, eg. "0x90000000 % 3".
1079dnl  We're hoping it'll be fixed in 2.95.4, and we know it'll be fixed in
1080dnl  gcc 3.
1081dnl
1082dnl  There's only a couple of places gmp cares about this, one is the
1083dnl  size==1 case in mpn/generic/mode1o.c, and this shows up in
1084dnl  tests/mpz/t-jac.c as a wrong result from mpz_kronecker_ui.
1085
1086AC_DEFUN([GMP_GCC_ARM_UMODSI],
1087[AC_MSG_CHECKING([whether ARM gcc unsigned division works])
1088tmp_version=`$1 --version`
1089echo "$tmp_version" >&AC_FD_CC
1090case $tmp_version in
1091  [2.95 | 2.95.[123]])
1092    ifelse([$3],,:,[$3])
1093    gmp_gcc_arm_umodsi_result=["no, gcc 2.95.[0123]"] ;;
1094  *)
1095    ifelse([$2],,:,[$2])
1096    gmp_gcc_arm_umodsi_result=yes ;;
1097esac
1098AC_MSG_RESULT([$gmp_gcc_arm_umodsi_result])
1099])
1100
1101
1102dnl  GMP_GCC_MIPS_O32(gcc,[actions-yes][,[actions-no]])
1103dnl  -------------------------------------------------
1104dnl  Test whether gcc supports o32.
1105dnl
1106dnl  gcc 2.7.2.2 only does o32, and doesn't accept -mabi=32.
1107dnl
1108dnl  gcc 2.95 accepts -mabi=32 but it only works on irix5, on irix6 it gives
1109dnl  "cc1: The -mabi=32 support does not work yet".
1110
1111AC_DEFUN([GMP_GCC_MIPS_O32],
1112[AC_MSG_CHECKING([whether gcc supports o32])
1113echo 'int x;' >conftest.c
1114echo "$1 -mabi=32 -c conftest.c" >&AC_FD_CC
1115if $1 -mabi=32 -c conftest.c >conftest.out 2>&1; then
1116  result=yes
1117else
1118  cat conftest.out >&AC_FD_CC
1119  if grep "cc1: Invalid option \`abi=32'" conftest.out >/dev/null; then
1120    result=yes
1121  else
1122    result=no
1123  fi
1124fi
1125rm -f conftest.*
1126AC_MSG_RESULT($result)
1127if test $result = yes; then
1128  ifelse([$2],,:,[$2])
1129else
1130  ifelse([$3],,:,[$3])
1131fi
1132])
1133
1134
1135dnl  GMP_GCC_NO_CPP_PRECOMP(CCBASE,CC,CFLAGS,[ACTIONS-YES][,ACTIONS-NO])
1136dnl  -------------------------------------------------------------------
1137dnl  Check whether -no-cpp-precomp should be used on this compiler, and
1138dnl  execute the corresponding ACTIONS-YES or ACTIONS-NO.
1139dnl
1140dnl  -no-cpp-precomp is only meant for Apple's hacked version of gcc found
1141dnl  on powerpc*-*-darwin*, but we can give it a try on any gcc.  Normal gcc
1142dnl  (as of 3.0 at least) only gives a warning, not an actual error, and we
1143dnl  watch for that and decide against the option in that case, to avoid
1144dnl  confusing the user.
1145
1146AC_DEFUN([GMP_GCC_NO_CPP_PRECOMP],
1147[if test "$ccbase" = gcc; then
1148  AC_MSG_CHECKING([compiler $2 $3 -no-cpp-precomp])
1149  result=no
1150  cat >conftest.c <<EOF
1151int main () { return 0; }
1152EOF
1153  gmp_compile="$2 $3 -no-cpp-precomp conftest.c >conftest.out 2>&1"
1154  if AC_TRY_EVAL(gmp_compile); then
1155    if grep "unrecognized option.*-no-cpp-precomp" conftest.out >/dev/null; then : ;
1156    else
1157      result=yes
1158    fi
1159  fi
1160  cat conftest.out >&AC_FD_CC
1161  rm -f conftest* a.out b.out a.exe a_out.exe
1162  AC_MSG_RESULT($result)
1163  if test "$result" = yes; then
1164      ifelse([$4],,:,[$4])
1165  else
1166      ifelse([$5],,:,[$5])
1167  fi
1168fi
1169])
1170
1171
1172dnl  GMP_GCC_PENTIUM4_SSE2(CC+CFLAGS,[ACTION-IF-YES][,ACTION-IF-NO])
1173dnl  ---------------------------------------------------------------
1174dnl  Determine whether gcc CC+CFLAGS is a good enough version for
1175dnl  -march=pentium4 with sse2.
1176dnl
1177dnl  Gcc 3.2.1 was seen generating incorrect code for raw double -> int
1178dnl  conversions through a union.  We believe the problem is in all 3.1 and
1179dnl  3.2 versions, but that it's fixed in 3.3.
1180
1181AC_DEFUN([GMP_GCC_PENTIUM4_SSE2],
1182[AC_MSG_CHECKING([whether gcc is good for sse2])
1183case `$1 -dumpversion` in
1184  [3.[012] | 3.[012].*]) result=no ;;
1185  *)                     result=yes ;;
1186esac
1187AC_MSG_RESULT($result)
1188if test "$result" = yes; then
1189  ifelse([$2],,:,[$2])
1190else
1191  ifelse([$3],,:,[$3])
1192fi
1193])
1194
1195
1196dnl  GMP_GCC_WA_MCPU(CC+CFLAGS, NEWFLAG [,ACTION-YES [,ACTION-NO]])
1197dnl  --------------------------------------------------------------
1198dnl  Check whether gcc (or gas rather) accepts a flag like "-Wa,-mev67".
1199dnl
1200dnl  Gas doesn't give an error for an unknown cpu, it only prints a warning
1201dnl  like "Warning: Unknown CPU identifier `ev78'".
1202dnl
1203dnl  This is intended for use on alpha, since only recent versions of gas
1204dnl  accept -mev67, but there's nothing here that's alpha specific.
1205
1206AC_DEFUN([GMP_GCC_WA_MCPU],
1207[AC_MSG_CHECKING([assembler $1 $2])
1208result=no
1209cat >conftest.c <<EOF
1210int main () {}
1211EOF
1212gmp_compile="$1 $2 -c conftest.c >conftest.out 2>&1"
1213if AC_TRY_EVAL(gmp_compile); then
1214  if grep "Unknown CPU identifier" conftest.out >/dev/null; then : ;
1215  else
1216    result=yes
1217  fi
1218fi
1219cat conftest.out >&AC_FD_CC
1220rm -f conftest*
1221AC_MSG_RESULT($result)
1222if test "$result" = yes; then
1223  ifelse([$3],,:,[$3])
1224else
1225  ifelse([$4],,:,[$4])
1226fi
1227])
1228
1229
1230dnl  GMP_GCC_WA_OLDAS(CC+CFLAGS [,ACTION-YES [,ACTION-NO]])
1231dnl  ------------------------------------------------------
1232dnl  Check whether gcc should be run with "-Wa,-oldas".
1233dnl
1234dnl  On systems alpha*-*-osf* (or maybe just osf5), apparently there's a
1235dnl  newish Compaq "as" which doesn't work with the gcc mips-tfile.
1236dnl  Compiling an empty file with "gcc -c foo.c" produces for instance
1237dnl
1238dnl      mips-tfile, /tmp/ccaqUNnF.s:7 Segmentation fault
1239dnl
1240dnl  The fix is to pass "-oldas" to that assembler, as noted by
1241dnl
1242dnl      http://gcc.gnu.org/install/specific.html#alpha*-dec-osf*
1243dnl
1244dnl  The test here tries to compile an empty file, and if that fails but
1245dnl  adding -Wa,-oldas makes it succeed, then that flag is considered
1246dnl  necessary.
1247dnl
1248dnl  We look for the failing case specifically, since it may not be a good
1249dnl  idea to use -Wa,-oldas in other circumstances.  For instance gas takes
1250dnl  "-oldas" to mean the "-o" option and will write a file called "ldas" as
1251dnl  its output.  Normally gcc puts its own "-o" after any -Wa options, so
1252dnl  -oldas ends up being harmless, but clearly that's only through good
1253dnl  luck.
1254dnl
1255dnl  This macro is designed for use while probing for a good compiler, and
1256dnl  so doesn't cache it's result.
1257
1258AC_DEFUN([GMP_GCC_WA_OLDAS],
1259[AC_MSG_CHECKING([for $1 -Wa,-oldas])
1260result=no
1261cat >conftest.c <<EOF
1262EOF
1263echo "with empty conftest.c" >&AC_FD_CC
1264gmp_compile="$1 -c conftest.c >&AC_FD_CC 2>&1"
1265if AC_TRY_EVAL(gmp_compile); then : ;
1266else
1267  # empty fails
1268  gmp_compile="$1 -Wa,-oldas -c conftest.c >&AC_FD_CC 2>&1"
1269  if AC_TRY_EVAL(gmp_compile); then
1270    # but with -Wa,-oldas it works
1271    result=yes
1272  fi
1273fi
1274rm -f conftest*
1275AC_MSG_RESULT($result)
1276if test "$result" = yes; then
1277  ifelse([$2],,:,[$2])
1278else
1279  ifelse([$3],,:,[$3])
1280fi
1281])
1282
1283
1284dnl  GMP_OS_X86_XMM(CC+CFLAGS,[ACTION-IF-YES][,ACTION-IF-NO])
1285dnl  --------------------------------------------------------
1286dnl  Determine whether the operating system supports XMM registers.
1287dnl
1288dnl  If build==host then a test program is run, executing an SSE2
1289dnl  instruction using an XMM register.  This will give a SIGILL if the
1290dnl  system hasn't set the OSFXSR bit in CR4 to say it knows it must use
1291dnl  fxsave/fxrestor in a context switch (to save xmm registers).
1292dnl
1293dnl  If build!=host, we can fallback on:
1294dnl
1295dnl      - FreeBSD version 4 is the first supporting xmm.
1296dnl
1297dnl      - Linux kernel 2.4 might be the first stable series supporting xmm
1298dnl        (not sure).  But there's no version number in the GNU/Linux
1299dnl        config tuple to test anyway.
1300dnl
1301dnl  The default is to allow xmm.  This might seem rash, but it's likely
1302dnl  most systems know xmm by now, so this will normally be what's wanted.
1303dnl  And cross compiling is a bit hairy anyway, so hopefully anyone doing it
1304dnl  will be smart enough to know what to do.
1305dnl
1306dnl  In the test program, .text and .globl are hard coded because this macro
1307dnl  is wanted before GMP_ASM_TEXT and GMP_ASM_GLOBL are run.  A .byte
1308dnl  sequence is used (for xorps %xmm0, %xmm0) to make us independent of
1309dnl  tests for whether the assembler supports sse2/xmm.  Obviously we need
1310dnl  both assembler and OS support, but this means we don't force the order
1311dnl  in which we test.
1312dnl
1313dnl  FIXME: Maybe we should use $CCAS to assemble, if it's set.  (Would
1314dnl  still want $CC/$CFLAGS for the link.)  But this test is used before
1315dnl  AC_PROG_CC sets $OBJEXT, so we'd need to check for various object file
1316dnl  suffixes ourselves.
1317
1318AC_DEFUN([GMP_OS_X86_XMM],
1319[AC_CACHE_CHECK([whether the operating system supports XMM registers],
1320		gmp_cv_os_x86_xmm,
1321[if test "$build" = "$host"; then
1322  # remove anything that might look like compiler output to our "||" expression
1323  rm -f conftest* a.out b.out a.exe a_out.exe
1324  cat >conftest.s <<EOF
1325	.text
1326main:
1327_main:
1328	.globl	main
1329	.globl	_main
1330	.byte	0x0f, 0x57, 0xc0
1331	xorl	%eax, %eax
1332	ret
1333EOF
1334  gmp_compile="$1 conftest.s -o conftest >&AC_FD_CC"
1335  if AC_TRY_EVAL(gmp_compile); then
1336    if AC_TRY_COMMAND([./a.out || ./b.out || ./a.exe || ./a_out.exe || ./conftest]); then
1337      gmp_cv_os_x86_xmm=yes
1338    else
1339      gmp_cv_os_x86_xmm=no
1340    fi
1341  else
1342    AC_MSG_WARN([Oops, cannot compile test program])
1343  fi
1344  rm -f conftest*
1345fi
1346
1347if test -z "$gmp_cv_os_x86_xmm"; then
1348  case $host_os in
1349    [freebsd[123] | freebsd[123].*])
1350      gmp_cv_os_x86_xmm=no ;;
1351    freebsd*)
1352      gmp_cv_os_x86_xmm=yes ;;
1353    *)
1354      gmp_cv_os_x86_xmm=probably ;;
1355  esac
1356fi
1357])
1358
1359if test "$gmp_cv_os_x86_xmm" = probably; then
1360  AC_MSG_WARN([Not certain of OS support for xmm when cross compiling.])
1361  AC_MSG_WARN([Will assume it's ok, expect a SIGILL if this is wrong.])
1362fi
1363
1364case $gmp_cv_os_x86_xmm in
1365no)
1366  $3
1367  ;;
1368*)
1369  $2
1370  ;;
1371esac
1372])
1373
1374
1375dnl  GMP_CRAY_HOST_TYPES(C90/T90-IEEE, C90/T90-CFP, J90/SV1)
1376dnl  -------------------------------------------------------
1377dnl  Execute the actions in the arguments on the respective Cray vector
1378dnl  systems.  For other hosts, do nothing.
1379dnl
1380dnl  This macro should be used after the C compiler has been chosen, since
1381dnl  on c90 and t90 we ask the compiler whether we're in IEEE or CFP float
1382dnl  mode.
1383dnl
1384dnl  This code is in a macro so that any AC_REQUIRE pre-requisites of
1385dnl  AC_EGREP_CPP will be expanded at the top-level, ie. for all hosts not
1386dnl  merely c90 and t90.  In autoconf 2.57 for instance this means
1387dnl  AC_PROG_EGREP, which is needed by various other macros.
1388
1389AC_DEFUN([GMP_CRAY_OPTIONS],
1390[case $host_cpu in
1391  c90 | t90)
1392    AC_EGREP_CPP(yes,
1393[#ifdef _CRAYIEEE
1394yes
1395#endif],
1396    [$1],
1397    [$2])
1398    ;;
1399  j90 | sv1)
1400    [$3]
1401    ;;
1402esac
1403])
1404
1405
1406dnl  GMP_HPPA_LEVEL_20(cc/cflags [, ACTION-GOOD [,ACTION-BAD]])
1407dnl  ----------------------------------------------------------
1408dnl  Check that the given cc/cflags accepts HPPA 2.0n assembler code.
1409dnl
1410dnl  Old versions of gas don't know 2.0 instructions.  It rejects ".level
1411dnl  2.0" for a start, so just test that.
1412dnl
1413dnl  This test is designed to be run for various different compiler and
1414dnl  flags combinations, and hence doesn't cache its result.
1415
1416AC_DEFUN([GMP_HPPA_LEVEL_20],
1417[AC_MSG_CHECKING([$1 assembler knows hppa 2.0])
1418result=no
1419cat >conftest.s <<EOF
1420	.level 2.0
1421EOF
1422gmp_compile="$1 -c conftest.s >&AC_FD_CC 2>&1"
1423if AC_TRY_EVAL(gmp_compile); then
1424  result=yes
1425else
1426  echo "failed program was" >&AC_FD_CC
1427  cat conftest.s >&AC_FD_CC
1428fi
1429rm -f conftest*
1430AC_MSG_RESULT($result)
1431if test "$result" = yes; then
1432  ifelse([$2],,:,[$2])
1433else
1434  ifelse([$3],,:,[$3])
1435fi
1436])
1437
1438
1439dnl  GMP_PROG_CXX_WORKS(cxx/cxxflags [, ACTION-YES [,ACTION-NO]])
1440dnl  ------------------------------------------------------------
1441dnl  Check whether cxx/cxxflags can compile and link.
1442dnl
1443dnl  This test is designed to be run repeatedly with different cxx/cxxflags
1444dnl  selections, so the result is not cached.
1445dnl
1446dnl  For a native build, we insist on being able to run the program, so as
1447dnl  to detect any problems with the standard C++ library.  During
1448dnl  development various systems with broken or incomplete C++ installations
1449dnl  were seen.
1450dnl
1451dnl  The various features and problems we try to detect are done in separate
1452dnl  compiles.  Although this is probably a bit slower than one test
1453dnl  program, it makes it easy to indicate the problem in AC_MSG_RESULT,
1454dnl  hence giving the user a clue about why we rejected the compiler.
1455
1456AC_DEFUN([GMP_PROG_CXX_WORKS],
1457[AC_MSG_CHECKING([C++ compiler $1])
1458gmp_prog_cxx_works=yes
1459
1460# start with a plain "main()", then go on to further checks
1461GMP_PROG_CXX_WORKS_PART([$1], [])
1462
1463GMP_PROG_CXX_WORKS_PART([$1], [namespace],
1464[namespace foo { }
1465using namespace foo;
1466])
1467
1468# GMP requires the standard C++ iostream classes
1469GMP_PROG_CXX_WORKS_PART([$1], [std iostream],
1470[/* This test rejects g++ 2.7.2 which doesn't have <iostream>, only a
1471    pre-standard iostream.h. */
1472#include <iostream>
1473
1474/* This test rejects OSF 5.1 Compaq C++ in its default pre-standard iostream
1475   mode, since that mode puts cout in the global namespace, not "std".  */
1476void someoutput (void) { std::cout << 123; }
1477])
1478
1479AC_MSG_RESULT($gmp_prog_cxx_works)
1480case $gmp_prog_cxx_works in
1481  yes)
1482    [$2]
1483    ;;
1484  *)
1485    [$3]
1486    ;;
1487esac
1488])
1489
1490dnl  Called: GMP_PROG_CXX_WORKS_PART(CXX+CXXFLAGS, FAIL-MESSAGE [,CODE])
1491dnl
1492AC_DEFUN([GMP_PROG_CXX_WORKS_PART],
1493[if test "$gmp_prog_cxx_works" = yes; then
1494  # remove anything that might look like compiler output to our "||" expression
1495  rm -f conftest* a.out b.out a.exe a_out.exe
1496  cat >conftest.cc <<EOF
1497[$3]
1498int main (void) { return 0; }
1499EOF
1500  echo "Test compile: [$2]" >&AC_FD_CC
1501  gmp_cxxcompile="$1 conftest.cc >&AC_FD_CC"
1502  if AC_TRY_EVAL(gmp_cxxcompile); then
1503    if test "$cross_compiling" = no; then
1504      if AC_TRY_COMMAND([./a.out || ./b.out || ./a.exe || ./a_out.exe || ./conftest]); then :;
1505      else
1506        gmp_prog_cxx_works="no[]m4_if([$2],,,[, ])[$2], program does not run"
1507      fi
1508    fi
1509  else
1510    gmp_prog_cxx_works="no[]m4_if([$2],,,[, ])[$2]"
1511  fi
1512  case $gmp_prog_cxx_works in
1513    no*)
1514      echo "failed program was:" >&AC_FD_CC
1515      cat conftest.cc >&AC_FD_CC
1516      ;;
1517  esac
1518  rm -f conftest* a.out b.out a.exe a_out.exe
1519fi
1520])
1521
1522
1523dnl  GMP_INIT([M4-DEF-FILE])
1524dnl  -----------------------
1525dnl  Initializations for GMP config.m4 generation.
1526dnl
1527dnl  FIXME: The generated config.m4 doesn't get recreated by config.status.
1528dnl  Maybe the relevant "echo"s should go through AC_CONFIG_COMMANDS.
1529
1530AC_DEFUN([GMP_INIT],
1531[ifelse([$1], , gmp_configm4=config.m4, gmp_configm4="[$1]")
1532gmp_tmpconfigm4=cnfm4.tmp
1533gmp_tmpconfigm4i=cnfm4i.tmp
1534gmp_tmpconfigm4p=cnfm4p.tmp
1535rm -f $gmp_tmpconfigm4 $gmp_tmpconfigm4i $gmp_tmpconfigm4p
1536
1537# CONFIG_TOP_SRCDIR is a path from the mpn builddir to the top srcdir.
1538# The pattern here tests for an absolute path the same way as
1539# _AC_OUTPUT_FILES in autoconf acgeneral.m4.
1540case $srcdir in
1541[[\\/]]* | ?:[[\\/]]* )  tmp="$srcdir"    ;;
1542*)                       tmp="../$srcdir" ;;
1543esac
1544echo ["define(<CONFIG_TOP_SRCDIR>,<\`$tmp'>)"] >>$gmp_tmpconfigm4
1545
1546# All CPUs use asm-defs.m4
1547echo ["include][(CONFIG_TOP_SRCDIR\`/mpn/asm-defs.m4')"] >>$gmp_tmpconfigm4i
1548])
1549
1550
1551dnl  GMP_FINISH
1552dnl  ----------
1553dnl  Create config.m4 from its accumulated parts.
1554dnl
1555dnl  __CONFIG_M4_INCLUDED__ is used so that a second or subsequent include
1556dnl  of config.m4 is harmless.
1557dnl
1558dnl  A separate ifdef on the angle bracket quoted part ensures the quoting
1559dnl  style there is respected.  The basic defines from gmp_tmpconfigm4 are
1560dnl  fully quoted but are still put under an ifdef in case any have been
1561dnl  redefined by one of the m4 include files.
1562dnl
1563dnl  Doing a big ifdef within asm-defs.m4 and/or other macro files wouldn't
1564dnl  work, since it'd interpret parentheses and quotes in dnl comments, and
1565dnl  having a whole file as a macro argument would overflow the string space
1566dnl  on BSD m4.
1567
1568AC_DEFUN([GMP_FINISH],
1569[AC_REQUIRE([GMP_INIT])
1570echo "creating $gmp_configm4"
1571echo ["d""nl $gmp_configm4.  Generated automatically by configure."] > $gmp_configm4
1572if test -f $gmp_tmpconfigm4; then
1573  echo ["changequote(<,>)"] >> $gmp_configm4
1574  echo ["ifdef(<__CONFIG_M4_INCLUDED__>,,<"] >> $gmp_configm4
1575  cat $gmp_tmpconfigm4 >> $gmp_configm4
1576  echo [">)"] >> $gmp_configm4
1577  echo ["changequote(\`,')"] >> $gmp_configm4
1578  rm $gmp_tmpconfigm4
1579fi
1580echo ["ifdef(\`__CONFIG_M4_INCLUDED__',,\`"] >> $gmp_configm4
1581if test -f $gmp_tmpconfigm4i; then
1582  cat $gmp_tmpconfigm4i >> $gmp_configm4
1583  rm $gmp_tmpconfigm4i
1584fi
1585if test -f $gmp_tmpconfigm4p; then
1586  cat $gmp_tmpconfigm4p >> $gmp_configm4
1587  rm $gmp_tmpconfigm4p
1588fi
1589echo ["')"] >> $gmp_configm4
1590echo ["define(\`__CONFIG_M4_INCLUDED__')"] >> $gmp_configm4
1591])
1592
1593
1594dnl  GMP_INCLUDE_MPN(FILE)
1595dnl  ---------------------
1596dnl  Add an include_mpn(`FILE') to config.m4.  FILE should be a path
1597dnl  relative to the mpn source directory, for example
1598dnl
1599dnl      GMP_INCLUDE_MPN(`x86/x86-defs.m4')
1600dnl
1601
1602AC_DEFUN([GMP_INCLUDE_MPN],
1603[AC_REQUIRE([GMP_INIT])
1604echo ["include_mpn(\`$1')"] >> $gmp_tmpconfigm4i
1605])
1606
1607
1608dnl  GMP_DEFINE(MACRO, DEFINITION [, LOCATION])
1609dnl  ------------------------------------------
1610dnl  Define M4 macro MACRO as DEFINITION in temporary file.
1611dnl
1612dnl  If LOCATION is `POST', the definition will appear after any include()
1613dnl  directives inserted by GMP_INCLUDE.  Mind the quoting!  No shell
1614dnl  variables will get expanded.  Don't forget to invoke GMP_FINISH to
1615dnl  create file config.m4.  config.m4 uses `<' and '>' as quote characters
1616dnl  for all defines.
1617
1618AC_DEFUN([GMP_DEFINE],
1619[AC_REQUIRE([GMP_INIT])
1620echo ['define(<$1>, <$2>)'] >>ifelse([$3], [POST],
1621                              $gmp_tmpconfigm4p, $gmp_tmpconfigm4)
1622])
1623
1624
1625dnl  GMP_DEFINE_RAW(STRING [, LOCATION])
1626dnl  ------------------------------------
1627dnl  Put STRING into config.m4 file.
1628dnl
1629dnl  If LOCATION is `POST', the definition will appear after any include()
1630dnl  directives inserted by GMP_INCLUDE.  Don't forget to invoke GMP_FINISH
1631dnl  to create file config.m4.
1632
1633AC_DEFUN([GMP_DEFINE_RAW],
1634[AC_REQUIRE([GMP_INIT])
1635echo [$1] >> ifelse([$2], [POST], $gmp_tmpconfigm4p, $gmp_tmpconfigm4)
1636])
1637
1638
1639dnl  GMP_TRY_ASSEMBLE(asm-code,[action-success][,action-fail])
1640dnl  ----------------------------------------------------------
1641dnl  Attempt to assemble the given code.
1642dnl  Do "action-success" if this succeeds, "action-fail" if not.
1643dnl
1644dnl  conftest.o and conftest.out are available for inspection in
1645dnl  "action-success".  If either action does a "break" out of a loop then
1646dnl  an explicit "rm -f conftest*" will be necessary.
1647dnl
1648dnl  This is not unlike AC_TRY_COMPILE, but there's no default includes or
1649dnl  anything in "asm-code", everything wanted must be given explicitly.
1650
1651AC_DEFUN([GMP_TRY_ASSEMBLE],
1652[cat >conftest.s <<EOF
1653[$1]
1654EOF
1655gmp_assemble="$CCAS $CFLAGS $CPPFLAGS conftest.s >conftest.out 2>&1"
1656if AC_TRY_EVAL(gmp_assemble); then
1657  cat conftest.out >&AC_FD_CC
1658  ifelse([$2],,:,[$2])
1659else
1660  cat conftest.out >&AC_FD_CC
1661  echo "configure: failed program was:" >&AC_FD_CC
1662  cat conftest.s >&AC_FD_CC
1663  ifelse([$3],,:,[$3])
1664fi
1665rm -f conftest*
1666])
1667
1668
1669dnl Checks whether the stack can be marked nonexecutable by passing an option
1670dnl to the C-compiler when acting on .s files. Appends that option to ASMFLAGS.
1671dnl This macro is adapted from one found in GLIBC-2.3.5.
1672dnl FIXME: This test looks broken. It tests that a file with .note.GNU-stack...
1673dnl can be compiled/assembled with -Wa,--noexecstack.  It does not determine
1674dnl if that command-line option has any effect on general asm code.
1675AC_DEFUN([CL_AS_NOEXECSTACK],[
1676dnl AC_REQUIRE([AC_PROG_CC]) GMP uses something else
1677AC_CACHE_CHECK([whether assembler supports --noexecstack option],
1678cl_cv_as_noexecstack, [dnl
1679  cat > conftest.c <<EOF
1680void foo() {}
1681EOF
1682  if AC_TRY_COMMAND([${CC} $CFLAGS $CPPFLAGS
1683                     -S -o conftest.s conftest.c >/dev/null]) \
1684     && grep .note.GNU-stack conftest.s >/dev/null \
1685     && AC_TRY_COMMAND([${CC} $CFLAGS $CPPFLAGS -Wa,--noexecstack
1686                       -c -o conftest.o conftest.s >/dev/null])
1687  then
1688    cl_cv_as_noexecstack=yes
1689  else
1690    cl_cv_as_noexecstack=no
1691  fi
1692  rm -f conftest*])
1693  if test "$cl_cv_as_noexecstack" = yes; then
1694    ASMFLAGS="$ASMFLAGS -Wa,--noexecstack"
1695  fi
1696  AC_SUBST(ASMFLAGS)
1697])
1698
1699
1700dnl  GMP_ASM_LABEL_SUFFIX
1701dnl  --------------------
1702dnl  : - is usual.
1703dnl  empty - hppa on HP-UX doesn't use a :, just the label name
1704dnl
1705dnl  Note that it's necessary to test the empty case first, since HP "as"
1706dnl  will accept "somelabel:", and take it to mean a label with a name that
1707dnl  happens to end in a colon.
1708
1709AC_DEFUN([GMP_ASM_LABEL_SUFFIX],
1710[AC_REQUIRE([GMP_ASM_TEXT])
1711AC_CACHE_CHECK([for assembler label suffix],
1712                gmp_cv_asm_label_suffix,
1713[gmp_cv_asm_label_suffix=unknown
1714for i in "" ":"; do
1715  echo "trying $i" >&AC_FD_CC
1716  GMP_TRY_ASSEMBLE(
1717[	$gmp_cv_asm_text
1718somelabel$i],
1719    [gmp_cv_asm_label_suffix=$i
1720     rm -f conftest*
1721     break],
1722    [cat conftest.out >&AC_FD_CC])
1723done
1724if test "$gmp_cv_asm_label_suffix" = "unknown"; then
1725  AC_MSG_ERROR([Cannot determine label suffix])
1726fi
1727])
1728echo ["define(<LABEL_SUFFIX>, <$gmp_cv_asm_label_suffix>)"] >> $gmp_tmpconfigm4
1729])
1730
1731
1732dnl  GMP_ASM_UNDERSCORE
1733dnl  ------------------
1734dnl  Determine whether global symbols need to be prefixed with an underscore.
1735dnl  The output from "nm" is grepped to see what a typical symbol looks like.
1736dnl
1737dnl  This test used to grep the .o file directly, but that failed with greps
1738dnl  that don't like binary files (eg. SunOS 4).
1739dnl
1740dnl  This test also used to construct an assembler file with and without an
1741dnl  underscore and try to link that to a C file, to see which worked.
1742dnl  Although that's what will happen in the real build we don't really want
1743dnl  to depend on creating asm files within configure for every possible CPU
1744dnl  (or at least we don't want to do that more than we have to).
1745dnl
1746dnl  The fallback on no underscore is based on the assumption that the world
1747dnl  is moving towards non-underscore systems.  There should actually be no
1748dnl  good reason for nm to fail though.
1749
1750AC_DEFUN([GMP_ASM_UNDERSCORE],
1751[AC_REQUIRE([GMP_PROG_NM])
1752AC_CACHE_CHECK([if globals are prefixed by underscore],
1753               gmp_cv_asm_underscore,
1754[gmp_cv_asm_underscore="unknown"
1755cat >conftest.c <<EOF
1756int gurkmacka;
1757EOF
1758gmp_compile="$CC $CFLAGS $CPPFLAGS -c conftest.c >&AC_FD_CC"
1759if AC_TRY_EVAL(gmp_compile); then
1760  $NM conftest.$OBJEXT >conftest.out
1761  if grep "[[ 	]]_gurkmacka" conftest.out >/dev/null; then
1762    gmp_cv_asm_underscore=yes
1763  elif grep "[[ 	]]gurkmacka" conftest.out >/dev/null; then
1764    gmp_cv_asm_underscore=no
1765  else
1766    echo "configure: $NM doesn't have gurkmacka:" >&AC_FD_CC
1767    cat conftest.out >&AC_FD_CC
1768  fi
1769else
1770  echo "configure: failed program was:" >&AC_FD_CC
1771  cat conftest.c >&AC_FD_CC
1772fi
1773rm -f conftest*
1774])
1775case $gmp_cv_asm_underscore in
1776  yes)
1777    GMP_DEFINE(GSYM_PREFIX, [_]) ;;
1778  no)
1779    GMP_DEFINE(GSYM_PREFIX, []) ;;
1780  *)
1781    AC_MSG_WARN([+----------------------------------------------------------])
1782    AC_MSG_WARN([| Cannot determine global symbol prefix.])
1783    AC_MSG_WARN([| $NM output doesn't contain a global data symbol.])
1784    AC_MSG_WARN([| Will proceed with no underscore.])
1785    AC_MSG_WARN([| If this is wrong then you'll get link errors referring])
1786    AC_MSG_WARN([| to ___gmpn_add_n (note three underscores).])
1787    AC_MSG_WARN([| In this case do a fresh build with an override,])
1788    AC_MSG_WARN([|     ./configure gmp_cv_asm_underscore=yes])
1789    AC_MSG_WARN([+----------------------------------------------------------])
1790    GMP_DEFINE(GSYM_PREFIX, [])
1791    ;;
1792esac
1793])
1794
1795
1796dnl  GMP_ASM_ALIGN_LOG
1797dnl  -----------------
1798dnl  Is parameter to `.align' logarithmic?
1799
1800AC_DEFUN([GMP_ASM_ALIGN_LOG],
1801[AC_REQUIRE([GMP_ASM_GLOBL])
1802AC_REQUIRE([GMP_ASM_BYTE])
1803AC_REQUIRE([GMP_ASM_DATA])
1804AC_REQUIRE([GMP_ASM_LABEL_SUFFIX])
1805AC_REQUIRE([GMP_PROG_NM])
1806AC_CACHE_CHECK([if .align assembly directive is logarithmic],
1807               gmp_cv_asm_align_log,
1808[GMP_TRY_ASSEMBLE(
1809[	$gmp_cv_asm_data
1810	.align  4
1811	$gmp_cv_asm_globl	foo
1812	$gmp_cv_asm_byte	1
1813	.align	4
1814foo$gmp_cv_asm_label_suffix
1815	$gmp_cv_asm_byte	2],
1816  [gmp_tmp_val=[`$NM conftest.$OBJEXT | grep foo | \
1817     sed -e 's;[[][0-9][]]\(.*\);\1;' -e 's;[^1-9]*\([0-9]*\).*;\1;'`]
1818  if test "$gmp_tmp_val" = "10" || test "$gmp_tmp_val" = "16"; then
1819    gmp_cv_asm_align_log=yes
1820  else
1821    gmp_cv_asm_align_log=no
1822  fi],
1823  [AC_MSG_ERROR([cannot assemble alignment test])])])
1824
1825GMP_DEFINE_RAW(["define(<ALIGN_LOGARITHMIC>,<$gmp_cv_asm_align_log>)"])
1826])
1827
1828
1829dnl  GMP_ASM_ALIGN_FILL_0x90
1830dnl  -----------------------
1831dnl  Determine whether a ",0x90" suffix works on a .align directive.
1832dnl  This is only meant for use on x86, 0x90 being a "nop".
1833dnl
1834dnl  Old gas, eg. 1.92.3
1835dnl       Needs ",0x90" or else the fill is 0x00, which can't be executed
1836dnl       across.
1837dnl
1838dnl  New gas, eg. 2.91
1839dnl       Generates multi-byte nop fills even when ",0x90" is given.
1840dnl
1841dnl  Solaris 2.6 as
1842dnl       ",0x90" is not allowed, causes a fatal error.
1843dnl
1844dnl  Solaris 2.8 as
1845dnl       ",0x90" does nothing, generates a warning that it's being ignored.
1846dnl
1847dnl  SCO OpenServer 5 as
1848dnl       Second parameter is max bytes to fill, not a fill pattern.
1849dnl       ",0x90" is an error due to being bigger than the first parameter.
1850dnl       Multi-byte nop fills are generated in text segments.
1851dnl
1852dnl  Note that both solaris "as"s only care about ",0x90" if they actually
1853dnl  have to use it to fill something, hence the .byte in the test.  It's
1854dnl  the second .align which provokes the error or warning.
1855dnl
1856dnl  The warning from solaris 2.8 is suppressed to stop anyone worrying that
1857dnl  something might be wrong.
1858
1859AC_DEFUN([GMP_ASM_ALIGN_FILL_0x90],
1860[AC_REQUIRE([GMP_ASM_TEXT])
1861AC_CACHE_CHECK([if the .align directive accepts an 0x90 fill in .text],
1862               gmp_cv_asm_align_fill_0x90,
1863[GMP_TRY_ASSEMBLE(
1864[	$gmp_cv_asm_text
1865	.align  4, 0x90
1866	.byte   0
1867	.align  4, 0x90],
1868[if grep "Warning: Fill parameter ignored for executable section" conftest.out >/dev/null; then
1869  echo "Suppressing this warning by omitting 0x90" 1>&AC_FD_CC
1870  gmp_cv_asm_align_fill_0x90=no
1871else
1872  gmp_cv_asm_align_fill_0x90=yes
1873fi],
1874[gmp_cv_asm_align_fill_0x90=no])])
1875
1876GMP_DEFINE_RAW(["define(<ALIGN_FILL_0x90>,<$gmp_cv_asm_align_fill_0x90>)"])
1877])
1878
1879
1880dnl  GMP_ASM_BYTE
1881dnl  ------------
1882dnl  .byte - is usual.
1883dnl  data1 - required by ia64 (on hpux at least).
1884dnl
1885dnl  This macro is just to support other configure tests, not any actual asm
1886dnl  code.
1887
1888AC_DEFUN([GMP_ASM_BYTE],
1889[AC_REQUIRE([GMP_ASM_TEXT])
1890AC_REQUIRE([GMP_ASM_LABEL_SUFFIX])
1891AC_CACHE_CHECK([for assembler byte directive],
1892                gmp_cv_asm_byte,
1893[for i in .byte data1; do
1894  echo "trying $i" >&AC_FD_CC
1895  GMP_TRY_ASSEMBLE(
1896[	$gmp_cv_asm_data
1897	$i	0
1898],
1899    [gmp_cv_asm_byte=$i
1900     rm -f conftest*
1901     break],
1902    [cat conftest.out >&AC_FD_CC])
1903done
1904if test -z "$gmp_cv_asm_byte"; then
1905  AC_MSG_ERROR([Cannot determine how to emit a data byte])
1906fi
1907])
1908])
1909
1910
1911dnl  GMP_ASM_TEXT
1912dnl  ------------
1913dnl  .text - is usual.
1914dnl  .code - is needed by the hppa on HP-UX (but ia64 HP-UX uses .text)
1915dnl  .csect .text[PR] - is for AIX.
1916
1917AC_DEFUN([GMP_ASM_TEXT],
1918[AC_CACHE_CHECK([how to switch to text section],
1919                gmp_cv_asm_text,
1920[for i in ".text" ".code" [".csect .text[PR]"]; do
1921  echo "trying $i" >&AC_FD_CC
1922  GMP_TRY_ASSEMBLE([	$i],
1923    [gmp_cv_asm_text=$i
1924     rm -f conftest*
1925     break])
1926done
1927if test -z "$gmp_cv_asm_text"; then
1928  AC_MSG_ERROR([Cannot determine text section directive])
1929fi
1930])
1931echo ["define(<TEXT>, <$gmp_cv_asm_text>)"] >> $gmp_tmpconfigm4
1932])
1933
1934
1935dnl  GMP_ASM_DATA
1936dnl  ------------
1937dnl  Can we say `.data'?
1938
1939AC_DEFUN([GMP_ASM_DATA],
1940[AC_CACHE_CHECK([how to switch to data section],
1941                gmp_cv_asm_data,
1942[case $host in
1943  *-*-aix*) gmp_cv_asm_data=[".csect .data[RW]"] ;;
1944  *)        gmp_cv_asm_data=".data" ;;
1945esac
1946])
1947echo ["define(<DATA>, <$gmp_cv_asm_data>)"] >> $gmp_tmpconfigm4
1948])
1949
1950
1951dnl  GMP_ASM_RODATA
1952dnl  --------------
1953dnl  Find out how to switch to the read-only data section.
1954dnl
1955dnl  The compiler output is grepped for the right directive.  It's not
1956dnl  considered wise to just probe for ".section .rodata" or whatever works,
1957dnl  since arbitrary section names might be accepted, but not necessarily do
1958dnl  the right thing when they get to the linker.
1959dnl
1960dnl  Only a few asm files use RODATA, so this code is perhaps a bit
1961dnl  excessive right now, but should find more uses in the future.
1962dnl
1963dnl  FIXME: gcc on aix generates something like ".csect _foo.ro_c[RO],3"
1964dnl  where foo is the object file.  Might need to check for that if we use
1965dnl  RODATA there.
1966
1967AC_DEFUN([GMP_ASM_RODATA],
1968[AC_REQUIRE([GMP_ASM_TEXT])
1969AC_REQUIRE([GMP_ASM_DATA])
1970AC_REQUIRE([GMP_ASM_LABEL_SUFFIX])
1971AC_REQUIRE([GMP_ASM_UNDERSCORE])
1972AC_CACHE_CHECK([how to switch to read-only data section],
1973               gmp_cv_asm_rodata,
1974[
1975dnl Default to DATA on CPUs with split code/data caching, and TEXT
1976dnl elsewhere.  i386 means generic x86, so use DATA on it.
1977case $host in
1978X86_PATTERN | x86_64-*-*)
1979  gmp_cv_asm_rodata="$gmp_cv_asm_data" ;;
1980*)
1981  gmp_cv_asm_rodata="$gmp_cv_asm_text" ;;
1982esac
1983
1984cat >conftest.c <<EOF
1985extern const int foo[[]];		/* Suppresses C++'s suppression of foo */
1986const int foo[[]] = {1,2,3};
1987EOF
1988echo "Test program:" >&AC_FD_CC
1989cat conftest.c >&AC_FD_CC
1990gmp_compile="$CC $CFLAGS $CPPFLAGS -S conftest.c >&AC_FD_CC"
1991if AC_TRY_EVAL(gmp_compile); then
1992  echo "Compiler output:" >&AC_FD_CC
1993  cat conftest.s >&AC_FD_CC
1994  if test $gmp_cv_asm_underscore = yes; then
1995    tmp_gsym_prefix=_
1996  else
1997    tmp_gsym_prefix=
1998  fi
1999  # must see our label
2000  if grep "^${tmp_gsym_prefix}foo$gmp_cv_asm_label_suffix" conftest.s >/dev/null 2>&AC_FD_CC; then
2001    # take the last directive before our label (hence skipping segments
2002    # getting debugging info etc)
2003    tmp_match=`sed -n ["/^${tmp_gsym_prefix}foo$gmp_cv_asm_label_suffix/q
2004                        /^[. 	]*data/p
2005                        /^[. 	]*rdata/p
2006                        /^[. 	]*text/p
2007                        /^[. 	]*section/p
2008                        /^[. 	]*csect/p
2009                        /^[. 	]*CSECT/p"] conftest.s | sed -n '$p'`
2010    echo "Match: $tmp_match" >&AC_FD_CC
2011    if test -n "$tmp_match"; then
2012      gmp_cv_asm_rodata=$tmp_match
2013    fi
2014  else
2015    echo "Couldn't find label: ^${tmp_gsym_prefix}foo$gmp_cv_asm_label_suffix" >&AC_FD_CC
2016  fi
2017fi
2018rm -f conftest*
2019])
2020echo ["define(<RODATA>, <$gmp_cv_asm_rodata>)"] >> $gmp_tmpconfigm4
2021])
2022
2023
2024dnl  GMP_ASM_GLOBL
2025dnl  -------------
2026dnl  The assembler directive to mark a label as a global symbol.
2027dnl
2028dnl  ia64 - .global is standard, according to the Intel documentation.
2029dnl
2030dnl  hppa - ".export foo,entry" is demanded by HP hppa "as".  ".global" is a
2031dnl      kind of import.
2032dnl
2033dnl  other - .globl is usual.
2034dnl
2035dnl  "gas" tends to accept .globl everywhere, in addition to .export or
2036dnl  .global or whatever the system assembler demands.
2037
2038AC_DEFUN([GMP_ASM_GLOBL],
2039[AC_REQUIRE([GMP_ASM_TEXT])
2040AC_CACHE_CHECK([for assembler global directive],
2041                gmp_cv_asm_globl,
2042[case $host in
2043  hppa*-*-*)     gmp_cv_asm_globl=.export ;;
2044  IA64_PATTERN)  gmp_cv_asm_globl=.global ;;
2045  *)             gmp_cv_asm_globl=.globl  ;;
2046esac
2047])
2048echo ["define(<GLOBL>, <$gmp_cv_asm_globl>)"] >> $gmp_tmpconfigm4
2049])
2050
2051
2052dnl  GMP_ASM_GLOBL_ATTR
2053dnl  ------------------
2054dnl  Do we need something after `GLOBL symbol'?
2055
2056AC_DEFUN([GMP_ASM_GLOBL_ATTR],
2057[AC_REQUIRE([GMP_ASM_GLOBL])
2058AC_CACHE_CHECK([for assembler global directive attribute],
2059                gmp_cv_asm_globl_attr,
2060[case $gmp_cv_asm_globl in
2061  .export) gmp_cv_asm_globl_attr=",entry" ;;
2062  *)       gmp_cv_asm_globl_attr="" ;;
2063esac
2064])
2065echo ["define(<GLOBL_ATTR>, <$gmp_cv_asm_globl_attr>)"] >> $gmp_tmpconfigm4
2066])
2067
2068
2069dnl  GMP_ASM_TYPE
2070dnl  ------------
2071dnl  Can we say ".type", and how?
2072dnl
2073dnl  For i386 GNU/Linux ELF systems, and very likely other ELF systems,
2074dnl  .type and .size are important on functions in shared libraries.  If
2075dnl  .type is omitted and the mainline program references that function then
2076dnl  the code will be copied down to the mainline at load time like a piece
2077dnl  of data.  If .size is wrong or missing (it defaults to 4 bytes or some
2078dnl  such) then incorrect bytes will be copied and a segv is the most likely
2079dnl  result.  In any case such copying is not what's wanted, a .type
2080dnl  directive will ensure a PLT entry is used.
2081dnl
2082dnl  In GMP the assembler functions are normally only used from within the
2083dnl  library (since most programs are not interested in the low level
2084dnl  routines), and in those circumstances a missing .type isn't fatal,
2085dnl  letting the problem go unnoticed.  tests/mpn/t-asmtype.c aims to check
2086dnl  for it.
2087
2088AC_DEFUN([GMP_ASM_TYPE],
2089[AC_CACHE_CHECK([for assembler .type directive],
2090                gmp_cv_asm_type,
2091[gmp_cv_asm_type=
2092for gmp_tmp_prefix in @ \# %; do
2093  GMP_TRY_ASSEMBLE([	.type	sym,${gmp_tmp_prefix}function],
2094    [if grep "\.type pseudo-op used outside of \.def/\.endef ignored" conftest.out >/dev/null; then : ;
2095    else
2096      gmp_cv_asm_type=".type	\$][1,${gmp_tmp_prefix}\$][2"
2097      break
2098    fi])
2099done
2100rm -f conftest*
2101])
2102echo ["define(<TYPE>, <$gmp_cv_asm_type>)"] >> $gmp_tmpconfigm4
2103])
2104
2105
2106dnl  GMP_ASM_SIZE
2107dnl  ------------
2108dnl  Can we say `.size'?
2109
2110AC_DEFUN([GMP_ASM_SIZE],
2111[AC_CACHE_CHECK([for assembler .size directive],
2112                gmp_cv_asm_size,
2113[gmp_cv_asm_size=
2114GMP_TRY_ASSEMBLE([	.size	sym,1],
2115  [if grep "\.size pseudo-op used outside of \.def/\.endef ignored" conftest.out >/dev/null; then : ;
2116  else
2117    gmp_cv_asm_size=".size	\$][1,\$][2"
2118  fi])
2119])
2120echo ["define(<SIZE>, <$gmp_cv_asm_size>)"] >> $gmp_tmpconfigm4
2121])
2122
2123
2124dnl  GMP_ASM_COFF_TYPE
2125dnl  -----------------
2126dnl  Determine whether the assembler supports COFF type information.
2127dnl
2128dnl  Currently this is only needed for mingw (and cygwin perhaps) and so is
2129dnl  run only on the x86s, but it ought to work anywhere.
2130dnl
2131dnl  On MINGW, recent versions of the linker have an automatic import scheme
2132dnl  for data in a DLL which is referenced by a mainline but without
2133dnl  __declspec (__dllimport__) on the prototype.  It seems functions
2134dnl  without type information are treated as data, or something, and calls
2135dnl  to them from the mainline will crash.  gcc puts type information on the
2136dnl  C functions it generates, we need to do the same for assembler
2137dnl  functions.
2138dnl
2139dnl  This applies only to functions without __declspec(__dllimport__),
2140dnl  ie. without __GMP_DECLSPEC in the case of libgmp, so it also works just
2141dnl  to ensure all assembler functions used from outside libgmp have
2142dnl  __GMP_DECLSPEC on their prototypes.  But this isn't an ideal situation,
2143dnl  since we don't want perfectly valid calls going wrong just because
2144dnl  there wasn't a prototype in scope.
2145dnl
2146dnl  When an auto-import takes place, the following warning is given by the
2147dnl  linker.  This shouldn't be seen for any functions.
2148dnl
2149dnl      Info: resolving _foo by linking to __imp__foo (auto-import)
2150dnl
2151dnl
2152dnl  COFF type directives look like the following
2153dnl
2154dnl      .def    _foo
2155dnl      .scl    2
2156dnl      .type   32
2157dnl      .endef
2158dnl
2159dnl  _foo is the symbol with GSYM_PREFIX (_).  .scl is the storage class, 2
2160dnl  for external, 3 for static.  .type is the object type, 32 for a
2161dnl  function.
2162dnl
2163dnl  On an ELF system, this is (correctly) rejected due to .def, .endef and
2164dnl  .scl being invalid, and .type not having enough arguments.
2165
2166AC_DEFUN([GMP_ASM_COFF_TYPE],
2167[AC_REQUIRE([GMP_ASM_TEXT])
2168AC_REQUIRE([GMP_ASM_GLOBL])
2169AC_REQUIRE([GMP_ASM_GLOBL_ATTR])
2170AC_REQUIRE([GMP_ASM_LABEL_SUFFIX])
2171AC_REQUIRE([GMP_ASM_UNDERSCORE])
2172AC_CACHE_CHECK([for assembler COFF type directives],
2173		gmp_cv_asm_x86_coff_type,
2174[GMP_TRY_ASSEMBLE(
2175[	$gmp_cv_asm_text
2176	$gmp_cv_asm_globl ${tmp_gsym_prefix}foo$gmp_cv_asm_globl_attr
2177	.def	${tmp_gsym_prefix}foo
2178	.scl	2
2179	.type	32
2180	.endef
2181${tmp_gsym_prefix}foo$gmp_cv_asm_label_suffix
2182],
2183  [gmp_cv_asm_x86_coff_type=yes],
2184  [gmp_cv_asm_x86_coff_type=no])
2185])
2186echo ["define(<HAVE_COFF_TYPE>, <$gmp_cv_asm_x86_coff_type>)"] >> $gmp_tmpconfigm4
2187])
2188
2189
2190dnl  GMP_ASM_LSYM_PREFIX
2191dnl  -------------------
2192dnl  What is the prefix for a local label?
2193dnl
2194dnl  The prefixes tested are,
2195dnl
2196dnl      L  - usual for underscore systems
2197dnl      .L - usual for non-underscore systems
2198dnl      $  - alpha (gas and OSF system assembler)
2199dnl      L$ - hppa (gas and HP-UX system assembler)
2200dnl
2201dnl  The default is "L" if the tests fail for any reason.  There's a good
2202dnl  chance this will be adequate, since on most systems labels are local
2203dnl  anyway unless given a ".globl", and an "L" will avoid clashes with
2204dnl  other identifers.
2205dnl
2206dnl  For gas, ".L" is normally purely local to the assembler, it doesn't get
2207dnl  put into the object file at all.  This style is preferred, to keep the
2208dnl  object files nice and clean.
2209dnl
2210dnl  BSD format nm produces a line like
2211dnl
2212dnl      00000000 t Lgurkmacka
2213dnl
2214dnl  The symbol code is normally "t" for text, but any lower case letter
2215dnl  indicates a local definition.
2216dnl
2217dnl  Code "n" is for a debugging symbol, OSF "nm -B" gives that as an upper
2218dnl  case "N" for a local.
2219dnl
2220dnl  HP-UX nm prints an error message (though seems to give a 0 exit) if
2221dnl  there's no symbols at all in an object file, hence the use of "dummy".
2222
2223AC_DEFUN([GMP_ASM_LSYM_PREFIX],
2224[AC_REQUIRE([GMP_ASM_LABEL_SUFFIX])
2225AC_REQUIRE([GMP_ASM_TEXT])
2226AC_REQUIRE([GMP_PROG_NM])
2227AC_CACHE_CHECK([for assembler local label prefix],
2228               gmp_cv_asm_lsym_prefix,
2229[gmp_tmp_pre_appears=yes
2230for gmp_tmp_pre in L .L $L $ L$; do
2231  echo "Trying $gmp_tmp_pre" >&AC_FD_CC
2232  GMP_TRY_ASSEMBLE(
2233[	$gmp_cv_asm_text
2234dummy${gmp_cv_asm_label_suffix}
2235${gmp_tmp_pre}gurkmacka${gmp_cv_asm_label_suffix}],
2236  [if $NM conftest.$OBJEXT >conftest.nm 2>&AC_FD_CC; then : ; else
2237    cat conftest.nm >&AC_FD_CC
2238    AC_MSG_WARN(["$NM" failure])
2239    break
2240  fi
2241  cat conftest.nm >&AC_FD_CC
2242  if grep gurkmacka conftest.nm >/dev/null; then : ; else
2243    # no mention of the symbol, this is good
2244    echo "$gmp_tmp_pre label doesn't appear in object file at all (good)" >&AC_FD_CC
2245    gmp_cv_asm_lsym_prefix="$gmp_tmp_pre"
2246    gmp_tmp_pre_appears=no
2247    break
2248  fi
2249  if grep [' [a-zN] .*gurkmacka'] conftest.nm >/dev/null; then
2250    # symbol mentioned as a local, use this if nothing better
2251    echo "$gmp_tmp_pre label is local but still in object file" >&AC_FD_CC
2252    if test -z "$gmp_cv_asm_lsym_prefix"; then
2253      gmp_cv_asm_lsym_prefix="$gmp_tmp_pre"
2254    fi
2255  else
2256    echo "$gmp_tmp_pre label is something unknown" >&AC_FD_CC
2257  fi
2258  ])
2259done
2260rm -f conftest*
2261if test -z "$gmp_cv_asm_lsym_prefix"; then
2262  gmp_cv_asm_lsym_prefix=L
2263  AC_MSG_WARN([cannot determine local label, using default $gmp_cv_asm_lsym_prefix])
2264fi
2265# for development purposes, note whether we got a purely temporary local label
2266echo "Local label appears in object files: $gmp_tmp_pre_appears" >&AC_FD_CC
2267])
2268echo ["define(<LSYM_PREFIX>, <${gmp_cv_asm_lsym_prefix}>)"] >> $gmp_tmpconfigm4
2269AC_DEFINE_UNQUOTED(LSYM_PREFIX, "$gmp_cv_asm_lsym_prefix",
2270                   [Assembler local label prefix])
2271])
2272
2273
2274dnl  GMP_ASM_W32
2275dnl  -----------
2276dnl  How to define a 32-bit word.
2277dnl
2278dnl  FIXME: This test is not right for ia64-*-hpux*.  The directive should
2279dnl  be "data4", but the W32 macro is not currently used by the mpn/ia64 asm
2280dnl  files.
2281
2282AC_DEFUN([GMP_ASM_W32],
2283[AC_REQUIRE([GMP_ASM_DATA])
2284AC_REQUIRE([GMP_ASM_BYTE])
2285AC_REQUIRE([GMP_ASM_GLOBL])
2286AC_REQUIRE([GMP_ASM_LABEL_SUFFIX])
2287AC_REQUIRE([GMP_PROG_NM])
2288AC_CACHE_CHECK([how to define a 32-bit word],
2289	       gmp_cv_asm_w32,
2290[case $host in
2291  *-*-hpux*)
2292    # FIXME: HPUX puts first symbol at 0x40000000, breaking our assumption
2293    # that it's at 0x0.  We'll have to declare another symbol before the
2294    # .long/.word and look at the distance between the two symbols.  The
2295    # only problem is that the sed expression(s) barfs (on Solaris, for
2296    # example) for the symbol with value 0.  For now, HPUX uses .word.
2297    gmp_cv_asm_w32=".word"
2298    ;;
2299  *-*-*)
2300    gmp_tmp_val=
2301    for gmp_tmp_op in .long .word data4; do
2302      GMP_TRY_ASSEMBLE(
2303[	$gmp_cv_asm_data
2304	$gmp_cv_asm_globl	foo
2305	$gmp_tmp_op	0
2306foo$gmp_cv_asm_label_suffix
2307	$gmp_cv_asm_byte	0],
2308        [gmp_tmp_val=[`$NM conftest.$OBJEXT | grep foo | \
2309          sed -e 's;[[][0-9][]]\(.*\);\1;' -e 's;[^1-9]*\([0-9]*\).*;\1;'`]
2310        if test "$gmp_tmp_val" = 4; then
2311          gmp_cv_asm_w32="$gmp_tmp_op"
2312          break
2313        fi])
2314    done
2315    rm -f conftest*
2316    ;;
2317esac
2318if test -z "$gmp_cv_asm_w32"; then
2319  AC_MSG_ERROR([cannot determine how to define a 32-bit word])
2320fi
2321])
2322echo ["define(<W32>, <$gmp_cv_asm_w32>)"] >> $gmp_tmpconfigm4
2323])
2324
2325
2326dnl  GMP_X86_ASM_GOT_UNDERSCORE
2327dnl  --------------------------
2328dnl  Determine whether i386 _GLOBAL_OFFSET_TABLE_ needs an additional
2329dnl  underscore prefix.
2330dnl
2331dnl    SVR4      - the standard is _GLOBAL_OFFSET_TABLE_
2332dnl    GNU/Linux - follows SVR4
2333dnl    OpenBSD   - an a.out underscore system, uses __GLOBAL_OFFSET_TABLE_
2334dnl    NetBSD    - also an a.out underscore system, but _GLOBAL_OFFSET_TABLE_
2335dnl
2336dnl  The test attempts to link a program using _GLOBAL_OFFSET_TABLE_ or
2337dnl  __GLOBAL_OFFSET_TABLE_ to see which works.
2338dnl
2339dnl  $lt_prog_compiler_pic is included in the compile because old versions
2340dnl  of gas wouldn't accept PIC idioms without the right option (-K).  This
2341dnl  is the same as what libtool and mpn/Makeasm.am will do.
2342dnl
2343dnl  $lt_prog_compiler_pic is also included in the link because OpenBSD ld
2344dnl  won't accept an R_386_GOTPC relocation without the right options.  This
2345dnl  is not what's done by the Makefiles when building executables, but
2346dnl  let's hope it's ok (it works fine with gcc).
2347dnl
2348dnl  The fallback is no additional underscore, on the basis that this will
2349dnl  suit SVR4/ELF style systems, which should be much more common than
2350dnl  a.out systems with shared libraries.
2351dnl
2352dnl  Note that it's not an error for the tests to fail, since for instance
2353dnl  cygwin, mingw and djgpp don't have a _GLOBAL_OFFSET_TABLE_ scheme at
2354dnl  all.
2355dnl
2356dnl  Perhaps $CCAS could be asked to do the linking as well as the
2357dnl  assembling, but in the Makefiles it's only used for assembling, so lets
2358dnl  keep it that way.
2359dnl
2360dnl  The test here is run even under --disable-shared, so that PIC objects
2361dnl  can be built and tested by the tune/many.pl development scheme.  The
2362dnl  tests will be reasonably quick and won't give a fatal error, so this
2363dnl  arrangement is ok.  AC_LIBTOOL_PROG_COMPILER_PIC does its
2364dnl  $lt_prog_compiler_pic setups even for --disable-shared too.
2365
2366AC_DEFUN([GMP_ASM_X86_GOT_UNDERSCORE],
2367[AC_REQUIRE([GMP_ASM_TEXT])
2368AC_REQUIRE([GMP_ASM_GLOBL])
2369AC_REQUIRE([GMP_ASM_GLOBL_ATTR])
2370AC_REQUIRE([GMP_ASM_LABEL_SUFFIX])
2371AC_REQUIRE([GMP_ASM_UNDERSCORE])
2372AC_REQUIRE([AC_LIBTOOL_PROG_COMPILER_PIC])
2373AC_CACHE_CHECK([if _GLOBAL_OFFSET_TABLE_ is prefixed by underscore],
2374               gmp_cv_asm_x86_got_underscore,
2375[gmp_cv_asm_x86_got_underscore="not applicable"
2376if test $gmp_cv_asm_underscore = yes; then
2377  tmp_gsym_prefix=_
2378else
2379  tmp_gsym_prefix=
2380fi
2381for tmp_underscore in "" "_"; do
2382  cat >conftest.s <<EOF
2383	$gmp_cv_asm_text
2384	$gmp_cv_asm_globl ${tmp_gsym_prefix}main$gmp_cv_asm_globl_attr
2385${tmp_gsym_prefix}main$gmp_cv_asm_label_suffix
2386	addl	$ ${tmp_underscore}_GLOBAL_OFFSET_TABLE_, %ebx
2387EOF
2388  gmp_compile="$CCAS $CFLAGS $CPPFLAGS $lt_prog_compiler_pic conftest.s >&AC_FD_CC && $CC $CFLAGS $CPPFLAGS $lt_prog_compiler_pic conftest.$OBJEXT >&AC_FD_CC"
2389  if AC_TRY_EVAL(gmp_compile); then
2390    if test "$tmp_underscore" = "_"; then
2391      gmp_cv_asm_x86_got_underscore=yes
2392    else
2393      gmp_cv_asm_x86_got_underscore=no
2394    fi
2395    break
2396  fi
2397done
2398rm -f conftest* a.out b.out a.exe a_out.exe
2399])
2400if test "$gmp_cv_asm_x86_got_underscore" = "yes"; then
2401  GMP_DEFINE(GOT_GSYM_PREFIX, [_])
2402else
2403  GMP_DEFINE(GOT_GSYM_PREFIX, [])
2404fi
2405])
2406
2407
2408dnl  GMP_ASM_X86_GOT_EAX_OK(CC+CFLAGS, [ACTION-YES] [, ACTION-NO])
2409dnl  -------------------------------------------------------------
2410dnl  Determine whether _GLOBAL_OFFSET_TABLE_ used with %eax is ok.
2411dnl
2412dnl  An instruction
2413dnl
2414dnl          addl  $_GLOBAL_OFFSET_TABLE_, %eax
2415dnl
2416dnl  is incorrectly assembled by gas 2.12 (or thereabouts) and earlier.  It
2417dnl  puts an addend 2 into the R_386_GOTPC relocation, but it should be 1
2418dnl  for this %eax form being a 1 byte opcode (with other registers it's 2
2419dnl  opcode bytes).  See note about this in mpn/x86/README too.
2420dnl
2421dnl  We assemble this, surrounded by some unlikely byte sequences as
2422dnl  delimiters, and check for the bad output.
2423dnl
2424dnl  This is for use by compiler probing in GMP_PROG_CC_WORKS, so the result
2425dnl  is not cached.
2426dnl
2427dnl  This test is not specific to gas, but old gas is the only assembler we
2428dnl  know of with this problem.  The Solaris has been seen coming out ok.
2429dnl
2430dnl  ".text" is hard coded because this macro is wanted before GMP_ASM_TEXT.
2431dnl  This should be fine, ".text" is normal on x86 systems, and certainly
2432dnl  will be fine with the offending gas.
2433dnl
2434dnl  If an error occurs when assembling, we consider the assembler ok, since
2435dnl  the bad output does not occur.  This happens for instance on mingw,
2436dnl  where _GLOBAL_OFFSET_TABLE_ results in a bfd error, since there's no
2437dnl  GOT etc in PE object files.
2438dnl
2439dnl  This test is used before the object file extension has been determined,
2440dnl  so we force output to conftest.o.  Using -o with -c is not portable,
2441dnl  but we think all x86 compilers will accept -o with -c, certainly gcc
2442dnl  does.
2443dnl
2444dnl  -fPIC is hard coded here, because this test is for use before libtool
2445dnl  has established the pic options.  It's right for gcc, but perhaps not
2446dnl  other compilers.
2447
2448AC_DEFUN([GMP_ASM_X86_GOT_EAX_OK],
2449[echo "Testing gas GOT with eax good" >&AC_FD_CC
2450cat >conftest.awk <<\EOF
2451[BEGIN {
2452  want[0]  = "001"
2453  want[1]  = "043"
2454  want[2]  = "105"
2455  want[3]  = "147"
2456  want[4]  = "211"
2457  want[5]  = "253"
2458  want[6]  = "315"
2459  want[7]  = "357"
2460
2461  want[8]  = "005"
2462  want[9]  = "002"
2463  want[10] = "000"
2464  want[11] = "000"
2465  want[12] = "000"
2466
2467  want[13] = "376"
2468  want[14] = "334"
2469  want[15] = "272"
2470  want[16] = "230"
2471  want[17] = "166"
2472  want[18] = "124"
2473  want[19] = "062"
2474  want[20] = "020"
2475
2476  result = "yes"
2477}
2478{
2479  for (f = 2; f <= NF; f++)
2480    {
2481      for (i = 0; i < 20; i++)
2482        got[i] = got[i+1];
2483      got[20] = $f;
2484
2485      found = 1
2486      for (i = 0; i < 21; i++)
2487        if (got[i] != want[i])
2488          {
2489            found = 0
2490            break
2491          }
2492      if (found)
2493        {
2494          result = "no"
2495          exit
2496        }
2497    }
2498}
2499END {
2500  print result
2501}
2502]EOF
2503cat >conftest.s <<\EOF
2504[	.text
2505	.byte	1, 35, 69, 103, 137, 171, 205, 239
2506	addl	$_GLOBAL_OFFSET_TABLE_, %eax
2507	.byte	254, 220, 186, 152, 118, 84, 50, 16
2508]EOF
2509tmp_got_good=yes
2510gmp_compile="$1 -fPIC -o conftest.o -c conftest.s >&AC_FD_CC 2>&1"
2511if AC_TRY_EVAL(gmp_compile); then
2512  tmp_got_good=`od -b conftest.o | $AWK -f conftest.awk`
2513fi
2514rm -f conftest.*
2515echo "Result: $tmp_got_good" >&AC_FD_CC
2516if test "$tmp_got_good" = no; then
2517  ifelse([$3],,:,[$3])
2518else
2519  ifelse([$2],,:,[$2])
2520fi
2521])
2522
2523
2524dnl  GMP_ASM_X86_MMX([ACTION-IF-YES][,ACTION-IF-NO])
2525dnl  -----------------------------------------------
2526dnl  Determine whether the assembler supports MMX instructions.
2527dnl
2528dnl  This macro is wanted before GMP_ASM_TEXT, so ".text" is hard coded
2529dnl  here.  ".text" is believed to be correct on all x86 systems.  Actually
2530dnl  ".text" probably isn't needed at all, at least for just checking
2531dnl  instruction syntax.
2532dnl
2533dnl  "movq %mm0, %mm1" should assemble to "0f 6f c8", but Solaris 2.6 and
2534dnl  2.7 wrongly assemble it to "0f 6f c1" (that being the reverse "movq
2535dnl  %mm1, %mm0").  It seems more trouble than it's worth to work around
2536dnl  this in the code, so just detect and reject.
2537
2538AC_DEFUN([GMP_ASM_X86_MMX],
2539[AC_CACHE_CHECK([if the assembler knows about MMX instructions],
2540		gmp_cv_asm_x86_mmx,
2541[GMP_TRY_ASSEMBLE(
2542[	.text
2543	movq	%mm0, %mm1],
2544[gmp_cv_asm_x86_mmx=yes
2545case $host in
2546*-*-solaris*)
2547  if (dis conftest.$OBJEXT >conftest.out) 2>/dev/null; then
2548    if grep "0f 6f c1" conftest.out >/dev/null; then
2549      gmp_cv_asm_x86_mmx=movq-bug
2550    fi
2551  else
2552    AC_MSG_WARN(["dis" not available to check for "as" movq bug])
2553  fi
2554esac],
2555[gmp_cv_asm_x86_mmx=no])])
2556
2557case $gmp_cv_asm_x86_mmx in
2558movq-bug)
2559  AC_MSG_WARN([+----------------------------------------------------------])
2560  AC_MSG_WARN([| WARNING WARNING WARNING])
2561  AC_MSG_WARN([| Host CPU has MMX code, but the assembler])
2562  AC_MSG_WARN([|     $CCAS $CFLAGS $CPPFLAGS])
2563  AC_MSG_WARN([| has the Solaris 2.6 and 2.7 bug where register to register])
2564  AC_MSG_WARN([| movq operands are reversed.])
2565  AC_MSG_WARN([| Non-MMX replacements will be used.])
2566  AC_MSG_WARN([| This will be an inferior build.])
2567  AC_MSG_WARN([+----------------------------------------------------------])
2568  ;;
2569no)
2570  AC_MSG_WARN([+----------------------------------------------------------])
2571  AC_MSG_WARN([| WARNING WARNING WARNING])
2572  AC_MSG_WARN([| Host CPU has MMX code, but it can't be assembled by])
2573  AC_MSG_WARN([|     $CCAS $CFLAGS $CPPFLAGS])
2574  AC_MSG_WARN([| Non-MMX replacements will be used.])
2575  AC_MSG_WARN([| This will be an inferior build.])
2576  AC_MSG_WARN([+----------------------------------------------------------])
2577  ;;
2578esac
2579if test "$gmp_cv_asm_x86_mmx" = yes; then
2580  ifelse([$1],,:,[$1])
2581else
2582  ifelse([$2],,:,[$2])
2583fi
2584])
2585
2586
2587dnl  GMP_ASM_X86_SHLDL_CL
2588dnl  --------------------
2589
2590AC_DEFUN([GMP_ASM_X86_SHLDL_CL],
2591[AC_REQUIRE([GMP_ASM_TEXT])
2592AC_CACHE_CHECK([if the assembler takes cl with shldl],
2593		gmp_cv_asm_x86_shldl_cl,
2594[GMP_TRY_ASSEMBLE(
2595[	$gmp_cv_asm_text
2596	shldl	%cl, %eax, %ebx],
2597  gmp_cv_asm_x86_shldl_cl=yes,
2598  gmp_cv_asm_x86_shldl_cl=no)
2599])
2600if test "$gmp_cv_asm_x86_shldl_cl" = "yes"; then
2601  GMP_DEFINE(WANT_SHLDL_CL,1)
2602else
2603  GMP_DEFINE(WANT_SHLDL_CL,0)
2604fi
2605])
2606
2607
2608dnl  GMP_ASM_X86_SSE2([ACTION-IF-YES][,ACTION-IF-NO])
2609dnl  ------------------------------------------------
2610dnl  Determine whether the assembler supports SSE2 instructions.
2611dnl
2612dnl  This macro is wanted before GMP_ASM_TEXT, so ".text" is hard coded
2613dnl  here.  ".text" is believed to be correct on all x86 systems, certainly
2614dnl  it's all GMP_ASM_TEXT gives currently.  Actually ".text" probably isn't
2615dnl  needed at all, at least for just checking instruction syntax.
2616
2617AC_DEFUN([GMP_ASM_X86_SSE2],
2618[AC_CACHE_CHECK([if the assembler knows about SSE2 instructions],
2619		gmp_cv_asm_x86_sse2,
2620[GMP_TRY_ASSEMBLE(
2621[	.text
2622	paddq	%mm0, %mm1],
2623  [gmp_cv_asm_x86_sse2=yes],
2624  [gmp_cv_asm_x86_sse2=no])
2625])
2626case $gmp_cv_asm_x86_sse2 in
2627yes)
2628  ifelse([$1],,:,[$1])
2629  ;;
2630*)
2631  AC_MSG_WARN([+----------------------------------------------------------])
2632  AC_MSG_WARN([| WARNING WARNING WARNING])
2633  AC_MSG_WARN([| Host CPU has SSE2 code, but it can't be assembled by])
2634  AC_MSG_WARN([|     $CCAS $CFLAGS $CPPFLAGS])
2635  AC_MSG_WARN([| Non-SSE2 replacements will be used.])
2636  AC_MSG_WARN([| This will be an inferior build.])
2637  AC_MSG_WARN([+----------------------------------------------------------])
2638  ifelse([$2],,:,[$2])
2639  ;;
2640esac
2641])
2642
2643
2644dnl  GMP_ASM_X86_MULX([ACTION-IF-YES][,ACTION-IF-NO])
2645dnl  ------------------------------------------------
2646dnl  Determine whether the assembler supports the mulx instruction which debut
2647dnl  with Haswell.
2648dnl
2649dnl  This macro is wanted before GMP_ASM_TEXT, so ".text" is hard coded
2650dnl  here.  ".text" is believed to be correct on all x86 systems, certainly
2651dnl  it's all GMP_ASM_TEXT gives currently.  Actually ".text" probably isn't
2652dnl  needed at all, at least for just checking instruction syntax.
2653
2654AC_DEFUN([GMP_ASM_X86_MULX],
2655[AC_CACHE_CHECK([if the assembler knows about the mulx instruction],
2656		gmp_cv_asm_x86_mulx,
2657[GMP_TRY_ASSEMBLE(
2658[	.text
2659	mulx	%r8, %r9, %r10],
2660  [gmp_cv_asm_x86_mulx=yes],
2661  [gmp_cv_asm_x86_mulx=no])
2662])
2663case $gmp_cv_asm_x86_mulx in
2664yes)
2665  AC_DEFINE(X86_ASM_MULX, 1,
2666  [Define to 1 if the assembler understands the mulx instruction])
2667  ifelse([$1],,:,[$1])
2668  ;;
2669*)
2670  AC_MSG_WARN([+----------------------------------------------------------])
2671  AC_MSG_WARN([| WARNING WARNING WARNING])
2672  AC_MSG_WARN([| Host CPU has the mulx instruction, but it can't be])
2673  AC_MSG_WARN([| assembled by])
2674  AC_MSG_WARN([|     $CCAS $CFLAGS $CPPFLAGS])
2675  AC_MSG_WARN([| Older x86 instructions will be used.])
2676  AC_MSG_WARN([| This will be an inferior build.])
2677  AC_MSG_WARN([+----------------------------------------------------------])
2678  ifelse([$2],,:,[$2])
2679  ;;
2680esac
2681])
2682
2683
2684dnl  GMP_ASM_X86_MCOUNT
2685dnl  ------------------
2686dnl  Find out how to call mcount for profiling on an x86 system.
2687dnl
2688dnl  A dummy function is compiled and the ".s" output examined.  The pattern
2689dnl  matching might be a bit fragile, but should work at least with gcc on
2690dnl  sensible systems.  Certainly it's better than hard coding a table of
2691dnl  conventions.
2692dnl
2693dnl  For non-PIC, any ".data" is taken to mean a counter might be passed.
2694dnl  It's assumed a movl will set it up, and the right register is taken
2695dnl  from that movl.  Any movl involving %esp is ignored (a frame pointer
2696dnl  setup normally).
2697dnl
2698dnl  For PIC, any ".data" is similarly interpreted, but a GOTOFF identifies
2699dnl  the line setting up the right register.
2700dnl
2701dnl  In both cases a line with "mcount" identifies the call and that line is
2702dnl  used literally.
2703dnl
2704dnl  On some systems (eg. FreeBSD 3.5) gcc emits ".data" but doesn't use it,
2705dnl  so it's not an error to have .data but then not find a register.
2706dnl
2707dnl  Variations in mcount conventions on different x86 systems can be found
2708dnl  in gcc config/i386.  mcount can have a "_" prefix or be .mcount or
2709dnl  _mcount_ptr, and for PIC it can be called through a GOT entry, or via
2710dnl  the PLT.  If a pointer to a counter is required it's passed in %eax or
2711dnl  %edx.
2712dnl
2713dnl  Flags to specify PIC are taken from $lt_prog_compiler_pic set by
2714dnl  AC_PROG_LIBTOOL.
2715dnl
2716dnl  Enhancement: Cache the values determined here. But what's the right way
2717dnl  to get two variables (mcount_nonpic_reg and mcount_nonpic_call say) set
2718dnl  from one block of commands?
2719
2720AC_DEFUN([GMP_ASM_X86_MCOUNT],
2721[AC_REQUIRE([AC_ENABLE_SHARED])
2722AC_REQUIRE([AC_PROG_LIBTOOL])
2723AC_MSG_CHECKING([how to call x86 mcount])
2724cat >conftest.c <<EOF
2725foo(){bar();}
2726EOF
2727
2728if test "$enable_static" = yes; then
2729  gmp_asmout_compile="$CC $CFLAGS $CPPFLAGS -S conftest.c 1>&AC_FD_CC"
2730  if AC_TRY_EVAL(gmp_asmout_compile); then
2731    if grep '\.data' conftest.s >/dev/null; then
2732      mcount_nonpic_reg=`sed -n ['/esp/!s/.*movl.*,\(%[a-z]*\).*$/\1/p'] conftest.s`
2733    else
2734      mcount_nonpic_reg=
2735    fi
2736    mcount_nonpic_call=`grep 'call.*mcount' conftest.s`
2737    if test -z "$mcount_nonpic_call"; then
2738      AC_MSG_ERROR([Cannot find mcount call for non-PIC])
2739    fi
2740  else
2741    AC_MSG_ERROR([Cannot compile test program for non-PIC])
2742  fi
2743fi
2744
2745if test "$enable_shared" = yes; then
2746  gmp_asmout_compile="$CC $CFLAGS $CPPFLAGS $lt_prog_compiler_pic -S conftest.c 1>&AC_FD_CC"
2747  if AC_TRY_EVAL(gmp_asmout_compile); then
2748    if grep '\.data' conftest.s >/dev/null; then
2749      case $lt_prog_compiler_pic in
2750        *-DDLL_EXPORT*)
2751          # Windows DLLs have non-PIC style mcount
2752          mcount_pic_reg=`sed -n ['/esp/!s/.*movl.*,\(%[a-z]*\).*$/\1/p'] conftest.s`
2753          ;;
2754        *)
2755          mcount_pic_reg=`sed -n ['s/.*GOTOFF.*,\(%[a-z]*\).*$/\1/p'] conftest.s`
2756          ;;
2757      esac
2758    else
2759      mcount_pic_reg=
2760    fi
2761    mcount_pic_call=`grep 'call.*mcount' conftest.s`
2762    if test -z "$mcount_pic_call"; then
2763      AC_MSG_ERROR([Cannot find mcount call for PIC])
2764    fi
2765  else
2766    AC_MSG_ERROR([Cannot compile test program for PIC])
2767  fi
2768fi
2769
2770GMP_DEFINE_RAW(["define(<MCOUNT_NONPIC_REG>, <\`$mcount_nonpic_reg'>)"])
2771GMP_DEFINE_RAW(["define(<MCOUNT_NONPIC_CALL>,<\`$mcount_nonpic_call'>)"])
2772GMP_DEFINE_RAW(["define(<MCOUNT_PIC_REG>,    <\`$mcount_pic_reg'>)"])
2773GMP_DEFINE_RAW(["define(<MCOUNT_PIC_CALL>,   <\`$mcount_pic_call'>)"])
2774
2775rm -f conftest.*
2776AC_MSG_RESULT([determined])
2777])
2778
2779
2780dnl  GMP_ASM_IA64_ALIGN_OK
2781dnl  ---------------------
2782dnl  Determine whether .align correctly pads with nop instructions in a text
2783dnl  segment.
2784dnl
2785dnl  gas 2.14 and earlier byte swaps its padding bundle on big endian
2786dnl  systems, which is incorrect (endianness only changes data).  What
2787dnl  should be "nop.m / nop.f / nop.i" comes out as "break" instructions.
2788dnl
2789dnl  The test here detects the bad case, and assumes anything else is ok
2790dnl  (there are many sensible nop bundles, so it'd be impractical to try to
2791dnl  match everything good).
2792
2793AC_DEFUN([GMP_ASM_IA64_ALIGN_OK],
2794[AC_CACHE_CHECK([whether assembler .align padding is good],
2795		gmp_cv_asm_ia64_align_ok,
2796[cat >conftest.awk <<\EOF
2797[BEGIN {
2798  want[0]  = "011"
2799  want[1]  = "160"
2800  want[2]  = "074"
2801  want[3]  = "040"
2802  want[4]  = "000"
2803  want[5]  = "040"
2804  want[6]  = "020"
2805  want[7]  = "221"
2806  want[8]  = "114"
2807  want[9]  = "000"
2808  want[10] = "100"
2809  want[11] = "200"
2810  want[12] = "122"
2811  want[13] = "261"
2812  want[14] = "000"
2813  want[15] = "200"
2814
2815  want[16] = "000"
2816  want[17] = "004"
2817  want[18] = "000"
2818  want[19] = "000"
2819  want[20] = "000"
2820  want[21] = "000"
2821  want[22] = "002"
2822  want[23] = "000"
2823  want[24] = "000"
2824  want[25] = "000"
2825  want[26] = "000"
2826  want[27] = "001"
2827  want[28] = "000"
2828  want[29] = "000"
2829  want[30] = "000"
2830  want[31] = "014"
2831
2832  want[32] = "011"
2833  want[33] = "270"
2834  want[34] = "140"
2835  want[35] = "062"
2836  want[36] = "000"
2837  want[37] = "040"
2838  want[38] = "240"
2839  want[39] = "331"
2840  want[40] = "160"
2841  want[41] = "000"
2842  want[42] = "100"
2843  want[43] = "240"
2844  want[44] = "343"
2845  want[45] = "371"
2846  want[46] = "000"
2847  want[47] = "200"
2848
2849  result = "yes"
2850}
2851{
2852  for (f = 2; f <= NF; f++)
2853    {
2854      for (i = 0; i < 47; i++)
2855        got[i] = got[i+1];
2856      got[47] = $f;
2857
2858      found = 1
2859      for (i = 0; i < 48; i++)
2860        if (got[i] != want[i])
2861          {
2862            found = 0
2863            break
2864          }
2865      if (found)
2866        {
2867          result = "no"
2868          exit
2869        }
2870    }
2871}
2872END {
2873  print result
2874}
2875]EOF
2876GMP_TRY_ASSEMBLE(
2877[	.text
2878	.align	32
2879{ .mmi;	add	r14 = r15, r16
2880	add	r17 = r18, r19
2881	add	r20 = r21, r22 ;; }
2882	.align	32
2883{ .mmi;	add	r23 = r24, r25
2884	add	r26 = r27, r28
2885	add	r29 = r30, r31 ;; }
2886],
2887  [gmp_cv_asm_ia64_align_ok=`od -b conftest.$OBJEXT | $AWK -f conftest.awk`],
2888  [AC_MSG_WARN([oops, cannot compile test program])
2889   gmp_cv_asm_ia64_align_ok=yes])
2890])
2891GMP_DEFINE_RAW(["define(<IA64_ALIGN_OK>, <\`$gmp_cv_asm_ia64_align_ok'>)"])
2892])
2893
2894
2895
2896
2897dnl  GMP_ASM_M68K_INSTRUCTION
2898dnl  ------------------------
2899dnl  Not sure if ".l" and "%" are independent settings, but it doesn't hurt
2900dnl  to try all four possibilities.  Note that the % ones must be first, so
2901dnl  "d0" won't be interpreted as a label.
2902dnl
2903dnl  gas 1.92.3 on NetBSD 1.4 needs to be tested with a two operand
2904dnl  instruction.  It takes registers without "%", but a single operand
2905dnl  "clrl %d0" only gives a warning, not an error.
2906
2907AC_DEFUN([GMP_ASM_M68K_INSTRUCTION],
2908[AC_REQUIRE([GMP_ASM_TEXT])
2909AC_CACHE_CHECK([assembler instruction and register style],
2910		gmp_cv_asm_m68k_instruction,
2911[for i in "addl %d0,%d1" "add.l %d0,%d1" "addl d0,d1" "add.l d0,d1"; do
2912  GMP_TRY_ASSEMBLE(
2913    [	$gmp_cv_asm_text
2914	$i],
2915    [gmp_cv_asm_m68k_instruction=$i
2916    rm -f conftest*
2917    break])
2918done
2919if test -z "$gmp_cv_asm_m68k_instruction"; then
2920  AC_MSG_ERROR([cannot determine assembler instruction and register style])
2921fi
2922])
2923case $gmp_cv_asm_m68k_instruction in
2924"addl d0,d1")    want_dot_size=no;  want_register_percent=no  ;;
2925"addl %d0,%d1")  want_dot_size=no;  want_register_percent=yes ;;
2926"add.l d0,d1")   want_dot_size=yes; want_register_percent=no  ;;
2927"add.l %d0,%d1") want_dot_size=yes; want_register_percent=yes ;;
2928*) AC_MSG_ERROR([oops, unrecognised instruction and register style]) ;;
2929esac
2930GMP_DEFINE_RAW(["define(<WANT_REGISTER_PERCENT>, <\`$want_register_percent'>)"])
2931GMP_DEFINE_RAW(["define(<WANT_DOT_SIZE>, <\`$want_dot_size'>)"])
2932])
2933
2934
2935dnl  GMP_ASM_M68K_ADDRESSING
2936dnl  -----------------------
2937
2938AC_DEFUN([GMP_ASM_M68K_ADDRESSING],
2939[AC_REQUIRE([GMP_ASM_TEXT])
2940AC_REQUIRE([GMP_ASM_M68K_INSTRUCTION])
2941AC_CACHE_CHECK([assembler addressing style],
2942		gmp_cv_asm_m68k_addressing,
2943[case $gmp_cv_asm_m68k_instruction in
2944addl*)  movel=movel ;;
2945add.l*) movel=move.l ;;
2946*) AC_MSG_ERROR([oops, unrecognised gmp_cv_asm_m68k_instruction]) ;;
2947esac
2948case $gmp_cv_asm_m68k_instruction in
2949*"%d0,%d1") dreg=%d0; areg=%a0 ;;
2950*"d0,d1")   dreg=d0;  areg=a0  ;;
2951*) AC_MSG_ERROR([oops, unrecognised gmp_cv_asm_m68k_instruction]) ;;
2952esac
2953GMP_TRY_ASSEMBLE(
2954[	$gmp_cv_asm_text
2955	$movel	$dreg, $areg@-],
2956  [gmp_cv_asm_m68k_addressing=mit],
2957[GMP_TRY_ASSEMBLE(
2958[	$gmp_cv_asm_text
2959	$movel	$dreg, -($areg)],
2960  [gmp_cv_asm_m68k_addressing=motorola],
2961[AC_MSG_ERROR([cannot determine assembler addressing style])])])
2962])
2963GMP_DEFINE_RAW(["define(<WANT_ADDRESSING>, <\`$gmp_cv_asm_m68k_addressing'>)"])
2964])
2965
2966
2967dnl  GMP_ASM_M68K_BRANCHES
2968dnl  ---------------------
2969dnl  "bra" is the standard branch instruction.  "jra" or "jbra" are
2970dnl  preferred where available, since on gas for instance they give a
2971dnl  displacement only as big as it needs to be, whereas "bra" is always
2972dnl  16-bits.  This applies to the conditional branches "bcc" etc too.
2973dnl  However "dbcc" etc on gas are already only as big as they need to be.
2974
2975AC_DEFUN([GMP_ASM_M68K_BRANCHES],
2976[AC_REQUIRE([GMP_ASM_TEXT])
2977AC_CACHE_CHECK([assembler shortest branches],
2978		gmp_cv_asm_m68k_branches,
2979[for i in jra jbra bra; do
2980  GMP_TRY_ASSEMBLE(
2981[	$gmp_cv_asm_text
2982foo$gmp_cv_asm_label_suffix
2983	$i	foo],
2984  [gmp_cv_asm_m68k_branches=$i
2985  rm -f conftest*
2986  break])
2987done
2988if test -z "$gmp_cv_asm_m68k_branches"; then
2989  AC_MSG_ERROR([cannot determine assembler branching style])
2990fi
2991])
2992GMP_DEFINE_RAW(["define(<WANT_BRANCHES>, <\`$gmp_cv_asm_m68k_branches'>)"])
2993])
2994
2995
2996dnl  GMP_ASM_POWERPC_PIC_ALWAYS
2997dnl  --------------------------
2998dnl  Determine whether PIC is the default compiler output.
2999dnl
3000dnl  SVR4 style "foo@ha" addressing is interpreted as non-PIC, and anything
3001dnl  else is assumed to require PIC always (Darwin or AIX).  SVR4 is the
3002dnl  only non-PIC addressing syntax the asm files have at the moment anyway.
3003dnl
3004dnl  Libtool does this by taking "*-*-aix* | *-*-darwin* | *-*-rhapsody*" to
3005dnl  mean PIC always, but it seems more reliable to grep the compiler
3006dnl  output.
3007dnl
3008dnl The next paragraph is untrue for Tiger.  Was it ever true?  For tiger,
3009dnl "cc -fast" makes non-PIC the default (and the binaries do run).
3010dnl  On Darwin "cc -static" is non-PIC with syntax "ha16(_foo)", but that's
3011dnl  apparently only for use in the kernel, which we're not attempting to
3012dnl  target at the moment, so don't look for that.
3013
3014AC_DEFUN([GMP_ASM_POWERPC_PIC_ALWAYS],
3015[AC_REQUIRE([AC_PROG_CC])
3016AC_CACHE_CHECK([whether compiler output is PIC by default],
3017               gmp_cv_asm_powerpc_pic,
3018[gmp_cv_asm_powerpc_pic=yes
3019cat >conftest.c <<EOF
3020int foo;
3021int *bar() { return &foo; }
3022EOF
3023echo "Test program:" >&AC_FD_CC
3024cat conftest.c >&AC_FD_CC
3025gmp_compile="$CC $CFLAGS $CPPFLAGS -S conftest.c >&AC_FD_CC"
3026if AC_TRY_EVAL(gmp_compile); then
3027  echo "Compiler output:" >&AC_FD_CC
3028  cat conftest.s >&AC_FD_CC
3029  if grep 'foo@ha' conftest.s >/dev/null 2>&AC_FD_CC; then
3030    gmp_cv_asm_powerpc_pic=no
3031  fi
3032  if grep 'ha16(_foo)' conftest.s >/dev/null 2>&AC_FD_CC; then
3033    gmp_cv_asm_powerpc_pic=no
3034  fi
3035fi
3036rm -f conftest*
3037])
3038GMP_DEFINE_RAW(["define(<PIC_ALWAYS>,<$gmp_cv_asm_powerpc_pic>)"])
3039])
3040
3041
3042dnl  GMP_ASM_POWERPC_R_REGISTERS
3043dnl  ---------------------------
3044dnl  Determine whether the assembler takes powerpc registers with an "r" as
3045dnl  in "r6", or as plain "6".  The latter is standard, but NeXT, Rhapsody,
3046dnl  and MacOS-X require the "r" forms.
3047dnl
3048dnl  See also mpn/powerpc32/powerpc-defs.m4 which uses the result of this
3049dnl  test.
3050
3051AC_DEFUN([GMP_ASM_POWERPC_R_REGISTERS],
3052[AC_REQUIRE([GMP_ASM_TEXT])
3053AC_CACHE_CHECK([if the assembler needs r on registers],
3054               gmp_cv_asm_powerpc_r_registers,
3055[GMP_TRY_ASSEMBLE(
3056[	$gmp_cv_asm_text
3057	mtctr	6],
3058[gmp_cv_asm_powerpc_r_registers=no],
3059[GMP_TRY_ASSEMBLE(
3060[	$gmp_cv_asm_text
3061	mtctr	r6],
3062[gmp_cv_asm_powerpc_r_registers=yes],
3063[AC_MSG_ERROR([neither "mtctr 6" nor "mtctr r6" works])])])])
3064
3065GMP_DEFINE_RAW(["define(<WANT_R_REGISTERS>,<$gmp_cv_asm_powerpc_r_registers>)"])
3066])
3067
3068
3069dnl  GMP_ASM_SPARC_REGISTER
3070dnl  ----------------------
3071dnl  Determine whether the assembler accepts the ".register" directive.
3072dnl  Old versions of solaris "as" don't.
3073dnl
3074dnl  See also mpn/sparc32/sparc-defs.m4 which uses the result of this test.
3075
3076AC_DEFUN([GMP_ASM_SPARC_REGISTER],
3077[AC_REQUIRE([GMP_ASM_TEXT])
3078AC_CACHE_CHECK([if the assembler accepts ".register"],
3079               gmp_cv_asm_sparc_register,
3080[GMP_TRY_ASSEMBLE(
3081[	$gmp_cv_asm_text
3082	.register	%g2,#scratch
3083],
3084[gmp_cv_asm_sparc_register=yes],
3085[gmp_cv_asm_sparc_register=no])])
3086
3087GMP_DEFINE_RAW(["define(<HAVE_REGISTER>,<$gmp_cv_asm_sparc_register>)"])
3088])
3089
3090
3091dnl  GMP_ASM_SPARC_GOTDATA
3092dnl  ----------------------
3093dnl  Determine whether the assembler accepts gotdata relocations.
3094dnl
3095dnl  See also mpn/sparc32/sparc-defs.m4 which uses the result of this test.
3096
3097AC_DEFUN([GMP_ASM_SPARC_GOTDATA],
3098[AC_REQUIRE([GMP_ASM_TEXT])
3099AC_CACHE_CHECK([if the assembler accepts gotdata relocations],
3100               gmp_cv_asm_sparc_gotdata,
3101[GMP_TRY_ASSEMBLE(
3102[	$gmp_cv_asm_text
3103	.text
3104	sethi	%gdop_hix22(symbol), %g1
3105	or	%g1, %gdop_lox10(symbol), %g1
3106],
3107[gmp_cv_asm_sparc_gotdata=yes],
3108[gmp_cv_asm_sparc_gotdata=no])])
3109
3110GMP_DEFINE_RAW(["define(<HAVE_GOTDATA>,<$gmp_cv_asm_sparc_gotdata>)"])
3111])
3112
3113
3114dnl  GMP_ASM_SPARC_SHARED_THUNKS
3115dnl  ----------------------
3116dnl  Determine whether the assembler supports all of the features
3117dnl  necessary in order to emit shared PIC thunks on sparc.
3118dnl
3119dnl  See also mpn/sparc32/sparc-defs.m4 which uses the result of this test.
3120
3121AC_DEFUN([GMP_ASM_SPARC_SHARED_THUNKS],
3122[AC_REQUIRE([GMP_ASM_TEXT])
3123AC_CACHE_CHECK([if the assembler can support shared PIC thunks],
3124               gmp_cv_asm_sparc_shared_thunks,
3125[GMP_TRY_ASSEMBLE(
3126[	$gmp_cv_asm_text
3127	.section	.text.__sparc_get_pc_thunk.l7,"axG",@progbits,__sparc_get_pc_thunk.l7,comdat
3128	.weak	__sparc_get_pc_thunk.l7
3129	.hidden	__sparc_get_pc_thunk.l7
3130	.type	__sparc_get_pc_thunk.l7, #function
3131__sparc_get_pc_thunk.l7:
3132	jmp	%o7+8
3133	 add	%o7, %l7, %l7
3134],
3135[gmp_cv_asm_sparc_shared_thunks=yes],
3136[gmp_cv_asm_sparc_shared_thunks=no])])
3137
3138GMP_DEFINE_RAW(["define(<HAVE_SHARED_THUNKS>,<$gmp_cv_asm_sparc_shared_thunks>)"])
3139])
3140
3141
3142dnl  GMP_C_ATTRIBUTE_CONST
3143dnl  ---------------------
3144
3145AC_DEFUN([GMP_C_ATTRIBUTE_CONST],
3146[AC_CACHE_CHECK([whether gcc __attribute__ ((const)) works],
3147                gmp_cv_c_attribute_const,
3148[AC_TRY_COMPILE([int foo (int x) __attribute__ ((const));], ,
3149  gmp_cv_c_attribute_const=yes, gmp_cv_c_attribute_const=no)
3150])
3151if test $gmp_cv_c_attribute_const = yes; then
3152  AC_DEFINE(HAVE_ATTRIBUTE_CONST, 1,
3153  [Define to 1 if the compiler accepts gcc style __attribute__ ((const))])
3154fi
3155])
3156
3157
3158dnl  GMP_C_ATTRIBUTE_MALLOC
3159dnl  ----------------------
3160dnl  gcc 2.95.x accepts __attribute__ ((malloc)) but with a warning that
3161dnl  it's ignored.  Pretend it doesn't exist in this case, to avoid that
3162dnl  warning.
3163
3164AC_DEFUN([GMP_C_ATTRIBUTE_MALLOC],
3165[AC_CACHE_CHECK([whether gcc __attribute__ ((malloc)) works],
3166                gmp_cv_c_attribute_malloc,
3167[cat >conftest.c <<EOF
3168void *foo (int x) __attribute__ ((malloc));
3169EOF
3170gmp_compile="$CC $CFLAGS $CPPFLAGS -c conftest.c >conftest.out 2>&1"
3171if AC_TRY_EVAL(gmp_compile); then
3172  if grep "attribute directive ignored" conftest.out >/dev/null; then
3173    gmp_cv_c_attribute_malloc=no
3174  else
3175    gmp_cv_c_attribute_malloc=yes
3176  fi
3177else
3178  gmp_cv_c_attribute_malloc=no
3179fi
3180cat conftest.out >&AC_FD_CC
3181rm -f conftest*
3182])
3183if test $gmp_cv_c_attribute_malloc = yes; then
3184  AC_DEFINE(HAVE_ATTRIBUTE_MALLOC, 1,
3185  [Define to 1 if the compiler accepts gcc style __attribute__ ((malloc))])
3186fi
3187])
3188
3189
3190dnl  GMP_C_ATTRIBUTE_MODE
3191dnl  --------------------
3192dnl  Introduced in gcc 2.2, but perhaps not in all Apple derived versions.
3193
3194AC_DEFUN([GMP_C_ATTRIBUTE_MODE],
3195[AC_CACHE_CHECK([whether gcc __attribute__ ((mode (XX))) works],
3196                gmp_cv_c_attribute_mode,
3197[AC_TRY_COMPILE([typedef int SItype __attribute__ ((mode (SI)));], ,
3198  gmp_cv_c_attribute_mode=yes, gmp_cv_c_attribute_mode=no)
3199])
3200if test $gmp_cv_c_attribute_mode = yes; then
3201  AC_DEFINE(HAVE_ATTRIBUTE_MODE, 1,
3202  [Define to 1 if the compiler accepts gcc style __attribute__ ((mode (XX)))])
3203fi
3204])
3205
3206
3207dnl  GMP_C_ATTRIBUTE_NORETURN
3208dnl  ------------------------
3209
3210AC_DEFUN([GMP_C_ATTRIBUTE_NORETURN],
3211[AC_CACHE_CHECK([whether gcc __attribute__ ((noreturn)) works],
3212                gmp_cv_c_attribute_noreturn,
3213[AC_TRY_COMPILE([void foo (int x) __attribute__ ((noreturn));], ,
3214  gmp_cv_c_attribute_noreturn=yes, gmp_cv_c_attribute_noreturn=no)
3215])
3216if test $gmp_cv_c_attribute_noreturn = yes; then
3217  AC_DEFINE(HAVE_ATTRIBUTE_NORETURN, 1,
3218  [Define to 1 if the compiler accepts gcc style __attribute__ ((noreturn))])
3219fi
3220])
3221
3222dnl  GMP_C_HIDDEN_ALIAS
3223dnl  ------------------------
3224
3225AC_DEFUN([GMP_C_HIDDEN_ALIAS],
3226[AC_CACHE_CHECK([whether gcc hidden aliases work],
3227                gmp_cv_c_hidden_alias,
3228[AC_TRY_COMPILE(
3229[void hid() __attribute__ ((visibility("hidden")));
3230void hid() {}
3231void pub() __attribute__ ((alias("hid")));],
3232, gmp_cv_c_hidden_alias=yes, gmp_cv_c_hidden_alias=no)
3233])
3234if test $gmp_cv_c_hidden_alias = yes; then
3235  AC_DEFINE(HAVE_HIDDEN_ALIAS, 1,
3236  [Define to 1 if the compiler accepts gcc style __attribute__ ((visibility))
3237and __attribute__ ((alias))])
3238fi
3239])
3240
3241dnl  GMP_C_DOUBLE_FORMAT
3242dnl  -------------------
3243dnl  Determine the floating point format.
3244dnl
3245dnl  The object file is grepped, in order to work when cross compiling.  A
3246dnl  start and end sequence is included to avoid false matches, and allowance
3247dnl  is made for the desired data crossing an "od -b" line boundary.  The test
3248dnl  number is a small integer so it should appear exactly, no rounding or
3249dnl  truncation etc.
3250dnl
3251dnl  "od -b", incidentally, is supported even by Unix V7, and the awk script
3252dnl  used doesn't have functions or anything, so even an "old" awk should
3253dnl  suffice.
3254dnl
3255dnl  The C code here declares the variable foo as extern; without that, some
3256dnl  C++ compilers will not put foo in the object file.
3257
3258AC_DEFUN([GMP_C_DOUBLE_FORMAT],
3259[AC_REQUIRE([AC_PROG_CC])
3260AC_REQUIRE([AC_PROG_AWK])
3261AC_CACHE_CHECK([format of `double' floating point],
3262                gmp_cv_c_double_format,
3263[gmp_cv_c_double_format=unknown
3264cat >conftest.c <<\EOF
3265[#include <stdio.h>
3266struct foo {
3267  char    before[8];
3268  double  x;
3269  char    after[8];
3270};
3271extern struct foo foo;
3272struct foo foo = {
3273  { '\001', '\043', '\105', '\147', '\211', '\253', '\315', '\357' },
3274  -123456789.0,
3275  { '\376', '\334', '\272', '\230', '\166', '\124', '\062', '\020' },
3276};
3277int main(){
3278  int i;
3279  for (i = 0; i < 8; i++) {
3280    printf ("%d %f\n", foo.before[i] + foo.after[i], foo.x);
3281  }
3282  return 0;
3283}]
3284EOF
3285gmp_compile="$CC $CFLAGS $CPPFLAGS conftest.c -o conftest$EXEEXT >&AC_FD_CC 2>&1"
3286if AC_TRY_EVAL(gmp_compile); then
3287cat >conftest.awk <<\EOF
3288[
3289BEGIN {
3290  found = 0
3291}
3292
3293{
3294  for (f = 2; f <= NF; f++)
3295    {
3296      for (i = 0; i < 23; i++)
3297        got[i] = got[i+1];
3298      got[23] = $f;
3299
3300      # match the special begin and end sequences
3301      if (got[0] != "001") continue
3302      if (got[1] != "043") continue
3303      if (got[2] != "105") continue
3304      if (got[3] != "147") continue
3305      if (got[4] != "211") continue
3306      if (got[5] != "253") continue
3307      if (got[6] != "315") continue
3308      if (got[7] != "357") continue
3309      if (got[16] != "376") continue
3310      if (got[17] != "334") continue
3311      if (got[18] != "272") continue
3312      if (got[19] != "230") continue
3313      if (got[20] != "166") continue
3314      if (got[21] != "124") continue
3315      if (got[22] != "062") continue
3316      if (got[23] != "020") continue
3317
3318      saw = " (" got[8] " " got[9] " " got[10] " " got[11] " " got[12] " " got[13] " " got[14] " " got[15] ")"
3319
3320      if (got[8]  == "000" &&  \
3321          got[9]  == "000" &&  \
3322          got[10] == "000" &&  \
3323          got[11] == "124" &&  \
3324          got[12] == "064" &&  \
3325          got[13] == "157" &&  \
3326          got[14] == "235" &&  \
3327          got[15] == "301")
3328        {
3329          print "IEEE little endian"
3330          found = 1
3331          exit
3332        }
3333
3334      # Little endian with the two 4-byte halves swapped, as used by ARM
3335      # when the chip is in little endian mode.
3336      #
3337      if (got[8]  == "064" &&  \
3338          got[9]  == "157" &&  \
3339          got[10] == "235" &&  \
3340          got[11] == "301" &&  \
3341          got[12] == "000" &&  \
3342          got[13] == "000" &&  \
3343          got[14] == "000" &&  \
3344          got[15] == "124")
3345        {
3346          print "IEEE little endian, swapped halves"
3347          found = 1
3348          exit
3349        }
3350
3351      # gcc 2.95.4 on one GNU/Linux ARM system was seen generating 000 in
3352      # the last byte, whereas 124 is correct.  Not sure where the bug
3353      # actually lies, but a running program didn't seem to get a full
3354      # mantissa worth of working bits.
3355      #
3356      # We match this case explicitly so we can give a nice result message,
3357      # but we deliberately exclude it from the normal IEEE double setups
3358      # since it's too broken.
3359      #
3360      if (got[8]  == "064" &&  \
3361          got[9]  == "157" &&  \
3362          got[10] == "235" &&  \
3363          got[11] == "301" &&  \
3364          got[12] == "000" &&  \
3365          got[13] == "000" &&  \
3366          got[14] == "000" &&  \
3367          got[15] == "000")
3368        {
3369          print "bad ARM software floats"
3370          found = 1
3371          exit
3372        }
3373
3374      if (got[8]  == "301" &&  \
3375          got[9]  == "235" &&  \
3376          got[10] == "157" &&  \
3377          got[11] == "064" &&  \
3378          got[12] == "124" &&  \
3379          got[13] == "000" &&  \
3380          got[14] == "000" &&  \
3381          got[15] == "000")
3382        {
3383          print "IEEE big endian"
3384          found = 1
3385          exit
3386        }
3387
3388      if (got[8]  == "353" &&  \
3389          got[9]  == "315" &&  \
3390          got[10] == "242" &&  \
3391          got[11] == "171" &&  \
3392          got[12] == "000" &&  \
3393          got[13] == "240" &&  \
3394          got[14] == "000" &&  \
3395          got[15] == "000")
3396        {
3397          print "VAX D"
3398          found = 1
3399          exit
3400        }
3401
3402      if (got[8]  == "275" &&  \
3403          got[9]  == "301" &&  \
3404          got[10] == "064" &&  \
3405          got[11] == "157" &&  \
3406          got[12] == "000" &&  \
3407          got[13] == "124" &&  \
3408          got[14] == "000" &&  \
3409          got[15] == "000")
3410        {
3411          print "VAX G"
3412          found = 1
3413          exit
3414        }
3415
3416      if (got[8]  == "300" &&  \
3417          got[9]  == "033" &&  \
3418          got[10] == "353" &&  \
3419          got[11] == "171" &&  \
3420          got[12] == "242" &&  \
3421          got[13] == "240" &&  \
3422          got[14] == "000" &&  \
3423          got[15] == "000")
3424        {
3425          print "Cray CFP"
3426          found = 1
3427          exit
3428        }
3429    }
3430}
3431
3432END {
3433  if (! found)
3434    print "unknown", saw
3435}
3436]
3437EOF
3438  gmp_cv_c_double_format=`od -b conftest$EXEEXT | $AWK -f conftest.awk`
3439  case $gmp_cv_c_double_format in
3440  unknown*)
3441    echo "cannot match anything, conftest$EXEEXT contains" >&AC_FD_CC
3442    od -b conftest$EXEEXT >&AC_FD_CC
3443    ;;
3444  esac
3445else
3446  AC_MSG_WARN([oops, cannot compile test program])
3447fi
3448rm -f conftest*
3449])
3450
3451AH_VERBATIM([HAVE_DOUBLE],
3452[/* Define one of the following to 1 for the format of a `double'.
3453   If your format is not among these choices, or you don't know what it is,
3454   then leave all undefined.
3455   IEEE_LITTLE_SWAPPED means little endian, but with the two 4-byte halves
3456   swapped, as used by ARM CPUs in little endian mode.  */
3457#undef HAVE_DOUBLE_IEEE_BIG_ENDIAN
3458#undef HAVE_DOUBLE_IEEE_LITTLE_ENDIAN
3459#undef HAVE_DOUBLE_IEEE_LITTLE_SWAPPED
3460#undef HAVE_DOUBLE_VAX_D
3461#undef HAVE_DOUBLE_VAX_G
3462#undef HAVE_DOUBLE_CRAY_CFP])
3463
3464case $gmp_cv_c_double_format in
3465  "IEEE big endian")
3466    AC_DEFINE(HAVE_DOUBLE_IEEE_BIG_ENDIAN, 1)
3467    GMP_DEFINE_RAW("define_not_for_expansion(\`HAVE_DOUBLE_IEEE_BIG_ENDIAN')", POST)
3468    ;;
3469  "IEEE little endian")
3470    AC_DEFINE(HAVE_DOUBLE_IEEE_LITTLE_ENDIAN, 1)
3471    GMP_DEFINE_RAW("define_not_for_expansion(\`HAVE_DOUBLE_IEEE_LITTLE_ENDIAN')", POST)
3472    ;;
3473  "IEEE little endian, swapped halves")
3474    AC_DEFINE(HAVE_DOUBLE_IEEE_LITTLE_SWAPPED, 1) ;;
3475  "VAX D")
3476    AC_DEFINE(HAVE_DOUBLE_VAX_D, 1) ;;
3477  "VAX G")
3478    AC_DEFINE(HAVE_DOUBLE_VAX_G, 1) ;;
3479  "Cray CFP")
3480    AC_DEFINE(HAVE_DOUBLE_CRAY_CFP, 1) ;;
3481  "bad ARM software floats")
3482    ;;
3483  unknown*)
3484    AC_MSG_WARN([Could not determine float format.])
3485    AC_MSG_WARN([Conversions to and from "double" may be slow.])
3486    ;;
3487  *)
3488    AC_MSG_WARN([oops, unrecognised float format: $gmp_cv_c_double_format])
3489    ;;
3490esac
3491])
3492
3493
3494dnl  GMP_C_STDARG
3495dnl  ------------
3496dnl  Test whether to use <stdarg.h>.
3497dnl
3498dnl  Notice the AC_DEFINE here is HAVE_STDARG to avoid clashing with
3499dnl  HAVE_STDARG_H which could arise from AC_CHECK_HEADERS.
3500dnl
3501dnl  This test might be slight overkill, after all there's really only going
3502dnl  to be ANSI or K&R and the two can be differentiated by AC_PROG_CC_STDC
3503dnl  or very likely by the setups for _PROTO in gmp.h.  On the other hand
3504dnl  this test is nice and direct, being what we're going to actually use.
3505
3506dnl  AC_DEFUN([GMP_C_STDARG],
3507dnl  [AC_CACHE_CHECK([whether <stdarg.h> exists and works],
3508dnl                  gmp_cv_c_stdarg,
3509dnl  [AC_TRY_COMPILE(
3510dnl  [#include <stdarg.h>
3511dnl  int foo (int x, ...)
3512dnl  {
3513dnl    va_list  ap;
3514dnl    int      y;
3515dnl    va_start (ap, x);
3516dnl    y = va_arg (ap, int);
3517dnl    va_end (ap);
3518dnl    return y;
3519dnl  }],,
3520dnl  gmp_cv_c_stdarg=yes, gmp_cv_c_stdarg=no)
3521dnl  ])
3522dnl  if test $gmp_cv_c_stdarg = yes; then
3523dnl    AC_DEFINE(HAVE_STDARG, 1, [Define to 1 if <stdarg.h> exists and works])
3524dnl  fi
3525dnl  ])
3526
3527
3528dnl  GMP_FUNC_ALLOCA
3529dnl  ---------------
3530dnl  Determine whether "alloca" is available.  This is AC_FUNC_ALLOCA from
3531dnl  autoconf, but changed so it doesn't use alloca.c if alloca() isn't
3532dnl  available, and also to use gmp-impl.h for the conditionals detecting
3533dnl  compiler builtin alloca's.
3534
3535AC_DEFUN([GMP_FUNC_ALLOCA],
3536[AC_REQUIRE([GMP_HEADER_ALLOCA])
3537AC_CACHE_CHECK([for alloca (via gmp-impl.h)],
3538               gmp_cv_func_alloca,
3539[AC_TRY_LINK(
3540GMP_INCLUDE_GMP_H
3541[#include "$srcdir/gmp-impl.h"
3542],
3543  [char *p = (char *) alloca (1);],
3544  gmp_cv_func_alloca=yes,
3545  gmp_cv_func_alloca=no)])
3546if test $gmp_cv_func_alloca = yes; then
3547  AC_DEFINE(HAVE_ALLOCA, 1, [Define to 1 if alloca() works (via gmp-impl.h).])
3548fi
3549])
3550
3551AC_DEFUN([GMP_HEADER_ALLOCA],
3552[# The Ultrix 4.2 mips builtin alloca declared by alloca.h only works
3553# for constant arguments.  Useless!
3554AC_CACHE_CHECK([for working alloca.h],
3555               gmp_cv_header_alloca,
3556[AC_TRY_LINK([#include <alloca.h>],
3557  [char *p = (char *) alloca (2 * sizeof (int));],
3558  gmp_cv_header_alloca=yes,
3559  gmp_cv_header_alloca=no)])
3560if test $gmp_cv_header_alloca = yes; then
3561  AC_DEFINE(HAVE_ALLOCA_H, 1,
3562  [Define to 1 if you have <alloca.h> and it should be used (not on Ultrix).])
3563fi
3564])
3565
3566
3567dnl  GMP_OPTION_ALLOCA
3568dnl  -----------------
3569dnl  Decide what to do about --enable-alloca from the user.
3570dnl  This is a macro so it can require GMP_FUNC_ALLOCA.
3571
3572AC_DEFUN([GMP_OPTION_ALLOCA],
3573[AC_REQUIRE([GMP_FUNC_ALLOCA])
3574AC_CACHE_CHECK([how to allocate temporary memory],
3575               gmp_cv_option_alloca,
3576[case $enable_alloca in
3577  yes)
3578    gmp_cv_option_alloca=alloca
3579    ;;
3580  no)
3581    gmp_cv_option_alloca=malloc-reentrant
3582    ;;
3583  reentrant | notreentrant)
3584    case $gmp_cv_func_alloca in
3585    yes)  gmp_cv_option_alloca=alloca ;;
3586    *)    gmp_cv_option_alloca=malloc-$enable_alloca ;;
3587    esac
3588    ;;
3589  *)
3590    gmp_cv_option_alloca=$enable_alloca
3591    ;;
3592esac
3593])
3594
3595AH_VERBATIM([WANT_TMP],
3596[/* Define one of these to 1 for the desired temporary memory allocation
3597   method, per --enable-alloca. */
3598#undef WANT_TMP_ALLOCA
3599#undef WANT_TMP_REENTRANT
3600#undef WANT_TMP_NOTREENTRANT
3601#undef WANT_TMP_DEBUG])
3602
3603case $gmp_cv_option_alloca in
3604  alloca)
3605    if test $gmp_cv_func_alloca = no; then
3606      AC_MSG_ERROR([--enable-alloca=alloca specified, but alloca not available])
3607    fi
3608    AC_DEFINE(WANT_TMP_ALLOCA)
3609    TAL_OBJECT=tal-reent$U.lo
3610    ;;
3611  malloc-reentrant)
3612    AC_DEFINE(WANT_TMP_REENTRANT)
3613    TAL_OBJECT=tal-reent$U.lo
3614    ;;
3615  malloc-notreentrant)
3616    AC_DEFINE(WANT_TMP_NOTREENTRANT)
3617    TAL_OBJECT=tal-notreent$U.lo
3618    ;;
3619  debug)
3620    AC_DEFINE(WANT_TMP_DEBUG)
3621    TAL_OBJECT=tal-debug$U.lo
3622    ;;
3623  *)
3624    # checks at the start of configure.in should protect us
3625    AC_MSG_ERROR([unrecognised --enable-alloca=$gmp_cv_option_alloca])
3626    ;;
3627esac
3628AC_SUBST(TAL_OBJECT)
3629])
3630
3631
3632dnl  GMP_FUNC_SSCANF_WRITABLE_INPUT
3633dnl  ------------------------------
3634dnl  Determine whether sscanf requires a writable input string.
3635dnl
3636dnl  It might be nicer to run a program to determine this when doing a
3637dnl  native build, but the systems afflicted are few and far between these
3638dnl  days, so it seems good enough just to list them.
3639
3640AC_DEFUN([GMP_FUNC_SSCANF_WRITABLE_INPUT],
3641[AC_CACHE_CHECK([whether sscanf needs writable input],
3642                 gmp_cv_func_sscanf_writable_input,
3643[case $host in
3644  *-*-hpux9 | *-*-hpux9.*)
3645     gmp_cv_func_sscanf_writable_input=yes ;;
3646  *) gmp_cv_func_sscanf_writable_input=no  ;;
3647esac
3648])
3649case $gmp_cv_func_sscanf_writable_input in
3650  yes) AC_DEFINE(SSCANF_WRITABLE_INPUT, 1,
3651                 [Define to 1 if sscanf requires writable inputs]) ;;
3652  no)  ;;
3653  *)   AC_MSG_ERROR([unrecognised \$gmp_cv_func_sscanf_writable_input]) ;;
3654esac
3655])
3656
3657
3658dnl  GMP_FUNC_VSNPRINTF
3659dnl  ------------------
3660dnl  Check whether vsnprintf exists, and works properly.
3661dnl
3662dnl  Systems without vsnprintf include mingw32, OSF 4.
3663dnl
3664dnl  Sparc Solaris 2.7 in 64-bit mode doesn't always truncate, making
3665dnl  vsnprintf like vsprintf, and hence completely useless.  On one system a
3666dnl  literal string is enough to provoke the problem, on another a "%n" was
3667dnl  needed.  There seems to be something weird going on with the optimizer
3668dnl  or something, since on the first system adding a second check with
3669dnl  "%n", or even just an initialized local variable, makes it work.  In
3670dnl  any case, without bothering to get to the bottom of this, the two
3671dnl  program runs in the code below end up successfully detecting the
3672dnl  problem.
3673dnl
3674dnl  glibc 2.0.x returns either -1 or bufsize-1 for an overflow (both seen,
3675dnl  not sure which 2.0.x does which), but still puts the correct null
3676dnl  terminated result into the buffer.
3677
3678AC_DEFUN([GMP_FUNC_VSNPRINTF],
3679[AC_CHECK_FUNC(vsnprintf,
3680              [gmp_vsnprintf_exists=yes],
3681              [gmp_vsnprintf_exists=no])
3682if test "$gmp_vsnprintf_exists" = no; then
3683  gmp_cv_func_vsnprintf=no
3684else
3685  AC_CACHE_CHECK([whether vsnprintf works],
3686                 gmp_cv_func_vsnprintf,
3687  [gmp_cv_func_vsnprintf=yes
3688   for i in 'return check ("hello world");' 'int n; return check ("%nhello world", &n);'; do
3689     AC_TRY_RUN([
3690#include <string.h>  /* for strcmp */
3691#include <stdio.h>   /* for vsnprintf */
3692
3693#include <stdarg.h>
3694
3695int
3696check (const char *fmt, ...)
3697{
3698  static char  buf[128];
3699  va_list  ap;
3700  int      ret;
3701
3702  va_start (ap, fmt);
3703
3704  ret = vsnprintf (buf, 4, fmt, ap);
3705
3706  if (ret == -1 || strcmp (buf, "hel") != 0)
3707    return 1;
3708
3709  /* allowed return values */
3710  if (ret != 3 && ret != 11)
3711    return 2;
3712
3713  return 0;
3714}
3715
3716int
3717main ()
3718{
3719$i
3720}
3721],
3722      [:],
3723      [gmp_cv_func_vsnprintf=no; break],
3724      [gmp_cv_func_vsnprintf=probably; break])
3725  done
3726  ])
3727  if test "$gmp_cv_func_vsnprintf" = probably; then
3728    AC_MSG_WARN([cannot check for properly working vsnprintf when cross compiling, will assume it's ok])
3729  fi
3730  if test "$gmp_cv_func_vsnprintf" != no; then
3731    AC_DEFINE(HAVE_VSNPRINTF,1,
3732    [Define to 1 if you have the `vsnprintf' function and it works properly.])
3733  fi
3734fi
3735])
3736
3737
3738dnl  GMP_H_EXTERN_INLINE
3739dnl  -------------------
3740dnl  If the compiler has an "inline" of some sort, check whether the
3741dnl  #ifdef's in gmp.h recognise it.
3742
3743AC_DEFUN([GMP_H_EXTERN_INLINE],
3744[AC_REQUIRE([AC_C_INLINE])
3745case $ac_cv_c_inline in
3746no) ;;
3747*)
3748  AC_TRY_COMPILE(
3749[#define __GMP_WITHIN_CONFIGURE_INLINE 1
3750]GMP_INCLUDE_GMP_H[
3751#ifndef __GMP_EXTERN_INLINE
3752die die die
3753#endif
3754],,,
3755  [case $ac_cv_c_inline in
3756  yes) tmp_inline=inline ;;
3757  *)   tmp_inline=$ac_cv_c_inline ;;
3758  esac
3759  AC_MSG_WARN([gmp.h doesnt recognise compiler "$tmp_inline", inlines will be unavailable])])
3760  ;;
3761esac
3762])
3763
3764
3765dnl  GMP_H_HAVE_FILE
3766dnl  ---------------
3767dnl  Check whether the #ifdef's in gmp.h recognise when stdio.h has been
3768dnl  included to get FILE.
3769
3770AC_DEFUN([GMP_H_HAVE_FILE],
3771[AC_TRY_COMPILE(
3772[#include <stdio.h>]
3773GMP_INCLUDE_GMP_H
3774[#if ! _GMP_H_HAVE_FILE
3775die die die
3776#endif
3777],,,
3778  [AC_MSG_WARN([gmp.h doesnt recognise <stdio.h>, FILE prototypes will be unavailable])])
3779])
3780
3781
3782dnl  GMP_PROG_CC_FOR_BUILD
3783dnl  ---------------------
3784dnl  Establish CC_FOR_BUILD, a C compiler for the build system.
3785dnl
3786dnl  If CC_FOR_BUILD is set then it's expected to work, likewise the old
3787dnl  style HOST_CC, otherwise some likely candidates are tried, the same as
3788dnl  configfsf.guess.
3789
3790AC_DEFUN([GMP_PROG_CC_FOR_BUILD],
3791[AC_REQUIRE([AC_PROG_CC])
3792if test -n "$CC_FOR_BUILD"; then
3793  GMP_PROG_CC_FOR_BUILD_WORKS($CC_FOR_BUILD,,
3794    [AC_MSG_ERROR([Specified CC_FOR_BUILD doesn't seem to work])])
3795elif test -n "$HOST_CC"; then
3796  GMP_PROG_CC_FOR_BUILD_WORKS($HOST_CC,
3797    [CC_FOR_BUILD=$HOST_CC],
3798    [AC_MSG_ERROR([Specified HOST_CC doesn't seem to work])])
3799else
3800  for i in "$CC" "$CC $CFLAGS $CPPFLAGS" cc gcc c89 c99; do
3801    GMP_PROG_CC_FOR_BUILD_WORKS($i,
3802      [CC_FOR_BUILD=$i
3803       break])
3804  done
3805  if test -z "$CC_FOR_BUILD"; then
3806    AC_MSG_ERROR([Cannot find a build system compiler])
3807  fi
3808fi
3809
3810AC_ARG_VAR(CC_FOR_BUILD,[build system C compiler])
3811AC_SUBST(CC_FOR_BUILD)
3812])
3813
3814
3815dnl  GMP_PROG_CC_FOR_BUILD_WORKS(cc/cflags[,[action-if-good][,action-if-bad]])
3816dnl  -------------------------------------------------------------------------
3817dnl  See if the given cc/cflags works on the build system.
3818dnl
3819dnl  It seems easiest to just use the default compiler output, rather than
3820dnl  figuring out the .exe or whatever at this stage.
3821
3822AC_DEFUN([GMP_PROG_CC_FOR_BUILD_WORKS],
3823[AC_MSG_CHECKING([build system compiler $1])
3824# remove anything that might look like compiler output to our "||" expression
3825rm -f conftest* a.out b.out a.exe a_out.exe
3826cat >conftest.c <<EOF
3827int
3828main ()
3829{
3830  return 0;
3831}
3832EOF
3833gmp_compile="$1 conftest.c"
3834cc_for_build_works=no
3835if AC_TRY_EVAL(gmp_compile); then
3836  if (./a.out || ./b.out || ./a.exe || ./a_out.exe || ./conftest) >&AC_FD_CC 2>&1; then
3837    cc_for_build_works=yes
3838  fi
3839fi
3840rm -f conftest* a.out b.out a.exe a_out.exe
3841AC_MSG_RESULT($cc_for_build_works)
3842if test "$cc_for_build_works" = yes; then
3843  ifelse([$2],,:,[$2])
3844else
3845  ifelse([$3],,:,[$3])
3846fi
3847])
3848
3849
3850dnl  GMP_PROG_CPP_FOR_BUILD
3851dnl  ---------------------
3852dnl  Establish CPP_FOR_BUILD, the build system C preprocessor.
3853dnl  The choices tried here are the same as AC_PROG_CPP, but with
3854dnl  CC_FOR_BUILD.
3855
3856AC_DEFUN([GMP_PROG_CPP_FOR_BUILD],
3857[AC_REQUIRE([GMP_PROG_CC_FOR_BUILD])
3858AC_MSG_CHECKING([for build system preprocessor])
3859if test -z "$CPP_FOR_BUILD"; then
3860  AC_CACHE_VAL(gmp_cv_prog_cpp_for_build,
3861  [cat >conftest.c <<EOF
3862#define FOO BAR
3863EOF
3864  for i in "$CC_FOR_BUILD -E" "$CC_FOR_BUILD -E -traditional-cpp" "/lib/cpp"; do
3865    gmp_compile="$i conftest.c"
3866    if AC_TRY_EVAL(gmp_compile) >&AC_FD_CC 2>&1; then
3867      gmp_cv_prog_cpp_for_build=$i
3868      break
3869    fi
3870  done
3871  rm -f conftest* a.out b.out a.exe a_out.exe
3872  if test -z "$gmp_cv_prog_cpp_for_build"; then
3873    AC_MSG_ERROR([Cannot find build system C preprocessor.])
3874  fi
3875  ])
3876  CPP_FOR_BUILD=$gmp_cv_prog_cpp_for_build
3877fi
3878AC_MSG_RESULT([$CPP_FOR_BUILD])
3879
3880AC_ARG_VAR(CPP_FOR_BUILD,[build system C preprocessor])
3881AC_SUBST(CPP_FOR_BUILD)
3882])
3883
3884
3885dnl  GMP_PROG_EXEEXT_FOR_BUILD
3886dnl  -------------------------
3887dnl  Determine EXEEXT_FOR_BUILD, the build system executable suffix.
3888dnl
3889dnl  The idea is to find what "-o conftest$foo" will make it possible to run
3890dnl  the program with ./conftest.  On Unix-like systems this is of course
3891dnl  nothing, for DOS it's ".exe", or for a strange RISC OS foreign file
3892dnl  system cross compile it can be ",ff8" apparently.  Not sure if the
3893dnl  latter actually applies to a build-system executable, maybe it doesn't,
3894dnl  but it won't hurt to try.
3895
3896AC_DEFUN([GMP_PROG_EXEEXT_FOR_BUILD],
3897[AC_REQUIRE([GMP_PROG_CC_FOR_BUILD])
3898AC_CACHE_CHECK([for build system executable suffix],
3899               gmp_cv_prog_exeext_for_build,
3900[cat >conftest.c <<EOF
3901int
3902main ()
3903{
3904  return 0;
3905}
3906EOF
3907for i in .exe ,ff8 ""; do
3908  gmp_compile="$CC_FOR_BUILD conftest.c -o conftest$i"
3909  if AC_TRY_EVAL(gmp_compile); then
3910    if (./conftest) 2>&AC_FD_CC; then
3911      gmp_cv_prog_exeext_for_build=$i
3912      break
3913    fi
3914  fi
3915done
3916rm -f conftest*
3917if test "${gmp_cv_prog_exeext_for_build+set}" != set; then
3918  AC_MSG_ERROR([Cannot determine executable suffix])
3919fi
3920])
3921AC_SUBST(EXEEXT_FOR_BUILD,$gmp_cv_prog_exeext_for_build)
3922])
3923
3924
3925dnl  GMP_C_FOR_BUILD_ANSI
3926dnl  --------------------
3927dnl  Determine whether CC_FOR_BUILD is ANSI, and establish U_FOR_BUILD
3928dnl  accordingly.
3929dnl
3930dnl  FIXME: Use AC_PROG_CC sets ac_cv_prog_cc_c89 which could be used instead
3931
3932AC_DEFUN([GMP_C_FOR_BUILD_ANSI],
3933[AC_REQUIRE([GMP_PROG_CC_FOR_BUILD])
3934AC_CACHE_CHECK([whether build system compiler is ANSI],
3935               gmp_cv_c_for_build_ansi,
3936[cat >conftest.c <<EOF
3937int
3938main (int argc, char **argv)
3939{
3940  return 0;
3941}
3942EOF
3943gmp_compile="$CC_FOR_BUILD conftest.c"
3944if AC_TRY_EVAL(gmp_compile); then
3945  gmp_cv_c_for_build_ansi=yes
3946else
3947  gmp_cv_c_for_build_ansi=no
3948fi
3949rm -f conftest* a.out b.out a.exe a_out.exe
3950])
3951if test "$gmp_cv_c_for_build_ansi" = yes; then
3952  U_FOR_BUILD=
3953else
3954  AC_SUBST(U_FOR_BUILD,_)
3955fi
3956])
3957
3958
3959dnl  GMP_CHECK_LIBM_FOR_BUILD
3960dnl  ------------------------
3961dnl  Establish LIBM_FOR_BUILD as -lm, if that seems to work.
3962dnl
3963dnl  Libtool AC_CHECK_LIBM also uses -lmw on *-ncr-sysv4.3*, if it works.
3964dnl  Don't know what that does, lets assume it's not needed just for log().
3965
3966AC_DEFUN([GMP_CHECK_LIBM_FOR_BUILD],
3967[AC_REQUIRE([GMP_PROG_CC_FOR_BUILD])
3968AC_CACHE_CHECK([for build system compiler math library],
3969               gmp_cv_check_libm_for_build,
3970[cat >conftest.c <<EOF
3971#include <math.h>
3972int
3973main ()
3974{
3975  return 0;
3976}
3977double d;
3978double
3979foo ()
3980{
3981  return log (d);
3982}
3983EOF
3984gmp_compile="$CC_FOR_BUILD conftest.c -lm"
3985if AC_TRY_EVAL(gmp_compile); then
3986  gmp_cv_check_libm_for_build=-lm
3987else
3988  gmp_cv_check_libm_for_build=no
3989fi
3990rm -f conftest* a.out b.out a.exe a_out.exe
3991])
3992case $gmp_cv_check_libm_for_build in
3993  yes) AC_SUBST(LIBM_FOR_BUILD,-lm) ;;
3994  no)  LIBM_FOR_BUILD= ;;
3995  *)   LIBM_FOR_BUILD=$gmp_cv_check_libm_for_build ;;
3996esac
3997])
3998