Skip to content

Commit e1bcf76

Browse files
committed
Use a common function for normal and frameless implementations
1 parent 4f07c5d commit e1bcf76

File tree

1 file changed

+16
-35
lines changed

1 file changed

+16
-35
lines changed

ext/standard/math.c

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -389,17 +389,7 @@ PHP_FUNCTION(round)
389389
}
390390
/* }}} */
391391

392-
/* {{{ Return the given value if in range of min and max */
393-
PHP_FUNCTION(clamp)
394-
{
395-
zval *zvalue, *zmin, *zmax;
396-
397-
ZEND_PARSE_PARAMETERS_START(3, 3)
398-
Z_PARAM_ZVAL(zvalue)
399-
Z_PARAM_ZVAL(zmin)
400-
Z_PARAM_ZVAL(zmax)
401-
ZEND_PARSE_PARAMETERS_END();
402-
392+
static inline double php_clamp(const zval *zvalue, const zval *zmin, const zval *zmax) {
403393
if (EXPECTED(Z_TYPE_P(zmin) == IS_DOUBLE) && UNEXPECTED(zend_isnan(Z_DVAL_P(zmin)))) {
404394
zend_argument_value_error(2, "cannot be NAN");
405395
RETURN_THROWS();
@@ -425,6 +415,20 @@ PHP_FUNCTION(clamp)
425415

426416
RETURN_COPY(zvalue);
427417
}
418+
419+
/* {{{ Return the given value if in range of min and max */
420+
PHP_FUNCTION(clamp)
421+
{
422+
zval *zvalue, *zmin, *zmax;
423+
424+
ZEND_PARSE_PARAMETERS_START(3, 3)
425+
Z_PARAM_ZVAL(zvalue)
426+
Z_PARAM_ZVAL(zmin)
427+
Z_PARAM_ZVAL(zmax)
428+
ZEND_PARSE_PARAMETERS_END();
429+
430+
php_clamp(zvalue, zmin, zmax);
431+
}
428432
/* }}} */
429433

430434
/* {{{ Return the given value if in range of min and max */
@@ -435,30 +439,7 @@ ZEND_FRAMELESS_FUNCTION(clamp, 3)
435439
Z_FLF_PARAM_ZVAL(2, zmin);
436440
Z_FLF_PARAM_ZVAL(3, zmax);
437441

438-
if (EXPECTED(Z_TYPE_P(zmin) == IS_DOUBLE) && UNEXPECTED(zend_isnan(Z_DVAL_P(zmin)))) {
439-
zend_argument_value_error(2, "cannot be NAN");
440-
RETURN_THROWS();
441-
}
442-
443-
if (EXPECTED(Z_TYPE_P(zmax) == IS_DOUBLE) && UNEXPECTED(zend_isnan(Z_DVAL_P(zmax)))) {
444-
zend_argument_value_error(3, "cannot be NAN");
445-
RETURN_THROWS();
446-
}
447-
448-
if (zend_compare(zmax, zmin) == -1) {
449-
zend_argument_value_error(2, "must be smaller than or equal to argument #3 ($max)");
450-
RETURN_THROWS();
451-
}
452-
453-
if (zend_compare(zmax, zvalue) == -1) {
454-
RETURN_COPY(zmax);
455-
}
456-
457-
if (zend_compare(zvalue, zmin) == -1) {
458-
RETURN_COPY(zmin);
459-
}
460-
461-
RETURN_COPY(zvalue);
442+
php_clamp(zvalue, zmin, zmax);
462443
}
463444
/* }}} */
464445

0 commit comments

Comments
 (0)