com php-src: Use checked arithmetic intrinsics instead of asm, when possible: Zend/zend_multiply.h Zend/zend_operators.h

From: Date: Tue, 09 Aug 2016 23:46:05 +0000
Subject: com php-src: Use checked arithmetic intrinsics instead of asm, when possible: Zend/zend_multiply.h Zend/zend_operators.h
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-93743@lists.php.net to get a copy of this message
Commit: 0987737397c28f8df48a5085cb0d5e857962af93 Author: Andrea Faulds <ajf@ajf.me> Wed, 10 Aug 2016 00:15:13 +0100 Parents: f41b69e814b8b23509a9c15de4fca5ea59dc80cb Branches: master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=0987737397c28f8df48a5085cb0d5e857962af93 Log: Use checked arithmetic intrinsics instead of asm, when possible Changed paths: M Zend/zend_multiply.h M Zend/zend_operators.h Diff: diff --git a/Zend/zend_multiply.h b/Zend/zend_multiply.h index dfd21f7..7fb500a 100644 --- a/Zend/zend_multiply.h +++ b/Zend/zend_multiply.h @@ -19,10 +19,32 @@ /* $Id$ */ +#include "zend_portability.h" + #ifndef ZEND_MULTIPLY_H #define ZEND_MULTIPLY_H -#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) +#if __has_builtin(__builtin_smull_overflow) && SIZEOF_LONG == SIZEOF_ZEND_LONG + +#define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do { \ + long __tmpvar; \ + if (((usedval) = __builtin_smull_overflow((a), (b), &__tmpvar))) { \ + (dval) = (double) (a) * (double) (b); \ + } \ + else (lval) = __tmpvar; \ +} while (0) + +#elif __has_builtin(__builtin_smulll_overflow) && SIZEOF_LONG_LONG == SIZEOF_ZEND_LONG + +#define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do { \ + long long __tmpvar; \ + if (((usedval) = __builtin_smulll_overflow((a), (b), &__tmpvar))) { \ + (dval) = (double) (a) * (double) (b); \ + } \ + else (lval) = __tmpvar; \ +} while (0) + +#elif (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) #define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do { \ zend_long __tmpvar; \ diff --git a/Zend/zend_operators.h b/Zend/zend_operators.h index 3705022..d4bdb7f 100644 --- a/Zend/zend_operators.h +++ b/Zend/zend_operators.h @@ -35,6 +35,7 @@ #include <ieeefp.h> #endif +#include "zend_portability.h" #include "zend_strtod.h" #include "zend_multiply.h" @@ -520,7 +521,21 @@ static zend_always_inline void fast_long_decrement_function(zval *op1) static zend_always_inline void fast_long_add_function(zval *result, zval *op1, zval *op2) { -#if defined(__GNUC__) && defined(__i386__) && !(4 == __GNUC__ && 8 == __GNUC_MINOR__) +#if __has_builtin(__builtin_saddl_overflow) && SIZEOF_LONG == SIZEOF_ZEND_LONG + long lresult; + if (UNEXPECTED(__builtin_saddl_overflow(Z_LVAL_P(op1), Z_LVAL_P(op2), &lresult))) { + ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2)); + } else { + ZVAL_LONG(result, lresult); + } +#elif __has_builtin(__builtin_saddll_overflow) && SIZEOF_LONG_LONG == SIZEOF_ZEND_LONG + long long llresult; + if (UNEXPECTED(__builtin_saddl_overflow(Z_LVAL_P(op1), Z_LVAL_P(op2), &llresult))) { + ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2)); + } else { + ZVAL_LONG(result, llresult); + } +#elif defined(__GNUC__) && defined(__i386__) && !(4 == __GNUC__ && 8 == __GNUC_MINOR__) __asm__( "movl (%1), %%eax\n\t" "addl (%2), %%eax\n\t" @@ -606,7 +621,21 @@ static zend_always_inline int fast_add_function(zval *result, zval *op1, zval *o static zend_always_inline void fast_long_sub_function(zval *result, zval *op1, zval *op2) { -#if defined(__GNUC__) && defined(__i386__) && !(4 == __GNUC__ && 8 == __GNUC_MINOR__) +#if __has_builtin(__builtin_ssubl_overflow) && SIZEOF_LONG == SIZEOF_ZEND_LONG + long lresult; + if (UNEXPECTED(__builtin_ssubl_overflow(Z_LVAL_P(op1), Z_LVAL_P(op2), &lresult))) { + ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2)); + } else { + ZVAL_LONG(result, lresult); + } +#elif __has_builtin(__builtin_ssubll_overflow) && SIZEOF_LONG_LONG == SIZEOF_ZEND_LONG + long long llresult; + if (UNEXPECTED(__builtin_ssubl_overflow(Z_LVAL_P(op1), Z_LVAL_P(op2), &llresult))) { + ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2)); + } else { + ZVAL_LONG(result, llresult); + } +#elif defined(__GNUC__) && defined(__i386__) && !(4 == __GNUC__ && 8 == __GNUC_MINOR__) __asm__( "movl (%1), %%eax\n\t" "subl (%2), %%eax\n\t"

« previous php.cvs (#93743) next »