xref: /PHP-5.5/Zend/zend_multiply.h (revision 73c1be26)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2015 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: Sascha Schumann <sascha@schumann.cx>                        |
16    |          Ard Biesheuvel <ard.biesheuvel@linaro.org>                  |
17    +----------------------------------------------------------------------+
18 */
19 
20 /* $Id$ */
21 
22 #if defined(__i386__) && defined(__GNUC__)
23 
24 #define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do {	\
25 	long __tmpvar; 													\
26 	__asm__ ("imul %3,%0\n"											\
27 		"adc $0,%1" 												\
28 			: "=r"(__tmpvar),"=r"(usedval) 							\
29 			: "0"(a), "r"(b), "1"(0));								\
30 	if (usedval) (dval) = (double) (a) * (double) (b);				\
31 	else (lval) = __tmpvar;											\
32 } while (0)
33 
34 #elif defined(__x86_64__) && defined(__GNUC__)
35 
36 #define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do {	\
37 	long __tmpvar; 													\
38 	__asm__ ("imul %3,%0\n"											\
39 		"adc $0,%1" 												\
40 			: "=r"(__tmpvar),"=r"(usedval) 							\
41 			: "0"(a), "r"(b), "1"(0));								\
42 	if (usedval) (dval) = (double) (a) * (double) (b);				\
43 	else (lval) = __tmpvar;											\
44 } while (0)
45 
46 #elif defined(__arm__) && defined(__GNUC__)
47 
48 #define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do {	\
49 	long __tmpvar; 													\
50 	__asm__("smull %0, %1, %2, %3\n"								\
51 		"sub %1, %1, %0, asr #31"									\
52 			: "=r"(__tmpvar), "=r"(usedval)							\
53 			: "r"(a), "r"(b));										\
54 	if (usedval) (dval) = (double) (a) * (double) (b);				\
55 	else (lval) = __tmpvar;											\
56 } while (0)
57 
58 #elif defined(__aarch64__) && defined(__GNUC__)
59 
60 #define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do {	\
61 	long __tmpvar; 													\
62 	__asm__("mul %0, %2, %3\n"										\
63 		"smulh %1, %2, %3\n"										\
64 		"sub %1, %1, %0, asr #63\n"									\
65 			: "=X"(__tmpvar), "=X"(usedval)							\
66 			: "X"(a), "X"(b));										\
67 	if (usedval) (dval) = (double) (a) * (double) (b);				\
68 	else (lval) = __tmpvar;											\
69 } while (0)
70 
71 #elif SIZEOF_LONG == 4 && defined(HAVE_ZEND_LONG64)
72 
73 #define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do {	\
74 	zend_long64 __result = (zend_long64) (a) * (zend_long64) (b);	\
75 	if (__result > LONG_MAX || __result < LONG_MIN) {				\
76 		(dval) = (double) __result;									\
77 		(usedval) = 1;												\
78 	} else {														\
79 		(lval) = (long) __result;									\
80 		(usedval) = 0;												\
81 	}																\
82 } while (0)
83 
84 #else
85 
86 #define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do {	\
87 	long   __lres  = (a) * (b);										\
88 	long double __dres  = (long double)(a) * (long double)(b);		\
89 	long double __delta = (long double) __lres - __dres;			\
90 	if ( ((usedval) = (( __dres + __delta ) != __dres))) {			\
91 		(dval) = __dres;											\
92 	} else {														\
93 		(lval) = __lres;											\
94 	}																\
95 } while (0)
96 
97 #endif
98