Integers.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 Integers
  15. {
  16. public const int NumBits = 32;
  17. public const int NumBytes = 4;
  18. private static readonly byte[] DeBruijnTZ = {
  19. 0x1F, 0x00, 0x1B, 0x01, 0x1C, 0x0D, 0x17, 0x02, 0x1D, 0x15, 0x13, 0x0E, 0x18, 0x10, 0x03, 0x07,
  20. 0x1E, 0x1A, 0x0C, 0x16, 0x14, 0x12, 0x0F, 0x06, 0x19, 0x0B, 0x11, 0x05, 0x0A, 0x04, 0x09, 0x08 };
  21. public static int HighestOneBit(int i)
  22. {
  23. return (int)HighestOneBit((uint)i);
  24. }
  25. [CLSCompliant(false)]
  26. public static uint HighestOneBit(uint i)
  27. {
  28. i |= i >> 1;
  29. i |= i >> 2;
  30. i |= i >> 4;
  31. i |= i >> 8;
  32. i |= i >> 16;
  33. return i - (i >> 1);
  34. }
  35. public static int LowestOneBit(int i)
  36. {
  37. return i & -i;
  38. }
  39. [CLSCompliant(false)]
  40. public static uint LowestOneBit(uint i)
  41. {
  42. return (uint)LowestOneBit((int)i);
  43. }
  44. public static int NumberOfLeadingZeros(int i)
  45. {
  46. #if NETCOREAPP3_0_OR_GREATER
  47. if (Lzcnt.IsSupported)
  48. {
  49. return (int)Lzcnt.LeadingZeroCount((uint)i);
  50. }
  51. #endif
  52. if (i <= 0)
  53. return (~i >> (31 - 5)) & (1 << 5);
  54. uint u = (uint)i;
  55. int n = 1;
  56. if (0 == (u >> 16)) { n += 16; u <<= 16; }
  57. if (0 == (u >> 24)) { n += 8; u <<= 8; }
  58. if (0 == (u >> 28)) { n += 4; u <<= 4; }
  59. if (0 == (u >> 30)) { n += 2; u <<= 2; }
  60. n -= (int)(u >> 31);
  61. return n;
  62. }
  63. public static int NumberOfTrailingZeros(int i)
  64. {
  65. #if NETCOREAPP3_0_OR_GREATER
  66. if (Bmi1.IsSupported)
  67. {
  68. return (int)Bmi1.TrailingZeroCount((uint)i);
  69. }
  70. #endif
  71. int n = DeBruijnTZ[(uint)((i & -i) * 0x0EF96A62) >> 27];
  72. int m = (((i & 0xFFFF) | (int)((uint)i >> 16)) - 1) >> 31;
  73. return n - m;
  74. }
  75. public static int Reverse(int i)
  76. {
  77. return (int)Reverse((uint)i);
  78. }
  79. [CLSCompliant(false)]
  80. public static uint Reverse(uint i)
  81. {
  82. i = Bits.BitPermuteStepSimple(i, 0x55555555U, 1);
  83. i = Bits.BitPermuteStepSimple(i, 0x33333333U, 2);
  84. i = Bits.BitPermuteStepSimple(i, 0x0F0F0F0FU, 4);
  85. return ReverseBytes(i);
  86. }
  87. public static int ReverseBytes(int 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 (int)ReverseBytes((uint)i);
  93. #endif
  94. }
  95. [CLSCompliant(false)]
  96. public static uint ReverseBytes(uint 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 & 0xFF00FF00U, 8) |
  102. RotateLeft(i & 0x00FF00FFU, 24);
  103. #endif
  104. }
  105. public static int RotateLeft(int i, int distance)
  106. {
  107. #if NETCOREAPP3_0_OR_GREATER
  108. return (int)BitOperations.RotateLeft((uint)i, distance);
  109. #else
  110. return (i << distance) | (int)((uint)i >> -distance);
  111. #endif
  112. }
  113. [CLSCompliant(false)]
  114. public static uint RotateLeft(uint i, int distance)
  115. {
  116. #if NETCOREAPP3_0_OR_GREATER
  117. return BitOperations.RotateLeft(i, distance);
  118. #else
  119. return (i << distance) | (i >> -distance);
  120. #endif
  121. }
  122. public static int RotateRight(int i, int distance)
  123. {
  124. #if NETCOREAPP3_0_OR_GREATER
  125. return (int)BitOperations.RotateRight((uint)i, distance);
  126. #else
  127. return (int)((uint)i >> distance) | (i << -distance);
  128. #endif
  129. }
  130. [CLSCompliant(false)]
  131. public static uint RotateRight(uint i, int distance)
  132. {
  133. #if NETCOREAPP3_0_OR_GREATER
  134. return BitOperations.RotateRight(i, distance);
  135. #else
  136. return (i >> distance) | (i << -distance);
  137. #endif
  138. }
  139. }
  140. }
  141. #pragma warning restore
  142. #endif