Longs.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  5. using System.Buffers.Binary;
  6. #endif
  7. #if NETCOREAPP3_0_OR_GREATER
  8. using System.Numerics;
  9. using System.Runtime.Intrinsics.X86;
  10. #endif
  11. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  12. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities
  13. {
  14. public static class Longs
  15. {
  16. public const int NumBits = 64;
  17. public const int NumBytes = 8;
  18. private static readonly byte[] DeBruijnTZ = {
  19. 0x3F, 0x00, 0x01, 0x34, 0x02, 0x06, 0x35, 0x1A, 0x03, 0x25, 0x28, 0x07, 0x21, 0x36, 0x2F, 0x1B,
  20. 0x3D, 0x04, 0x26, 0x2D, 0x2B, 0x29, 0x15, 0x08, 0x17, 0x22, 0x3A, 0x37, 0x30, 0x11, 0x1C, 0x0A,
  21. 0x3E, 0x33, 0x05, 0x19, 0x24, 0x27, 0x20, 0x2E, 0x3C, 0x2C, 0x2A, 0x14, 0x16, 0x39, 0x10, 0x09,
  22. 0x32, 0x18, 0x23, 0x1F, 0x3B, 0x13, 0x38, 0x0F, 0x31, 0x1E, 0x12, 0x0E, 0x1D, 0x0D, 0x0C, 0x0B };
  23. public static long HighestOneBit(long i)
  24. {
  25. return (long)HighestOneBit((ulong)i);
  26. }
  27. [CLSCompliant(false)]
  28. public static ulong HighestOneBit(ulong i)
  29. {
  30. i |= i >> 1;
  31. i |= i >> 2;
  32. i |= i >> 4;
  33. i |= i >> 8;
  34. i |= i >> 16;
  35. i |= i >> 32;
  36. return i - (i >> 1);
  37. }
  38. public static long LowestOneBit(long i)
  39. {
  40. return i & -i;
  41. }
  42. [CLSCompliant(false)]
  43. public static ulong LowestOneBit(ulong i)
  44. {
  45. return (ulong)LowestOneBit((long)i);
  46. }
  47. public static int NumberOfLeadingZeros(long i)
  48. {
  49. #if NETCOREAPP3_0_OR_GREATER
  50. if (Lzcnt.X64.IsSupported)
  51. {
  52. return (int)Lzcnt.X64.LeadingZeroCount((ulong)i);
  53. }
  54. #endif
  55. int x = (int)(i >> 32), n = 0;
  56. if (x == 0)
  57. {
  58. n = 32;
  59. x = (int)i;
  60. }
  61. return n + Integers.NumberOfLeadingZeros(x);
  62. }
  63. public static int NumberOfTrailingZeros(long i)
  64. {
  65. #if NETCOREAPP3_0_OR_GREATER
  66. if (Bmi1.X64.IsSupported)
  67. {
  68. return (int)Bmi1.X64.TrailingZeroCount((ulong)i);
  69. }
  70. #endif
  71. int n = DeBruijnTZ[(uint)((ulong)((i & -i) * 0x045FBAC7992A70DAL) >> 58)];
  72. long m = (((i & 0xFFFFFFFFL) | (long)((ulong)i >> 32)) - 1L) >> 63;
  73. return n - (int)m;
  74. }
  75. public static long Reverse(long i)
  76. {
  77. return (long)Reverse((ulong)i);
  78. }
  79. [CLSCompliant(false)]
  80. public static ulong Reverse(ulong i)
  81. {
  82. i = Bits.BitPermuteStepSimple(i, 0x5555555555555555UL, 1);
  83. i = Bits.BitPermuteStepSimple(i, 0x3333333333333333UL, 2);
  84. i = Bits.BitPermuteStepSimple(i, 0x0F0F0F0F0F0F0F0FUL, 4);
  85. return ReverseBytes(i);
  86. }
  87. public static long ReverseBytes(long i)
  88. {
  89. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  90. return BinaryPrimitives.ReverseEndianness(i);
  91. #else
  92. return (long)ReverseBytes((ulong)i);
  93. #endif
  94. }
  95. [CLSCompliant(false)]
  96. public static ulong ReverseBytes(ulong i)
  97. {
  98. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  99. return BinaryPrimitives.ReverseEndianness(i);
  100. #else
  101. return RotateLeft(i & 0xFF000000FF000000UL, 8) |
  102. RotateLeft(i & 0x00FF000000FF0000UL, 24) |
  103. RotateLeft(i & 0x0000FF000000FF00UL, 40) |
  104. RotateLeft(i & 0x000000FF000000FFUL, 56);
  105. #endif
  106. }
  107. public static long RotateLeft(long i, int distance)
  108. {
  109. #if NETCOREAPP3_0_OR_GREATER
  110. return (long)BitOperations.RotateLeft((ulong)i, distance);
  111. #else
  112. return (i << distance) | (long)((ulong)i >> -distance);
  113. #endif
  114. }
  115. [CLSCompliant(false)]
  116. public static ulong RotateLeft(ulong i, int distance)
  117. {
  118. #if NETCOREAPP3_0_OR_GREATER
  119. return BitOperations.RotateLeft(i, distance);
  120. #else
  121. return (i << distance) | (i >> -distance);
  122. #endif
  123. }
  124. public static long RotateRight(long i, int distance)
  125. {
  126. #if NETCOREAPP3_0_OR_GREATER
  127. return (long)BitOperations.RotateRight((ulong)i, distance);
  128. #else
  129. return (long)((ulong)i >> distance) | (i << -distance);
  130. #endif
  131. }
  132. [CLSCompliant(false)]
  133. public static ulong RotateRight(ulong i, int distance)
  134. {
  135. #if NETCOREAPP3_0_OR_GREATER
  136. return BitOperations.RotateRight(i, distance);
  137. #else
  138. return (i >> distance) | (i << -distance);
  139. #endif
  140. }
  141. }
  142. }
  143. #pragma warning restore
  144. #endif