1dnl This file contains Zend specific autoconf macros.
2
3dnl
4dnl ZEND_CHECK_FLOAT_PRECISION
5dnl
6dnl x87 floating point internal precision control checks
7dnl See: http://wiki.php.net/rfc/rounding
8dnl
9AC_DEFUN([ZEND_CHECK_FLOAT_PRECISION],[
10  AC_MSG_CHECKING([for usable _FPU_SETCW])
11  AC_LINK_IFELSE([AC_LANG_PROGRAM([[
12    #include <fpu_control.h>
13  ]],[[
14    fpu_control_t fpu_oldcw, fpu_cw;
15    volatile double result;
16    double a = 2877.0;
17    volatile double b = 1000000.0;
18
19    _FPU_GETCW(fpu_oldcw);
20    fpu_cw = (fpu_oldcw & ~_FPU_EXTENDED & ~_FPU_SINGLE) | _FPU_DOUBLE;
21    _FPU_SETCW(fpu_cw);
22    result = a / b;
23    _FPU_SETCW(fpu_oldcw);
24  ]])],[ac_cfp_have__fpu_setcw=yes],[ac_cfp_have__fpu_setcw=no])
25  if test "$ac_cfp_have__fpu_setcw" = "yes" ; then
26    AC_DEFINE(HAVE__FPU_SETCW, 1, [whether _FPU_SETCW is present and usable])
27    AC_MSG_RESULT(yes)
28  else
29    AC_MSG_RESULT(no)
30  fi
31
32  AC_MSG_CHECKING([for usable fpsetprec])
33  AC_LINK_IFELSE([AC_LANG_PROGRAM([[
34    #include <machine/ieeefp.h>
35  ]],[[
36    fp_prec_t fpu_oldprec;
37    volatile double result;
38    double a = 2877.0;
39    volatile double b = 1000000.0;
40
41    fpu_oldprec = fpgetprec();
42    fpsetprec(FP_PD);
43    result = a / b;
44    fpsetprec(fpu_oldprec);
45  ]])], [ac_cfp_have_fpsetprec=yes], [ac_cfp_have_fpsetprec=no])
46  if test "$ac_cfp_have_fpsetprec" = "yes" ; then
47    AC_DEFINE(HAVE_FPSETPREC, 1, [whether fpsetprec is present and usable])
48    AC_MSG_RESULT(yes)
49  else
50    AC_MSG_RESULT(no)
51  fi
52
53  AC_MSG_CHECKING([for usable _controlfp])
54  AC_LINK_IFELSE([AC_LANG_PROGRAM([[
55    #include <float.h>
56  ]],[[
57    unsigned int fpu_oldcw;
58    volatile double result;
59    double a = 2877.0;
60    volatile double b = 1000000.0;
61
62    fpu_oldcw = _controlfp(0, 0);
63    _controlfp(_PC_53, _MCW_PC);
64    result = a / b;
65    _controlfp(fpu_oldcw, _MCW_PC);
66  ]])], [ac_cfp_have__controlfp=yes], [ac_cfp_have__controlfp=no])
67  if test "$ac_cfp_have__controlfp" = "yes" ; then
68    AC_DEFINE(HAVE__CONTROLFP, 1, [whether _controlfp is present usable])
69    AC_MSG_RESULT(yes)
70  else
71    AC_MSG_RESULT(no)
72  fi
73
74  AC_MSG_CHECKING([for usable _controlfp_s])
75  AC_LINK_IFELSE([AC_LANG_PROGRAM([[
76   #include <float.h>
77  ]],[[
78    unsigned int fpu_oldcw, fpu_cw;
79    volatile double result;
80    double a = 2877.0;
81    volatile double b = 1000000.0;
82
83    _controlfp_s(&fpu_cw, 0, 0);
84    fpu_oldcw = fpu_cw;
85    _controlfp_s(&fpu_cw, _PC_53, _MCW_PC);
86    result = a / b;
87    _controlfp_s(&fpu_cw, fpu_oldcw, _MCW_PC);
88  ]])], [ac_cfp_have__controlfp_s=yes], [ac_cfp_have__controlfp_s=no])
89  if test "$ac_cfp_have__controlfp_s" = "yes" ; then
90    AC_DEFINE(HAVE__CONTROLFP_S, 1, [whether _controlfp_s is present and usable])
91    AC_MSG_RESULT(yes)
92  else
93    AC_MSG_RESULT(no)
94  fi
95
96  AC_MSG_CHECKING([whether FPU control word can be manipulated by inline assembler])
97  AC_LINK_IFELSE([AC_LANG_PROGRAM([[
98    /* nothing */
99  ]],[[
100    unsigned int oldcw, cw;
101    volatile double result;
102    double a = 2877.0;
103    volatile double b = 1000000.0;
104
105    __asm__ __volatile__ ("fnstcw %0" : "=m" (*&oldcw));
106    cw = (oldcw & ~0x0 & ~0x300) | 0x200;
107    __asm__ __volatile__ ("fldcw %0" : : "m" (*&cw));
108
109    result = a / b;
110
111    __asm__ __volatile__ ("fldcw %0" : : "m" (*&oldcw));
112  ]])], [ac_cfp_have_fpu_inline_asm_x86=yes], [ac_cfp_have_fpu_inline_asm_x86=no])
113  if test "$ac_cfp_have_fpu_inline_asm_x86" = "yes" ; then
114    AC_DEFINE(HAVE_FPU_INLINE_ASM_X86, 1, [whether FPU control word can be manipulated by inline assembler])
115    AC_MSG_RESULT(yes)
116  else
117    AC_MSG_RESULT(no)
118  fi
119])
120
121dnl
122dnl LIBZEND_BASIC_CHECKS
123dnl
124dnl Basic checks specific for the Zend engine library.
125dnl
126AC_DEFUN([LIBZEND_BASIC_CHECKS],[
127AC_REQUIRE([AC_PROG_CC])
128
129AC_CHECK_HEADERS([cpuid.h])
130
131dnl
132dnl LIBZEND_DLSYM_CHECK
133dnl
134dnl Ugly hack to check if dlsym() requires a leading underscore in symbol name.
135dnl
136AC_DEFUN([LIBZEND_DLSYM_CHECK],[
137AC_MSG_CHECKING([whether dlsym() requires a leading underscore in symbol names])
138_LT_AC_TRY_DLOPEN_SELF([
139  AC_MSG_RESULT(no)
140], [
141  AC_MSG_RESULT(yes)
142  AC_DEFINE(DLSYM_NEEDS_UNDERSCORE, 1, [Define if dlsym() requires a leading underscore in symbol names. ])
143], [
144  AC_MSG_RESULT(no)
145], [])
146])
147
148dnl Checks for library functions.
149AC_CHECK_FUNCS(getpid kill sigsetjmp)
150
151ZEND_CHECK_FLOAT_PRECISION
152
153dnl Test whether double cast to long preserves least significant bits.
154AC_MSG_CHECKING(whether double cast to long preserves least significant bits)
155
156AC_RUN_IFELSE([AC_LANG_SOURCE([[
157#include <limits.h>
158#include <stdlib.h>
159
160int main()
161{
162	if (sizeof(long) == 4) {
163		double d = (double) LONG_MIN * LONG_MIN + 2e9;
164
165		if ((long) d == 2e9 && (long) -d == -2e9) {
166			return 0;
167		}
168	} else if (sizeof(long) == 8) {
169		double correct = 18e18 - ((double) LONG_MIN * -2); /* Subtract ULONG_MAX + 1 */
170
171		if ((long) 18e18 == correct) { /* On 64-bit, only check between LONG_MAX and ULONG_MAX */
172			return 0;
173		}
174	}
175	return 1;
176}
177]])], [
178  AC_DEFINE([ZEND_DVAL_TO_LVAL_CAST_OK], 1, [Define if double cast to long preserves least significant bits])
179  AC_MSG_RESULT(yes)
180], [
181  AC_MSG_RESULT(no)
182], [
183  AC_MSG_RESULT(no)
184])
185
186])
187
188dnl
189dnl LIBZEND_OTHER_CHECKS
190dnl
191AC_DEFUN([LIBZEND_OTHER_CHECKS],[
192
193AC_MSG_CHECKING(whether to enable thread-safety)
194AC_MSG_RESULT($ZEND_ZTS)
195
196AC_MSG_CHECKING(whether to enable Zend debugging)
197AC_MSG_RESULT($ZEND_DEBUG)
198
199if test "$ZEND_DEBUG" = "yes"; then
200  AC_DEFINE(ZEND_DEBUG,1,[ ])
201  echo " $CFLAGS" | grep ' -g' >/dev/null || DEBUG_CFLAGS="-g"
202  if test "$CFLAGS" = "-g -O2"; then
203  	CFLAGS=-g
204  fi
205else
206  AC_DEFINE(ZEND_DEBUG,0,[ ])
207fi
208
209test -n "$GCC" && CFLAGS="-Wall -Wextra -Wno-strict-aliasing -Wno-unused-parameter -Wno-sign-compare $CFLAGS"
210dnl Check if compiler supports -Wno-clobbered (only GCC)
211AX_CHECK_COMPILE_FLAG([-Wno-clobbered], CFLAGS="-Wno-clobbered $CFLAGS", , [-Werror])
212dnl Check for support for implicit fallthrough level 1, also add after previous CFLAGS as level 3 is enabled in -Wextra
213AX_CHECK_COMPILE_FLAG([-Wimplicit-fallthrough=1], CFLAGS="$CFLAGS -Wimplicit-fallthrough=1", , [-Werror])
214AX_CHECK_COMPILE_FLAG([-Wduplicated-cond], CFLAGS="-Wduplicated-cond $CFLAGS", , [-Werror])
215AX_CHECK_COMPILE_FLAG([-Wlogical-op], CFLAGS="-Wlogical-op $CFLAGS", , [-Werror])
216AX_CHECK_COMPILE_FLAG([-Wformat-truncation], CFLAGS="-Wformat-truncation $CFLAGS", , [-Werror])
217AX_CHECK_COMPILE_FLAG([-Wstrict-prototypes], CFLAGS="-Wstrict-prototypes $CFLAGS", , [-Werror])
218AX_CHECK_COMPILE_FLAG([-fno-common], CFLAGS="-fno-common $CFLAGS", , [-Werror])
219
220test -n "$DEBUG_CFLAGS" && CFLAGS="$CFLAGS $DEBUG_CFLAGS"
221
222if test "$ZEND_ZTS" = "yes"; then
223  AC_DEFINE(ZTS,1,[ ])
224  CFLAGS="$CFLAGS -DZTS"
225fi
226
227AC_C_INLINE
228
229AC_MSG_CHECKING(target system is Darwin)
230if echo "$target" | grep "darwin" > /dev/null; then
231  AC_DEFINE([DARWIN], 1, [Define if the target system is darwin])
232  AC_MSG_RESULT(yes)
233else
234  AC_MSG_RESULT(no)
235fi
236
237dnl Test and set the alignment define for ZEND_MM. This also does the
238dnl logarithmic test for ZEND_MM.
239AC_MSG_CHECKING(for MM alignment and log values)
240
241AC_RUN_IFELSE([AC_LANG_SOURCE([[
242#include <stdio.h>
243#include <stdlib.h>
244
245typedef union _mm_align_test {
246  void *ptr;
247  double dbl;
248  long lng;
249} mm_align_test;
250
251#if (defined (__GNUC__) && __GNUC__ >= 2)
252#define ZEND_MM_ALIGNMENT (__alignof__ (mm_align_test))
253#else
254#define ZEND_MM_ALIGNMENT (sizeof(mm_align_test))
255#endif
256
257int main()
258{
259  int i = ZEND_MM_ALIGNMENT;
260  int zeros = 0;
261  FILE *fp;
262
263  while (i & ~0x1) {
264    zeros++;
265    i = i >> 1;
266  }
267
268  fp = fopen("conftest.zend", "w");
269  fprintf(fp, "%d %d\n", ZEND_MM_ALIGNMENT, zeros);
270  fclose(fp);
271
272  return 0;
273}
274]])], [
275  LIBZEND_MM_ALIGN=`cat conftest.zend | cut -d ' ' -f 1`
276  LIBZEND_MM_ALIGN_LOG2=`cat conftest.zend | cut -d ' ' -f 2`
277  AC_DEFINE_UNQUOTED(ZEND_MM_ALIGNMENT, $LIBZEND_MM_ALIGN, [ ])
278  AC_DEFINE_UNQUOTED(ZEND_MM_ALIGNMENT_LOG2, $LIBZEND_MM_ALIGN_LOG2, [ ])
279], [], [
280  dnl Cross compilation needs something here.
281  AC_DEFINE_UNQUOTED(ZEND_MM_ALIGNMENT, 8, [ ])
282  AC_DEFINE_UNQUOTED(ZEND_MM_ALIGNMENT_LOG2, 3, [ ])
283])
284
285AC_MSG_RESULT(done)
286
287AC_CHECK_FUNCS(mremap)
288
289AC_ARG_ENABLE([zend-signals],
290  [AS_HELP_STRING([--disable-zend-signals],
291    [whether to enable zend signal handling])],
292  [ZEND_SIGNALS=$enableval],
293  [ZEND_SIGNALS=yes])
294
295AC_CHECK_FUNCS([sigaction], [], [
296  ZEND_SIGNALS=no
297])
298if test "$ZEND_SIGNALS" = "yes"; then
299	AC_DEFINE(ZEND_SIGNALS, 1, [Use zend signal handling])
300	CFLAGS="$CFLAGS -DZEND_SIGNALS"
301fi
302
303AC_MSG_CHECKING(whether to enable zend signal handling)
304AC_MSG_RESULT($ZEND_SIGNALS)
305
306])
307
308AC_MSG_CHECKING(whether /dev/urandom exists)
309if test -r "/dev/urandom" && test -c "/dev/urandom"; then
310  AC_DEFINE([HAVE_DEV_URANDOM], 1, [Define if the target system has /dev/urandom device])
311  AC_MSG_RESULT(yes)
312else
313  AC_MSG_RESULT(no)
314fi
315
316AC_ARG_ENABLE([gcc-global-regs],
317  [AS_HELP_STRING([--disable-gcc-global-regs],
318    [whether to enable GCC global register variables])],
319  [ZEND_GCC_GLOBAL_REGS=$enableval],
320  [ZEND_GCC_GLOBAL_REGS=yes])
321
322AC_MSG_CHECKING(for global register variables support)
323if test "$ZEND_GCC_GLOBAL_REGS" != "no"; then
324  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
325#if defined(__GNUC__)
326# define ZEND_GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
327#else
328# define ZEND_GCC_VERSION 0
329#endif
330#if defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(i386)
331# define ZEND_VM_FP_GLOBAL_REG "%esi"
332# define ZEND_VM_IP_GLOBAL_REG "%edi"
333#elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__x86_64__)
334# define ZEND_VM_FP_GLOBAL_REG "%r14"
335# define ZEND_VM_IP_GLOBAL_REG "%r15"
336#elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__powerpc64__)
337# define ZEND_VM_FP_GLOBAL_REG "r28"
338# define ZEND_VM_IP_GLOBAL_REG "r29"
339#elif defined(__IBMC__) && ZEND_GCC_VERSION >= 4002 && defined(__powerpc64__)
340# define ZEND_VM_FP_GLOBAL_REG "r28"
341# define ZEND_VM_IP_GLOBAL_REG "r29"
342#elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__aarch64__)
343# define ZEND_VM_FP_GLOBAL_REG "x27"
344# define ZEND_VM_IP_GLOBAL_REG "x28"
345#else
346# error "global register variables are not supported"
347#endif
348typedef int (*opcode_handler_t)(void);
349register void *FP  __asm__(ZEND_VM_FP_GLOBAL_REG);
350register const opcode_handler_t *IP __asm__(ZEND_VM_IP_GLOBAL_REG);
351int emu(const opcode_handler_t *ip, void *fp) {
352	const opcode_handler_t *orig_ip = IP;
353	void *orig_fp = FP;
354	IP = ip;
355	FP = fp;
356	while ((*ip)());
357	FP = orig_fp;
358	IP = orig_ip;
359}
360  ]], [[
361  ]])], [
362    ZEND_GCC_GLOBAL_REGS=yes
363  ], [
364    ZEND_GCC_GLOBAL_REGS=no
365  ])
366fi
367if test "$ZEND_GCC_GLOBAL_REGS" = "yes"; then
368  AC_DEFINE([HAVE_GCC_GLOBAL_REGS], 1, [Define if the target system has support for global register variables])
369else
370  HAVE_GCC_GLOBAL_REGS=no
371fi
372AC_MSG_RESULT($ZEND_GCC_GLOBAL_REGS)
373
374dnl Check whether __cpuid_count is available.
375AC_CACHE_CHECK(whether __cpuid_count is available, ac_cv_cpuid_count_available, [
376AC_LINK_IFELSE([AC_LANG_PROGRAM([[
377  #include <cpuid.h>
378]], [[
379  unsigned eax, ebx, ecx, edx;
380  __cpuid_count(0, 0, eax, ebx, ecx, edx);
381]])], [
382  ac_cv_cpuid_count_available=yes
383], [
384  ac_cv_cpuid_count_available=no
385])])
386if test "$ac_cv_cpuid_count_available" = "yes"; then
387  AC_DEFINE([HAVE_CPUID_COUNT], 1, [whether __cpuid_count is available])
388fi
389