3v4l.org

run code in 300+ PHP versions simultaneously
<?php define('INT_SIGNED', 1); define('INT_LE', 2); function is32Bit() { $test = unpack('N', "\xFF\xFF\xFF\xFF"); return current($test) === -1; } function unpack_int32($str, $flags = 0) { $length = strlen($str); if ($length < 4) { return false; } else if ($length > 4) { $str = substr($str, 0, 4); } if ($flags & INT_LE) { $str = strrev($str); } $result = unpack('N', $str); $result = current($result); $signBit = (bool) ($result & 0x80000000); $isSigned = (bool) ($flags & INT_SIGNED); $is32bit = is32Bit(); if ($signBit && $isSigned && !$is32bit) { $result = (0x00000000FFFFFFFF << 32) | $result; } else if ($signBit && !$isSigned && $is32bit) { $result = (($result & 0x7FFFFFFF) * 2) + ~$result + 1; } return $result; } function unpack_int64($str, $flags = 0) { $length = strlen($str); if ($length < 8) { return false; } else if ($length > 8) { $str = substr($str, 0, 8); } if ($flags & INT_LE) { $str = strrev($str); } $longs = array_values(unpack('N*', $str)); $signBit = (bool) ($longs[0] & 0x80000000); $isSigned = (bool) ($flags & INT_SIGNED); if (is32Bit()) { if ($longs[1] & 0x80000000) { $longs[1] = (($longs[1] & 0x7FFFFFFF) * 2) + ~$longs[1] + 1; } if ($isSigned && $signBit) { $result = ($longs[0] * pow(2, 32)) + $longs[1]; } else { if ($signBit) { $longs[0] = (($longs[0] & 0x7FFFFFFF) * 2) + ~$longs[0] + 1; } $result = ($longs[0] * pow(2, 32)) + $longs[1]; } } else { $result = ($longs[0] << 32) | $longs[1]; if ($signBit && !$isSigned) { $result = (($result & 0x7FFFFFFFFFFFFFFF) * 2) + ~$result + 1; } } return $result; } printf("%f\n", unpack_int64("\xFF\xFF\xFF\xFE\x00\x00\x00\x00")); printf("%f\n", unpack_int64("\xFF\xFF\xFF\xFE\x00\x00\x00\x00", INT_SIGNED)); printf("%f\n", unpack_int64("\xFF\xFF\xFF\xFE\x00\x00\x00\x00", INT_LE)); printf("%f\n", unpack_int64("\xFF\xFF\xFF\xFE\x00\x00\x00\x00", INT_LE | INT_SIGNED));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sXPPb
function name:  (null)
number of ops:  45
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                               'define'
          1        SEND_VAL                                                 'INT_SIGNED'
          2        SEND_VAL                                                 1
          3        DO_ICALL                                                 
    4     4        INIT_FCALL                                               'define'
          5        SEND_VAL                                                 'INT_LE'
          6        SEND_VAL                                                 2
          7        DO_ICALL                                                 
   76     8        INIT_FCALL                                               'printf'
          9        SEND_VAL                                                 '%25f%0A'
         10        INIT_FCALL                                               'unpack_int64'
         11        SEND_VAL                                                 '%FF%FF%FF%FE%00%00%00%00'
         12        DO_FCALL                                      0  $2      
         13        SEND_VAR                                                 $2
         14        DO_ICALL                                                 
   77    15        INIT_FCALL                                               'printf'
         16        SEND_VAL                                                 '%25f%0A'
         17        INIT_FCALL                                               'unpack_int64'
         18        SEND_VAL                                                 '%FF%FF%FF%FE%00%00%00%00'
         19        FETCH_CONSTANT                                   ~4      'INT_SIGNED'
         20        SEND_VAL                                                 ~4
         21        DO_FCALL                                      0  $5      
         22        SEND_VAR                                                 $5
         23        DO_ICALL                                                 
   78    24        INIT_FCALL                                               'printf'
         25        SEND_VAL                                                 '%25f%0A'
         26        INIT_FCALL                                               'unpack_int64'
         27        SEND_VAL                                                 '%FF%FF%FF%FE%00%00%00%00'
         28        FETCH_CONSTANT                                   ~7      'INT_LE'
         29        SEND_VAL                                                 ~7
         30        DO_FCALL                                      0  $8      
         31        SEND_VAR                                                 $8
         32        DO_ICALL                                                 
   79    33        INIT_FCALL                                               'printf'
         34        SEND_VAL                                                 '%25f%0A'
         35        INIT_FCALL                                               'unpack_int64'
         36        SEND_VAL                                                 '%FF%FF%FF%FE%00%00%00%00'
         37        FETCH_CONSTANT                                   ~10     'INT_LE'
         38        FETCH_CONSTANT                                   ~11     'INT_SIGNED'
         39        BW_OR                                            ~12     ~10, ~11
         40        SEND_VAL                                                 ~12
         41        DO_FCALL                                      0  $13     
         42        SEND_VAR                                                 $13
         43        DO_ICALL                                                 
         44      > RETURN                                                   1

