ConstantHelper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. #nullable enable
  5. using System.Runtime.CompilerServices;
  6. namespace System.Numerics
  7. {
  8. internal class ConstantHelper
  9. {
  10. [MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
  11. public static byte GetByteWithAllBitsSet()
  12. {
  13. byte value = 0;
  14. unsafe
  15. {
  16. unchecked
  17. {
  18. *((byte*)&value) = (byte)0xff;
  19. }
  20. }
  21. return value;
  22. }
  23. [MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
  24. public static sbyte GetSByteWithAllBitsSet()
  25. {
  26. sbyte value = 0;
  27. unsafe
  28. {
  29. unchecked
  30. {
  31. *((sbyte*)&value) = (sbyte)0xff;
  32. }
  33. }
  34. return value;
  35. }
  36. [MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
  37. public static ushort GetUInt16WithAllBitsSet()
  38. {
  39. ushort value = 0;
  40. unsafe
  41. {
  42. unchecked
  43. {
  44. *((ushort*)&value) = (ushort)0xffff;
  45. }
  46. }
  47. return value;
  48. }
  49. [MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
  50. public static short GetInt16WithAllBitsSet()
  51. {
  52. short value = 0;
  53. unsafe
  54. {
  55. unchecked
  56. {
  57. *((short*)&value) = (short)0xffff;
  58. }
  59. }
  60. return value;
  61. }
  62. [MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
  63. public static uint GetUInt32WithAllBitsSet()
  64. {
  65. uint value = 0;
  66. unsafe
  67. {
  68. unchecked
  69. {
  70. *((uint*)&value) = (uint)0xffffffff;
  71. }
  72. }
  73. return value;
  74. }
  75. [MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
  76. public static int GetInt32WithAllBitsSet()
  77. {
  78. int value = 0;
  79. unsafe
  80. {
  81. unchecked
  82. {
  83. *((int*)&value) = (int)0xffffffff;
  84. }
  85. }
  86. return value;
  87. }
  88. [MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
  89. public static ulong GetUInt64WithAllBitsSet()
  90. {
  91. ulong value = 0;
  92. unsafe
  93. {
  94. unchecked
  95. {
  96. *((ulong*)&value) = (ulong)0xffffffffffffffff;
  97. }
  98. }
  99. return value;
  100. }
  101. [MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
  102. public static long GetInt64WithAllBitsSet()
  103. {
  104. long value = 0;
  105. unsafe
  106. {
  107. unchecked
  108. {
  109. *((long*)&value) = (long)0xffffffffffffffff;
  110. }
  111. }
  112. return value;
  113. }
  114. [MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
  115. public static float GetSingleWithAllBitsSet()
  116. {
  117. float value = 0;
  118. unsafe
  119. {
  120. unchecked
  121. {
  122. *((int*)&value) = (int)0xffffffff;
  123. }
  124. }
  125. return value;
  126. }
  127. [MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
  128. public static double GetDoubleWithAllBitsSet()
  129. {
  130. double value = 0;
  131. unsafe
  132. {
  133. unchecked
  134. {
  135. *((long*)&value) = (long)0xffffffffffffffff;
  136. }
  137. }
  138. return value;
  139. }
  140. }
  141. }