LZCNT
计算领先零位数
stableVMJITAOTinstruction
编码
| 操作码 | 指令 | Op/En | 64 位 | 兼容/传统 | 说明 |
|---|---|---|---|---|---|
F3 0F BD /r | LZCNT r16, r/m16 | RM | 有效 | 有效 | 计算r/m16中领先零位数,返回结果为r16. |
F3 0F BD /r | LZCNT r32, r/m32 | RM | 有效 | 有效 | 计算r/m32中领先零位数,返回结果为r32. |
F3 REX.W 0F BD /r | LZCNT r64, r/m64 | RM | 有效 | 不可编码 | 计算r/m64中领先零位数,返回结果为r64. |
操作数编码
每个模式对应上表 Op/En 列的一个取值,说明各操作数按书写顺序分别编码在指令的哪个字段,以及指令对它是读、是写还是两者兼有。
RM
modrm.regescrituraModRM 字节的 reg 字段(第 5-3 位)modrm.rmlecturaModRM 字节的 r/m 字段(第 2-0 位);当 mod 字段要求时,还包括 SIB 字节和位移
实测开销
正在从 arch-data 加载实测数据...
涉及的标志位
CF进位PF奇偶AF辅助进位ZF零SF符号OF溢出
说明
LZCNT计算一个源操作数(第二个操作数)中领先最显著的零位数,并在目的地返回结果(第一个操作数). LZCNT是BSR指令的扩展. LZCNT和BSR指令的关键区别在于当源代码为0时,LZCNT会将运行大小输出给目的代码,而BSR则会离开目的代码代码代码未修改.
在不支持LZCNT的处理器上,指令字节编码以BSR执行.
行动
temp := OperandSize - 1
DEST := 0
WHILE (temp >= 0) AND (Bit(SRC, temp) = 0)
DO
temp := temp - 1
DEST := DEST+ 1
OD
IF DEST = OperandSize
CF := 1
ELSE
CF := 0
FI
IF DEST = 0
ZF := 1
ELSE
ZF := 0
FI受影响的旗帜
ZF旗在输出为零时设定为1(来源中最显著的位被设定),而到0时,如果输入为零,否则CF旗被设定为1. OF, SF, PF,和AF的旗帜没有定义.
Intel C/C++ 内在编译器
LZCNT unsigned __int32 _lzcnt_u32(unsigned __int32 src);
LZCNT unsigned __int64 _lzcnt_u64(unsigned __int64 src);异常
保护模式
#GP(0) | 对于CS,DS,ES,FS或GS片段中非法的内存操作数有效地址,如果DS,ES,FS,或GS寄存器用于访问内存,并包含一个无效的段选择子. |
#SS(0) | 用于SS段的一个非法地址. #PF(错误码)用于一个页面错误. |
#AC(0) | 如果启用了对齐检查,并且在当前特权级别为3时会给出不对齐的内存引用. |
#UD | 如果使用 LOCK 前缀 。 |
实地址模式
#GP(0) | 如果操作数的任何部分位于有效地址空间之外,从0到0FFFFH. |
#SS(0) | 在党卫军部分非法地址 |
#UD | 如果使用 LOCK 前缀。 虚拟 8086 模式例外 |
#GP(0) | 如果操作数的任何部分位于有效地址空间之外,从0到0FFFFH. |
#SS(0) | 用于SS段的一个非法地址. #PF(错误码)用于一个页面错误. |
#AC(0) | 如果启用了对齐检查,并且在当前特权级别为3时会给出不对齐的内存引用. |
#UD | 如果使用 LOCK 前缀 。 |
兼容模式
| 与保护模式中的例外相同. |
64 位模式
#GP(0) | 如果内存地址是非冠状的. |
#SS(0) | 如果引用SS段的内存地址为非冠状形式. #PF(错误编码) for a 页面错误. |
#AC(0) | 如果启用了对齐检查,并且在当前特权级别为3时会给出不对齐的内存引用. |
#UD | If LOCK prefix is used. CHAPTER 4 4.1 IMM8 CONTROL BYTE OPERATION FOR PCMPESTRI / PCMPESTRM / PCMPISTRI / PCMPISTRM The notations introduced in this section are referenced in the reference pages of PCMPESTRI, PCMPESTRM, PCMP- ISTRI, PCMPISTRM. The operation of the immediate control byte is common to these four string text processing instructions of SSE4.2. This section describes the common operations. 4.1.1 General Description The operation of PCMPESTRI, PCMPESTRM, PCMPISTRI, PCMPISTRM is defined by the combination of the respective opcode and the interpretation of an immediate control byte that is part of the instruction encoding. The opcode controls the relationship of input bytes/words to each other (determines whether the inputs terminated strings or whether lengths are expressed explicitly) as well as the desired output (index or mask). The imm8 control byte for PCMPESTRM/PCMPESTRI/PCMPISTRM/PCMPISTRI encodes a significant amount of programmable control over the functionality of those instructions. Some functionality is unique to each instruction while some is common across some or all of the four instructions. This section describes functionality which is common across the four instructions. The arithmetic flags (ZF, CF, SF, OF, AF, PF) are set as a result of these instructions. However, the meanings of the flags have been overloaded from their typical meanings in order to provide additional information regarding the relationships of the two inputs. PCMPxSTRx instructions perform arithmetic comparisons between all possible pairs of bytes or words, one from each packed input source operand. The boolean results of those comparisons are then aggregated in order to produce meaningful results. The imm8 control byte is used to affect the interpretation of individual input elements as well as control the arithmetic comparisons used and the specific aggregation scheme. Specifically, the imm8 Control Byte consists of bit fields that control the following attributes: * Source data format -- Byte/word data element granularity, signed or unsigned elements. * Aggregation operation -- Encodes the mode of per-element comparison operation and the aggregation of per-element comparisons into an intermediate result. * Polarity -- Specifies intermediate processing to be performed on the intermediate result. * Output selection -- Specifies final operation to produce the output (depending on index or mask) from the intermediate result. 4.1.2 Source Data Format Table 4-1. Source Data Format Imm8[1:0] Meaning Description 00b Unsigned bytes Both 128-bit sources are treated as packed, unsigned bytes. 01b Unsigned words Both 128-bit sources are treated as packed, unsigned words. 10b Signed bytes Both 128-bit sources are treated as packed, signed bytes. 11b Signed words Both 128-bit sources are treated as packed, signed words. If the imm8 control byte has bit[0] cleared, each source contains 16 packed bytes. If the bit is set each source contains 8 packed words. If the imm8 control byte has bit[1] cleared, each input contains unsigned data. If the bit is set each source contains signed data. 4.1.3 Aggregation Operation Table 4-2. Aggregation Operation Imm8[3:2] Mode Comparison 00b Equal any The arithmetic comparison is "equal." 01b Ranges Arithmetic comparison is "greater than or equal" between even indexed bytes/words of reg and each byte/word of reg/mem. Arithmetic comparison is "less than or equal" between odd indexed bytes/words of reg and each byte/word of reg/mem. (reg/mem[m] >= reg[n] for n = even, reg/mem[m] <= reg[n] for n = odd) 10b Equal each The arithmetic comparison is "equal." 11b Equal ordered The arithmetic comparison is "equal." All 256 (64) possible comparisons are always performed. The individual Boolean results of those comparisons are referred by "BoolRes[Reg/Mem element index, Reg element index]." Comparisons evaluating to "True" are represented with a 1, False with a 0 (positive logic). The initial results are then aggregated into a 16-bit (8-bit) intermediate result (IntRes1) using one of the modes described in the table below, as determined by imm8 control byte bits[3:2]. See Section 4.1.6 for a description of the overrideIfDataInvalid() function used in Table 4-3. Table 4-3. Aggregation Operation Mode Pseudocode Equal any UpperBound = imm8[0] ? 7 : 15; (find characters from a set) IntRes1 = 0; For j = 0 to UpperBound, j++ For i = 0 to UpperBound, i++ IntRes1[j] OR= overrideIfDataInvalid(BoolRes[j,i]) Ranges UpperBound = imm8[0] ? 7 : 15; (find characters from ranges) IntRes1 = 0; For j = 0 to UpperBound, j++ For i = 0 to UpperBound, i+=2 IntRes1[j] OR= (overrideIfDataInvalid(BoolRes[j,i]) AND overrideIfDataInvalid(BoolRes[j,i+1])) Equal each UpperBound = imm8[0] ? 7 : 15; (string compare) IntRes1 = 0; For i = 0 to UpperBound, i++ IntRes1[i] = overrideIfDataInvalid(BoolRes[i,i]) Equal ordered UpperBound = imm8[0] ? 7 :15; (substring search) IntRes1 = imm8[0] ? FFH : FFFFH For j = 0 to UpperBound, j++ For i = 0 to UpperBound-j, k=j to UpperBound, k++, i++ IntRes1[j] AND= overrideIfDataInvalid(BoolRes[k,i]) 4.1.4 Polarity IntRes1 may then be further modified by performing a 1's complement, according to the value of the imm8 control byte bit[4]. Optionally, a mask may be used such that only those IntRes1 bits which correspond to "valid" reg/mem input elements are complemented (note that the definition of a valid input element is dependent on the specific opcode and is defined in each opcode's description). The result of the possible negation is referred to as IntRes2. Imm8[5:4] Operation Table 4-4. Polarity 00b Positive Polarity (+) Description 01b Negative Polarity (-) IntRes2 = IntRes1 10b Masked (+) IntRes2 = -1 XOR IntRes1 11b Masked (-) IntRes2 = IntRes1 IntRes2[i] = IntRes1[i] if reg/mem[i] invalid, else = ~IntRes1[i] 4.1.5 Output Selection Table 4-5. Output Selection Imm8[6] Operation Description 0b Least significant index The index returned to ECX is of the least significant set bit in IntRes2. 1b Most significant index The index returned to ECX is of the most significant set bit in IntRes2. For PCMPESTRI/PCMPISTRI, the imm8 control byte bit[6] is used to determine if the index is of the least significant or most significant bit of IntRes2. Imm8[6] Operation Table 4-6. Output Selection 0b Bit mask Description 1b Byte/word mask IntRes2 is returned as the mask to the least significant bits of XMM0 with zero extension to 128 bits. IntRes2 is expanded into a byte/word mask (based on imm8[1]) and placed in XMM0. The expansion is performed by replicating each bit into all of the bits of the byte/word of the same index. Specifically for PCMPESTRM/PCMPISTRM, the imm8 control byte bit[6] is used to determine if the mask is a 16 (8) bit mask or a 128 bit byte/word mask. 4.1.6 Valid/Invalid Override of Comparisons PCMPxSTRx instructions allow for the possibility that an end-of-string (EOS) situation may occur within the 128-bit packed data value (see the instruction descriptions below for details). Any data elements on either source that are determined to be past the EOS are considered to be invalid, and the treatment of invalid data within a comparison pair varies depending on the aggregation function being performed. In general, the individual comparison result for each element pair BoolRes[i.j] can be forced true or false if one or more elements in the pair are invalid. See Table 4-7. Table 4-7. Comparison Result for Each Element Pair BoolRes[i.j] xmm1 xmm2/ m128 Imm8[3:2] = 00b Imm8[3:2] = 01b Imm8[3:2] = 10b Imm8[3:2] = 11b byte/ word byte/word (equal any) (ranges) (equal each) (equal ordered) Invalid Invalid Force false Force false Force true Force true Invalid Valid Force false Force false Force false Force true Valid Invalid Force false Force false Force false Force false Valid Valid Do not force Do not force Do not force Do not force 4.1.7 Summary of Im8 Control byte Imm8 Table 4-8. Summary of Imm8 Control Byte -------0b Description -------1b 128-bit sources treated as 16 packed bytes. ------0-b 128-bit sources treated as 8 packed words. ------1-b Packed bytes/words are unsigned. ----00--b Packed bytes/words are signed. ----01--b Mode is equal any. ----10--b Mode is ranges. ----11--b Mode is equal each. ---0----b Mode is equal ordered. ---1----b IntRes1 is unmodified. --0-----b IntRes1 is negated (1's complement). --1-----b Negation of IntRes1 is for all 16 (8) bits. -0------b Negation of IntRes1 is masked by reg/mem validity. Index of the least significant, set, bit is used (regardless of corresponding input element validity). -1------b IntRes2 is returned in least significant bits of XMM0. Index of the most significant, set, bit is used (regardless of corresponding input element validity). 0-------b Each bit of IntRes2 is expanded to byte/word. 1-------b This bit currently has no defined effect, should be 0. This bit currently has no defined effect, should be 0. 4.1.8 Diagram Comparison and Aggregation Process Figure 4-1. Operation of PCMPSTRx and PCMPESTRx 4.2 COMMON TRANSFORMATION AND PRIMITIVE FUNCTIONS FOR SHA1XXX AND SHA256XXX The following primitive functions and transformations are used in the algorithmic descriptions of SHA1 and SHA256 instruction extensions SHA1NEXTE, SHA1RNDS4, SHA1MSG1, SHA1MSG2, SHA256RNDS4, SHA256MSG1, and SHA256MSG2. The operands of these primitives and transformation are generally 32-bit DWORD integers. * f0(): A bit oriented logical operation that derives a new dword from three SHA1 state variables (dword). This function is used in SHA1 round 1 to 20 processing. f0(B,C,D) := (B AND C) XOR ((NOT(B) AND D) * f1(): A bit oriented logical operation that derives a new dword from three SHA1 state variables (dword). This function is used in SHA1 round 21 to 40 processing. f1(B,C,D) := B XOR C XOR D * f2(): A bit oriented logical operation that derives a new dword from three SHA1 state variables (dword). This function is used in SHA1 round 41 to 60 processing. f2(B,C,D) := (B AND C) XOR (B AND D) XOR (C AND D) * f3(): A bit oriented logical operation that derives a new dword from three SHA1 state variables (dword). This function is used in SHA1 round 61 to 80 processing. It is the same as f1(). f3(B,C,D) := B XOR C XOR D * Ch(): A bit oriented logical operation that derives a new dword from three SHA256 state variables (dword). Ch(E,F,G) := (E AND F) XOR ((NOT E) AND G) * Maj(): A bit oriented logical operation that derives a new dword from three SHA256 state variables (dword). Maj(A,B,C) := (A AND B) XOR (A AND C) XOR (B AND C) ROR is rotate right operation (A ROR N) := A[N-1:0] || A[Width-1:N] ROL is rotate left operation (A ROL N) := A ROR (Width-N) SHR is the right shift operation (A SHR N) := ZEROES[N-1:0] || A[Width-1:N] * 0( ): A bit oriented logical and rotational transformation performed on a dword SHA256 state variable. 0(A) := (A ROR 2) XOR (A ROR 13) XOR (A ROR 22) * 1( ): A bit oriented logical and rotational transformation performed on a dword SHA256 state variable. 1(E) := (E ROR 6) XOR (E ROR 11) XOR (E ROR 25) * 0( ): A bit oriented logical and rotational transformation performed on a SHA256 message dword used in the message scheduling. 0(W) := (W ROR 7) XOR (W ROR 18) XOR (W SHR 3) * 1( ): A bit oriented logical and rotational transformation performed on a SHA256 message dword used in the message scheduling. 1(W) := (W ROR 17) XOR (W ROR 19) XOR (W SHR 10) * Ki: SHA1 Constants dependent on immediate i. K0 = 0x5A827999 K1 = 0x6ED9EBA1 K2 = 0X8F1BBCDC K3 = 0xCA62C1D6 4.3 INSTRUCTIONS (M-U) Chapter 4 continues an alphabetical discussion of Intel(R) 64 and IA-32 instructions (M-U). See also: Chapter 3, "Instruction Set Reference, A-L," in the Intel(R) 64 and IA-32 Architectures Software Developer's Manual, Volume 2A; Chapter 4, "Instruction Set Reference, M-U," in the Intel(R) 64 and IA-32 Architectures Software Developer's Manual, Volume 2C; and Chapter 4, "Instruction Set Reference, M-U," in the Intel(R) 64 and IA-32 Architectures Software Developer's Manual, Volume 2D. |