Source code of Windows XP (NT5)
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.

205 lines
2.7 KiB

  1. #ifndef MISC_H_INCLUDED
  2. #define MISC_H_INCLUDED
  3. //
  4. // Endian-conversion routines
  5. //
  6. // begin_ntddk
  7. //
  8. // Bit Scan Reverse - 0x00010100 -> bit set at 16
  9. //
  10. __inline LONG
  11. NTAPI
  12. RtlGetMostSigBitSet(
  13. IN LONG Value
  14. )
  15. {
  16. ULONG Mask = 0x80000000;
  17. UINT Index = 31;
  18. while ((Value & Mask) == 0)
  19. {
  20. Index--;
  21. Value <<= 1;
  22. }
  23. return Index;
  24. }
  25. //
  26. // Bit Scan Forward - 0x00010100 -> bit set at 9
  27. //
  28. __inline LONG
  29. NTAPI
  30. RtlGetLeastSigBitSet(
  31. IN LONG Value
  32. )
  33. {
  34. ULONG Mask = 0x00000001;
  35. UINT Index = 0;
  36. while ((Value & Mask) == 0)
  37. {
  38. Index++;
  39. Value >>= 1;
  40. }
  41. return Index;
  42. }
  43. #if (defined(MIDL_PASS) || defined(__cplusplus) || !defined(_M_IX86))
  44. //
  45. // Short integer conversion - 0xABCD -> 0xCDAB
  46. //
  47. __inline SHORT
  48. NTAPI
  49. RtlConvertEndianShort(
  50. IN SHORT Value
  51. )
  52. {
  53. return (((Value) & 0xFF00) >> 8) | ((UCHAR)(Value) << 8);
  54. }
  55. //
  56. // Long integer conversion - 0x1234ABCD -> 0xCDAB3412
  57. //
  58. __inline LONG
  59. NTAPI
  60. RtlConvertEndianLong(
  61. IN LONG Value
  62. )
  63. {
  64. return (((Value) & 0xFF000000) >> 24) |
  65. (((Value) & 0x00FF0000) >> 8) |
  66. (((Value) & 0x0000FF00) << 8) |
  67. (((UCHAR)(Value)) << 24);
  68. }
  69. //
  70. // Bit Scan Reverse - 0x00010100 -> bit set at 16
  71. //
  72. __inline LONG
  73. NTAPI
  74. RtlGetMostSigBitSetEx(
  75. IN LONG Value
  76. )
  77. {
  78. ULONG Mask = 0x80000000;
  79. UINT Index = 31;
  80. while ((Value & Mask) == 0)
  81. {
  82. Index--;
  83. Value <<= 1;
  84. }
  85. return Index;
  86. }
  87. //
  88. // Bit Scan Forward - 0x00010100 -> bit set at 9
  89. //
  90. __inline LONG
  91. NTAPI
  92. RtlGetLeastSigBitSetEx(
  93. IN LONG Value
  94. )
  95. {
  96. ULONG Mask = 0x00000001;
  97. UINT Index = 0;
  98. while ((Value & Mask) == 0)
  99. {
  100. Index++;
  101. Value >>= 1;
  102. }
  103. return Index;
  104. }
  105. #else
  106. #pragma warning(disable:4035) // re-enable below
  107. //
  108. // Short integer conversion - 0xABCD -> 0xCDAB
  109. //
  110. __inline SHORT
  111. NTAPI
  112. RtlConvertEndianShort(
  113. IN SHORT Value
  114. )
  115. {
  116. __asm {
  117. mov ax, Value
  118. xchg ah, al
  119. }
  120. }
  121. //
  122. // Long integer conversion - 0x1234ABCD -> 0xCDAB3412
  123. //
  124. __inline LONG
  125. NTAPI
  126. RtlConvertEndianLong(
  127. IN LONG Value
  128. )
  129. {
  130. __asm {
  131. mov eax, Value
  132. bswap eax
  133. }
  134. }
  135. //
  136. // Bit Scan Reverse - 0x00010100 -> bit set at 16
  137. //
  138. __inline LONG
  139. NTAPI
  140. RtlGetMostSigBitSetEx(
  141. IN LONG Value
  142. )
  143. {
  144. __asm {
  145. bsr eax, Value
  146. }
  147. }
  148. //
  149. // Bit Scan Forward - 0x00010100 -> bit set at 9
  150. //
  151. __inline LONG
  152. NTAPI
  153. RtlGetLeastSigBitSetEx(
  154. IN LONG Value
  155. )
  156. {
  157. __asm {
  158. bsf eax, Value
  159. }
  160. }
  161. #pragma warning(default:4035)
  162. #endif
  163. // end_ntddk
  164. #endif // MISC_H_INCLUDED