Advertisement
Guest User

Untitled

a guest
May 9th, 2018
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.05 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Security;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. using System.Runtime.CompilerServices;
  7.  
  8. using alib;
  9. using alib.Enumerable;
  10. using alib.Debugging;
  11.  
  12. namespace ConsoleApp5
  13. {
  14.     [SuppressUnmanagedCodeSecurity]
  15.     public static class _test_A
  16.     {
  17.         static _test_A()
  18.         {
  19.             highest_one_16 = init_highest_1s();
  20.         }
  21.  
  22.         [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  23.         static readonly sbyte[] highest_one_16;
  24.  
  25.         static sbyte[] init_highest_1s()
  26.         {
  27.             int i, c, j;
  28.             sbyte n;
  29.             var p = new sbyte[0x10000];
  30.  
  31.             p[0] = -1;
  32.             j = 1;
  33.             for (n = 0, c = 1; n < 16; c <<= 1, n++)
  34.                 for (i = 0; i < c; i++)
  35.                     p[j++] = n;
  36.  
  37.             return p;
  38.         }
  39.  
  40.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  41.         public static unsafe int HighestOne0(ulong ul)
  42.         {
  43.             byte* pb = (byte*)&ul;          // CIL: 35
  44.             int i = *(ushort*)(pb + 6) == 0 ?
  45.                     *(ushort*)(pb + 4) == 0 ?
  46.                     *(ushort*)(pb + 2) == 0 ? 0 : 2 : 4 : 6;
  47.             return (i << 3) + highest_one_16[*(ushort*)(pb + i)];
  48.         }
  49.  
  50.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  51.         public static unsafe int HighestOne1(ulong ul)
  52.         {
  53.             // this one is more sensitive to the data, performance varies more widely than the others, even more with the neg-check
  54.             byte* pb = (byte*)&ul + 6;      // CIL: 3B
  55.  
  56.             if (*(int*)(pb - 2) == 0)
  57.                 pb -= 4;
  58.  
  59.             if (*(ushort*)pb == 0)
  60.                 pb -= 2;
  61.  
  62.             return ((int)(pb - (long)&ul) << 3) + highest_one_16[*(ushort*)pb];
  63.         }
  64.  
  65.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  66.         public static unsafe int HighestOne2(ulong ul)
  67.         {
  68.             byte* pb = (byte*)&ul;          // CIL: 3A
  69.  
  70.             return highest_one_16[
  71.                 *(ushort*)((*(int*)(pb + 4) != 0 ?
  72.                     (*(ushort*)(pb += 6)) :
  73.                     (*(ushort*)(pb += 2))) != 0 ?
  74.                         pb : (pb -= 2))] + ((int)(pb - (long)&ul) << 3);
  75.         }
  76.  
  77.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  78.         public static unsafe int HighestOne3(ulong ul)
  79.         {
  80.             byte* pb = (byte*)&ul;          // CIL: 44
  81.  
  82.             return highest_one_16[
  83.                 *(ushort*)
  84.                     (*(int*)(pb + 4) != 0 ?
  85.                         (*(ushort*)(pb + 6) != 0 ?
  86.                              (pb += 6) :
  87.                              (pb += 4)) :
  88.                         (*(ushort*)(pb + 2) != 0 ?
  89.                              (pb += 2) :
  90.                              pb))
  91.                              ] + ((int)(pb - (long)&ul) << 3);
  92.         }
  93.  
  94.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  95.         public static unsafe int HighestOne4(ulong ul)
  96.         {
  97.             byte* pb = (byte*)&ul;          // CIL: 41
  98.  
  99.             int v;
  100.             return highest_one_16[
  101.                 *(*(int*)(pb + 4) != 0 ?
  102.                         (*(ushort*)(pb + 6) != 0 ?
  103.                              (ushort*)(pb + (v = 6)) :
  104.                              (ushort*)(pb + (v = 4))) :
  105.                         (*(ushort*)(pb + 2) != 0 ?
  106.                              (ushort*)(pb + (v = 2)) :
  107.                              (ushort*)(pb + (v = 0))))
  108.                              ] + (v << 3);
  109.         }
  110.  
  111.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  112.         public static unsafe int HighestOne5(ulong ul)
  113.         {
  114.             byte* pb = (byte*)&ul;          // CIL: 3C
  115.  
  116.             pb += (*(int*)(pb + 4) != 0 ?
  117.                 (*(ushort*)(pb + 6) != 0 ? 6 : 4) :
  118.                 (*(ushort*)(pb + 2) != 0 ? 2 : 0));
  119.  
  120.             return highest_one_16[*(ushort*)pb] + ((int)(pb - (long)&ul) << 3);
  121.         }
  122.  
  123.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  124.         public static unsafe int HighestOne6(ulong ul)
  125.         {
  126.             byte* pb = (byte*)&ul;          // CIL: 35
  127.  
  128.             return highest_one_16[*(ushort*)
  129.                                   (*(ushort*)(pb += (*(int*)(pb + 4) != 0 ? 6 : 2)) == 0 ?
  130.                                         pb -= 2 :
  131.                                         pb)] + ((int)(pb - (long)&ul) << 3);
  132.         }
  133.  
  134.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  135.         public static unsafe int HighestOne7(ulong ul)
  136.         {
  137.             byte* pb = (byte*)&ul;          // CIL: 34
  138.  
  139.             return ((int)(-(long)pb +
  140.                             (*(ushort*)(pb += *(int*)(pb + 4) != 0 ? 6 : 2) == 0 ?
  141.                                 pb -= 2 :
  142.                                 pb)) << 3) + highest_one_16[*(ushort*)pb];
  143.         }
  144.  
  145.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  146.         public static int HighestOne8(ulong ul)
  147.         {
  148.             int i, j;
  149.             return
  150.                 (i = highest_one_16[ul >> (j = 48)]) < 0 &&
  151.                 (i = highest_one_16[ul >> (j = 32)]) < 0 &&
  152.                 (i = highest_one_16[ul >> (j = 16)]) < 0 ?
  153.                     highest_one_16[(ushort)ul] :
  154.                     (i + j);
  155.         }
  156.     };
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement