暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Popcnt.cs 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Diagnostics;
  2. namespace Unity.Burst.Intrinsics
  3. {
  4. public unsafe static partial class X86
  5. {
  6. /// <summary>
  7. /// popcnt intrinsics
  8. /// </summary>
  9. public static class Popcnt
  10. {
  11. /// <summary>
  12. /// Evaluates to true at compile time if popcnt intrinsics are supported.
  13. ///
  14. /// Burst ties popcnt support to SSE4.2 support to simplify feature sets to support.
  15. /// </summary>
  16. public static bool IsPopcntSupported { get { return Sse4_2.IsSse42Supported; } }
  17. /// <summary>
  18. /// Count the number of bits set to 1 in unsigned 32-bit integer a, and return that count in dst.
  19. /// </summary>
  20. /// <remarks>
  21. /// **** popcnt r32, r32
  22. /// </remarks>
  23. /// <param name="v">Integer to be counted in</param>
  24. /// <returns>Count</returns>
  25. [DebuggerStepThrough]
  26. public static int popcnt_u32(uint v)
  27. {
  28. int result = 0;
  29. uint mask = 0x80000000u;
  30. while (mask != 0)
  31. {
  32. result += ((v & mask) != 0) ? 1 : 0;
  33. mask >>= 1;
  34. }
  35. return result;
  36. }
  37. /// <summary>
  38. /// Count the number of bits set to 1 in unsigned 64-bit integer a, and return that count in dst.
  39. /// </summary>
  40. /// <remarks>
  41. /// **** popcnt r64, r64
  42. /// </remarks>
  43. /// <param name="v">Integer to be counted in</param>
  44. /// <returns>Count</returns>
  45. [DebuggerStepThrough]
  46. public static int popcnt_u64(ulong v)
  47. {
  48. int result = 0;
  49. ulong mask = 0x8000000000000000u;
  50. while (mask != 0)
  51. {
  52. result += ((v & mask) != 0) ? 1 : 0;
  53. mask >>= 1;
  54. }
  55. return result;
  56. }
  57. }
  58. }
  59. }