xref: /php-src/Zend/zend_portability.h (revision 825509ee)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 2.00 of the Zend license,     |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.zend.com/license/2_00.txt.                                |
11    | If you did not receive a copy of the Zend license and are unable to  |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Authors: Andi Gutmans <andi@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    |          Dmitry Stogov <dmitry@php.net>                              |
18    +----------------------------------------------------------------------+
19 */
20 
21 #ifndef ZEND_PORTABILITY_H
22 #define ZEND_PORTABILITY_H
23 
24 #ifdef __cplusplus
25 #define BEGIN_EXTERN_C() extern "C" {
26 #define END_EXTERN_C() }
27 #else
28 #define BEGIN_EXTERN_C()
29 #define END_EXTERN_C()
30 #endif
31 
32 /*
33  * general definitions
34  */
35 
36 #ifdef ZEND_WIN32
37 # include "zend_config.w32.h"
38 # define ZEND_PATHS_SEPARATOR		';'
39 #elif defined(__riscos__)
40 # include <zend_config.h>
41 # define ZEND_PATHS_SEPARATOR		';'
42 #else
43 # include <zend_config.h>
44 # define ZEND_PATHS_SEPARATOR		':'
45 #endif
46 
47 #include "../TSRM/TSRM.h"
48 
49 #include <stdio.h>
50 #include <assert.h>
51 #include <math.h>
52 
53 #ifdef HAVE_UNIX_H
54 # include <unix.h>
55 #endif
56 
57 #include <stdarg.h>
58 #include <stddef.h>
59 
60 #ifdef HAVE_DLFCN_H
61 # include <dlfcn.h>
62 #endif
63 
64 #include <limits.h>
65 
66 #if defined(ZEND_WIN32) && !defined(__clang__)
67 #include <intrin.h>
68 #endif
69 
70 #include "zend_range_check.h"
71 
72 /* GCC x.y.z supplies __GNUC__ = x and __GNUC_MINOR__ = y */
73 #ifdef __GNUC__
74 # define ZEND_GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
75 #else
76 # define ZEND_GCC_VERSION 0
77 #endif
78 
79 /* Compatibility with non-clang compilers */
80 #ifndef __has_attribute
81 # define __has_attribute(x) 0
82 #endif
83 #ifndef __has_builtin
84 # define __has_builtin(x) 0
85 #endif
86 #ifndef __has_feature
87 # define __has_feature(x) 0
88 #endif
89 
90 #if defined(ZEND_WIN32) && !defined(__clang__)
91 # define ZEND_ASSUME(c)	__assume(c)
92 #elif defined(__clang__) && __has_builtin(__builtin_assume)
93 # pragma clang diagnostic ignored "-Wassume"
94 # define ZEND_ASSUME(c)	__builtin_assume(c)
95 #elif defined(PHP_HAVE_BUILTIN_UNREACHABLE) && defined(PHP_HAVE_BUILTIN_EXPECT)
96 # define ZEND_ASSUME(c)	do { \
97 		if (__builtin_expect(!(c), 0)) __builtin_unreachable(); \
98 	} while (0)
99 #else
100 # define ZEND_ASSUME(c)
101 #endif
102 
103 #if ZEND_DEBUG
104 # define ZEND_ASSERT(c)	assert(c)
105 #else
106 # define ZEND_ASSERT(c) ZEND_ASSUME(c)
107 #endif
108 
109 #ifdef PHP_HAVE_BUILTIN_UNREACHABLE
110 # define _ZEND_UNREACHABLE() __builtin_unreachable()
111 #else
112 # define _ZEND_UNREACHABLE() ZEND_ASSUME(0)
113 #endif
114 
115 #if ZEND_DEBUG
116 # define ZEND_UNREACHABLE() do {ZEND_ASSERT(0); _ZEND_UNREACHABLE();} while (0)
117 #else
118 # define ZEND_UNREACHABLE() _ZEND_UNREACHABLE()
119 #endif
120 
121 /* pseudo fallthrough keyword; */
122 #if defined(__GNUC__) && __GNUC__ >= 7
123 # define ZEND_FALLTHROUGH __attribute__((__fallthrough__))
124 #else
125 # define ZEND_FALLTHROUGH ((void)0)
126 #endif
127 
128 /* Only use this macro if you know for sure that all of the switches values
129    are covered by its case statements */
130 #define EMPTY_SWITCH_DEFAULT_CASE() default: ZEND_UNREACHABLE(); break;
131 
132 #if defined(__GNUC__) && __GNUC__ >= 4
133 # define ZEND_IGNORE_VALUE(x) (({ __typeof__ (x) __x = (x); (void) __x; }))
134 #else
135 # define ZEND_IGNORE_VALUE(x) ((void) (x))
136 #endif
137 
138 #define zend_quiet_write(...) ZEND_IGNORE_VALUE(write(__VA_ARGS__))
139 
140 /* all HAVE_XXX test have to be after the include of zend_config above */
141 
142 #if defined(HAVE_LIBDL) && !defined(ZEND_WIN32)
143 
144 # if __has_feature(address_sanitizer) && !defined(__SANITIZE_ADDRESS__)
145 #  define __SANITIZE_ADDRESS__
146 # endif
147 
148 # ifndef RTLD_LAZY
149 #  define RTLD_LAZY 1    /* Solaris 1, FreeBSD's (2.1.7.1 and older) */
150 # endif
151 
152 # ifndef RTLD_GLOBAL
153 #  define RTLD_GLOBAL 0
154 # endif
155 
156 # ifdef PHP_USE_RTLD_NOW
157 #  define PHP_RTLD_MODE  RTLD_NOW
158 # else
159 #  define PHP_RTLD_MODE  RTLD_LAZY
160 # endif
161 
162 # if defined(RTLD_GROUP) && defined(RTLD_WORLD) && defined(RTLD_PARENT)
163 #  define DL_LOAD(libname)			dlopen(libname, PHP_RTLD_MODE | RTLD_GLOBAL | RTLD_GROUP | RTLD_WORLD | RTLD_PARENT)
164 # elif defined(RTLD_DEEPBIND) && !defined(__SANITIZE_ADDRESS__) && !__has_feature(memory_sanitizer)
165 #  define DL_LOAD(libname)			dlopen(libname, PHP_RTLD_MODE | RTLD_GLOBAL | RTLD_DEEPBIND)
166 # else
167 #  define DL_LOAD(libname)			dlopen(libname, PHP_RTLD_MODE | RTLD_GLOBAL)
168 # endif
169 # define DL_UNLOAD					dlclose
170 # if defined(DLSYM_NEEDS_UNDERSCORE)
171 #  define DL_FETCH_SYMBOL(h,s)		dlsym((h), "_" s)
172 # else
173 #  define DL_FETCH_SYMBOL			dlsym
174 # endif
175 # define DL_ERROR					dlerror
176 # define DL_HANDLE					void *
177 # define ZEND_EXTENSIONS_SUPPORT	1
178 #elif defined(ZEND_WIN32)
179 # define DL_LOAD(libname)			LoadLibrary(libname)
180 # define DL_FETCH_SYMBOL			GetProcAddress
181 # define DL_UNLOAD					FreeLibrary
182 # define DL_HANDLE					HMODULE
183 # define ZEND_EXTENSIONS_SUPPORT	1
184 #else
185 # define DL_HANDLE					void *
186 # define ZEND_EXTENSIONS_SUPPORT	0
187 #endif
188 
189 #if defined(HAVE_ALLOCA_H) && !defined(_ALLOCA_H)
190 # include <alloca.h>
191 #endif
192 /* AIX requires this to be the first thing in the file.  */
193 #ifndef __GNUC__
194 # ifndef HAVE_ALLOCA_H
195 #  ifdef _AIX
196 #   pragma alloca
197 #  else
198 #   ifndef alloca /* predefined by HP cc +Olibcalls */
199 char *alloca();
200 #   endif
201 #  endif
202 # endif
203 #endif
204 
205 #if !ZEND_DEBUG && (defined(HAVE_ALLOCA) || (defined (__GNUC__) && __GNUC__ >= 2)) && !(defined(ZTS) && defined(HPUX)) && !defined(__APPLE__)
206 # define ZEND_ALLOCA_MAX_SIZE (32 * 1024)
207 # define ALLOCA_FLAG(name) \
208 	bool name;
209 # define SET_ALLOCA_FLAG(name) \
210 	name = true
211 # define do_alloca_ex(size, limit, use_heap) \
212 	((use_heap = (UNEXPECTED((size) > (limit)))) ? emalloc(size) : alloca(size))
213 # define do_alloca(size, use_heap) \
214 	do_alloca_ex(size, ZEND_ALLOCA_MAX_SIZE, use_heap)
215 # define free_alloca(p, use_heap) \
216 	do { if (UNEXPECTED(use_heap)) efree(p); } while (0)
217 #else
218 # define ALLOCA_FLAG(name)
219 # define SET_ALLOCA_FLAG(name)
220 # define do_alloca(p, use_heap)		emalloc(p)
221 # define free_alloca(p, use_heap)	efree(p)
222 #endif
223 
224 #if ZEND_GCC_VERSION >= 2096 || __has_attribute(__malloc__)
225 # define ZEND_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
226 #elif defined(ZEND_WIN32)
227 # define ZEND_ATTRIBUTE_MALLOC __declspec(allocator) __declspec(restrict)
228 #else
229 # define ZEND_ATTRIBUTE_MALLOC
230 #endif
231 
232 #if ZEND_GCC_VERSION >= 4003 || __has_attribute(alloc_size)
233 # define ZEND_ATTRIBUTE_ALLOC_SIZE(X) __attribute__ ((alloc_size(X)))
234 # define ZEND_ATTRIBUTE_ALLOC_SIZE2(X,Y) __attribute__ ((alloc_size(X,Y)))
235 #else
236 # define ZEND_ATTRIBUTE_ALLOC_SIZE(X)
237 # define ZEND_ATTRIBUTE_ALLOC_SIZE2(X,Y)
238 #endif
239 
240 #if ZEND_GCC_VERSION >= 3000
241 # define ZEND_ATTRIBUTE_CONST __attribute__((const))
242 #else
243 # define ZEND_ATTRIBUTE_CONST
244 #endif
245 
246 #if ZEND_GCC_VERSION >= 2007 || __has_attribute(format)
247 # define ZEND_ATTRIBUTE_FORMAT(type, idx, first) __attribute__ ((format(type, idx, first)))
248 #else
249 # define ZEND_ATTRIBUTE_FORMAT(type, idx, first)
250 #endif
251 
252 #if (ZEND_GCC_VERSION >= 3001 && !defined(__INTEL_COMPILER)) || __has_attribute(format)
253 # define ZEND_ATTRIBUTE_PTR_FORMAT(type, idx, first) __attribute__ ((format(type, idx, first)))
254 #else
255 # define ZEND_ATTRIBUTE_PTR_FORMAT(type, idx, first)
256 #endif
257 
258 #if ZEND_GCC_VERSION >= 3001 || __has_attribute(deprecated)
259 # define ZEND_ATTRIBUTE_DEPRECATED  __attribute__((deprecated))
260 #elif defined(ZEND_WIN32)
261 # define ZEND_ATTRIBUTE_DEPRECATED  __declspec(deprecated)
262 #else
263 # define ZEND_ATTRIBUTE_DEPRECATED
264 #endif
265 
266 #if ZEND_GCC_VERSION >= 4003 || __has_attribute(unused)
267 # define ZEND_ATTRIBUTE_UNUSED __attribute__((unused))
268 #else
269 # define ZEND_ATTRIBUTE_UNUSED
270 #endif
271 
272 #if ZEND_GCC_VERSION >= 3003 || __has_attribute(nonnull)
273 /* All pointer arguments must be non-null */
274 # define ZEND_ATTRIBUTE_NONNULL  __attribute__((nonnull))
275 /* Specified arguments must be non-null (1-based) */
276 # define ZEND_ATTRIBUTE_NONNULL_ARGS(...)  __attribute__((nonnull(__VA_ARGS__)))
277 #else
278 # define ZEND_ATTRIBUTE_NONNULL
279 # define ZEND_ATTRIBUTE_NONNULL_ARGS(...)
280 #endif
281 
282 #if defined(__GNUC__) && ZEND_GCC_VERSION >= 4003
283 # define ZEND_COLD __attribute__((cold))
284 # ifdef __OPTIMIZE__
285 #  define ZEND_OPT_SIZE  __attribute__((optimize("Os")))
286 #  define ZEND_OPT_SPEED __attribute__((optimize("Ofast")))
287 # else
288 #  define ZEND_OPT_SIZE
289 #  define ZEND_OPT_SPEED
290 # endif
291 #else
292 # define ZEND_COLD
293 # define ZEND_OPT_SIZE
294 # define ZEND_OPT_SPEED
295 #endif
296 
297 #if defined(__GNUC__) && ZEND_GCC_VERSION >= 5000
298 # define ZEND_ATTRIBUTE_UNUSED_LABEL __attribute__((unused));
299 # define ZEND_ATTRIBUTE_COLD_LABEL __attribute__((cold));
300 #else
301 # define ZEND_ATTRIBUTE_UNUSED_LABEL
302 # define ZEND_ATTRIBUTE_COLD_LABEL
303 #endif
304 
305 #if defined(__GNUC__) && ZEND_GCC_VERSION >= 3004 && defined(__i386__)
306 # define ZEND_FASTCALL __attribute__((fastcall))
307 #elif defined(_MSC_VER) && defined(_M_IX86) && _MSC_VER == 1700
308 # define ZEND_FASTCALL __fastcall
309 #elif defined(_MSC_VER) && _MSC_VER >= 1800
310 # define ZEND_FASTCALL __vectorcall
311 #else
312 # define ZEND_FASTCALL
313 #endif
314 
315 #if (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__INTEL_COMPILER) && !defined(__APPLE__) && !defined(__hpux) && !defined(_AIX) && !defined(__osf__)) || __has_attribute(noreturn)
316 # define HAVE_NORETURN
317 # define ZEND_NORETURN __attribute__((noreturn))
318 #elif defined(ZEND_WIN32)
319 # define HAVE_NORETURN
320 # define ZEND_NORETURN __declspec(noreturn)
321 #else
322 # define ZEND_NORETURN
323 #endif
324 
325 #if __has_attribute(force_align_arg_pointer)
326 # define ZEND_STACK_ALIGNED __attribute__((force_align_arg_pointer))
327 #else
328 # define ZEND_STACK_ALIGNED
329 #endif
330 
331 #if (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__INTEL_COMPILER) && !defined(__APPLE__) && !defined(__hpux) && !defined(_AIX) && !defined(__osf__))
332 # define HAVE_NORETURN_ALIAS
333 # define HAVE_ATTRIBUTE_WEAK
334 #endif
335 
336 #if ZEND_GCC_VERSION >= 3001 || __has_builtin(__builtin_constant_p)
337 # define HAVE_BUILTIN_CONSTANT_P
338 #endif
339 
340 #if __has_attribute(element_count)
341 #define ZEND_ELEMENT_COUNT(m) __attribute__((element_count(m)))
342 #elif __has_attribute(counted_by)
343 #define ZEND_ELEMENT_COUNT(m) __attribute__((counted_by(m)))
344 #else
345 #define ZEND_ELEMENT_COUNT(m)
346 #endif
347 
348 #ifdef HAVE_BUILTIN_CONSTANT_P
349 # define ZEND_CONST_COND(_condition, _default) \
350 	(__builtin_constant_p(_condition) ? (_condition) : (_default))
351 #else
352 # define ZEND_CONST_COND(_condition, _default) \
353 	(_default)
354 #endif
355 
356 #if ZEND_DEBUG || defined(ZEND_WIN32_NEVER_INLINE)
357 # define zend_always_inline inline
358 # define zend_never_inline
359 #else
360 # if defined(__GNUC__)
361 #  if __GNUC__ >= 3
362 #   define zend_always_inline inline __attribute__((always_inline))
363 #   define zend_never_inline __attribute__((noinline))
364 #  else
365 #   define zend_always_inline inline
366 #   define zend_never_inline
367 #  endif
368 # elif defined(_MSC_VER)
369 #  define zend_always_inline __forceinline
370 #  define zend_never_inline __declspec(noinline)
371 # else
372 #  if __has_attribute(always_inline)
373 #   define zend_always_inline inline __attribute__((always_inline))
374 #  else
375 #   define zend_always_inline inline
376 #  endif
377 #  if __has_attribute(noinline)
378 #   define zend_never_inline __attribute__((noinline))
379 #  else
380 #   define zend_never_inline
381 #  endif
382 # endif
383 #endif /* ZEND_DEBUG */
384 
385 #ifdef PHP_HAVE_BUILTIN_EXPECT
386 # define EXPECTED(condition)   __builtin_expect(!!(condition), 1)
387 # define UNEXPECTED(condition) __builtin_expect(!!(condition), 0)
388 #else
389 # define EXPECTED(condition)   (condition)
390 # define UNEXPECTED(condition) (condition)
391 #endif
392 
393 #ifndef XtOffsetOf
394 # define XtOffsetOf(s_type, field) offsetof(s_type, field)
395 #endif
396 
397 #ifndef ZEND_WIN32
398 # define SETJMP(a) sigsetjmp(a, 0)
399 # define LONGJMP(a,b) siglongjmp(a, b)
400 # define JMP_BUF sigjmp_buf
401 #else
402 # define SETJMP(a) setjmp(a)
403 # define LONGJMP(a,b) longjmp(a, b)
404 # define JMP_BUF jmp_buf
405 #endif
406 
407 #if ZEND_DEBUG
408 # define ZEND_FILE_LINE_D				const char *__zend_filename, const uint32_t __zend_lineno
409 # define ZEND_FILE_LINE_DC				, ZEND_FILE_LINE_D
410 # define ZEND_FILE_LINE_ORIG_D			const char *__zend_orig_filename, const uint32_t __zend_orig_lineno
411 # define ZEND_FILE_LINE_ORIG_DC			, ZEND_FILE_LINE_ORIG_D
412 # define ZEND_FILE_LINE_RELAY_C			__zend_filename, __zend_lineno
413 # define ZEND_FILE_LINE_RELAY_CC		, ZEND_FILE_LINE_RELAY_C
414 # define ZEND_FILE_LINE_C				__FILE__, __LINE__
415 # define ZEND_FILE_LINE_CC				, ZEND_FILE_LINE_C
416 # define ZEND_FILE_LINE_EMPTY_C			NULL, 0
417 # define ZEND_FILE_LINE_EMPTY_CC		, ZEND_FILE_LINE_EMPTY_C
418 # define ZEND_FILE_LINE_ORIG_RELAY_C	__zend_orig_filename, __zend_orig_lineno
419 # define ZEND_FILE_LINE_ORIG_RELAY_CC	, ZEND_FILE_LINE_ORIG_RELAY_C
420 #else
421 # define ZEND_FILE_LINE_D				void
422 # define ZEND_FILE_LINE_DC
423 # define ZEND_FILE_LINE_ORIG_D			void
424 # define ZEND_FILE_LINE_ORIG_DC
425 # define ZEND_FILE_LINE_RELAY_C
426 # define ZEND_FILE_LINE_RELAY_CC
427 # define ZEND_FILE_LINE_C
428 # define ZEND_FILE_LINE_CC
429 # define ZEND_FILE_LINE_EMPTY_C
430 # define ZEND_FILE_LINE_EMPTY_CC
431 # define ZEND_FILE_LINE_ORIG_RELAY_C
432 # define ZEND_FILE_LINE_ORIG_RELAY_CC
433 #endif	/* ZEND_DEBUG */
434 
435 #if ZEND_DEBUG
436 # define Z_DBG(expr)		(expr)
437 #else
438 # define Z_DBG(expr)
439 #endif
440 
441 #ifdef ZTS
442 # define ZTS_V 1
443 #else
444 # define ZTS_V 0
445 #endif
446 
447 #define MAX_LENGTH_OF_DOUBLE 32
448 
449 #undef MIN
450 #undef MAX
451 #define MAX(a, b)  (((a)>(b))?(a):(b))
452 #define MIN(a, b)  (((a)<(b))?(a):(b))
453 
454 #define ZEND_BIT_TEST(bits, bit) \
455 	(((bits)[(bit) / (sizeof((bits)[0])*8)] >> ((bit) & (sizeof((bits)[0])*8-1))) & 1)
456 
457 #define ZEND_INFINITY INFINITY
458 
459 #define ZEND_NAN NAN
460 
461 #if defined(__cplusplus) && __cplusplus >= 201103L
462 extern "C++" {
463 # include <cmath>
464 }
465 # define zend_isnan std::isnan
466 # define zend_isinf std::isinf
467 # define zend_finite std::isfinite
468 #else
469 # include <math.h>
470 # define zend_isnan(a) isnan(a)
471 # define zend_isinf(a) isinf(a)
472 # define zend_finite(a) isfinite(a)
473 #endif
474 
475 #define ZEND_STRL(str)		(str), (sizeof(str)-1)
476 #define ZEND_STRS(str)		(str), (sizeof(str))
477 #define ZEND_NORMALIZE_BOOL(n)			\
478 	((n) ? (((n)<0) ? -1 : 1) : 0)
479 #define ZEND_TRUTH(x)		((x) ? 1 : 0)
480 #define ZEND_LOG_XOR(a, b)		(ZEND_TRUTH(a) ^ ZEND_TRUTH(b))
481 
482 /**
483  * Do a three-way comparison of two integers and returns -1, 0 or 1
484  * depending on whether #a is smaller, equal or larger than #b.
485  */
486 #define ZEND_THREEWAY_COMPARE(a, b) ((a) == (b) ? 0 : ((a) < (b) ? -1 : 1))
487 
488 #define ZEND_MAX_RESERVED_RESOURCES	6
489 
490 /* excpt.h on Digital Unix 4.0 defines function_table */
491 #undef function_table
492 
493 #ifdef ZEND_WIN32
494 #define ZEND_SECURE_ZERO(var, size) RtlSecureZeroMemory((var), (size))
495 #else
496 #define ZEND_SECURE_ZERO(var, size) explicit_bzero((var), (size))
497 #endif
498 
499 /* This check should only be used on network socket, not file descriptors */
500 #ifdef ZEND_WIN32
501 #define ZEND_VALID_SOCKET(sock) (INVALID_SOCKET != (sock))
502 #else
503 #define ZEND_VALID_SOCKET(sock) ((sock) >= 0)
504 #endif
505 
506 /* Intrinsics macros start. */
507 
508 /* Memory sanitizer is incompatible with ifunc resolvers. Even if the resolver
509  * is marked as no_sanitize("memory") it will still be instrumented and crash. */
510 #if __has_feature(memory_sanitizer) || __has_feature(thread_sanitizer) || \
511 	__has_feature(dataflow_sanitizer)
512 # undef HAVE_FUNC_ATTRIBUTE_IFUNC
513 #endif
514 
515 /* Only use ifunc resolvers if we have __builtin_cpu_supports() and __builtin_cpu_init(),
516  * otherwise the use of zend_cpu_supports() may not be safe inside ifunc resolvers. */
517 #if defined(HAVE_FUNC_ATTRIBUTE_IFUNC) && defined(HAVE_FUNC_ATTRIBUTE_TARGET) && \
518 	defined(PHP_HAVE_BUILTIN_CPU_SUPPORTS) && defined(PHP_HAVE_BUILTIN_CPU_INIT)
519 # define ZEND_INTRIN_HAVE_IFUNC_TARGET 1
520 #endif
521 
522 #if (defined(__i386__) || defined(__x86_64__))
523 # if defined(HAVE_TMMINTRIN_H)
524 #  define PHP_HAVE_SSSE3
525 # endif
526 
527 # if defined(HAVE_NMMINTRIN_H)
528 #  define PHP_HAVE_SSE4_2
529 # endif
530 
531 # if defined(HAVE_WMMINTRIN_H)
532 #  define PHP_HAVE_PCLMUL
533 # endif
534 
535 /*
536  * AVX2 support was added in gcc 4.7, but AVX2 intrinsics don't work in
537  * __attribute__((target("avx2"))) functions until gcc 4.9.
538  */
539 # if defined(HAVE_IMMINTRIN_H) && \
540   (defined(__llvm__) || defined(__clang__) || (defined(__GNUC__) && ZEND_GCC_VERSION >= 4009))
541 #  define PHP_HAVE_AVX2
542 # endif
543 #endif
544 
545 #ifdef __SSSE3__
546 /* Instructions compiled directly. */
547 # define ZEND_INTRIN_SSSE3_NATIVE 1
548 #elif (defined(HAVE_FUNC_ATTRIBUTE_TARGET) && defined(PHP_HAVE_SSSE3)) || (defined(ZEND_WIN32) && (!defined(_M_ARM64)))
549 /* Function resolved by ifunc or MINIT. */
550 # define ZEND_INTRIN_SSSE3_RESOLVER 1
551 #endif
552 
553 /* Do not use for conditional declaration of API functions! */
554 #if defined(ZEND_INTRIN_SSSE3_RESOLVER) && defined(ZEND_INTRIN_HAVE_IFUNC_TARGET)
555 # define ZEND_INTRIN_SSSE3_FUNC_PROTO 1
556 #elif defined(ZEND_INTRIN_SSSE3_RESOLVER)
557 # define ZEND_INTRIN_SSSE3_FUNC_PTR 1
558 #endif
559 
560 #ifdef ZEND_INTRIN_SSSE3_RESOLVER
561 # ifdef HAVE_FUNC_ATTRIBUTE_TARGET
562 #  define ZEND_INTRIN_SSSE3_FUNC_DECL(func) ZEND_API func __attribute__((target("ssse3")))
563 # else
564 #  define ZEND_INTRIN_SSSE3_FUNC_DECL(func) func
565 # endif
566 #else
567 # define ZEND_INTRIN_SSSE3_FUNC_DECL(func)
568 #endif
569 
570 #ifdef __SSE4_2__
571 /* Instructions compiled directly. */
572 # define ZEND_INTRIN_SSE4_2_NATIVE 1
573 #elif (defined(HAVE_FUNC_ATTRIBUTE_TARGET) && defined(PHP_HAVE_SSE4_2)) || (defined(ZEND_WIN32) && (!defined(_M_ARM64)))
574 /* Function resolved by ifunc or MINIT. */
575 # define ZEND_INTRIN_SSE4_2_RESOLVER 1
576 #endif
577 
578 /* Do not use for conditional declaration of API functions! */
579 #if defined(ZEND_INTRIN_SSE4_2_RESOLVER) && defined(ZEND_INTRIN_HAVE_IFUNC_TARGET)
580 # define ZEND_INTRIN_SSE4_2_FUNC_PROTO 1
581 #elif defined(ZEND_INTRIN_SSE4_2_RESOLVER)
582 # define ZEND_INTRIN_SSE4_2_FUNC_PTR 1
583 #endif
584 
585 #ifdef ZEND_INTRIN_SSE4_2_RESOLVER
586 # ifdef HAVE_FUNC_ATTRIBUTE_TARGET
587 #  define ZEND_INTRIN_SSE4_2_FUNC_DECL(func) ZEND_API func __attribute__((target("sse4.2")))
588 # else
589 #  define ZEND_INTRIN_SSE4_2_FUNC_DECL(func) func
590 # endif
591 #else
592 # define ZEND_INTRIN_SSE4_2_FUNC_DECL(func)
593 #endif
594 
595 #ifdef __PCLMUL__
596 /* Instructions compiled directly. */
597 # define ZEND_INTRIN_PCLMUL_NATIVE 1
598 #elif (defined(HAVE_FUNC_ATTRIBUTE_TARGET) && defined(PHP_HAVE_PCLMUL)) || (defined(ZEND_WIN32) && (!defined(_M_ARM64)))
599 /* Function resolved by ifunc or MINIT. */
600 # define ZEND_INTRIN_PCLMUL_RESOLVER 1
601 #endif
602 
603 /* Do not use for conditional declaration of API functions! */
604 #if defined(ZEND_INTRIN_PCLMUL_RESOLVER) && defined(ZEND_INTRIN_HAVE_IFUNC_TARGET) && (!defined(__GNUC__) || (ZEND_GCC_VERSION >= 9000))
605 /* __builtin_cpu_supports has pclmul from gcc9 */
606 # define ZEND_INTRIN_PCLMUL_FUNC_PROTO 1
607 #elif defined(ZEND_INTRIN_PCLMUL_RESOLVER)
608 # define ZEND_INTRIN_PCLMUL_FUNC_PTR 1
609 #endif
610 
611 #ifdef ZEND_INTRIN_PCLMUL_RESOLVER
612 # ifdef HAVE_FUNC_ATTRIBUTE_TARGET
613 #  define ZEND_INTRIN_PCLMUL_FUNC_DECL(func) ZEND_API func __attribute__((target("pclmul")))
614 # else
615 #  define ZEND_INTRIN_PCLMUL_FUNC_DECL(func) func
616 # endif
617 #else
618 # define ZEND_INTRIN_PCLMUL_FUNC_DECL(func)
619 #endif
620 
621 #if defined(ZEND_INTRIN_SSE4_2_NATIVE) && defined(ZEND_INTRIN_PCLMUL_NATIVE)
622 /* Instructions compiled directly. */
623 # define ZEND_INTRIN_SSE4_2_PCLMUL_NATIVE 1
624 #elif (defined(HAVE_FUNC_ATTRIBUTE_TARGET) && defined(PHP_HAVE_SSE4_2) && defined(PHP_HAVE_PCLMUL)) || (defined(ZEND_WIN32) && (!defined(_M_ARM64)))
625 /* Function resolved by ifunc or MINIT. */
626 # define ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER 1
627 #endif
628 
629 /* Do not use for conditional declaration of API functions! */
630 #if defined(ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER) && defined(ZEND_INTRIN_HAVE_IFUNC_TARGET) && (!defined(__GNUC__) || (ZEND_GCC_VERSION >= 9000))
631 /* __builtin_cpu_supports has pclmul from gcc9 */
632 # define ZEND_INTRIN_SSE4_2_PCLMUL_FUNC_PROTO 1
633 #elif defined(ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER)
634 # define ZEND_INTRIN_SSE4_2_PCLMUL_FUNC_PTR 1
635 #endif
636 
637 #ifdef ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER
638 # ifdef HAVE_FUNC_ATTRIBUTE_TARGET
639 #  define ZEND_INTRIN_SSE4_2_PCLMUL_FUNC_DECL(func) ZEND_API func __attribute__((target("sse4.2,pclmul")))
640 # else
641 #  define ZEND_INTRIN_SSE4_2_PCLMUL_FUNC_DECL(func) func
642 # endif
643 #else
644 # define ZEND_INTRIN_SSE4_2_PCLMUL_FUNC_DECL(func)
645 #endif
646 
647 #ifdef __AVX2__
648 # define ZEND_INTRIN_AVX2_NATIVE 1
649 #elif (defined(HAVE_FUNC_ATTRIBUTE_TARGET) && defined(PHP_HAVE_AVX2)) || (defined(ZEND_WIN32) && (!defined(_M_ARM64)))
650 # define ZEND_INTRIN_AVX2_RESOLVER 1
651 #endif
652 
653 /* Do not use for conditional declaration of API functions! */
654 #if defined(ZEND_INTRIN_AVX2_RESOLVER) && defined(ZEND_INTRIN_HAVE_IFUNC_TARGET)
655 # define ZEND_INTRIN_AVX2_FUNC_PROTO 1
656 #elif defined(ZEND_INTRIN_AVX2_RESOLVER)
657 # define ZEND_INTRIN_AVX2_FUNC_PTR 1
658 #endif
659 
660 #ifdef ZEND_INTRIN_AVX2_RESOLVER
661 # ifdef HAVE_FUNC_ATTRIBUTE_TARGET
662 #  define ZEND_INTRIN_AVX2_FUNC_DECL(func) ZEND_API func __attribute__((target("avx2")))
663 # else
664 #  define ZEND_INTRIN_AVX2_FUNC_DECL(func) func
665 # endif
666 #else
667 # define ZEND_INTRIN_AVX2_FUNC_DECL(func)
668 #endif
669 
670 #if defined(PHP_HAVE_AVX512_SUPPORTS) && defined(HAVE_FUNC_ATTRIBUTE_TARGET) || defined(ZEND_WIN32)
671 #define ZEND_INTRIN_AVX512_RESOLVER 1
672 #endif
673 
674 #if defined(ZEND_INTRIN_AVX512_RESOLVER) && defined(ZEND_INTRIN_HAVE_IFUNC_TARGET)
675 # define ZEND_INTRIN_AVX512_FUNC_PROTO 1
676 #elif defined(ZEND_INTRIN_AVX512_RESOLVER)
677 # define ZEND_INTRIN_AVX512_FUNC_PTR 1
678 #endif
679 
680 #ifdef ZEND_INTRIN_AVX512_RESOLVER
681 # ifdef HAVE_FUNC_ATTRIBUTE_TARGET
682 #  define ZEND_INTRIN_AVX512_FUNC_DECL(func) ZEND_API func __attribute__((target("avx512f,avx512cd,avx512vl,avx512dq,avx512bw")))
683 # else
684 #  define ZEND_INTRIN_AVX512_FUNC_DECL(func) func
685 # endif
686 #else
687 # define ZEND_INTRIN_AVX512_FUNC_DECL(func)
688 #endif
689 
690 #if defined(PHP_HAVE_AVX512_VBMI_SUPPORTS) && defined(HAVE_FUNC_ATTRIBUTE_TARGET)
691 #define ZEND_INTRIN_AVX512_VBMI_RESOLVER 1
692 #endif
693 
694 #if defined(ZEND_INTRIN_AVX512_VBMI_RESOLVER) && defined(ZEND_INTRIN_HAVE_IFUNC_TARGET)
695 # define ZEND_INTRIN_AVX512_VBMI_FUNC_PROTO 1
696 #elif defined(ZEND_INTRIN_AVX512_VBMI_RESOLVER)
697 # define ZEND_INTRIN_AVX512_VBMI_FUNC_PTR 1
698 #endif
699 
700 #ifdef ZEND_INTRIN_AVX512_VBMI_RESOLVER
701 # ifdef HAVE_FUNC_ATTRIBUTE_TARGET
702 #  define ZEND_INTRIN_AVX512_VBMI_FUNC_DECL(func) ZEND_API func __attribute__((target("avx512f,avx512cd,avx512vl,avx512dq,avx512bw,avx512vbmi")))
703 # else
704 #  define ZEND_INTRIN_AVX512_VBMI_FUNC_DECL(func) func
705 # endif
706 #else
707 # define ZEND_INTRIN_AVX512_VBMI_FUNC_DECL(func)
708 #endif
709 
710 /* Intrinsics macros end. */
711 
712 #ifdef ZEND_WIN32
713 # define ZEND_SET_ALIGNED(alignment, decl) __declspec(align(alignment)) decl
714 #elif defined(HAVE_ATTRIBUTE_ALIGNED)
715 # define ZEND_SET_ALIGNED(alignment, decl) decl __attribute__ ((__aligned__ (alignment)))
716 #else
717 # define ZEND_SET_ALIGNED(alignment, decl) decl
718 #endif
719 
720 #define ZEND_SLIDE_TO_ALIGNED(alignment, ptr) (((uintptr_t)(ptr) + ((alignment)-1)) & ~((alignment)-1))
721 #define ZEND_SLIDE_TO_ALIGNED16(ptr) ZEND_SLIDE_TO_ALIGNED(Z_UL(16), ptr)
722 
723 #ifdef ZEND_WIN32
724 # define _ZEND_EXPAND_VA(a) a
725 # define ZEND_EXPAND_VA(code) _ZEND_EXPAND_VA(code)
726 #else
727 # define ZEND_EXPAND_VA(code) code
728 #endif
729 
730 /* On CPU with few registers, it's cheaper to reload value then use spill slot */
731 #if defined(__i386__) || (defined(_WIN32) && !defined(_WIN64))
732 # define ZEND_PREFER_RELOAD
733 #endif
734 
735 #if defined(ZEND_WIN32) && defined(_DEBUG)
736 # define ZEND_IGNORE_LEAKS_BEGIN() _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) & ~_CRTDBG_ALLOC_MEM_DF)
737 # define ZEND_IGNORE_LEAKS_END() _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_ALLOC_MEM_DF)
738 #else
739 # define ZEND_IGNORE_LEAKS_BEGIN()
740 # define ZEND_IGNORE_LEAKS_END()
741 #endif
742 
743 /* MSVC yields C4090 when a (const T **) is passed to a (void *); ZEND_VOIDP works around that */
744 #ifdef _MSC_VER
745 # define ZEND_VOIDP(ptr) ((void *) ptr)
746 #else
747 # define ZEND_VOIDP(ptr) (ptr)
748 #endif
749 
750 #if __has_attribute(__indirect_return__)
751 # define ZEND_INDIRECT_RETURN __attribute__((__indirect_return__))
752 #else
753 # define ZEND_INDIRECT_RETURN
754 #endif
755 
756 #define __ZEND_DO_PRAGMA(x) _Pragma(#x)
757 #define _ZEND_DO_PRAGMA(x) __ZEND_DO_PRAGMA(x)
758 #if defined(__clang__)
759 # define ZEND_DIAGNOSTIC_IGNORED_START(warning) \
760 	_Pragma("clang diagnostic push") \
761 	_ZEND_DO_PRAGMA(clang diagnostic ignored warning)
762 # define ZEND_DIAGNOSTIC_IGNORED_END \
763 	_Pragma("clang diagnostic pop")
764 #elif defined(__GNUC__)
765 # define ZEND_DIAGNOSTIC_IGNORED_START(warning) \
766 	_Pragma("GCC diagnostic push") \
767 	_ZEND_DO_PRAGMA(GCC diagnostic ignored warning)
768 # define ZEND_DIAGNOSTIC_IGNORED_END \
769 	_Pragma("GCC diagnostic pop")
770 #else
771 # define ZEND_DIAGNOSTIC_IGNORED_START(warning)
772 # define ZEND_DIAGNOSTIC_IGNORED_END
773 #endif
774 
775 /** @deprecated */
776 #define ZEND_CGG_DIAGNOSTIC_IGNORED_START ZEND_DIAGNOSTIC_IGNORED_START
777 /** @deprecated */
778 #define ZEND_CGG_DIAGNOSTIC_IGNORED_END ZEND_DIAGNOSTIC_IGNORED_END
779 
780 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */
781 # define ZEND_STATIC_ASSERT(c, m) _Static_assert((c), m)
782 #else
783 # define ZEND_STATIC_ASSERT(c, m)
784 #endif
785 
786 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) /* C11 */
787 typedef max_align_t zend_max_align_t;
788 #elif (defined(__cplusplus) && __cplusplus >= 201103L) /* C++11 */
789 extern "C++" {
790 # include <cstddef>
791 }
792 typedef std::max_align_t zend_max_align_t;
793 #else
794 typedef union {
795 	char c;
796 	short s;
797 	int i;
798 	long l;
799 #if SIZEOF_LONG_LONG
800 	long long ll;
801 #endif
802 	float f;
803 	double d;
804 	long double ld;
805 	void *p;
806 	void (*fun)(void);
807 } zend_max_align_t;
808 #endif
809 
810 /* Bytes swap */
811 #ifdef _MSC_VER
812 #  include <stdlib.h>
813 #  define ZEND_BYTES_SWAP32(u) _byteswap_ulong(u)
814 #  define ZEND_BYTES_SWAP64(u) _byteswap_uint64(u)
815 #elif defined(HAVE_BYTESWAP_H)
816 #  include <byteswap.h>
817 #  define ZEND_BYTES_SWAP32(u) bswap_32(u)
818 #  define ZEND_BYTES_SWAP64(u) bswap_64(u)
819 #elif defined(HAVE_SYS_BSWAP_H)
820 #  include <sys/bswap.h>
821 #  define ZEND_BYTES_SWAP32(u) bswap32(u)
822 #  define ZEND_BYTES_SWAP64(u) bswap64(u)
823 #elif defined(__GNUC__)
824 #  define ZEND_BYTES_SWAP32(u) __builtin_bswap32(u)
825 #  define ZEND_BYTES_SWAP64(u) __builtin_bswap64(u)
826 #elif defined(__has_builtin)
827 #  if __has_builtin(__builtin_bswap32)
828 #    define ZEND_BYTES_SWAP32(u) __builtin_bswap32(u)
829 #  endif
830 #  if __has_builtin(__builtin_bswap64)
831 #    define ZEND_BYTES_SWAP64(u) __builtin_bswap64(u)
832 #  endif
833 #endif
834 
835 #ifndef ZEND_BYTES_SWAP32
ZEND_BYTES_SWAP32(uint32_t u)836 static zend_always_inline uint32_t ZEND_BYTES_SWAP32(uint32_t u)
837 {
838   return (((u & 0xff000000) >> 24)
839           | ((u & 0x00ff0000) >>  8)
840           | ((u & 0x0000ff00) <<  8)
841           | ((u & 0x000000ff) << 24));
842 }
843 #endif
844 #ifndef ZEND_BYTES_SWAP64
ZEND_BYTES_SWAP64(uint64_t u)845 static zend_always_inline uint64_t ZEND_BYTES_SWAP64(uint64_t u)
846 {
847    return (((u & 0xff00000000000000ULL) >> 56)
848           | ((u & 0x00ff000000000000ULL) >> 40)
849           | ((u & 0x0000ff0000000000ULL) >> 24)
850           | ((u & 0x000000ff00000000ULL) >>  8)
851           | ((u & 0x00000000ff000000ULL) <<  8)
852           | ((u & 0x0000000000ff0000ULL) << 24)
853           | ((u & 0x000000000000ff00ULL) << 40)
854           | ((u & 0x00000000000000ffULL) << 56));
855 }
856 #endif
857 
858 #endif /* ZEND_PORTABILITY_H */
859