rfc:operator_overloading_gmp
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:operator_overloading_gmp [2013/05/16 13:39] – Add some intro into operator overloading for non-core-developers nikic | rfc:operator_overloading_gmp [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 4: | Line 4: | ||
* Date: 2013-05-12 | * Date: 2013-05-12 | ||
* Author: Nikita Popov < | * Author: Nikita Popov < | ||
- | * Status: | + | * Status: |
* Patch: https:// | * Patch: https:// | ||
- | * Target version: PHP 5.6 (or whatever the next one is) | ||
===== Introduction ===== | ===== Introduction ===== | ||
Line 13: | Line 12: | ||
===== Proposal A: Operator overloading ===== | ===== Proposal A: Operator overloading ===== | ||
+ | |||
+ | Note: This proposal is only about **internal** operator overloading and **not** about userland overloading. | ||
==== Why operator overloading? | ==== Why operator overloading? | ||
Line 81: | Line 82: | ||
* Unsigned arithmetic and arithmetic on other integral types PHP does not support (e.g. cross platform 64bit integers) | * Unsigned arithmetic and arithmetic on other integral types PHP does not support (e.g. cross platform 64bit integers) | ||
* Vector and matrix calculations | * Vector and matrix calculations | ||
- | * (Misuse | + | |
+ | Due to potential pitfalls of misusing operator overloading known from other languages | ||
==== Technical proposal ==== | ==== Technical proposal ==== | ||
- | The operator overloading is implemented | + | The operator overloading is implemented |
+ | |||
+ | === do_operation === | ||
+ | |||
+ | The '' | ||
< | < | ||
Line 110: | Line 116: | ||
~ | ~ | ||
! | ! | ||
- | == ZEND_IS_EQUAL | ||
- | != ZEND_IS_NOT_EQUAL | ||
- | < | ||
- | <= ZEND_IS_SMALLER_OR_EQUAL | ||
</ | </ | ||
- | The operators ''>'', | + | The unary '' |
< | < | ||
- | $a > $b | + | +$a ==> |
- | $a >= $b ==> | + | -$a ==> |
- | +$a ==> | + | |
- | -$a | + | |
</ | </ | ||
Line 129: | Line 129: | ||
The prefix operators '' | The prefix operators '' | ||
- | The operators '' | + | === compare === |
- | The ability | + | The '' |
+ | |||
+ | < | ||
+ | typedef int (*zend_object_compare_zvals_t)(zval *result, zval *op1, zval *op2 TSRMLS_DC); | ||
+ | </ | ||
+ | |||
+ | Here '' | ||
+ | |||
+ | The '' | ||
+ | |||
+ | The difference between the '' | ||
===== Proposal B: GMP Improvements ===== | ===== Proposal B: GMP Improvements ===== | ||
Line 144: | Line 154: | ||
* Bad reporting on leaks. During the port I found that many functions leak resources, especially in error conditions. | * Bad reporting on leaks. During the port I found that many functions leak resources, especially in error conditions. | ||
- | This RFC proposes to make GMP use objects (of type '' | + | This RFC proposes to make GMP use objects (of type '' |
- | + | ||
- | In the following there are examples for some of the new behaviors: | + | |
=== Casting === | === Casting === | ||
Line 164: | Line 172: | ||
var_dump($s = serialize($n)); | var_dump($s = serialize($n)); | ||
var_dump(unserialize($s)); | var_dump(unserialize($s)); | ||
+ | |||
// outputs | // outputs | ||
+ | |||
object(GMP)# | object(GMP)# | ||
[" | [" | ||
Line 173: | Line 183: | ||
[" | [" | ||
string(2) " | string(2) " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Cloning === | ||
+ | |||
+ | <code php> | ||
+ | $a = gmp_init(3); | ||
+ | $b = clone $a; | ||
+ | gmp_clrbit($a, | ||
+ | var_dump($a, | ||
+ | |||
+ | // Output: (Note that $b is still 3) | ||
+ | |||
+ | object(GMP)# | ||
+ | [" | ||
+ | string(1) " | ||
+ | } | ||
+ | object(GMP)# | ||
+ | [" | ||
+ | string(1) " | ||
} | } | ||
</ | </ | ||
Line 185: | Line 215: | ||
var_dump($a + 17); | var_dump($a + 17); | ||
var_dump(42 + $b); | var_dump(42 + $b); | ||
+ | |||
// Outputs the following 3 times: | // Outputs the following 3 times: | ||
+ | |||
object(GMP)# | object(GMP)# | ||
[" | [" | ||
Line 192: | Line 224: | ||
</ | </ | ||
- | The following operators are supported: '' | + | The following operators are supported: '' |
+ | |||
+ | === Overloaded operators: Comparison === | ||
+ | |||
+ | <code php> | ||
+ | $a = gmp_init(42); | ||
+ | var_dump($a == 42, $a == 17, $a < 40, $a < 100); | ||
+ | // | ||
+ | </ | ||
+ | |||
+ | Comparison is supported via the '' | ||
+ | |||
+ | <code php> | ||
+ | $arr = [gmp_init(0), | ||
+ | sort($arr); | ||
+ | var_dump($arr); | ||
+ | |||
+ | // Outputs | ||
+ | |||
+ | array(4) { | ||
+ | [0]=> | ||
+ | int(-3) | ||
+ | [1]=> | ||
+ | object(GMP)#1 (1) { | ||
+ | [" | ||
+ | string(1) " | ||
+ | } | ||
+ | [2]=> | ||
+ | int(1) | ||
+ | [3]=> | ||
+ | object(GMP)#2 (1) { | ||
+ | [" | ||
+ | string(1) " | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Other minor changes === | ||
+ | |||
+ | During the refactoring of the implementation a few additional, small changes were done: | ||
+ | |||
+ | * If you pass a GMP instance to '' | ||
+ | * Previously some functions like '' | ||
+ | * Due to the previous change '' | ||
+ | * If you pass an invalid rounding mode to a function, you will now get a warning. | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 208: | Line 284: | ||
< | < | ||
| | ||
- | a) gmp_add($a, $b) 1.16 1.25 | + | a) gmp_add($a, $b) 1.07 1.25 |
- | b) gmp_add($a, 17) 1.08 1.21 | + | b) gmp_add($a, 17) 1.02 1.21 |
- | c) gmp_add(42, $b) 1.29 1.84 | + | c) gmp_add(42, $b) 1.20 1.84 |
- | d) $a + $b 0.83 --- | + | d) $a + $b 0.76 --- |
</ | </ | ||
Line 219: | Line 295: | ||
The pull request for this RFC can be found here: https:// | The pull request for this RFC can be found here: https:// | ||
+ | |||
+ | ===== Vote ===== | ||
+ | |||
+ | The vote started on 10.06.2013 and ended on 17.06.2013. Both proposals are accepted. | ||
+ | |||
+ | <doodle title=" | ||
+ | * Internal operator overloading | ||
+ | * GMP changes | ||
+ | * None | ||
+ | </ | ||
===== Previous discussions ===== | ===== Previous discussions ===== | ||
- | http:// | + | http:// |
rfc/operator_overloading_gmp.1368711540.txt.gz · Last modified: 2017/09/22 13:28 (external edit)