3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Since integers in 32-bit PHP 5.x are signed, exclusively, this only goes up to 2147483647 */ function add_32bit($intA, $intB) { if (!is_int($intA) || !is_int($intB)) { return false; } if ($intA > 2147483647 || $intB > 2147483647) { return false; } $sum = 0; $carry = 0; $rem = 0; for ($i = 0; $i < 4; ++$i) { $oA = $intA >> ($i * 8); $oB = $intB >> ($i * 8); $pA = $oA & 0xff; $pB = $oB & 0xff; $tmp = ($pA + $pB + $carry); $rem = $tmp & 0xff; $carry = ($tmp - $rem) >> 8; $inc = ($rem << ($i * 8)); $sum |= $inc & 0x7fffffff; } return $sum; } // Tests var_dump( add_32bit(1000, 1001) ); var_dump( add_32bit(1000, 2000) ); var_dump( add_32bit(1, 1) ); var_dump( add_32bit(10000000, 10000000) ); var_dump( 2147483646, 1 ); var_dump( 2147483646, 2 ); var_dump( 2147483646, 3 ); var_dump( 2147483646, 4 );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7AIOa
function name:  (null)
number of ops:  45
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL                                               'add_32bit'
          2        SEND_VAL                                                 1000
          3        SEND_VAL                                                 1001
          4        DO_FCALL                                      0  $0      
          5        SEND_VAR                                                 $0
          6        DO_ICALL                                                 
   35     7        INIT_FCALL                                               'var_dump'
          8        INIT_FCALL                                               'add_32bit'
          9        SEND_VAL                                                 1000
         10        SEND_VAL                                                 2000
         11        DO_FCALL                                      0  $2      
         12        SEND_VAR                                                 $2
         13        DO_ICALL                                                 
   36    14        INIT_FCALL                                               'var_dump'
         15        INIT_FCALL                                               'add_32bit'
         16        SEND_VAL                                                 1
         17        SEND_VAL                                                 1
         18        DO_FCALL                                      0  $4      
         19        SEND_VAR                                                 $4
         20        DO_ICALL                                                 
   37    21        INIT_FCALL                                               'var_dump'
         22        INIT_FCALL                                               'add_32bit'
         23        SEND_VAL                                                 10000000
         24        SEND_VAL                                                 10000000
         25        DO_FCALL                                      0  $6      
         26        SEND_VAR                                                 $6
         27        DO_ICALL                                                 
   38    28        INIT_FCALL                                               'var_dump'
         29        SEND_VAL                                                 2147483646
         30        SEND_VAL                                                 1
         31        DO_ICALL                                                 
   39    32        INIT_FCALL                                               'var_dump'
         33        SEND_VAL                                                 2147483646
         34        SEND_VAL                                                 2
         35        DO_ICALL                                                 
   40    36        INIT_FCALL                                               'var_dump'
         37        SEND_VAL                                                 2147483646
         38        SEND_VAL                                                 3
         39        DO_ICALL                                                 
   41    40        INIT_FCALL                                               'var_dump'
         41        SEND_VAL                                                 2147483646
         42        SEND_VAL                                                 4
         43        DO_ICALL                                                 
         44      > RETURN                                                   1

Function add_32bit:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 47) Position 1 = 12, Position 2 = 14
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 16
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
2 jumps found. (Code = 44) Position 1 = 47, Position 2 = 21
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 47, Position 2 = 21
Branch analysis from position: 47
Branch analysis from position: 21
Branch analysis from position: 14
Branch analysis from position: 8
filename:       /in/7AIOa
function name:  add_32bit
number of ops:  49
compiled vars:  !0 = $intA, !1 = $intB, !2 = $sum, !3 = $carry, !4 = $rem, !5 = $i, !6 = $oA, !7 = $oB, !8 = $pA, !9 = $pB, !10 = $tmp, !11 = $inc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    8     2        TYPE_CHECK                                   16  ~12     !0
          3        BOOL_NOT                                         ~13     ~12
          4      > JMPNZ_EX                                         ~13     ~13, ->8
          5    >   TYPE_CHECK                                   16  ~14     !1
          6        BOOL_NOT                                         ~15     ~14
          7        BOOL                                             ~13     ~15
          8    > > JMPZ                                                     ~13, ->10
    9     9    > > RETURN                                                   <false>
   11    10    >   IS_SMALLER                                       ~16     2147483647, !0
         11      > JMPNZ_EX                                         ~16     ~16, ->14
         12    >   IS_SMALLER                                       ~17     2147483647, !1
         13        BOOL                                             ~16     ~17
         14    > > JMPZ                                                     ~16, ->16
   12    15    > > RETURN                                                   <false>
   14    16    >   ASSIGN                                                   !2, 0
   15    17        ASSIGN                                                   !3, 0
   16    18        ASSIGN                                                   !4, 0
   18    19        ASSIGN                                                   !5, 0
         20      > JMP                                                      ->45
   19    21    >   MUL                                              ~22     !5, 8
         22        SR                                               ~23     !0, ~22
         23        ASSIGN                                                   !6, ~23
   20    24        MUL                                              ~25     !5, 8
         25        SR                                               ~26     !1, ~25
         26        ASSIGN                                                   !7, ~26
   21    27        BW_AND                                           ~28     !6, 255
         28        ASSIGN                                                   !8, ~28
   22    29        BW_AND                                           ~30     !7, 255
         30        ASSIGN                                                   !9, ~30
   24    31        ADD                                              ~32     !8, !9
         32        ADD                                              ~33     ~32, !3
         33        ASSIGN                                                   !10, ~33
   25    34        BW_AND                                           ~35     !10, 255
         35        ASSIGN                                                   !4, ~35
   26    36        SUB                                              ~37     !10, !4
         37        SR                                               ~38     ~37, 8
         38        ASSIGN                                                   !3, ~38
   27    39        MUL                                              ~40     !5, 8
         40        SL                                               ~41     !4, ~40
         41        ASSIGN                                                   !11, ~41
   28    42        BW_AND                                           ~43     !11, 2147483647
         43        ASSIGN_OP                                     9          !2, ~43
   18    44        PRE_INC                                                  !5
         45    >   IS_SMALLER                                               !5, 4
         46      > JMPNZ                                                    ~46, ->21
   30    47    > > RETURN                                                   !2
   31    48*     > RETURN                                                   null

End of function add_32bit

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.3 ms | 1021 KiB | 18 Q