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/12 16:01] – add overloading applications 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 ===== | ||
- | PHP offers facilities for large number and decimal arithmetic (GMP and BCMath), but currently using those is a PITA. This RFC proposes to improve the situation by adding support for **operator overloading in internal classes**. | + | PHP offers facilities for large number and decimal arithmetic (GMP and BCMath), but currently using those is a PITA. This RFC proposes to improve the situation by adding support for **operator overloading in internal classes**. |
- | + | ||
- | TODO: More motivation! | + | |
===== Proposal A: Operator overloading ===== | ===== Proposal A: Operator overloading ===== | ||
- | The operator overloading is implemented | + | Note: This proposal is only about **internal** operator overloading and **not** about userland overloading. |
+ | |||
+ | ==== Why operator overloading? | ||
+ | |||
+ | There are several reasons why overloaded operators are preferable over '' | ||
+ | |||
+ | The first is that code using overloaded operators is simply **more readable**. As an example, consider the following two code snippets, one using '' | ||
+ | |||
+ | <code php> | ||
+ | $result = gmp_mod( | ||
+ | gmp_add( | ||
+ | gmp_mul($c0, | ||
+ | gmp_add( | ||
+ | gmp_mul($c1, | ||
+ | gmp_mul($c2, | ||
+ | ) | ||
+ | ), | ||
+ | gmp_mul($n0, | ||
+ | ); | ||
+ | |||
+ | $result = ( | ||
+ | $c0 * $ms0 * gmp_invert($ms0, | ||
+ | + $c1 * $ms1 * gmp_invert($ms1, | ||
+ | + $c2 * $ms2 * gmp_invert($ms2, | ||
+ | ) % ($n0 * $n1 * $n2); | ||
+ | </ | ||
+ | |||
+ | Even without understanding what the above code does (it's an excerpt from a Coppersmith attack on RSA), it should be obvious that the second code is a lot clearer. It makes the structure of the code immediately clear (three multiplications are summed up and the modulus is taken), whereas the function-based code actively hides any structure in the code. For mathematical operations infix notation just comes a lot more naturally. | ||
+ | |||
+ | Another advantage of overloaded operators is that it allows **polymorphism** for functions doing arithmetic operations. As an example, consider manually implementing a function like '' | ||
+ | |||
+ | <code php> | ||
+ | function powm($base, $exponent, $modulus) { | ||
+ | $result = 1; | ||
+ | while ($exponent > 0) { | ||
+ | if ($exponent % 2 == 1) { | ||
+ | $result = $result * $base % $modulus; | ||
+ | $exponent--; | ||
+ | } | ||
+ | $exponent /= 2; | ||
+ | $base = ($base * $base) % $modulus; | ||
+ | } | ||
+ | return $result; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | With operator overloading | ||
+ | |||
+ | <code php> | ||
+ | var_dump(powm(123, | ||
+ | var_dump(powm(gmp_init(123), | ||
+ | var_dump(powm( | ||
+ | gmp_init(" | ||
+ | gmp_init(" | ||
+ | gmp_init(" | ||
+ | )); // GMP(36912902142130032810) | ||
+ | </ | ||
+ | |||
+ | Without operator overloading this would not be possible. Instead one would have to implement the same function once using the normal '' | ||
+ | |||
+ | ==== Applications of operator overloading ==== | ||
+ | |||
+ | Some examples what the operator overloading capability can be used for, apart from the bignum arithmetic outlined in this RFC: | ||
+ | |||
+ | * Decimal arithmetic. This is particularly important in PHP as PHP commonly deals with monetary values which **can not** be represented as floating point numbers. | ||
+ | * Date calculations. Also very common in PHP ('' | ||
+ | * Ratio and complex arithmetic | ||
+ | * Unsigned arithmetic and arithmetic on other integral types PHP does not support (e.g. cross platform 64bit integers) | ||
+ | * Vector and matrix calculations | ||
+ | |||
+ | Due to potential pitfalls of misusing operator overloading known from other languages (most notably C++), the use of this new feature should be limited to cases where there are clear definitions to the behavior of all overloaded operators. The application of this feature should be for mathematical use cases only (as noted above), and not ' | ||
+ | |||
+ | ==== Technical proposal ==== | ||
+ | |||
+ | The operator overloading is implemented using two new object handlers: | ||
+ | |||
+ | === do_operation === | ||
+ | |||
+ | The '' | ||
< | < | ||
Line 41: | 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 60: | Line 129: | ||
The prefix operators '' | The prefix operators '' | ||
- | The operators '' | + | === compare === |
- | The ability to overload operators | + | The '' |
- | === Applications of operator overloading === | + | < |
+ | typedef int (*zend_object_compare_zvals_t)(zval *result, zval *op1, zval *op2 TSRMLS_DC); | ||
+ | </ | ||
- | Some examples what the operator overloading capability | + | Here '' |
- | * Decimal arithmetic. This is particularly important in PHP as PHP commonly deals with monetary values which **can not** be represented as floating point numbers. | + | The '' |
- | * Ratio and complex arithmetic | + | |
- | * Unsigned arithmetic | + | The difference between the '' |
- | * Vector | + | |
- | | + | |
- | | + | |
===== Proposal B: GMP Improvements ===== | ===== Proposal B: GMP Improvements ===== | ||
Line 86: | 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 106: | 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 115: | 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 127: | 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 134: | 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 141: | Line 275: | ||
The switch from GMP resources to objects can break scripts that checked whether something is a GMP integer using code like '' | The switch from GMP resources to objects can break scripts that checked whether something is a GMP integer using code like '' | ||
+ | |||
+ | ===== Performance ===== | ||
+ | |||
+ | The addition of operator overloading does not affect performance (or at least I couldn' | ||
+ | |||
+ | The changes to GMP improve performance in all scenarios I measured (4M runs each): | ||
+ | |||
+ | < | ||
+ | | ||
+ | a) gmp_add($a, $b) | ||
+ | b) gmp_add($a, 17) | ||
+ | c) gmp_add(42, $b) | ||
+ | d) $a + $b | ||
+ | </ | ||
+ | |||
+ | The difference between tests b) and c) is that the former makes use of an operator specialized on integers rather than creating a temporary GMP instance. | ||
===== Patch ===== | ===== Patch ===== | ||
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 ===== | ||
+ | |||
+ | http:// |
rfc/operator_overloading_gmp.1368374497.txt.gz · Last modified: 2017/09/22 13:28 (external edit)