Skip to content

Commit 4e73214

Browse files
committed
Add frameless handlers for min/max with arity 2
1 parent 088f1c6 commit 4e73214

File tree

3 files changed

+101
-5
lines changed

3 files changed

+101
-5
lines changed

ext/standard/array.c

+78
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,45 @@ PHP_FUNCTION(min)
12941294
}
12951295
/* }}} */
12961296

1297+
ZEND_FRAMELESS_FUNCTION(min, 2)
1298+
{
1299+
zval *lhs, *rhs;
1300+
1301+
Z_FLF_PARAM_ZVAL(1, lhs);
1302+
Z_FLF_PARAM_ZVAL(2, rhs);
1303+
1304+
double lhs_dval;
1305+
1306+
if (Z_TYPE_P(lhs) == IS_LONG) {
1307+
zend_long lhs_lval = Z_LVAL_P(lhs);
1308+
1309+
if (EXPECTED(Z_TYPE_P(rhs) == IS_LONG)) {
1310+
RETURN_COPY_VALUE(lhs_lval < Z_LVAL_P(rhs) ? lhs : rhs);
1311+
} else if (Z_TYPE_P(rhs) == IS_DOUBLE && (zend_dval_to_lval((double) lhs_lval) == lhs_lval)) {
1312+
/* if lhs_lval can be exactly represented as a double, go to double dedicated code */
1313+
lhs_dval = (double) lhs_lval;
1314+
goto double_compare;
1315+
} else {
1316+
goto generic_compare;
1317+
}
1318+
} else if (Z_TYPE_P(lhs) == IS_DOUBLE) {
1319+
lhs_dval = Z_DVAL_P(lhs);
1320+
1321+
if (EXPECTED(Z_TYPE_P(rhs) == IS_DOUBLE)) {
1322+
double_compare:
1323+
RETURN_COPY_VALUE(lhs_dval < Z_DVAL_P(rhs) ? lhs : rhs);
1324+
} else if (Z_TYPE_P(rhs) == IS_LONG && (zend_dval_to_lval((double) Z_LVAL_P(rhs)) == Z_LVAL_P(rhs))) {
1325+
/* if the value can be exactly represented as a double, use double dedicated code otherwise generic */
1326+
RETURN_COPY_VALUE(lhs_dval < (double)Z_LVAL_P(rhs) ? lhs : rhs);
1327+
} else {
1328+
goto generic_compare;
1329+
}
1330+
} else {
1331+
generic_compare:
1332+
RETURN_COPY(zend_compare(lhs, rhs) < 0 ? lhs : rhs);
1333+
}
1334+
}
1335+
12971336
/* {{{
12981337
* proto mixed max(array values)
12991338
* proto mixed max(mixed arg1 [, mixed arg2 [, mixed ...]])
@@ -1383,6 +1422,45 @@ PHP_FUNCTION(max)
13831422
}
13841423
/* }}} */
13851424

1425+
ZEND_FRAMELESS_FUNCTION(max, 2)
1426+
{
1427+
zval *lhs, *rhs;
1428+
1429+
Z_FLF_PARAM_ZVAL(1, lhs);
1430+
Z_FLF_PARAM_ZVAL(2, rhs);
1431+
1432+
double lhs_dval;
1433+
1434+
if (Z_TYPE_P(lhs) == IS_LONG) {
1435+
zend_long lhs_lval = Z_LVAL_P(lhs);
1436+
1437+
if (EXPECTED(Z_TYPE_P(rhs) == IS_LONG)) {
1438+
RETURN_COPY_VALUE(lhs_lval >= Z_LVAL_P(rhs) ? lhs : rhs);
1439+
} else if (Z_TYPE_P(rhs) == IS_DOUBLE && (zend_dval_to_lval((double) lhs_lval) == lhs_lval)) {
1440+
/* if lhs_lval can be exactly represented as a double, go to double dedicated code */
1441+
lhs_dval = (double) lhs_lval;
1442+
goto double_compare;
1443+
} else {
1444+
goto generic_compare;
1445+
}
1446+
} else if (Z_TYPE_P(lhs) == IS_DOUBLE) {
1447+
lhs_dval = Z_DVAL_P(lhs);
1448+
1449+
if (EXPECTED(Z_TYPE_P(rhs) == IS_DOUBLE)) {
1450+
double_compare:
1451+
RETURN_COPY_VALUE(lhs_dval >= Z_DVAL_P(rhs) ? lhs : rhs);
1452+
} else if (Z_TYPE_P(rhs) == IS_LONG && (zend_dval_to_lval((double) Z_LVAL_P(rhs)) == Z_LVAL_P(rhs))) {
1453+
/* if the value can be exactly represented as a double, use double dedicated code otherwise generic */
1454+
RETURN_COPY_VALUE(lhs_dval >= (double)Z_LVAL_P(rhs) ? lhs : rhs);
1455+
} else {
1456+
goto generic_compare;
1457+
}
1458+
} else {
1459+
generic_compare:
1460+
RETURN_COPY(zend_compare(lhs, rhs) >= 0 ? lhs : rhs);
1461+
}
1462+
}
1463+
13861464
typedef struct {
13871465
zend_fcall_info fci;
13881466
zend_fcall_info_cache fci_cache;

ext/standard/basic_functions.stub.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -1612,10 +1612,16 @@ function pos(array|object $array): mixed {}
16121612

16131613
function key(array|object $array): int|string|null {}
16141614

1615-
/** @compile-time-eval */
1615+
/**
1616+
* @compile-time-eval
1617+
* @frameless-function {"arity": 2}
1618+
*/
16161619
function min(mixed $value, mixed ...$values): mixed {}
16171620

1618-
/** @compile-time-eval */
1621+
/**
1622+
* @compile-time-eval
1623+
* @frameless-function {"arity": 2}
1624+
*/
16191625
function max(mixed $value, mixed ...$values): mixed {}
16201626

16211627
function array_walk(array|object &$array, callable $callback, mixed $arg = UNKNOWN): true {}

ext/standard/basic_functions_arginfo.h

+15-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)