ConstantHelper.cs 3.9 KB

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