Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Security;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using System.Runtime.CompilerServices;
- using alib;
- using alib.Enumerable;
- using alib.Debugging;
- namespace ConsoleApp5
- {
- [SuppressUnmanagedCodeSecurity]
- public static class _test_B
- {
- static _test_B()
- {
- highest_one_16 = init_highest_1s();
- }
- [DebuggerBrowsable(DebuggerBrowsableState.Never)]
- static readonly sbyte[] highest_one_16;
- static sbyte[] init_highest_1s()
- {
- int i, c, j;
- sbyte n;
- var p = new sbyte[0x10000];
- p[0] = -1;
- j = 1;
- for (n = 0, c = 1; n < 16; c <<= 1, n++)
- for (i = 0; i < c; i++)
- p[j++] = n;
- return p;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe int HighestOne0(ulong ul)
- {
- if ((long)ul <= 0)
- return (int)((ul >> 57) & 64) - 1;
- byte* pb = (byte*)&ul; // CIL: 35
- int i = *(ushort*)(pb + 6) == 0 ?
- *(ushort*)(pb + 4) == 0 ?
- *(ushort*)(pb + 2) == 0 ? 0 : 2 : 4 : 6;
- return (i << 3) + highest_one_16[*(ushort*)(pb + i)];
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe int HighestOne1(ulong ul)
- {
- if ((long)ul <= 0)
- return (int)((ul >> 57) & 64) - 1;
- // this one is more sensitive to the data, performance varies more widely than the others, even more with the neg-check
- byte* pb = (byte*)&ul + 6; // CIL: 3B
- if (*(int*)(pb - 2) == 0)
- pb -= 4;
- if (*(ushort*)pb == 0)
- pb -= 2;
- return ((int)(pb - (long)&ul) << 3) + highest_one_16[*(ushort*)pb];
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe int HighestOne2(ulong ul)
- {
- if ((long)ul <= 0)
- return (int)((ul >> 57) & 64) - 1;
- byte* pb = (byte*)&ul; // CIL: 3A
- return highest_one_16[
- *(ushort*)((*(int*)(pb + 4) != 0 ?
- (*(ushort*)(pb += 6)) :
- (*(ushort*)(pb += 2))) != 0 ?
- pb : (pb -= 2))] + ((int)(pb - (long)&ul) << 3);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe int HighestOne3(ulong ul)
- {
- if ((long)ul <= 0)
- return (int)((ul >> 57) & 64) - 1;
- byte* pb = (byte*)&ul; // CIL: 44
- return highest_one_16[
- *(ushort*)
- (*(int*)(pb + 4) != 0 ?
- (*(ushort*)(pb + 6) != 0 ?
- (pb += 6) :
- (pb += 4)) :
- (*(ushort*)(pb + 2) != 0 ?
- (pb += 2) :
- pb))
- ] + ((int)(pb - (long)&ul) << 3);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe int HighestOne4(ulong ul)
- {
- if ((long)ul <= 0)
- return (int)((ul >> 57) & 64) - 1;
- byte* pb = (byte*)&ul; // CIL: 41
- int v;
- return highest_one_16[
- *(*(int*)(pb + 4) != 0 ?
- (*(ushort*)(pb + 6) != 0 ?
- (ushort*)(pb + (v = 6)) :
- (ushort*)(pb + (v = 4))) :
- (*(ushort*)(pb + 2) != 0 ?
- (ushort*)(pb + (v = 2)) :
- (ushort*)(pb + (v = 0))))
- ] + (v << 3);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe int HighestOne5(ulong ul)
- {
- if ((long)ul <= 0)
- return (int)((ul >> 57) & 64) - 1;
- byte* pb = (byte*)&ul; // CIL: 3C
- pb += (*(int*)(pb + 4) != 0 ?
- (*(ushort*)(pb + 6) != 0 ? 6 : 4) :
- (*(ushort*)(pb + 2) != 0 ? 2 : 0));
- return highest_one_16[*(ushort*)pb] + ((int)(pb - (long)&ul) << 3);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe int HighestOne6(ulong ul)
- {
- if ((long)ul <= 0)
- return (int)((ul >> 57) & 64) - 1;
- byte* pb = (byte*)&ul; // CIL: 35
- return highest_one_16[*(ushort*)
- (*(ushort*)(pb += (*(int*)(pb + 4) != 0 ? 6 : 2)) == 0 ?
- pb -= 2 :
- pb)] + ((int)(pb - (long)&ul) << 3);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe int HighestOne7(ulong ul)
- {
- if ((long)ul <= 0)
- return (int)((ul >> 57) & 64) - 1;
- byte* pb = (byte*)&ul; // CIL: 34
- return ((int)(-(long)pb +
- (*(ushort*)(pb += *(int*)(pb + 4) != 0 ? 6 : 2) == 0 ?
- pb -= 2 :
- pb)) << 3) + highest_one_16[*(ushort*)pb];
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int HighestOne8(ulong ul)
- {
- if ((long)ul <= 0)
- return (int)((ul >> 57) & 64) - 1;
- int i, j;
- return
- (i = highest_one_16[ul >> (j = 48)]) < 0 &&
- (i = highest_one_16[ul >> (j = 32)]) < 0 &&
- (i = highest_one_16[ul >> (j = 16)]) < 0 ?
- highest_one_16[(ushort)ul] :
- (i + j);
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement