macro lzcntflags(input, output) {
 ZF = (output == 0);
 CF = (input == 0);
 # OF, SF, PF, AF are undefined
}

####
#### LZCNT instructions
####


:LZCNT Reg16, rm16	is vexMode=0 & opsize=0 & $(PRE_66) & $(PRE_F3) & byte=0x0F; byte=0xBD; Reg16 ... & rm16 {

  countTmp:2 = 0;
  inputTmp:2 = rm16;

 <loopbegin>
  if ((inputTmp & 0x8000) != 0) goto <loopend>;

  countTmp = countTmp + 1;
  inputTmp = (inputTmp << 1) | 1;
  goto <loopbegin>;

 <loopend>
  Reg16 = countTmp;
  lzcntflags(rm16, Reg16);
}

:LZCNT Reg32, rm32	is vexMode=0 & opsize=1 & $(PRE_F3) & byte=0x0F; byte=0xBD; Reg32 ... & rm32 {

  countTmp:4 = 0;
  inputTmp:4 = rm32;

 <loopbegin>
  if ((inputTmp & 0x80000000) != 0) goto <loopend>;

  countTmp = countTmp + 1;
  inputTmp = (inputTmp << 1) | 1;
  goto <loopbegin>;

 <loopend>
  Reg32 = countTmp;
  lzcntflags(rm32, Reg32);
}

@ifdef IA64
:LZCNT Reg64, rm64	is vexMode=0 & opsize=2 & $(PRE_F3) & $(REX_W) & byte=0x0F; byte=0xBD; Reg64 ... & rm64 {

  countTmp:8 = 0;
  inputTmp:8 = rm64;

 <loopbegin>
  if ((inputTmp & 0x8000000000000000) != 0) goto <loopend>;

  countTmp = countTmp + 1;
  inputTmp = (inputTmp << 1) | 1;
  goto <loopbegin>;

 <loopend>
  Reg64 = countTmp;
  lzcntflags(rm64, Reg64);
}
@endif

