daily pastebin goal
31%
SHARE
TWEET

cmov

a guest Jan 19th, 2018 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. inline int32_t cmov_nz(uint32_t val)
  2. {
  3. #if defined(__GNUC__)
  4.   uint32_t ret;
  5.   __asm__
  6.   (
  7.     "       .att_syntax   noprefix  \n\t"
  8.     "cmp             $0,  %1        \n\t"
  9.     "mov    $0xffffffff,  %%ecx     \n\t"  // -1
  10.     "xor          %%eax,  %%eax     \n\t"  //  0
  11.     "cmovnz       %%ecx,  %%eax     \n\t"
  12.     "mov          %%eax,  %0        \n\t"  // ret = %eax
  13.     : "=g" (ret)
  14.     : "g" (val)
  15.     : "eax", "ecx", "cc"
  16.   );
  17.   return (int32_t)ret;
  18. #else
  19.   const int32_t v = (int32_t) val;
  20.   return (int32_t)((1 & ((uint32_t)(v - 1) >> 8)) - 1);
  21. #endif
  22. }
RAW Paste Data
Top