Function is32bit:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sXPPb
function name:  is32Bit
number of ops:  11
compiled vars:  !0 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   INIT_FCALL                                               'unpack'
          1        SEND_VAL                                                 'N'
          2        SEND_VAL                                                 '%FF%FF%FF%FF'
          3        DO_ICALL                                         $1      
          4        ASSIGN                                                   !0, $1
    8     5        INIT_FCALL                                               'current'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $3      
          8        IS_IDENTICAL                                     ~4      $3, -1
          9      > RETURN                                                   ~4
    9    10*     > RETURN                                                   null

End of function is32bit

Function unpack_int32:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 16
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 23
Branch analysis from position: 19
2 jumps found. (Code = 46) Position 1 = 43, Position 2 = 44
Branch analysis from position: 43
2 jumps found. (Code = 46) Position 1 = 45, Position 2 = 47
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 48, Position 2 = 51
Branch analysis from position: 48
1 jumps found. (Code = 42) Position 1 = 63
Branch analysis from position: 63
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 51
2 jumps found. (Code = 46) Position 1 = 52, Position 2 = 54
Branch analysis from position: 52
2 jumps found. (Code = 46) Position 1 = 55, Position 2 = 56
Branch analysis from position: 55
2 jumps found. (Code = 43) Position 1 = 57, Position 2 = 63
Branch analysis from position: 57
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 63
Branch analysis from position: 56
Branch analysis from position: 54
Branch analysis from position: 47
Branch analysis from position: 44
Branch analysis from position: 23
Branch analysis from position: 16
filename:       /in/sXPPb
function name:  unpack_int32
number of ops:  65
compiled vars:  !0 = $str, !1 = $flags, !2 = $length, !3 = $result, !4 = $signBit, !5 = $isSigned, !6 = $is32bit
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      0
   12     2        STRLEN                                           ~7      !0
          3        ASSIGN                                                   !2, ~7
   13     4        IS_SMALLER                                               !2, 4
          5      > JMPZ                                                     ~9, ->8
   14     6    > > RETURN                                                   <false>
          7*       JMP                                                      ->16
   15     8    >   IS_SMALLER                                               4, !2
          9      > JMPZ                                                     ~10, ->16
   16    10    >   INIT_FCALL                                               'substr'
         11        SEND_VAR                                                 !0
         12        SEND_VAL                                                 0
         13        SEND_VAL                                                 4
         14        DO_ICALL                                         $11     
         15        ASSIGN                                                   !0, $11
   19    16    >   FETCH_CONSTANT                                   ~13     'INT_LE'
         17        BW_AND                                           ~14     !1, ~13
         18      > JMPZ                                                     ~14, ->23
   20    19    >   INIT_FCALL                                               'strrev'
         20        SEND_VAR                                                 !0
         21        DO_ICALL                                         $15     
         22        ASSIGN                                                   !0, $15
   22    23    >   INIT_FCALL                                               'unpack'
         24        SEND_VAL                                                 'N'
         25        SEND_VAR                                                 !0
         26        DO_ICALL                                         $17     
         27        ASSIGN                                                   !3, $17
   23    28        INIT_FCALL                                               'current'
         29        SEND_VAR                                                 !3
         30        DO_ICALL                                         $19     
         31        ASSIGN                                                   !3, $19
   25    32        BW_AND                                           ~21     !3, 2147483648
         33        BOOL                                             ~22     ~21
         34        ASSIGN                                                   !4, ~22
   26    35        FETCH_CONSTANT                                   ~24     'INT_SIGNED'
         36        BW_AND                                           ~25     !1, ~24
         37        BOOL                                             ~26     ~25
         38        ASSIGN                                                   !5, ~26
   27    39        INIT_FCALL                                               'is32bit'
         40        DO_FCALL                                      0  $28     
         41        ASSIGN                                                   !6, $28
   29    42      > JMPZ_EX                                          ~30     !4, ->44
         43    >   BOOL                                             ~30     !5
         44    > > JMPZ_EX                                          ~30     ~30, ->47
         45    >   BOOL_NOT                                         ~31     !6
         46        BOOL                                             ~30     ~31
         47    > > JMPZ                                                     ~30, ->51
   30    48    >   BW_OR                                            ~32     !3, -4294967296
         49        ASSIGN                                                   !3, ~32
         50      > JMP                                                      ->63
   31    51    > > JMPZ_EX                                          ~34     !4, ->54
         52    >   BOOL_NOT                                         ~35     !5
         53        BOOL                                             ~34     ~35
         54    > > JMPZ_EX                                          ~34     ~34, ->56
         55    >   BOOL                                             ~34     !6
         56    > > JMPZ                                                     ~34, ->63
   32    57    >   BW_AND                                           ~36     !3, 2147483647
         58        MUL                                              ~37     ~36, 2
         59        BW_NOT                                           ~38     !3
         60        ADD                                              ~39     ~37, ~38
         61        ADD                                              ~40     ~39, 1
         62        ASSIGN                                                   !3, ~40
   35    63    > > RETURN                                                   !3
   36    64*     > RETURN                                                   null

