public static ulong Sqrt(ulong x) { if (x <= 1) { return(x); } int s = 32 - (BitOperator.GetNumberOfLeadingZero(x - 1) >> 1); ulong g0 = ((ulong)1) << s; ulong g1 = (g0 + (x >> s)) >> 1; while (g1 < g0) { g0 = g1; g1 = (g0 + x / g0) >> 1; } return(g0); }
public static uint Sqrt(uint x) { if (x <= 1) { return(x); } int s = (16 - (BitOperator.GetNumberOfLeadingZero(x - 1) >> 1)); uint g0 = (uint)(1 << s); uint g1 = ((g0 + (x >> s)) >> 1); while (g1 < g0) { g0 = g1; g1 = (g0 + x / g0) >> 1; } return(g0); }
public static ushort Sqrt(ushort x) { if (x <= 1) { return(x); } ushort s = (ushort)(4 - (BitOperator.GetNumberOfLeadingZero(x - 1) >> 1)); ushort g0 = (ushort)(1 << s); ushort g1 = (ushort)((g0 + (x >> s)) >> 1); while (g1 < g0) { g0 = g1; g1 = (ushort)((g0 + x / g0) >> 1); } return(g0); }
public static byte Sqrt(byte x) { if (x <= 1) { return(x); } byte s = (byte)(4 - (BitOperator.GetNumberOfLeadingZero(x - 1) >> 1)); byte g0 = (byte)(1 << s); byte g1 = (byte)((g0 + (x >> s)) >> 1); while (g1 < g0) { g0 = g1; g1 = (byte)((g0 + x / g0) >> 1); } return(g0); }
public static ulong Log2(ulong x) { return((ulong)(63 - BitOperator.GetNumberOfLeadingZero(x))); }
public static uint Log2(uint x) { return((uint)(31 - BitOperator.GetNumberOfLeadingZero(x))); }
public static ushort Log2(ushort x) { return((ushort)(15 - BitOperator.GetNumberOfLeadingZero(x))); }
public static byte Log2(byte x) { return((byte)(7 - BitOperator.GetNumberOfLeadingZero(x))); }