Skip to content

Instantly share code, notes, and snippets.

@adsr
Created August 16, 2022 02:32
Show Gist options
  • Save adsr/7da730fb51aad0a3cb0ec90c2234a800 to your computer and use it in GitHub Desktop.
Save adsr/7da730fb51aad0a3cb0ec90c2234a800 to your computer and use it in GitHub Desktop.
From dac14b44692297e60f909bcb03cf209652f97b09 Mon Sep 17 00:00:00 2001
From: Adam Saponara <as@php.net>
Date: Mon, 15 Aug 2022 22:31:13 -0400
Subject: [PATCH] Add intadd function
---
ext/standard/basic_functions.stub.php | 3 +++
ext/standard/basic_functions_arginfo.h | 6 +++++-
ext/standard/math.c | 14 ++++++++++++++
ext/standard/tests/math/intadd.phpt | 14 ++++++++++++++
4 files changed, 36 insertions(+), 1 deletion(-)
create mode 100644 ext/standard/tests/math/intadd.phpt
diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php
index 541dfc4645..5be000664b 100755
--- a/ext/standard/basic_functions.stub.php
+++ b/ext/standard/basic_functions.stub.php
@@ -1520,6 +1520,9 @@ function is_nan(float $num): bool {}
/** @compile-time-eval */
function intdiv(int $num1, int $num2): int {}
+/** @compile-time-eval */
+function intadd(int $num1, int $num2): int {}
+
/** @compile-time-eval */
function is_infinite(float $num): bool {}
diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h
index c01805f3a3..15c1826b1b 100644
--- a/ext/standard/basic_functions_arginfo.h
+++ b/ext/standard/basic_functions_arginfo.h
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
- * Stub hash: a4c98e83e51a9546a89797b80bdd8771ef0075f9 */
+ * Stub hash: 18d98db34661802f2eaaede16a7140386c4d0810 */
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 0)
@@ -1669,6 +1669,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_intdiv, 0, 2, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, num2, IS_LONG, 0)
ZEND_END_ARG_INFO()
+#define arginfo_intadd arginfo_intdiv
+
#define arginfo_is_infinite arginfo_is_finite
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_pow, 0, 2, MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_OBJECT)
@@ -2662,6 +2664,7 @@ ZEND_FUNCTION(pi);
ZEND_FUNCTION(is_finite);
ZEND_FUNCTION(is_nan);
ZEND_FUNCTION(intdiv);
+ZEND_FUNCTION(intadd);
ZEND_FUNCTION(is_infinite);
ZEND_FUNCTION(pow);
ZEND_FUNCTION(exp);
@@ -3300,6 +3303,7 @@ static const zend_function_entry ext_functions[] = {
ZEND_SUPPORTS_COMPILE_TIME_EVAL_FE(is_finite, arginfo_is_finite)
ZEND_SUPPORTS_COMPILE_TIME_EVAL_FE(is_nan, arginfo_is_nan)
ZEND_SUPPORTS_COMPILE_TIME_EVAL_FE(intdiv, arginfo_intdiv)
+ ZEND_SUPPORTS_COMPILE_TIME_EVAL_FE(intadd, arginfo_intadd)
ZEND_SUPPORTS_COMPILE_TIME_EVAL_FE(is_infinite, arginfo_is_infinite)
ZEND_SUPPORTS_COMPILE_TIME_EVAL_FE(pow, arginfo_pow)
ZEND_FE(exp, arginfo_exp)
diff --git a/ext/standard/math.c b/ext/standard/math.c
index ad2823ea49..54a099e78d 100644
--- a/ext/standard/math.c
+++ b/ext/standard/math.c
@@ -1215,3 +1215,17 @@ PHP_FUNCTION(intdiv)
RETURN_LONG(dividend / divisor);
}
/* }}} */
+
+/* {{{ Returns sum of integers, allowing overflow */
+PHP_FUNCTION(intadd)
+{
+ zend_long addend1, addend2;
+
+ ZEND_PARSE_PARAMETERS_START(2, 2)
+ Z_PARAM_LONG(addend1)
+ Z_PARAM_LONG(addend2)
+ ZEND_PARSE_PARAMETERS_END();
+
+ RETURN_LONG(addend1 + addend2);
+}
+/* }}} */
diff --git a/ext/standard/tests/math/intadd.phpt b/ext/standard/tests/math/intadd.phpt
new file mode 100644
index 0000000000..5f1f1b5994
--- /dev/null
+++ b/ext/standard/tests/math/intadd.phpt
@@ -0,0 +1,14 @@
+--TEST--
+intadd functionality
+--FILE--
+<?php
+var_dump(intadd(41, 1) === 42);
+var_dump(intadd(-1, 1) === 0);
+var_dump(intadd(PHP_INT_MAX, 1) === PHP_INT_MIN);
+var_dump(intadd(PHP_INT_MIN, -1) === PHP_INT_MAX);
+?>
+--EXPECT--
+bool(true)
+bool(true)
+bool(true)
+bool(true)
--
2.35.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment