Advertisement
Guest User

Untitled

a guest
May 9th, 2018
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.62 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_B
  16.     {
  17.         static _test_B()
  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.             if ((long)ul <= 0)
  44.                 return (int)((ul >> 57) & 64) - 1;
  45.  
  46.             byte* pb = (byte*)&ul;          // CIL: 35
  47.             int i = *(ushort*)(pb + 6) == 0 ?
  48.                     *(ushort*)(pb + 4) == 0 ?
  49.                     *(ushort*)(pb + 2) == 0 ? 0 : 2 : 4 : 6;
  50.             return (i << 3) + highest_one_16[*(ushort*)(pb + i)];
  51.         }
  52.  
  53.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  54.         public static unsafe int HighestOne1(ulong ul)
  55.         {
  56.             if ((long)ul <= 0)
  57.                 return (int)((ul >> 57) & 64) - 1;
  58.  
  59.             // this one is more sensitive to the data, performance varies more widely than the others, even more with the neg-check
  60.             byte* pb = (byte*)&ul + 6;      // CIL: 3B
  61.  
  62.             if (*(int*)(pb - 2) == 0)
  63.                 pb -= 4;
  64.  
  65.             if (*(ushort*)pb == 0)
  66.                 pb -= 2;
  67.  
  68.             return ((int)(pb - (long)&ul) << 3) + highest_one_16[*(ushort*)pb];
  69.         }
  70.  
  71.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  72.         public static unsafe int HighestOne2(ulong ul)
  73.         {
  74.             if ((long)ul <= 0)
  75.                 return (int)((ul >> 57) & 64) - 1;
  76.  
  77.             byte* pb = (byte*)&ul;          // CIL: 3A
  78.  
  79.             return highest_one_16[
  80.                 *(ushort*)((*(int*)(pb + 4) != 0 ?
  81.                     (*(ushort*)(pb += 6)) :
  82.                     (*(ushort*)(pb += 2))) != 0 ?
  83.                         pb : (pb -= 2))] + ((int)(pb - (long)&ul) << 3);
  84.         }
  85.  
  86.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  87.         public static unsafe int HighestOne3(ulong ul)
  88.         {
  89.             if ((long)ul <= 0)
  90.                 return (int)((ul >> 57) & 64) - 1;
  91.  
  92.             byte* pb = (byte*)&ul;          // CIL: 44
  93.  
  94.             return highest_one_16[
  95.                 *(ushort*)
  96.                     (*(int*)(pb + 4) != 0 ?
  97.                         (*(ushort*)(pb + 6) != 0 ?
  98.                              (pb += 6) :
  99.                              (pb += 4)) :
  100.                         (*(ushort*)(pb + 2) != 0 ?
  101.                              (pb += 2) :
  102.                              pb))
  103.                              ] + ((int)(pb - (long)&ul) << 3);
  104.         }
  105.  
  106.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  107.         public static unsafe int HighestOne4(ulong ul)
  108.         {
  109.             if ((long)ul <= 0)
  110.                 return (int)((ul >> 57) & 64) - 1;
  111.  
  112.             byte* pb = (byte*)&ul;          // CIL: 41
  113.  
  114.             int v;
  115.             return highest_one_16[
  116.                 *(*(int*)(pb + 4) != 0 ?
  117.                         (*(ushort*)(pb + 6) != 0 ?
  118.                              (ushort*)(pb + (v = 6)) :
  119.                              (ushort*)(pb + (v = 4))) :
  120.                         (*(ushort*)(pb + 2) != 0 ?
  121.                              (ushort*)(pb + (v = 2)) :
  122.                              (ushort*)(pb + (v = 0))))
  123.                              ] + (v << 3);
  124.         }
  125.  
  126.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  127.         public static unsafe int HighestOne5(ulong ul)
  128.         {
  129.             if ((long)ul <= 0)
  130.                 return (int)((ul >> 57) & 64) - 1;
  131.  
  132.             byte* pb = (byte*)&ul;          // CIL: 3C
  133.  
  134.             pb += (*(int*)(pb + 4) != 0 ?
  135.                 (*(ushort*)(pb + 6) != 0 ? 6 : 4) :
  136.                 (*(ushort*)(pb + 2) != 0 ? 2 : 0));
  137.  
  138.             return highest_one_16[*(ushort*)pb] + ((int)(pb - (long)&ul) << 3);
  139.         }
  140.  
  141.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  142.         public static unsafe int HighestOne6(ulong ul)
  143.         {
  144.             if ((long)ul <= 0)
  145.                 return (int)((ul >> 57) & 64) - 1;
  146.  
  147.             byte* pb = (byte*)&ul;          // CIL: 35
  148.  
  149.             return highest_one_16[*(ushort*)
  150.                                   (*(ushort*)(pb += (*(int*)(pb + 4) != 0 ? 6 : 2)) == 0 ?
  151.                                         pb -= 2 :
  152.                                         pb)] + ((int)(pb - (long)&ul) << 3);
  153.         }
  154.  
  155.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  156.         public static unsafe int HighestOne7(ulong ul)
  157.         {
  158.             if ((long)ul <= 0)
  159.                 return (int)((ul >> 57) & 64) - 1;
  160.  
  161.             byte* pb = (byte*)&ul;          // CIL: 34
  162.  
  163.             return ((int)(-(long)pb +
  164.                             (*(ushort*)(pb += *(int*)(pb + 4) != 0 ? 6 : 2) == 0 ?
  165.                                 pb -= 2 :
  166.                                 pb)) << 3) + highest_one_16[*(ushort*)pb];
  167.         }
  168.  
  169.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  170.         public static int HighestOne8(ulong ul)
  171.         {
  172.             if ((long)ul <= 0)
  173.                 return (int)((ul >> 57) & 64) - 1;
  174.  
  175.             int i, j;
  176.             return
  177.                 (i = highest_one_16[ul >> (j = 48)]) < 0 &&
  178.                 (i = highest_one_16[ul >> (j = 32)]) < 0 &&
  179.                 (i = highest_one_16[ul >> (j = 16)]) < 0 ?
  180.                     highest_one_16[(ushort)ul] :
  181.                     (i + j);
  182.         }
  183.     };
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement