3v4l.org

run code in 300+ PHP versions simultaneously
<?php function isLongerThanCT(string $input, int $targetLength): bool { $amount = PHP_INT_SIZE === 8 ? 63 : 31; $len = strlen($input); // use mb_strlen($input, '8bit'); on older PHP $res = (($targetLength - $len) >> $amount) & 1; /* * It's worth taking a moment to explain what the hell the line above is doing. * * ($targetLength - $len) will return a negative value if $targetLength is larger than $len. * * By two's complement, negative values when shifted 31 or 63 places to the right will have a lower * bit set. We then use a bit mask of `1` and the bitwise `&` operator on the result of the bitshift. * * End result? It's the same as if we did the following, except constant-time: * $res = (int) ($len > $targetLength); */ return (bool) ($res); } $long = str_repeat('A', 72); var_dump( isLongerThanCT($long, 71), isLongerThanCT($long, 72), isLongerThanCT($long, 73) );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ebQoq
function name:  (null)
number of ops:  23
compiled vars:  !0 = $long
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   INIT_FCALL                                               'str_repeat'
          1        SEND_VAL                                                 'A'
          2        SEND_VAL                                                 72
          3        DO_ICALL                                         $1      
          4        ASSIGN                                                   !0, $1
   23     5        INIT_FCALL                                               'var_dump'
   24     6        INIT_FCALL                                               'islongerthanct'
          7        SEND_VAR                                                 !0
          8        SEND_VAL                                                 71
          9        DO_FCALL                                      0  $3      
         10        SEND_VAR                                                 $3
   25    11        INIT_FCALL                                               'islongerthanct'
         12        SEND_VAR                                                 !0
         13        SEND_VAL                                                 72
         14        DO_FCALL                                      0  $4      
         15        SEND_VAR                                                 $4
   26    16        INIT_FCALL                                               'islongerthanct'
         17        SEND_VAR                                                 !0
         18        SEND_VAL                                                 73
         19        DO_FCALL                                      0  $5      
         20        SEND_VAR                                                 $5
   23    21        DO_ICALL                                                 
   27    22      > RETURN                                                   1

Function islongerthanct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ebQoq
function name:  isLongerThanCT
number of ops:  18
compiled vars:  !0 = $input, !1 = $targetLength, !2 = $amount, !3 = $len, !4 = $res
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2      > JMPZ                                                     <true>, ->5
          3    >   QM_ASSIGN                                        ~5      63
          4      > JMP                                                      ->6
          5    >   QM_ASSIGN                                        ~5      31
          6    >   ASSIGN                                                   !2, ~5
    6     7        STRLEN                                           ~7      !0
          8        ASSIGN                                                   !3, ~7
    7     9        SUB                                              ~9      !1, !3
         10        SR                                               ~10     ~9, !2
         11        BW_AND                                           ~11     ~10, 1
         12        ASSIGN                                                   !4, ~11
   19    13        BOOL                                             ~13     !4
         14        VERIFY_RETURN_TYPE                                       ~13
         15      > RETURN                                                   ~13
   20    16*       VERIFY_RETURN_TYPE                                       
         17*     > RETURN                                                   null

End of function islongerthanct

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
141.8 ms | 1467 KiB | 18 Q