End of function unpack_int32

Function unpack_int64:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 16
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 23
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 87
Branch analysis from position: 42
2 jumps found. (Code = 43) Position 1 = 45, Position 2 = 54
Branch analysis from position: 45
2 jumps found. (Code = 46) Position 1 = 55, Position 2 = 56
Branch analysis from position: 55
2 jumps found. (Code = 43) Position 1 = 57, Position 2 = 67
Branch analysis from position: 57
1 jumps found. (Code = 42) Position 1 = 86
Branch analysis from position: 86
1 jumps found. (Code = 42) Position 1 = 102
Branch analysis from position: 102
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 67
2 jumps found. (Code = 43) Position 1 = 68, Position 2 = 77
Branch analysis from position: 68
1 jumps found. (Code = 42) Position 1 = 102
Branch analysis from position: 102
Branch analysis from position: 77
Branch analysis from position: 56
Branch analysis from position: 54
Branch analysis from position: 87
2 jumps found. (Code = 46) Position 1 = 93, Position 2 = 95
Branch analysis from position: 93
2 jumps found. (Code = 43) Position 1 = 96, Position 2 = 102
Branch analysis from position: 96
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 102
Branch analysis from position: 95
Branch analysis from position: 23
Branch analysis from position: 16
filename:       /in/sXPPb
function name:  unpack_int64
number of ops:  104
compiled vars:  !0 = $str, !1 = $flags, !2 = $length, !3 = $longs, !4 = $signBit, !5 = $isSigned, !6 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      0
   39     2        STRLEN                                           ~7      !0
          3        ASSIGN                                                   !2, ~7
   40     4        IS_SMALLER                                               !2, 8
          5      > JMPZ                                                     ~9, ->8
   41     6    > > RETURN                                                   <false>
          7*       JMP                                                      ->16
   42     8    >   IS_SMALLER                                               8, !2
          9      > JMPZ                                                     ~10, ->16
   43    10    >   INIT_FCALL                                               'substr'
         11        SEND_VAR                                                 !0
         12        SEND_VAL                                                 0
         13        SEND_VAL                                                 8
         14        DO_ICALL                                         $11     
         15        ASSIGN                                                   !0, $11
   46    16    >   FETCH_CONSTANT                                   ~13     'INT_LE'
         17        BW_AND                                           ~14     !1, ~13
         18      > JMPZ                                                     ~14, ->23
   47    19    >   INIT_FCALL                                               'strrev'
         20        SEND_VAR                                                 !0
         21        DO_ICALL                                         $15     
         22        ASSIGN                                                   !0, $15
   49    23    >   INIT_FCALL                                               'array_values'
         24        INIT_FCALL                                               'unpack'
         25        SEND_VAL                                                 'N%2A'
         26        SEND_VAR                                                 !0
         27        DO_ICALL                                         $17     
         28        SEND_VAR                                                 $17
         29        DO_ICALL                                         $18     
         30        ASSIGN                                                   !3, $18
   51    31        FETCH_DIM_R                                      ~20     !3, 0
         32        BW_AND                                           ~21     ~20, 2147483648
         33        BOOL                                             ~22     ~21
         34        ASSIGN                                                   !4, ~22
   52    35        FETCH_CONSTANT                                   ~24     'INT_SIGNED'
         36        BW_AND                                           ~25     !1, ~24
         37        BOOL                                             ~26     ~25
         38        ASSIGN                                                   !5, ~26
   54    39        INIT_FCALL                                               'is32bit'
         40        DO_FCALL                                      0  $28     
         41      > JMPZ                                                     $28, ->87
   55    42    >   FETCH_DIM_R                                      ~29     !3, 1
         43        BW_AND                                           ~30     ~29, 2147483648
         44      > JMPZ                                                     ~30, ->54
   56    45    >   FETCH_DIM_R                                      ~32     !3, 1
         46        BW_AND                                           ~33     ~32, 2147483647
         47        MUL                                              ~34     ~33, 2
         48        FETCH_DIM_R                                      ~35     !3, 1
         49        BW_NOT                                           ~36     ~35
         50        ADD                                              ~37     ~34, ~36
         51        ADD                                              ~38     ~37, 1
         52        ASSIGN_DIM                                               !3, 1
         53        OP_DATA                                                  ~38
   58    54    > > JMPZ_EX                                          ~39     !5, ->56
         55    >   BOOL                                             ~39     !4
         56    > > JMPZ                                                     ~39, ->67
   59    57    >   FETCH_DIM_R                                      ~40     !3, 0
         58        INIT_FCALL                                               'pow'
         59        SEND_VAL                                                 2
         60        SEND_VAL                                                 32
         61        DO_ICALL                                         $41     
         62        MUL                                              ~42     $41, ~40
         63        FETCH_DIM_R                                      ~43     !3, 1
         64        ADD                                              ~44     ~42, ~43
         65        ASSIGN                                                   !6, ~44
         66      > JMP                                                      ->86
   61    67    > > JMPZ                                                     !4, ->77
   62    68    >   FETCH_DIM_R                                      ~47     !3, 0
         69        BW_AND                                           ~48     ~47, 2147483647
         70        MUL                                              ~49     ~48, 2
         71        FETCH_DIM_R                                      ~50     !3, 0
         72        BW_NOT                                           ~51     ~50
         73        ADD                                              ~52     ~49, ~51
         74        ADD                                              ~53     ~52, 1
         75        ASSIGN_DIM                                               !3, 0
         76        OP_DATA                                                  ~53
   64    77    >   FETCH_DIM_R                                      ~54     !3, 0
         78        INIT_FCALL                                               'pow'
         79        SEND_VAL                                                 2
         80        SEND_VAL                                                 32
         81        DO_ICALL                                         $55     
         82        MUL                                              ~56     $55, ~54
         83        FETCH_DIM_R                                      ~57     !3, 1
         84        ADD                                              ~58     ~56, ~57
         85        ASSIGN                                                   !6, ~58
         86    > > JMP                                                      ->102
   67    87    >   FETCH_DIM_R                                      ~60     !3, 0
         88        SL                                               ~61     ~60, 32
         89        FETCH_DIM_R                                      ~62     !3, 1
         90        BW_OR                                            ~63     ~61, ~62
         91        ASSIGN                                                   !6, ~63
   68    92      > JMPZ_EX                                          ~65     !4, ->95
         93    >   BOOL_NOT                                         ~66     !5
         94        BOOL                                             ~65     ~66
         95    > > JMPZ                                                     ~65, ->102
   69    96    >   BW_AND                                           ~67     !6, 9223372036854775807
         97        MUL                                              ~68     ~67, 2
         98        BW_NOT                                           ~69     !6
         99        ADD                                              ~70     ~68, ~69
        100        ADD                                              ~71     ~70, 1
        101        ASSIGN                                                   !6, ~71
   73   102    > > RETURN                                                   !6
   74   103*     > RETURN                                                   null

End of function unpack_int64

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.29 ms | 1419 KiB | 35 Q