|
| 1 | +--TEST-- |
| 2 | +JIT: BW_NOT (~) on integers in a hot loop (function JIT), with type changes |
| 3 | +--EXTENSIONS-- |
| 4 | +opcache |
| 5 | +--INI-- |
| 6 | +opcache.enable=1 |
| 7 | +opcache.enable_cli=1 |
| 8 | +opcache.jit=function |
| 9 | +opcache.jit_buffer_size=32M |
| 10 | +opcache.jit_hot_loop=1 |
| 11 | +opcache.jit_hot_func=1 |
| 12 | +--FILE-- |
| 13 | +<?php |
| 14 | + |
| 15 | +function not_cv(int $x): int { |
| 16 | + $y = ~$x; // result in a CV |
| 17 | + return $y; |
| 18 | +} |
| 19 | + |
| 20 | +function not_tmp(int $x): int { |
| 21 | + return ~$x & 0xFF; // result in a TMP |
| 22 | +} |
| 23 | + |
| 24 | +function not_twice(int $x): int { |
| 25 | + return ~~$x; |
| 26 | +} |
| 27 | + |
| 28 | +function not_chain(int $a, int $b): int { |
| 29 | + return ~$a & $b; // and-not combination |
| 30 | +} |
| 31 | + |
| 32 | +$values = [0, 1, -1, 2, 255, PHP_INT_MAX, PHP_INT_MIN, PHP_INT_MAX - 1, PHP_INT_MIN + 1, |
| 33 | + 0x123456789ABCDEF, -0x123456789ABCDEF]; |
| 34 | +mt_srand(7); |
| 35 | +for ($i = 0; $i < 100; $i++) { |
| 36 | + $values[] = (mt_rand() << 33) ^ (mt_rand() << 11) ^ mt_rand(); |
| 37 | +} |
| 38 | + |
| 39 | +$bad = 0; |
| 40 | +for ($round = 0; $round < 20; $round++) { |
| 41 | + foreach ($values as $v) { |
| 42 | + // ~x == -x - 1 for every int except PHP_INT_MIN (where -x overflows) |
| 43 | + $expected = ($v === PHP_INT_MIN) ? PHP_INT_MAX : -$v - 1; |
| 44 | + if (not_cv($v) !== $expected) $bad++; |
| 45 | + if (not_tmp($v) !== ($expected & 0xFF)) $bad++; |
| 46 | + if (not_twice($v) !== $v) $bad++; |
| 47 | + if (not_chain($v, 0x0F0F) !== ($expected & 0x0F0F)) $bad++; |
| 48 | + } |
| 49 | +} |
| 50 | +var_dump($bad); |
| 51 | + |
| 52 | +// Same loop bodies, now fed other types after the trace was compiled for int. |
| 53 | +function not_any($x) { |
| 54 | + return ~$x; |
| 55 | +} |
| 56 | +for ($i = 0; $i < 200; $i++) { |
| 57 | + not_any($i); |
| 58 | +} |
| 59 | +var_dump(not_any(5)); |
| 60 | +var_dump(not_any(2.0)); |
| 61 | +var_dump(not_any("ab") === "\x9E\x9D"); |
| 62 | +foreach ([null, true, false, [], new stdClass] as $bad_value) { |
| 63 | + try { |
| 64 | + not_any($bad_value); |
| 65 | + echo "no exception\n"; |
| 66 | + } catch (TypeError $e) { |
| 67 | + echo $e->getMessage(), "\n"; |
| 68 | + } |
| 69 | +} |
| 70 | +var_dump(not_any(7)); |
| 71 | + |
| 72 | +// Undefined variable operand |
| 73 | +function not_undef() { |
| 74 | + return ~$undefined; |
| 75 | +} |
| 76 | +for ($i = 0; $i < 3; $i++) { |
| 77 | + try { |
| 78 | + not_undef(); |
| 79 | + } catch (TypeError $e) { |
| 80 | + echo $e->getMessage(), "\n"; |
| 81 | + } |
| 82 | +} |
| 83 | +?> |
| 84 | +--EXPECTF-- |
| 85 | +int(0) |
| 86 | +int(-6) |
| 87 | +int(-3) |
| 88 | +bool(true) |
| 89 | +Cannot perform bitwise not on null |
| 90 | +Cannot perform bitwise not on true |
| 91 | +Cannot perform bitwise not on false |
| 92 | +Cannot perform bitwise not on array |
| 93 | +Cannot perform bitwise not on stdClass |
| 94 | +int(-8) |
| 95 | + |
| 96 | +Warning: Undefined variable $undefined in %s on line %d |
| 97 | +Cannot perform bitwise not on null |
| 98 | + |
| 99 | +Warning: Undefined variable $undefined in %s on line %d |
| 100 | +Cannot perform bitwise not on null |
| 101 | + |
| 102 | +Warning: Undefined variable $undefined in %s on line %d |
| 103 | +Cannot perform bitwise not on null |
0 commit comments