, rdi . , edi , 2 mov ecx, edi. , - !
, , rdi 0, . , sub edi, edx - rcx. ( , 32 , sub rdi, rdx, , . add/sub; .)
, , , 255 , mov cl, -1 (2 ). rcx 0xFF , . ( Nehalem , RCX, RCX). , mov al, -2/sub al, cl, 8- . .
, rcx , , .
lea ecx,[rax-1] , xor-zeroed eax, 1 uop 1 .
, , xor-zeroed, 3- lea 3- , . (. CPU 1 ).
, , cpu, .
, repne scasb . 15 Intel, , Agner Fog, >= 6n >= 2n , n - (.. 2 , , ), lea.
- ecx , lea.
repne scasb, , , , , pcmpeqb/pmovmsbk/cmp. integer cmp/jne , 4 8 ( 0), , , .. "" . , . , = 7 4, 2 1 , dword, 1 . cmp dword [rdi], first_4_bytes / jne; cmp dword [rdi+3], last_4_bytes / jne.
LEA
CPU Sandybridge lea , xor -zero . xor -zeroing /, uop ROB " ". , RAX. ( xor lea, , RAX , lea , , .)
lea port0 port1 SnB port1/port5 Skylake (2 , SnB). 1- , .
, mov ecx, -1 (5 ), ALU.
AMD Ryzen, lea r32, [m] 64- "" LEA, 2 2 c 1. , Ryzen xor-zeroing.
microbenchmark , . , , lea - .
- , . , , , jcc, + . ( , ).
stc/sbb ecx,ecx , AMD sbb ( CF, ). Intel Haswell , sbb 2 uop ( 3 : 2 GP +). 2 , . ( , .)
, , strlen+2, - . dec ecx - 1 32- , x86-64 inc/dec. , /dec 64- .
repne scas ecx = -len - 2 ( ecx = -1), and gives you -x-1 (i.e. + len + 2 - 1`).
; eax = 0
; ecx = -1
repne scasb ; ecx = -len - 2
sub eax, ecx ; eax = +len + 2