3v4l.org

run code in 300+ PHP versions simultaneously
<?php define('INT_SIGNED', 1); define('INT_LE', 2); function is32Bit() { // This is for max compatibility: // PHP_INT_MAX doesn't exist in PHP4 // current() *requires* a reference in PHP <5.1.0 (except 4.4.1, dafuq) $test = unpack('N', "\xFF\xFF\xFF\xFF"); return current($test) === -1; } function unpack_int32($str, $flags = 0) { // Make sure data is sane $str = (string) $str; $length = strlen($str); if ($length < 4) { return false; } else if ($length > 4) { $str = substr($str, 0, 4); } // Normalize to big endian and unpack to int32 if ($flags & INT_LE) { $str = strrev($str); } $result = unpack('N', $str); $result = current($result); // Sign and word size info $signBit = (bool) ($result & 0x80000000); $isSigned = (bool) ($flags & INT_SIGNED); $is32bit = is32Bit(); if ($signBit && $isSigned && !$is32bit) { // Left pad with set bits for negative numbers on 64-bit $result = (0x00000000FFFFFFFF << 32) | $result; } else if ($signBit && !$isSigned && $is32bit) { // Convert int32 to unsigned float $result = (($result & 0x7FFFFFFF) * 2) + ~$result + 1; } return $result; } function unpack_int64($str, $flags = 0) { // Make sure data is sane $str = (string) $str; $length = strlen($str); if ($length < 8) { return false; } else if ($length > 8) { $str = substr($str, 0, 8); } // Normalize to big endian and unpack to int32s if ($flags & INT_LE) { $str = strrev($str); } $int32s = array_values(unpack('N*', $str)); // Sign and word size info $signBit = (bool) ($int32s[0] & 0x80000000); $isSigned = (bool) ($flags & INT_SIGNED); $is32bit = is32Bit(); if ($is32bit) { if ($int32s[1] & 0x80000000) { // Convert second int32 to unsigned float $int32s[1] = (($int32s[1] & 0x7FFFFFFF) * 2) + ~$int32s[1] + 1; } if ($isSigned && $signBit) { // Negative $result = ($int32s[0] * pow(2, 32)) + $int32s[1]; } else { // Positive if ($signBit) { // Convert first int32 to unsigned float $int32s[0] = (($int32s[0] & 0x7FFFFFFF) * 2) + ~$int32s[0] + 1; } $result = ($int32s[0] * pow(2, 32)) + $int32s[1]; } } else { // Combine int32s into int64 $result = ($int32s[0] << 32) | $int32s[1]; if ($signBit && !$isSigned) { // Convert to negative $result = (($result & 0x7FFFFFFFFFFFFFFF) * 2) + ~$result + 1; } } return $result; } echo "int32:\n"; printf("%f\n", unpack_int32("\xFF\xFC\x00\x80")); printf("%f\n", unpack_int32("\xFF\xFC\x00\x80", INT_SIGNED)); printf("%f\n", unpack_int32("\xFF\xFC\x00\x80", INT_LE)); printf("%f\n", unpack_int32("\xFF\xFC\x00\x80", INT_LE | INT_SIGNED)); echo "\n"; echo "int64:\n"; printf("%f\n", unpack_int64("\xFF\xFF\xFF\xFC\x00\x00\x00\x80")); printf("%f\n", unpack_int64("\xFF\xFF\xFF\xFC\x00\x00\x00\x80", INT_SIGNED)); printf("%f\n", unpack_int64("\xFF\xFF\xFF\xFC\x00\x00\x00\x80", INT_LE)); printf("%f\n", unpack_int64("\xFF\xFF\xFF\xFC\x00\x00\x00\x80", INT_LE | INT_SIGNED));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iAOOG
function name:  (null)
number of ops:  84
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                                                 
  116     8        ECHO                                                     'int32%3A%0A'
  117     9        INIT_FCALL                                               'printf'
         10        SEND_VAL                                                 '%25f%0A'
         11        INIT_FCALL                                               'unpack_int32'
         12        SEND_VAL                                                 '%FF%FC%00%80'
         13        DO_FCALL                                      0  $2      
         14        SEND_VAR                                                 $2
         15        DO_ICALL                                                 
  118    16        INIT_FCALL                                               'printf'
         17        SEND_VAL                                                 '%25f%0A'
         18        INIT_FCALL                                               'unpack_int32'
         19        SEND_VAL                                                 '%FF%FC%00%80'
         20        FETCH_CONSTANT                                   ~4      'INT_SIGNED'
         21        SEND_VAL                                                 ~4
         22        DO_FCALL                                      0  $5      
         23        SEND_VAR                                                 $5
         24        DO_ICALL                                                 
  119    25        INIT_FCALL                                               'printf'
         26        SEND_VAL                                                 '%25f%0A'
         27        INIT_FCALL                                               'unpack_int32'
         28        SEND_VAL                                                 '%FF%FC%00%80'
         29        FETCH_CONSTANT                                   ~7      'INT_LE'
         30        SEND_VAL                                                 ~7
         31        DO_FCALL                                      0  $8      
         32        SEND_VAR                                                 $8
         33        DO_ICALL                                                 
  120    34        INIT_FCALL                                               'printf'
         35        SEND_VAL                                                 '%25f%0A'
         36        INIT_FCALL                                               'unpack_int32'
         37        SEND_VAL                                                 '%FF%FC%00%80'
         38        FETCH_CONSTANT                                   ~10     'INT_LE'
         39        FETCH_CONSTANT                                   ~11     'INT_SIGNED'
         40        BW_OR                                            ~12     ~10, ~11
         41        SEND_VAL                                                 ~12
         42        DO_FCALL                                      0  $13     
         43        SEND_VAR                                                 $13
         44        DO_ICALL                                                 
  122    45        ECHO                                                     '%0A'
  124    46        ECHO                                                     'int64%3A%0A'
  125    47        INIT_FCALL                                               'printf'
         48        SEND_VAL                                                 '%25f%0A'
         49        INIT_FCALL                                               'unpack_int64'
         50        SEND_VAL                                                 '%FF%FF%FF%FC%00%00%00%80'
         51        DO_FCALL                                      0  $15     
         52        SEND_VAR                                                 $15
         53        DO_ICALL                                                 
  126    54        INIT_FCALL                                               'printf'
         55        SEND_VAL                                                 '%25f%0A'
         56        INIT_FCALL                                               'unpack_int64'
         57        SEND_VAL                                                 '%FF%FF%FF%FC%00%00%00%80'
         58        FETCH_CONSTANT                                   ~17     'INT_SIGNED'
         59        SEND_VAL                                                 ~17
         60        DO_FCALL                                      0  $18     
         61        SEND_VAR                                                 $18
         62        DO_ICALL                                                 
  127    63        INIT_FCALL                                               'printf'
         64        SEND_VAL                                                 '%25f%0A'
         65        INIT_FCALL                                               'unpack_int64'
         66        SEND_VAL                                                 '%FF%FF%FF%FC%00%00%00%80'
         67        FETCH_CONSTANT                                   ~20     'INT_LE'
         68        SEND_VAL                                                 ~20
         69        DO_FCALL                                      0  $21     
         70        SEND_VAR                                                 $21
         71        DO_ICALL                                                 
  128    72        INIT_FCALL                                               'printf'
         73        SEND_VAL                                                 '%25f%0A'
         74        INIT_FCALL                                               'unpack_int64'
         75        SEND_VAL                                                 '%FF%FF%FF%FC%00%00%00%80'
         76        FETCH_CONSTANT                                   ~23     'INT_LE'
         77        FETCH_CONSTANT                                   ~24     'INT_SIGNED'
         78        BW_OR                                            ~25     ~23, ~24
         79        SEND_VAL                                                 ~25
         80        DO_FCALL                                      0  $26     
         81        SEND_VAR                                                 $26
         82        DO_ICALL                                                 
         83      > RETURN                                                   1

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

End of function unpack_int64

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
170.3 ms | 1423 KiB | 39 Q