Advertisement
Guest User

q3.c

a guest
Aug 2nd, 2023
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.14 KB | Software | 0 0
  1. #define _POSIX_C_SOURCE 200809L
  2. #define LOG_ENABLE
  3. #include "types.h"
  4. #include "log.h"
  5. #include "utils.h"
  6. #include "profiler.h"
  7.  
  8. /*
  9.  === Compiled ===
  10.  clang -O3 -fno-inline-functions
  11.  
  12.  === Output ===
  13. test: contains_color_basic()
  14.                          End_pixel: 0b01010110, End_color: 0b00000010
  15. test: contains_color_lut()
  16.                          End_pixel: 0b01010110, End_color: 0b00000010
  17. test: contains_color_simd_shift()
  18.                          End_pixel: 0b01010110, End_color: 0b00000010
  19. test: contains_color_simd_mul()
  20.                          End_pixel: 0b01010110, End_color: 0b00000010
  21. test: contains_color_paul_shift()
  22.                          End_pixel: 0b01010110, End_color: 0b00000010
  23. test: contains_color_paul_mul()
  24.                          End_pixel: 0b01010110, End_color: 0b00000010
  25. === Profiling Results ===
  26.     Total Time: 3265.87 ms
  27.     test: contains_color_basic():         1 hits, 248.45 ms ( 7.61%) with children, 248.45 ms ( 7.61%) no children
  28.     test: contains_color_lut():           1 hits, 650.16 ms (19.91%) with children, 650.16 ms (19.91%) no children
  29.     test: contains_color_simd_shift():    1 hits, 630.71 ms (19.31%) with children, 630.71 ms (19.31%) no children
  30.     test: contains_color_simd_mul():      1 hits, 600.76 ms (18.40%) with children, 600.76 ms (18.40%) no children
  31.     test: contains_color_paul_shift():    1 hits, 613.90 ms (18.80%) with children, 613.90 ms (18.80%) no children
  32.     test: contains_color_paul_mul():      1 hits, 521.83 ms (15.98%) with children, 521.83 ms (15.98%) no children
  33.  
  34.  === Disasm ===
  35. 00000000000016c0 <contains_color_basic>:
  36.     16c0:   89 f9                   mov    ecx,edi
  37.     16c2:   83 e1 03                and    ecx,0x3
  38.     16c5:   b8 01 00 00 00          mov    eax,0x1
  39.     16ca:   39 f1                   cmp    ecx,esi
  40.     16cc:   74 22                   je     16f0 <contains_color_basic+0x30>
  41.     16ce:   89 f9                   mov    ecx,edi
  42.     16d0:   c1 e9 02                shr    ecx,0x2
  43.     16d3:   83 e1 03                and    ecx,0x3
  44.     16d6:   39 f1                   cmp    ecx,esi
  45.     16d8:   74 16                   je     16f0 <contains_color_basic+0x30>
  46.     16da:   89 f9                   mov    ecx,edi
  47.     16dc:   c1 e9 04                shr    ecx,0x4
  48.     16df:   83 e1 03                and    ecx,0x3
  49.     16e2:   39 f1                   cmp    ecx,esi
  50.     16e4:   74 0a                   je     16f0 <contains_color_basic+0x30>
  51.     16e6:   c1 ef 06                shr    edi,0x6
  52.     16e9:   31 c0                   xor    eax,eax
  53.     16eb:   39 f7                   cmp    edi,esi
  54.     16ed:   0f 94 c0                sete   al
  55.     16f0:   c3                      ret    
  56.     16f1:   66 2e 0f 1f 84 00 00    cs nop WORD PTR [rax+rax*1+0x0]
  57.     16f8:   00 00 00
  58.     16fb:   0f 1f 44 00 00          nop    DWORD PTR [rax+rax*1+0x0]
  59.  
  60. 0000000000001700 <init_lut>:
  61.     1700:   41 56                   push   r14
  62.     1702:   53                      push   rbx
  63.     1703:   50                      push   rax
  64.     1704:   31 db                   xor    ebx,ebx
  65.     1706:   4c 8d 35 c3 49 00 00    lea    r14,[rip+0x49c3]        # 60d0 <matches_lut>
  66.     170d:   0f 1f 00                nop    DWORD PTR [rax]
  67.     1710:   0f b6 fb                movzx  edi,bl
  68.     1713:   31 f6                   xor    esi,esi
  69.     1715:   e8 a6 ff ff ff          call   16c0 <contains_color_basic>
  70.     171a:   42 88 04 33             mov    BYTE PTR [rbx+r14*1],al
  71.     171e:   48 83 c3 01             add    rbx,0x1
  72.     1722:   48 81 fb 00 01 00 00    cmp    rbx,0x100
  73.     1729:   75 e5                   jne    1710 <init_lut+0x10>
  74.     172b:   48 83 c4 08             add    rsp,0x8
  75.     172f:   5b                      pop    rbx
  76.     1730:   41 5e                   pop    r14
  77.     1732:   c3                      ret    
  78.     1733:   66 2e 0f 1f 84 00 00    cs nop WORD PTR [rax+rax*1+0x0]
  79.     173a:   00 00 00
  80.     173d:   0f 1f 00                nop    DWORD PTR [rax]
  81.  
  82. 0000000000001740 <contains_color_lut>:
  83.     1740:   89 f0                   mov    eax,esi
  84.     1742:   48 8d 0d 3f 29 00 00    lea    rcx,[rip+0x293f]        # 4088 <color_dup_lut>
  85.     1749:   40 32 3c 08             xor    dil,BYTE PTR [rax+rcx*1]
  86.     174d:   40 0f b6 c7             movzx  eax,dil
  87.     1751:   48 8d 0d 78 49 00 00    lea    rcx,[rip+0x4978]        # 60d0 <matches_lut>
  88.     1758:   0f b6 04 08             movzx  eax,BYTE PTR [rax+rcx*1]
  89.     175c:   c3                      ret    
  90.     175d:   0f 1f 00                nop    DWORD PTR [rax]
  91.  
  92. 0000000000001760 <contains_color_simd_shift>:
  93.     1760:   8d 04 b5 00 00 00 00    lea    eax,[rsi*4+0x0]
  94.     1767:   40 08 f0                or     al,sil
  95.     176a:   89 f1                   mov    ecx,esi
  96.     176c:   c0 e1 04                shl    cl,0x4
  97.     176f:   40 c0 e6 06             shl    sil,0x6
  98.     1773:   40 08 ce                or     sil,cl
  99.     1776:   40 08 c6                or     sil,al
  100.     1779:   40 30 fe                xor    sil,dil
  101.     177c:   89 f1                   mov    ecx,esi
  102.     177e:   d0 e9                   shr    cl,1
  103.     1780:   40 08 f1                or     cl,sil
  104.     1783:   f6 d1                   not    cl
  105.     1785:   31 c0                   xor    eax,eax
  106.     1787:   f6 c1 55                test   cl,0x55
  107.     178a:   0f 95 c0                setne  al
  108.     178d:   c3                      ret    
  109.     178e:   66 90                   xchg   ax,ax
  110.  
  111. 0000000000001790 <contains_color_simd_mul>:
  112.     1790:   6b c6 55                imul   eax,esi,0x55
  113.     1793:   31 f8                   xor    eax,edi
  114.     1795:   89 c1                   mov    ecx,eax
  115.     1797:   d0 e9                   shr    cl,1
  116.     1799:   08 c1                   or     cl,al
  117.     179b:   f6 d1                   not    cl
  118.     179d:   31 c0                   xor    eax,eax
  119.     179f:   f6 c1 55                test   cl,0x55
  120.     17a2:   0f 95 c0                setne  al
  121.     17a5:   c3                      ret    
  122.     17a6:   66 2e 0f 1f 84 00 00    cs nop WORD PTR [rax+rax*1+0x0]
  123.     17ad:   00 00 00
  124.  
  125. 00000000000017b0 <contains_color_paul_shift>:
  126.     17b0:   8d 04 b5 00 00 00 00    lea    eax,[rsi*4+0x0]
  127.     17b7:   40 08 f0                or     al,sil
  128.     17ba:   89 f1                   mov    ecx,esi
  129.     17bc:   c0 e1 04                shl    cl,0x4
  130.     17bf:   40 c0 e6 06             shl    sil,0x6
  131.     17c3:   40 08 ce                or     sil,cl
  132.     17c6:   40 08 c6                or     sil,al
  133.     17c9:   40 30 fe                xor    sil,dil
  134.     17cc:   40 f6 d6                not    sil
  135.     17cf:   89 f0                   mov    eax,esi
  136.     17d1:   d0 e8                   shr    al,1
  137.     17d3:   40 20 f0                and    al,sil
  138.     17d6:   24 55                   and    al,0x55
  139.     17d8:   0f b6 c0                movzx  eax,al
  140.     17db:   c3                      ret    
  141.     17dc:   0f 1f 40 00             nop    DWORD PTR [rax+0x0]
  142.  
  143. 00000000000017e0 <contains_color_paul_mul>:
  144.     17e0:   6b c6 55                imul   eax,esi,0x55
  145.     17e3:   31 f8                   xor    eax,edi
  146.     17e5:   f6 d0                   not    al
  147.     17e7:   89 c1                   mov    ecx,eax
  148.     17e9:   d0 e9                   shr    cl,1
  149.     17eb:   20 c1                   and    cl,al
  150.     17ed:   80 e1 55                and    cl,0x55
  151.     17f0:   0f b6 c1                movzx  eax,cl
  152.     17f3:   c3                      ret    
  153.     17f4:   66 2e 0f 1f 84 00 00    cs nop WORD PTR [rax+rax*1+0x0]
  154.     17fb:   00 00 00
  155.     17fe:   66 90                   xchg   ax,ax
  156. */
  157.  
  158. bool contains_color_basic(u8 pixel, u8 color)
  159. {
  160.     return  (((pixel >> 0) & 0b11) == color) ||
  161.             (((pixel >> 2) & 0b11) == color) ||
  162.             (((pixel >> 4) & 0b11) == color) ||
  163.             (((pixel >> 6) & 0b11) == color);
  164. }
  165.  
  166. u8 matches_lut[256] = {0};
  167. u8 color_dup_lut[4] = { 0b00000000, 0b01010101, 0b10101010, 0b11111111, };
  168.  
  169. void init_lut()
  170. {
  171.     for (u32 i = 0; i < 256; ++i)
  172.     {
  173.         matches_lut[i] = contains_color_basic((u8) i, 0);
  174.     }
  175. }
  176.  
  177. bool contains_color_lut(u8 pixel, u8 color)
  178. {
  179.     return matches_lut[color_dup_lut[color] ^ pixel];
  180. }
  181.  
  182. bool contains_color_simd_shift(u8 pixel, u8 color)
  183. {
  184.     u8 mask = 0b01010101;
  185.     color = (color << 0) | (color << 2) | (color << 4) | (color << 6);
  186.  
  187.     u8 xored = pixel ^ color;
  188.     u8 right_bits = xored & mask;
  189.     u8 left_bits  = (xored >> 1) & mask;
  190.  
  191.     return ((left_bits | right_bits) != mask);
  192. }
  193.  
  194. bool contains_color_simd_mul(u8 pixel, u8 color)
  195. {
  196.     u8 mask = 0b01010101;
  197.     color *= mask;
  198.  
  199.     u8 xored = pixel ^ color;
  200.     u8 right_bits = xored & mask;
  201.     u8 left_bits  = (xored >> 1) & mask;
  202.  
  203.     return ((left_bits | right_bits) != mask);
  204. }
  205.  
  206. bool contains_color_paul_shift(u8 pixel, u8 color)
  207. {
  208.     u8 mask = 0b01010101;
  209.     color = (color << 0) | (color << 2) | (color << 4) | (color << 6);
  210.  
  211.     u8 x = pixel ^ (~color);
  212.     u8 y = x & (x >> 1);
  213.     u8 z = y & mask;
  214.  
  215.     return z;
  216. }
  217.  
  218. bool contains_color_paul_mul(u8 pixel, u8 color)
  219. {
  220.     u8 mask = 0b01010101;
  221.     color *= mask;
  222.  
  223.     u8 x = pixel ^ (~color);
  224.     u8 y = x & (x >> 1);
  225.     u8 z = y & mask;
  226.  
  227.     return z;
  228. }
  229.  
  230. int main()
  231. {
  232.     profiler_start();
  233.  
  234.     u8 pixel_start = 0b10010110;
  235.     u8 color_start = 0b01;
  236.  
  237.     u8 pixel_inc_true  = 55;
  238.     u8 pixel_inc_false = 103;
  239.  
  240.     u8 color_inc_true  = 1;
  241.     u8 color_inc_false = 2;
  242.  
  243.     u32 iterations = 100000000;
  244.  
  245.     char *test_name = "";
  246.     u8 pixel = 0;
  247.     u8 color = 0;
  248.  
  249.     test_name = "test: contains_color_basic()";
  250.     pixel = pixel_start;
  251.     color = color_start;
  252.     {
  253.         profile_block(test_name);
  254.  
  255.         for (u32 i = 0; i < iterations; ++i)
  256.         {
  257.             pixel += contains_color_basic(pixel, color) ? pixel_inc_true : pixel_inc_false;
  258.             color += contains_color_basic(pixel, color) ? color_inc_true : color_inc_false;
  259.             color &= 0b00000011;
  260.         }
  261.     }
  262.     LOG("%s\n\t\t\t\t\t\t End_pixel: 0b%08b, End_color: 0b%08b", test_name, pixel, color);
  263.  
  264.     test_name = "test: contains_color_lut()";
  265.     pixel = pixel_start;
  266.     color = color_start;
  267.     init_lut();
  268.     {
  269.         profile_block(test_name);
  270.  
  271.         for (u32 i = 0; i < iterations; ++i)
  272.         {
  273.             pixel += contains_color_lut(pixel, color) ? pixel_inc_true : pixel_inc_false;
  274.             color += contains_color_lut(pixel, color) ? color_inc_true : color_inc_false;
  275.             color &= 0b00000011;
  276.         }
  277.     }
  278.     LOG("%s\n\t\t\t\t\t\t End_pixel: 0b%08b, End_color: 0b%08b", test_name, pixel, color);
  279.  
  280.     test_name = "test: contains_color_simd_shift()";
  281.     pixel = pixel_start;
  282.     color = color_start;
  283.     {
  284.         profile_block(test_name);
  285.  
  286.         for (u32 i = 0; i < iterations; ++i)
  287.         {
  288.             pixel += contains_color_simd_shift(pixel, color) ? pixel_inc_true : pixel_inc_false;
  289.             color += contains_color_simd_shift(pixel, color) ? color_inc_true : color_inc_false;
  290.             color &= 0b00000011;
  291.         }
  292.     }
  293.     LOG("%s\n\t\t\t\t\t\t End_pixel: 0b%08b, End_color: 0b%08b", test_name, pixel, color);
  294.  
  295.     test_name = "test: contains_color_simd_mul()";
  296.     pixel = pixel_start;
  297.     color = color_start;
  298.     {
  299.         profile_block(test_name);
  300.  
  301.         for (u32 i = 0; i < iterations; ++i)
  302.         {
  303.             pixel += contains_color_simd_mul(pixel, color) ? pixel_inc_true : pixel_inc_false;
  304.             color += contains_color_simd_mul(pixel, color) ? color_inc_true : color_inc_false;
  305.             color &= 0b00000011;
  306.         }
  307.     }
  308.     LOG("%s\n\t\t\t\t\t\t End_pixel: 0b%08b, End_color: 0b%08b", test_name, pixel, color);
  309.  
  310.     test_name = "test: contains_color_paul_shift()";
  311.     pixel = pixel_start;
  312.     color = color_start;
  313.     {
  314.         profile_block(test_name);
  315.  
  316.         for (u32 i = 0; i < iterations; ++i)
  317.         {
  318.             pixel += contains_color_paul_shift(pixel, color) ? pixel_inc_true : pixel_inc_false;
  319.             color += contains_color_paul_shift(pixel, color) ? color_inc_true : color_inc_false;
  320.             color &= 0b00000011;
  321.         }
  322.     }
  323.     LOG("%s\n\t\t\t\t\t\t End_pixel: 0b%08b, End_color: 0b%08b", test_name, pixel, color);
  324.  
  325.     test_name = "test: contains_color_paul_mul()";
  326.     pixel = pixel_start;
  327.     color = color_start;
  328.     {
  329.         profile_block(test_name);
  330.  
  331.         for (u32 i = 0; i < iterations; ++i)
  332.         {
  333.             pixel += contains_color_paul_mul(pixel, color) ? pixel_inc_true : pixel_inc_false;
  334.             color += contains_color_paul_mul(pixel, color) ? color_inc_true : color_inc_false;
  335.             color &= 0b00000011;
  336.         }
  337.     }
  338.     LOG("%s\n\t\t\t\t\t\t End_pixel: 0b%08b, End_color: 0b%08b", test_name, pixel, color);
  339.  
  340.     profiler_end_and_print();
  341.  
  342.     return 0;
  343. }
  344.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement