Skip to content

Micro-optimize double comparison #11061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions Zend/zend_operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -2109,7 +2109,7 @@ ZEND_API int ZEND_FASTCALL numeric_compare_function(zval *op1, zval *op2) /* {{{
d1 = zval_get_double(op1);
d2 = zval_get_double(op2);

return ZEND_NORMALIZE_BOOL(d1 - d2);
return ZEND_THREEWAY_COMPARE(d1, d2);
}
/* }}} */

Expand All @@ -2131,8 +2131,7 @@ static int compare_long_to_string(zend_long lval, zend_string *str) /* {{{ */
}

if (type == IS_DOUBLE) {
double diff = (double) lval - str_dval;
return ZEND_NORMALIZE_BOOL(diff);
return ZEND_THREEWAY_COMPARE((double) lval, str_dval);
}

zend_string *lval_as_str = zend_long_to_str(lval);
Expand All @@ -2150,15 +2149,11 @@ static int compare_double_to_string(double dval, zend_string *str) /* {{{ */
uint8_t type = is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &str_lval, &str_dval, 0);

if (type == IS_LONG) {
double diff = dval - (double) str_lval;
return ZEND_NORMALIZE_BOOL(diff);
return ZEND_THREEWAY_COMPARE(dval, (double) str_lval);
}

if (type == IS_DOUBLE) {
if (dval == str_dval) {
return 0;
}
return ZEND_NORMALIZE_BOOL(dval - str_dval);
return ZEND_THREEWAY_COMPARE(dval, str_dval);
}

zend_string *dval_as_str = zend_double_to_str(dval);
Expand All @@ -2180,17 +2175,13 @@ ZEND_API int ZEND_FASTCALL zend_compare(zval *op1, zval *op2) /* {{{ */
return Z_LVAL_P(op1)>Z_LVAL_P(op2)?1:(Z_LVAL_P(op1)<Z_LVAL_P(op2)?-1:0);

case TYPE_PAIR(IS_DOUBLE, IS_LONG):
return ZEND_NORMALIZE_BOOL(Z_DVAL_P(op1) - (double)Z_LVAL_P(op2));
return ZEND_THREEWAY_COMPARE(Z_DVAL_P(op1), (double)Z_LVAL_P(op2));

case TYPE_PAIR(IS_LONG, IS_DOUBLE):
return ZEND_NORMALIZE_BOOL((double)Z_LVAL_P(op1) - Z_DVAL_P(op2));
return ZEND_THREEWAY_COMPARE((double)Z_LVAL_P(op1), Z_DVAL_P(op2));

case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE):
if (Z_DVAL_P(op1) == Z_DVAL_P(op2)) {
return 0;
} else {
return ZEND_NORMALIZE_BOOL(Z_DVAL_P(op1) - Z_DVAL_P(op2));
}
return ZEND_THREEWAY_COMPARE(Z_DVAL_P(op1), Z_DVAL_P(op2));

case TYPE_PAIR(IS_ARRAY, IS_ARRAY):
return zend_compare_arrays(op1, op2);
Expand Down
2 changes: 1 addition & 1 deletion ext/spl/spl_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ static int spl_ptr_pqueue_elem_cmp_long(void *x, void *y, zval *object) {
static int spl_ptr_pqueue_elem_cmp_double(void *x, void *y, zval *object) {
double a = Z_DVAL(((spl_pqueue_elem*) x)->priority);
double b = Z_DVAL(((spl_pqueue_elem*) y)->priority);
return ZEND_NORMALIZE_BOOL(a - b);
return ZEND_THREEWAY_COMPARE(a, b);
}

static spl_ptr_heap *spl_ptr_heap_init(spl_ptr_heap_cmp_func cmp, spl_ptr_heap_ctor_func ctor, spl_ptr_heap_dtor_func dtor, size_t elem_size) /* {{{ */
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ static zend_always_inline int php_array_key_compare_numeric_unstable_i(Bucket *f
} else {
d2 = (double)(zend_long)s->h;
}
return ZEND_NORMALIZE_BOOL(d1 - d2);
return ZEND_THREEWAY_COMPARE(d1, d2);
}
}
/* }}} */
Expand Down