pmcgee

Write C++20 char array without branching (I)

Sep 26th, 2020 (edited)
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <bit>
  2. #include <bitset>
  3. #include <iostream>
  4.  
  5.         char*         a = "abcdef";
  6.         unsigned int  i = 0;
  7.         unsigned int  k = 1;            // k to be determined
  8.  
  9. int main() {
  10.         start:
  11.             int b = std::popcount( std::uint8_t(a[i]) ) * k;
  12.             __asm__ ("jmp done+b");
  13.         done:
  14.             goto exit;
  15.         print:
  16.             __asm__ ("nop");            // fill in with
  17.             __asm__ ("nop");            // 8*k nops
  18.             __asm__ ("nop");
  19.             __asm__ ("nop");
  20.             __asm__ ("nop");
  21.             __asm__ ("nop");
  22.             __asm__ ("nop");
  23.             __asm__ ("nop");
  24.             std::cout << a[i++]; goto start;
  25.         exit:
  26.             std::cout << "\n";
  27.             return 0;
  28. }
  29.  
Add Comment
Please, Sign In to add comment